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