1 /* $Id: msg-thread.cpp,v 1.12 2006-03-30 10:31:17 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
26 #include <yazpp/socket-observer.h>
29 #include "msg-thread.h"
31 using namespace yazpp_1;
33 IMsg_Thread::~IMsg_Thread()
38 Msg_Thread_Queue::Msg_Thread_Queue()
43 int Msg_Thread_Queue::size()
46 Msg_Thread_Queue_List *l;
47 for (l = m_list; l; l = l->m_next)
52 void Msg_Thread_Queue::enqueue(IMsg_Thread *m)
54 Msg_Thread_Queue_List *l = new Msg_Thread_Queue_List;
60 IMsg_Thread *Msg_Thread_Queue::dequeue()
62 Msg_Thread_Queue_List **l = &m_list;
67 IMsg_Thread *m = (*l)->m_item;
73 static void *tfunc(void *p)
75 Msg_Thread *pt = (Msg_Thread *) p;
81 Msg_Thread::Msg_Thread(ISocketObservable *obs, int no_threads)
82 : m_SocketObservable(obs)
85 obs->addObserver(m_fd[0], this);
86 obs->maskObserver(this, SOCKET_OBSERVE_READ);
89 pthread_mutex_init(&m_mutex_input_data, 0);
90 pthread_cond_init(&m_cond_input_data, 0);
91 pthread_mutex_init(&m_mutex_output_data, 0);
93 m_no_threads = no_threads;
94 m_thread_id = new pthread_t[no_threads];
96 for (i = 0; i<m_no_threads; i++)
97 pthread_create(&m_thread_id[i], 0, tfunc, this);
100 Msg_Thread::~Msg_Thread()
102 pthread_mutex_lock(&m_mutex_input_data);
104 pthread_cond_broadcast(&m_cond_input_data);
105 pthread_mutex_unlock(&m_mutex_input_data);
108 for (i = 0; i<m_no_threads; i++)
109 pthread_join(m_thread_id[i], 0);
110 delete [] m_thread_id;
112 m_SocketObservable->deleteObserver(this);
114 pthread_cond_destroy(&m_cond_input_data);
115 pthread_mutex_destroy(&m_mutex_input_data);
116 pthread_mutex_destroy(&m_mutex_output_data);
121 void Msg_Thread::socketNotify(int event)
123 if (event & SOCKET_OBSERVE_READ)
126 read(m_fd[0], buf, 1);
127 pthread_mutex_lock(&m_mutex_output_data);
128 IMsg_Thread *out = m_output.dequeue();
129 pthread_mutex_unlock(&m_mutex_output_data);
135 void Msg_Thread::run(void *p)
139 pthread_mutex_lock(&m_mutex_input_data);
140 while (!m_stop_flag && m_input.size() == 0)
141 pthread_cond_wait(&m_cond_input_data, &m_mutex_input_data);
144 pthread_mutex_unlock(&m_mutex_input_data);
147 IMsg_Thread *in = m_input.dequeue();
148 pthread_mutex_unlock(&m_mutex_input_data);
150 IMsg_Thread *out = in->handle();
151 pthread_mutex_lock(&m_mutex_output_data);
152 m_output.enqueue(out);
154 write(m_fd[1], "", 1);
155 pthread_mutex_unlock(&m_mutex_output_data);
159 void Msg_Thread::put(IMsg_Thread *m)
161 pthread_mutex_lock(&m_mutex_input_data);
163 pthread_cond_signal(&m_cond_input_data);
164 pthread_mutex_unlock(&m_mutex_input_data);
169 * indent-tabs-mode: nil
171 * vim: shiftwidth=4 tabstop=8 expandtab