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