1 /* $Id: thread_pool_observer.cpp,v 1.2 2005-10-08 23:29:32 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>
30 #include "thread_pool_observer.h"
32 using namespace yazpp_1;
34 IThreadPoolMsg::~IThreadPoolMsg()
39 static void *tfunc(void *p)
41 ThreadPoolSocketObserver *pt = (ThreadPoolSocketObserver *) p;
47 ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs, int no_threads)
48 : m_SocketObservable(obs)
51 obs->addObserver(m_fd[0], this);
52 obs->maskObserver(this, SOCKET_OBSERVE_READ);
55 pthread_mutex_init(&m_mutex_input_data, 0);
56 pthread_cond_init(&m_cond_input_data, 0);
57 pthread_mutex_init(&m_mutex_output_data, 0);
59 m_no_threads = no_threads;
60 m_thread_id = new pthread_t[no_threads];
62 for (i = 0; i<m_no_threads; i++)
63 pthread_create(&m_thread_id[i], 0, tfunc, this);
66 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
68 pthread_mutex_lock(&m_mutex_input_data);
70 pthread_cond_broadcast(&m_cond_input_data);
71 pthread_mutex_unlock(&m_mutex_input_data);
74 for (i = 0; i<m_no_threads; i++)
75 pthread_join(m_thread_id[i], 0);
76 delete [] m_thread_id;
78 m_SocketObservable->deleteObserver(this);
80 pthread_cond_destroy(&m_cond_input_data);
81 pthread_mutex_destroy(&m_mutex_input_data);
82 pthread_mutex_destroy(&m_mutex_output_data);
87 void ThreadPoolSocketObserver::socketNotify(int event)
89 if (event & SOCKET_OBSERVE_READ)
92 read(m_fd[0], buf, 1);
93 pthread_mutex_lock(&m_mutex_output_data);
94 IThreadPoolMsg *out = m_output.front();
96 pthread_mutex_unlock(&m_mutex_output_data);
102 void ThreadPoolSocketObserver::run(void *p)
106 pthread_mutex_lock(&m_mutex_input_data);
107 while (!m_stop_flag && m_input.size() == 0)
108 pthread_cond_wait(&m_cond_input_data, &m_mutex_input_data);
111 pthread_mutex_unlock(&m_mutex_input_data);
114 IThreadPoolMsg *in = m_input.front();
116 pthread_mutex_unlock(&m_mutex_input_data);
118 IThreadPoolMsg *out = in->handle();
119 pthread_mutex_lock(&m_mutex_output_data);
121 m_output.push_back(out);
123 write(m_fd[1], "", 1);
124 pthread_mutex_unlock(&m_mutex_output_data);
128 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
130 pthread_mutex_lock(&m_mutex_input_data);
131 m_input.push_back(m);
132 pthread_cond_signal(&m_cond_input_data);
133 pthread_mutex_unlock(&m_mutex_input_data);
138 * indent-tabs-mode: nil
140 * vim: shiftwidth=4 tabstop=8 expandtab