1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 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 <boost/thread/thread.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
38 #include <yazpp/socket-observer.h>
41 #include "thread_pool_observer.hpp"
44 namespace metaproxy_1 {
45 class ThreadPoolSocketObserver::Worker {
47 Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
48 ThreadPoolSocketObserver *m_s;
49 void operator() (void) {
54 class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
55 friend class ThreadPoolSocketObserver;
57 Rep(yazpp_1::ISocketObservable *obs);
60 yazpp_1::ISocketObservable *m_socketObservable;
62 boost::thread_group m_thrds;
63 boost::mutex m_mutex_input_data;
64 boost::condition m_cond_input_data;
65 boost::condition m_cond_input_full;
66 boost::mutex m_mutex_output_data;
67 std::deque<IThreadPoolMsg *> m_input;
68 std::deque<IThreadPoolMsg *> m_output;
70 unsigned m_no_threads;
72 const unsigned int queue_size_per_thread = 64;
77 using namespace yazpp_1;
78 using namespace metaproxy_1;
80 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
81 : m_socketObservable(obs), m_pipe(9123)
85 ThreadPoolSocketObserver::Rep::~Rep()
89 IThreadPoolMsg::~IThreadPoolMsg()
94 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
95 yazpp_1::ISocketObservable *obs, int no_threads)
98 obs->addObserver(m_p->m_pipe.read_fd(), this);
99 obs->maskObserver(this, SOCKET_OBSERVE_READ);
101 m_p->m_stop_flag = false;
102 m_p->m_no_threads = no_threads;
104 for (i = 0; i<no_threads; i++)
107 m_p->m_thrds.add_thread(new boost::thread(w));
111 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
114 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
115 m_p->m_stop_flag = true;
116 m_p->m_cond_input_data.notify_all();
118 m_p->m_thrds.join_all();
120 m_p->m_socketObservable->deleteObserver(this);
123 void ThreadPoolSocketObserver::socketNotify(int event)
125 if (event & SOCKET_OBSERVE_READ)
129 recv(m_p->m_pipe.read_fd(), buf, 1, 0);
131 ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
134 if (r == (ssize_t) (-1))
135 yaz_log(YLOG_WARN|YLOG_ERRNO,
136 "ThreadPoolSocketObserver::socketNotify. read fail");
139 "ThreadPoolSocketObserver::socketNotify. read returned 0");
144 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
145 out = m_p->m_output.front();
146 m_p->m_output.pop_front();
153 void ThreadPoolSocketObserver::run(void *p)
157 IThreadPoolMsg *in = 0;
159 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
160 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
161 m_p->m_cond_input_data.wait(input_lock);
162 if (m_p->m_stop_flag)
165 in = m_p->m_input.front();
166 m_p->m_input.pop_front();
167 m_p->m_cond_input_full.notify_all();
169 IThreadPoolMsg *out = in->handle();
171 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
172 m_p->m_output.push_back(out);
174 send(m_p->m_pipe.write_fd(), "", 1, 0);
176 ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
179 if (r == (ssize_t) (-1))
180 yaz_log(YLOG_WARN|YLOG_ERRNO,
181 "ThreadPoolSocketObserver::run. write fail");
184 "ThreadPoolSocketObserver::run. write returned 0");
191 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
193 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
194 while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
195 m_p->m_cond_input_full.wait(input_lock);
196 m_p->m_input.push_back(m);
197 m_p->m_cond_input_data.notify_one();
202 * c-file-style: "Stroustrup"
203 * indent-tabs-mode: nil
205 * vim: shiftwidth=4 tabstop=8 expandtab