1 /* $Id: thread_pool_observer.cpp,v 1.5 2005-10-14 10:08:40 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
25 #include <yaz++/socket-observer.h>
29 #include "thread_pool_observer.hpp"
31 using namespace yazpp_1;
33 IThreadPoolMsg::~IThreadPoolMsg()
38 ThreadPoolSocketObserver::ThreadPoolSocketObserver(ISocketObservable *obs, int no_threads)
39 : m_SocketObservable(obs)
42 obs->addObserver(m_fd[0], this);
43 obs->maskObserver(this, SOCKET_OBSERVE_READ);
46 m_no_threads = no_threads;
48 for (i = 0; i<no_threads; i++)
51 m_thrds.add_thread(new boost::thread(w));
55 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
58 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
60 m_cond_input_data.notify_all();
64 m_SocketObservable->deleteObserver(this);
70 void ThreadPoolSocketObserver::socketNotify(int event)
72 if (event & SOCKET_OBSERVE_READ)
75 read(m_fd[0], buf, 1);
78 boost::mutex::scoped_lock output_lock(m_mutex_output_data);
79 out = m_output.front();
87 void ThreadPoolSocketObserver::run(void *p)
91 IThreadPoolMsg *in = 0;
93 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
94 while (!m_stop_flag && m_input.size() == 0)
95 m_cond_input_data.wait(input_lock);
102 IThreadPoolMsg *out = in->handle();
104 boost::mutex::scoped_lock output_lock(m_mutex_output_data);
105 m_output.push_back(out);
106 write(m_fd[1], "", 1);
111 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
113 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
114 m_input.push_back(m);
115 m_cond_input_data.notify_one();
120 * indent-tabs-mode: nil
122 * vim: shiftwidth=4 tabstop=8 expandtab