summaryrefslogtreecommitdiff
path: root/src/thrd
diff options
context:
space:
mode:
authoradmin (centauri.fritz.box) <puppet@mx.buetow.org>2014-07-01 20:17:01 +0200
committeradmin (centauri.fritz.box) <puppet@mx.buetow.org>2014-07-01 20:17:01 +0200
commit174529af9197d99d2c5e8f75798fb6d6d9247254 (patch)
tree7bdb29f9391b5a3d3629bba3ebec1ca7832b3c9c /src/thrd
parent751eada68e49f9ffae9a0be743d88a3ce956883f (diff)
parent6fde6b0fe90abde84011202edd40fe46eb06af44 (diff)
Diffstat (limited to 'src/thrd')
-rw-r--r--src/thrd/pool.cpp113
-rw-r--r--src/thrd/pool.h61
-rw-r--r--src/thrd/thro.cpp67
-rw-r--r--src/thrd/thro.h54
4 files changed, 81 insertions, 214 deletions
diff --git a/src/thrd/pool.cpp b/src/thrd/pool.cpp
index a27beac..4574620 100644
--- a/src/thrd/pool.cpp
+++ b/src/thrd/pool.cpp
@@ -1,7 +1,7 @@
/*:*
*: File: ./src/thrd/pool.cpp
*:
- *: yChat; Homepage: www.yChat.org; Version 0.7.9.5-RELEASE
+ *: yChat; Homepage: www.yChat.org; Version 0.8.3-CURRENT
*:
*: Copyright (C) 2003 Paul C. Buetow, Volker Richter
*: Copyright (C) 2004 Paul C. Buetow
@@ -29,26 +29,44 @@
using namespace std;
-pool::pool()
+int pool::i_num_avail_threads;
+int pool::i_num_total_threads;
+
+int pool::i_max_queue_size;
+int pool::i_cur_queue_index;
+int pool::i_free_queue_index;
+
+task** pool::queue_tasks;
+
+pthread_mutex_t pool::mut_threads;
+pthread_mutex_t pool::mut_queue_tasks;
+pthread_mutex_t pool::mut_num_avail_threads;
+pthread_cond_t pool::cond_new_task;
+
+void
+pool::init()
{
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_total_threads = i_cur_queue_index = i_free_queue_index = 0;
i_num_avail_threads = tool::string2int( wrap::CONF->get_elem( "httpd.thread.initpoolsize" ) );
+ i_max_queue_size = tool::string2int( wrap::CONF->get_elem( "httpd.thread.maxqueuesize" ) );
+
+ queue_tasks = new task*[i_max_queue_size];
+ for (int i = 0; i < i_max_queue_size; ++i)
+ queue_tasks[i] = NULL;
+
increase_pool(i_num_avail_threads);
}
-pool::~pool()
+void
+pool::destroy()
{
pthread_mutex_lock(&mut_queue_tasks);
- while (!queue_tasks.empty())
- {
- delete queue_tasks.front();
- queue_tasks.pop();
- }
+ delete [] queue_tasks;
pthread_mutex_unlock(&mut_queue_tasks);
pthread_mutex_destroy(&mut_threads);
@@ -74,60 +92,43 @@ pool::increase_pool(int i_num)
++i_num_total_threads;
pthread_t p_pthread;
- pthread_create(&p_pthread, 0, wait_for_task, (void*) this );
+ pthread_create(&p_pthread, 0, wait_for_task, (void*) p_pthread );
}
return i_num;
}
-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);
-
- pthread_cond_signal(&cond_new_task);
-
-}
-
void*
pool::wait_for_task( void* p_void )
{
- pool* p_pool = static_cast<pool*>(p_void);
-
for (;;)
{
+ pthread_mutex_lock(&mut_threads);
+ pthread_cond_wait(&cond_new_task, &mut_threads);
-#ifdef NCURSES
- p_pool->print_pool_size();
-#endif
-
- pthread_mutex_lock(&p_pool->mut_threads);
- pthread_cond_wait(&p_pool->cond_new_task, &p_pool->mut_threads);
-
- pthread_mutex_lock(&p_pool->mut_num_avail_threads);
- if ( --p_pool->i_num_avail_threads < 5 )
+ pthread_mutex_lock(&mut_num_avail_threads);
+ if ( --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;
+ int i_size = 9 - i_num_avail_threads;
+ i_size = increase_pool(i_size);
+ i_num_avail_threads += i_size;
}
- pthread_mutex_unlock(&p_pool->mut_num_avail_threads);
+ pthread_mutex_unlock(&mut_num_avail_threads);
- 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);
+ pthread_mutex_lock(&mut_queue_tasks);
+ task* p_task = queue_tasks[i_cur_queue_index];
+ queue_tasks[i_cur_queue_index++] = NULL;
+ i_cur_queue_index %= i_max_queue_size;
+ pthread_mutex_unlock(&mut_queue_tasks);
- pthread_mutex_unlock(&p_pool->mut_threads);
+ pthread_mutex_unlock(&mut_threads);
(*(p_task->p_func))(p_task->p_void);
delete p_task;
- 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_lock(&mut_num_avail_threads);
+ i_num_avail_threads++;
+ pthread_mutex_unlock(&mut_num_avail_threads);
}
return 0;
@@ -136,13 +137,19 @@ pool::wait_for_task( void* p_void )
void
pool::run(void* p_void)
{
- add_task(run_func, p_void);
+ pthread_mutex_lock(&mut_queue_tasks);
+ queue_tasks[i_free_queue_index++] = new task(run_func, p_void);
+ i_free_queue_index %= i_max_queue_size;
+
+ pthread_mutex_unlock(&mut_queue_tasks);
+
+ pthread_cond_signal(&cond_new_task);
}
void
pool::run_func(void *p_void)
{
- socketcontainer* p_sock = static_cast<socketcontainer*>(p_void);
+ _socket* p_sock = static_cast<_socket*>(p_void);
wrap::SOCK->read_write(p_sock);
}
@@ -164,18 +171,4 @@ pool::allow_user_login()
return true;
}
-#ifdef NCURSES
-void
-pool::print_pool_size()
-{
- 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 0fe97f3..87b73c2 100644
--- a/src/thrd/pool.h
+++ b/src/thrd/pool.h
@@ -1,7 +1,7 @@
/*:*
*: File: ./src/thrd/pool.h
*:
- *: yChat; Homepage: www.yChat.org; Version 0.7.9.5-RELEASE
+ *: yChat; Homepage: www.yChat.org; Version 0.8.3-CURRENT
*:
*: Copyright (C) 2003 Paul C. Buetow, Volker Richter
*: Copyright (C) 2004 Paul C. Buetow
@@ -27,53 +27,48 @@
#ifndef POOL_H
#define POOL_H
-#include <queue>
-
using namespace std;
-class pool
+struct task
{
-private:
- friend class thro;
+ void(*p_func)(void*);
+ void *p_void;
- struct task
+ task(void(*p_func)(void*), void *p_void)
{
- void(*p_func)(void*);
- void *p_void;
+ this->p_func = p_func;
+ this->p_void = p_void;
+ }
+};
- task(void(*p_func)(void*), void *p_void)
- {
- this->p_func = p_func;
- this->p_void = p_void;
- }
- };
+class pool
+{
+private:
+ friend class thro;
- pthread_mutex_t mut_threads;
- pthread_mutex_t mut_queue_tasks;
- pthread_mutex_t mut_num_avail_threads;
- pthread_cond_t cond_new_task;
+ static pthread_mutex_t mut_threads;
+ static pthread_mutex_t mut_queue_tasks;
+ static pthread_mutex_t mut_num_avail_threads;
+ static pthread_cond_t cond_new_task;
- int i_num_avail_threads;
- int i_num_total_threads;
+ static int i_num_avail_threads;
+ static int i_num_total_threads;
- queue<task*> queue_tasks;
+ static int i_max_queue_size;
+ static int i_cur_queue_index;
+ static int i_free_queue_index;
+ static task** queue_tasks;
- int increase_pool(int i_num);
- void add_task( void(*p_func)(void*), void* p_void );
+ static int increase_pool(int i_num);
static void* wait_for_task(void *p_void);
static void run_func(void *p_void);
public:
- pool();
- ~pool();
+ static void init();
+ static void destroy();
- void run(void* p_void);
- bool allow_user_login();
-
-#ifdef NCURSES
-
- void print_pool_size();
-#endif
+ static void run(void* p_void);
+ static bool allow_user_login();
};
#endif
diff --git a/src/thrd/thro.cpp b/src/thrd/thro.cpp
deleted file mode 100644
index 04372b3..0000000
--- a/src/thrd/thro.cpp
+++ /dev/null
@@ -1,67 +0,0 @@
-/*:*
- *: File: ./src/thrd/thro.cpp
- *:
- *: yChat; Homepage: www.yChat.org; Version 0.7.9.5-RELEASE
- *:
- *: Copyright (C) 2003 Paul C. Buetow, Volker Richter
- *: Copyright (C) 2004 Paul C. Buetow
- *: Copyright (C) 2005 EXA Digital Solutions GbR
- *:
- *: This program is free software; you can redistribute it and/or
- *: modify it under the terms of the GNU General Public License
- *: as published by the Free Software Foundation; either version 2
- *: of the License, or (at your option) any later version.
- *:
- *: This program is distributed in the hope that it will be useful,
- *: but WITHOUT ANY WARRANTY; without even the implied warranty of
- *: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- *: GNU General Public License for more details.
- *:
- *: You should have received a copy of the GNU General Public License
- *: along with this program; if not, write to the Free Software
- *: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *:*/
-
-#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;
- //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 );
-}
-
-void
-thro::start( void *p_void )
-{
- wrap::system_message( THRDSTR );
-}
-
-#endif
diff --git a/src/thrd/thro.h b/src/thrd/thro.h
deleted file mode 100644
index 76e5a51..0000000
--- a/src/thrd/thro.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*:*
- *: File: ./src/thrd/thro.h
- *:
- *: yChat; Homepage: www.yChat.org; Version 0.7.9.5-RELEASE
- *:
- *: Copyright (C) 2003 Paul C. Buetow, Volker Richter
- *: Copyright (C) 2004 Paul C. Buetow
- *: Copyright (C) 2005 EXA Digital Solutions GbR
- *:
- *: This program is free software; you can redistribute it and/or
- *: modify it under the terms of the GNU General Public License
- *: as published by the Free Software Foundation; either version 2
- *: of the License, or (at your option) any later version.
- *:
- *: This program is distributed in the hope that it will be useful,
- *: but WITHOUT ANY WARRANTY; without even the implied warranty of
- *: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- *: GNU General Public License for more details.
- *:
- *: You should have received a copy of the GNU General Public License
- *: along with this program; if not, write to the Free Software
- *: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *:*/
-
-#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