Allow stack-size to be set for ThreadPoolSocketObserver
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20
21 #if HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #ifdef WIN32
26 #include <windows.h>
27 #include <winsock.h>
28 #endif
29 #include <boost/thread/thread.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
32
33 #include <ctype.h>
34 #include <stdio.h>
35
36 #include <deque>
37 #include <sstream>
38
39 #include <yazpp/socket-observer.h>
40 #include <yaz/log.h>
41
42 #include "thread_pool_observer.hpp"
43 #include "pipe.hpp"
44
45 namespace metaproxy_1 {
46     class ThreadPoolSocketObserver::Worker {
47     public:
48         Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
49         ThreadPoolSocketObserver *m_s;
50         void operator() (void) {
51             m_s->run(0);
52         }
53     };
54
55     class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
56         friend class ThreadPoolSocketObserver;
57     public:
58         Rep(yazpp_1::ISocketObservable *obs);
59         ~Rep();
60     private:
61         yazpp_1::ISocketObservable *m_socketObservable;
62         Pipe m_pipe;
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;
70         bool m_stop_flag;
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;
76     };
77     const unsigned int queue_size_per_thread = 64;
78 }
79
80
81
82 using namespace yazpp_1;
83 using namespace metaproxy_1;
84
85 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
86     : m_socketObservable(obs), m_pipe(9123)
87 {
88 }
89
90 ThreadPoolSocketObserver::Rep::~Rep()
91 {
92 }
93
94 IThreadPoolMsg::~IThreadPoolMsg()
95 {
96
97 }
98
99 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
100     yazpp_1::ISocketObservable *obs,
101     unsigned min_threads, unsigned max_threads,
102     unsigned stack_size)
103     : m_p(new Rep(obs))
104 {
105     obs->addObserver(m_p->m_pipe.read_fd(), this);
106     obs->maskObserver(this, SOCKET_OBSERVE_READ);
107
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;
113     unsigned i;
114     for (i = 0; i < m_p->m_no_threads; i++)
115     {
116         Worker w(this);
117         boost::thread::attributes attrs;
118         if (m_p->m_stack_size)
119             attrs.set_stack_size(m_p->m_stack_size);
120
121         boost::thread *x = new boost::thread(attrs, w);
122
123         m_p->m_thrds.add_thread(x);
124     }
125 }
126
127 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
128 {
129     {
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();
133     }
134     m_p->m_thrds.join_all();
135     m_p->m_socketObservable->deleteObserver(this);
136 }
137
138 void ThreadPoolSocketObserver::socketNotify(int event)
139 {
140     if (event & SOCKET_OBSERVE_READ)
141     {
142         char buf[2];
143 #ifdef WIN32
144         recv(m_p->m_pipe.read_fd(), buf, 1, 0);
145 #else
146         ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
147         if (r != 1)
148         {
149             if (r == (ssize_t) (-1))
150                 yaz_log(YLOG_WARN|YLOG_ERRNO,
151                         "ThreadPoolSocketObserver::socketNotify. read fail");
152             else
153                 yaz_log(YLOG_WARN,
154                         "ThreadPoolSocketObserver::socketNotify. read returned 0");
155         }
156 #endif
157         IThreadPoolMsg *out;
158         {
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();
162         }
163         if (out)
164         {
165             std::ostringstream os;
166             {
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();
173             }
174             out->result(os.str().c_str());
175         }
176     }
177 }
178
179 void ThreadPoolSocketObserver::get_thread_info(int &tbusy, int &total)
180 {
181     tbusy = m_p->m_no_threads - m_p->m_waiting_threads;
182     total = m_p->m_no_threads;
183 }
184
185 void ThreadPoolSocketObserver::run(void *p)
186 {
187     while(1)
188     {
189         IThreadPoolMsg *in = 0;
190         {
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)
197                 break;
198
199             in = m_p->m_input.front();
200             m_p->m_input.pop_front();
201             m_p->m_cond_input_full.notify_all();
202         }
203         IThreadPoolMsg *out = in->handle();
204         {
205             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
206             m_p->m_output.push_back(out);
207 #ifdef WIN32
208             send(m_p->m_pipe.write_fd(), "", 1, 0);
209 #else
210             ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
211             if (r != 1)
212             {
213                 if (r == (ssize_t) (-1))
214                     yaz_log(YLOG_WARN|YLOG_ERRNO,
215                             "ThreadPoolSocketObserver::run. write fail");
216                 else
217                     yaz_log(YLOG_WARN,
218                             "ThreadPoolSocketObserver::run. write returned 0");
219             }
220 #endif
221         }
222     }
223 }
224
225 void ThreadPoolSocketObserver::cleanup(IThreadPoolMsg *m, void *info)
226 {
227     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
228
229     std::deque<IThreadPoolMsg *>::iterator it = m_p->m_input.begin();
230     while (it != m_p->m_input.end())
231     {
232         if ((*it)->cleanup(info))
233         {
234             delete *it;
235             it = m_p->m_input.erase(it);
236         }
237         else
238             it++;
239     }
240 }
241
242 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
243 {
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)
247     {
248         m_p->m_no_threads++;
249         Worker w(this);
250
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);
255
256         m_p->m_thrds.add_thread(x);
257     }
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();
262 }
263
264 /*
265  * Local variables:
266  * c-basic-offset: 4
267  * c-file-style: "Stroustrup"
268  * indent-tabs-mode: nil
269  * End:
270  * vim: shiftwidth=4 tabstop=8 expandtab
271  */
272