diff --git a/include/ConditionalVariable.h b/include/ConditionalVariable.h deleted file mode 100644 index d9909c8e0f..0000000000 --- a/include/ConditionalVariable.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * (C) 2013-17 - ntop.org - * - * - * 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 3 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 _CONDITIONAL_VARIABLE_H_ -#define _CONDITIONAL_VARIABLE_H_ - -#include "ntop_includes.h" - -/* ******************************* */ - -class ConditionalVariable { - private: - pthread_mutex_t mutex; - pthread_cond_t condvar; - - public: - ConditionalVariable(); - ~ConditionalVariable(); - - int wait(); - int signal(bool broadcast); -}; - - -#endif /* _CONDITIONAL_VARIABLE_H_ */ diff --git a/include/Mutex.h b/include/Mutex.h index 1890009f86..b313d49904 100644 --- a/include/Mutex.h +++ b/include/Mutex.h @@ -44,6 +44,9 @@ class Mutex { void lock(const char *filename, const int line); void unlock(const char *filename, const int line); inline bool is_locked() { return(locked); }; + + /* NOTE: this must be called while locked */ + inline int cond_wait(pthread_cond_t *condvar) { return pthread_cond_wait(condvar, &the_mutex); }; }; diff --git a/include/ThreadPool.h b/include/ThreadPool.h index 784cc6dcde..1f708cfee5 100644 --- a/include/ThreadPool.h +++ b/include/ThreadPool.h @@ -29,7 +29,7 @@ class ThreadPool { bool terminating; u_int8_t pool_size; u_int16_t queue_len; - ConditionalVariable *c; + pthread_cond_t condvar; Mutex *m; pthread_t *threadsState; std::queue threads; diff --git a/include/ntop_includes.h b/include/ntop_includes.h index ba0756c818..27eff95489 100644 --- a/include/ntop_includes.h +++ b/include/ntop_includes.h @@ -158,7 +158,6 @@ using namespace std; #include "patricia.h" #include "ntop_defines.h" #include "Mutex.h" -#include "ConditionalVariable.h" #include "RwLock.h" #include "MDNS.h" #include "AddressTree.h" diff --git a/src/ConditionalVariable.cpp b/src/ConditionalVariable.cpp deleted file mode 100644 index 16708cef1e..0000000000 --- a/src/ConditionalVariable.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * - * (C) 2013-17 - ntop.org - * - * - * 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 3 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 "ntop_includes.h" - -/* ******************************* */ - -ConditionalVariable::ConditionalVariable() { - pthread_mutex_init(&mutex, NULL); - pthread_cond_init(&condvar, NULL); -} - -/* ************************************ */ - -ConditionalVariable::~ConditionalVariable() { - pthread_mutex_destroy(&mutex); - pthread_cond_destroy(&condvar); -} - -/* ************************************ */ - -int ConditionalVariable::wait() { - int rc; - - if((rc = pthread_mutex_lock(&mutex)) != 0) { - ntop->getTrace()->traceEvent(TRACE_ERROR, "lock failed %d/%s", rc, strerror(rc)); - return rc; - } - - pthread_cond_wait(&condvar, &mutex); - return(pthread_mutex_unlock(&mutex)); -} - -/* ************************************ */ - -int ConditionalVariable::signal(bool broadcast) { - int rc; - -#ifdef DEBUG - ntop->getTrace()->traceEvent(TRACE_NORMAL, "%s(%s)", __FUNCTION__, broadcast ? "broadcast" : ""); -#endif - - if(broadcast) - rc = pthread_cond_broadcast(&condvar); - else - rc = pthread_cond_signal(&condvar); - - return rc; -} - diff --git a/src/ThreadPool.cpp b/src/ThreadPool.cpp index 31c7631808..2ca0c5c70b 100644 --- a/src/ThreadPool.cpp +++ b/src/ThreadPool.cpp @@ -35,7 +35,7 @@ static void* doRun(void* ptr) { ThreadPool::ThreadPool(u_int8_t _pool_size) { pool_size = _pool_size, queue_len = 0; m = new Mutex(); - c = new ConditionalVariable(); + pthread_cond_init(&condvar, NULL); terminating = false; if((threadsState = (pthread_t*)malloc(sizeof(pthread_t)*pool_size)) == NULL) @@ -58,9 +58,9 @@ ThreadPool::~ThreadPool() { #endif pthread_join(threadsState[i], &res); } - + + pthread_cond_destroy(&condvar); delete m; - delete c; } /* **************************************************** */ @@ -107,9 +107,9 @@ bool ThreadPool::queueJob(ThreadedActivity *j) { m->lock(__FILE__, __LINE__); threads.push(j); queue_len++; + pthread_cond_signal(&condvar); m->unlock(__FILE__, __LINE__); - c->signal(false); return(true); /* TODO: add a max queue len and return false */ } @@ -118,20 +118,21 @@ bool ThreadPool::queueJob(ThreadedActivity *j) { ThreadedActivity* ThreadPool::dequeueJob(bool waitIfEmpty) { ThreadedActivity *t; + m->lock(__FILE__, __LINE__); if(waitIfEmpty) { while((queue_len == 0) && (!terminating)) - c->wait(); + m->cond_wait(&condvar); } - if((queue_len == 0) || terminating) - return(NULL); + if((queue_len == 0) || terminating) { + t = NULL; + } else { + t = threads.front(); + threads.pop(); + queue_len--; + } - m->lock(__FILE__, __LINE__); - t = threads.front(); - threads.pop(); - queue_len--; m->unlock(__FILE__, __LINE__); - return(t); } @@ -141,7 +142,9 @@ void ThreadPool::shutdown() { #ifdef THREAD_DEBUG ntop->getTrace()->traceEvent(TRACE_NORMAL, "*** %s() ***", __FUNCTION__); #endif - + + m->lock(__FILE__, __LINE__); terminating = true; - c->signal(true /* Broadcast */); + pthread_cond_broadcast(&condvar); + m->unlock(__FILE__, __LINE__); }