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