00001 #ifndef __JBMW_THREAD_H_
00002 #define __JBMW_THREAD_H_
00003
00004 #include <pthread.h>
00005
00006 namespace mdw
00007 {
00008 class ThreadStatic
00009 {
00010 public:
00011 typedef pthread_t thread_t;
00012 typedef pthread_attr_t thread_attr_t;
00013 typedef pthread_mutex_t mutex_t;
00014 typedef pthread_mutexattr_t mutex_attr_t;
00015 typedef pthread_cond_t cond_t;
00016 typedef pthread_condattr_t cond_attr_t;
00017 typedef pthread_key_t key_t;
00018 typedef pthread_rwlock_t rwlock_t;
00019 typedef pthread_rwlockattr_t rwlock_attr_t;
00020
00021 static thread_attr_t our_default_thread_attr;
00022 static mutex_attr_t our_default_mutex_attr;
00023 static cond_attr_t our_default_cond_attr;
00024 static rwlock_attr_t our_default_rwlock_attr;
00025
00026 class Guard
00027 {
00028 public:
00029 Guard (mutex_t &);
00030 ~Guard();
00031 private:
00032 mutex_t &myMutex;
00033 };
00034
00035 class GuardRead
00036 {
00037 public:
00038 GuardRead (rwlock_t &);
00039 ~GuardRead();
00040 private:
00041 rwlock_t &myLock;
00042 };
00043
00044 class GuardWrite
00045 {
00046 public:
00047 GuardWrite (rwlock_t &);
00048 ~GuardWrite();
00049 private:
00050 rwlock_t &myLock;
00051 };
00052
00053 typedef enum
00054 {
00055 Detached_e = PTHREAD_CREATE_DETACHED,
00056 Joinable_e = PTHREAD_CREATE_JOINABLE
00057 } AttachedState_t;
00058
00059
00060 static void initialise();
00061
00062
00063 static void finalize();
00064
00065
00066
00067
00068 static int create (thread_t &thread,
00069 void * (*start_routine) (void*),
00070 void *arg,
00071 thread_attr_t &attr = our_default_thread_attr);
00072
00073
00074 static void exit (void *);
00075
00076
00077 static int cancel (thread_t);
00078
00079
00080 static int join (thread_t, void **);
00081
00082
00083 static int setdetachstate (thread_attr_t &, AttachedState_t);
00084
00085
00086 static int getdetachstate (const thread_attr_t &, AttachedState_t &);
00087
00088
00089
00090
00091
00092 static int mutex_init (mutex_t &, const mutex_attr_t &attr = our_default_mutex_attr);
00093
00094
00095 static int mutex_destroy (mutex_t &);
00096
00097
00098 static int mutex_lock (mutex_t &);
00099
00100
00101 static int mutex_trylock (mutex_t &);
00102
00103
00104 static int mutex_unlock (mutex_t &);
00105
00106
00107 static int cond_init (cond_t &, const cond_attr_t &attr = our_default_cond_attr);
00108
00109
00110 static int cond_destroy (cond_t &);
00111
00112
00113 static int cond_signal (cond_t &);
00114
00115
00116 static int cond_wait (cond_t &, mutex_t &);
00117
00118
00119 static int cond_timedwait (cond_t &, mutex_t &, const struct timespec &);
00120
00121
00122
00123
00124 static int key_create (key_t &, void (*) (void *));
00125
00126
00127 static int setspecific (key_t, const void *);
00128
00129
00130 static void *getspecific (key_t);
00131
00132
00133 static int key_delete (key_t);
00134
00135
00136
00137
00138
00139 static bool equal (thread_t, thread_t);
00140
00141
00142 static int detach (thread_t);
00143
00144
00145 static thread_t self();
00146
00147
00148
00149
00150 static int rwlock_init (rwlock_t &, const rwlock_attr_t &attr = our_default_rwlock_attr);
00151
00152
00153 static int rwlock_destroy (rwlock_t &);
00154
00155
00156 static int rwlock_rdlock (rwlock_t &);
00157
00158
00159 static int rwlock_tryrdlock (rwlock_t &);
00160
00161
00162 static int rwlock_wrlock (rwlock_t &);
00163
00164
00165 static int rwlock_trywrlock (rwlock_t &);
00166
00167
00168 static int rwlock_unlock (rwlock_t &);
00169
00170
00171
00172
00173 static int thread_attr_init (thread_attr_t &);
00174 static int mutex_attr_init (mutex_attr_t&);
00175 static int cond_attr_init (cond_attr_t&);
00176 static int rwlock_attr_init (rwlock_attr_t&);
00177
00178
00179 static int thread_attr_destroy (thread_attr_t &);
00180 static int mutex_attr_destroy (mutex_attr_t&);
00181 static int cond_attr_destroy (cond_attr_t&);
00182 static int rwlock_attr_destroy (rwlock_attr_t&);
00183
00184 };
00185 }
00186 #endif
00187