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"
25 EventDispatcher::EventDispatcher()
28 pthread_mutex_init(&mutex, NULL);
30 mutex = CreateMutex(NULL, FALSE, NULL);
34 void EventDispatcher::edRegister(EDReceiver* edr)
37 receivers.push_back(edr);
41 bool EventDispatcher::edFindAndCall(void* userTag)
45 EDReceiver* edr = NULL;
47 for(i = receivers.begin(); i != receivers.end(); i++)
49 if (ed_cb_find(*i, userTag))
52 break; // found (by asking the EventDispatcher implementor to check if userTag is for *i
56 if ((i == receivers.end()) || edr->callinprogress || edr->nomorecalls)
62 edr->callinprogress = true;
64 bool edrType = edr->call(userTag);
66 edr->callinprogress = false;
68 if (edrType == false) // it's a multicall
70 if (edr->nomorecalls) // External has called unRegister - probably the receiver
72 // wake up the thread waiting in unregister
74 pthread_cond_signal(&edr->cond);
80 else // It's a single call. The receiver should be removed from the list. There will be a thread to wake up
82 for(i = receivers.begin(); i != receivers.end(); i++)
91 if (i == receivers.end()) abort(); // should never happen
92 // but it can happen under windows ... how??
96 pthread_cond_signal(&edr->cond);
106 void EventDispatcher::edUnregister(EDReceiver* edr)
111 for(i = receivers.begin(); i != receivers.end(); i++)
113 if (*i == edr) break; // found
116 // Not in the list. Already unregistered? Perhaps vdr::connectionDied already removed this streamclient
117 // 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.
118 if (i == receivers.end())
124 if (!edr->callinprogress)
131 edr->nomorecalls = true;
133 // edUnlock, wait for callinprogres=false (cond to be signalled), lock
135 pthread_cond_wait(&edr->cond, &mutex);
138 WaitForSingleObject(edr->cond,INFINITE);
139 WaitForSingleObject(mutex, INFINITE);
140 ResetEvent(edr->cond);
144 for(i = receivers.begin(); i != receivers.end(); i++)
146 if (*i == edr) break; // found
149 if (i == receivers.end()) abort(); // should never happen
155 // ---------------------------------------
157 void EventDispatcher::edLock()
160 pthread_mutex_lock(&mutex);
162 WaitForSingleObject(mutex, INFINITE);
166 void EventDispatcher::edUnlock()
169 pthread_mutex_unlock(&mutex);
175 // ---------------------------------------
177 void EventDispatcher::edSleepThisReceiver(EDReceiver* edr)
179 // For blocking version, not callback version. Call with edLock locked
182 pthread_cond_wait(&edr->cond, &mutex);
184 ResetEvent(edr->cond);
186 WaitForSingleObject(edr->cond,INFINITE);
187 ResetEvent(edr->cond);
188 WaitForSingleObject(mutex, INFINITE);
192 // -------------- EDReceiver implementation
194 EDReceiver::EDReceiver()
197 callinprogress = false;
199 pthread_cond_init(&cond, NULL);
201 cond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
205 EDReceiver::~EDReceiver()
208 if (cond!=NULL) CloseHandle(cond);
210 pthread_cond_destroy(&cond);