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