00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00035 #ifndef _PTHREAD_POOL_
00036 #define _PTHREAD_POOL_
00037
00038
00039 #include <pthread.h>
00040 #include <sched.h>
00041 #include <unistd.h>
00042
00043
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047
00048
00052 enum
00053 {
00057 PTHREAD_POOL_NONBLOCKING,
00058
00062 PTHREAD_POOL_BLOCKING
00063 };
00064
00068 #define PTHREAD_POOL_NONBLOCKING PTHREAD_POOL_NONBLOCKING
00069
00073 #define PTHREAD_POOL_BLOCKING PTHREAD_POOL_BLOCKING
00074
00075
00076 #ifdef CTF_DEBUG
00077 #if (defined(_POSIX_THREAD_ATTR_STACKSIZE) && \
00078 defined(_POSIX_THREAD_ATTR_STACKADDR))
00079
00082 enum
00083 {
00087 PTHREAD_POOL_CHECKSTACK_OFF,
00088
00092 PTHREAD_POOL_CHECKSTACK_ON
00093 };
00094
00098 #define PTHREAD_POOL_CHECKSTACK_OFF PTHREAD_POOL_CHECKSTACK_OFF
00099
00103 #define PTHREAD_POOL_CHECKSTACK_ON PTHREAD_POOL_CHECKSTACK_ON
00104 #endif
00105 #endif
00106
00107
00111 struct pool_data_t
00112 {
00116 void (*routine)(void *);
00117
00121 void * arg;
00122 };
00123
00124
00128 typedef struct
00129 {
00133 pthread_mutexattr_t mutex_attr;
00134
00138 pthread_condattr_t cond_attr;
00139
00143 pthread_attr_t thread_attr;
00144
00145 #ifdef CTF_DEBUG
00146 #if ((_POSIX_THREAD_ATTR_STACKSIZE == 1) && \
00147 (_POSIX_THREAD_ATTR_STACKADDR == 1))
00148
00151 short check_stack;
00152
00153 #endif
00154 #endif
00155
00159 int pshared;
00160
00164 int block;
00165
00166 } pthread_poolattr_t;
00167
00168
00172 typedef struct
00173 {
00177 pthread_mutex_t lock;
00178
00182 pthread_cond_t queue_not_empty;
00183
00187 pthread_cond_t queue_not_full;
00188
00192 pthread_cond_t work_finished;
00193
00197 pthread_t * threads;
00198
00202 int num_threads;
00203
00207 struct pool_data_t * queue;
00208
00212 int front;
00213
00217 int back;
00218
00222 short queue_open;
00223
00227 int max_queue_length;
00228
00232 int queue_length;
00233
00237 int num_active;
00238
00242 int destroyed;
00243
00247 int queue_full_wait;
00248
00252 int work_not_finished_wait;
00253
00257 int block;
00258
00259 #ifdef CTF_DEBUG
00260 #if ((_POSIX_THREAD_ATTR_STACKSIZE == 1) && \
00261 (_POSIX_THREAD_ATTR_STACKADDR == 1))
00262
00265 short check_stack;
00266
00270 size_t ** stack_address;
00271
00275 size_t stack_size;
00276
00280 size_t guard_size;
00281 #endif
00282 #endif
00283
00284 } pthread_pool_t;
00285
00286
00297 extern int pthread_pool_init(pthread_pool_t * pool,
00298 const pthread_poolattr_t * attr,
00299 const int num_threads, const int max_queue_length);
00300
00308 extern int pthread_pool_destroy(pthread_pool_t * pool);
00309
00319 extern int pthread_pool_addwork(pthread_pool_t * pool,
00320 void (*routine)(void *), void * arg);
00321
00330 extern int pthread_pool_getindex(pthread_pool_t * pool,
00331 int * pool_index);
00332
00340 extern int pthread_pool_finishwork(pthread_pool_t * pool);
00341
00342 #ifdef CTF_DEBUG
00343 #if ((_POSIX_THREAD_ATTR_STACKSIZE == 1) && \
00344 (_POSIX_THREAD_ATTR_STACKADDR == 1))
00345
00352 extern int pthread_pool_db_display_maxstack(pthread_pool_t * pool);
00353 #endif
00354 #endif
00355
00363 extern int pthread_poolattr_init(pthread_poolattr_t * attr);
00364
00372 extern int pthread_poolattr_destroy(pthread_poolattr_t * attr);
00373
00382 extern int pthread_poolattr_getblock(const pthread_poolattr_t * attr,
00383 int * block);
00384
00393 extern int pthread_poolattr_setblock(pthread_poolattr_t * attr,
00394 const int block);
00395
00396 #if ((_XOPEN_VERSION - 0) >= 500)
00397
00405 extern int pthread_poolattr_getguardsize(const pthread_poolattr_t * attr,
00406 size_t * guardsize);
00407
00416 extern int pthread_poolattr_setguardsize(pthread_poolattr_t * attr,
00417 const size_t guardsize);
00418 #endif
00419
00420 #if (_POSIX_THREAD_PRIORITY_SCHEDULING == 1)
00421
00429 extern int pthread_poolattr_getsinheritsched(const pthread_poolattr_t * attr,
00430 int * inherit);
00431
00440 extern int pthread_poolattr_setsinheritsched(pthread_poolattr_t * attr,
00441 const int inherit);
00442
00451 extern int pthread_poolattr_getschedparam(const pthread_poolattr_t * attr,
00452 struct sched_param * param);
00453
00462 extern int pthread_poolattr_setschedparam(pthread_poolattr_t * attr,
00463 const struct sched_param * param);
00464
00473 extern int pthread_poolattr_getschedpolicy(const pthread_poolattr_t * attr,
00474 int * policy);
00475
00484 extern int pthread_poolattr_setschedpolicy(pthread_poolattr_t * attr,
00485 const int policy);
00486
00495 extern int pthread_poolattr_getscope(const pthread_poolattr_t * attr,
00496 int * scope);
00497
00506 extern int pthread_poolattr_setscope(pthread_poolattr_t * attr,
00507 const int scope);
00508 #endif
00509
00510 #if (_POSIX_THREAD_PROCESS_SHARED == 1)
00511
00519 extern int pthread_poolattr_getpshared(const pthread_poolattr_t * attr,
00520 int * pshared);
00521
00530 extern int pthread_poolattr_setpshared(pthread_poolattr_t * attr,
00531 const int pshared);
00532 #endif
00533
00534 #if (_POSIX_THREAD_ATTR_STACKSIZE == 1)
00535
00543 extern int pthread_poolattr_getstacksize(const pthread_poolattr_t * attr,
00544 size_t * stacksize);
00545
00554 extern int pthread_poolattr_setstacksize(pthread_poolattr_t * attr,
00555 const size_t stacksize);
00556 #endif
00557
00558 #ifdef CTF_DEBUG
00559 #if ((_POSIX_THREAD_ATTR_STACKSIZE == 1) && \
00560 (_POSIX_THREAD_ATTR_STACKADDR == 1))
00561
00569 extern int pthread_poolattr_getcheckstack(const pthread_poolattr_t * attr,
00570 int * checkstack);
00571
00580 extern int pthread_poolattr_setcheckstack(pthread_poolattr_t * attr,
00581 const int checkstack);
00582 #endif
00583 #endif
00584
00585 #ifdef __cplusplus
00586 }
00587 #endif
00588
00589 #endif