2 Copyright 2007 Chris Tallon
4 This file is part of VOMP.
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.
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.
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.
21 #include "eventdispatcher.h"
23 EventDispatcher::EventDispatcher()
26 pthread_mutex_init(&mutex, NULL);
28 mutex = CreateMutex(NULL, FALSE, NULL);
32 void EventDispatcher::edRegister(EDReceiver* edr)
35 receivers.push_back(edr);
39 bool EventDispatcher::edFindAndCall(void* userTag)
43 EDReceiver* edr = NULL;
45 for(i = receivers.begin(); i != receivers.end(); i++)
47 if (ed_cb_find(*i, userTag))
50 break; // found (by asking the EventDispatcher implementor to check if userTag is for *i
54 if ((i == receivers.end()) || edr->callinprogress || edr->nomorecalls)
60 edr->callinprogress = true;
62 bool edrType = edr->call(userTag);
64 edr->callinprogress = false;
66 if (edrType == false) // it's a multicall
68 if (edr->nomorecalls) // External has called unRegister - probably the receiver
70 // wake up the thread waiting in unregister
72 pthread_cond_signal(&edr->cond);
78 else // It's a single call. The receiver should be removed from the list. There will be a thread to wake up
80 for(i = receivers.begin(); i != receivers.end(); i++)
89 if (i == receivers.end()) abort(); // should never happen
90 // but it can happen under windows ... how??
94 pthread_cond_signal(&edr->cond);
104 void EventDispatcher::edUnregister(EDReceiver* edr)
109 for(i = receivers.begin(); i != receivers.end(); i++)
111 if (*i == edr) break; // found
114 if (i == receivers.end()) abort(); // should never happen
116 if (!edr->callinprogress)
123 edr->nomorecalls = true;
125 // edUnlock, wait for callinprogres=false (cond to be signalled), lock
127 pthread_cond_wait(&edr->cond, &mutex);
130 WaitForSingleObject(edr->cond,INFINITE);
131 WaitForSingleObject(mutex, INFINITE);
132 ResetEvent(edr->cond);
136 for(i = receivers.begin(); i != receivers.end(); i++)
138 if (*i == edr) break; // found
141 if (i == receivers.end()) abort(); // should never happen
147 // ---------------------------------------
149 void EventDispatcher::edLock()
152 pthread_mutex_lock(&mutex);
154 WaitForSingleObject(mutex, INFINITE);
158 void EventDispatcher::edUnlock()
161 pthread_mutex_unlock(&mutex);
167 // ---------------------------------------
169 void EventDispatcher::edSleepThisReceiver(EDReceiver* edr)
171 // For blocking version, not callback version. Call with edLock locked
174 pthread_cond_init(&edr->cond, NULL);
175 pthread_cond_wait(&edr->cond, &mutex);
177 edr->cond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
179 WaitForSingleObject(edr->cond,INFINITE);
180 ResetEvent(edr->cond);
181 WaitForSingleObject(mutex, INFINITE);
185 // -------------- EDReceiver implementation
187 EDReceiver::EDReceiver()
190 callinprogress = false;
196 EDReceiver::~EDReceiver()
199 if (cond!=NULL) CloseHandle(cond);