1 /* This file is part of Metaproxy.
2 Copyright (C) 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>
39 #include <yazpp/socket-observer.h>
42 #include "thread_pool_observer.hpp"
45 namespace metaproxy_1 {
46 class ThreadPoolSocketObserver::Worker {
48 Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
49 ThreadPoolSocketObserver *m_s;
50 void operator() (void) {
55 class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
56 friend class ThreadPoolSocketObserver;
58 Rep(yazpp_1::ISocketObservable *obs);
61 yazpp_1::ISocketObservable *m_socketObservable;
63 boost::thread_group m_thrds;
64 boost::mutex m_mutex_input_data;
65 boost::condition m_cond_input_data;
66 boost::condition m_cond_input_full;
67 boost::mutex m_mutex_output_data;
68 std::deque<IThreadPoolMsg *> m_input;
69 std::deque<IThreadPoolMsg *> m_output;
71 unsigned m_no_threads;
72 unsigned m_min_threads;
73 unsigned m_max_threads;
74 unsigned m_waiting_threads;
76 const unsigned int queue_size_per_thread = 64;
81 using namespace yazpp_1;
82 using namespace metaproxy_1;
84 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
85 : m_socketObservable(obs), m_pipe(9123)
89 ThreadPoolSocketObserver::Rep::~Rep()
93 IThreadPoolMsg::~IThreadPoolMsg()
98 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
99 yazpp_1::ISocketObservable *obs,
100 unsigned min_threads, unsigned max_threads)
103 obs->addObserver(m_p->m_pipe.read_fd(), this);
104 obs->maskObserver(this, SOCKET_OBSERVE_READ);
106 m_p->m_stop_flag = false;
107 m_p->m_min_threads = m_p->m_no_threads = min_threads;
108 m_p->m_max_threads = max_threads;
109 m_p->m_waiting_threads = 0;
111 for (i = 0; i < m_p->m_no_threads; i++)
114 m_p->m_thrds.add_thread(new boost::thread(w));
118 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
121 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
122 m_p->m_stop_flag = true;
123 m_p->m_cond_input_data.notify_all();
125 m_p->m_thrds.join_all();
126 m_p->m_socketObservable->deleteObserver(this);
129 void ThreadPoolSocketObserver::socketNotify(int event)
131 if (event & SOCKET_OBSERVE_READ)
135 recv(m_p->m_pipe.read_fd(), buf, 1, 0);
137 ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
140 if (r == (ssize_t) (-1))
141 yaz_log(YLOG_WARN|YLOG_ERRNO,
142 "ThreadPoolSocketObserver::socketNotify. read fail");
145 "ThreadPoolSocketObserver::socketNotify. read returned 0");
150 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
151 out = m_p->m_output.front();
152 m_p->m_output.pop_front();
156 std::ostringstream os;
158 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
159 os << "tbusy/total " <<
160 m_p->m_no_threads - m_p->m_waiting_threads <<
161 "/" << m_p->m_no_threads
162 << " queue in/out " << m_p->m_input.size() << "/"
163 << m_p->m_output.size();
165 out->result(os.str().c_str());
170 void ThreadPoolSocketObserver::get_thread_info(int &tbusy, int &total)
172 tbusy = m_p->m_no_threads - m_p->m_waiting_threads;
173 total = m_p->m_no_threads;
176 void ThreadPoolSocketObserver::run(void *p)
180 IThreadPoolMsg *in = 0;
182 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
183 m_p->m_waiting_threads++;
184 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
185 m_p->m_cond_input_data.wait(input_lock);
186 m_p->m_waiting_threads--;
187 if (m_p->m_stop_flag)
190 in = m_p->m_input.front();
191 m_p->m_input.pop_front();
192 m_p->m_cond_input_full.notify_all();
194 IThreadPoolMsg *out = in->handle();
196 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
197 m_p->m_output.push_back(out);
199 send(m_p->m_pipe.write_fd(), "", 1, 0);
201 ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
204 if (r == (ssize_t) (-1))
205 yaz_log(YLOG_WARN|YLOG_ERRNO,
206 "ThreadPoolSocketObserver::run. write fail");
209 "ThreadPoolSocketObserver::run. write returned 0");
216 void ThreadPoolSocketObserver::cleanup(IThreadPoolMsg *m, void *info)
218 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
220 std::deque<IThreadPoolMsg *>::iterator it = m_p->m_input.begin();
221 while (it != m_p->m_input.end())
223 if ((*it)->cleanup(info))
226 it = m_p->m_input.erase(it);
233 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
235 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
236 if (m_p->m_waiting_threads == 0 &&
237 m_p->m_no_threads < m_p->m_max_threads)
241 m_p->m_thrds.add_thread(new boost::thread(w));
243 while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
244 m_p->m_cond_input_full.wait(input_lock);
245 m_p->m_input.push_back(m);
246 m_p->m_cond_input_data.notify_one();
252 * c-file-style: "Stroustrup"
253 * indent-tabs-mode: nil
255 * vim: shiftwidth=4 tabstop=8 expandtab