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()) return; // Not in the list. Already unregistered? Perhaps vdr::connectionDied already removed this streamclient
115 // FIXME, this should probably be done another way. A call to edUnregister with an object that may or may not be in the list? Not good.
117 if (!edr->callinprogress)
124 edr->nomorecalls = true;
126 // edUnlock, wait for callinprogres=false (cond to be signalled), lock
128 pthread_cond_wait(&edr->cond, &mutex);
131 WaitForSingleObject(edr->cond,INFINITE);
132 WaitForSingleObject(mutex, INFINITE);
133 ResetEvent(edr->cond);
137 for(i = receivers.begin(); i != receivers.end(); i++)
139 if (*i == edr) break; // found
142 if (i == receivers.end()) abort(); // should never happen
148 // ---------------------------------------
150 void EventDispatcher::edLock()
153 pthread_mutex_lock(&mutex);
155 WaitForSingleObject(mutex, INFINITE);
159 void EventDispatcher::edUnlock()
162 pthread_mutex_unlock(&mutex);
168 // ---------------------------------------
170 void EventDispatcher::edSleepThisReceiver(EDReceiver* edr)
172 // For blocking version, not callback version. Call with edLock locked
175 pthread_cond_init(&edr->cond, NULL);
176 pthread_cond_wait(&edr->cond, &mutex);
178 edr->cond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
180 WaitForSingleObject(edr->cond,INFINITE);
181 ResetEvent(edr->cond);
182 WaitForSingleObject(mutex, INFINITE);
186 // -------------- EDReceiver implementation
188 EDReceiver::EDReceiver()
191 callinprogress = false;
197 EDReceiver::~EDReceiver()
200 if (cond!=NULL) CloseHandle(cond);