spinlock.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1998 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* Internal locks */
  15. #include <errno.h>
  16. #include <sched.h>
  17. #include <time.h>
  18. #include <stdlib.h>
  19. #include <limits.h>
  20. #include "pthread.h"
  21. #include "internals.h"
  22. #include "spinlock.h"
  23. #include "restart.h"
  24. libpthread_hidden_proto(nanosleep)
  25. static void __pthread_acquire(int * spinlock);
  26. static __inline__ void __pthread_release(int * spinlock)
  27. {
  28. WRITE_MEMORY_BARRIER();
  29. *spinlock = __LT_SPINLOCK_INIT;
  30. __asm__ __volatile__ ("" : "=m" (*spinlock) : "m" (*spinlock));
  31. }
  32. /* The status field of a spinlock is a pointer whose least significant
  33. bit is a locked flag.
  34. Thus the field values have the following meanings:
  35. status == 0: spinlock is free
  36. status == 1: spinlock is taken; no thread is waiting on it
  37. (status & 1) == 1: spinlock is taken and (status & ~1L) is a
  38. pointer to the first waiting thread; other
  39. waiting threads are linked via the p_nextlock
  40. field.
  41. (status & 1) == 0: same as above, but spinlock is not taken.
  42. The waiting list is not sorted by priority order.
  43. Actually, we always insert at top of list (sole insertion mode
  44. that can be performed without locking).
  45. For __pthread_unlock, we perform a linear search in the list
  46. to find the highest-priority, oldest waiting thread.
  47. This is safe because there are no concurrent __pthread_unlock
  48. operations -- only the thread that locked the mutex can unlock it. */
  49. void internal_function __pthread_lock(struct _pthread_fastlock * lock,
  50. pthread_descr self)
  51. {
  52. #if defined HAS_COMPARE_AND_SWAP
  53. long oldstatus, newstatus;
  54. int successful_seizure, spurious_wakeup_count;
  55. #endif
  56. #if defined TEST_FOR_COMPARE_AND_SWAP
  57. if (!__pthread_has_cas)
  58. #endif
  59. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  60. {
  61. __pthread_acquire(&lock->__spinlock);
  62. return;
  63. }
  64. #endif
  65. #if defined HAS_COMPARE_AND_SWAP
  66. /* First try it without preparation. Maybe it's a completely
  67. uncontested lock. */
  68. if (lock->__status == 0 && __compare_and_swap (&lock->__status, 0, 1))
  69. return;
  70. spurious_wakeup_count = 0;
  71. /* On SMP, try spinning to get the lock. */
  72. #if 0
  73. if (__pthread_smp_kernel) {
  74. int spin_count;
  75. int max_count = lock->__spinlock * 2 + 10;
  76. if (max_count > MAX_ADAPTIVE_SPIN_COUNT)
  77. max_count = MAX_ADAPTIVE_SPIN_COUNT;
  78. for (spin_count = 0; spin_count < max_count; spin_count++) {
  79. if (((oldstatus = lock->__status) & 1) == 0) {
  80. if(__compare_and_swap(&lock->__status, oldstatus, oldstatus | 1))
  81. {
  82. if (spin_count)
  83. lock->__spinlock += (spin_count - lock->__spinlock) / 8;
  84. READ_MEMORY_BARRIER();
  85. return;
  86. }
  87. }
  88. #ifdef BUSY_WAIT_NOP
  89. BUSY_WAIT_NOP;
  90. #endif
  91. __asm__ __volatile__ ("" : "=m" (lock->__status) : "m" (lock->__status));
  92. }
  93. lock->__spinlock += (spin_count - lock->__spinlock) / 8;
  94. }
  95. #endif
  96. again:
  97. /* No luck, try once more or suspend. */
  98. do {
  99. oldstatus = lock->__status;
  100. successful_seizure = 0;
  101. if ((oldstatus & 1) == 0) {
  102. newstatus = oldstatus | 1;
  103. successful_seizure = 1;
  104. } else {
  105. if (self == NULL)
  106. self = thread_self();
  107. newstatus = (long) self | 1;
  108. }
  109. if (self != NULL) {
  110. THREAD_SETMEM(self, p_nextlock, (pthread_descr) (oldstatus));
  111. /* Make sure the store in p_nextlock completes before performing
  112. the compare-and-swap */
  113. MEMORY_BARRIER();
  114. }
  115. } while(! __compare_and_swap(&lock->__status, oldstatus, newstatus));
  116. /* Suspend with guard against spurious wakeup.
  117. This can happen in pthread_cond_timedwait_relative, when the thread
  118. wakes up due to timeout and is still on the condvar queue, and then
  119. locks the queue to remove itself. At that point it may still be on the
  120. queue, and may be resumed by a condition signal. */
  121. if (!successful_seizure) {
  122. for (;;) {
  123. suspend(self);
  124. if (self->p_nextlock != NULL) {
  125. /* Count resumes that don't belong to us. */
  126. spurious_wakeup_count++;
  127. continue;
  128. }
  129. break;
  130. }
  131. goto again;
  132. }
  133. /* Put back any resumes we caught that don't belong to us. */
  134. while (spurious_wakeup_count--)
  135. restart(self);
  136. READ_MEMORY_BARRIER();
  137. #endif
  138. }
  139. int __pthread_unlock(struct _pthread_fastlock * lock)
  140. {
  141. #if defined HAS_COMPARE_AND_SWAP
  142. long oldstatus;
  143. pthread_descr thr, * ptr, * maxptr;
  144. int maxprio;
  145. #endif
  146. #if defined TEST_FOR_COMPARE_AND_SWAP
  147. if (!__pthread_has_cas)
  148. #endif
  149. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  150. {
  151. __pthread_release(&lock->__spinlock);
  152. return 0;
  153. }
  154. #endif
  155. #if defined HAS_COMPARE_AND_SWAP
  156. WRITE_MEMORY_BARRIER();
  157. again:
  158. oldstatus = lock->__status;
  159. if (oldstatus == 0 || oldstatus == 1) {
  160. /* No threads are waiting for this lock. Please note that we also
  161. enter this case if the lock is not taken at all. If this wouldn't
  162. be done here we would crash further down. */
  163. if (! __compare_and_swap_with_release_semantics(&lock->__status,
  164. oldstatus, 0))
  165. goto again;
  166. return 0;
  167. }
  168. /* Find thread in waiting queue with maximal priority */
  169. ptr = (pthread_descr *) &lock->__status;
  170. thr = (pthread_descr) (oldstatus & ~1L);
  171. maxprio = 0;
  172. maxptr = ptr;
  173. /* Before we iterate over the wait queue, we need to execute
  174. a read barrier, otherwise we may read stale contents of nodes that may
  175. just have been inserted by other processors. One read barrier is enough to
  176. ensure we have a stable list; we don't need one for each pointer chase
  177. through the list, because we are the owner of the lock; other threads
  178. can only add nodes at the front; if a front node is consistent,
  179. the ones behind it must also be. */
  180. READ_MEMORY_BARRIER();
  181. while (thr != 0) {
  182. if (thr->p_priority >= maxprio) {
  183. maxptr = ptr;
  184. maxprio = thr->p_priority;
  185. }
  186. ptr = &(thr->p_nextlock);
  187. thr = (pthread_descr)((long)(thr->p_nextlock) & ~1L);
  188. }
  189. /* Remove max prio thread from waiting list. */
  190. if (maxptr == (pthread_descr *) &lock->__status) {
  191. /* If max prio thread is at head, remove it with compare-and-swap
  192. to guard against concurrent lock operation. This removal
  193. also has the side effect of marking the lock as released
  194. because the new status comes from thr->p_nextlock whose
  195. least significant bit is clear. */
  196. thr = (pthread_descr) (oldstatus & ~1L);
  197. if (! __compare_and_swap_with_release_semantics
  198. (&lock->__status, oldstatus, (long)(thr->p_nextlock) & ~1L))
  199. goto again;
  200. } else {
  201. /* No risk of concurrent access, remove max prio thread normally.
  202. But in this case we must also flip the least significant bit
  203. of the status to mark the lock as released. */
  204. thr = (pthread_descr)((long)*maxptr & ~1L);
  205. *maxptr = thr->p_nextlock;
  206. /* Ensure deletion from linked list completes before we
  207. release the lock. */
  208. WRITE_MEMORY_BARRIER();
  209. do {
  210. oldstatus = lock->__status;
  211. } while (!__compare_and_swap_with_release_semantics(&lock->__status,
  212. oldstatus, oldstatus & ~1L));
  213. }
  214. /* Wake up the selected waiting thread. Woken thread can check
  215. its own p_nextlock field for NULL to detect that it has been removed. No
  216. barrier is needed here, since restart() and suspend() take
  217. care of memory synchronization. */
  218. thr->p_nextlock = NULL;
  219. restart(thr);
  220. return 0;
  221. #endif
  222. }
  223. /*
  224. * Alternate fastlocks do not queue threads directly. Instead, they queue
  225. * these wait queue node structures. When a timed wait wakes up due to
  226. * a timeout, it can leave its wait node in the queue (because there
  227. * is no safe way to remove from the quue). Some other thread will
  228. * deallocate the abandoned node.
  229. */
  230. struct wait_node {
  231. struct wait_node *next; /* Next node in null terminated linked list */
  232. pthread_descr thr; /* The thread waiting with this node */
  233. int abandoned; /* Atomic flag */
  234. };
  235. static long wait_node_free_list;
  236. static int wait_node_free_list_spinlock;
  237. /* Allocate a new node from the head of the free list using an atomic
  238. operation, or else using malloc if that list is empty. A fundamental
  239. assumption here is that we can safely access wait_node_free_list->next.
  240. That's because we never free nodes once we allocate them, so a pointer to a
  241. node remains valid indefinitely. */
  242. static struct wait_node *wait_node_alloc(void)
  243. {
  244. struct wait_node *new_node = 0;
  245. __pthread_acquire(&wait_node_free_list_spinlock);
  246. if (wait_node_free_list != 0) {
  247. new_node = (struct wait_node *) wait_node_free_list;
  248. wait_node_free_list = (long) new_node->next;
  249. }
  250. WRITE_MEMORY_BARRIER();
  251. __pthread_release(&wait_node_free_list_spinlock);
  252. if (new_node == 0)
  253. return malloc(sizeof *wait_node_alloc());
  254. return new_node;
  255. }
  256. /* Return a node to the head of the free list using an atomic
  257. operation. */
  258. static void wait_node_free(struct wait_node *wn)
  259. {
  260. __pthread_acquire(&wait_node_free_list_spinlock);
  261. wn->next = (struct wait_node *) wait_node_free_list;
  262. wait_node_free_list = (long) wn;
  263. WRITE_MEMORY_BARRIER();
  264. __pthread_release(&wait_node_free_list_spinlock);
  265. return;
  266. }
  267. #if defined HAS_COMPARE_AND_SWAP
  268. /* Remove a wait node from the specified queue. It is assumed
  269. that the removal takes place concurrently with only atomic insertions at the
  270. head of the queue. */
  271. static void wait_node_dequeue(struct wait_node **pp_head,
  272. struct wait_node **pp_node,
  273. struct wait_node *p_node)
  274. {
  275. /* If the node is being deleted from the head of the
  276. list, it must be deleted using atomic compare-and-swap.
  277. Otherwise it can be deleted in the straightforward way. */
  278. if (pp_node == pp_head) {
  279. /* We don't need a read barrier between these next two loads,
  280. because it is assumed that the caller has already ensured
  281. the stability of *p_node with respect to p_node. */
  282. long oldvalue = (long) p_node;
  283. long newvalue = (long) p_node->next;
  284. if (__compare_and_swap((long *) pp_node, oldvalue, newvalue))
  285. return;
  286. /* Oops! Compare and swap failed, which means the node is
  287. no longer first. We delete it using the ordinary method. But we don't
  288. know the identity of the node which now holds the pointer to the node
  289. being deleted, so we must search from the beginning. */
  290. for (pp_node = pp_head; p_node != *pp_node; ) {
  291. pp_node = &(*pp_node)->next;
  292. READ_MEMORY_BARRIER(); /* Stabilize *pp_node for next iteration. */
  293. }
  294. }
  295. *pp_node = p_node->next;
  296. return;
  297. }
  298. #endif
  299. void __pthread_alt_lock(struct _pthread_fastlock * lock,
  300. pthread_descr self)
  301. {
  302. #if defined HAS_COMPARE_AND_SWAP
  303. long oldstatus, newstatus;
  304. #endif
  305. struct wait_node wait_node;
  306. #if defined TEST_FOR_COMPARE_AND_SWAP
  307. if (!__pthread_has_cas)
  308. #endif
  309. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  310. {
  311. int suspend_needed = 0;
  312. __pthread_acquire(&lock->__spinlock);
  313. if (lock->__status == 0)
  314. lock->__status = 1;
  315. else {
  316. if (self == NULL)
  317. self = thread_self();
  318. wait_node.abandoned = 0;
  319. wait_node.next = (struct wait_node *) lock->__status;
  320. wait_node.thr = self;
  321. lock->__status = (long) &wait_node;
  322. suspend_needed = 1;
  323. }
  324. __pthread_release(&lock->__spinlock);
  325. if (suspend_needed)
  326. suspend (self);
  327. return;
  328. }
  329. #endif
  330. #if defined HAS_COMPARE_AND_SWAP
  331. do {
  332. oldstatus = lock->__status;
  333. if (oldstatus == 0) {
  334. newstatus = 1;
  335. } else {
  336. if (self == NULL)
  337. self = thread_self();
  338. wait_node.thr = self;
  339. newstatus = (long) &wait_node;
  340. }
  341. wait_node.abandoned = 0;
  342. wait_node.next = (struct wait_node *) oldstatus;
  343. /* Make sure the store in wait_node.next completes before performing
  344. the compare-and-swap */
  345. MEMORY_BARRIER();
  346. } while(! __compare_and_swap(&lock->__status, oldstatus, newstatus));
  347. /* Suspend. Note that unlike in __pthread_lock, we don't worry
  348. here about spurious wakeup. That's because this lock is not
  349. used in situations where that can happen; the restart can
  350. only come from the previous lock owner. */
  351. if (oldstatus != 0)
  352. suspend(self);
  353. READ_MEMORY_BARRIER();
  354. #endif
  355. }
  356. /* Timed-out lock operation; returns 0 to indicate timeout. */
  357. int __pthread_alt_timedlock(struct _pthread_fastlock * lock,
  358. pthread_descr self, const struct timespec *abstime)
  359. {
  360. long oldstatus = 0;
  361. #if defined HAS_COMPARE_AND_SWAP
  362. long newstatus;
  363. #endif
  364. struct wait_node *p_wait_node = wait_node_alloc();
  365. /* Out of memory, just give up and do ordinary lock. */
  366. if (p_wait_node == 0) {
  367. __pthread_alt_lock(lock, self);
  368. return 1;
  369. }
  370. #if defined TEST_FOR_COMPARE_AND_SWAP
  371. if (!__pthread_has_cas)
  372. #endif
  373. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  374. {
  375. __pthread_acquire(&lock->__spinlock);
  376. if (lock->__status == 0)
  377. lock->__status = 1;
  378. else {
  379. if (self == NULL)
  380. self = thread_self();
  381. p_wait_node->abandoned = 0;
  382. p_wait_node->next = (struct wait_node *) lock->__status;
  383. p_wait_node->thr = self;
  384. lock->__status = (long) p_wait_node;
  385. oldstatus = 1; /* force suspend */
  386. }
  387. __pthread_release(&lock->__spinlock);
  388. goto suspend;
  389. }
  390. #endif
  391. #if defined HAS_COMPARE_AND_SWAP
  392. do {
  393. oldstatus = lock->__status;
  394. if (oldstatus == 0) {
  395. newstatus = 1;
  396. } else {
  397. if (self == NULL)
  398. self = thread_self();
  399. p_wait_node->thr = self;
  400. newstatus = (long) p_wait_node;
  401. }
  402. p_wait_node->abandoned = 0;
  403. p_wait_node->next = (struct wait_node *) oldstatus;
  404. /* Make sure the store in wait_node.next completes before performing
  405. the compare-and-swap */
  406. MEMORY_BARRIER();
  407. } while(! __compare_and_swap(&lock->__status, oldstatus, newstatus));
  408. #endif
  409. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  410. suspend:
  411. #endif
  412. /* If we did not get the lock, do a timed suspend. If we wake up due
  413. to a timeout, then there is a race; the old lock owner may try
  414. to remove us from the queue. This race is resolved by us and the owner
  415. doing an atomic testandset() to change the state of the wait node from 0
  416. to 1. If we succeed, then it's a timeout and we abandon the node in the
  417. queue. If we fail, it means the owner gave us the lock. */
  418. if (oldstatus != 0) {
  419. if (timedsuspend(self, abstime) == 0) {
  420. if (!testandset(&p_wait_node->abandoned))
  421. return 0; /* Timeout! */
  422. /* Eat oustanding resume from owner, otherwise wait_node_free() below
  423. will race with owner's wait_node_dequeue(). */
  424. suspend(self);
  425. }
  426. }
  427. wait_node_free(p_wait_node);
  428. READ_MEMORY_BARRIER();
  429. return 1; /* Got the lock! */
  430. }
  431. void __pthread_alt_unlock(struct _pthread_fastlock *lock)
  432. {
  433. struct wait_node *p_node, **pp_node, *p_max_prio, **pp_max_prio;
  434. struct wait_node ** const pp_head = (struct wait_node **) &lock->__status;
  435. int maxprio;
  436. WRITE_MEMORY_BARRIER();
  437. #if defined TEST_FOR_COMPARE_AND_SWAP
  438. if (!__pthread_has_cas)
  439. #endif
  440. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  441. {
  442. __pthread_acquire(&lock->__spinlock);
  443. }
  444. #endif
  445. while (1) {
  446. /* If no threads are waiting for this lock, try to just
  447. atomically release it. */
  448. #if defined TEST_FOR_COMPARE_AND_SWAP
  449. if (!__pthread_has_cas)
  450. #endif
  451. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  452. {
  453. if (lock->__status == 0 || lock->__status == 1) {
  454. lock->__status = 0;
  455. break;
  456. }
  457. }
  458. #endif
  459. #if defined TEST_FOR_COMPARE_AND_SWAP
  460. else
  461. #endif
  462. #if defined HAS_COMPARE_AND_SWAP
  463. {
  464. long oldstatus = lock->__status;
  465. if (oldstatus == 0 || oldstatus == 1) {
  466. if (__compare_and_swap_with_release_semantics (&lock->__status, oldstatus, 0))
  467. break;
  468. else
  469. continue;
  470. }
  471. }
  472. #endif
  473. /* Process the entire queue of wait nodes. Remove all abandoned
  474. wait nodes and put them into the global free queue, and
  475. remember the one unabandoned node which refers to the thread
  476. having the highest priority. */
  477. pp_max_prio = pp_node = pp_head;
  478. p_max_prio = p_node = *pp_head;
  479. maxprio = INT_MIN;
  480. READ_MEMORY_BARRIER(); /* Prevent access to stale data through p_node */
  481. while (p_node != (struct wait_node *) 1) {
  482. int prio;
  483. if (p_node->abandoned) {
  484. /* Remove abandoned node. */
  485. #if defined TEST_FOR_COMPARE_AND_SWAP
  486. if (!__pthread_has_cas)
  487. #endif
  488. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  489. *pp_node = p_node->next;
  490. #endif
  491. #if defined TEST_FOR_COMPARE_AND_SWAP
  492. else
  493. #endif
  494. #if defined HAS_COMPARE_AND_SWAP
  495. wait_node_dequeue(pp_head, pp_node, p_node);
  496. #endif
  497. wait_node_free(p_node);
  498. /* Note that the next assignment may take us to the beginning
  499. of the queue, to newly inserted nodes, if pp_node == pp_head.
  500. In that case we need a memory barrier to stabilize the first of
  501. these new nodes. */
  502. p_node = *pp_node;
  503. if (pp_node == pp_head)
  504. READ_MEMORY_BARRIER(); /* No stale reads through p_node */
  505. continue;
  506. } else if ((prio = p_node->thr->p_priority) >= maxprio) {
  507. /* Otherwise remember it if its thread has a higher or equal priority
  508. compared to that of any node seen thus far. */
  509. maxprio = prio;
  510. pp_max_prio = pp_node;
  511. p_max_prio = p_node;
  512. }
  513. /* This canno6 jump backward in the list, so no further read
  514. barrier is needed. */
  515. pp_node = &p_node->next;
  516. p_node = *pp_node;
  517. }
  518. /* If all threads abandoned, go back to top */
  519. if (maxprio == INT_MIN)
  520. continue;
  521. /* Now we want to to remove the max priority thread's wait node from
  522. the list. Before we can do this, we must atomically try to change the
  523. node's abandon state from zero to nonzero. If we succeed, that means we
  524. have the node that we will wake up. If we failed, then it means the
  525. thread timed out and abandoned the node in which case we repeat the
  526. whole unlock operation. */
  527. if (!testandset(&p_max_prio->abandoned)) {
  528. #if defined TEST_FOR_COMPARE_AND_SWAP
  529. if (!__pthread_has_cas)
  530. #endif
  531. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  532. *pp_max_prio = p_max_prio->next;
  533. #endif
  534. #if defined TEST_FOR_COMPARE_AND_SWAP
  535. else
  536. #endif
  537. #if defined HAS_COMPARE_AND_SWAP
  538. wait_node_dequeue(pp_head, pp_max_prio, p_max_prio);
  539. #endif
  540. restart(p_max_prio->thr);
  541. break;
  542. }
  543. }
  544. #if defined TEST_FOR_COMPARE_AND_SWAP
  545. if (!__pthread_has_cas)
  546. #endif
  547. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  548. {
  549. __pthread_release(&lock->__spinlock);
  550. }
  551. #endif
  552. }
  553. /* Compare-and-swap emulation with a spinlock */
  554. #ifdef TEST_FOR_COMPARE_AND_SWAP
  555. int __pthread_has_cas = 0;
  556. #endif
  557. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  558. int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
  559. int * spinlock)
  560. {
  561. int res;
  562. __pthread_acquire(spinlock);
  563. if (*ptr == oldval) {
  564. *ptr = newval; res = 1;
  565. } else {
  566. res = 0;
  567. }
  568. __pthread_release(spinlock);
  569. return res;
  570. }
  571. #endif
  572. /* The retry strategy is as follows:
  573. - We test and set the spinlock MAX_SPIN_COUNT times, calling
  574. sched_yield() each time. This gives ample opportunity for other
  575. threads with priority >= our priority to make progress and
  576. release the spinlock.
  577. - If a thread with priority < our priority owns the spinlock,
  578. calling sched_yield() repeatedly is useless, since we're preventing
  579. the owning thread from making progress and releasing the spinlock.
  580. So, after MAX_SPIN_LOCK attemps, we suspend the calling thread
  581. using nanosleep(). This again should give time to the owning thread
  582. for releasing the spinlock.
  583. Notice that the nanosleep() interval must not be too small,
  584. since the kernel does busy-waiting for short intervals in a realtime
  585. process (!). The smallest duration that guarantees thread
  586. suspension is currently 2ms.
  587. - When nanosleep() returns, we try again, doing MAX_SPIN_COUNT
  588. sched_yield(), then sleeping again if needed. */
  589. static void __pthread_acquire(int * spinlock)
  590. {
  591. int cnt = 0;
  592. struct timespec tm;
  593. READ_MEMORY_BARRIER();
  594. while (testandset(spinlock)) {
  595. if (cnt < MAX_SPIN_COUNT) {
  596. sched_yield();
  597. cnt++;
  598. } else {
  599. tm.tv_sec = 0;
  600. tm.tv_nsec = SPIN_SLEEP_DURATION;
  601. nanosleep(&tm, NULL);
  602. cnt = 0;
  603. }
  604. }
  605. }