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