spinlock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "pthread.h"
  19. #include "internals.h"
  20. #include "spinlock.h"
  21. #include "restart.h"
  22. /* The status field of a fastlock has the following meaning:
  23. 0: fastlock is free
  24. 1: fastlock is taken, no thread is waiting on it
  25. ADDR: fastlock is taken, ADDR is address of thread descriptor for
  26. first waiting thread, other waiting threads are linked via
  27. their p_nextlock field.
  28. The waiting list is not sorted by priority order.
  29. Actually, we always insert at top of list (sole insertion mode
  30. that can be performed without locking).
  31. For __pthread_unlock, we perform a linear search in the list
  32. to find the highest-priority, oldest waiting thread.
  33. This is safe because there are no concurrent __pthread_unlock
  34. operations -- only the thread that locked the mutex can unlock it. */
  35. void internal_function __pthread_lock(struct _pthread_fastlock * lock,
  36. pthread_descr self)
  37. {
  38. long oldstatus, newstatus;
  39. int spurious_wakeup_count = 0;
  40. do {
  41. oldstatus = lock->__status;
  42. if (oldstatus == 0) {
  43. newstatus = 1;
  44. } else {
  45. if (self == NULL)
  46. self = thread_self();
  47. newstatus = (long) self;
  48. }
  49. if (self != NULL) {
  50. ASSERT(self->p_nextlock == NULL);
  51. THREAD_SETMEM(self, p_nextlock, (pthread_descr) oldstatus);
  52. }
  53. } while(! compare_and_swap(&lock->__status, oldstatus, newstatus,
  54. &lock->__spinlock));
  55. /* Suspend with guard against spurious wakeup.
  56. This can happen in pthread_cond_timedwait_relative, when the thread
  57. wakes up due to timeout and is still on the condvar queue, and then
  58. locks the queue to remove itself. At that point it may still be on the
  59. queue, and may be resumed by a condition signal. */
  60. if (oldstatus != 0) {
  61. for (;;) {
  62. suspend(self);
  63. if (self->p_nextlock != NULL) {
  64. /* Count resumes that don't belong to us. */
  65. spurious_wakeup_count++;
  66. continue;
  67. }
  68. break;
  69. }
  70. }
  71. /* Put back any resumes we caught that don't belong to us. */
  72. while (spurious_wakeup_count--)
  73. restart(self);
  74. }
  75. void internal_function __pthread_unlock(struct _pthread_fastlock * lock)
  76. {
  77. long oldstatus;
  78. pthread_descr thr, * ptr, * maxptr;
  79. int maxprio;
  80. again:
  81. oldstatus = lock->__status;
  82. if (oldstatus == 0 || oldstatus == 1) {
  83. /* No threads are waiting for this lock. Please note that we also
  84. enter this case if the lock is not taken at all. If this wouldn't
  85. be done here we would crash further down. */
  86. if (! compare_and_swap(&lock->__status, oldstatus, 0, &lock->__spinlock))
  87. goto again;
  88. return;
  89. }
  90. /* Find thread in waiting queue with maximal priority */
  91. ptr = (pthread_descr *) &lock->__status;
  92. thr = (pthread_descr) oldstatus;
  93. maxprio = 0;
  94. maxptr = ptr;
  95. while (thr != (pthread_descr) 1) {
  96. if (thr->p_priority >= maxprio) {
  97. maxptr = ptr;
  98. maxprio = thr->p_priority;
  99. }
  100. ptr = &(thr->p_nextlock);
  101. thr = *ptr;
  102. }
  103. /* Remove max prio thread from waiting list. */
  104. if (maxptr == (pthread_descr *) &lock->__status) {
  105. /* If max prio thread is at head, remove it with compare-and-swap
  106. to guard against concurrent lock operation */
  107. thr = (pthread_descr) oldstatus;
  108. if (! compare_and_swap(&lock->__status,
  109. oldstatus, (long)(thr->p_nextlock),
  110. &lock->__spinlock))
  111. goto again;
  112. } else {
  113. /* No risk of concurrent access, remove max prio thread normally */
  114. thr = *maxptr;
  115. *maxptr = thr->p_nextlock;
  116. }
  117. /* Wake up the selected waiting thread */
  118. thr->p_nextlock = NULL;
  119. restart(thr);
  120. }
  121. /* Compare-and-swap emulation with a spinlock */
  122. #ifdef TEST_FOR_COMPARE_AND_SWAP
  123. int __pthread_has_cas = 0;
  124. #endif
  125. #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  126. static void __pthread_acquire(int * spinlock);
  127. int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
  128. int * spinlock)
  129. {
  130. int res;
  131. if (testandset(spinlock)) __pthread_acquire(spinlock);
  132. if (*ptr == oldval) {
  133. *ptr = newval; res = 1;
  134. } else {
  135. res = 0;
  136. }
  137. *spinlock = 0;
  138. return res;
  139. }
  140. /* This function is called if the inlined test-and-set
  141. in __pthread_compare_and_swap() failed */
  142. /* The retry strategy is as follows:
  143. - We test and set the spinlock MAX_SPIN_COUNT times, calling
  144. sched_yield() each time. This gives ample opportunity for other
  145. threads with priority >= our priority to make progress and
  146. release the spinlock.
  147. - If a thread with priority < our priority owns the spinlock,
  148. calling sched_yield() repeatedly is useless, since we're preventing
  149. the owning thread from making progress and releasing the spinlock.
  150. So, after MAX_SPIN_LOCK attemps, we suspend the calling thread
  151. using nanosleep(). This again should give time to the owning thread
  152. for releasing the spinlock.
  153. Notice that the nanosleep() interval must not be too small,
  154. since the kernel does busy-waiting for short intervals in a realtime
  155. process (!). The smallest duration that guarantees thread
  156. suspension is currently 2ms.
  157. - When nanosleep() returns, we try again, doing MAX_SPIN_COUNT
  158. sched_yield(), then sleeping again if needed. */
  159. static void __pthread_acquire(int * spinlock)
  160. {
  161. int cnt = 0;
  162. struct timespec tm;
  163. while (testandset(spinlock)) {
  164. if (cnt < MAX_SPIN_COUNT) {
  165. sched_yield();
  166. cnt++;
  167. } else {
  168. tm.tv_sec = 0;
  169. tm.tv_nsec = SPIN_SLEEP_DURATION;
  170. nanosleep(&tm, NULL);
  171. cnt = 0;
  172. }
  173. }
  174. }
  175. #endif