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_stack_size;
72 unsigned m_no_threads;
73 unsigned m_min_threads;
74 unsigned m_max_threads;
75 unsigned m_waiting_threads;
77 const unsigned int queue_size_per_thread = 64;
82 using namespace yazpp_1;
83 using namespace metaproxy_1;
85 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
86 : m_socketObservable(obs), m_pipe(9123)
90 ThreadPoolSocketObserver::Rep::~Rep()
94 IThreadPoolMsg::~IThreadPoolMsg()
99 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
100 yazpp_1::ISocketObservable *obs,
101 unsigned min_threads, unsigned max_threads,
105 obs->addObserver(m_p->m_pipe.read_fd(), this);
106 obs->maskObserver(this, SOCKET_OBSERVE_READ);
108 m_p->m_stop_flag = false;
109 m_p->m_min_threads = m_p->m_no_threads = min_threads;
110 m_p->m_max_threads = max_threads;
111 m_p->m_waiting_threads = 0;
112 m_p->m_stack_size = stack_size;
114 for (i = 0; i < m_p->m_no_threads; i++)
117 boost::thread::attributes attrs;
118 if (m_p->m_stack_size)
119 attrs.set_stack_size(m_p->m_stack_size);
121 boost::thread *x = new boost::thread(attrs, w);
123 m_p->m_thrds.add_thread(x);
127 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
130 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
131 m_p->m_stop_flag = true;
132 m_p->m_cond_input_data.notify_all();
134 m_p->m_thrds.join_all();
135 m_p->m_socketObservable->deleteObserver(this);
138 void ThreadPoolSocketObserver::socketNotify(int event)
140 if (event & SOCKET_OBSERVE_READ)
144 recv(m_p->m_pipe.read_fd(), buf, 1, 0);
146 ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
149 if (r == (ssize_t) (-1))
150 yaz_log(YLOG_WARN|YLOG_ERRNO,
151 "ThreadPoolSocketObserver::socketNotify. read fail");
154 "ThreadPoolSocketObserver::socketNotify. read returned 0");
159 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
160 out = m_p->m_output.front();
161 m_p->m_output.pop_front();
165 std::ostringstream os;
167 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
168 os << "tbusy/total " <<
169 m_p->m_no_threads - m_p->m_waiting_threads <<
170 "/" << m_p->m_no_threads
171 << " queue in/out " << m_p->m_input.size() << "/"
172 << m_p->m_output.size();
174 out->result(os.str().c_str());
179 void ThreadPoolSocketObserver::get_thread_info(int &tbusy, int &total)
181 tbusy = m_p->m_no_threads - m_p->m_waiting_threads;
182 total = m_p->m_no_threads;
185 void ThreadPoolSocketObserver::run(void *p)
189 IThreadPoolMsg *in = 0;
191 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
192 m_p->m_waiting_threads++;
193 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
194 m_p->m_cond_input_data.wait(input_lock);
195 m_p->m_waiting_threads--;
196 if (m_p->m_stop_flag)
199 in = m_p->m_input.front();
200 m_p->m_input.pop_front();
201 m_p->m_cond_input_full.notify_all();
203 IThreadPoolMsg *out = in->handle();
205 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
206 m_p->m_output.push_back(out);
208 send(m_p->m_pipe.write_fd(), "", 1, 0);
210 ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
213 if (r == (ssize_t) (-1))
214 yaz_log(YLOG_WARN|YLOG_ERRNO,
215 "ThreadPoolSocketObserver::run. write fail");
218 "ThreadPoolSocketObserver::run. write returned 0");
225 void ThreadPoolSocketObserver::cleanup(IThreadPoolMsg *m, void *info)
227 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
229 std::deque<IThreadPoolMsg *>::iterator it = m_p->m_input.begin();
230 while (it != m_p->m_input.end())
232 if ((*it)->cleanup(info))
235 it = m_p->m_input.erase(it);
242 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
244 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
245 if (m_p->m_waiting_threads == 0 &&
246 m_p->m_no_threads < m_p->m_max_threads)
251 boost::thread::attributes attrs;
252 if (m_p->m_stack_size)
253 attrs.set_stack_size(m_p->m_stack_size);
254 boost::thread *x = new boost::thread(attrs, w);
256 m_p->m_thrds.add_thread(x);
258 while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
259 m_p->m_cond_input_full.wait(input_lock);
260 m_p->m_input.push_back(m);
261 m_p->m_cond_input_data.notify_one();
267 * c-file-style: "Stroustrup"
268 * indent-tabs-mode: nil
270 * vim: shiftwidth=4 tabstop=8 expandtab