(__pthread_trylock): Define inline. (__pthread_lock): Add extra parameter to declarat...
[glibc.git] / linuxthreads / spinlock.c
blob172cb7afe480cd309d476d472c6f4401cac81bb0
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_nextwaiting 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 __pthread_lock(struct _pthread_fastlock * lock)
41 long oldstatus, newstatus;
42 pthread_descr self = NULL;
44 do {
45 oldstatus = lock->__status;
46 if (oldstatus == 0) {
47 newstatus = 1;
48 } else {
49 self = thread_self();
50 newstatus = (long) self;
52 if (self != NULL)
53 THREAD_SETMEM(self, p_nextwaiting, (pthread_descr) oldstatus);
54 } while(! compare_and_swap(&lock->__status, oldstatus, newstatus,
55 &lock->__spinlock));
56 if (oldstatus != 0) suspend(self);
59 int __pthread_trylock(struct _pthread_fastlock * lock)
61 long oldstatus;
63 do {
64 oldstatus = lock->__status;
65 if (oldstatus != 0) return EBUSY;
66 } while(! compare_and_swap(&lock->__status, 0, 1, &lock->__spinlock));
67 return 0;
70 void __pthread_unlock(struct _pthread_fastlock * lock)
72 long oldstatus;
73 pthread_descr thr, * ptr, * maxptr;
74 int maxprio;
76 again:
77 oldstatus = lock->__status;
78 if (oldstatus == 1) {
79 /* No threads are waiting for this lock */
80 if (! compare_and_swap(&lock->__status, 1, 0, &lock->__spinlock))
81 goto again;
82 return;
84 /* Find thread in waiting queue with maximal priority */
85 ptr = (pthread_descr *) &lock->__status;
86 thr = (pthread_descr) oldstatus;
87 maxprio = 0;
88 maxptr = ptr;
89 while (thr != (pthread_descr) 1) {
90 if (thr->p_priority >= maxprio) {
91 maxptr = ptr;
92 maxprio = thr->p_priority;
94 ptr = &(thr->p_nextwaiting);
95 thr = *ptr;
97 /* Remove max prio thread from waiting list. */
98 if (maxptr == (pthread_descr *) &lock->__status) {
99 /* If max prio thread is at head, remove it with compare-and-swap
100 to guard against concurrent lock operation */
101 thr = (pthread_descr) oldstatus;
102 if (! compare_and_swap(&lock->__status,
103 oldstatus, (long)(thr->p_nextwaiting),
104 &lock->__spinlock))
105 goto again;
106 } else {
107 /* No risk of concurrent access, remove max prio thread normally */
108 thr = *maxptr;
109 *maxptr = thr->p_nextwaiting;
111 /* Wake up the selected waiting thread */
112 thr->p_nextwaiting = NULL;
113 restart(thr);
116 /* Compare-and-swap emulation with a spinlock */
118 #ifdef TEST_FOR_COMPARE_AND_SWAP
119 int __pthread_has_cas = 0;
120 #endif
122 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
124 static void __pthread_acquire(int * spinlock);
126 int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
127 int * spinlock)
129 int res;
130 if (testandset(spinlock)) __pthread_acquire(spinlock);
131 if (*ptr == oldval) {
132 *ptr = newval; res = 1;
133 } else {
134 res = 0;
136 *spinlock = 0;
137 return res;
140 /* This function is called if the inlined test-and-set
141 in __pthread_compare_and_swap() failed */
143 /* The retry strategy is as follows:
144 - We test and set the spinlock MAX_SPIN_COUNT times, calling
145 sched_yield() each time. This gives ample opportunity for other
146 threads with priority >= our priority to make progress and
147 release the spinlock.
148 - If a thread with priority < our priority owns the spinlock,
149 calling sched_yield() repeatedly is useless, since we're preventing
150 the owning thread from making progress and releasing the spinlock.
151 So, after MAX_SPIN_LOCK attemps, we suspend the calling thread
152 using nanosleep(). This again should give time to the owning thread
153 for releasing the spinlock.
154 Notice that the nanosleep() interval must not be too small,
155 since the kernel does busy-waiting for short intervals in a realtime
156 process (!). The smallest duration that guarantees thread
157 suspension is currently 2ms.
158 - When nanosleep() returns, we try again, doing MAX_SPIN_COUNT
159 sched_yield(), then sleeping again if needed. */
161 static void __pthread_acquire(int * spinlock)
163 int cnt = 0;
164 struct timespec tm;
166 while (testandset(spinlock)) {
167 if (cnt < MAX_SPIN_COUNT) {
168 sched_yield();
169 cnt++;
170 } else {
171 tm.tv_sec = 0;
172 tm.tv_nsec = SPIN_SLEEP_DURATION;
173 nanosleep(&tm, NULL);
174 cnt = 0;
179 #endif