00001 #include "threading.h" 00002 #include "dbug_mem.h" 00003 00004 00010 00011 00012 int 00013 thread_create(T_THREAD *thread, T_THREADATTR *attr, void *(*routine)(void *), void *arg) 00014 { 00015 #ifdef WITH_PTHREADS 00016 return pthread_create(thread, attr, routine, arg); 00017 #elif defined(WITH_SDLTHREADS) 00018 *thread = SDL_CreateThread((int(*)(void *))routine, arg); 00019 if(*thread != NULL) { 00020 return 0; 00021 } 00022 return -1; 00023 #else 00024 return 0; 00025 #endif 00026 } 00027 00028 00029 int 00030 thread_join(T_THREAD thread, void **ret) 00031 { 00032 #ifdef WITH_PTHREADS 00033 return pthread_join(thread, ret); 00034 #elif defined(WITH_SDLTHREADS) 00035 return -1; 00036 #else 00037 return 0; 00038 #endif 00039 } 00040 00041 00042 int 00043 thread_exit(void *ret) 00044 { 00045 #ifdef WITH_PTHREADS 00046 pthread_exit(ret); 00047 return 0; 00048 #elif defined(WITH_SDLTHREADS) 00049 return 0; 00050 #else 00051 return 0; 00052 #endif 00053 } 00054 00055 00056 int 00057 thread_cancel(T_THREAD thread) 00058 { 00059 #ifdef WITH_PTHREADS 00060 return pthread_cancel(thread); 00061 #elif defined(WITH_SDLTHREADS) 00062 SDL_KillThread(thread); 00063 return 0; 00064 #else 00065 return 0; 00066 #endif 00067 } 00068 00069 00075 00076 00077 int 00078 thread_cond_init(T_COND *cond, T_CONDATTR *cond_attr) 00079 { 00080 #ifdef WITH_PTHREADS 00081 return pthread_cond_init(cond, cond_attr); 00082 #elif defined(WITH_SDLTHREADS) 00083 *cond = SDL_CreateCond(); 00084 if(*cond != NULL) { 00085 return 0; 00086 } 00087 return -1; 00088 #else 00089 return 0; 00090 #endif 00091 } 00092 00093 00094 int 00095 thread_cond_signal(T_COND *cond) 00096 { 00097 #ifdef WITH_PTHREADS 00098 return pthread_cond_signal(cond); 00099 #elif defined(WITH_SDLTHREADS) 00100 if(*cond != NULL) { 00101 return SDL_CondSignal(*cond); 00102 } 00103 return -1; 00104 #else 00105 return 0; 00106 #endif 00107 } 00108 00109 00110 int 00111 thread_cond_broadcast(T_COND *cond) 00112 { 00113 #ifdef WITH_PTHREADS 00114 return pthread_cond_broadcast(cond); 00115 #elif defined(WITH_SDLTHREADS) 00116 if(*cond != NULL) { 00117 return SDL_CondBroadcast(*cond); 00118 } 00119 return -1; 00120 #else 00121 return 0; 00122 #endif 00123 } 00124 00125 00126 int 00127 thread_cond_wait(T_COND *cond, T_MUTEX *mutex) 00128 { 00129 #ifdef WITH_PTHREADS 00130 return pthread_cond_wait(cond, mutex); 00131 #elif defined(WITH_SDLTHREADS) 00132 if(*cond != NULL) { 00133 return SDL_CondWait(*cond, *mutex); 00134 } 00135 return -1; 00136 #else 00137 return 0; 00138 #endif 00139 } 00140 00141 00142 int 00143 thread_cond_timedwait(T_COND *cond, T_MUTEX *mutex, unsigned int timeout) 00144 { 00145 #ifdef WITH_PTHREADS 00146 struct timeval now; 00147 struct timespec to; 00148 00149 gettimeofday(&now, NULL); 00150 to.tv_sec = now.tv_sec + (long)(timeout/1000); 00151 to.tv_nsec = now.tv_usec * 1000 * (long)((timeout%1000)*1000); 00152 if (pthread_cond_timedwait(cond, mutex, &to) != 0) { 00153 if (errno == ETIMEDOUT) { 00154 return 1; 00155 } 00156 return -1; 00157 } 00158 return 0; 00159 #elif defined(WITH_SDLTHREADS) 00160 int retcode = 0; 00161 00162 if(*cond != NULL) { 00163 retcode = SDL_CondWaitTimeout(*cond, *mutex, timeout); 00164 if (retcode == SDL_MUTEX_TIMEDOUT) { 00165 return 1; 00166 } 00167 return retcode; 00168 } 00169 return -1; 00170 #else 00171 return 0; 00172 #endif 00173 } 00174 00175 00176 int 00177 thread_cond_destroy(T_COND *cond) 00178 { 00179 #ifdef WITH_PTHREADS 00180 return pthread_cond_destroy(cond); 00181 #elif defined(WITH_SDLTHREADS) 00182 if(*cond != NULL) { 00183 SDL_DestroyCond(*cond); 00184 return 0; 00185 } 00186 return -1; 00187 #else 00188 return 0; 00189 #endif 00190 } 00191 00192 00198 00199 00200 int 00201 thread_mutex_init(T_MUTEX *mutex, T_MUTEXATTR *mutexattr) 00202 { 00203 #ifdef WITH_PTHREADS 00204 return pthread_mutex_init(mutex, mutexattr); 00205 #elif defined(WITH_SDLTHREADS) 00206 *mutex = SDL_CreateMutex(); 00207 if(*mutex != NULL) { 00208 return 0; 00209 } 00210 return -1; 00211 #else 00212 return 0; 00213 #endif 00214 } 00215 00216 00217 int 00218 thread_mutex_lock(T_MUTEX *mutex) 00219 { 00220 #ifdef WITH_PTHREADS 00221 return pthread_mutex_lock(mutex); 00222 #elif defined(WITH_SDLTHREADS) 00223 if(*mutex != NULL) { 00224 return SDL_mutexP(*mutex); 00225 } 00226 return -1; 00227 #else 00228 return 0; 00229 #endif 00230 } 00231 00232 00233 int 00234 thread_mutex_trylock(T_MUTEX *mutex) 00235 { 00236 #ifdef WITH_PTHREADS 00237 return pthread_mutex_trylock(mutex); 00238 #elif defined(WITH_SDLTHREADS) 00239 if(*mutex != NULL) { 00240 return SDL_mutexP(*mutex); 00241 } 00242 return -1; 00243 #else 00244 return 0; 00245 #endif 00246 } 00247 00248 00249 int 00250 thread_mutex_unlock(T_MUTEX *mutex) 00251 { 00252 #ifdef WITH_PTHREADS 00253 return pthread_mutex_unlock(mutex); 00254 #elif defined(WITH_SDLTHREADS) 00255 if(*mutex != NULL) { 00256 return SDL_mutexV(*mutex); 00257 } 00258 return -1; 00259 #else 00260 return 0; 00261 #endif 00262 } 00263 00264 00265 int 00266 thread_mutex_destroy(T_MUTEX *mutex) 00267 { 00268 #ifdef WITH_PTHREADS 00269 return pthread_mutex_destroy(mutex); 00270 #elif defined(WITH_SDLTHREADS) 00271 if(*mutex != NULL) { 00272 SDL_DestroyMutex(*mutex); 00273 return 0; 00274 } 00275 return -1; 00276 #else 00277 return 0; 00278 #endif 00279 } 00280 00281 00287 00288 00289 int 00290 thread_rdwr_init_np(T_RDWR *rdwrp, T_RDWRATTR *attrp) 00291 { 00292 #ifdef WITH_PTHREADS 00293 rdwrp->readers_reading = 0; 00294 rdwrp->writer_writing = 0; 00295 T_MUTEX_INIT(&(rdwrp->mutex), NULL); 00296 T_COND_INIT(&(rdwrp->lock_free), NULL); 00297 return 0; 00298 #elif defined(WITH_SDLTHREADS) 00299 return 0; 00300 #else 00301 return 0; 00302 #endif 00303 } 00304 00305 00306 int 00307 thread_rdwr_rlock_np(T_RDWR *rdwrp) 00308 { 00309 #ifdef WITH_PTHREADS 00310 T_MUTEX_LOCK(&(rdwrp->mutex)); 00311 while(rdwrp->writer_writing) { 00312 T_COND_WAIT(&(rdwrp->lock_free), &(rdwrp->mutex)); 00313 } 00314 rdwrp->readers_reading++; 00315 T_MUTEX_UNLOCK(&(rdwrp->mutex)); 00316 return 0; 00317 #elif defined(WITH_SDLTHREADS) 00318 return 0; 00319 #else 00320 return 0; 00321 #endif 00322 } 00323 00324 00325 int 00326 thread_rdwr_runlock_np(T_RDWR *rdwrp) 00327 { 00328 #ifdef WITH_PTHREADS 00329 T_MUTEX_LOCK(&(rdwrp->mutex)); 00330 if (rdwrp->readers_reading == 0) { 00331 T_MUTEX_UNLOCK(&(rdwrp->mutex)); 00332 return -1; 00333 } else { 00334 rdwrp->readers_reading--; 00335 if (rdwrp->readers_reading == 0) { 00336 T_COND_SIGNAL(&(rdwrp->lock_free)); 00337 } 00338 T_MUTEX_UNLOCK(&(rdwrp->mutex)); 00339 return 0; 00340 } 00341 #elif defined(WITH_SDLTHREADS) 00342 return 0; 00343 #else 00344 return 0; 00345 #endif 00346 } 00347 00348 00349 int 00350 thread_rdwr_wlock_np(T_RDWR *rdwrp) 00351 { 00352 #ifdef WITH_PTHREADS 00353 T_MUTEX_LOCK(&(rdwrp->mutex)); 00354 while(rdwrp->writer_writing || rdwrp->readers_reading) { 00355 T_COND_WAIT(&(rdwrp->lock_free), &(rdwrp->mutex)); 00356 } 00357 rdwrp->writer_writing++; 00358 T_MUTEX_UNLOCK(&(rdwrp->mutex)); 00359 return 0; 00360 #elif defined(WITH_SDLTHREADS) 00361 return 0; 00362 #else 00363 return 0; 00364 #endif 00365 } 00366 00367 00368 int 00369 thread_rdwr_wunlock_np(T_RDWR *rdwrp) 00370 { 00371 #ifdef WITH_PTHREADS 00372 T_MUTEX_LOCK(&(rdwrp->mutex)); 00373 if (rdwrp->writer_writing == 0) { 00374 T_MUTEX_UNLOCK(&(rdwrp->mutex)); 00375 return -1; 00376 } 00377 else { 00378 rdwrp->writer_writing = 0; 00379 T_COND_BROADCAST(&(rdwrp->lock_free)); 00380 T_MUTEX_UNLOCK(&(rdwrp->mutex)); 00381 return 0; 00382 } 00383 #elif defined(WITH_SDLTHREADS) 00384 return 0; 00385 #else 00386 return 0; 00387 #endif 00388 }