Updated copyright headers. Omit CVS ID.
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2008 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 #ifdef WIN32
25 #include <winsock.h>
26 #endif
27
28 #if HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
30 #endif
31
32 #include <boost/thread/thread.hpp>
33 #include <boost/thread/mutex.hpp>
34 #include <boost/thread/condition.hpp>
35
36 #include <ctype.h>
37 #include <stdio.h>
38
39 #include <deque>
40
41 #include <yazpp/socket-observer.h>
42 #include <yaz/log.h>
43
44 #include "thread_pool_observer.hpp"
45 #include "pipe.hpp"
46
47 namespace metaproxy_1 {
48     class ThreadPoolSocketObserver::Worker {
49     public:
50         Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
51         ThreadPoolSocketObserver *m_s;
52         void operator() (void) {
53             m_s->run(0);
54         }
55     };
56
57     class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
58         friend class ThreadPoolSocketObserver;
59     public:
60         Rep(yazpp_1::ISocketObservable *obs);
61         ~Rep();
62     private:
63         yazpp_1::ISocketObservable *m_socketObservable;
64         Pipe m_pipe;
65         boost::thread_group m_thrds;
66         boost::mutex m_mutex_input_data;
67         boost::condition m_cond_input_data;
68         boost::condition m_cond_input_full;
69         boost::mutex m_mutex_output_data;
70         std::deque<IThreadPoolMsg *> m_input;
71         std::deque<IThreadPoolMsg *> m_output;
72         bool m_stop_flag;
73         unsigned m_no_threads;
74     };
75     const unsigned int queue_size_per_thread = 64;
76 }
77
78
79
80 using namespace yazpp_1;
81 using namespace metaproxy_1;
82
83 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
84     : m_socketObservable(obs), m_pipe(9123)
85 {
86 }
87
88 ThreadPoolSocketObserver::Rep::~Rep()
89 {
90 }
91
92 IThreadPoolMsg::~IThreadPoolMsg()
93 {
94
95 }
96
97 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
98     yazpp_1::ISocketObservable *obs, int no_threads)
99     : m_p(new Rep(obs))
100 {
101     obs->addObserver(m_p->m_pipe.read_fd(), this);
102     obs->maskObserver(this, SOCKET_OBSERVE_READ);
103
104     m_p->m_stop_flag = false;
105     m_p->m_no_threads = no_threads;
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         read(m_p->m_pipe.read_fd(), buf, 1);
135 #endif
136         IThreadPoolMsg *out;
137         {
138             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
139             out = m_p->m_output.front();
140             m_p->m_output.pop_front();
141         }
142         if (out)
143             out->result();
144     }
145 }
146
147 void ThreadPoolSocketObserver::run(void *p)
148 {
149     while(1)
150     {
151         IThreadPoolMsg *in = 0;
152         {
153             boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
154             while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
155                 m_p->m_cond_input_data.wait(input_lock);
156             if (m_p->m_stop_flag)
157                 break;
158             
159             in = m_p->m_input.front();
160             m_p->m_input.pop_front(); 
161             m_p->m_cond_input_full.notify_all();
162         }
163         IThreadPoolMsg *out = in->handle();
164         {
165             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
166             m_p->m_output.push_back(out);
167 #ifdef WIN32
168             send(m_p->m_pipe.write_fd(), "", 1, 0);
169 #else
170             write(m_p->m_pipe.write_fd(), "", 1);
171 #endif
172         }
173     }
174 }
175
176 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
177 {
178     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
179     while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
180         m_p->m_cond_input_full.wait(input_lock);
181     m_p->m_input.push_back(m);
182     m_p->m_cond_input_data.notify_one();
183 }
184 /*
185  * Local variables:
186  * c-basic-offset: 4
187  * indent-tabs-mode: nil
188  * c-file-style: "stroustrup"
189  * End:
190  * vim: shiftwidth=4 tabstop=8 expandtab
191  */
192