diff options
| author | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:41 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:41 +0200 |
| commit | 9cd3ccffd5372dfde3af478e3f832f18db4be3f1 (patch) | |
| tree | 631c295a4a4a16b57502b847626763a279bf6df7 /yhttpd-0.7.1/src/thrd | |
| parent | 13aaf70af703748fe096e0664c305cd202637ad2 (diff) | |
tagging tags
Diffstat (limited to 'yhttpd-0.7.1/src/thrd')
| -rwxr-xr-x | yhttpd-0.7.1/src/thrd/pool.cpp | 206 | ||||
| -rwxr-xr-x | yhttpd-0.7.1/src/thrd/pool.h | 77 | ||||
| -rwxr-xr-x | yhttpd-0.7.1/src/thrd/thrd.cpp | 26 | ||||
| -rwxr-xr-x | yhttpd-0.7.1/src/thrd/thrd.h | 28 | ||||
| -rw-r--r-- | yhttpd-0.7.1/src/thrd/thro.cpp | 45 | ||||
| -rw-r--r-- | yhttpd-0.7.1/src/thrd/thro.h | 30 |
6 files changed, 412 insertions, 0 deletions
diff --git a/yhttpd-0.7.1/src/thrd/pool.cpp b/yhttpd-0.7.1/src/thrd/pool.cpp new file mode 100755 index 0000000..1db639d --- /dev/null +++ b/yhttpd-0.7.1/src/thrd/pool.cpp @@ -0,0 +1,206 @@ +#ifndef POOL_CPP +#define POOL_CPP + +#include "pool.h" +#include "thrd.h" + +using namespace std; + +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 ); +} + +void +pool::tpool_init( tpool_t *tpoolp, int num_worker_threads, int max_queue_size, int do_not_block_when_full ) +{ + int i, rtn; + tpool_t tpool; + + // allocate a pool data structure + if (( tpool = (tpool_t) malloc( sizeof( struct tpool ) ) ) == NULL ) + { + wrap::system_message( POOLERR ); + exit(-1); + } + + // 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; + + if ( ( tpool->threads = (pthread_t*) malloc( sizeof( pthread_t ) *num_worker_threads ) ) == NULL ) + { + wrap::system_message( POOLERR ); + exit(-1); + } + + tpool->cur_queue_size = 0; + tpool->queue_head = NULL; + tpool->queue_tail = NULL; + tpool->queue_closed = 0; + tpool->shutdown = 0; + + if ( ( rtn = pthread_mutex_init( &(tpool->queue_lock), NULL ) ) != 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), NULL ) ) != 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_full), NULL ) ) != 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_empty), NULL ) ) != 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]) , NULL, tpool_thread, (void*)tpool ); + + *tpoolp = tpool; +} + +void* +pool::tpool_thread( void* arg ) +{ + tpool_t tpool = (tpool_t) arg; + tpool_work_t *my_workp; + + while( true ) + { + 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 ); + } + + my_workp = tpool->queue_head; + tpool->cur_queue_size--; + + if ( tpool->cur_queue_size == 0) + tpool->queue_head = tpool->queue_tail = NULL; + + else + tpool->queue_head = my_workp->next; + + if ( ( ! tpool->do_not_block_when_full ) && + ( tpool->cur_queue_size == ( tpool->max_queue_size - 1 ) ) ) + pthread_cond_signal( &(tpool->queue_not_full) ); + + if ( tpool->cur_queue_size == 0 ) + pthread_cond_signal( &(tpool->queue_empty) ); + + 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(); + + // recasting the client thread object. + thrd *t = (thrd*) v_pointer; + + // start parsing the client request and sending response's back. + t-> run (); + + // close the client socket. + t->~thrd(); + + wrap::SOCK->decrease_num_threads(); + + free(v_pointer); +} + +int +pool::tpool_add_work( tpool_t tpool, void(*routine)(void*), void* arg ) /// +{ + 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; +} + +#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(); + } +} +#endif + +#endif diff --git a/yhttpd-0.7.1/src/thrd/pool.h b/yhttpd-0.7.1/src/thrd/pool.h new file mode 100755 index 0000000..a744133 --- /dev/null +++ b/yhttpd-0.7.1/src/thrd/pool.h @@ -0,0 +1,77 @@ +#include "../incl.h" + +#ifndef POOL_H +#define POOL_H + +using namespace std; + +class pool +{ +private: + typedef struct tpool_work + { + void (*routine)(void*); /// + void *arg; + struct tpool_work *next; + } + tpool_work_t; + + typedef struct tpool + { + // pool characteristics: + int num_threads; + int max_queue_size; + int do_not_block_when_full; + + // pool state + pthread_t *threads; + int cur_queue_size; + + tpool_work_t *queue_head; + tpool_work_t *queue_tail; + + pthread_mutex_t queue_lock; + pthread_cond_t queue_not_empty; + pthread_cond_t queue_not_full; + pthread_cond_t queue_empty; + + int queue_closed; + int shutdown; + } + *tpool_t; + + int i_thrd_pool_size; + int i_thrd_pool_queue; + + tpool_t thread_pool; + + void + tpool_init( tpool_t *tpoolp, int num_worker_threads, int max_queue_size, int do_not_block_when_full ); + + int + tpool_add_work( tpool_t tpool, void(*routine)(void*), void* arg ); + + // virtual void + // tpool_destroy( tpool_t tpoolp, int finish ); + + static void* + tpool_thread( void* arg); + + static void + run_func( void *v_pointer ); + + // public methods: +public: + pool( ); + + // inline (speed)! + void run( void *arg ) + { + tpool_add_work( thread_pool, run_func, arg ); + } +#ifdef NCURSES + void print_pool_size(); +#endif +}; + +#endif diff --git a/yhttpd-0.7.1/src/thrd/thrd.cpp b/yhttpd-0.7.1/src/thrd/thrd.cpp new file mode 100755 index 0000000..63135a8 --- /dev/null +++ b/yhttpd-0.7.1/src/thrd/thrd.cpp @@ -0,0 +1,26 @@ +#ifndef THRD_CPP +#define THRD_CPP + +#include "thrd.h" +#include <sys/socket.h> + +using namespace std; + +thrd::thrd( int i_sock ) +{ + this->i_sock = i_sock; +} + +thrd::~thrd() +{ + shutdown ( get_sock() , 2 ); + close ( get_sock() ); +} + +void +thrd::run() +{ + wrap::SOCK->read_write( this, i_sock ); +} + +#endif diff --git a/yhttpd-0.7.1/src/thrd/thrd.h b/yhttpd-0.7.1/src/thrd/thrd.h new file mode 100755 index 0000000..7cd63b7 --- /dev/null +++ b/yhttpd-0.7.1/src/thrd/thrd.h @@ -0,0 +1,28 @@ +// class thrd declaration. +#include "../incl.h" + +#ifndef THRD_H +#define THRD_H + +using namespace std; + +class thrd +{ +private: + int i_sock; + +public: + + // small inline methods: + int get_sock() + { + return i_sock; + } + + // public methods: + explicit thrd( int i_sock ); + ~thrd(); // destructor. + virtual void run(); +}; + +#endif diff --git a/yhttpd-0.7.1/src/thrd/thro.cpp b/yhttpd-0.7.1/src/thrd/thro.cpp new file mode 100644 index 0000000..f6acf53 --- /dev/null +++ b/yhttpd-0.7.1/src/thrd/thro.cpp @@ -0,0 +1,45 @@ +#ifndef THRO_CPP +#define THRO_CPP + +#include "thro.h" + +using namespace std; + +thro::thro() +{ +} + +thro::~thro() +{ +} + +void +thro::run() +{ + void* p_void; + run( p_void ); +} + +void +thro::run( void *p_void ) +{ + elem.p_thro = this; + elem.p_void = p_void; + pthread_create( &pthread, NULL, start_, &elem ); +} + +void* +thro::start_( void *p_void ) +{ + elements *e = (elements*) p_void; + e->p_thro->start( e->p_void ); +} + +void +thro::start( void *p_void ) +{ + wrap::system_message( THRDSTR ); +} + + +#endif diff --git a/yhttpd-0.7.1/src/thrd/thro.h b/yhttpd-0.7.1/src/thrd/thro.h new file mode 100644 index 0000000..ef38793 --- /dev/null +++ b/yhttpd-0.7.1/src/thrd/thro.h @@ -0,0 +1,30 @@ +// Threaded Object (thro) + +#include "../incl.h" + +#ifndef THRO_H +#define THRO_H + +using namespace std; + +class thro +{ +private: + pthread_t pthread; + + struct elements { + thro* p_thro; + void* p_void; + } elem; + + static void* start_( void* p_void ); + +public: + thro( ); + ~thro( ); + void run(); + void run( void* p_void ); + virtual void start( void* p_void ); +}; + +#endif |
