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