Back to index

glibc  2.9
mutex.c
Go to the documentation of this file.
00001 /* Linuxthreads - a simple clone()-based implementation of Posix        */
00002 /* threads for Linux.                                                   */
00003 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
00004 /*                                                                      */
00005 /* This program is free software; you can redistribute it and/or        */
00006 /* modify it under the terms of the GNU Library General Public License  */
00007 /* as published by the Free Software Foundation; either version 2       */
00008 /* of the License, or (at your option) any later version.               */
00009 /*                                                                      */
00010 /* This program is distributed in the hope that it will be useful,      */
00011 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
00012 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
00013 /* GNU Library General Public License for more details.                 */
00014 
00015 /* Mutexes */
00016 
00017 #include <bits/libc-lock.h>
00018 #include <errno.h>
00019 #include <sched.h>
00020 #include <stddef.h>
00021 #include <limits.h>
00022 #include "pthread.h"
00023 #include "internals.h"
00024 #include "spinlock.h"
00025 #include "queue.h"
00026 #include "restart.h"
00027 
00028 int __pthread_mutex_init(pthread_mutex_t * mutex,
00029                        const pthread_mutexattr_t * mutex_attr)
00030 {
00031   __pthread_init_lock(&mutex->__m_lock);
00032   mutex->__m_kind =
00033     mutex_attr == NULL ? PTHREAD_MUTEX_TIMED_NP : mutex_attr->__mutexkind;
00034   mutex->__m_count = 0;
00035   mutex->__m_owner = NULL;
00036   return 0;
00037 }
00038 strong_alias (__pthread_mutex_init, pthread_mutex_init)
00039 hidden_def (__pthread_mutex_init)
00040 
00041 int __pthread_mutex_destroy(pthread_mutex_t * mutex)
00042 {
00043   switch (mutex->__m_kind) {
00044   case PTHREAD_MUTEX_ADAPTIVE_NP:
00045   case PTHREAD_MUTEX_RECURSIVE_NP:
00046     if ((mutex->__m_lock.__status & 1) != 0)
00047       return EBUSY;
00048     return 0;
00049   case PTHREAD_MUTEX_ERRORCHECK_NP:
00050   case PTHREAD_MUTEX_TIMED_NP:
00051     if (mutex->__m_lock.__status != 0)
00052       return EBUSY;
00053     return 0;
00054   default:
00055     return EINVAL;
00056   }
00057 }
00058 strong_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
00059 hidden_def (__pthread_mutex_destroy)
00060 
00061 int __pthread_mutex_trylock(pthread_mutex_t * mutex)
00062 {
00063   pthread_descr self;
00064   int retcode;
00065 
00066   switch(mutex->__m_kind) {
00067   case PTHREAD_MUTEX_ADAPTIVE_NP:
00068     retcode = __pthread_trylock(&mutex->__m_lock);
00069     return retcode;
00070   case PTHREAD_MUTEX_RECURSIVE_NP:
00071     self = thread_self();
00072     if (mutex->__m_owner == self) {
00073       mutex->__m_count++;
00074       return 0;
00075     }
00076     retcode = __pthread_trylock(&mutex->__m_lock);
00077     if (retcode == 0) {
00078       mutex->__m_owner = self;
00079       mutex->__m_count = 0;
00080     }
00081     return retcode;
00082   case PTHREAD_MUTEX_ERRORCHECK_NP:
00083     retcode = __pthread_alt_trylock(&mutex->__m_lock);
00084     if (retcode == 0) {
00085       mutex->__m_owner = thread_self();
00086     }
00087     return retcode;
00088   case PTHREAD_MUTEX_TIMED_NP:
00089     retcode = __pthread_alt_trylock(&mutex->__m_lock);
00090     return retcode;
00091   default:
00092     return EINVAL;
00093   }
00094 }
00095 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
00096 hidden_def (__pthread_mutex_trylock)
00097 
00098 int __pthread_mutex_lock(pthread_mutex_t * mutex)
00099 {
00100   pthread_descr self;
00101 
00102   switch(mutex->__m_kind) {
00103   case PTHREAD_MUTEX_ADAPTIVE_NP:
00104     __pthread_lock(&mutex->__m_lock, NULL);
00105     return 0;
00106   case PTHREAD_MUTEX_RECURSIVE_NP:
00107     self = thread_self();
00108     if (mutex->__m_owner == self) {
00109       mutex->__m_count++;
00110       return 0;
00111     }
00112     __pthread_lock(&mutex->__m_lock, self);
00113     mutex->__m_owner = self;
00114     mutex->__m_count = 0;
00115     return 0;
00116   case PTHREAD_MUTEX_ERRORCHECK_NP:
00117     self = thread_self();
00118     if (mutex->__m_owner == self) return EDEADLK;
00119     __pthread_alt_lock(&mutex->__m_lock, self);
00120     mutex->__m_owner = self;
00121     return 0;
00122   case PTHREAD_MUTEX_TIMED_NP:
00123     __pthread_alt_lock(&mutex->__m_lock, NULL);
00124     return 0;
00125   default:
00126     return EINVAL;
00127   }
00128 }
00129 strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
00130 hidden_def (__pthread_mutex_lock)
00131 
00132 int __pthread_mutex_timedlock (pthread_mutex_t *mutex,
00133                             const struct timespec *abstime)
00134 {
00135   pthread_descr self;
00136   int res;
00137 
00138   if (__builtin_expect (abstime->tv_nsec, 0) < 0
00139       || __builtin_expect (abstime->tv_nsec, 0) >= 1000000000)
00140     return EINVAL;
00141 
00142   switch(mutex->__m_kind) {
00143   case PTHREAD_MUTEX_ADAPTIVE_NP:
00144     __pthread_lock(&mutex->__m_lock, NULL);
00145     return 0;
00146   case PTHREAD_MUTEX_RECURSIVE_NP:
00147     self = thread_self();
00148     if (mutex->__m_owner == self) {
00149       mutex->__m_count++;
00150       return 0;
00151     }
00152     __pthread_lock(&mutex->__m_lock, self);
00153     mutex->__m_owner = self;
00154     mutex->__m_count = 0;
00155     return 0;
00156   case PTHREAD_MUTEX_ERRORCHECK_NP:
00157     self = thread_self();
00158     if (mutex->__m_owner == self) return EDEADLK;
00159     res = __pthread_alt_timedlock(&mutex->__m_lock, self, abstime);
00160     if (res != 0)
00161       {
00162        mutex->__m_owner = self;
00163        return 0;
00164       }
00165     return ETIMEDOUT;
00166   case PTHREAD_MUTEX_TIMED_NP:
00167     /* Only this type supports timed out lock. */
00168     return (__pthread_alt_timedlock(&mutex->__m_lock, NULL, abstime)
00169            ? 0 : ETIMEDOUT);
00170   default:
00171     return EINVAL;
00172   }
00173 }
00174 strong_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock)
00175 
00176 int __pthread_mutex_unlock(pthread_mutex_t * mutex)
00177 {
00178   switch (mutex->__m_kind) {
00179   case PTHREAD_MUTEX_ADAPTIVE_NP:
00180     __pthread_unlock(&mutex->__m_lock);
00181     return 0;
00182   case PTHREAD_MUTEX_RECURSIVE_NP:
00183     if (mutex->__m_owner != thread_self())
00184       return EPERM;
00185     if (mutex->__m_count > 0) {
00186       mutex->__m_count--;
00187       return 0;
00188     }
00189     mutex->__m_owner = NULL;
00190     __pthread_unlock(&mutex->__m_lock);
00191     return 0;
00192   case PTHREAD_MUTEX_ERRORCHECK_NP:
00193     if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
00194       return EPERM;
00195     mutex->__m_owner = NULL;
00196     __pthread_alt_unlock(&mutex->__m_lock);
00197     return 0;
00198   case PTHREAD_MUTEX_TIMED_NP:
00199     __pthread_alt_unlock(&mutex->__m_lock);
00200     return 0;
00201   default:
00202     return EINVAL;
00203   }
00204 }
00205 strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
00206 hidden_def (__pthread_mutex_unlock)
00207 
00208 int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
00209 {
00210   attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP;
00211   return 0;
00212 }
00213 strong_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
00214 
00215 int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
00216 {
00217   return 0;
00218 }
00219 strong_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
00220 
00221 int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
00222 {
00223   if (kind != PTHREAD_MUTEX_ADAPTIVE_NP
00224       && kind != PTHREAD_MUTEX_RECURSIVE_NP
00225       && kind != PTHREAD_MUTEX_ERRORCHECK_NP
00226       && kind != PTHREAD_MUTEX_TIMED_NP)
00227     return EINVAL;
00228   attr->__mutexkind = kind;
00229   return 0;
00230 }
00231 weak_alias (__pthread_mutexattr_settype, pthread_mutexattr_settype)
00232 strong_alias ( __pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
00233 weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
00234 
00235 int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
00236 {
00237   *kind = attr->__mutexkind;
00238   return 0;
00239 }
00240 weak_alias (__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
00241 strong_alias (__pthread_mutexattr_gettype, __pthread_mutexattr_getkind_np)
00242 weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
00243 
00244 int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr,
00245                                int *pshared)
00246 {
00247   *pshared = PTHREAD_PROCESS_PRIVATE;
00248   return 0;
00249 }
00250 weak_alias (__pthread_mutexattr_getpshared, pthread_mutexattr_getpshared)
00251 
00252 int __pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared)
00253 {
00254   if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
00255     return EINVAL;
00256 
00257   /* For now it is not possible to shared a conditional variable.  */
00258   if (pshared != PTHREAD_PROCESS_PRIVATE)
00259     return ENOSYS;
00260 
00261   return 0;
00262 }
00263 weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
00264 
00265 /* Once-only execution */
00266 
00267 static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
00268 static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
00269 static int fork_generation = 0;    /* Child process increments this after fork. */
00270 
00271 enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
00272 
00273 /* If a thread is canceled while calling the init_routine out of
00274    pthread once, this handler will reset the once_control variable
00275    to the NEVER state. */
00276 
00277 static void pthread_once_cancelhandler(void *arg)
00278 {
00279     pthread_once_t *once_control = arg;
00280 
00281     pthread_mutex_lock(&once_masterlock);
00282     *once_control = NEVER;
00283     pthread_mutex_unlock(&once_masterlock);
00284     pthread_cond_broadcast(&once_finished);
00285 }
00286 
00287 int __pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
00288 {
00289   /* flag for doing the condition broadcast outside of mutex */
00290   int state_changed;
00291 
00292   /* Test without locking first for speed */
00293   if (*once_control == DONE) {
00294     READ_MEMORY_BARRIER();
00295     return 0;
00296   }
00297   /* Lock and test again */
00298 
00299   state_changed = 0;
00300 
00301   pthread_mutex_lock(&once_masterlock);
00302 
00303   /* If this object was left in an IN_PROGRESS state in a parent
00304      process (indicated by stale generation field), reset it to NEVER. */
00305   if ((*once_control & 3) == IN_PROGRESS && (*once_control & ~3) != fork_generation)
00306     *once_control = NEVER;
00307 
00308   /* If init_routine is being called from another routine, wait until
00309      it completes. */
00310   while ((*once_control & 3) == IN_PROGRESS) {
00311     pthread_cond_wait(&once_finished, &once_masterlock);
00312   }
00313   /* Here *once_control is stable and either NEVER or DONE. */
00314   if (*once_control == NEVER) {
00315     *once_control = IN_PROGRESS | fork_generation;
00316     pthread_mutex_unlock(&once_masterlock);
00317     pthread_cleanup_push(pthread_once_cancelhandler, once_control);
00318     init_routine();
00319     pthread_cleanup_pop(0);
00320     pthread_mutex_lock(&once_masterlock);
00321     WRITE_MEMORY_BARRIER();
00322     *once_control = DONE;
00323     state_changed = 1;
00324   }
00325   pthread_mutex_unlock(&once_masterlock);
00326 
00327   if (state_changed)
00328     pthread_cond_broadcast(&once_finished);
00329 
00330   return 0;
00331 }
00332 strong_alias (__pthread_once, pthread_once)
00333 
00334 /*
00335  * Handle the state of the pthread_once mechanism across forks.  The
00336  * once_masterlock is acquired in the parent process prior to a fork to ensure
00337  * that no thread is in the critical region protected by the lock.  After the
00338  * fork, the lock is released. In the child, the lock and the condition
00339  * variable are simply reset.  The child also increments its generation
00340  * counter which lets pthread_once calls detect stale IN_PROGRESS states
00341  * and reset them back to NEVER.
00342  */
00343 
00344 void __pthread_once_fork_prepare(void)
00345 {
00346   pthread_mutex_lock(&once_masterlock);
00347 }
00348 
00349 void __pthread_once_fork_parent(void)
00350 {
00351   pthread_mutex_unlock(&once_masterlock);
00352 }
00353 
00354 void __pthread_once_fork_child(void)
00355 {
00356   pthread_mutex_init(&once_masterlock, NULL);
00357   pthread_cond_init(&once_finished, NULL);
00358   if (fork_generation <= INT_MAX - 4)
00359     fork_generation += 4;   /* leave least significant two bits zero */
00360   else
00361     fork_generation = 0;
00362 }