bf60e7362be6f6cf2855aef41d8b246099b8c1ac
[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_no_threads;
72         unsigned m_min_threads;
73         unsigned m_max_threads;
74         unsigned m_waiting_threads;
75     };
76     const unsigned int queue_size_per_thread = 64;
77 }
78
79
80
81 using namespace yazpp_1;
82 using namespace metaproxy_1;
83
84 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
85     : m_socketObservable(obs), m_pipe(9123)
86 {
87 }
88
89 ThreadPoolSocketObserver::Rep::~Rep()
90 {
91 }
92
93 IThreadPoolMsg::~IThreadPoolMsg()
94 {
95
96 }
97
98 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
99     yazpp_1::ISocketObservable *obs,
100     unsigned min_threads, unsigned max_threads)
101     : m_p(new Rep(obs))
102 {
103     obs->addObserver(m_p->m_pipe.read_fd(), this);
104     obs->maskObserver(this, SOCKET_OBSERVE_READ);
105
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;
110     unsigned i;
111     for (i = 0; i < m_p->m_no_threads; i++)
112     {
113         Worker w(this);
114         m_p->m_thrds.add_thread(new boost::thread(w));
115     }
116 }
117
118 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
119 {
120     {
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();
124     }
125     m_p->m_thrds.join_all();
126     m_p->m_socketObservable->deleteObserver(this);
127 }
128
129 void ThreadPoolSocketObserver::socketNotify(int event)
130 {
131     if (event & SOCKET_OBSERVE_READ)
132     {
133         char buf[2];
134 #ifdef WIN32
135         recv(m_p->m_pipe.read_fd(), buf, 1, 0);
136 #else
137         ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
138         if (r != 1)
139         {
140             if (r == (ssize_t) (-1))
141                 yaz_log(YLOG_WARN|YLOG_ERRNO,
142                         "ThreadPoolSocketObserver::socketNotify. read fail");
143             else
144                 yaz_log(YLOG_WARN,
145                         "ThreadPoolSocketObserver::socketNotify. read returned 0");
146         }
147 #endif
148         IThreadPoolMsg *out;
149         {
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();
153         }
154         if (out)
155         {
156             std::ostringstream os;
157             {
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();
164             }
165             out->result(os.str().c_str());
166         }
167     }
168 }
169
170 void ThreadPoolSocketObserver::get_thread_info(int &tbusy, int &total)
171 {
172     tbusy = m_p->m_no_threads - m_p->m_waiting_threads;
173     total = m_p->m_no_threads;
174 }
175
176 void ThreadPoolSocketObserver::run(void *p)
177 {
178     while(1)
179     {
180         IThreadPoolMsg *in = 0;
181         {
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)
188                 break;
189
190             in = m_p->m_input.front();
191             m_p->m_input.pop_front();
192             m_p->m_cond_input_full.notify_all();
193         }
194         IThreadPoolMsg *out = in->handle();
195         {
196             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
197             m_p->m_output.push_back(out);
198 #ifdef WIN32
199             send(m_p->m_pipe.write_fd(), "", 1, 0);
200 #else
201             ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
202             if (r != 1)
203             {
204                 if (r == (ssize_t) (-1))
205                     yaz_log(YLOG_WARN|YLOG_ERRNO,
206                             "ThreadPoolSocketObserver::run. write fail");
207                 else
208                     yaz_log(YLOG_WARN,
209                             "ThreadPoolSocketObserver::run. write returned 0");
210             }
211 #endif
212         }
213     }
214 }
215
216 void ThreadPoolSocketObserver::cleanup(IThreadPoolMsg *m, void *info)
217 {
218     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
219
220     std::deque<IThreadPoolMsg *>::iterator it = m_p->m_input.begin();
221     while (it != m_p->m_input.end())
222     {
223         if ((*it)->cleanup(info))
224         {
225             delete *it;
226             it = m_p->m_input.erase(it);
227         }
228         else
229             it++;
230     }
231 }
232
233 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
234 {
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)
238     {
239         m_p->m_no_threads++;
240         Worker w(this);
241         m_p->m_thrds.add_thread(new boost::thread(w));
242     }
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();
247 }
248
249 /*
250  * Local variables:
251  * c-basic-offset: 4
252  * c-file-style: "Stroustrup"
253  * indent-tabs-mode: nil
254  * End:
255  * vim: shiftwidth=4 tabstop=8 expandtab
256  */
257