usermode/library/malloc-original/benchmarks/larson/thread-m.h

00001 /* Basic platform-independent macro definitions for mutexes and
00002    thread-specific data.
00003    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Wolfram Gloger <wmglo@dent.med.uni-muenchen.de>, 1996.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public License as
00009    published by the Free Software Foundation; either version 2 of the
00010    License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00019    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020    Boston, MA 02111-1307, USA.  */
00021 
00022 /* One out of _LIBC, USE_PTHREADS, USE_THR, USE_SPROC or WIN32 should be
00023    defined, otherwise the token NO_THREADS and dummy implementations
00024    of the macros will be defined.  */
00025 
00026 #ifndef _THREAD_M_H
00027 #define _THREAD_M_H
00028 
00029 #if defined(_LIBC) /* The GNU C library, a special case of Posix threads */
00030 
00031 #include <libc-lock.h>
00032 
00033 #ifdef PTHREAD_MUTEX_INITIALIZER
00034 
00035 typedef pthread_t thread_id;
00036 
00037 /* mutex */
00038 typedef pthread_mutex_t mutex_t;
00039 
00040 /* thread specific data */
00041 typedef pthread_key_t tsd_key_t;
00042 
00043 #define MUTEX_INITIALIZER       PTHREAD_MUTEX_INITIALIZER
00044 
00045 static Void_t *malloc_key_data;
00046 
00047 #define tsd_key_create(key, destr) \
00048   if (__pthread_key_create != NULL) {                                         \
00049     __pthread_key_create(key, destr);                                         \
00050   } else { *(key) = (tsd_key_t) 0; }
00051 #define tsd_setspecific(key, data) \
00052   if (__pthread_setspecific != NULL) {                                        \
00053     __pthread_setspecific(key, data);                                         \
00054   } else { malloc_key_data = (Void_t *) data; }
00055 #define tsd_getspecific(key, vptr) \
00056   (vptr = (__pthread_getspecific != NULL                                      \
00057            ? __pthread_getspecific(key) : malloc_key_data))
00058 
00059 #define mutex_init(m)           \
00060    (__pthread_mutex_init != NULL ? __pthread_mutex_init (m, NULL) : 0)
00061 #define mutex_lock(m)           \
00062    (__pthread_mutex_lock != NULL ? __pthread_mutex_lock (m) : 0)
00063 #define mutex_trylock(m)        \
00064    (__pthread_mutex_trylock != NULL ? __pthread_mutex_trylock (m) : 0)
00065 #define mutex_unlock(m)         \
00066    (__pthread_mutex_unlock != NULL ? __pthread_mutex_unlock (m) : 0)
00067 
00068 #elif defined(MUTEX_INITIALIZER)
00069 /* Assume hurd, with cthreads */
00070 
00071 /* Cthreads `mutex_t' is a pointer to a mutex, and malloc wants just the
00072    mutex itself.  */
00073 #undef mutex_t
00074 #define mutex_t struct mutex
00075 
00076 #undef mutex_lock
00077 #define mutex_lock(m) (__mutex_lock(m), 0)
00078 
00079 #undef mutex_unlock
00080 #define mutex_unlock(m) (__mutex_unlock(m), 0)
00081 
00082 #define mutex_trylock(m) (!__mutex_trylock(m))
00083 
00084 #include <hurd/threadvar.h>
00085 
00086 /* thread specific data */
00087 typedef int tsd_key_t;
00088 
00089 static int tsd_keys_alloced = 0;
00090 
00091 #define tsd_key_create(key, destr) \
00092   (assert (tsd_keys_alloced == 0), tsd_keys_alloced++)
00093 #define tsd_setspecific(key, data) \
00094   (*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC) = (unsigned long)(data))
00095 #define tsd_getspecific(key, vptr) \
00096   ((vptr) = (void *)*__hurd_threadvar_location (_HURD_THREADVAR_MALLOC))
00097 
00098 /* No we're *not* using pthreads.  */
00099 #define __pthread_initialize ((void (*)(void))0)
00100 
00101 #else
00102 
00103 #define NO_THREADS
00104 
00105 #endif /* MUTEX_INITIALIZER && PTHREAD_MUTEX_INITIALIZER */
00106 
00107 #elif defined(USE_PTHREADS) /* Posix threads */
00108 
00109 #include <pthread.h>
00110 
00111 typedef pthread_t thread_id;
00112 
00113 /* mutex */
00114 typedef pthread_mutex_t mutex_t;
00115 
00116 #define MUTEX_INITIALIZER          PTHREAD_MUTEX_INITIALIZER
00117 #define mutex_init(m)              pthread_mutex_init(m, NULL)
00118 #define mutex_lock(m)              pthread_mutex_lock(m)
00119 #define mutex_trylock(m)           pthread_mutex_trylock(m)
00120 #define mutex_unlock(m)            pthread_mutex_unlock(m)
00121 
00122 /* thread specific data */
00123 typedef pthread_key_t tsd_key_t;
00124 
00125 #define tsd_key_create(key, destr) pthread_key_create(key, destr)
00126 #define tsd_setspecific(key, data) pthread_setspecific(key, data)
00127 #define tsd_getspecific(key, vptr) (vptr = pthread_getspecific(key))
00128 
00129 #elif USE_THR /* Solaris threads */
00130 
00131 #include <thread.h>
00132 
00133 typedef thread_t thread_id;
00134 
00135 #define MUTEX_INITIALIZER          { 0 }
00136 #define mutex_init(m)              mutex_init(m, USYNC_THREAD, NULL)
00137 
00138 /*
00139  * Hack for thread-specific data on Solaris.  We can't use thr_setspecific
00140  * because that function calls malloc() itself.
00141  */
00142 typedef void *tsd_key_t[256];
00143 #define tsd_key_create(key, destr) do { \
00144   int i; \
00145   for(i=0; i<256; i++) (*key)[i] = 0; \
00146 } while(0)
00147 #define tsd_setspecific(key, data) (key[(unsigned)thr_self() % 256] = (data))
00148 #define tsd_getspecific(key, vptr) (vptr = key[(unsigned)thr_self() % 256])
00149 
00150 #elif USE_SPROC /* SGI sproc() threads */
00151 
00152 #include <sys/wait.h>
00153 #include <sys/types.h>
00154 #include <sys/prctl.h>
00155 #include <abi_mutex.h>
00156 
00157 typedef int thread_id;
00158 
00159 typedef abilock_t mutex_t;
00160 
00161 #define MUTEX_INITIALIZER          { 0 }
00162 #define mutex_init(m)              init_lock(m)
00163 #define mutex_lock(m)              (spin_lock(m), 0)
00164 #define mutex_trylock(m)           acquire_lock(m)
00165 #define mutex_unlock(m)            release_lock(m)
00166 
00167 typedef int tsd_key_t;
00168 int tsd_key_next;
00169 #define tsd_key_create(key, destr) ((*key) = tsd_key_next++)
00170 #define tsd_setspecific(key, data) (((void **)(&PRDA->usr_prda))[key] = data)
00171 #define tsd_getspecific(key, vptr) (vptr = ((void **)(&PRDA->usr_prda))[key])
00172 
00173 
00174 #elif WIN32 /* WIN32 threads + Paul's spin locks*/
00175 
00176 #include <windows.h>
00177 #include "parallel.h"
00178 
00179 typedef DWORD thread_id;
00180 
00181 #ifdef WIN32_CRITICAL_SECTIONS
00182 
00183 typedef CRITICAL_SECTION  mutex_t;
00184 
00185 _inline int EnterCS( LPCRITICAL_SECTION pcs )
00186 { EnterCriticalSection(pcs);
00187         return(0) ;
00188 }
00189 _inline int TryEnterCS( LPCRITICAL_SECTION pcs )
00190 { int val = TryEnterCriticalSection(pcs);
00191         return(!val) ;
00192 }
00193 _inline int LeaveCS( LPCRITICAL_SECTION pcs )
00194 { LeaveCriticalSection(pcs);
00195         return(0) ;
00196 }
00197 
00198 #define MUTEX_INITIALIZER          { 0 }
00199 #define mutex_init(m)              InitializeCriticalSection(m)
00200 #define mutex_lock(m)              EnterCS(m)  
00201 #define mutex_trylock(m)           TryEnterCS(m)
00202 #define mutex_unlock(m)            LeaveCS(m)
00203 
00204 #else
00205 
00206 typedef splock_t mutex_t;
00207 
00208 #define MUTEX_INITIALIZER          { L_UNLOCKED }
00209 #define mutex_init(m)              S_INIT_LOCK(m)
00210 #define mutex_lock(m)              S_LOCK(m)
00211 #define mutex_trylock(m)           S_TRY_LOCK(m)
00212 #define mutex_unlock(m)            S_UNLOCK(m)
00213 
00214 #endif
00215 
00216 #define tsd_key_t __declspec(thread) Void_t *
00217 
00218 #define tsd_key_create(key, destr) ( *key = NULL )
00219 #define tsd_setspecific(key, data) ( key = data )
00220 #define tsd_getspecific(key, vptr) ( vptr = key )
00221 
00222 #else /* no _LIBC or USE_... are defined */
00223 
00224 #define NO_THREADS
00225 
00226 #endif /* defined(_LIBC) */
00227 
00228 #ifdef NO_THREADS /* No threads, provide dummy macros */
00229 
00230 typedef int thread_id;
00231 
00232 typedef int mutex_t;
00233 
00234 #define MUTEX_INITIALIZER          0
00235 #define mutex_init(m)              (*(m) = 0)
00236 #define mutex_lock(m)              (0)
00237 #define mutex_trylock(m)           (0)
00238 #define mutex_unlock(m)            (0)
00239 
00240 typedef void *tsd_key_t;
00241 #define tsd_key_create(key, destr) do {} while(0)
00242 #define tsd_setspecific(key, data) do {} while(0)
00243 #define tsd_getspecific(key, vptr) (vptr = NULL)
00244 
00245 #endif /* defined(NO_THREADS) */
00246 
00247 #endif /* !defined(_THREAD_M_H) */

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7