Back to index

glibc  2.9
spinlock.h
Go to the documentation of this file.
00001 /* Linuxthreads - a simple clone()-based implementation of Posix        */
00002 /* threads for Linux.                                                   */
00003 /* Copyright (C) 1998 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 #include <bits/initspin.h>
00016 
00017 
00018 /* There are 2 compare and swap synchronization primitives with
00019    different semantics:
00020 
00021        1. compare_and_swap, which has acquire semantics (i.e. it
00022        completes befor subsequent writes.)
00023        2. compare_and_swap_with_release_semantics, which has release
00024        semantics (it completes after previous writes.)
00025 
00026    For those platforms on which they are the same. HAS_COMPARE_AND_SWAP
00027    should be defined. For those platforms on which they are different,
00028    HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS has to be defined.  */
00029 
00030 #ifndef HAS_COMPARE_AND_SWAP
00031 #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
00032 #define HAS_COMPARE_AND_SWAP
00033 #endif
00034 #endif
00035 
00036 #if defined(TEST_FOR_COMPARE_AND_SWAP)
00037 
00038 extern int __pthread_has_cas;
00039 extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
00040                                       int * spinlock);
00041 
00042 static inline int compare_and_swap(long * ptr, long oldval, long newval,
00043                                    int * spinlock)
00044 {
00045   if (__builtin_expect (__pthread_has_cas, 1))
00046     return __compare_and_swap(ptr, oldval, newval);
00047   else
00048     return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
00049 }
00050 
00051 #elif defined(HAS_COMPARE_AND_SWAP)
00052 
00053 #ifdef IMPLEMENT_TAS_WITH_CAS
00054 #define testandset(p) !__compare_and_swap((long int *) p, 0, 1)
00055 #endif
00056 
00057 #ifdef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
00058 
00059 static inline int
00060 compare_and_swap_with_release_semantics (long * ptr, long oldval,
00061                                     long newval, int * spinlock)
00062 {
00063   return __compare_and_swap_with_release_semantics (ptr, oldval,
00064                                               newval);
00065 }
00066 
00067 #endif
00068 
00069 static inline int compare_and_swap(long * ptr, long oldval, long newval,
00070                                    int * spinlock)
00071 {
00072   return __compare_and_swap(ptr, oldval, newval);
00073 }
00074 
00075 #else
00076 
00077 extern int __pthread_compare_and_swap(long * ptr, long oldval, long newval,
00078                                       int * spinlock);
00079 
00080 static inline int compare_and_swap(long * ptr, long oldval, long newval,
00081                                    int * spinlock)
00082 {
00083   return __pthread_compare_and_swap(ptr, oldval, newval, spinlock);
00084 }
00085 
00086 #endif
00087 
00088 #ifndef HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
00089 #define compare_and_swap_with_release_semantics compare_and_swap
00090 #define __compare_and_swap_with_release_semantics __compare_and_swap
00091 #endif
00092 
00093 /* Internal locks */
00094 
00095 extern void internal_function __pthread_lock(struct _pthread_fastlock * lock,
00096                                         pthread_descr self);
00097 extern int __pthread_unlock(struct _pthread_fastlock *lock);
00098 
00099 static inline void __pthread_init_lock(struct _pthread_fastlock * lock)
00100 {
00101   lock->__status = 0;
00102   lock->__spinlock = __LT_SPINLOCK_INIT;
00103 }
00104 
00105 static inline int __pthread_trylock (struct _pthread_fastlock * lock)
00106 {
00107 #if defined TEST_FOR_COMPARE_AND_SWAP
00108   if (!__pthread_has_cas)
00109 #endif
00110 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
00111   {
00112     return (testandset(&lock->__spinlock) ? EBUSY : 0);
00113   }
00114 #endif
00115 
00116 #if defined HAS_COMPARE_AND_SWAP
00117   do {
00118     if (lock->__status != 0) return EBUSY;
00119   } while(! __compare_and_swap(&lock->__status, 0, 1));
00120   return 0;
00121 #endif
00122 }
00123 
00124 /* Variation of internal lock used for pthread_mutex_t, supporting
00125    timed-out waits.  Warning: do not mix these operations with the above ones
00126    over the same lock object! */
00127 
00128 extern void __pthread_alt_lock(struct _pthread_fastlock * lock,
00129                             pthread_descr self);
00130 
00131 extern int __pthread_alt_timedlock(struct _pthread_fastlock * lock,
00132                             pthread_descr self, const struct timespec *abstime);
00133 
00134 extern void __pthread_alt_unlock(struct _pthread_fastlock *lock);
00135 
00136 static inline void __pthread_alt_init_lock(struct _pthread_fastlock * lock)
00137 {
00138   lock->__status = 0;
00139   lock->__spinlock = __LT_SPINLOCK_INIT;
00140 }
00141 
00142 static inline int __pthread_alt_trylock (struct _pthread_fastlock * lock)
00143 {
00144 #if defined TEST_FOR_COMPARE_AND_SWAP
00145   if (!__pthread_has_cas)
00146 #endif
00147 #if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
00148   {
00149     int res = EBUSY;
00150 
00151     if (testandset(&lock->__spinlock) == 0)
00152       {
00153        if (lock->__status == 0)
00154          {
00155            lock->__status = 1;
00156            WRITE_MEMORY_BARRIER();
00157            res = 0;
00158          }
00159        lock->__spinlock = __LT_SPINLOCK_INIT;
00160       }
00161     return res;
00162   }
00163 #endif
00164 
00165 #if defined HAS_COMPARE_AND_SWAP
00166   do {
00167     if (lock->__status != 0) return EBUSY;
00168   } while(! compare_and_swap(&lock->__status, 0, 1, &lock->__spinlock));
00169   return 0;
00170 #endif
00171 }
00172 
00173 /* Operations on pthread_atomic, which is defined in internals.h */
00174 
00175 static inline long
00176 pthread_atomic_increment (struct pthread_atomic *pa)
00177 {
00178     long oldval;
00179 
00180     do {
00181        oldval = pa->p_count;
00182     } while (!compare_and_swap(&pa->p_count, oldval, oldval + 1, &pa->p_spinlock));
00183 
00184     return oldval;
00185 }
00186 
00187 
00188 static inline long
00189 pthread_atomic_decrement (struct pthread_atomic *pa)
00190 {
00191     long oldval;
00192 
00193     do {
00194        oldval = pa->p_count;
00195     } while (!compare_and_swap(&pa->p_count, oldval, oldval - 1, &pa->p_spinlock));
00196 
00197     return oldval;
00198 }
00199 
00200 
00201 static inline __attribute__((always_inline)) void
00202 __pthread_set_own_extricate_if (pthread_descr self, pthread_extricate_if *peif)
00203 {
00204   /* Only store a non-null peif if the thread has cancellation enabled.
00205      Otherwise pthread_cancel will unconditionally call the extricate handler,
00206      and restart the thread giving rise to forbidden spurious wakeups. */
00207   if (peif == NULL
00208       || THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)
00209     {
00210       /* If we are removing the extricate interface, we need to synchronize
00211         against pthread_cancel so that it does not continue with a pointer
00212          to a deallocated pthread_extricate_if struct! The thread lock
00213          is (ab)used for this synchronization purpose. */
00214       if (peif == NULL)
00215        __pthread_lock (THREAD_GETMEM(self, p_lock), self);
00216       THREAD_SETMEM(self, p_extricate, peif);
00217       if (peif == NULL)
00218        __pthread_unlock (THREAD_GETMEM(self, p_lock));
00219     }
00220 }