Moving ThreadPoolSocketObserver and IThreadPoolMsg to yp2 namespace
[metaproxy-moved-to-github.git] / src / thread_pool_observer.hpp
1 /* $Id: thread_pool_observer.hpp,v 1.3 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
22 #ifndef YP2_THREAD_POOL_OBSERVER_HPP
23 #define YP2_THREAD_POOL_OBSERVER_HPP
24
25 #include <boost/thread/thread.hpp>
26 #include <boost/thread/mutex.hpp>
27 #include <boost/thread/condition.hpp>
28
29 #include <unistd.h>
30 #include <ctype.h>
31
32 #include <deque>
33 #include <yaz++/socket-observer.h>
34 #include <yaz/yconfig.h>
35
36 namespace yp2 {
37     class IThreadPoolMsg {
38     public:
39         virtual IThreadPoolMsg *handle() = 0;
40         virtual void result() = 0;
41         virtual ~IThreadPoolMsg();
42     };
43
44     class ThreadPoolSocketObserver : public yazpp_1::ISocketObserver {
45     private:
46         class Worker {
47         public:
48             Worker(ThreadPoolSocketObserver *s) : m_s(s) {};
49             ThreadPoolSocketObserver *m_s;
50             void operator() (void) {
51                 m_s->run(0);
52             }
53         };
54     public:
55         ThreadPoolSocketObserver(yazpp_1::ISocketObservable *obs,
56                                  int no_threads);
57         virtual ~ThreadPoolSocketObserver();
58         void socketNotify(int event);
59         void put(IThreadPoolMsg *m);
60         IThreadPoolMsg *get();
61         void run(void *p);
62         int m_fd[2];
63     private:
64         yazpp_1::ISocketObservable *m_SocketObservable;
65         int m_no_threads;
66         boost::thread_group m_thrds;
67
68         std::deque<IThreadPoolMsg *> m_input;
69         std::deque<IThreadPoolMsg *> m_output;
70
71         boost::mutex m_mutex_input_data;
72         boost::condition m_cond_input_data;
73         boost::mutex m_mutex_output_data;
74         bool m_stop_flag;
75
76     
77     };
78 }
79 #endif
80 /*
81  * Local variables:
82  * c-basic-offset: 4
83  * indent-tabs-mode: nil
84  * End:
85  * vim: shiftwidth=4 tabstop=8 expandtab
86  */
87