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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #ifndef _MACROS_H_
00084 #define _MACROS_H_
00085
00086 #ifndef _WIN32
00087 #include <pthread.h>
00088 #include <semaphore.h>
00089 #include "config.h"
00090 #else
00091 #include <windows.h>
00092 #endif
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 #define GET_THREAD_MODE(a, b) ( (a & (1<<b)) / (1<<b) )
00112
00113
00114 #define SET_THREAD_ON(a, b) a = (a | (1<<b))
00115 #define SET_THREAD_OFF(a, b) a = (a & (~(1<<b)))
00116
00117
00118
00119 #define STRUCT( name, members ) \
00120 typedef struct name##_s { \
00121 members \
00122 } name##_t; \
00123 typedef name##_t* name##_p;
00124
00125
00126
00127
00128
00129 #ifndef _WIN32
00130
00131
00132
00133
00134 #define PTHREAD_STACK_SIZE 131072
00135 #define THREAD_T pthread_t
00136 #define THREAD_CREATE( thread_handle, function, arg ) \
00137 pthread_create( \
00138 thread_handle, \
00139 &attr, \
00140 function, \
00141 (void*) arg \
00142 )
00143
00144 #define THREAD_CANCEL( thread_handle ) \
00145 pthread_cancel( thread_handle )
00146
00147 #define THREAD_JOIN(thread_handle ) \
00148 pthread_join( thread_handle, NULL )
00149
00150 #define THREAD_EXIT() \
00151 pthread_exit(NULL)
00152
00153
00154
00155
00156
00157
00158 #define MUTEX_T pthread_mutex_t
00159
00160 #define MUTEX_INIT(mutex) \
00161 pthread_mutex_init(mutex, NULL)
00162
00163 #define MUTEX_DESTROY(mutex) \
00164 pthread_mutex_destroy(mutex)
00165
00166 #define MUTEX_LOCK(mutex) \
00167 if (pthread_mutex_lock( mutex )) \
00168 fprintf(stderr, "pthread lock error: %s:%d\n", __FILE__, __LINE__)
00169 #define MUTEX_UNLOCK(mutex) \
00170 pthread_mutex_unlock( mutex )
00171 #define MUTEX_NEW(mutex) \
00172 mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); \
00173 if (mutex == NULL) \
00174 fprintf(stderr, "Memory Error. %s:%d\n", __FILE__,__LINE__); \
00175
00176
00177
00178
00179
00180 #define COND_T pthread_cond_t
00181
00182 #define COND_INIT(cond) \
00183 pthread_cond_init(cond, NULL)
00184
00185 #define COND_DESTROY(cond) \
00186 pthread_cond_destroy(cond)
00187
00188 #define COND_WAIT( cond , mutex ) \
00189 pthread_cond_wait(cond, mutex )
00190
00191 #define COND_SLEEP( cond, mutex, test ) \
00192 if (pthread_mutex_lock( mutex )) \
00193 printf("pthread lock error: %s:%d\n", __FILE__, __LINE__); \
00194 if (!test) { \
00195 pthread_cond_wait( cond, mutex ); \
00196 }
00197 #define COND_RESET( cond, mutex ) \
00198 pthread_mutex_unlock( mutex );
00199 #define COND_SLEEP_ACTION(cond, mutex, action) \
00200 if (pthread_mutex_lock( mutex )) \
00201 printf("pthread lock error: %s:%d\n", __FILE__, __LINE__); \
00202 action; \
00203 pthread_cond_wait( cond, mutex );
00204 #define SIGNAL(cond, mutex, action) \
00205 pthread_mutex_lock( mutex ); \
00206 action; \
00207 pthread_cond_signal( cond ); \
00208 pthread_mutex_unlock( mutex )
00209 #define COND_BROADCAST(cond) \
00210 pthread_cond_broadcast( cond )
00211 #define COND_SIGNAL(cond) \
00212 pthread_cond_signal( cond )
00213
00214
00215
00216
00217
00218 #define SEMAPHORE_T sem_t
00219
00220 #define SEMAPHORE_INIT(sem) \
00221 sem_init(sem, 0, 0)
00222
00223 #define SEMAPHORE_DESTROY(sem) \
00224 sem_destroy(sem)
00225
00226 #define SEMAPHORE_WAIT(sem) \
00227 sem_wait(sem)
00228 #define SEMAPHORE_POST(sem) \
00229 sem_post(sem)
00230
00231
00232
00233
00234
00235 #ifdef HAVE_PTHREAD_RWLOCK_T
00236 #define RWLOCK_T pthread_rwlock_t
00237 #else
00238 #define RWLOCK_T mc_rwlock_t
00239 #endif
00240
00241 #ifdef HAVE_PTHREAD_RWLOCK_T
00242 #define RWLOCK_INIT(rwlock) \
00243 pthread_rwlock_init(rwlock, NULL)
00244 #else
00245 #define RWLOCK_INIT(rwlock) \
00246 mc_rwlock_init(rwlock)
00247 #endif
00248
00249 #ifdef HAVE_PTHREAD_RWLOCK_T
00250 #define RWLOCK_DESTROY(rwlock) \
00251 pthread_rwlock_destroy(rwlock)
00252 #else
00253 #define RWLOCK_DESTROY(rwlock) \
00254 mc_rwlock_destroy(rwlock)
00255 #endif
00256
00257 #ifdef HAVE_PTHREAD_RWLOCK_T
00258 #define RWLOCK_RDLOCK(rwlock) \
00259 if (pthread_rwlock_rdlock(rwlock)) \
00260 fprintf(stderr, "rwlock error: %s:%d\n", __FILE__, __LINE__)
00261 #define RWLOCK_RDUNLOCK(rwlock) \
00262 if (pthread_rwlock_unlock(rwlock)) \
00263 fprintf(stderr, "rwunlock error: %s:%d\n", __FILE__, __LINE__)
00264 #define RWLOCK_WRLOCK(rwlock) \
00265 if (pthread_rwlock_wrlock(rwlock)) \
00266 fprintf(stderr, "rwlock error: %s:%d\n", __FILE__, __LINE__)
00267 #define RWLOCK_WRUNLOCK(rwlock) \
00268 if (pthread_rwlock_unlock(rwlock)) \
00269 fprintf(stderr, "rwunlock error: %s:%d\n", __FILE__, __LINE__)
00270 #else
00271 #define RWLOCK_RDLOCK(rwlock) \
00272 mc_rwlock_rdlock(rwlock)
00273 #define RWLOCK_RDUNLOCK(rwlock) \
00274 mc_rwlock_rdunlock(rwlock)
00275 #define RWLOCK_WRLOCK(rwlock) \
00276 mc_rwlock_wrlock(rwlock)
00277 #define RWLOCK_WRUNLOCK(rwlock) \
00278 mc_rwlock_wrunlock(rwlock)
00279 #endif
00280
00281
00282
00283
00284
00285 #define WAKE_QUEUE(queue, action) \
00286 if (pthread_mutex_trylock( queue->lock ) == 0) { \
00287 action; \
00288 pthread_cond_signal( queue->cond); \
00289 pthread_mutex_unlock( queue->lock); \
00290 }
00291 #define SLEEP_QUEUE( queue ) \
00292 if (pthread_mutex_lock( queue->thread_mutex )) \
00293 printf("pthread lock error: %s:%d\n", __FILE__, __LINE__); \
00294 pthread_cond_wait( queue->touched_signal, queue->thread_mutex )
00295 #define SLEEP_RESET( queue ) \
00296 pthread_mutex_unlock( queue->thread_mutex )
00297
00298
00299
00300 #else
00301
00302
00303
00304
00305
00306
00307
00308 #define THREAD_T HANDLE
00309 #define THREAD_CREATE(thread_handle, function, arg) \
00310 *(thread_handle) = CreateThread( \
00311 NULL, \
00312 (SIZE_T)stack_size, \
00313 function, \
00314 arg, \
00315 0, \
00316 NULL \
00317 )
00318
00319 #define THREAD_CANCEL(thread_handle) \
00320 TerminateThread( thread_handle, 0)
00321
00322 #define THREAD_JOIN(thread_handle) \
00323 WaitForSingleObject(thread_handle, INFINITE)
00324
00325 #define THREAD_EXIT() \
00326 ExitThread(0)
00327
00328
00329
00330
00331
00332 #define MUTEX_T HANDLE
00333
00334 #define MUTEX_INIT(mutex) \
00335 *mutex = CreateMutex(NULL, FALSE, NULL)
00336
00337 #define MUTEX_DESTROY(mutex)
00338
00339 #define MUTEX_LOCK(mutex) \
00340 WaitForSingleObject( \
00341 *mutex , \
00342 INFINITE)
00343 #define MUTEX_UNLOCK(mutex) \
00344 ReleaseMutex( *mutex )
00345 #define MUTEX_NEW(mutex) \
00346 mutex = (HANDLE*)malloc(sizeof(HANDLE)); \
00347 if(mutex == NULL) \
00348 fprintf(stderr, "Memory Error. %s:%d\n", __FILE__, __LINE__)
00349
00350
00351
00352
00353
00354
00355 #define COND_T HANDLE
00356
00357 #define COND_INIT(cond) \
00358 *cond = CreateEvent(NULL, TRUE, TRUE, NULL);\
00359 ResetEvent(*cond)
00360
00361 #define COND_DESTROY(cond)
00362
00363
00364 #define COND_WAIT( cond , mutex ) \
00365 ResetEvent(*cond); \
00366 ReleaseMutex(*mutex); \
00367 WaitForSingleObject( *cond, INFINITE)
00368 #define COND_SLEEP( cond, mutex, test ) \
00369 ResetEvent( *cond ); \
00370 if (!test){ \
00371 WaitForSingleObject( *cond, INFINITE); \
00372 }
00373 #define COND_RESET( cond, mutex ) \
00374 ResetEvent(*cond)
00375 #define COND_SLEEP_ACTION(cond, mutex, action) \
00376 ResetEvent( *cond ); \
00377 action; \
00378 WaitForSingleObject( *cond, INFINITE)
00379 #define SIGNAL(cond, mutex, action) \
00380 action; \
00381 SetEvent( *cond )
00382 #define COND_BROADCAST(cond) \
00383 SetEvent(*cond)
00384 #define COND_SIGNAL(cond) \
00385 SetEvent(*cond)
00386
00387
00388
00389
00390
00391
00392 #define SEMAPHORE_T HANDLE
00393
00394 #define SEMAPHORE_INIT(sem) \
00395 *sem = CreateSemaphore( \
00396 NULL, \
00397 0, \
00398 1024, \
00399 NULL )
00400
00401 #define SEMAPHORE_DESTROY(sem)
00402
00403 #define SEMAPHORE_WAIT(sem) \
00404 WaitForSingleObject(sem, INFINITE)
00405 #define SEMAPHORE_POST(sem) \
00406 ReleaseSemaphore(sem, 1, NULL)
00407
00408
00409
00410
00411
00412
00413 #define RWLOCK_T mc_rwlock_t
00414
00415 #define RWLOCK_INIT(rwlock) \
00416 mc_rwlock_init(rwlock)
00417
00418 #define RWLOCK_DESTROY(rwlock) mc_rwlock_destroy(rwlock)
00419
00420 #define RWLOCK_RDLOCK(rwlock) \
00421 mc_rwlock_rdlock(rwlock)
00422 #define RWLOCK_RDUNLOCK(rwlock) \
00423 mc_rwlock_rdunlock(rwlock)
00424 #define RWLOCK_WRLOCK(rwlock) \
00425 mc_rwlock_wrlock(rwlock)
00426 #define RWLOCK_WRUNLOCK(rwlock) \
00427 mc_rwlock_wrunlock(rwlock)
00428
00429
00430
00431
00432
00433 #define SLEEP_QUEUE( queue ) \
00434 ResetEvent( *(queue->touched_signal) ); \
00435 WaitForSingleObject( *(queue->touched_signal), INFINITE )
00436 #define WAKE_QUEUE(queue, action) \
00437 action; \
00438 SetEvent( *(queue->cond))
00439 #define SLEEP_RESET( queue )
00440
00441 #endif
00442
00443
00444
00445
00446
00447
00448
00449 #define CHECK_NULL( var, action ) \
00450 if ( var == NULL ) { \
00451 fprintf(stderr, "Pointer var is null: expected otherwise.\n"); \
00452 fprintf(stderr, "Error occured at %s:%d", __FILE__, __LINE__); \
00453 action; \
00454 }
00455
00456 #define WARN( message ) \
00457 fprintf(stderr, "WARNING: "); \
00458 fprintf(stderr, message ); \
00459 fprintf(stderr, " %s:%d\n", __FILE__, __LINE__ )
00460
00461
00462
00463
00464
00465
00466 #define CH_DATATYPE_SIZE(type, size) \
00467 switch(type) { \
00468 case CH_CHARTYPE: \
00469 size = sizeof(char); \
00470 break; \
00471 case CH_INTTYPE: \
00472 size = sizeof(int); \
00473 break; \
00474 case CH_UINTTYPE: \
00475 size = sizeof(unsigned int); \
00476 break; \
00477 case CH_SHORTTYPE: \
00478 size = sizeof(short); \
00479 break; \
00480 case CH_USHORTTYPE: \
00481 size = sizeof(unsigned short); \
00482 break; \
00483 case CH_FLOATTYPE: \
00484 size = sizeof(float); \
00485 break; \
00486 case CH_DOUBLETYPE: \
00487 size = sizeof(double); \
00488 break; \
00489 default: \
00490 fprintf(stderr, "Unknown data type: %d at %s:%d", \
00491 type, __FILE__, __LINE__); \
00492 size=0; \
00493 }
00494
00495
00496
00497 #define CH_DATATYPE_STRING(type, string) \
00498 switch(type) { \
00499 case CH_CHARTYPE: \
00500 strcpy(string, "char"); \
00501 break; \
00502 case CH_INTTYPE: \
00503 strcpy(string, "int"); \
00504 break; \
00505 case CH_UINTTYPE: \
00506 strcpy(string, "unsigned int"); \
00507 break; \
00508 case CH_SHORTTYPE: \
00509 strcpy(string, "short"); \
00510 break; \
00511 case CH_USHORTTYPE: \
00512 strcpy(string, "unsigned short"); \
00513 break; \
00514 case CH_FLOATTYPE: \
00515 strcpy(string, "float"); \
00516 break; \
00517 case CH_DOUBLETYPE: \
00518 strcpy(string, "double"); \
00519 break; \
00520 default: \
00521 fprintf(stderr, \
00522 "Unsupported data type: %d %s:%d\n", \
00523 type, __FILE__, __LINE__ ); \
00524 }
00525
00526
00527
00528
00529 #define CH_DATATYPE_VALUE_STRING(type, string, p) \
00530 switch(type) { \
00531 case CH_CHARTYPE: \
00532 sprintf(string, "%c", *((char*)p)); \
00533 break; \
00534 case CH_INTTYPE: \
00535 sprintf(string, "%d", *((int*)p)); \
00536 break; \
00537 case CH_UINTTYPE: \
00538 sprintf(string, "%d", *((unsigned int*)p)); \
00539 break; \
00540 case CH_SHORTTYPE: \
00541 sprintf(string, "%d", *((short*)p)); \
00542 break; \
00543 case CH_USHORTTYPE: \
00544 sprintf(string, "%d", *((unsigned short*)p)); \
00545 break; \
00546 case CH_FLOATTYPE: \
00547 sprintf(string, "%f", *((float*)p)); \
00548 break; \
00549 case CH_DOUBLETYPE: \
00550 sprintf(string, "%f", *((double*)p)); \
00551 break; \
00552 default: \
00553 fprintf(stderr, \
00554 "Unsupported data type: %d %s:%d\n", \
00555 type, __FILE__, __LINE__); \
00556 }
00557
00558 #define CH_STRING_DATATYPE(string, type) \
00559 if (!strcmp(string, "int")) { \
00560 type = CH_INTTYPE; \
00561 } else if (!strcmp(string, "float")) { \
00562 type = CH_FLOATTYPE; \
00563 } else if (!strcmp(string, "double")) { \
00564 type = CH_DOUBLETYPE; \
00565 } else if (!strcmp(string, "unsigned int")) { \
00566 type = CH_UINTTYPE; \
00567 } else if (!strcmp(string, "short")) { \
00568 type = CH_SHORTTYPE; \
00569 } else if (!strcmp(string, "unsigned short")) { \
00570 type = CH_USHORTTYPE; \
00571 } else if (!strcmp(string, "char")) { \
00572 type = CH_CHARTYPE; \
00573 } else { \
00574 fprintf(stderr, \
00575 "Unsupported data type: %d %s:%d\n", \
00576 type, __FILE__, __LINE__ ); \
00577 }
00578
00579 #ifndef _WIN32
00580 #define CH_DATATYPE_STR_TO_VAL(type, string, val) \
00581 switch (type) { \
00582 case CH_INTTYPE: \
00583 *(int*)val = atoi(string); \
00584 break; \
00585 case CH_UINTTYPE: \
00586 *(unsigned int*)val = atoi(string); \
00587 break; \
00588 case CH_SHORTTYPE: \
00589 *(short*)val = (short)atoi(string); \
00590 break; \
00591 case CH_USHORTTYPE: \
00592 *(unsigned short*)val = (unsigned short)atoi(string); \
00593 break; \
00594 case CH_FLOATTYPE: \
00595 *(float*)val = strtof(string, NULL); \
00596 break; \
00597 case CH_DOUBLETYPE: \
00598 *(double*)val = strtod(string, NULL); \
00599 break; \
00600 default: \
00601 fprintf(stderr, \
00602 "Unsupported data type: %d %s:%d\n", \
00603 type, __FILE__, __LINE__ ); \
00604 }
00605 #else
00606 #define CH_DATATYPE_STR_TO_VAL(type, string, val) \
00607 switch (type) { \
00608 case CH_INTTYPE: \
00609 *(int*)val = atoi(string); \
00610 break; \
00611 case CH_UINTTYPE: \
00612 *(unsigned int*)val = atoi(string); \
00613 break; \
00614 case CH_SHORTTYPE: \
00615 *(short*)val = (short)atoi(string); \
00616 break; \
00617 case CH_USHORTTYPE: \
00618 *(unsigned short*)val = (unsigned short)atoi(string); \
00619 break; \
00620 case CH_FLOATTYPE: \
00621 *(float*)val = (double)strtod(string, NULL); \
00622 break; \
00623 case CH_DOUBLETYPE: \
00624 *(double*)val = strtod(string, NULL); \
00625 break; \
00626 default: \
00627 fprintf(stderr, \
00628 "Unsupported data type: %d %s:%d\n", \
00629 type, __FILE__, __LINE__ ); \
00630 }
00631 #endif
00632
00633
00634 #endif