1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 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;
71 unsigned m_no_threads_waiting;
73 const unsigned int queue_size_per_thread = 64;
78 using namespace yazpp_1;
79 using namespace metaproxy_1;
81 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
82 : m_socketObservable(obs), m_pipe(9123)
86 ThreadPoolSocketObserver::Rep::~Rep()
90 IThreadPoolMsg::~IThreadPoolMsg()
95 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
96 yazpp_1::ISocketObservable *obs, int no_threads)
99 obs->addObserver(m_p->m_pipe.read_fd(), this);
100 obs->maskObserver(this, SOCKET_OBSERVE_READ);
102 m_p->m_stop_flag = false;
103 m_p->m_no_threads = no_threads;
104 m_p->m_no_threads_waiting = 0;
106 for (i = 0; i<no_threads; i++)
109 m_p->m_thrds.add_thread(new boost::thread(w));
113 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
116 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
117 m_p->m_stop_flag = true;
118 m_p->m_cond_input_data.notify_all();
120 m_p->m_thrds.join_all();
122 m_p->m_socketObservable->deleteObserver(this);
125 void ThreadPoolSocketObserver::socketNotify(int event)
127 if (event & SOCKET_OBSERVE_READ)
131 recv(m_p->m_pipe.read_fd(), buf, 1, 0);
133 ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
136 if (r == (ssize_t) (-1))
137 yaz_log(YLOG_WARN|YLOG_ERRNO,
138 "ThreadPoolSocketObserver::socketNotify. read fail");
141 "ThreadPoolSocketObserver::socketNotify. read returned 0");
146 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
147 out = m_p->m_output.front();
148 m_p->m_output.pop_front();
154 std::ostringstream os;
156 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
157 os << "tbusy/total " <<
158 m_p->m_no_threads - m_p->m_no_threads_waiting <<
159 "/" << m_p->m_no_threads
160 << " queue in/out " << m_p->m_input.size() << "/"
161 << m_p->m_output.size();
163 out->result(os.str().c_str());
168 void ThreadPoolSocketObserver::run(void *p)
172 IThreadPoolMsg *in = 0;
174 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
175 m_p->m_no_threads_waiting++;
176 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
177 m_p->m_cond_input_data.wait(input_lock);
178 m_p->m_no_threads_waiting--;
179 if (m_p->m_stop_flag)
182 in = m_p->m_input.front();
183 m_p->m_input.pop_front();
184 m_p->m_cond_input_full.notify_all();
186 IThreadPoolMsg *out = in->handle();
188 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
189 m_p->m_output.push_back(out);
191 send(m_p->m_pipe.write_fd(), "", 1, 0);
193 ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
196 if (r == (ssize_t) (-1))
197 yaz_log(YLOG_WARN|YLOG_ERRNO,
198 "ThreadPoolSocketObserver::run. write fail");
201 "ThreadPoolSocketObserver::run. write returned 0");
208 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
210 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
211 while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
212 m_p->m_cond_input_full.wait(input_lock);
213 m_p->m_input.push_back(m);
214 m_p->m_cond_input_data.notify_one();
219 * c-file-style: "Stroustrup"
220 * indent-tabs-mode: nil
222 * vim: shiftwidth=4 tabstop=8 expandtab