summaryrefslogtreecommitdiff
path: root/src/thrd
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2013-04-06 13:14:48 +0200
committerPaul Buetow <paul@buetow.org>2013-04-06 13:14:48 +0200
commitbf5fc4cc4a15e8e57db58c2e065e0f5adbd8e800 (patch)
tree855a98aec6d09361074c9725e08063553044ecfb /src/thrd
parentd9c4a95345bd758e45196b29368bd2ff0f2790e3 (diff)
tagging ychat-0.7.9.3ychat-0.7.9.3
Diffstat (limited to 'src/thrd')
-rwxr-xr-xsrc/thrd/pool.cpp257
-rwxr-xr-xsrc/thrd/pool.h82
-rw-r--r--src/thrd/thro.cpp24
-rw-r--r--src/thrd/thro.h26
4 files changed, 158 insertions, 231 deletions
diff --git a/src/thrd/pool.cpp b/src/thrd/pool.cpp
index 1db639d..dd29d6a 100755
--- a/src/thrd/pool.cpp
+++ b/src/thrd/pool.cpp
@@ -2,205 +2,156 @@
#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 );
+ 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, int do_not_block_when_full )
+pool::~pool()
{
- 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);
- }
+ 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);
+}
- tpool->cur_queue_size = 0;
- tpool->queue_head = NULL;
- tpool->queue_tail = NULL;
- tpool->queue_closed = 0;
- tpool->shutdown = 0;
+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" ) );
- if ( ( rtn = pthread_mutex_init( &(tpool->queue_lock), NULL ) ) != 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_mutex_init " );
- 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_not_empty), NULL ) ) != 0 )
- {
- string s_err( "pthread_mutex_init " );
- s_err.append( strerror( rtn ) );
- wrap::system_message( s_err );
+ ++i_num_total_threads;
+ pthread_t p_pthread;
+ pthread_create(&p_pthread, 0, wait_for_task, (void*) this );
+ }
- 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 ) );
+ return i_num;
+}
- wrap::system_message( s_err );
+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);
- exit(-1);
- }
- // create threads
- for ( i = 0; i < num_worker_threads; i++ )
- pthread_create( &(tpool->threads[i]) , NULL, tpool_thread, (void*)tpool );
+ pthread_cond_signal(&cond_new_task);
- *tpoolp = tpool;
}
void*
-pool::tpool_thread( void* arg )
+pool::wait_for_task( void* p_void )
{
- tpool_t tpool = (tpool_t) arg;
- tpool_work_t *my_workp;
+ pool* p_pool = static_cast<pool*>(p_void);
- while( true )
- {
- pthread_mutex_lock( &(tpool->queue_lock) );
+ for (;;)
+ {
- while ( (tpool->cur_queue_size == 0) && (!tpool->shutdown) )
- pthread_cond_wait( &(tpool->queue_not_empty), &(tpool->queue_lock) );
+#ifdef NCURSES
+ p_pool->print_pool_size();
+#endif
- if (tpool->shutdown)
- {
- pthread_mutex_unlock( &(tpool->queue_lock) );
- pthread_exit( NULL );
- }
+ 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--;
+ 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 == 0)
- tpool->queue_head = tpool->queue_tail = NULL;
+ 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);
- else
- tpool->queue_head = my_workp->next;
+ pthread_mutex_unlock(&p_pool->mut_threads);
- if ( ( ! tpool->do_not_block_when_full ) &&
- ( tpool->cur_queue_size == ( tpool->max_queue_size - 1 ) ) )
- pthread_cond_signal( &(tpool->queue_not_full) );
+ (*(p_task->p_func))(p_task->p_void);
+ delete p_task;
- if ( tpool->cur_queue_size == 0 )
- pthread_cond_signal( &(tpool->queue_empty) );
+ pthread_mutex_lock(&p_pool->mut_num_avail_threads);
+ p_pool->i_num_avail_threads++;
+ pthread_mutex_unlock(&p_pool->mut_num_avail_threads);
+ }
- pthread_mutex_unlock( &(tpool->queue_lock) );
- (*(my_workp->routine))(my_workp->arg);
- free((void*)my_workp);
- }
+ return 0;
}
-void pool::run_func( void *v_pointer )
+void
+pool::run(void* p_void)
{
- 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);
+ add_task(run_func, p_void);
}
-int
-pool::tpool_add_work( tpool_t tpool, void(*routine)(void*), void* arg ) ///
+void
+pool::run_func(void *p_void)
{
- 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;
+ socketcontainer* p_sock = static_cast<socketcontainer*>(p_void);
+ wrap::SOCK->read_write(p_sock);
+}
- if( tpool->cur_queue_size == 0 )
- {
- tpool->queue_tail = tpool->queue_head = workp;
- }
- 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);
- pthread_cond_signal( &tpool->queue_not_empty );
- tpool->cur_queue_size++;
- pthread_mutex_unlock( &(tpool->queue_lock) );
- return 1;
+ return true;
}
#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() )
+ {
+ 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
diff --git a/src/thrd/pool.h b/src/thrd/pool.h
index a744133..3a5f7b6 100755
--- a/src/thrd/pool.h
+++ b/src/thrd/pool.h
@@ -3,74 +3,52 @@
#ifndef POOL_H
#define POOL_H
+#include <queue>
+
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;
+ friend class thro;
- tpool_work_t *queue_head;
- tpool_work_t *queue_tail;
+ struct task
+ {
+ void(*p_func)(void*);
+ void *p_void;
- 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;
+ task(void(*p_func)(void*), void *p_void)
+ {
+ this->p_func = p_func;
+ this->p_void = p_void;
}
- *tpool_t;
+ };
- int i_thrd_pool_size;
- int i_thrd_pool_queue;
+ pthread_mutex_t mut_threads;
+ pthread_mutex_t mut_queue_tasks;
+ pthread_mutex_t mut_num_avail_threads;
+ pthread_cond_t cond_new_task;
- tpool_t thread_pool;
+ int i_num_avail_threads;
+ int i_num_total_threads;
- void
- tpool_init( tpool_t *tpoolp, int num_worker_threads, int max_queue_size, int do_not_block_when_full );
+ queue<task*> queue_tasks;
- int
- tpool_add_work( tpool_t tpool, void(*routine)(void*), void* arg );
+ int increase_pool(int i_num);
+ void add_task( void(*p_func)(void*), void* p_void );
+ static void* wait_for_task(void *p_void);
+ static void run_func(void *p_void);
- // 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( );
+ pool();
+ ~pool();
+
+ void run(void* p_void);
+ bool allow_user_login();
- // inline (speed)!
- void run( void *arg )
- {
- tpool_add_work( thread_pool, run_func, arg );
- }
#ifdef NCURSES
- void print_pool_size();
+
+ void print_pool_size();
#endif
};
diff --git a/src/thrd/thro.cpp b/src/thrd/thro.cpp
index f6acf53..8b3f1ba 100644
--- a/src/thrd/thro.cpp
+++ b/src/thrd/thro.cpp
@@ -6,40 +6,38 @@
using namespace std;
thro::thro()
-{
-}
+{}
thro::~thro()
-{
-}
+{}
void
thro::run()
{
- void* p_void;
- run( p_void );
+ 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 );
+ elem.p_thro = this;
+ elem.p_void = p_void;
+ //wrap::POOL->add_task(start_, &elem);
+ pthread_create( &pthread, NULL, start_, &elem );
}
void*
thro::start_( void *p_void )
{
- elements *e = (elements*) p_void;
- e->p_thro->start( e->p_void );
+ elements *e = (elements*) p_void;
+ e->p_thro->start( e->p_void );
}
void
thro::start( void *p_void )
{
- wrap::system_message( THRDSTR );
+ wrap::system_message( THRDSTR );
}
-
#endif
diff --git a/src/thrd/thro.h b/src/thrd/thro.h
index ef38793..8e7e0cf 100644
--- a/src/thrd/thro.h
+++ b/src/thrd/thro.h
@@ -1,5 +1,3 @@
-// Threaded Object (thro)
-
#include "../incl.h"
#ifndef THRO_H
@@ -10,21 +8,23 @@ using namespace std;
class thro
{
private:
- pthread_t pthread;
+ pthread_t pthread;
- struct elements {
- thro* p_thro;
- void* p_void;
- } elem;
+ struct elements
+ {
+ thro *p_thro;
+ void *p_void;
+ }
+ elem;
- static void* start_( void* p_void );
+ static void *start_( void *p_void );
public:
- thro( );
- ~thro( );
- void run();
- void run( void* p_void );
- virtual void start( void* p_void );
+ thro( );
+ ~thro( );
+ void run();
+ void run( void *p_void );
+ virtual void start( void *p_void );
};
#endif