1 /* $Id: thread_pool_observer.cpp,v 1.7 2005-10-15 14:09:09 adam Exp $
2 Copyright (c) 2005, Index Data.
10 #include <yaz++/socket-observer.h>
14 #include "thread_pool_observer.hpp"
16 using namespace yazpp_1;
19 IThreadPoolMsg::~IThreadPoolMsg()
24 ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs, int no_threads)
25 : m_SocketObservable(obs)
28 obs->addObserver(m_fd[0], this);
29 obs->maskObserver(this, SOCKET_OBSERVE_READ);
32 m_no_threads = no_threads;
34 for (i = 0; i<no_threads; i++)
37 m_thrds.add_thread(new boost::thread(w));
41 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
44 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
46 m_cond_input_data.notify_all();
50 m_SocketObservable->deleteObserver(this);
56 void ThreadPoolSocketObserver::socketNotify(int event)
58 if (event & SOCKET_OBSERVE_READ)
61 read(m_fd[0], buf, 1);
64 boost::mutex::scoped_lock output_lock(m_mutex_output_data);
65 out = m_output.front();
73 void ThreadPoolSocketObserver::run(void *p)
77 IThreadPoolMsg *in = 0;
79 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
80 while (!m_stop_flag && m_input.size() == 0)
81 m_cond_input_data.wait(input_lock);
88 IThreadPoolMsg *out = in->handle();
90 boost::mutex::scoped_lock output_lock(m_mutex_output_data);
91 m_output.push_back(out);
92 write(m_fd[1], "", 1);
97 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
99 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
100 m_input.push_back(m);
101 m_cond_input_data.notify_one();
106 * indent-tabs-mode: nil
107 * c-file-style: "stroustrup"
109 * vim: shiftwidth=4 tabstop=8 expandtab