1 /* $Id: msg-thread.cpp,v 1.4 2005-06-10 22:54:22 adam Exp $
2 Copyright (c) 1998-2005, Index Data.
4 This file is part of the yaz-proxy.
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
26 #include <yaz++/socket-observer.h>
29 #include "msg-thread.h"
31 using namespace yazpp_1;
33 Msg_Thread_Queue::Msg_Thread_Queue()
38 int Msg_Thread_Queue::size()
41 Msg_Thread_Queue_List *l;
42 for (l = m_list; l; l = l->m_next)
47 void Msg_Thread_Queue::enqueue(IMsg_Thread *m)
49 Msg_Thread_Queue_List *l = new Msg_Thread_Queue_List;
55 IMsg_Thread *Msg_Thread_Queue::dequeue()
57 Msg_Thread_Queue_List **l = &m_list;
62 IMsg_Thread *m = (*l)->m_item;
68 static void *tfunc(void *p)
70 Msg_Thread *pt = (Msg_Thread *) p;
76 Msg_Thread::Msg_Thread(ISocketObservable *obs)
77 : m_SocketObservable(obs)
80 obs->addObserver(m_fd[0], this);
81 obs->maskObserver(this, SOCKET_OBSERVE_READ);
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 pthread_cond_init(&m_cond_output_data, 0);
88 pthread_create(&m_thread_id, 0, tfunc, this);
91 Msg_Thread::~Msg_Thread()
93 pthread_mutex_lock(&m_mutex_input_data);
95 pthread_cond_signal(&m_cond_input_data);
96 pthread_mutex_unlock(&m_mutex_input_data);
98 pthread_join(m_thread_id, 0);
100 m_SocketObservable->deleteObserver(this);
102 pthread_cond_destroy(&m_cond_input_data);
103 pthread_mutex_destroy(&m_mutex_input_data);
104 pthread_cond_destroy(&m_cond_output_data);
105 pthread_mutex_destroy(&m_mutex_output_data);
110 void Msg_Thread::socketNotify(int event)
112 if (event & SOCKET_OBSERVE_READ)
115 read(m_fd[0], buf, 1);
116 pthread_mutex_lock(&m_mutex_output_data);
117 IMsg_Thread *out = m_output.dequeue();
118 pthread_mutex_unlock(&m_mutex_output_data);
124 void Msg_Thread::run(void *p)
128 pthread_mutex_lock(&m_mutex_input_data);
129 pthread_cond_wait(&m_cond_input_data, &m_mutex_input_data);
134 pthread_mutex_unlock(&m_mutex_input_data);
137 IMsg_Thread *in = m_input.dequeue();
138 pthread_mutex_unlock(&m_mutex_input_data);
141 IMsg_Thread *out = in->handle();
142 pthread_mutex_lock(&m_mutex_output_data);
143 m_output.enqueue(out);
144 pthread_cond_signal(&m_cond_output_data);
145 pthread_mutex_unlock(&m_mutex_output_data);
147 write(m_fd[1], "", 1);
149 pthread_mutex_lock(&m_mutex_input_data);
154 void Msg_Thread::put(IMsg_Thread *m)
156 pthread_mutex_lock(&m_mutex_input_data);
158 pthread_cond_signal(&m_cond_input_data);
159 pthread_mutex_unlock(&m_mutex_input_data);