Fixed bug #1064: Test test_thread_pool_observer hangs.
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* $Id: thread_pool_observer.cpp,v 1.20 2007-04-18 12:06:59 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4    See the LICENSE file for details
5  */
6 #include "config.hpp"
7
8 #if HAVE_UNISTD_H
9 #include <unistd.h>
10 #endif
11 #ifdef WIN32
12 #include <winsock.h>
13 #endif
14
15 #if HAVE_SYS_SOCKET_H
16 #include <sys/socket.h>
17 #endif
18
19 #include <boost/thread/thread.hpp>
20 #include <boost/thread/mutex.hpp>
21 #include <boost/thread/condition.hpp>
22
23 #include <ctype.h>
24 #include <stdio.h>
25
26 #include <deque>
27
28 #include <yazpp/socket-observer.h>
29 #include <yaz/log.h>
30
31 #include "thread_pool_observer.hpp"
32 #include "pipe.hpp"
33
34 namespace metaproxy_1 {
35     class ThreadPoolSocketObserver::Worker {
36     public:
37         Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
38         ThreadPoolSocketObserver *m_s;
39         void operator() (void) {
40             m_s->run(0);
41         }
42     };
43
44     class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
45         friend class ThreadPoolSocketObserver;
46     public:
47         Rep(yazpp_1::ISocketObservable *obs);
48         ~Rep();
49     private:
50         yazpp_1::ISocketObservable *m_socketObservable;
51         Pipe m_pipe;
52         boost::thread_group m_thrds;
53         boost::mutex m_mutex_input_data;
54         boost::condition m_cond_input_data;
55         boost::condition m_cond_input_full;
56         boost::mutex m_mutex_output_data;
57         std::deque<IThreadPoolMsg *> m_input;
58         std::deque<IThreadPoolMsg *> m_output;
59         bool m_stop_flag;
60         unsigned m_no_threads;
61     };
62     const unsigned int queue_size_per_thread = 64;
63 }
64
65
66
67 using namespace yazpp_1;
68 using namespace metaproxy_1;
69
70 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
71     : m_socketObservable(obs), m_pipe(9123)
72 {
73 }
74
75 ThreadPoolSocketObserver::Rep::~Rep()
76 {
77 }
78
79 IThreadPoolMsg::~IThreadPoolMsg()
80 {
81
82 }
83
84 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
85     yazpp_1::ISocketObservable *obs, int no_threads)
86     : m_p(new Rep(obs))
87 {
88     obs->addObserver(m_p->m_pipe.read_fd(), this);
89     obs->maskObserver(this, SOCKET_OBSERVE_READ);
90
91     m_p->m_stop_flag = false;
92     m_p->m_no_threads = no_threads;
93     int i;
94     for (i = 0; i<no_threads; i++)
95     {
96         Worker w(this);
97         m_p->m_thrds.add_thread(new boost::thread(w));
98     }
99 }
100
101 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
102 {
103     {
104         boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
105         m_p->m_stop_flag = true;
106         m_p->m_cond_input_data.notify_all();
107     }
108     m_p->m_thrds.join_all();
109
110     m_p->m_socketObservable->deleteObserver(this);
111 }
112
113 void ThreadPoolSocketObserver::socketNotify(int event)
114 {
115     if (event & SOCKET_OBSERVE_READ)
116     {
117         char buf[2];
118 #ifdef WIN32
119         recv(m_p->m_pipe.read_fd(), buf, 1, 0);
120 #else
121         read(m_p->m_pipe.read_fd(), buf, 1);
122 #endif
123         IThreadPoolMsg *out;
124         {
125             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
126             out = m_p->m_output.front();
127             m_p->m_output.pop_front();
128         }
129         if (out)
130             out->result();
131     }
132 }
133
134 void ThreadPoolSocketObserver::run(void *p)
135 {
136     while(1)
137     {
138         IThreadPoolMsg *in = 0;
139         {
140             boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
141             while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
142                 m_p->m_cond_input_data.wait(input_lock);
143             if (m_p->m_stop_flag)
144                 break;
145             
146             in = m_p->m_input.front();
147             m_p->m_input.pop_front(); 
148             m_p->m_cond_input_full.notify_all();
149         }
150         IThreadPoolMsg *out = in->handle();
151         {
152             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
153             m_p->m_output.push_back(out);
154 #ifdef WIN32
155             send(m_p->m_pipe.write_fd(), "", 1, 0);
156 #else
157             write(m_p->m_pipe.write_fd(), "", 1);
158 #endif
159         }
160     }
161 }
162
163 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
164 {
165     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
166     while (m_p->m_input.size() >= m_p->m_no_threads * queue_size_per_thread)
167         m_p->m_cond_input_full.wait(input_lock);
168     m_p->m_input.push_back(m);
169     m_p->m_cond_input_data.notify_one();
170 }
171 /*
172  * Local variables:
173  * c-basic-offset: 4
174  * indent-tabs-mode: nil
175  * c-file-style: "stroustrup"
176  * End:
177  * vim: shiftwidth=4 tabstop=8 expandtab
178  */
179