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 edr_delete=false;
65 bool edrType = edr->call(userTag,edr_delete);
67 edr->callinprogress = false;
69 if (edrType == false) // it's a multicall
71 if (edr->nomorecalls) // External has called unRegister - probably the receiver
73 // wake up the thread waiting in unregister
75 pthread_cond_signal(&edr->cond);
81 else // It's a single call. The receiver should be removed from the list. There will be a thread to wake up
83 for(i = receivers.begin(); i != receivers.end(); i++)
92 if (i == receivers.end()) abort(); // should never happen
93 // but it can happen under windows ... how??
97 pthread_cond_signal(&edr->cond);
101 if (edr_delete) delete edr;
108 void EventDispatcher::edUnregister(EDReceiver* edr)
113 for(i = receivers.begin(); i != receivers.end(); i++)
115 if (*i == edr) break; // found
118 // Not in the list. Already unregistered? Perhaps vdr::connectionDied already removed this streamclient
119 // 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.
120 if (i == receivers.end())
126 if (!edr->callinprogress)
133 edr->nomorecalls = true;
135 // edUnlock, wait for callinprogres=false (cond to be signalled), lock
137 pthread_cond_wait(&edr->cond, &mutex);
140 WaitForSingleObject(edr->cond,INFINITE);
141 WaitForSingleObject(mutex, INFINITE);
142 ResetEvent(edr->cond);
146 for(i = receivers.begin(); i != receivers.end(); i++)
148 if (*i == edr) break; // found
151 if (i == receivers.end()) abort(); // should never happen
157 // ---------------------------------------
159 void EventDispatcher::edLock()
162 pthread_mutex_lock(&mutex);
164 WaitForSingleObject(mutex, INFINITE);
168 void EventDispatcher::edUnlock()
171 pthread_mutex_unlock(&mutex);
177 // ---------------------------------------
179 void EventDispatcher::edSleepThisReceiver(EDReceiver* edr)
181 // For blocking version, not callback version. Call with edLock locked
184 pthread_cond_wait(&edr->cond, &mutex);
186 ResetEvent(edr->cond);
188 WaitForSingleObject(edr->cond,INFINITE);
189 ResetEvent(edr->cond);
190 WaitForSingleObject(mutex, INFINITE);
194 // -------------- EDReceiver implementation
196 EDReceiver::EDReceiver()
199 callinprogress = false;
201 pthread_cond_init(&cond, NULL);
203 cond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
207 EDReceiver::~EDReceiver()
210 if (cond!=NULL) CloseHandle(cond);
212 pthread_cond_destroy(&cond);