Year 2007.
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* $Id: thread_pool_observer.cpp,v 1.18 2007-01-25 14:05:54 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::mutex m_mutex_output_data;
56         std::deque<IThreadPoolMsg *> m_input;
57         std::deque<IThreadPoolMsg *> m_output;
58         bool m_stop_flag;
59         int m_no_threads;
60     };
61 }
62
63
64 using namespace yazpp_1;
65 using namespace metaproxy_1;
66
67 ThreadPoolSocketObserver::Rep::Rep(yazpp_1::ISocketObservable *obs)
68     : m_socketObservable(obs), m_pipe(9123)
69 {
70 }
71
72 ThreadPoolSocketObserver::Rep::~Rep()
73 {
74 }
75
76 IThreadPoolMsg::~IThreadPoolMsg()
77 {
78
79 }
80
81 ThreadPoolSocketObserver::ThreadPoolSocketObserver(
82     yazpp_1::ISocketObservable *obs, int no_threads)
83     : m_p(new Rep(obs))
84 {
85     obs->addObserver(m_p->m_pipe.read_fd(), this);
86     obs->maskObserver(this, SOCKET_OBSERVE_READ);
87
88     m_p->m_stop_flag = false;
89     m_p->m_no_threads = no_threads;
90     int i;
91     for (i = 0; i<no_threads; i++)
92     {
93         Worker w(this);
94         m_p->m_thrds.add_thread(new boost::thread(w));
95     }
96 }
97
98 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
99 {
100     {
101         boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
102         m_p->m_stop_flag = true;
103         m_p->m_cond_input_data.notify_all();
104     }
105     m_p->m_thrds.join_all();
106
107     m_p->m_socketObservable->deleteObserver(this);
108 }
109
110 void ThreadPoolSocketObserver::socketNotify(int event)
111 {
112     if (event & SOCKET_OBSERVE_READ)
113     {
114         char buf[2];
115         recv(m_p->m_pipe.read_fd(), buf, 1, 0);
116         IThreadPoolMsg *out;
117         {
118             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
119             out = m_p->m_output.front();
120             m_p->m_output.pop_front();
121         }
122         if (out)
123             out->result();
124     }
125 }
126
127 void ThreadPoolSocketObserver::run(void *p)
128 {
129     while(1)
130     {
131         IThreadPoolMsg *in = 0;
132         {
133             boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
134             while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
135                 m_p->m_cond_input_data.wait(input_lock);
136             if (m_p->m_stop_flag)
137                 break;
138             
139             in = m_p->m_input.front();
140             m_p->m_input.pop_front();
141         }
142         IThreadPoolMsg *out = in->handle();
143         {
144             boost::mutex::scoped_lock output_lock(m_p->m_mutex_output_data);
145             m_p->m_output.push_back(out);
146             send(m_p->m_pipe.write_fd(), "", 1, 0);
147         }
148     }
149 }
150
151 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
152 {
153     boost::mutex::scoped_lock input_lock(m_p->m_mutex_input_data);
154     m_p->m_input.push_back(m);
155     m_p->m_cond_input_data.notify_one();
156 }
157 /*
158  * Local variables:
159  * c-basic-offset: 4
160  * indent-tabs-mode: nil
161  * c-file-style: "stroustrup"
162  * End:
163  * vim: shiftwidth=4 tabstop=8 expandtab
164  */
165