1 /* $Id: thread_pool_observer.cpp,v 1.1 2005-10-06 19:33:58 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 "thread_pool_observer.h"
31 using namespace yazpp_1;
33 IThreadPoolMsg::~IThreadPoolMsg()
38 static void *tfunc(void *p)
40 ThreadPoolSocketObserver *pt = (ThreadPoolSocketObserver *) p;
46 ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs, int no_threads)
47 : m_SocketObservable(obs)
50 obs->addObserver(m_fd[0], this);
51 obs->maskObserver(this, SOCKET_OBSERVE_READ);
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);
58 m_no_threads = no_threads;
59 m_thread_id = new pthread_t[no_threads];
61 for (i = 0; i<m_no_threads; i++)
62 pthread_create(&m_thread_id[i], 0, tfunc, this);
65 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
67 pthread_mutex_lock(&m_mutex_input_data);
69 pthread_cond_broadcast(&m_cond_input_data);
70 pthread_mutex_unlock(&m_mutex_input_data);
73 for (i = 0; i<m_no_threads; i++)
74 pthread_join(m_thread_id[i], 0);
75 delete [] m_thread_id;
77 m_SocketObservable->deleteObserver(this);
79 pthread_cond_destroy(&m_cond_input_data);
80 pthread_mutex_destroy(&m_mutex_input_data);
81 pthread_mutex_destroy(&m_mutex_output_data);
86 void ThreadPoolSocketObserver::socketNotify(int event)
88 if (event & SOCKET_OBSERVE_READ)
91 read(m_fd[0], buf, 1);
92 pthread_mutex_lock(&m_mutex_output_data);
93 IThreadPoolMsg *out = m_output.front();
95 pthread_mutex_unlock(&m_mutex_output_data);
101 void ThreadPoolSocketObserver::run(void *p)
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);
110 pthread_mutex_unlock(&m_mutex_input_data);
113 IThreadPoolMsg *in = m_input.front();
115 pthread_mutex_unlock(&m_mutex_input_data);
117 IThreadPoolMsg *out = in->handle();
118 pthread_mutex_lock(&m_mutex_output_data);
120 m_output.push_back(out);
122 write(m_fd[1], "", 1);
123 pthread_mutex_unlock(&m_mutex_output_data);
127 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
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);
137 * indent-tabs-mode: nil
139 * vim: shiftwidth=4 tabstop=8 expandtab