Msg_Thread constructor now has no_threads which is the number of
[yazproxy-moved-to-github.git] / src / msg-thread.cpp
1 /* $Id: msg-thread.cpp,v 1.8 2005-09-12 20:09:14 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 "msg-thread.h"
30
31 using namespace yazpp_1;
32
33 Msg_Thread_Queue::Msg_Thread_Queue()
34 {
35     m_list = 0;
36 }
37
38 int Msg_Thread_Queue::size()
39 {
40     int no = 0;
41     Msg_Thread_Queue_List *l;
42     for (l = m_list; l; l = l->m_next)
43         no++;
44     return no;
45 }
46
47 void Msg_Thread_Queue::enqueue(IMsg_Thread *m)
48 {
49     Msg_Thread_Queue_List *l = new Msg_Thread_Queue_List;
50     l->m_next = m_list;
51     l->m_item = m;
52     m_list = l;
53 }
54
55 IMsg_Thread *Msg_Thread_Queue::dequeue()
56 {
57     Msg_Thread_Queue_List **l = &m_list;
58     if (!*l)
59         return 0;
60     while ((*l)->m_next)
61         l = &(*l)->m_next;
62     IMsg_Thread *m = (*l)->m_item;
63     delete *l;
64     *l = 0;
65     return m;
66 }
67
68 static void *tfunc(void *p)
69 {
70     Msg_Thread *pt = (Msg_Thread *) p;
71     pt->run(0);
72     return 0;
73 }
74
75
76 Msg_Thread::Msg_Thread(ISocketObservable *obs, int no_threads)
77     : m_SocketObservable(obs)
78 {
79     pipe(m_fd);
80     obs->addObserver(m_fd[0], this);
81     obs->maskObserver(this, SOCKET_OBSERVE_READ);
82
83     m_stop_flag = false;
84     pthread_mutex_init(&m_mutex_input_data, 0);
85     pthread_cond_init(&m_cond_input_data, 0);
86     pthread_mutex_init(&m_mutex_output_data, 0);
87
88     m_no_threads = no_threads;
89     m_thread_id = new pthread_t[no_threads];
90     int i;
91     for (i = 0; i<m_no_threads; i++)
92         pthread_create(&m_thread_id[i], 0, tfunc, this);
93 }
94
95 Msg_Thread::~Msg_Thread()
96 {
97     pthread_mutex_lock(&m_mutex_input_data);
98     m_stop_flag = true;
99     pthread_cond_signal(&m_cond_input_data);
100     pthread_mutex_unlock(&m_mutex_input_data);
101     
102     int i;
103     for (i = 0; i<m_no_threads; i++)
104         pthread_join(m_thread_id[i], 0);
105     delete [] m_thread_id;
106
107     m_SocketObservable->deleteObserver(this);
108
109     pthread_cond_destroy(&m_cond_input_data);
110     pthread_mutex_destroy(&m_mutex_input_data);
111     pthread_mutex_destroy(&m_mutex_output_data);
112     close(m_fd[0]);
113     close(m_fd[1]);
114 }
115
116 void Msg_Thread::socketNotify(int event)
117 {
118     if (event & SOCKET_OBSERVE_READ)
119     {
120         char buf[2];
121         read(m_fd[0], buf, 1);
122         pthread_mutex_lock(&m_mutex_output_data);
123         IMsg_Thread *out = m_output.dequeue();
124         pthread_mutex_unlock(&m_mutex_output_data);
125         if (out)
126             out->result();
127     }
128 }
129
130 void Msg_Thread::run(void *p)
131 {
132     while(1)
133     {
134         pthread_mutex_lock(&m_mutex_input_data);
135         while (!m_stop_flag && m_input.size() == 0)
136             pthread_cond_wait(&m_cond_input_data, &m_mutex_input_data);
137         if (m_stop_flag)
138         {
139             pthread_mutex_unlock(&m_mutex_input_data);
140             break;
141         }
142         IMsg_Thread *in = m_input.dequeue();
143         pthread_mutex_unlock(&m_mutex_input_data);
144
145         IMsg_Thread *out = in->handle();
146         pthread_mutex_lock(&m_mutex_output_data);
147         m_output.enqueue(out);
148         
149         write(m_fd[1], "", 1);
150         pthread_mutex_unlock(&m_mutex_output_data);
151     }
152 }
153
154 void Msg_Thread::put(IMsg_Thread *m)
155 {
156     pthread_mutex_lock(&m_mutex_input_data);
157     m_input.enqueue(m);
158     pthread_cond_signal(&m_cond_input_data);
159     pthread_mutex_unlock(&m_mutex_input_data);
160 }
161 /*
162  * Local variables:
163  * c-basic-offset: 4
164  * indent-tabs-mode: nil
165  * End:
166  * vim: shiftwidth=4 tabstop=8 expandtab
167  */
168