Fix GCC warnings. Set imp name/version for init response
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2009 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 #include <boost/thread/thread.hpp>
26 #include <boost/thread/mutex.hpp>
27 #include <boost/thread/condition.hpp>
28
29 #include <ctype.h>
30 #include <stdio.h>
31
32 #include <deque>
33
34 #include <yazpp/socket-observer.h>
35 #include <yaz/log.h>
36
37 #include "thread_pool_observer.hpp"
38 #include "pipe.hpp"
39
40 namespace metaproxy_1 {
41     class ThreadPoolSocketObserver::Worker {
42     public:
43         Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
44         ThreadPoolSocketObserver *m_s;
45         void operator() (void) {
46             m_s->run(0);
47         }
48     };
49
50     class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
51         friend class ThreadPoolSocketObserver;
52     public:
53         Rep(yazpp_1::ISocketObservable *obs);
54         ~Rep();
55     private:
56         yazpp_1::ISocketObservable *m_socketObservable;
57         Pipe m_pipe;
58         boost::thread_group m_thrds;
59         boost::mutex m_mutex_input_data;
60         boost::condition m_cond_input_data;
61         boost::condition m_cond_input_full;
62         boost::mutex m_mutex_output_data;
63         std::deque<IThreadPoolMsg *> m_input;
64         std::deque<IThreadPoolMsg *> m_output;
65         bool m_stop_flag;
66         unsigned m_no_threads;
67     };
68     const unsigned int queue_size_per_thread = 64;
69 }
70
71
72
73 using namespace yazpp_1;
74 using namespace metaproxy_1;
75
76 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
77     : m_socketObservable(obs), m_pipe(9123)
78 {
79 }
80
81 ThreadPoolSocketObserver::Rep::~Rep()
82 {
83 }
84
85 IThreadPoolMsg::~IThreadPoolMsg()
86 {
87
88 }
89
90 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
91     yazpp_1::ISocketObservable *obs, int no_threads)
92     : m_p(new Rep(obs))
93 {
94     obs->addObserver(m_p->m_pipe.read_fd(), this);
95     obs->maskObserver(this, SOCKET_OBSERVE_READ);
96
97     m_p->m_stop_flag = false;
98     m_p->m_no_threads = no_threads;
99     int i;
100     for (i = 0; i<no_threads; i++)
101     {
102         Worker w(this);
103         m_p->m_thrds.add_thread(new boost::thread(w));
104     }
105 }
106
107 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
108 {
109     {
110         boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
111         m_p->m_stop_flag = true;
112         m_p->m_cond_input_data.notify_all();
113     }
114     m_p->m_thrds.join_all();
115
116     m_p->m_socketObservable->deleteObserver(this);
117 }
118
119 void ThreadPoolSocketObserver::socketNotify(int event)
120 {
121     if (event & SOCKET_OBSERVE_READ)
122     {
123         char buf[2];
124 #ifdef WIN32
125         recv(m_p->m_pipe.read_fd(), buf, 1, 0);
126 #else
127         ssize_t r = read(m_p->m_pipe.read_fd(), buf, 1);
128         if (r != 1)
129         {
130             if (r == (ssize_t) (-1))
131                 yaz_log(YLOG_WARN|YLOG_ERRNO,
132                         "ThreadPoolSocketObserver::socketNotify. read fail");
133             else
134                 yaz_log(YLOG_WARN,
135                         "ThreadPoolSocketObserver::socketNotify. read returned 0");
136         }
137 #endif
138         IThreadPoolMsg *out;
139         {
140             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
141             out = m_p->m_output.front();
142             m_p->m_output.pop_front();
143         }
144         if (out)
145             out->result();
146     }
147 }
148
149 void ThreadPoolSocketObserver::run(void *p)
150 {
151     while(1)
152     {
153         IThreadPoolMsg *in = 0;
154         {
155             boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
156             while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
157                 m_p->m_cond_input_data.wait(input_lock);
158             if (m_p->m_stop_flag)
159                 break;
160             
161             in = m_p->m_input.front();
162             m_p->m_input.pop_front(); 
163             m_p->m_cond_input_full.notify_all();
164         }
165         IThreadPoolMsg *out = in->handle();
166         {
167             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
168             m_p->m_output.push_back(out);
169 #ifdef WIN32
170             send(m_p->m_pipe.write_fd(), "", 1, 0);
171 #else
172             ssize_t r = write(m_p->m_pipe.write_fd(), "", 1);
173             if (r != 1)
174             {
175                 if (r == (ssize_t) (-1))
176                     yaz_log(YLOG_WARN|YLOG_ERRNO,
177                             "ThreadPoolSocketObserver::run. write fail");
178                 else
179                     yaz_log(YLOG_WARN,
180                             "ThreadPoolSocketObserver::run. write returned 0");
181             }
182 #endif
183         }
184     }
185 }
186
187 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
188 {
189     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
190     while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
191         m_p->m_cond_input_full.wait(input_lock);
192     m_p->m_input.push_back(m);
193     m_p->m_cond_input_data.notify_one();
194 }
195 /*
196  * Local variables:
197  * c-basic-offset: 4
198  * c-file-style: "Stroustrup"
199  * indent-tabs-mode: nil
200  * End:
201  * vim: shiftwidth=4 tabstop=8 expandtab
202  */
203