From 9c480b66660e691cd0dc5ed869c1d902250dd674 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 12 Oct 2005 23:30:43 +0000 Subject: [PATCH] Make test_thread_pool_observer a Boost test. Use Boost::thread in the implementation of ThreadPoolSocketObserver. --- src/Makefile.am | 6 +-- src/test_thread_pool_observer.cpp | 62 ++++++++++++++++--------- src/thread_pool_observer.cpp | 93 +++++++++++++++++-------------------- src/thread_pool_observer.h | 16 ++++--- 4 files changed, 93 insertions(+), 84 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 42e729f..0f2d0c7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.14 2005-10-12 20:45:56 marc Exp $ +## $Id: Makefile.am,v 1.15 2005-10-12 23:30:43 adam Exp $ MAINTAINERCLEANFILES = Makefile.in config.in config.hpp @@ -40,9 +40,7 @@ test_session1_LDADD = $(LDADD) -lboost_unit_test_framework test_session2_LDADD = $(LDADD) -lboost_unit_test_framework test_boost_threads_LDADD = $(LDADD) -lboost_unit_test_framework test_boost_time_LDADD = $(LDADD) -lboost_unit_test_framework - - - +test_thread_pool_observer_LDADD = $(LDADD) -lboost_unit_test_framework # doxygen target dox: diff --git a/src/test_thread_pool_observer.cpp b/src/test_thread_pool_observer.cpp index 5ca3bfd..8086d2c 100644 --- a/src/test_thread_pool_observer.cpp +++ b/src/test_thread_pool_observer.cpp @@ -1,4 +1,4 @@ -/* $Id: test_thread_pool_observer.cpp,v 1.2 2005-10-08 23:29:32 adam Exp $ +/* $Id: test_thread_pool_observer.cpp,v 1.3 2005-10-12 23:30:43 adam Exp $ Copyright (c) 1998-2005, Index Data. This file is part of the yaz-proxy. @@ -28,71 +28,87 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include "thread_pool_observer.h" +#define BOOST_AUTO_TEST_MAIN +#include + +using namespace boost::unit_test; using namespace yazpp_1; +class My_Timer_Thread; + class My_Msg : public IThreadPoolMsg { public: IThreadPoolMsg *handle(); void result(); int m_val; + My_Timer_Thread *m_timer; }; +class My_Timer_Thread : public ISocketObserver { +private: + ISocketObservable *m_obs; + int m_fd[2]; + ThreadPoolSocketObserver *m_t; +public: + int m_sum; + int m_requests; + int m_responses; + My_Timer_Thread(ISocketObservable *obs, ThreadPoolSocketObserver *t); + void socketNotify(int event); +}; + + IThreadPoolMsg *My_Msg::handle() { My_Msg *res = new My_Msg; - int sl = rand() % 5; + + if (m_val == 7) + sleep(1); res->m_val = m_val; - printf("My_Msg::handle val=%d sleep=%d\n", m_val, sl); - sleep(sl); + res->m_timer = m_timer; return res; } void My_Msg::result() { - printf("My_Msg::result val=%d\n", m_val); + m_timer->m_sum += m_val; + m_timer->m_responses++; } -class My_Timer_Thread : public ISocketObserver { -private: - ISocketObservable *m_obs; - int m_fd[2]; - ThreadPoolSocketObserver *m_t; -public: - My_Timer_Thread(ISocketObservable *obs, ThreadPoolSocketObserver *t); - void socketNotify(int event); -}; - My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs, ThreadPoolSocketObserver *t) : m_obs(obs) { pipe(m_fd); m_t = t; + m_sum = 0; + m_requests = 0; + m_responses = 0; obs->addObserver(m_fd[0], this); obs->maskObserver(this, SOCKET_OBSERVE_READ); - obs->timeoutObserver(this, 1); + obs->timeoutObserver(this, 0); } void My_Timer_Thread::socketNotify(int event) { - static int seq = 1; - printf("Add %d\n", seq); My_Msg *m = new My_Msg; - m->m_val = seq++; + m->m_val = m_requests++; + m->m_timer = this; m_t->put(m); } -int main(int argc, char **argv) +BOOST_AUTO_TEST_CASE( thread_pool_observer1 ) { SocketManager mySocketManager; ThreadPoolSocketObserver m(&mySocketManager, 3); My_Timer_Thread t(&mySocketManager, &m) ; - int i = 0; - while (++i < 5 && mySocketManager.processEvent() > 0) + while (t.m_responses < 30 && mySocketManager.processEvent() > 0) ; - return 0; + BOOST_CHECK_EQUAL(t.m_responses, 30); + BOOST_CHECK(t.m_sum >= 435); } + /* * Local variables: * c-basic-offset: 4 diff --git a/src/thread_pool_observer.cpp b/src/thread_pool_observer.cpp index b626e92..b66be7e 100644 --- a/src/thread_pool_observer.cpp +++ b/src/thread_pool_observer.cpp @@ -1,4 +1,4 @@ -/* $Id: thread_pool_observer.cpp,v 1.2 2005-10-08 23:29:32 adam Exp $ +/* $Id: thread_pool_observer.cpp,v 1.3 2005-10-12 23:30:43 adam Exp $ Copyright (c) 1998-2005, Index Data. This file is part of the yaz-proxy. @@ -18,7 +18,6 @@ along with YAZ proxy; see the file LICENSE. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include #include #include #include @@ -36,13 +35,14 @@ IThreadPoolMsg::~IThreadPoolMsg() } -static void *tfunc(void *p) -{ - ThreadPoolSocketObserver *pt = (ThreadPoolSocketObserver *) p; - pt->run(0); - return 0; -} - +class worker { +public: + worker(ThreadPoolSocketObserver *s) : m_s(s) {}; + ThreadPoolSocketObserver *m_s; + void operator() (void) { + m_s->run(0); + } +}; ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs, int no_threads) : m_SocketObservable(obs) @@ -52,34 +52,26 @@ ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs, int n obs->maskObserver(this, SOCKET_OBSERVE_READ); m_stop_flag = false; - pthread_mutex_init(&m_mutex_input_data, 0); - pthread_cond_init(&m_cond_input_data, 0); - pthread_mutex_init(&m_mutex_output_data, 0); - m_no_threads = no_threads; - m_thread_id = new pthread_t[no_threads]; int i; - for (i = 0; ideleteObserver(this); - pthread_cond_destroy(&m_cond_input_data); - pthread_mutex_destroy(&m_mutex_input_data); - pthread_mutex_destroy(&m_mutex_output_data); close(m_fd[0]); close(m_fd[1]); } @@ -90,10 +82,12 @@ void ThreadPoolSocketObserver::socketNotify(int event) { char buf[2]; read(m_fd[0], buf, 1); - pthread_mutex_lock(&m_mutex_output_data); - IThreadPoolMsg *out = m_output.front(); - m_output.pop_front(); - pthread_mutex_unlock(&m_mutex_output_data); + IThreadPoolMsg *out; + { + boost::mutex::scoped_lock output_lock(m_mutex_output_data); + out = m_output.front(); + m_output.pop_front(); + } if (out) out->result(); } @@ -103,34 +97,31 @@ void ThreadPoolSocketObserver::run(void *p) { while(1) { - pthread_mutex_lock(&m_mutex_input_data); - while (!m_stop_flag && m_input.size() == 0) - pthread_cond_wait(&m_cond_input_data, &m_mutex_input_data); - if (m_stop_flag) + IThreadPoolMsg *in = 0; { - pthread_mutex_unlock(&m_mutex_input_data); - break; + boost::mutex::scoped_lock input_lock(m_mutex_input_data); + while (!m_stop_flag && m_input.size() == 0) + m_cond_input_data.wait(input_lock); + if (m_stop_flag) + break; + + in = m_input.front(); + m_input.pop_front(); } - IThreadPoolMsg *in = m_input.front(); - m_input.pop_front(); - pthread_mutex_unlock(&m_mutex_input_data); - IThreadPoolMsg *out = in->handle(); - pthread_mutex_lock(&m_mutex_output_data); - - m_output.push_back(out); - - write(m_fd[1], "", 1); - pthread_mutex_unlock(&m_mutex_output_data); + { + boost::mutex::scoped_lock output_lock(m_mutex_output_data); + m_output.push_back(out); + write(m_fd[1], "", 1); + } } } void ThreadPoolSocketObserver::put(IThreadPoolMsg *m) { - pthread_mutex_lock(&m_mutex_input_data); + boost::mutex::scoped_lock input_lock(m_mutex_input_data); m_input.push_back(m); - pthread_cond_signal(&m_cond_input_data); - pthread_mutex_unlock(&m_mutex_input_data); + m_cond_input_data.notify_one(); } /* * Local variables: diff --git a/src/thread_pool_observer.h b/src/thread_pool_observer.h index 03604c2..34e23a0 100644 --- a/src/thread_pool_observer.h +++ b/src/thread_pool_observer.h @@ -1,4 +1,4 @@ -/* $Id: thread_pool_observer.h,v 1.1 2005-10-06 19:33:58 adam Exp $ +/* $Id: thread_pool_observer.h,v 1.2 2005-10-12 23:30:43 adam Exp $ Copyright (c) 1998-2005, Index Data. This file is part of the yaz-proxy. @@ -19,7 +19,10 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include +#include +#include +#include + #include #include @@ -50,13 +53,14 @@ class ThreadPoolSocketObserver : public yazpp_1::ISocketObserver { private: yazpp_1::ISocketObservable *m_SocketObservable; int m_no_threads; - pthread_t *m_thread_id; + boost::thread_group m_thrds; std::deque m_input; std::deque m_output; - pthread_mutex_t m_mutex_input_data; - pthread_cond_t m_cond_input_data; - pthread_mutex_t m_mutex_output_data; + + boost::mutex m_mutex_input_data; + boost::condition m_cond_input_data; + boost::mutex m_mutex_output_data; bool m_stop_flag; }; -- 1.7.10.4