Include config.hpp in all .cpp files
[metaproxy-moved-to-github.git] / src / thread_pool_observer.cpp
1 /* $Id: thread_pool_observer.cpp,v 1.2 2005-10-08 23:29:32 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 <pthread.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <stdio.h>
25
26 #include <yaz++/socket-observer.h>
27 #include <yaz/log.h>
28
29 #include "config.hpp"
30 #include "thread_pool_observer.h"
31
32 using namespace yazpp_1;
33
34 IThreadPoolMsg::~IThreadPoolMsg()
35 {
36
37 }
38
39 static void *tfunc(void *p)
40 {
41     ThreadPoolSocketObserver *pt = (ThreadPoolSocketObserver *) p;
42     pt->run(0);
43     return 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     pthread_mutex_init(&m_mutex_input_data, 0);
56     pthread_cond_init(&m_cond_input_data, 0);
57     pthread_mutex_init(&m_mutex_output_data, 0);
58
59     m_no_threads = no_threads;
60     m_thread_id = new pthread_t[no_threads];
61     int i;
62     for (i = 0; i<m_no_threads; i++)
63         pthread_create(&m_thread_id[i], 0, tfunc, this);
64 }
65
66 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
67 {
68     pthread_mutex_lock(&m_mutex_input_data);
69     m_stop_flag = true;
70     pthread_cond_broadcast(&m_cond_input_data);
71     pthread_mutex_unlock(&m_mutex_input_data);
72     
73     int i;
74     for (i = 0; i<m_no_threads; i++)
75         pthread_join(m_thread_id[i], 0);
76     delete [] m_thread_id;
77
78     m_SocketObservable->deleteObserver(this);
79
80     pthread_cond_destroy(&m_cond_input_data);
81     pthread_mutex_destroy(&m_mutex_input_data);
82     pthread_mutex_destroy(&m_mutex_output_data);
83     close(m_fd[0]);
84     close(m_fd[1]);
85 }
86
87 void ThreadPoolSocketObserver::socketNotify(int event)
88 {
89     if (event & SOCKET_OBSERVE_READ)
90     {
91         char buf[2];
92         read(m_fd[0], buf, 1);
93         pthread_mutex_lock(&m_mutex_output_data);
94         IThreadPoolMsg *out = m_output.front();
95         m_output.pop_front();
96         pthread_mutex_unlock(&m_mutex_output_data);
97         if (out)
98             out->result();
99     }
100 }
101
102 void ThreadPoolSocketObserver::run(void *p)
103 {
104     while(1)
105     {
106         pthread_mutex_lock(&m_mutex_input_data);
107         while (!m_stop_flag && m_input.size() == 0)
108             pthread_cond_wait(&m_cond_input_data, &m_mutex_input_data);
109         if (m_stop_flag)
110         {
111             pthread_mutex_unlock(&m_mutex_input_data);
112             break;
113         }
114         IThreadPoolMsg *in = m_input.front();
115         m_input.pop_front();
116         pthread_mutex_unlock(&m_mutex_input_data);
117
118         IThreadPoolMsg *out = in->handle();
119         pthread_mutex_lock(&m_mutex_output_data);
120
121         m_output.push_back(out);
122         
123         write(m_fd[1], "", 1);
124         pthread_mutex_unlock(&m_mutex_output_data);
125     }
126 }
127
128 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
129 {
130     pthread_mutex_lock(&m_mutex_input_data);
131     m_input.push_back(m);
132     pthread_cond_signal(&m_cond_input_data);
133     pthread_mutex_unlock(&m_mutex_input_data);
134 }
135 /*
136  * Local variables:
137  * c-basic-offset: 4
138  * indent-tabs-mode: nil
139  * End:
140  * vim: shiftwidth=4 tabstop=8 expandtab
141  */
142