]> git.vomp.tv Git - vompclient.git/blob - eventdispatcher.cc
Windows and endian updates
[vompclient.git] / eventdispatcher.cc
1 /*
2     Copyright 2007 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "eventdispatcher.h"
22
23 EventDispatcher::EventDispatcher()
24 {
25 #ifndef WIN32
26   pthread_mutex_init(&mutex, NULL);
27 #else
28   mutex = CreateMutex(NULL, FALSE, NULL);
29 #endif
30 }
31
32 void EventDispatcher::edRegister(EDReceiver* edr)
33 {
34   edLock();
35   receivers.push_back(edr);
36   edUnlock();
37 }  
38
39 bool EventDispatcher::edFindAndCall(void* userTag)
40 {
41   edLock();
42   
43   EDReceiver* edr = NULL;
44   EDRL::iterator i;
45   for(i = receivers.begin(); i != receivers.end(); i++)
46   {
47     if (ed_cb_find(*i, userTag))
48     {
49       edr = *i;
50       break; // found (by asking the EventDispatcher implementor to check if userTag is for *i
51     }
52   }
53   
54   if ((i == receivers.end()) || edr->callinprogress || edr->nomorecalls)
55   {
56     edUnlock();
57     return false;
58   }
59   
60   edr->callinprogress = true;
61   edUnlock();  
62   bool edrType = edr->call(userTag);
63   edLock();
64   edr->callinprogress = false;
65
66   if (edrType == false) // it's a multicall
67   {
68     if (edr->nomorecalls) // External has called unRegister - probably the receiver
69     {
70       // wake up the thread waiting in unregister
71       #ifndef WIN32
72       pthread_cond_signal(&edr->cond);
73       #else
74       SetEvent(edr->cond);
75       #endif
76     }
77   }
78   else // It's a single call. The receiver should be removed from the list. There will be a thread to wake up
79   {
80     for(i = receivers.begin(); i != receivers.end(); i++)
81     {
82       if (*i == edr)
83       {
84         receivers.erase(i);
85         break;
86       }
87     }
88     if (i == receivers.end()) abort(); // should never happen
89     
90     #ifndef WIN32
91     pthread_cond_signal(&edr->cond);
92     #else
93     SetEvent(edr->cond);
94     #endif
95   }
96   
97   edUnlock();
98   return true;
99 }
100
101 void EventDispatcher::edUnregister(EDReceiver* edr)
102 {
103   edLock();
104
105   EDRL::iterator i;
106   for(i = receivers.begin(); i != receivers.end(); i++)
107   {
108     if (*i == edr) break; // found
109   }
110
111   if (i == receivers.end()) abort(); // should never happen
112
113   if (!edr->callinprogress)
114   {
115     receivers.erase(i);
116     edUnlock();
117     return;
118   }
119
120   edr->nomorecalls = true;
121   
122   // edUnlock, wait for callinprogres=false (cond to be signalled), lock  
123 #ifndef WIN32
124   pthread_cond_wait(&edr->cond, &mutex);
125 #else
126   ReleaseMutex(mutex);
127   WaitForSingleObject(edr->cond,INFINITE);
128   WaitForSingleObject(mutex, INFINITE);
129   ResetEvent(edr->cond);
130
131 #endif
132
133   for(i = receivers.begin(); i != receivers.end(); i++)
134   {
135     if (*i == edr) break; // found
136   }
137
138   if (i == receivers.end()) abort(); // should never happen
139
140   receivers.erase(i);
141   edUnlock();
142 }
143
144 // ---------------------------------------
145
146 void EventDispatcher::edLock()
147 {
148 #ifndef WIN32
149   pthread_mutex_lock(&mutex);
150 #else
151   WaitForSingleObject(mutex, INFINITE);
152 #endif
153 }
154
155 void EventDispatcher::edUnlock()
156 {
157 #ifndef WIN32
158   pthread_mutex_unlock(&mutex);
159 #else
160   ReleaseMutex(mutex);
161 #endif
162 }
163
164 // ---------------------------------------
165
166 void EventDispatcher::edSleepThisReceiver(EDReceiver* edr)
167 {
168   // For blocking version, not callback version. Call with edLock locked
169   
170 #ifndef WIN32
171   pthread_cond_init(&edr->cond, NULL);
172   pthread_cond_wait(&edr->cond, &mutex);
173 #else
174    edr->cond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
175    ReleaseMutex(mutex);
176    WaitForSingleObject(edr->cond,INFINITE);
177    ResetEvent(edr->cond);
178    WaitForSingleObject(mutex, INFINITE);
179 #endif
180 }
181
182 // -------------- EDReceiver implementation
183
184 EDReceiver::EDReceiver()
185 {
186   nomorecalls = false;
187   callinprogress = false;
188 #ifdef WIN32
189   cond=NULL;
190 #endif
191 }
192
193 EDReceiver::~EDReceiver()
194 {
195 #ifdef WIN32
196   if (cond!=NULL) CloseHandle(cond);
197 #endif
198 }