Update.
[glibc.git] / linuxthreads / spinlock.c
blob43190a2eda4debde918d259b768a6e4833918772
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. */
15 /* Internal locks */
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"
27 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
28 static void __pthread_acquire(int * spinlock);
29 #endif
32 /* The status field of a spinlock is a pointer whose least significant
33 bit is a locked flag.
35 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
40 (status & 1) == 1: spinlock is taken and (status & ~1L) is a
41 pointer to the first waiting thread; other
42 waiting threads are linked via the p_nextlock
43 field.
44 (status & 1) == 0: same as above, but spinlock is not taken.
46 The waiting list is not sorted by priority order.
47 Actually, we always insert at top of list (sole insertion mode
48 that can be performed without locking).
49 For __pthread_unlock, we perform a linear search in the list
50 to find the highest-priority, oldest waiting thread.
51 This is safe because there are no concurrent __pthread_unlock
52 operations -- only the thread that locked the mutex can unlock it. */
55 void internal_function __pthread_lock(struct _pthread_fastlock * lock,
56 pthread_descr self)
58 #if defined HAS_COMPARE_AND_SWAP
59 long oldstatus, newstatus;
60 int successful_seizure, spurious_wakeup_count = 0;
61 int spin_count = 0;
62 #endif
64 #if defined TEST_FOR_COMPARE_AND_SWAP
65 if (!__pthread_has_cas)
66 #endif
67 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
69 __pthread_acquire(&lock->__spinlock);
70 return;
72 #endif
74 #if defined HAS_COMPARE_AND_SWAP
75 again:
77 /* On SMP, try spinning to get the lock. */
79 if (__pthread_smp_kernel) {
80 int max_count = lock->__spinlock * 2 + 10;
82 for (spin_count = 0; spin_count < max_count; spin_count++) {
83 if (((oldstatus = lock->__status) & 1) == 0) {
84 if(__compare_and_swap(&lock->__status, oldstatus, oldstatus | 1))
86 if (spin_count)
87 lock->__spinlock += (spin_count - lock->__spinlock) / 8;
88 return;
93 lock->__spinlock += (spin_count - lock->__spinlock) / 8;
96 /* No luck, try once more or suspend. */
98 do {
99 oldstatus = lock->__status;
100 successful_seizure = 0;
102 if ((oldstatus & 1) == 0) {
103 newstatus = oldstatus | 1;
104 successful_seizure = 1;
105 } else {
106 if (self == NULL)
107 self = thread_self();
108 newstatus = (long) self | 1;
111 if (self != NULL) {
112 THREAD_SETMEM(self, p_nextlock, (pthread_descr) (oldstatus & ~1L));
113 /* Make sure the store in p_nextlock completes before performing
114 the compare-and-swap */
115 MEMORY_BARRIER();
117 } 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. */
125 if (!successful_seizure) {
126 for (;;) {
127 suspend(self);
128 if (self->p_nextlock != NULL) {
129 /* Count resumes that don't belong to us. */
130 spurious_wakeup_count++;
131 continue;
133 break;
135 goto again;
138 /* Put back any resumes we caught that don't belong to us. */
139 while (spurious_wakeup_count--)
140 restart(self);
141 #endif
144 int __pthread_unlock(struct _pthread_fastlock * lock)
146 #if defined HAS_COMPARE_AND_SWAP
147 long oldstatus;
148 pthread_descr thr, * ptr, * maxptr;
149 int maxprio;
150 #endif
152 #if defined TEST_FOR_COMPARE_AND_SWAP
153 if (!__pthread_has_cas)
154 #endif
155 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
157 WRITE_MEMORY_BARRIER();
158 lock->__spinlock = 0;
159 return 0;
161 #endif
163 #if defined HAS_COMPARE_AND_SWAP
164 again:
165 oldstatus = lock->__status;
167 while ((oldstatus = lock->__status) == 1) {
168 if (__compare_and_swap_with_release_semantics(&lock->__status,
169 oldstatus, 0))
170 return 0;
173 /* Find thread in waiting queue with maximal priority */
174 ptr = (pthread_descr *) &lock->__status;
175 thr = (pthread_descr) (oldstatus & ~1L);
176 maxprio = 0;
177 maxptr = ptr;
178 while (thr != 0) {
179 if (thr->p_priority >= maxprio) {
180 maxptr = ptr;
181 maxprio = thr->p_priority;
183 ptr = &(thr->p_nextlock);
184 /* Prevent reordering of the load of lock->__status above and the
185 load of *ptr below, as well as reordering of *ptr between
186 several iterations of the while loop. Some processors (e.g.
187 multiprocessor Alphas) could perform such reordering even though
188 the loads are dependent. */
189 READ_MEMORY_BARRIER();
190 thr = *ptr;
192 /* Prevent reordering of the load of lock->__status above and
193 thr->p_nextlock below */
194 READ_MEMORY_BARRIER();
195 /* Remove max prio thread from waiting list. */
196 if (maxptr == (pthread_descr *) &lock->__status) {
197 /* If max prio thread is at head, remove it with compare-and-swap
198 to guard against concurrent lock operation. This removal
199 also has the side effect of marking the lock as released
200 because the new status comes from thr->p_nextlock whose
201 least significant bit is clear. */
202 thr = (pthread_descr) (oldstatus & ~1L);
203 if (! __compare_and_swap_with_release_semantics
204 (&lock->__status, oldstatus, (long)(thr->p_nextlock)))
205 goto again;
206 } else {
207 /* No risk of concurrent access, remove max prio thread normally.
208 But in this case we must also flip the least significant bit
209 of the status to mark the lock as released. */
210 thr = *maxptr;
211 *maxptr = thr->p_nextlock;
213 do {
214 oldstatus = lock->__status;
215 } while (!__compare_and_swap_with_release_semantics(&lock->__status,
216 oldstatus, oldstatus & ~1L));
218 /* Prevent reordering of store to *maxptr above and store to thr->p_nextlock
219 below */
220 WRITE_MEMORY_BARRIER();
221 /* Wake up the selected waiting thread */
222 thr->p_nextlock = NULL;
223 restart(thr);
225 return 0;
226 #endif
230 * Alternate fastlocks do not queue threads directly. Instead, they queue
231 * these wait queue node structures. When a timed wait wakes up due to
232 * a timeout, it can leave its wait node in the queue (because there
233 * is no safe way to remove from the quue). Some other thread will
234 * deallocate the abandoned node.
238 struct wait_node {
239 struct wait_node *next; /* Next node in null terminated linked list */
240 pthread_descr thr; /* The thread waiting with this node */
241 int abandoned; /* Atomic flag */
244 static long wait_node_free_list;
245 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
246 static int wait_node_free_list_spinlock;
247 #endif
249 /* Allocate a new node from the head of the free list using an atomic
250 operation, or else using malloc if that list is empty. A fundamental
251 assumption here is that we can safely access wait_node_free_list->next.
252 That's because we never free nodes once we allocate them, so a pointer to a
253 node remains valid indefinitely. */
255 static struct wait_node *wait_node_alloc(void)
257 #if defined HAS_COMPARE_AND_SWAP
258 long oldvalue, newvalue;
259 #endif
261 #if defined TEST_FOR_COMPARE_AND_SWAP
262 if (!__pthread_has_cas)
263 #endif
264 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
266 struct wait_node *new_node = 0;
268 __pthread_acquire(&wait_node_free_list_spinlock);
269 if (wait_node_free_list != 0) {
270 new_node = (struct wait_node *) wait_node_free_list;
271 wait_node_free_list = (long) new_node->next;
273 WRITE_MEMORY_BARRIER();
274 wait_node_free_list_spinlock = 0;
276 if (new_node == 0)
277 return malloc(sizeof *wait_node_alloc());
279 return new_node;
281 #endif
283 #if defined HAS_COMPARE_AND_SWAP
284 do {
285 oldvalue = wait_node_free_list;
287 if (oldvalue == 0)
288 return malloc(sizeof *wait_node_alloc());
290 newvalue = (long) ((struct wait_node *) oldvalue)->next;
291 WRITE_MEMORY_BARRIER();
292 } while (! __compare_and_swap(&wait_node_free_list, oldvalue, newvalue));
294 return (struct wait_node *) oldvalue;
295 #endif
298 /* Return a node to the head of the free list using an atomic
299 operation. */
301 static void wait_node_free(struct wait_node *wn)
303 #if defined HAS_COMPARE_AND_SWAP
304 long oldvalue, newvalue;
305 #endif
307 #if defined TEST_FOR_COMPARE_AND_SWAP
308 if (!__pthread_has_cas)
309 #endif
310 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
312 __pthread_acquire(&wait_node_free_list_spinlock);
313 wn->next = (struct wait_node *) wait_node_free_list;
314 wait_node_free_list = (long) wn;
315 WRITE_MEMORY_BARRIER();
316 wait_node_free_list_spinlock = 0;
317 return;
319 #endif
321 #if defined HAS_COMPARE_AND_SWAP
322 do {
323 oldvalue = wait_node_free_list;
324 wn->next = (struct wait_node *) oldvalue;
325 newvalue = (long) wn;
326 WRITE_MEMORY_BARRIER();
327 } while (! __compare_and_swap(&wait_node_free_list, oldvalue, newvalue));
328 #endif
331 #if defined HAS_COMPARE_AND_SWAP
333 /* Remove a wait node from the specified queue. It is assumed
334 that the removal takes place concurrently with only atomic insertions at the
335 head of the queue. */
337 static void wait_node_dequeue(struct wait_node **pp_head,
338 struct wait_node **pp_node,
339 struct wait_node *p_node)
341 /* If the node is being deleted from the head of the
342 list, it must be deleted using atomic compare-and-swap.
343 Otherwise it can be deleted in the straightforward way. */
345 if (pp_node == pp_head) {
346 long oldvalue = (long) p_node;
347 long newvalue = (long) p_node->next;
349 if (__compare_and_swap((long *) pp_node, oldvalue, newvalue))
350 return;
352 /* Oops! Compare and swap failed, which means the node is
353 no longer first. We delete it using the ordinary method. But we don't
354 know the identity of the node which now holds the pointer to the node
355 being deleted, so we must search from the beginning. */
357 for (pp_node = pp_head; *pp_node != p_node; pp_node = &(*pp_node)->next)
358 ; /* null body */
361 *pp_node = p_node->next;
362 return;
365 #endif
367 void __pthread_alt_lock(struct _pthread_fastlock * lock,
368 pthread_descr self)
370 #if defined HAS_COMPARE_AND_SWAP
371 long oldstatus, newstatus;
372 #endif
373 struct wait_node wait_node;
375 #if defined TEST_FOR_COMPARE_AND_SWAP
376 if (!__pthread_has_cas)
377 #endif
378 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
380 int suspend_needed = 0;
381 __pthread_acquire(&lock->__spinlock);
383 if (lock->__status == 0)
384 lock->__status = 1;
385 else {
386 if (self == NULL)
387 self = thread_self();
389 wait_node.abandoned = 0;
390 wait_node.next = (struct wait_node *) lock->__status;
391 wait_node.thr = self;
392 suspend_needed = 1;
395 WRITE_MEMORY_BARRIER();
396 lock->__spinlock = 0;
398 if (suspend_needed)
399 suspend (self);
400 return;
402 #endif
404 #if defined HAS_COMPARE_AND_SWAP
405 do {
406 oldstatus = lock->__status;
407 if (oldstatus == 0) {
408 newstatus = 1;
409 } else {
410 if (self == NULL)
411 self = thread_self();
412 wait_node.thr = self;
413 newstatus = (long) &wait_node;
415 wait_node.abandoned = 0;
416 wait_node.next = (struct wait_node *) oldstatus;
417 /* Make sure the store in wait_node.next completes before performing
418 the compare-and-swap */
419 MEMORY_BARRIER();
420 } while(! compare_and_swap(&lock->__status, oldstatus, newstatus,
421 &lock->__spinlock));
423 /* Suspend. Note that unlike in __pthread_lock, we don't worry
424 here about spurious wakeup. That's because this lock is not
425 used in situations where that can happen; the restart can
426 only come from the previous lock owner. */
428 if (oldstatus != 0)
429 suspend(self);
430 #endif
433 /* Timed-out lock operation; returns 0 to indicate timeout. */
435 int __pthread_alt_timedlock(struct _pthread_fastlock * lock,
436 pthread_descr self, const struct timespec *abstime)
438 long oldstatus;
439 #if defined HAS_COMPARE_AND_SWAP
440 long newstatus;
441 #endif
442 struct wait_node *p_wait_node = wait_node_alloc();
444 /* Out of memory, just give up and do ordinary lock. */
445 if (p_wait_node == 0) {
446 __pthread_alt_lock(lock, self);
447 return 1;
450 #if defined TEST_FOR_COMPARE_AND_SWAP
451 if (!__pthread_has_cas)
452 #endif
453 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
455 __pthread_acquire(&lock->__spinlock);
457 if (lock->__status == 0)
458 lock->__status = 1;
459 else {
460 if (self == NULL)
461 self = thread_self();
463 p_wait_node->abandoned = 0;
464 p_wait_node->next = (struct wait_node *) lock->__status;
465 p_wait_node->thr = self;
468 WRITE_MEMORY_BARRIER();
469 lock->__spinlock = 0;
470 oldstatus = 1; /* force suspend */
471 goto suspend;
473 #endif
475 #if defined HAS_COMPARE_AND_SWAP
476 do {
477 oldstatus = lock->__status;
478 if (oldstatus == 0) {
479 newstatus = 1;
480 } else {
481 if (self == NULL)
482 p_wait_node->thr = self;
483 newstatus = (long) p_wait_node;
485 p_wait_node->abandoned = 0;
486 p_wait_node->next = (struct wait_node *) oldstatus;
487 /* Make sure the store in wait_node.next completes before performing
488 the compare-and-swap */
489 MEMORY_BARRIER();
490 } while(! compare_and_swap(&lock->__status, oldstatus, newstatus,
491 &lock->__spinlock));
492 #endif
494 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
495 suspend:
496 #endif
498 /* If we did not get the lock, do a timed suspend. If we wake up due
499 to a timeout, then there is a race; the old lock owner may try
500 to remove us from the queue. This race is resolved by us and the owner
501 doing an atomic testandset() to change the state of the wait node from 0
502 to 1. If we succeed, then it's a timeout and we abandon the node in the
503 queue. If we fail, it means the owner gave us the lock. */
505 if (oldstatus != 0) {
506 if (timedsuspend(self, abstime) == 0) {
507 if (!testandset(&p_wait_node->abandoned))
508 return 0; /* Timeout! */
510 /* Eat oustanding resume from owner, otherwise wait_node_free() below
511 will race with owner's wait_node_dequeue(). */
512 suspend(self);
516 wait_node_free(p_wait_node);
518 return 1; /* Got the lock! */
521 void __pthread_alt_unlock(struct _pthread_fastlock *lock)
523 struct wait_node *p_node, **pp_node, *p_max_prio, **pp_max_prio;
524 struct wait_node ** const pp_head = (struct wait_node **) &lock->__status;
525 int maxprio;
527 #if defined TEST_FOR_COMPARE_AND_SWAP
528 if (!__pthread_has_cas)
529 #endif
530 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
532 __pthread_acquire(&lock->__spinlock);
534 #endif
536 while (1) {
538 /* If no threads are waiting for this lock, try to just
539 atomically release it. */
540 #if defined TEST_FOR_COMPARE_AND_SWAP
541 if (!__pthread_has_cas)
542 #endif
543 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
545 if (lock->__status == 0 || lock->__status == 1) {
546 lock->__status = 0;
547 break;
550 #endif
552 #if defined TEST_FOR_COMPARE_AND_SWAP
553 else
554 #endif
556 #if defined HAS_COMPARE_AND_SWAP
558 long oldstatus = lock->__status;
559 if (oldstatus == 0 || oldstatus == 1) {
560 if (__compare_and_swap_with_release_semantics (&lock->__status, oldstatus, 0))
561 break;
562 else
563 continue;
566 #endif
568 /* Process the entire queue of wait nodes. Remove all abandoned
569 wait nodes and put them into the global free queue, and
570 remember the one unabandoned node which refers to the thread
571 having the highest priority. */
573 pp_max_prio = pp_node = pp_head;
574 p_max_prio = p_node = *pp_head;
575 maxprio = INT_MIN;
577 while (p_node != (struct wait_node *) 1) {
578 int prio;
580 if (p_node->abandoned) {
581 /* Remove abandoned node. */
582 #if defined TEST_FOR_COMPARE_AND_SWAP
583 if (!__pthread_has_cas)
584 #endif
585 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
586 *pp_node = p_node->next;
587 #endif
588 #if defined TEST_FOR_COMPARE_AND_SWAP
589 else
590 #endif
591 #if defined HAS_COMPARE_AND_SWAP
592 wait_node_dequeue(pp_head, pp_node, p_node);
593 #endif
594 wait_node_free(p_node);
595 READ_MEMORY_BARRIER();
596 p_node = *pp_node;
597 continue;
598 } else if ((prio = p_node->thr->p_priority) >= maxprio) {
599 /* Otherwise remember it if its thread has a higher or equal priority
600 compared to that of any node seen thus far. */
601 maxprio = prio;
602 pp_max_prio = pp_node;
603 p_max_prio = p_node;
606 pp_node = &p_node->next;
607 READ_MEMORY_BARRIER();
608 p_node = *pp_node;
611 READ_MEMORY_BARRIER();
613 /* If all threads abandoned, go back to top */
614 if (maxprio == INT_MIN)
615 continue;
617 ASSERT (p_max_prio != (struct wait_node *) 1);
619 /* Now we want to to remove the max priority thread's wait node from
620 the list. Before we can do this, we must atomically try to change the
621 node's abandon state from zero to nonzero. If we succeed, that means we
622 have the node that we will wake up. If we failed, then it means the
623 thread timed out and abandoned the node in which case we repeat the
624 whole unlock operation. */
626 if (!testandset(&p_max_prio->abandoned)) {
627 #if defined TEST_FOR_COMPARE_AND_SWAP
628 if (!__pthread_has_cas)
629 #endif
630 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
631 *pp_max_prio = p_max_prio->next;
632 #endif
633 #if defined TEST_FOR_COMPARE_AND_SWAP
634 else
635 #endif
636 #if defined HAS_COMPARE_AND_SWAP
637 wait_node_dequeue(pp_head, pp_max_prio, p_max_prio);
638 #endif
639 WRITE_MEMORY_BARRIER();
640 restart(p_max_prio->thr);
641 break;
645 #if defined TEST_FOR_COMPARE_AND_SWAP
646 if (!__pthread_has_cas)
647 #endif
648 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
650 WRITE_MEMORY_BARRIER();
651 lock->__spinlock = 0;
653 #endif
657 /* Compare-and-swap emulation with a spinlock */
659 #ifdef TEST_FOR_COMPARE_AND_SWAP
660 int __pthread_has_cas = 0;
661 #endif
663 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
665 int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
666 int * spinlock)
668 int res;
669 if (testandset(spinlock)) __pthread_acquire(spinlock);
670 if (*ptr == oldval) {
671 *ptr = newval; res = 1;
672 } else {
673 res = 0;
675 /* Prevent reordering of store to *ptr above and store to *spinlock below */
676 WRITE_MEMORY_BARRIER();
677 *spinlock = 0;
678 return res;
681 /* This function is called if the inlined test-and-set
682 in __pthread_compare_and_swap() failed */
684 /* The retry strategy is as follows:
685 - We test and set the spinlock MAX_SPIN_COUNT times, calling
686 sched_yield() each time. This gives ample opportunity for other
687 threads with priority >= our priority to make progress and
688 release the spinlock.
689 - If a thread with priority < our priority owns the spinlock,
690 calling sched_yield() repeatedly is useless, since we're preventing
691 the owning thread from making progress and releasing the spinlock.
692 So, after MAX_SPIN_LOCK attemps, we suspend the calling thread
693 using nanosleep(). This again should give time to the owning thread
694 for releasing the spinlock.
695 Notice that the nanosleep() interval must not be too small,
696 since the kernel does busy-waiting for short intervals in a realtime
697 process (!). The smallest duration that guarantees thread
698 suspension is currently 2ms.
699 - When nanosleep() returns, we try again, doing MAX_SPIN_COUNT
700 sched_yield(), then sleeping again if needed. */
702 static void __pthread_acquire(int * spinlock)
704 int cnt = 0;
705 struct timespec tm;
707 while (testandset(spinlock)) {
708 if (cnt < MAX_SPIN_COUNT) {
709 sched_yield();
710 cnt++;
711 } else {
712 tm.tv_sec = 0;
713 tm.tv_nsec = SPIN_SLEEP_DURATION;
714 nanosleep(&tm, NULL);
715 cnt = 0;
720 #endif