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