(CFLAGS-rcmd.c, CFLAGS-rexec.c, CFLAGS-ruserpass.c): Removed.
[glibc.git] / linuxthreads / spinlock.c
blob3f5b8233b0b8fbd3e57dc65320b99216ef7ad4aa
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 spinlock has the following meaning:
26 0: spinlock is free
27 1: spinlock is taken, no thread is waiting on it
28 ADDR: psinlock 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;
43 int spurious_wakeup_count = 0;
45 do {
46 oldstatus = lock->__status;
47 if (oldstatus == 0) {
48 newstatus = 1;
49 } else {
50 if (self == NULL)
51 self = thread_self();
52 newstatus = (long) self;
54 if (self != NULL) {
55 THREAD_SETMEM(self, p_nextlock, (pthread_descr) oldstatus);
56 /* Make sure the store in p_nextlock completes before performing
57 the compare-and-swap */
58 MEMORY_BARRIER();
60 } while(! compare_and_swap(&lock->__status, oldstatus, newstatus,
61 &lock->__spinlock));
63 /* Suspend with guard against spurious wakeup.
64 This can happen in pthread_cond_timedwait_relative, when the thread
65 wakes up due to timeout and is still on the condvar queue, and then
66 locks the queue to remove itself. At that point it may still be on the
67 queue, and may be resumed by a condition signal. */
69 if (oldstatus != 0) {
70 for (;;) {
71 suspend(self);
72 if (self->p_nextlock != NULL) {
73 /* Count resumes that don't belong to us. */
74 spurious_wakeup_count++;
75 continue;
77 break;
81 /* Put back any resumes we caught that don't belong to us. */
82 while (spurious_wakeup_count--)
83 restart(self);
86 int __pthread_unlock(struct _pthread_fastlock * lock)
88 long oldstatus;
89 pthread_descr thr, * ptr, * maxptr;
90 int maxprio;
92 again:
93 oldstatus = lock->__status;
94 if (oldstatus == 0 || oldstatus == 1) {
95 /* No threads are waiting for this lock. Please note that we also
96 enter this case if the lock is not taken at all. If this wouldn't
97 be done here we would crash further down. */
98 if (! compare_and_swap(&lock->__status, oldstatus, 0, &lock->__spinlock))
99 goto again;
100 return 0;
102 /* Find thread in waiting queue with maximal priority */
103 ptr = (pthread_descr *) &lock->__status;
104 thr = (pthread_descr) oldstatus;
105 maxprio = 0;
106 maxptr = ptr;
107 while (thr != (pthread_descr) 1) {
108 if (thr->p_priority >= maxprio) {
109 maxptr = ptr;
110 maxprio = thr->p_priority;
112 ptr = &(thr->p_nextlock);
113 /* Prevent reordering of the load of lock->__status above and the
114 load of *ptr below, as well as reordering of *ptr between
115 several iterations of the while loop. Some processors (e.g.
116 multiprocessor Alphas) could perform such reordering even though
117 the loads are dependent. */
118 READ_MEMORY_BARRIER();
119 thr = *ptr;
121 /* Prevent reordering of the load of lock->__status above and
122 thr->p_nextlock below */
123 READ_MEMORY_BARRIER();
124 /* Remove max prio thread from waiting list. */
125 if (maxptr == (pthread_descr *) &lock->__status) {
126 /* If max prio thread is at head, remove it with compare-and-swap
127 to guard against concurrent lock operation */
128 thr = (pthread_descr) oldstatus;
129 if (! compare_and_swap(&lock->__status,
130 oldstatus, (long)(thr->p_nextlock),
131 &lock->__spinlock))
132 goto again;
133 } else {
134 /* No risk of concurrent access, remove max prio thread normally */
135 thr = *maxptr;
136 *maxptr = thr->p_nextlock;
138 /* Prevent reordering of store to *maxptr above and store to thr->p_nextlock
139 below */
140 WRITE_MEMORY_BARRIER();
141 /* Wake up the selected waiting thread */
142 thr->p_nextlock = NULL;
143 restart(thr);
145 return 0;
149 /* Compare-and-swap emulation with a spinlock */
151 #ifdef TEST_FOR_COMPARE_AND_SWAP
152 int __pthread_has_cas = 0;
153 #endif
155 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
157 static void __pthread_acquire(int * spinlock);
159 int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
160 int * spinlock)
162 int res;
163 if (testandset(spinlock)) __pthread_acquire(spinlock);
164 if (*ptr == oldval) {
165 *ptr = newval; res = 1;
166 } else {
167 res = 0;
169 /* Prevent reordering of store to *ptr above and store to *spinlock below */
170 WRITE_MEMORY_BARRIER();
171 *spinlock = 0;
172 return res;
175 /* This function is called if the inlined test-and-set
176 in __pthread_compare_and_swap() failed */
178 /* The retry strategy is as follows:
179 - We test and set the spinlock MAX_SPIN_COUNT times, calling
180 sched_yield() each time. This gives ample opportunity for other
181 threads with priority >= our priority to make progress and
182 release the spinlock.
183 - If a thread with priority < our priority owns the spinlock,
184 calling sched_yield() repeatedly is useless, since we're preventing
185 the owning thread from making progress and releasing the spinlock.
186 So, after MAX_SPIN_LOCK attemps, we suspend the calling thread
187 using nanosleep(). This again should give time to the owning thread
188 for releasing the spinlock.
189 Notice that the nanosleep() interval must not be too small,
190 since the kernel does busy-waiting for short intervals in a realtime
191 process (!). The smallest duration that guarantees thread
192 suspension is currently 2ms.
193 - When nanosleep() returns, we try again, doing MAX_SPIN_COUNT
194 sched_yield(), then sleeping again if needed. */
196 static void __pthread_acquire(int * spinlock)
198 int cnt = 0;
199 struct timespec tm;
201 while (testandset(spinlock)) {
202 if (cnt < MAX_SPIN_COUNT) {
203 sched_yield();
204 cnt++;
205 } else {
206 tm.tv_sec = 0;
207 tm.tv_nsec = SPIN_SLEEP_DURATION;
208 nanosleep(&tm, NULL);
209 cnt = 0;
214 #endif