Update for the yazpp_1 namespace.
[yazproxy-moved-to-github.git] / src / msg-thread.cpp
1 /* $Id: msg-thread.cpp,v 1.2 2005-06-02 06:40:46 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
5
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
9 version.
10
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
14 for more details.
15
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
19 02111-1307, USA.
20  */
21 #include <pthread.h>
22 #include <unistd.h>
23 #include <ctype.h>
24
25 #include <yaz++/socket-observer.h>
26 #include <yaz/log.h>
27
28 #include "msg-thread.h"
29
30 using namespace yazpp_1;
31
32 Msg_Thread_Queue::Msg_Thread_Queue()
33 {
34     m_list = 0;
35 }
36
37 int Msg_Thread_Queue::size()
38 {
39     int no = 0;
40     Msg_Thread_Queue_List *l;
41     for (l = m_list; l; l = l->m_next)
42         no++;
43     return no;
44 }
45
46 void Msg_Thread_Queue::enqueue(IMsg_Thread *m)
47 {
48     Msg_Thread_Queue_List *l = new Msg_Thread_Queue_List;
49     l->m_next = m_list;
50     l->m_item = m;
51     m_list = l;
52 }
53
54 IMsg_Thread *Msg_Thread_Queue::dequeue()
55 {
56     Msg_Thread_Queue_List **l = &m_list;
57     if (!*l)
58         return 0;
59     while ((*l)->m_next)
60         l = &(*l)->m_next;
61     IMsg_Thread *m = (*l)->m_item;
62     delete *l;
63     *l = 0;
64     return m;
65 }
66
67 static void *tfunc(void *p)
68 {
69     Msg_Thread *pt = (Msg_Thread *) p;
70     pt->run(0);
71     return 0;
72 }
73
74
75 Msg_Thread::Msg_Thread(IYazSocketObservable *obs)
76     : m_SocketObservable(obs)
77 {
78     pipe(m_fd);
79     obs->addObserver(m_fd[0], this);
80     obs->maskObserver(this, YAZ_SOCKET_OBSERVE_READ);
81
82     m_stop_flag = false;
83     pthread_mutex_init(&m_mutex_input_data, 0);
84     pthread_cond_init(&m_cond_input_data, 0);
85     pthread_mutex_init(&m_mutex_output_data, 0);
86     pthread_cond_init(&m_cond_output_data, 0);
87     pthread_create(&m_thread_id, 0, tfunc, this);
88 }
89
90 Msg_Thread::~Msg_Thread()
91 {
92     pthread_mutex_lock(&m_mutex_input_data);
93     m_stop_flag = true;
94     pthread_cond_signal(&m_cond_input_data);
95     pthread_mutex_unlock(&m_mutex_input_data);
96     
97     pthread_join(m_thread_id, 0);
98
99     m_SocketObservable->deleteObserver(this);
100
101     pthread_cond_destroy(&m_cond_input_data);
102     pthread_mutex_destroy(&m_mutex_input_data);
103     pthread_cond_destroy(&m_cond_output_data);
104     pthread_mutex_destroy(&m_mutex_output_data);
105     close(m_fd[0]);
106     close(m_fd[1]);
107 }
108
109 void Msg_Thread::socketNotify(int event)
110 {
111     if (event & YAZ_SOCKET_OBSERVE_READ)
112     {
113         char buf[2];
114         read(m_fd[0], buf, 1);
115         pthread_mutex_lock(&m_mutex_output_data);
116         IMsg_Thread *out = m_output.dequeue();
117         pthread_mutex_unlock(&m_mutex_output_data);
118         if (out)
119             out->result();
120     }
121 }
122
123 void Msg_Thread::run(void *p)
124 {
125     while(1)
126     {
127         pthread_mutex_lock(&m_mutex_input_data);
128         pthread_cond_wait(&m_cond_input_data, &m_mutex_input_data);
129         while (1)
130         {
131             if (m_stop_flag)
132             {
133                 pthread_mutex_unlock(&m_mutex_input_data);
134                 return;
135             }
136             IMsg_Thread *in = m_input.dequeue();
137             pthread_mutex_unlock(&m_mutex_input_data);
138             if (!in)
139                 break;
140             IMsg_Thread *out = in->handle();
141             pthread_mutex_lock(&m_mutex_output_data);
142             m_output.enqueue(out);
143             pthread_cond_signal(&m_cond_output_data);
144             pthread_mutex_unlock(&m_mutex_output_data);
145
146             write(m_fd[1], "", 1);
147
148             pthread_mutex_lock(&m_mutex_input_data);
149         }
150     }
151 }
152
153 void Msg_Thread::put(IMsg_Thread *m)
154 {
155     pthread_mutex_lock(&m_mutex_input_data);
156     m_input.enqueue(m);
157     pthread_cond_signal(&m_cond_input_data);
158     pthread_mutex_unlock(&m_mutex_input_data);
159 }