Move includes to implementation file
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1
2 /* $Id: thread_pool_observer.cpp,v 1.10 2005-11-07 12:31:05 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
30 namespace yp2 {
31     class ThreadPoolSocketObserver::Worker {
32     public:
33         Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
34         ThreadPoolSocketObserver *m_s;
35         void operator() (void) {
36             m_s->run(0);
37         }
38     };
39
40     class ThreadPoolSocketObserver::Rep : public boost::noncopyable {
41         friend class ThreadPoolSocketObserver;
42     public:
43         Rep(yazpp_1::ISocketObservable *obs);
44         ~Rep();
45     private:
46         yazpp_1::ISocketObservable *m_socketObservable;
47         int m_fd[2];
48         boost::thread_group m_thrds;
49         boost::mutex m_mutex_input_data;
50         boost::condition m_cond_input_data;
51         boost::mutex m_mutex_output_data;
52         std::deque<IThreadPoolMsg *> m_input;
53         std::deque<IThreadPoolMsg *> m_output;
54         bool m_stop_flag;
55         int m_no_threads;
56     };
57 }
58
59
60 using namespace yazpp_1;
61 using namespace yp2;
62
63 ThreadPoolSocketObserver::Rep::Rep(ISocketObservable *obs)
64     : m_socketObservable(obs)
65 {
66 }
67
68 ThreadPoolSocketObserver::Rep::~Rep()
69 {
70 }
71
72 IThreadPoolMsg::~IThreadPoolMsg()
73 {
74
75 }
76
77 ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs,
78                                                    int no_threads)
79     : m_p(new Rep(obs))
80 {
81     pipe(m_p->m_fd);
82     obs->addObserver(m_p->m_fd[0], 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     close(m_p->m_fd[0]);
107     close(m_p->m_fd[1]);
108 }
109
110 void ThreadPoolSocketObserver::socketNotify(int event)
111 {
112     if (event & SOCKET_OBSERVE_READ)
113     {
114         char buf[2];
115         read(m_p->m_fd[0], buf, 1);
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             write(m_p->m_fd[1], "", 1);
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