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 ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
130 if (r == (ssize_t) (-1))
131 yaz_log(YLOG_WARN|YLOG_ERRNO,
132 "ThreadPoolSocketObserver::socketNotify. read fail");
135 "ThreadPoolSocketObserver::socketNotify. read returned 0");
140 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
141 out = m_p->m_output.front();
142 m_p->m_output.pop_front();
149 void ThreadPoolSocketObserver::run(void *p)
153 IThreadPoolMsg *in = 0;
155 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
156 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
157 m_p->m_cond_input_data.wait(input_lock);
158 if (m_p->m_stop_flag)
161 in = m_p->m_input.front();
162 m_p->m_input.pop_front();
163 m_p->m_cond_input_full.notify_all();
165 IThreadPoolMsg *out = in->handle();
167 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
168 m_p->m_output.push_back(out);
170 send(m_p->m_pipe.write_fd(), "", 1, 0);
172 ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
175 if (r == (ssize_t) (-1))
176 yaz_log(YLOG_WARN|YLOG_ERRNO,
177 "ThreadPoolSocketObserver::run. write fail");
180 "ThreadPoolSocketObserver::run. write returned 0");
187 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
189 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
190 while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
191 m_p->m_cond_input_full.wait(input_lock);
192 m_p->m_input.push_back(m);
193 m_p->m_cond_input_data.notify_one();
198 * c-file-style: "Stroustrup"
199 * indent-tabs-mode: nil
201 * vim: shiftwidth=4 tabstop=8 expandtab