Back to index

glibc  2.9
oldsemaphore.c
Go to the documentation of this file.
00001 /*
00002  * This file contains the old semaphore code that we need to
00003  * preserve for glibc-2.0 backwards compatibility. Port to glibc 2.1
00004  * done by Cristian Gafton.
00005  */
00006 
00007 /* Linuxthreads - a simple clone()-based implementation of Posix        */
00008 /* threads for Linux.                                                   */
00009 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
00010 /*                                                                      */
00011 /* This program is free software; you can redistribute it and/or        */
00012 /* modify it under the terms of the GNU Library General Public License  */
00013 /* as published by the Free Software Foundation; either version 2       */
00014 /* of the License, or (at your option) any later version.               */
00015 /*                                                                      */
00016 /* This program is distributed in the hope that it will be useful,      */
00017 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
00018 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
00019 /* GNU Library General Public License for more details.                 */
00020 
00021 /* Semaphores a la POSIX 1003.1b */
00022 #include <shlib-compat.h>
00023 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
00024 
00025 #include <errno.h>
00026 #include "pthread.h"
00027 #include "internals.h"
00028 #include "spinlock.h"
00029 #include "restart.h"
00030 #include "queue.h"
00031 
00032 typedef struct {
00033     long int sem_status;
00034     int sem_spinlock;
00035 } old_sem_t;
00036 
00037 extern int __old_sem_init (old_sem_t *__sem, int __pshared, unsigned int __value);
00038 extern int __old_sem_wait (old_sem_t *__sem);
00039 extern int __old_sem_trywait (old_sem_t *__sem);
00040 extern int __old_sem_post (old_sem_t *__sem);
00041 extern int __old_sem_getvalue (old_sem_t *__sem, int *__sval);
00042 extern int __old_sem_destroy (old_sem_t *__sem);
00043 
00044 static inline int sem_compare_and_swap(old_sem_t *sem, long oldval, long newval)
00045 {
00046     return compare_and_swap(&sem->sem_status, oldval, newval, &sem->sem_spinlock);
00047 }
00048 
00049 /* The state of a semaphore is represented by a long int encoding
00050    either the semaphore count if >= 0 and no thread is waiting on it,
00051    or the head of the list of threads waiting for the semaphore.
00052    To distinguish the two cases, we encode the semaphore count N
00053    as 2N+1, so that it has the lowest bit set.
00054 
00055    A sequence of sem_wait operations on a semaphore initialized to N
00056    result in the following successive states:
00057      2N+1, 2N-1, ..., 3, 1, &first_waiting_thread, &second_waiting_thread, ...
00058 */
00059 
00060 static void sem_restart_list(pthread_descr waiting);
00061 
00062 int __old_sem_init(old_sem_t *sem, int pshared, unsigned int value)
00063 {
00064     if (value > SEM_VALUE_MAX) {
00065        errno = EINVAL;
00066        return -1;
00067     }
00068     if (pshared) {
00069        errno = ENOSYS;
00070        return -1;
00071     }
00072   sem->sem_spinlock = __LT_SPINLOCK_INIT;
00073   sem->sem_status = ((long)value << 1) + 1;
00074   return 0;
00075 }
00076 
00077 /* Function called by pthread_cancel to remove the thread from
00078    waiting inside __old_sem_wait. Here we simply unconditionally
00079    indicate that the thread is to be woken, by returning 1. */
00080 
00081 static int old_sem_extricate_func(void *obj, pthread_descr th)
00082 {
00083     return 1;
00084 }
00085 
00086 int __old_sem_wait(old_sem_t * sem)
00087 {
00088     long oldstatus, newstatus;
00089     volatile pthread_descr self = thread_self();
00090     pthread_descr * th;
00091     pthread_extricate_if extr;
00092 
00093     /* Set up extrication interface */
00094     extr.pu_object = 0;
00095     extr.pu_extricate_func = old_sem_extricate_func;
00096 
00097     while (1) {
00098        /* Register extrication interface */
00099        __pthread_set_own_extricate_if(self, &extr);
00100        do {
00101             oldstatus = sem->sem_status;
00102             if ((oldstatus & 1) && (oldstatus != 1))
00103               newstatus = oldstatus - 2;
00104             else {
00105               newstatus = (long) self;
00106               self->p_nextwaiting = (pthread_descr) oldstatus;
00107            }
00108        }
00109        while (! sem_compare_and_swap(sem, oldstatus, newstatus));
00110        if (newstatus & 1) {
00111            /* We got the semaphore. */
00112            __pthread_set_own_extricate_if(self, 0);
00113            self->p_nextwaiting = NULL;
00114            return 0;
00115        }
00116        /* Wait for sem_post or cancellation */
00117        suspend(self);
00118        __pthread_set_own_extricate_if(self, 0);
00119 
00120        /* This is a cancellation point */
00121        if (self->p_canceled && self->p_cancelstate == PTHREAD_CANCEL_ENABLE) {
00122            /* Remove ourselves from the waiting list if we're still on it */
00123            /* First check if we're at the head of the list. */
00124             do {
00125               oldstatus = sem->sem_status;
00126               if (oldstatus != (long) self) break;
00127               newstatus = (long) self->p_nextwaiting;
00128            }
00129             while (! sem_compare_and_swap(sem, oldstatus, newstatus));
00130             /* Now, check if we're somewhere in the list.
00131               There's a race condition with sem_post here, but it does not matter:
00132               the net result is that at the time pthread_exit is called,
00133               self is no longer reachable from sem->sem_status. */
00134             if (oldstatus != (long) self && (oldstatus & 1) == 0) {
00135               for (th = &(((pthread_descr) oldstatus)->p_nextwaiting);
00136                    *th != NULL && *th != (pthread_descr) 1;
00137                    th = &((*th)->p_nextwaiting)) {
00138                   if (*th == self) {
00139                      *th = self->p_nextwaiting;
00140                      break;
00141                   }
00142               }
00143            }
00144             __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
00145        }
00146     }
00147 }
00148 
00149 int __old_sem_trywait(old_sem_t * sem)
00150 {
00151   long oldstatus, newstatus;
00152 
00153   do {
00154     oldstatus = sem->sem_status;
00155     if ((oldstatus & 1) == 0 || (oldstatus == 1)) {
00156       errno = EAGAIN;
00157       return -1;
00158     }
00159     newstatus = oldstatus - 2;
00160   }
00161   while (! sem_compare_and_swap(sem, oldstatus, newstatus));
00162   return 0;
00163 }
00164 
00165 int __old_sem_post(old_sem_t * sem)
00166 {
00167   long oldstatus, newstatus;
00168 
00169   do {
00170     oldstatus = sem->sem_status;
00171     if ((oldstatus & 1) == 0)
00172       newstatus = 3;
00173     else {
00174       if (oldstatus >= SEM_VALUE_MAX) {
00175         /* Overflow */
00176         errno = ERANGE;
00177         return -1;
00178       }
00179       newstatus = oldstatus + 2;
00180     }
00181   }
00182   while (! sem_compare_and_swap(sem, oldstatus, newstatus));
00183   if ((oldstatus & 1) == 0)
00184     sem_restart_list((pthread_descr) oldstatus);
00185   return 0;
00186 }
00187 
00188 int __old_sem_getvalue(old_sem_t * sem, int * sval)
00189 {
00190   long status = sem->sem_status;
00191   if (status & 1)
00192     *sval = (int)((unsigned long) status >> 1);
00193   else
00194     *sval = 0;
00195   return 0;
00196 }
00197 
00198 int __old_sem_destroy(old_sem_t * sem)
00199 {
00200   if ((sem->sem_status & 1) == 0) {
00201     errno = EBUSY;
00202     return -1;
00203   }
00204   return 0;
00205 }
00206 
00207 /* Auxiliary function for restarting all threads on a waiting list,
00208    in priority order. */
00209 
00210 static void sem_restart_list(pthread_descr waiting)
00211 {
00212   pthread_descr th, towake, *p;
00213 
00214   /* Sort list of waiting threads by decreasing priority (insertion sort) */
00215   towake = NULL;
00216   while (waiting != (pthread_descr) 1) {
00217     th = waiting;
00218     waiting = waiting->p_nextwaiting;
00219     p = &towake;
00220     while (*p != NULL && th->p_priority < (*p)->p_priority)
00221       p = &((*p)->p_nextwaiting);
00222     th->p_nextwaiting = *p;
00223     *p = th;
00224   }
00225   /* Wake up threads in priority order */
00226   while (towake != NULL) {
00227     th = towake;
00228     towake = towake->p_nextwaiting;
00229     th->p_nextwaiting = NULL;
00230     restart(th);
00231   }
00232 }
00233 
00234 compat_symbol (libpthread, __old_sem_init, sem_init, GLIBC_2_0);
00235 compat_symbol (libpthread, __old_sem_wait, sem_wait, GLIBC_2_0);
00236 compat_symbol (libpthread, __old_sem_trywait, sem_trywait, GLIBC_2_0);
00237 compat_symbol (libpthread, __old_sem_post, sem_post, GLIBC_2_0);
00238 compat_symbol (libpthread, __old_sem_getvalue, sem_getvalue, GLIBC_2_0);
00239 compat_symbol (libpthread, __old_sem_destroy, sem_destroy, GLIBC_2_0);
00240 
00241 #endif