Bump year in copyright msg in source
[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         read(m_p->m_pipe.read_fd(), buf, 1);
128 #endif
129         IThreadPoolMsg *out;
130         {
131             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
132             out = m_p->m_output.front();
133             m_p->m_output.pop_front();
134         }
135         if (out)
136             out->result();
137     }
138 }
139
140 void ThreadPoolSocketObserver::run(void *p)
141 {
142     while(1)
143     {
144         IThreadPoolMsg *in = 0;
145         {
146             boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
147             while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
148                 m_p->m_cond_input_data.wait(input_lock);
149             if (m_p->m_stop_flag)
150                 break;
151             
152             in = m_p->m_input.front();
153             m_p->m_input.pop_front(); 
154             m_p->m_cond_input_full.notify_all();
155         }
156         IThreadPoolMsg *out = in->handle();
157         {
158             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
159             m_p->m_output.push_back(out);
160 #ifdef WIN32
161             send(m_p->m_pipe.write_fd(), "", 1, 0);
162 #else
163             write(m_p->m_pipe.write_fd(), "", 1);
164 #endif
165         }
166     }
167 }
168
169 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
170 {
171     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
172     while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
173         m_p->m_cond_input_full.wait(input_lock);
174     m_p->m_input.push_back(m);
175     m_p->m_cond_input_data.notify_one();
176 }
177 /*
178  * Local variables:
179  * c-basic-offset: 4
180  * c-file-style: "Stroustrup"
181  * indent-tabs-mode: nil
182  * End:
183  * vim: shiftwidth=4 tabstop=8 expandtab
184  */
185