prettified the mutex code

This commit is contained in:
fgerlits 2006-12-01 20:25:01 +00:00
parent 0dc9d2bab3
commit 05beb4d5d0
3 changed files with 173 additions and 22 deletions

View File

@ -0,0 +1,154 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the Campcaster project.
http://campcaster.campware.org/
To report bugs, send an e-mail to bugs@campware.org
Campcaster is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Campcaster is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Campcaster; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author$
Version : $Revision$
Location : $URL$
------------------------------------------------------------------------------*/
#ifndef LiveSupport_Core_Mutex_h
#define LiveSupport_Core_Mutex_h
#ifndef __cplusplus
#error This is a C++ include file
#endif
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#include <pthread.h>
namespace LiveSupport {
namespace Core {
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/**
* A simple wrapper for pthread_mutex_t.
*
* @author $Author$
* @version $Revision$
*/
class Mutex
{
private:
/**
* The mutex object.
*/
pthread_mutex_t * mutex;
/**
* The error code returned by tryLockMutex().
*/
int mutexError;
public:
/**
* Default constructor.
*/
Mutex(void) throw ()
: mutexError(0)
{
mutex = new pthread_mutex_t;
pthread_mutex_init(mutex, NULL);
}
/**
* A virtual destructor, as this class has virtual functions.
*/
virtual
~Mutex(void) throw ()
{
pthread_mutex_destroy(mutex);
}
/**
* Lock the mutex.
* If the mutex is already locked, it blocks until it becomes free.
*/
void
lock(void) throw ()
{
pthread_mutex_lock(mutex);
}
/**
* Unlock a mutex.
*/
void
unlock(void) throw ()
{
pthread_mutex_unlock(mutex);
}
/**
* Try to lock a mutex.
* If the mutex is already locked, it returns false.
*
* @return true if the mutex was successfully locked; false otherwise.
*/
bool
tryLock(void) throw ()
{
mutexError = pthread_mutex_trylock(mutex);
return (mutexError == 0);
}
/**
* Get the error code (if any) after a call to tryLockMutex().
*
* @return the error code; 0 for no error.
*/
int
getMutexError(void) throw ()
{
return mutexError;
}
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // LiveSupport_Core_Mutex_h

View File

@ -64,14 +64,9 @@ SchedulerThread :: SchedulerThread(
throw () throw ()
: eventContainer(eventContainer), : eventContainer(eventContainer),
granularity(granularity), granularity(granularity),
shouldRun(false), shouldRun(false)
isPreloading(false)
{ {
//DEBUG_FUNC_INFO //DEBUG_FUNC_INFO
pthread_mutexattr_init(&mutexAttr);
pthread_mutex_init(&nextEventLock, &mutexAttr);
pthread_mutex_init(&preloadLock, &mutexAttr);
pthread_mutexattr_destroy(&mutexAttr);
} }
@ -107,16 +102,17 @@ SchedulerThread :: getNextEvent(Ptr<ptime>::Ref when) throw ()
void void
SchedulerThread :: nextStep(Ptr<ptime>::Ref now) throw () SchedulerThread :: nextStep(Ptr<ptime>::Ref now) throw ()
{ {
pthread_mutex_lock(&nextEventLock); nextEventMutex.lock();
if (nextEvent) { if (nextEvent) {
if (imminent(now, nextInitTime)) { if (imminent(now, nextInitTime)) {
pthread_mutex_lock(&preloadLock); preloadMutex.lock();
debug() << "::nextStep() - Init [" << *TimeConversion::now() debug() << "::nextStep() - Init [" << *TimeConversion::now()
<< "]" << endl; << "]" << endl;
try { try {
nextEvent->initialize(); nextEvent->initialize();
} catch (std::exception &e) { } catch (std::exception &e) {
pthread_mutex_unlock(&preloadLock); preloadMutex.unlock();
// cancel event by getting the next event after this was // cancel event by getting the next event after this was
// supposed to finish // supposed to finish
getNextEvent(nextEventEnd); getNextEvent(nextEventEnd);
@ -134,11 +130,11 @@ SchedulerThread :: nextStep(Ptr<ptime>::Ref now) throw ()
currentEvent = nextEvent; currentEvent = nextEvent;
currentEventEnd = nextEventEnd; currentEventEnd = nextEventEnd;
getNextEvent(TimeConversion::now()); getNextEvent(TimeConversion::now());
pthread_mutex_unlock(&preloadLock); preloadMutex.unlock();
} }
} }
pthread_mutex_unlock(&nextEventLock); nextEventMutex.unlock();
if (currentEvent && imminent(now, currentEventEnd)) { if (currentEvent && imminent(now, currentEventEnd)) {
Ptr<time_duration>::Ref timeLeft(new time_duration(*currentEventEnd Ptr<time_duration>::Ref timeLeft(new time_duration(*currentEventEnd
@ -192,13 +188,13 @@ SchedulerThread :: signal(int signalId) throw ()
switch (signalId) { switch (signalId) {
case UpdateSignal: case UpdateSignal:
if (!pthread_mutex_trylock(&nextEventLock)) { if (nextEventMutex.tryLock()) {
if (!pthread_mutex_trylock(&preloadLock)) { if (preloadMutex.tryLock()) {
getNextEvent(TimeConversion::now()); getNextEvent(TimeConversion::now());
pthread_mutex_unlock(&preloadLock); preloadMutex.unlock();
pthread_mutex_unlock(&nextEventLock); nextEventMutex.unlock();
} else { } else {
pthread_mutex_unlock(&nextEventLock); nextEventMutex.unlock();
} }
} }
break; break;

View File

@ -43,6 +43,7 @@
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include "LiveSupport/Core/RunnableInterface.h" #include "LiveSupport/Core/RunnableInterface.h"
#include "LiveSupport/Core/Mutex.h"
#include "LiveSupport/EventScheduler/ScheduledEventInterface.h" #include "LiveSupport/EventScheduler/ScheduledEventInterface.h"
#include "LiveSupport/EventScheduler/EventContainerInterface.h" #include "LiveSupport/EventScheduler/EventContainerInterface.h"
@ -128,14 +129,14 @@ class SchedulerThread : public virtual RunnableInterface
bool shouldRun; bool shouldRun;
/** /**
* Flag indicating that we are between an initialize() and * A mutex for getting the next event.
* a start() call.
*/ */
bool isPreloading; Mutex nextEventMutex;
pthread_mutex_t nextEventLock; /**
pthread_mutex_t preloadLock; * A mutex for the preload.
pthread_mutexattr_t mutexAttr; */
Mutex preloadMutex;
/** /**
* Default constructor. * Default constructor.