1 /* $Id: msg-thread.cpp,v 1.14 2006-03-30 14:16:34 adam Exp $
2 Copyright (c) 1998-2006, Index Data.
4 This file is part of the yazproxy.
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
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
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
33 #include <yazpp/socket-observer.h>
36 #include "msg-thread.h"
38 using namespace yazpp_1;
40 class Msg_Thread::Private {
43 Msg_Thread_Queue m_input;
44 Msg_Thread_Queue m_output;
47 yazpp_1::ISocketObservable *m_SocketObservable;
48 pthread_t *m_thread_id;
49 pthread_mutex_t m_mutex_input_data;
50 pthread_cond_t m_cond_input_data;
51 pthread_mutex_t m_mutex_output_data;
56 IMsg_Thread::~IMsg_Thread()
61 Msg_Thread_Queue::Msg_Thread_Queue()
66 int Msg_Thread_Queue::size()
69 Msg_Thread_Queue_List *l;
70 for (l = m_list; l; l = l->m_next)
75 void Msg_Thread_Queue::enqueue(IMsg_Thread *m)
77 Msg_Thread_Queue_List *l = new Msg_Thread_Queue_List;
83 IMsg_Thread *Msg_Thread_Queue::dequeue()
85 Msg_Thread_Queue_List **l = &m_list;
90 IMsg_Thread *m = (*l)->m_item;
97 static void *tfunc(void *p)
99 Msg_Thread *pt = (Msg_Thread *) p;
105 Msg_Thread::Msg_Thread(ISocketObservable *obs, int no_threads)
109 #if YAZ_POSIX_THREADS
110 m_p->m_SocketObservable = obs;
113 obs->addObserver(m_p->m_fd[0], this);
114 obs->maskObserver(this, SOCKET_OBSERVE_READ);
116 m_p->m_stop_flag = false;
117 pthread_mutex_init(&m_p->m_mutex_input_data, 0);
118 pthread_cond_init(&m_p->m_cond_input_data, 0);
119 pthread_mutex_init(&m_p->m_mutex_output_data, 0);
121 m_p->m_no_threads = no_threads;
122 m_p->m_thread_id = new pthread_t[no_threads];
124 for (i = 0; i<m_p->m_no_threads; i++)
125 pthread_create(&m_p->m_thread_id[i], 0, tfunc, this);
129 Msg_Thread::~Msg_Thread()
131 #if YAZ_POSIX_THREADS
132 pthread_mutex_lock(&m_p->m_mutex_input_data);
133 m_p->m_stop_flag = true;
134 pthread_cond_broadcast(&m_p->m_cond_input_data);
135 pthread_mutex_unlock(&m_p->m_mutex_input_data);
138 for (i = 0; i<m_p->m_no_threads; i++)
139 pthread_join(m_p->m_thread_id[i], 0);
140 delete [] m_p->m_thread_id;
142 m_p->m_SocketObservable->deleteObserver(this);
144 pthread_cond_destroy(&m_p->m_cond_input_data);
145 pthread_mutex_destroy(&m_p->m_mutex_input_data);
146 pthread_mutex_destroy(&m_p->m_mutex_output_data);
154 void Msg_Thread::socketNotify(int event)
156 #if YAZ_POSIX_THREADS
157 if (event & SOCKET_OBSERVE_READ)
160 read(m_p->m_fd[0], buf, 1);
161 pthread_mutex_lock(&m_p->m_mutex_output_data);
162 IMsg_Thread *out = m_p->m_output.dequeue();
163 pthread_mutex_unlock(&m_p->m_mutex_output_data);
170 #if YAZ_POSIX_THREADS
171 void Msg_Thread::run(void *p)
175 pthread_mutex_lock(&m_p->m_mutex_input_data);
176 while (!m_p->m_stop_flag && m_p->m_input.size() == 0)
177 pthread_cond_wait(&m_p->m_cond_input_data, &m_p->m_mutex_input_data);
178 if (m_p->m_stop_flag)
180 pthread_mutex_unlock(&m_p->m_mutex_input_data);
183 IMsg_Thread *in = m_p->m_input.dequeue();
184 pthread_mutex_unlock(&m_p->m_mutex_input_data);
186 IMsg_Thread *out = in->handle();
187 pthread_mutex_lock(&m_p->m_mutex_output_data);
188 m_p->m_output.enqueue(out);
190 write(m_p->m_fd[1], "", 1);
191 pthread_mutex_unlock(&m_p->m_mutex_output_data);
196 void Msg_Thread::put(IMsg_Thread *m)
198 #if YAZ_POSIX_THREADS
199 pthread_mutex_lock(&m_p->m_mutex_input_data);
200 m_p->m_input.enqueue(m);
201 pthread_cond_signal(&m_p->m_cond_input_data);
202 pthread_mutex_unlock(&m_p->m_mutex_input_data);
204 IMsg_Thread *out = m->handle();
212 * indent-tabs-mode: nil
214 * vim: shiftwidth=4 tabstop=8 expandtab