spinlock.c 21 KB

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