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_no_threads = 0;
110 m_p->m_min_threads = min_threads;
111 m_p->m_max_threads = max_threads;
112 m_p->m_waiting_threads = 0;
113 m_p->m_stack_size = stack_size;
115 for (i = 0; i < min_threads; i++)
119 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
122 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
123 m_p->m_stop_flag = true;
124 m_p->m_cond_input_data.notify_all();
126 m_p->m_thrds.join_all();
127 m_p->m_socketObservable->deleteObserver(this);
130 void ThreadPoolSocketObserver::add_worker(void)
133 #if BOOST_VERSION >= 1050000
134 boost::thread::attributes attrs;
135 if (m_p->m_stack_size)
136 attrs.set_stack_size(m_stack_size);
137 boost::thread *x = new boost::thread(attrs, w);
139 boost::thread *x = new boost::thread(w);
142 m_p->m_thrds.add_thread(x);
145 void ThreadPoolSocketObserver::socketNotify(int event)
147 if (event & SOCKET_OBSERVE_READ)
151 recv(m_p->m_pipe.read_fd(), buf, 1, 0);
153 ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
156 if (r == (ssize_t) (-1))
157 yaz_log(YLOG_WARN|YLOG_ERRNO,
158 "ThreadPoolSocketObserver::socketNotify. read fail");
161 "ThreadPoolSocketObserver::socketNotify. read returned 0");
166 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
167 out = m_p->m_output.front();
168 m_p->m_output.pop_front();
172 std::ostringstream os;
174 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
175 os << "tbusy/total " <<
176 m_p->m_no_threads - m_p->m_waiting_threads <<
177 "/" << m_p->m_no_threads
178 << " queue in/out " << m_p->m_input.size() << "/"
179 << m_p->m_output.size();
181 out->result(os.str().c_str());
186 void ThreadPoolSocketObserver::get_thread_info(int &tbusy, int &total)
188 tbusy = m_p->m_no_threads - m_p->m_waiting_threads;
189 total = m_p->m_no_threads;
192 void ThreadPoolSocketObserver::run(void *p)
196 IThreadPoolMsg *in = 0;
198 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
199 m_p->m_waiting_threads++;
200 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
201 m_p->m_cond_input_data.wait(input_lock);
202 m_p->m_waiting_threads--;
203 if (m_p->m_stop_flag)
206 in = m_p->m_input.front();
207 m_p->m_input.pop_front();
208 m_p->m_cond_input_full.notify_all();
210 IThreadPoolMsg *out = in->handle();
212 boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
213 m_p->m_output.push_back(out);
215 send(m_p->m_pipe.write_fd(), "", 1, 0);
217 ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
220 if (r == (ssize_t) (-1))
221 yaz_log(YLOG_WARN|YLOG_ERRNO,
222 "ThreadPoolSocketObserver::run. write fail");
225 "ThreadPoolSocketObserver::run. write returned 0");
232 void ThreadPoolSocketObserver::cleanup(IThreadPoolMsg *m, void *info)
234 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
236 std::deque<IThreadPoolMsg *>::iterator it = m_p->m_input.begin();
237 while (it != m_p->m_input.end())
239 if ((*it)->cleanup(info))
242 it = m_p->m_input.erase(it);
249 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
251 boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
252 if (m_p->m_waiting_threads == 0 &&
253 m_p->m_no_threads < m_p->m_max_threads)
257 while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
258 m_p->m_cond_input_full.wait(input_lock);
259 m_p->m_input.push_back(m);
260 m_p->m_cond_input_data.notify_one();
266 * c-file-style: "Stroustrup"
267 * indent-tabs-mode: nil
269 * vim: shiftwidth=4 tabstop=8 expandtab