Moving ThreadPoolSocketObserver and IThreadPoolMsg to yp2 namespace
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* $Id: thread_pool_observer.cpp,v 1.6 2005-10-14 10:27:18 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21 #include <unistd.h>
22 #include <ctype.h>
23 #include <stdio.h>
24
25 #include <yaz++/socket-observer.h>
26 #include <yaz/log.h>
27
28 #include "config.hpp"
29 #include "thread_pool_observer.hpp"
30
31 using namespace yazpp_1;
32 using namespace yp2;
33
34 IThreadPoolMsg::~IThreadPoolMsg()
35 {
36
37 }
38
39 ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs, int no_threads)
40     : m_SocketObservable(obs)
41 {
42     pipe(m_fd);
43     obs->addObserver(m_fd[0], this);
44     obs->maskObserver(this, SOCKET_OBSERVE_READ);
45
46     m_stop_flag = false;
47     m_no_threads = no_threads;
48     int i;
49     for (i = 0; i<no_threads; i++)
50     {
51         Worker w(this);
52         m_thrds.add_thread(new boost::thread(w));
53     }
54 }
55
56 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
57 {
58     {
59         boost::mutex::scoped_lock input_lock(m_mutex_input_data);
60         m_stop_flag = true;
61         m_cond_input_data.notify_all();
62     }
63     m_thrds.join_all();
64
65     m_SocketObservable->deleteObserver(this);
66
67     close(m_fd[0]);
68     close(m_fd[1]);
69 }
70
71 void ThreadPoolSocketObserver::socketNotify(int event)
72 {
73     if (event & SOCKET_OBSERVE_READ)
74     {
75         char buf[2];
76         read(m_fd[0], buf, 1);
77         IThreadPoolMsg *out;
78         {
79             boost::mutex::scoped_lock output_lock(m_mutex_output_data);
80             out = m_output.front();
81             m_output.pop_front();
82         }
83         if (out)
84             out->result();
85     }
86 }
87
88 void ThreadPoolSocketObserver::run(void *p)
89 {
90     while(1)
91     {
92         IThreadPoolMsg *in = 0;
93         {
94             boost::mutex::scoped_lock input_lock(m_mutex_input_data);
95             while (!m_stop_flag && m_input.size() == 0)
96                 m_cond_input_data.wait(input_lock);
97             if (m_stop_flag)
98                 break;
99             
100             in = m_input.front();
101             m_input.pop_front();
102         }
103         IThreadPoolMsg *out = in->handle();
104         {
105             boost::mutex::scoped_lock output_lock(m_mutex_output_data);
106             m_output.push_back(out);
107             write(m_fd[1], "", 1);
108         }
109     }
110 }
111
112 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
113 {
114     boost::mutex::scoped_lock input_lock(m_mutex_input_data);
115     m_input.push_back(m);
116     m_cond_input_data.notify_one();
117 }
118 /*
119  * Local variables:
120  * c-basic-offset: 4
121  * indent-tabs-mode: nil
122  * End:
123  * vim: shiftwidth=4 tabstop=8 expandtab
124  */
125