00001 #include "ThreadStatic.h"
00002
00003 namespace mdw
00004 {
00005 ThreadStatic::thread_attr_t ThreadStatic::our_default_thread_attr;
00006 ThreadStatic::mutex_attr_t ThreadStatic::our_default_mutex_attr;
00007 ThreadStatic::cond_attr_t ThreadStatic::our_default_cond_attr;
00008 ThreadStatic::rwlock_attr_t ThreadStatic::our_default_rwlock_attr;
00009
00010 ThreadStatic::Guard::Guard (mutex_t &mutex) : myMutex (mutex)
00011 {
00012 mutex_lock (myMutex);
00013 }
00014
00015 ThreadStatic::Guard::~Guard()
00016 {
00017 mutex_unlock (myMutex);
00018 }
00019
00020 ThreadStatic::GuardRead::GuardRead (rwlock_t &lock) : myLock (lock)
00021 {
00022 rwlock_rdlock (myLock);
00023 }
00024
00025 ThreadStatic::GuardRead::~GuardRead()
00026 {
00027 rwlock_unlock (myLock);
00028 }
00029
00030 ThreadStatic::GuardWrite::GuardWrite (rwlock_t &lock) : myLock (lock)
00031 {
00032 rwlock_wrlock (myLock);
00033 }
00034
00035 ThreadStatic::GuardWrite::~GuardWrite()
00036 {
00037 rwlock_unlock (myLock);
00038 }
00039
00040
00041 void ThreadStatic::initialise()
00042 {
00043 thread_attr_init (our_default_thread_attr);
00044 mutex_attr_init (our_default_mutex_attr);
00045 cond_attr_init (our_default_cond_attr);
00046 rwlock_attr_init (our_default_rwlock_attr);
00047 }
00048
00049
00050 void ThreadStatic::finalize()
00051 {
00052 thread_attr_destroy (our_default_thread_attr);
00053 mutex_attr_destroy (our_default_mutex_attr);
00054 cond_attr_destroy (our_default_cond_attr);
00055 rwlock_attr_destroy (our_default_rwlock_attr);
00056 }
00057
00058
00059
00060
00061 int ThreadStatic::create (ThreadStatic::thread_t &thread,
00062 void * (*start_routine) (void*),
00063 void *arg,
00064 ThreadStatic::thread_attr_t &attr)
00065 {
00066 return pthread_create (&thread, &attr, start_routine, arg);
00067 }
00068
00069
00070 void ThreadStatic::exit (void * result)
00071 {
00072 return pthread_exit (result);
00073 }
00074
00075
00076 int ThreadStatic::cancel (ThreadStatic::thread_t thread)
00077 {
00078 return pthread_cancel (thread);
00079 }
00080
00081
00082 int ThreadStatic::join (ThreadStatic::thread_t thread, void ** result)
00083 {
00084 return pthread_join (thread, result);
00085 }
00086
00087
00088 int ThreadStatic::setdetachstate (ThreadStatic::thread_attr_t &attr, AttachedState_t state)
00089 {
00090 return pthread_attr_setdetachstate (&attr, state);
00091 }
00092
00093
00094 int ThreadStatic::getdetachstate (const ThreadStatic::thread_attr_t &attr, AttachedState_t &state)
00095 {
00096 return pthread_attr_getdetachstate (&attr, (int*) &state);
00097 }
00098
00099
00100
00101
00102
00103 int ThreadStatic::mutex_init (ThreadStatic::mutex_t &mutex, const ThreadStatic::mutex_attr_t &attr)
00104 {
00105 return pthread_mutex_init (&mutex, &attr);
00106 }
00107
00108
00109 int ThreadStatic::mutex_destroy (ThreadStatic::mutex_t &mutex)
00110 {
00111 return pthread_mutex_destroy (&mutex);
00112 }
00113
00114
00115 int ThreadStatic::mutex_lock (ThreadStatic::mutex_t &mutex)
00116 {
00117 return pthread_mutex_lock (&mutex);
00118 }
00119
00120
00121 int ThreadStatic::mutex_trylock (ThreadStatic::mutex_t &mutex)
00122 {
00123 return pthread_mutex_trylock (&mutex);
00124 }
00125
00126
00127 int ThreadStatic::mutex_unlock (ThreadStatic::mutex_t &mutex)
00128 {
00129 return pthread_mutex_unlock (&mutex);
00130 }
00131
00132
00133 int ThreadStatic::cond_init (ThreadStatic::cond_t &cond, const ThreadStatic::cond_attr_t &attr)
00134 {
00135 return pthread_cond_init (&cond, &attr);
00136 }
00137
00138
00139 int ThreadStatic::cond_destroy (ThreadStatic::cond_t &cond)
00140 {
00141 return pthread_cond_destroy (&cond);
00142 }
00143
00144
00145 int ThreadStatic::cond_signal (ThreadStatic::cond_t &cond)
00146 {
00147 return pthread_cond_signal (&cond);
00148 }
00149
00150
00151 int ThreadStatic::cond_wait (ThreadStatic::cond_t &cond, ThreadStatic::mutex_t &mutex)
00152 {
00153 return pthread_cond_wait (&cond, &mutex);
00154 }
00155
00156
00157 int ThreadStatic::cond_timedwait (ThreadStatic::cond_t &cond, ThreadStatic::mutex_t &mutex, const struct timespec &time)
00158 {
00159 return pthread_cond_timedwait (&cond, &mutex, &time);
00160 }
00161
00162
00163
00164
00165 int ThreadStatic::key_create (ThreadStatic::key_t &key, void (*func) (void *))
00166 {
00167 return pthread_key_create (&key, func);
00168 }
00169
00170
00171 int ThreadStatic::setspecific (ThreadStatic::key_t key, const void *value)
00172 {
00173 return pthread_setspecific (key, value);
00174 }
00175
00176
00177 void *ThreadStatic::getspecific (ThreadStatic::key_t key)
00178 {
00179 return pthread_getspecific (key);
00180 }
00181
00182
00183 int ThreadStatic::key_delete (ThreadStatic::key_t key)
00184 {
00185 return pthread_key_delete (key);
00186 }
00187
00188
00189
00190
00191
00192 bool ThreadStatic::equal (ThreadStatic::thread_t thread1, ThreadStatic::thread_t thread2)
00193 {
00194 return (pthread_equal (thread1, thread2) != 0);
00195 }
00196
00197
00198 int ThreadStatic::detach (ThreadStatic::thread_t thread)
00199 {
00200 return pthread_detach (thread);
00201 }
00202
00203
00204 ThreadStatic::thread_t ThreadStatic::self()
00205 {
00206 return pthread_self();
00207 }
00208
00209
00210
00211
00212 int ThreadStatic::rwlock_init (ThreadStatic::rwlock_t &rwlock, const ThreadStatic::rwlock_attr_t &attr)
00213 {
00214 return pthread_rwlock_init (&rwlock, &attr);
00215 }
00216
00217
00218 int ThreadStatic::rwlock_destroy (ThreadStatic::rwlock_t &rwlock)
00219 {
00220 return pthread_rwlock_destroy (&rwlock);
00221 }
00222
00223
00224 int ThreadStatic::rwlock_rdlock (ThreadStatic::rwlock_t &rwlock)
00225 {
00226 return pthread_rwlock_rdlock (&rwlock);
00227 }
00228
00229
00230 int ThreadStatic::rwlock_tryrdlock (ThreadStatic::rwlock_t &rwlock)
00231 {
00232 return pthread_rwlock_tryrdlock (&rwlock);
00233 }
00234
00235
00236 int ThreadStatic::rwlock_wrlock (ThreadStatic::rwlock_t &rwlock)
00237 {
00238 return pthread_rwlock_wrlock (&rwlock);
00239 }
00240
00241
00242 int ThreadStatic::rwlock_trywrlock (ThreadStatic::rwlock_t &rwlock)
00243 {
00244 return pthread_rwlock_trywrlock (&rwlock);
00245 }
00246
00247
00248 int ThreadStatic::rwlock_unlock (ThreadStatic::rwlock_t &rwlock)
00249 {
00250 return pthread_rwlock_unlock (&rwlock);
00251 }
00252
00253
00254
00255
00256 int ThreadStatic::thread_attr_init (ThreadStatic::thread_attr_t &attr)
00257 {
00258 return pthread_attr_init (&attr);
00259 }
00260 int ThreadStatic::mutex_attr_init (ThreadStatic::mutex_attr_t &attr)
00261 {
00262 return pthread_mutexattr_init (&attr);
00263 }
00264 int ThreadStatic::cond_attr_init (ThreadStatic::cond_attr_t &attr)
00265 {
00266 return pthread_condattr_init (&attr);
00267 }
00268 int ThreadStatic::rwlock_attr_init (ThreadStatic::rwlock_attr_t &attr)
00269 {
00270 return pthread_rwlockattr_init (&attr);
00271 }
00272
00273
00274 int ThreadStatic::thread_attr_destroy (ThreadStatic::thread_attr_t &attr)
00275 {
00276 return pthread_attr_destroy (&attr);
00277 }
00278 int ThreadStatic::mutex_attr_destroy (ThreadStatic::mutex_attr_t &attr)
00279 {
00280 return pthread_mutexattr_destroy (&attr);
00281 }
00282 int ThreadStatic::cond_attr_destroy (ThreadStatic::cond_attr_t &attr)
00283 {
00284 return pthread_condattr_destroy (&attr);
00285 }
00286 int ThreadStatic::rwlock_attr_destroy (ThreadStatic::rwlock_attr_t &attr)
00287 {
00288 return pthread_rwlockattr_destroy (&attr);
00289 }
00290 }