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.
7 /* Linuxthreads - a simple clone()-based implementation of Posix */
8 /* threads for Linux. */
9 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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 */
25 #include "internals.h"
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
) {
66 sem
->sem_spinlock
= 0;
67 sem
->sem_status
= ((long)value
<< 1) + 1;
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
)
80 int __old_sem_wait(old_sem_t
* sem
)
82 long oldstatus
, newstatus
;
83 volatile pthread_descr self
= thread_self();
85 pthread_extricate_if extr
;
87 /* Set up extrication interface */
89 extr
.pu_extricate_func
= old_sem_extricate_func
;
92 /* Register extrication interface */
93 __pthread_set_own_extricate_if(self
, &extr
);
95 oldstatus
= sem
->sem_status
;
96 if ((oldstatus
& 1) && (oldstatus
!= 1))
97 newstatus
= oldstatus
- 2;
99 newstatus
= (long) self
;
100 self
->p_nextwaiting
= (pthread_descr
) oldstatus
;
103 while (! sem_compare_and_swap(sem
, oldstatus
, newstatus
));
105 /* We got the semaphore. */
106 __pthread_set_own_extricate_if(self
, 0);
109 /* Wait for sem_post or cancellation */
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. */
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
)) {
132 *th
= self
->p_nextwaiting
;
137 pthread_exit(PTHREAD_CANCELED
);
142 int __old_sem_trywait(old_sem_t
* sem
)
144 long oldstatus
, newstatus
;
147 oldstatus
= sem
->sem_status
;
148 if ((oldstatus
& 1) == 0 || (oldstatus
== 1)) {
152 newstatus
= oldstatus
- 2;
154 while (! sem_compare_and_swap(sem
, oldstatus
, newstatus
));
158 int __old_sem_post(old_sem_t
* sem
)
160 long oldstatus
, newstatus
;
163 oldstatus
= sem
->sem_status
;
164 if ((oldstatus
& 1) == 0)
167 if (oldstatus
>= SEM_VALUE_MAX
) {
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
);
181 int __old_sem_getvalue(old_sem_t
* sem
, int * sval
)
183 long status
= sem
->sem_status
;
185 *sval
= (int)((unsigned long) status
>> 1);
191 int __old_sem_destroy(old_sem_t
* sem
)
193 if ((sem
->sem_status
& 1) == 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) */
209 while (waiting
!= (pthread_descr
) 1) {
211 waiting
= waiting
->p_nextwaiting
;
213 while (*p
!= NULL
&& th
->p_priority
< (*p
)->p_priority
)
214 p
= &((*p
)->p_nextwaiting
);
215 th
->p_nextwaiting
= *p
;
218 /* Wake up threads in priority order */
219 while (towake
!= NULL
) {
221 towake
= towake
->p_nextwaiting
;
222 th
->p_nextwaiting
= NULL
;
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
);