* sysdeps/unix/sysv/linux/alpha/Versions: Put pciconfig_iobase in...
[glibc.git] / linuxthreads / oldsemaphore.c
blob62d281269368295a98fd5de32017f4a5455c491a
1 /*
2 * This file contains the old semaphore code that we need to
3 * preserve for glibc-2.0 backwards compatibility. Port to glibc 2.1
4 * done by Cristian Gafton.
5 */
7 /* Linuxthreads - a simple clone()-based implementation of Posix */
8 /* threads for Linux. */
9 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
10 /* */
11 /* This program is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Library General Public License */
13 /* as published by the Free Software Foundation; either version 2 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19 /* GNU Library General Public License for more details. */
21 /* Semaphores a la POSIX 1003.1b */
23 #include <errno.h>
24 #include "pthread.h"
25 #include "internals.h"
26 #include "spinlock.h"
27 #include "restart.h"
28 #include "queue.h"
30 typedef struct {
31 long int sem_status;
32 int sem_spinlock;
33 } old_sem_t;
35 /* Maximum value the semaphore can have. */
36 #define SEM_VALUE_MAX ((int) ((~0u) >> 1))
38 static inline int sem_compare_and_swap(old_sem_t *sem, long oldval, long newval)
40 return compare_and_swap(&sem->sem_status, oldval, newval, &sem->sem_spinlock);
43 /* The state of a semaphore is represented by a long int encoding
44 either the semaphore count if >= 0 and no thread is waiting on it,
45 or the head of the list of threads waiting for the semaphore.
46 To distinguish the two cases, we encode the semaphore count N
47 as 2N+1, so that it has the lowest bit set.
49 A sequence of sem_wait operations on a semaphore initialized to N
50 result in the following successive states:
51 2N+1, 2N-1, ..., 3, 1, &first_waiting_thread, &second_waiting_thread, ...
54 static void sem_restart_list(pthread_descr waiting);
56 int __old_sem_init(old_sem_t *sem, int pshared, unsigned int value)
58 if (value > SEM_VALUE_MAX) {
59 errno = EINVAL;
60 return -1;
62 if (pshared) {
63 errno = ENOSYS;
64 return -1;
66 sem->sem_spinlock = 0;
67 sem->sem_status = ((long)value << 1) + 1;
68 return 0;
71 /* Function called by pthread_cancel to remove the thread from
72 waiting inside __old_sem_wait. Here we simply unconditionally
73 indicate that the thread is to be woken, by returning 1. */
75 static int old_sem_extricate_func(void *obj, pthread_descr th)
77 return 1;
80 int __old_sem_wait(old_sem_t * sem)
82 long oldstatus, newstatus;
83 volatile pthread_descr self = thread_self();
84 pthread_descr * th;
85 pthread_extricate_if extr;
87 /* Set up extrication interface */
88 extr.pu_object = 0;
89 extr.pu_extricate_func = old_sem_extricate_func;
91 while (1) {
92 /* Register extrication interface */
93 __pthread_set_own_extricate_if(self, &extr);
94 do {
95 oldstatus = sem->sem_status;
96 if ((oldstatus & 1) && (oldstatus != 1))
97 newstatus = oldstatus - 2;
98 else {
99 newstatus = (long) self;
100 self->p_nextwaiting = (pthread_descr) oldstatus;
103 while (! sem_compare_and_swap(sem, oldstatus, newstatus));
104 if (newstatus & 1) {
105 /* We got the semaphore. */
106 __pthread_set_own_extricate_if(self, 0);
107 return 0;
109 /* Wait for sem_post or cancellation */
110 suspend(self);
111 __pthread_set_own_extricate_if(self, 0);
113 /* This is a cancellation point */
114 if (self->p_canceled && self->p_cancelstate == PTHREAD_CANCEL_ENABLE) {
115 /* Remove ourselves from the waiting list if we're still on it */
116 /* First check if we're at the head of the list. */
117 do {
118 oldstatus = sem->sem_status;
119 if (oldstatus != (long) self) break;
120 newstatus = (long) self->p_nextwaiting;
122 while (! sem_compare_and_swap(sem, oldstatus, newstatus));
123 /* Now, check if we're somewhere in the list.
124 There's a race condition with sem_post here, but it does not matter:
125 the net result is that at the time pthread_exit is called,
126 self is no longer reachable from sem->sem_status. */
127 if (oldstatus != (long) self && (oldstatus & 1) == 0) {
128 for (th = &(((pthread_descr) oldstatus)->p_nextwaiting);
129 *th != NULL && *th != (pthread_descr) 1;
130 th = &((*th)->p_nextwaiting)) {
131 if (*th == self) {
132 *th = self->p_nextwaiting;
133 break;
137 pthread_exit(PTHREAD_CANCELED);
142 int __old_sem_trywait(old_sem_t * sem)
144 long oldstatus, newstatus;
146 do {
147 oldstatus = sem->sem_status;
148 if ((oldstatus & 1) == 0 || (oldstatus == 1)) {
149 errno = EAGAIN;
150 return -1;
152 newstatus = oldstatus - 2;
154 while (! sem_compare_and_swap(sem, oldstatus, newstatus));
155 return 0;
158 int __old_sem_post(old_sem_t * sem)
160 long oldstatus, newstatus;
162 do {
163 oldstatus = sem->sem_status;
164 if ((oldstatus & 1) == 0)
165 newstatus = 3;
166 else {
167 if (oldstatus >= SEM_VALUE_MAX) {
168 /* Overflow */
169 errno = ERANGE;
170 return -1;
172 newstatus = oldstatus + 2;
175 while (! sem_compare_and_swap(sem, oldstatus, newstatus));
176 if ((oldstatus & 1) == 0)
177 sem_restart_list((pthread_descr) oldstatus);
178 return 0;
181 int __old_sem_getvalue(old_sem_t * sem, int * sval)
183 long status = sem->sem_status;
184 if (status & 1)
185 *sval = (int)((unsigned long) status >> 1);
186 else
187 *sval = 0;
188 return 0;
191 int __old_sem_destroy(old_sem_t * sem)
193 if ((sem->sem_status & 1) == 0) {
194 errno = EBUSY;
195 return -1;
197 return 0;
200 /* Auxiliary function for restarting all threads on a waiting list,
201 in priority order. */
203 static void sem_restart_list(pthread_descr waiting)
205 pthread_descr th, towake, *p;
207 /* Sort list of waiting threads by decreasing priority (insertion sort) */
208 towake = NULL;
209 while (waiting != (pthread_descr) 1) {
210 th = waiting;
211 waiting = waiting->p_nextwaiting;
212 p = &towake;
213 while (*p != NULL && th->p_priority < (*p)->p_priority)
214 p = &((*p)->p_nextwaiting);
215 th->p_nextwaiting = *p;
216 *p = th;
218 /* Wake up threads in priority order */
219 while (towake != NULL) {
220 th = towake;
221 towake = towake->p_nextwaiting;
222 th->p_nextwaiting = NULL;
223 restart(th);
227 #if defined PIC && DO_VERSIONING
228 symbol_version (__old_sem_init, sem_init, GLIBC_2.0);
229 symbol_version (__old_sem_wait, sem_wait, GLIBC_2.0);
230 symbol_version (__old_sem_trywait, sem_trywait, GLIBC_2.0);
231 symbol_version (__old_sem_post, sem_post, GLIBC_2.0);
232 symbol_version (__old_sem_getvalue, sem_getvalue, GLIBC_2.0);
233 symbol_version (__old_sem_destroy, sem_destroy, GLIBC_2.0);
234 #endif