diff options
| author | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:44 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:44 +0200 |
| commit | c8b2ef7b899766d04562f7e04a84251cea8fa701 (patch) | |
| tree | 52816b17c17e2db0cf89e68537ad1a52392f1510 /src/thrd/pool.cpp | |
| parent | ca28c0e618890330d429c0dc12429255b20f0c90 (diff) | |
tagging ychat-0.8.0ychat-0.8.0
Diffstat (limited to 'src/thrd/pool.cpp')
| -rwxr-xr-x | src/thrd/pool.cpp | 271 |
1 files changed, 102 insertions, 169 deletions
diff --git a/src/thrd/pool.cpp b/src/thrd/pool.cpp index ef7b0a3..dd29d6a 100755 --- a/src/thrd/pool.cpp +++ b/src/thrd/pool.cpp @@ -5,220 +5,153 @@ using namespace std; -int pool::i_thrd_used = 0; - pool::pool() { - 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); + pthread_mutex_init(&mut_threads, 0); + pthread_mutex_init(&mut_queue_tasks, 0); + pthread_mutex_init(&mut_num_avail_threads, 0); + pthread_cond_init(&cond_new_task, 0); + + i_num_total_threads = 0; + i_num_avail_threads = tool::string2int( wrap::CONF->get_elem( "httpd.thread.initpoolsize" ) ); + increase_pool(i_num_avail_threads); } -void -pool::tpool_init( tpool_t *tpoolp, int num_worker_threads, int max_queue_size) +pool::~pool() { - 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 ); + pthread_mutex_lock(&mut_queue_tasks); + while (!queue_tasks.empty()) + { + delete queue_tasks.front(); + queue_tasks.pop(); + } + pthread_mutex_unlock(&mut_queue_tasks); + + pthread_mutex_destroy(&mut_threads); + pthread_mutex_destroy(&mut_queue_tasks); + pthread_mutex_destroy(&mut_num_avail_threads); + pthread_cond_destroy(&cond_new_task); +} - exit(1); - } +int +pool::increase_pool(int i_num) +{ + wrap::system_message(POOLFLL + tool::int2string(i_num) +","+tool::int2string(i_num_total_threads)+")"); + int i_max_pool_size = tool::string2int( wrap::CONF->get_elem( "httpd.thread.maxpoolsize" ) ); - else if ( ( rtn = pthread_cond_init( &(tpool->queue_not_full), 0 ) ) != 0 ) + for ( int i = 0; i < i_num; ++i ) + { + if ( i_max_pool_size != 0 && i_num_total_threads >= i_max_pool_size ) { - string s_err( "pthread_cond_init (2): " ); - s_err.append( strerror( rtn ) ); - - wrap::system_message( s_err ); - - exit(1); + wrap::system_message(POOLER2+tool::int2string(i_max_pool_size)+")"); + wrap::system_message(POOLER1+tool::int2string(i)+")"); + return i; } - else if ( ( rtn = pthread_cond_init( &(tpool->queue_empty), 0 ) ) != 0 ) - { - string s_err( "pthread_mutex_init " ); - s_err.append( strerror( rtn ) ); + ++i_num_total_threads; + pthread_t p_pthread; + pthread_create(&p_pthread, 0, wait_for_task, (void*) this ); + } - wrap::system_message( s_err ); + return i_num; +} - exit(1); - } +void +pool::add_task( void(*p_func)(void*), void* p_void ) +{ + pthread_mutex_lock(&mut_queue_tasks); + queue_tasks.push(new task(p_func, p_void)); + pthread_mutex_unlock(&mut_queue_tasks); - // create threads - for ( i = 0; i < num_worker_threads; ++i ) - pthread_create( &(tpool->threads[i]) , 0, tpool_thread, (void*)tpool ); + pthread_cond_signal(&cond_new_task); - *tpoolp = tpool; } void* -pool::tpool_thread( void* p_void ) +pool::wait_for_task( void* p_void ) { - tpool_t tpool = (tpool_t) p_void; - tpool_work_t *my_workp; + pool* p_pool = static_cast<pool*>(p_void); - for( pthread_mutex_lock( &(tpool->queue_lock) );; - pthread_mutex_lock( &(tpool->queue_lock) ), --i_thrd_used ) - { + for (;;) + { #ifdef NCURSES - print_threads(i_thrd_used); + p_pool->print_pool_size(); #endif - while (tpool->cur_queue_size == 0) - pthread_cond_wait( &(tpool->queue_not_empty), &(tpool->queue_lock) ); + pthread_mutex_lock(&p_pool->mut_threads); + pthread_cond_wait(&p_pool->cond_new_task, &p_pool->mut_threads); - my_workp = tpool->queue_head; - tpool->cur_queue_size--; - - if ( tpool->cur_queue_size == 0) - tpool->queue_head = tpool->queue_tail = 0; - - else - tpool->queue_head = my_workp->next; + pthread_mutex_lock(&p_pool->mut_num_avail_threads); + if ( --p_pool->i_num_avail_threads < 5 ) + { + int i_size = 9 - p_pool->i_num_avail_threads; + i_size = p_pool->increase_pool(i_size); + p_pool->i_num_avail_threads += i_size; + } + pthread_mutex_unlock(&p_pool->mut_num_avail_threads); - if ( tpool->cur_queue_size == ( tpool->max_queue_size - 1 ) ) - pthread_cond_signal( &(tpool->queue_not_full) ); + pthread_mutex_lock(&p_pool->mut_queue_tasks); + task* p_task = p_pool->queue_tasks.front(); + p_pool->queue_tasks.pop(); + pthread_mutex_unlock(&p_pool->mut_queue_tasks); - if ( tpool->cur_queue_size == 0 ) - pthread_cond_signal( &(tpool->queue_empty) ); + pthread_mutex_unlock(&p_pool->mut_threads); - pthread_mutex_unlock( &(tpool->queue_lock) ); + (*(p_task->p_func))(p_task->p_void); + delete p_task; - (*(my_workp->routine))(my_workp->p_void); + pthread_mutex_lock(&p_pool->mut_num_avail_threads); + p_pool->i_num_avail_threads++; + pthread_mutex_unlock(&p_pool->mut_num_avail_threads); + } - free(my_workp); - } + return 0; } -void pool::run_func( void *p_void ) +void +pool::run(void* p_void) { - int* p_sock = (int*)p_void; - wrap::SOCK->read_write( p_sock ); - delete p_sock; + add_task(run_func, p_void); } -int -pool::tpool_add_work( tpool_t tpool, void(*routine)(void*), void* p_void ) /// +void +pool::run_func(void *p_void) { - tpool_work_t *workp; - pthread_mutex_lock( &(tpool->queue_lock) ); - - 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 ) - { - wrap::system_message(POOLER2+tool::int2string(i_thrd_used)+")"); - } - - else - { - int i_size = tpool->num_threads + 1; - - wrap::system_message(POOLFLL+tool::int2string(i_size)+")"); - - tpool->threads = (pthread_t*)realloc((void*)tpool->threads, sizeof(pthread_t)*tpool->num_threads); - - for ( int i = tpool->num_threads; i < i_size; ++i ) - pthread_create( &(tpool->threads[i]) , 0, tpool_thread, (void*)tpool ); - - i_thrd_pool_size = tpool->num_threads = i_size; -#ifdef NCURSES - print_pool_size(); -#endif - } - } - -#ifdef NCURSES - print_threads(i_thrd_used); -#endif - - while (tpool->cur_queue_size == tpool->max_queue_size) - pthread_cond_wait( &(tpool->queue_not_full), &(tpool->queue_lock) ); - - // allocate work structure: - workp = (tpool_work_t*) malloc( sizeof( tpool_work_t ) ); - - workp->routine = routine; - workp->p_void = p_void; - workp->next = 0; - - if (tpool->cur_queue_size == 0 ) - { - tpool->queue_tail = tpool->queue_head = workp; - } + socketcontainer* p_sock = static_cast<socketcontainer*>(p_void); + wrap::SOCK->read_write(p_sock); +} - else +bool +pool::allow_user_login() +{ + pthread_mutex_lock(&mut_num_avail_threads); + if ( i_num_avail_threads < 2 ) + { + int i_max_pool_size = tool::string2int( wrap::CONF->get_elem( "httpd.thread.maxpoolsize" ) ); + if ( i_max_pool_size != 0 && i_max_pool_size == i_num_total_threads ) { - (tpool->queue_tail)->next = workp; - tpool->queue_tail = workp; + pthread_mutex_unlock(&mut_num_avail_threads); + return false; } + } + pthread_mutex_unlock(&mut_num_avail_threads); - tpool->cur_queue_size++; - pthread_cond_signal( &tpool->queue_not_empty ); - pthread_mutex_unlock( &(tpool->queue_lock) ); - - return 0; + return true; } #ifdef NCURSES void -pool::print_threads(int i_thrd_used) -{ - if ( wrap::NCUR->is_ready() ) - { - mvprintw( NCUR_POOL_RUNNING_X,NCUR_POOL_RUNNING_Y, "In use: %d ", i_thrd_used); - refresh(); - } -} - -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() ) + { + pthread_mutex_lock(&mut_num_avail_threads); + mvprintw( NCUR_POOL_WAIT_X,NCUR_POOL_WAIT_Y, "Wait/Tot: %d/%d ", i_num_avail_threads, i_num_total_threads); + mvprintw( NCUR_POOL_RUNNING_X,NCUR_POOL_RUNNING_Y, "Running: %d ", i_num_total_threads-i_num_avail_threads); + pthread_mutex_unlock(&mut_num_avail_threads); + refresh(); + } } #endif - #endif |
