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