dthread-unix.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "common/h/dthread.h"
00032 #include <pthread.h>
00033 #include <assert.h>
00034
00035 DThread::DThread() :
00036 live(false)
00037 {
00038 }
00039
00040 DThread::~DThread()
00041 {
00042 }
00043
00044 typedef struct {
00045 DThread::initial_func_t func;
00046 void *param;
00047 } initial_data;
00048
00049 static void *thread_init(void *d) {
00050 assert(d);
00051 initial_data *data = (initial_data *) d;
00052 DThread::initial_func_t func = data->func;
00053 void *param = data->param;
00054 delete data;
00055 func(param);
00056 return NULL;
00057 }
00058
00059 bool DThread::spawn(initial_func_t func, void *param)
00060 {
00061 initial_data *data = new initial_data();
00062 data->func = func;
00063 data->param = param;
00064 int result = pthread_create(&thrd, NULL, thread_init, data);
00065 assert(result == 0);
00066 live = true;
00067 return true;
00068 }
00069
00070 bool DThread::join()
00071 {
00072 assert(live && pthread_self() != thrd);
00073 int result = pthread_join(thrd, NULL);
00074 return (result == 0);
00075 }
00076
00077 long DThread::id()
00078 {
00079 if (!live) return 0;
00080 return (long) thrd;
00081 }
00082
00083 long DThread::self()
00084 {
00085 return (long) pthread_self();
00086 }
00087
00088 Mutex::Mutex(bool recursive)
00089 {
00090 pthread_mutexattr_t attr;
00091 pthread_mutexattr_init(&attr);
00092 if (recursive) {
00093 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
00094 }
00095 int result = pthread_mutex_init(&mutex, &attr);
00096 assert(result == 0);
00097
00098 pthread_mutexattr_destroy(&attr);
00099 }
00100
00101 #include <stdio.h>
00102 #include <errno.h>
00103 #include <string.h>
00104
00105
00106 Mutex::~Mutex()
00107 {
00108 int result = pthread_mutex_destroy(&mutex);
00109 assert(result == 0);
00110 }
00111
00112 bool Mutex::lock()
00113 {
00114 int result = pthread_mutex_lock(&mutex);
00115 return (result == 0);
00116 }
00117
00118 bool Mutex::trylock()
00119 {
00120 int result = pthread_mutex_trylock(&mutex);
00121 return (result == 0);
00122 }
00123
00124 bool Mutex::unlock()
00125 {
00126 int result = pthread_mutex_unlock(&mutex);
00127 return (result == 0);
00128 }
00129
00130 CondVar::CondVar(Mutex *m)
00131 {
00132 if (!m) {
00133 created_mutex = true;
00134 mutex = new Mutex();
00135 }
00136 else {
00137 created_mutex = false;
00138 mutex = static_cast<Mutex *>(m);
00139 }
00140 pthread_cond_init(&cond, NULL);
00141 }
00142
00143 CondVar::~CondVar()
00144 {
00145 if (created_mutex)
00146 delete mutex;
00147 int result = pthread_cond_destroy(&cond);
00148 assert(result == 0);
00149 }
00150
00151 bool CondVar::unlock()
00152 {
00153 return mutex->unlock();
00154 }
00155
00156 bool CondVar::lock()
00157 {
00158 return mutex->lock();
00159 }
00160
00161 bool CondVar::trylock()
00162 {
00163 return mutex->trylock();
00164 }
00165
00166 bool CondVar::signal()
00167 {
00168 int result = pthread_cond_signal(&cond);
00169 return result == 0;
00170 }
00171
00172 bool CondVar::broadcast()
00173 {
00174 int result = pthread_cond_broadcast(&cond);
00175 return result == 0;
00176 }
00177
00178 bool CondVar::wait()
00179 {
00180 int result = pthread_cond_wait(&cond, &mutex->mutex);
00181 return result == 0;
00182 }
00183