Windows: use Boost 1.59, msvc 14.0
[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_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;
114     unsigned i;
115     for (i = 0; i < min_threads; i++)
116         add_worker();
117 }
118
119 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
120 {
121     {
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();
125     }
126     m_p->m_thrds.join_all();
127     m_p->m_socketObservable->deleteObserver(this);
128 }
129
130 void ThreadPoolSocketObserver::add_worker(void)
131 {
132     Worker w(this);
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);
138 #else
139     boost::thread *x = new boost::thread(w);
140 #endif
141     m_p->m_no_threads++;
142     m_p->m_thrds.add_thread(x);
143 }
144
145 void ThreadPoolSocketObserver::socketNotify(int event)
146 {
147     if (event & SOCKET_OBSERVE_READ)
148     {
149         char buf[2];
150 #ifdef WIN32
151         recv(m_p->m_pipe.read_fd(), buf, 1, 0);
152 #else
153         ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
154         if (r != 1)
155         {
156             if (r == (ssize_t) (-1))
157                 yaz_log(YLOG_WARN|YLOG_ERRNO,
158                         "ThreadPoolSocketObserver::socketNotify. read fail");
159             else
160                 yaz_log(YLOG_WARN,
161                         "ThreadPoolSocketObserver::socketNotify. read returned 0");
162         }
163 #endif
164         IThreadPoolMsg *out;
165         {
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();
169         }
170         if (out)
171         {
172             std::ostringstream os;
173             {
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();
180             }
181             out->result(os.str().c_str());
182         }
183     }
184 }
185
186 void ThreadPoolSocketObserver::get_thread_info(int &tbusy, int &total)
187 {
188     tbusy = m_p->m_no_threads - m_p->m_waiting_threads;
189     total = m_p->m_no_threads;
190 }
191
192 void ThreadPoolSocketObserver::run(void *p)
193 {
194     while(1)
195     {
196         IThreadPoolMsg *in = 0;
197         {
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)
204                 break;
205
206             in = m_p->m_input.front();
207             m_p->m_input.pop_front();
208             m_p->m_cond_input_full.notify_all();
209         }
210         IThreadPoolMsg *out = in->handle();
211         {
212             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
213             m_p->m_output.push_back(out);
214 #ifdef WIN32
215             send(m_p->m_pipe.write_fd(), "", 1, 0);
216 #else
217             ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
218             if (r != 1)
219             {
220                 if (r == (ssize_t) (-1))
221                     yaz_log(YLOG_WARN|YLOG_ERRNO,
222                             "ThreadPoolSocketObserver::run. write fail");
223                 else
224                     yaz_log(YLOG_WARN,
225                             "ThreadPoolSocketObserver::run. write returned 0");
226             }
227 #endif
228         }
229     }
230 }
231
232 void ThreadPoolSocketObserver::cleanup(IThreadPoolMsg *m, void *info)
233 {
234     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
235
236     std::deque<IThreadPoolMsg *>::iterator it = m_p->m_input.begin();
237     while (it != m_p->m_input.end())
238     {
239         if ((*it)->cleanup(info))
240         {
241             delete *it;
242             it = m_p->m_input.erase(it);
243         }
244         else
245             it++;
246     }
247 }
248
249 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
250 {
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)
254     {
255         add_worker();
256     }
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();
261 }
262
263 /*
264  * Local variables:
265  * c-basic-offset: 4
266  * c-file-style: "Stroustrup"
267  * indent-tabs-mode: nil
268  * End:
269  * vim: shiftwidth=4 tabstop=8 expandtab
270  */
271