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