1 /* $Id: tstthreads.cpp,v 1.1 2005-05-19 21:29: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
30 #include <yaz++/socket-observer.h>
31 #include <yaz++/socket-manager.h>
37 virtual void destroy() = 0;
38 virtual Proxy_Msg *handle() = 0;
39 virtual void result() = 0;
42 class Proxy_Msg_Queue_List {
43 friend class Proxy_Msg_Queue;
46 Proxy_Msg_Queue_List *m_next;
49 class Proxy_Msg_Queue {
52 void enqueue(Proxy_Msg *in);
56 Proxy_Msg_Queue_List *m_list;
59 Proxy_Msg_Queue::Proxy_Msg_Queue()
64 int Proxy_Msg_Queue::size()
67 Proxy_Msg_Queue_List *l;
68 for (l = m_list; l; l = l->m_next)
73 void Proxy_Msg_Queue::enqueue(Proxy_Msg *m)
75 Proxy_Msg_Queue_List *l = new Proxy_Msg_Queue_List;
81 Proxy_Msg *Proxy_Msg_Queue::dequeue()
83 Proxy_Msg_Queue_List **l = &m_list;
88 Proxy_Msg *m = (*l)->m_item;
94 class Proxy_Thread : public IYazSocketObserver {
96 Proxy_Thread(IYazSocketObservable *obs);
97 virtual ~Proxy_Thread();
98 void socketNotify(int event);
99 void put(Proxy_Msg *m);
103 IYazSocketObservable *m_obs;
105 pthread_t m_thread_id;
106 Proxy_Msg_Queue m_input;
107 Proxy_Msg_Queue m_output;
108 pthread_mutex_t m_mutex_input_data;
109 pthread_cond_t m_cond_input_data;
110 pthread_mutex_t m_mutex_output_data;
111 pthread_cond_t m_cond_output_data;
114 static void *tfunc(void *p)
116 Proxy_Thread *pt = (Proxy_Thread *) p;
122 Proxy_Thread::Proxy_Thread(IYazSocketObservable *obs)
125 pthread_mutex_init(&m_mutex_input_data, 0);
126 pthread_cond_init(&m_cond_input_data, 0);
127 pthread_mutex_init(&m_mutex_output_data, 0);
128 pthread_cond_init(&m_cond_output_data, 0);
129 m_fd[0] = m_fd[1] = -1;
132 m_obs->addObserver(m_fd[0], this);
133 m_obs->timeoutObserver(this, 2000);
134 m_obs->maskObserver(this, YAZ_SOCKET_OBSERVE_READ);
136 pthread_create(&m_thread_id, 0, tfunc, this);
139 Proxy_Thread::~Proxy_Thread()
144 void Proxy_Thread::socketNotify(int event)
147 read(m_fd[0], buf, 1);
148 pthread_mutex_lock(&m_mutex_output_data);
149 Proxy_Msg *out = m_output.dequeue();
150 pthread_mutex_unlock(&m_mutex_output_data);
155 void Proxy_Thread::run(void *p)
159 pthread_mutex_lock(&m_mutex_input_data);
160 pthread_cond_wait(&m_cond_input_data, &m_mutex_input_data);
163 Proxy_Msg *in = m_input.dequeue();
164 pthread_mutex_unlock(&m_mutex_input_data);
167 Proxy_Msg *out = in->handle();
168 pthread_mutex_lock(&m_mutex_output_data);
169 m_output.enqueue(out);
170 pthread_cond_signal(&m_cond_output_data);
171 pthread_mutex_unlock(&m_mutex_output_data);
172 write(m_fd[1], "", 1);
174 pthread_mutex_lock(&m_mutex_input_data);
179 void Proxy_Thread::put(Proxy_Msg *m)
181 pthread_mutex_lock(&m_mutex_input_data);
183 pthread_cond_signal(&m_cond_input_data);
184 int in_size = m_input.size();
185 pthread_mutex_unlock(&m_mutex_input_data);
186 int out_size = m_output.size();
187 printf("in-size=%d out-size=%d\n", in_size, out_size);
190 class My_Msg : public Proxy_Msg {
198 class My_Thread : public Proxy_Thread {
200 My_Thread(IYazSocketObservable *obs);
203 My_Thread::My_Thread(IYazSocketObservable *obs) : Proxy_Thread(obs)
207 Proxy_Msg *My_Msg::handle()
209 My_Msg *res = new My_Msg;
213 printf("My_Msg::handle val=%d sleep=%d\n", m_val, sl);
219 void My_Msg::result()
221 printf("My_Msg::result val=%d\n", m_val);
224 void My_Msg::destroy()
229 class My_Timer_Thread : public IYazSocketObserver {
231 IYazSocketObservable *m_obs;
235 My_Timer_Thread(IYazSocketObservable *obs, My_Thread *t);
236 void socketNotify(int event);
239 My_Timer_Thread::My_Timer_Thread(IYazSocketObservable *obs,
240 My_Thread *t) : m_obs(obs)
244 obs->addObserver(m_fd[0], this);
245 obs->timeoutObserver(this, 2);
248 void My_Timer_Thread::socketNotify(int event)
251 printf("Add %d\n", seq);
252 My_Msg *m = new My_Msg;
257 int main(int argc, char **argv)
259 Yaz_SocketManager mySocketManager;
261 My_Thread m(&mySocketManager);
262 My_Timer_Thread t(&mySocketManager, &m);
263 while (mySocketManager.processEvent() > 0)