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