Simplified process interface. Private sub class Worker.
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* $Id: thread_pool_observer.cpp,v 1.5 2005-10-14 10:08:40 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
33 IThreadPoolMsg::~IThreadPoolMsg()
34 {
35
36 }
37
38 ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs, int no_threads)
39     : m_SocketObservable(obs)
40 {
41     pipe(m_fd);
42     obs->addObserver(m_fd[0], this);
43     obs->maskObserver(this, SOCKET_OBSERVE_READ);
44
45     m_stop_flag = false;
46     m_no_threads = no_threads;
47     int i;
48     for (i = 0; i<no_threads; i++)
49     {
50         Worker w(this);
51         m_thrds.add_thread(new boost::thread(w));
52     }
53 }
54
55 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
56 {
57     {
58         boost::mutex::scoped_lock input_lock(m_mutex_input_data);
59         m_stop_flag = true;
60         m_cond_input_data.notify_all();
61     }
62     m_thrds.join_all();
63
64     m_SocketObservable->deleteObserver(this);
65
66     close(m_fd[0]);
67     close(m_fd[1]);
68 }
69
70 void ThreadPoolSocketObserver::socketNotify(int event)
71 {
72     if (event & SOCKET_OBSERVE_READ)
73     {
74         char buf[2];
75         read(m_fd[0], buf, 1);
76         IThreadPoolMsg *out;
77         {
78             boost::mutex::scoped_lock output_lock(m_mutex_output_data);
79             out = m_output.front();
80             m_output.pop_front();
81         }
82         if (out)
83             out->result();
84     }
85 }
86
87 void ThreadPoolSocketObserver::run(void *p)
88 {
89     while(1)
90     {
91         IThreadPoolMsg *in = 0;
92         {
93             boost::mutex::scoped_lock input_lock(m_mutex_input_data);
94             while (!m_stop_flag && m_input.size() == 0)
95                 m_cond_input_data.wait(input_lock);
96             if (m_stop_flag)
97                 break;
98             
99             in = m_input.front();
100             m_input.pop_front();
101         }
102         IThreadPoolMsg *out = in->handle();
103         {
104             boost::mutex::scoped_lock output_lock(m_mutex_output_data);
105             m_output.push_back(out);
106             write(m_fd[1], "", 1);
107         }
108     }
109 }
110
111 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
112 {
113     boost::mutex::scoped_lock input_lock(m_mutex_input_data);
114     m_input.push_back(m);
115     m_cond_input_data.notify_one();
116 }
117 /*
118  * Local variables:
119  * c-basic-offset: 4
120  * indent-tabs-mode: nil
121  * End:
122  * vim: shiftwidth=4 tabstop=8 expandtab
123  */
124