1 /* $Id: thread_pool_observer.cpp,v 1.3 2005-10-12 23:30:43 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.h"
31 using namespace yazpp_1;
33 IThreadPoolMsg::~IThreadPoolMsg()
40 worker(ThreadPoolSocketObserver *s) : m_s(s) {};
41 ThreadPoolSocketObserver *m_s;
42 void operator() (void) {
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 m_no_threads = no_threads;
57 for (i = 0; i<no_threads; i++)
60 m_thrds.add_thread(new boost::thread(w));
64 ThreadPoolSocketObserver::~ThreadPoolSocketObserver()
67 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
69 m_cond_input_data.notify_all();
73 m_SocketObservable->deleteObserver(this);
79 void ThreadPoolSocketObserver::socketNotify(int event)
81 if (event & SOCKET_OBSERVE_READ)
84 read(m_fd[0], buf, 1);
87 boost::mutex::scoped_lock output_lock(m_mutex_output_data);
88 out = m_output.front();
96 void ThreadPoolSocketObserver::run(void *p)
100 IThreadPoolMsg *in = 0;
102 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
103 while (!m_stop_flag && m_input.size() == 0)
104 m_cond_input_data.wait(input_lock);
108 in = m_input.front();
111 IThreadPoolMsg *out = in->handle();
113 boost::mutex::scoped_lock output_lock(m_mutex_output_data);
114 m_output.push_back(out);
115 write(m_fd[1], "", 1);
120 void ThreadPoolSocketObserver::put(IThreadPoolMsg *m)
122 boost::mutex::scoped_lock input_lock(m_mutex_input_data);
123 m_input.push_back(m);
124 m_cond_input_data.notify_one();
129 * indent-tabs-mode: nil
131 * vim: shiftwidth=4 tabstop=8 expandtab