1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2008 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
29 #include <sys/socket.h>
32 #include <boost/thread/thread.hpp>
33 #include <boost/thread/mutex.hpp>
34 #include <boost/thread/condition.hpp>
41 #include <yazpp/socket-observer.h>
44 #include "thread_pool_observer.hpp"
47 namespace metaproxy_1 {
48 class ThreadPoolSocketObserver::Worker {
50 Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
51 ThreadPoolSocketObserver *m_s;
52 void operator() (void) {
57 class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
58 friend class ThreadPoolSocketObserver;
60 Rep(yazpp_1::ISocketObservable *obs);
63 yazpp_1::ISocketObservable *m_socketObservable;
65 boost::thread_group m_thrds;
66 boost::mutex m_mutex_input_data;
67 boost::condition m_cond_input_data;
68 boost::condition m_cond_input_full;
69 boost::mutex m_mutex_output_data;
70 std::deque<IThreadPoolMsg *> m_input;
71 std::deque<IThreadPoolMsg *> m_output;
73 unsigned m_no_threads;
75 const unsigned int queue_size_per_thread = 64;
80 using namespace yazpp_1;
81 using namespace metaproxy_1;
83 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
84 : m_socketObservable(obs), m_pipe(9123)
88 ThreadPoolSocketObserver::Rep::~Rep()
92 IThreadPoolMsg::~IThreadPoolMsg()
97 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
98 yazpp_1::ISocketObservable *obs, int no_threads)
101 obs->addObserver(m_p->m_pipe.read_fd(), this);
102 obs->maskObserver(this, SOCKET_OBSERVE_READ);
104 m_p->m_stop_flag = false;
105 m_p->m_no_threads = no_threads;
107 for (i = 0; i<no_threads; i++)
110 m_p->m_thrds.add_thread(new boost::thread(w));
114 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
117 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
118 m_p->m_stop_flag = true;
119 m_p->m_cond_input_data.notify_all();
121 m_p->m_thrds.join_all();
123 m_p->m_socketObservable->deleteObserver(this);
126 void ThreadPoolSocketObserver::socketNotify(int event)
128 if (event & SOCKET_OBSERVE_READ)
132 recv(m_p->m_pipe.read_fd(), buf, 1, 0);
134 read(m_p->m_pipe.read_fd(), buf, 1);
138 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
139 out = m_p->m_output.front();
140 m_p->m_output.pop_front();
147 void ThreadPoolSocketObserver::run(void *p)
151 IThreadPoolMsg *in = 0;
153 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
154 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
155 m_p->m_cond_input_data.wait(input_lock);
156 if (m_p->m_stop_flag)
159 in = m_p->m_input.front();
160 m_p->m_input.pop_front();
161 m_p->m_cond_input_full.notify_all();
163 IThreadPoolMsg *out = in->handle();
165 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
166 m_p->m_output.push_back(out);
168 send(m_p->m_pipe.write_fd(), "", 1, 0);
170 write(m_p->m_pipe.write_fd(), "", 1);
176 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
178 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
179 while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
180 m_p->m_cond_input_full.wait(input_lock);
181 m_p->m_input.push_back(m);
182 m_p->m_cond_input_data.notify_one();
187 * indent-tabs-mode: nil
188 * c-file-style: "stroustrup"
190 * vim: shiftwidth=4 tabstop=8 expandtab