1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2009 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <boost/thread/thread.hpp>
26 #include <boost/thread/mutex.hpp>
27 #include <boost/thread/condition.hpp>
34 #include <yazpp/socket-observer.h>
37 #include "thread_pool_observer.hpp"
40 namespace metaproxy_1 {
41 class ThreadPoolSocketObserver::Worker {
43 Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
44 ThreadPoolSocketObserver *m_s;
45 void operator() (void) {
50 class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
51 friend class ThreadPoolSocketObserver;
53 Rep(yazpp_1::ISocketObservable *obs);
56 yazpp_1::ISocketObservable *m_socketObservable;
58 boost::thread_group m_thrds;
59 boost::mutex m_mutex_input_data;
60 boost::condition m_cond_input_data;
61 boost::condition m_cond_input_full;
62 boost::mutex m_mutex_output_data;
63 std::deque<IThreadPoolMsg *> m_input;
64 std::deque<IThreadPoolMsg *> m_output;
66 unsigned m_no_threads;
68 const unsigned int queue_size_per_thread = 64;
73 using namespace yazpp_1;
74 using namespace metaproxy_1;
76 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
77 : m_socketObservable(obs), m_pipe(9123)
81 ThreadPoolSocketObserver::Rep::~Rep()
85 IThreadPoolMsg::~IThreadPoolMsg()
90 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
91 yazpp_1::ISocketObservable *obs, int no_threads)
94 obs->addObserver(m_p->m_pipe.read_fd(), this);
95 obs->maskObserver(this, SOCKET_OBSERVE_READ);
97 m_p->m_stop_flag = false;
98 m_p->m_no_threads = no_threads;
100 for (i = 0; i<no_threads; i++)
103 m_p->m_thrds.add_thread(new boost::thread(w));
107 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
110 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
111 m_p->m_stop_flag = true;
112 m_p->m_cond_input_data.notify_all();
114 m_p->m_thrds.join_all();
116 m_p->m_socketObservable->deleteObserver(this);
119 void ThreadPoolSocketObserver::socketNotify(int event)
121 if (event & SOCKET_OBSERVE_READ)
125 recv(m_p->m_pipe.read_fd(), buf, 1, 0);
127 read(m_p->m_pipe.read_fd(), buf, 1);
131 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
132 out = m_p->m_output.front();
133 m_p->m_output.pop_front();
140 void ThreadPoolSocketObserver::run(void *p)
144 IThreadPoolMsg *in = 0;
146 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
147 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
148 m_p->m_cond_input_data.wait(input_lock);
149 if (m_p->m_stop_flag)
152 in = m_p->m_input.front();
153 m_p->m_input.pop_front();
154 m_p->m_cond_input_full.notify_all();
156 IThreadPoolMsg *out = in->handle();
158 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
159 m_p->m_output.push_back(out);
161 send(m_p->m_pipe.write_fd(), "", 1, 0);
163 write(m_p->m_pipe.write_fd(), "", 1);
169 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
171 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
172 while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
173 m_p->m_cond_input_full.wait(input_lock);
174 m_p->m_input.push_back(m);
175 m_p->m_cond_input_data.notify_one();
180 * c-file-style: "Stroustrup"
181 * indent-tabs-mode: nil
183 * vim: shiftwidth=4 tabstop=8 expandtab