diff options
| author | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:43 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:43 +0200 |
| commit | d3982ec72b255876db00825605d1d5aae0bc313e (patch) | |
| tree | a653552b7d229f7f27262980da7550d39961a102 /src/thrd/pool.cpp | |
| parent | 796609174e5ecb35fab992969e7690186840048a (diff) | |
tagging ychat-0.7.7.1ychat-0.7.7.1
Diffstat (limited to 'src/thrd/pool.cpp')
| -rwxr-xr-x | src/thrd/pool.cpp | 315 |
1 files changed, 165 insertions, 150 deletions
diff --git a/src/thrd/pool.cpp b/src/thrd/pool.cpp index 1db639d..d25a873 100755 --- a/src/thrd/pool.cpp +++ b/src/thrd/pool.cpp @@ -2,204 +2,219 @@ #define POOL_CPP #include "pool.h" -#include "thrd.h" using namespace std; +int pool::i_thrd_used = 0; + pool::pool() { - i_thrd_pool_size = tool::string2int( wrap::CONF->get_elem( "httpd.thread.poolsize" ) ); - i_thrd_pool_queue = tool::string2int( wrap::CONF->get_elem( "httpd.thread.queuesize" ) ); - tpool_init( &thread_pool, i_thrd_pool_size, i_thrd_pool_queue, 0 ); + i_thrd_pool_size = tool::string2int( wrap::CONF->get_elem( "httpd.thread.initpoolsize" ) ); + i_thrd_pool_queue = tool::string2int( wrap::CONF->get_elem( "httpd.thread.queuesize" ) ); + tpool_init( &thread_pool, i_thrd_pool_size, i_thrd_pool_queue); } void -pool::tpool_init( tpool_t *tpoolp, int num_worker_threads, int max_queue_size, int do_not_block_when_full ) +pool::tpool_init( tpool_t *tpoolp, int num_worker_threads, int max_queue_size) { - int i, rtn; - tpool_t tpool; + int i, rtn; + tpool_t tpool; + + // allocate a pool data structure + if (( tpool = (tpool_t) malloc( sizeof( struct tpool ) ) ) == 0 ) + { + wrap::system_message( POOLERR ); + exit(-1); + } + + // initialize th fields + tpool->num_threads = num_worker_threads; + tpool->max_queue_size = max_queue_size; + + if ( ( tpool->threads = (pthread_t*) malloc( sizeof(pthread_t)*num_worker_threads ) ) == 0 ) + { + wrap::system_message( POOLERR ); + exit(-1); + } + + tpool->cur_queue_size = 0; + tpool->queue_head = 0; + tpool->queue_tail = 0; + + if ( ( rtn = pthread_mutex_init( &(tpool->queue_lock), 0 ) ) != 0 ) + { + string s_err( "pthread_mutex_init " ); + s_err.append( strerror( rtn ) ); + + wrap::system_message( s_err ); + + exit(-1); + } + else if ( ( rtn = pthread_cond_init( &(tpool->queue_not_empty), 0 ) ) != 0 ) + { + string s_err( "pthread_cond_init (1): " ); + s_err.append( strerror( rtn ) ); + + wrap::system_message( s_err ); + + exit(1); + } + else if ( ( rtn = pthread_cond_init( &(tpool->queue_not_full), 0 ) ) != 0 ) + { + string s_err( "pthread_cond_init (2): " ); + s_err.append( strerror( rtn ) ); + + wrap::system_message( s_err ); + + exit(1); + } + else if ( ( rtn = pthread_cond_init( &(tpool->queue_empty), 0 ) ) != 0 ) + { + string s_err( "pthread_mutex_init " ); + s_err.append( strerror( rtn ) ); + + wrap::system_message( s_err ); + + exit(1); + } + + // create threads + for ( i = 0; i < num_worker_threads; ++i ) + pthread_create( &(tpool->threads[i]) , 0, tpool_thread, (void*)tpool ); + + *tpoolp = tpool; +} - // allocate a pool data structure - if (( tpool = (tpool_t) malloc( sizeof( struct tpool ) ) ) == NULL ) - { - wrap::system_message( POOLERR ); - exit(-1); - } +void* +pool::tpool_thread( void* p_void ) +{ + tpool_t tpool = (tpool_t) p_void; + tpool_work_t *my_workp; - // initialize th fields - tpool->num_threads = num_worker_threads; - tpool->max_queue_size = max_queue_size; - tpool->do_not_block_when_full = do_not_block_when_full; + for( pthread_mutex_lock( &(tpool->queue_lock) );; + pthread_mutex_lock( &(tpool->queue_lock) ), --i_thrd_used ) + { - if ( ( tpool->threads = (pthread_t*) malloc( sizeof( pthread_t ) *num_worker_threads ) ) == NULL ) - { - wrap::system_message( POOLERR ); - exit(-1); - } +#ifdef NCURSES + print_threads(i_thrd_used); +#endif - tpool->cur_queue_size = 0; - tpool->queue_head = NULL; - tpool->queue_tail = NULL; - tpool->queue_closed = 0; - tpool->shutdown = 0; + while (tpool->cur_queue_size == 0) + pthread_cond_wait( &(tpool->queue_not_empty), &(tpool->queue_lock) ); - if ( ( rtn = pthread_mutex_init( &(tpool->queue_lock), NULL ) ) != 0 ) - { - string s_err( "pthread_mutex_init " ); - s_err.append( strerror( rtn ) ); + my_workp = tpool->queue_head; + tpool->cur_queue_size--; - wrap::system_message( s_err ); + if ( tpool->cur_queue_size == 0) + tpool->queue_head = tpool->queue_tail = 0; - exit(-1); - } - else if ( ( rtn = pthread_cond_init( &(tpool->queue_not_empty), NULL ) ) != 0 ) - { - string s_err( "pthread_mutex_init " ); - s_err.append( strerror( rtn ) ); - - wrap::system_message( s_err ); + else + tpool->queue_head = my_workp->next; - exit(-1); - } - else if ( ( rtn = pthread_cond_init( &(tpool->queue_not_full), NULL ) ) != 0 ) - { - string s_err( "pthread_mutex_init " ); - s_err.append( strerror( rtn ) ); + if ( tpool->cur_queue_size == ( tpool->max_queue_size - 1 ) ) + pthread_cond_signal( &(tpool->queue_not_full) ); - wrap::system_message( s_err ); + if ( tpool->cur_queue_size == 0 ) + pthread_cond_signal( &(tpool->queue_empty) ); - exit(-1); - } - else if ( ( rtn = pthread_cond_init( &(tpool->queue_empty), NULL ) ) != 0 ) - { - string s_err( "pthread_mutex_init " ); - s_err.append( strerror( rtn ) ); + pthread_mutex_unlock( &(tpool->queue_lock) ); - wrap::system_message( s_err ); + (*(my_workp->routine))(my_workp->p_void); - exit(-1); - } - // create threads - for ( i = 0; i < num_worker_threads; i++ ) - pthread_create( &(tpool->threads[i]) , NULL, tpool_thread, (void*)tpool ); + free(my_workp); + } +} - *tpoolp = tpool; +void pool::run_func( void *p_void ) +{ + int* p_sock = (int*)p_void; + wrap::SOCK->read_write( p_sock ); + delete p_sock; } -void* -pool::tpool_thread( void* arg ) +int +pool::tpool_add_work( tpool_t tpool, void(*routine)(void*), void* p_void ) /// { - tpool_t tpool = (tpool_t) arg; - tpool_work_t *my_workp; + tpool_work_t *workp; + pthread_mutex_lock( &(tpool->queue_lock) ); - while( true ) + if ( ++i_thrd_used == tpool->num_threads ) + { + int i_max_pool_size = tool::string2int( wrap::CONF->get_elem( "httpd.thread.maxpoolsize" ) ); + if ( i_max_pool_size != 0 && i_thrd_used > i_max_pool_size ) { - pthread_mutex_lock( &(tpool->queue_lock) ); - - while ( (tpool->cur_queue_size == 0) && (!tpool->shutdown) ) - pthread_cond_wait( &(tpool->queue_not_empty), &(tpool->queue_lock) ); - - if (tpool->shutdown) - { - pthread_mutex_unlock( &(tpool->queue_lock) ); - pthread_exit( NULL ); - } + wrap::system_message(POOLER2+tool::int2string(i_thrd_used)+")"); + } + else + { + int i_size = tpool->num_threads + 1; - my_workp = tpool->queue_head; - tpool->cur_queue_size--; + wrap::system_message(POOLFLL+tool::int2string(i_size)+")"); - if ( tpool->cur_queue_size == 0) - tpool->queue_head = tpool->queue_tail = NULL; + tpool->threads = (pthread_t*)realloc((void*)tpool->threads, sizeof(pthread_t)*tpool->num_threads); - else - tpool->queue_head = my_workp->next; + for ( int i = tpool->num_threads; i < i_size; ++i ) + pthread_create( &(tpool->threads[i]) , 0, tpool_thread, (void*)tpool ); - if ( ( ! tpool->do_not_block_when_full ) && - ( tpool->cur_queue_size == ( tpool->max_queue_size - 1 ) ) ) - pthread_cond_signal( &(tpool->queue_not_full) ); + i_thrd_pool_size = tpool->num_threads = i_size; +#ifdef NCURSES - if ( tpool->cur_queue_size == 0 ) - pthread_cond_signal( &(tpool->queue_empty) ); + print_pool_size(); +#endif - pthread_mutex_unlock( &(tpool->queue_lock) ); - (*(my_workp->routine))(my_workp->arg); - free((void*)my_workp); } -} + } -void pool::run_func( void *v_pointer ) -{ - wrap::SOCK->increase_num_threads(); +#ifdef NCURSES + print_threads(i_thrd_used); +#endif - // recasting the client thread object. - thrd *t = (thrd*) v_pointer; + while (tpool->cur_queue_size == tpool->max_queue_size) + pthread_cond_wait( &(tpool->queue_not_full), &(tpool->queue_lock) ); - // start parsing the client request and sending response's back. - t-> run (); + // allocate work structure: + workp = (tpool_work_t*) malloc( sizeof( tpool_work_t ) ); - // close the client socket. - t->~thrd(); + workp->routine = routine; + workp->p_void = p_void; + workp->next = 0; - wrap::SOCK->decrease_num_threads(); + if (tpool->cur_queue_size == 0 ) + { + tpool->queue_tail = tpool->queue_head = workp; + } + else + { + (tpool->queue_tail)->next = workp; + tpool->queue_tail = workp; + } - free(v_pointer); + tpool->cur_queue_size++; + pthread_cond_signal( &tpool->queue_not_empty ); + pthread_mutex_unlock( &(tpool->queue_lock) ); + + return 0; } -int -pool::tpool_add_work( tpool_t tpool, void(*routine)(void*), void* arg ) /// +#ifdef NCURSES +void +pool::print_threads(int i_thrd_used) { - tpool_work_t *workp; - pthread_mutex_lock( &(tpool->queue_lock) ); - - if( ( tpool->cur_queue_size == tpool->max_queue_size ) && - tpool->do_not_block_when_full ) - { - pthread_mutex_unlock( &(tpool->queue_lock) ); - return -1; - } - - while( ( tpool->cur_queue_size == tpool->max_queue_size ) && - ( ! ( tpool->shutdown || tpool->queue_closed ) ) ) - pthread_cond_wait( &(tpool->queue_not_full), &(tpool->queue_lock) ); - - if( tpool->shutdown || tpool->queue_closed ) - { - pthread_mutex_unlock( &tpool->queue_lock ); - return -1; - } - - // allocate work structure: - workp = (tpool_work_t*) malloc( sizeof( tpool_work_t ) ); - - workp->routine = routine; - workp->arg = arg; - workp->next = NULL; - - if( tpool->cur_queue_size == 0 ) - { - tpool->queue_tail = tpool->queue_head = workp; - } - else - { - (tpool->queue_tail)->next = workp; - tpool->queue_tail = workp; - } - - pthread_cond_signal( &tpool->queue_not_empty ); - tpool->cur_queue_size++; - pthread_mutex_unlock( &(tpool->queue_lock) ); - return 1; + if ( wrap::NCUR->is_ready() ) + { + mvprintw( NCUR_POOL_RUNNING_X,NCUR_POOL_RUNNING_Y, "In use: %d ", i_thrd_used); + refresh(); + } } -#ifdef NCURSES void pool::print_pool_size() { - if ( wrap::NCUR->is_ready() ) - { - mvprintw( NCUR_POOL_SIZE_X,NCUR_POOL_SIZE_Y, "Size: %d %d", i_thrd_pool_size, i_thrd_pool_queue ); - refresh(); - } + if ( wrap::NCUR->is_ready() ) + { + mvprintw( NCUR_POOL_SIZE_X,NCUR_POOL_SIZE_Y, "Size: %d %d", i_thrd_pool_size, i_thrd_pool_queue ); + refresh(); + } } #endif |
