Added locking to prevent race conditions

This commit is contained in:
paul 2006-12-01 19:49:33 +00:00
parent 75d7852eb8
commit 3b481ec39c
2 changed files with 21 additions and 4 deletions

View File

@ -68,6 +68,10 @@ SchedulerThread :: SchedulerThread(
isPreloading(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);
} }
@ -103,8 +107,10 @@ 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);
if (nextEvent) { if (nextEvent) {
if (imminent(now, nextInitTime)) { if (imminent(now, nextInitTime)) {
pthread_mutex_lock(&preloadLock);
debug() << "::nextStep() - Init [" << *TimeConversion::now() debug() << "::nextStep() - Init [" << *TimeConversion::now()
<< "]" << endl; << "]" << endl;
try { try {
@ -117,7 +123,6 @@ SchedulerThread :: nextStep(Ptr<ptime>::Ref now) throw ()
std::cerr << "event initialization error: " << e.what() std::cerr << "event initialization error: " << e.what()
<< std::endl; << std::endl;
} }
isPreloading = true;
} else if (imminent(now, nextEventTime)) { } else if (imminent(now, nextEventTime)) {
Ptr<time_duration>::Ref timeLeft(new time_duration(*nextEventTime Ptr<time_duration>::Ref timeLeft(new time_duration(*nextEventTime
- *now)); - *now));
@ -128,9 +133,11 @@ SchedulerThread :: nextStep(Ptr<ptime>::Ref now) throw ()
currentEvent = nextEvent; currentEvent = nextEvent;
currentEventEnd = nextEventEnd; currentEventEnd = nextEventEnd;
getNextEvent(TimeConversion::now()); getNextEvent(TimeConversion::now());
isPreloading = false; pthread_mutex_unlock(&preloadLock);
} }
} }
pthread_mutex_unlock(&nextEventLock);
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
@ -184,8 +191,14 @@ SchedulerThread :: signal(int signalId) throw ()
switch (signalId) { switch (signalId) {
case UpdateSignal: case UpdateSignal:
if (!isPreloading) { if (!pthread_mutex_trylock(&nextEventLock)) {
getNextEvent(TimeConversion::now()); if (!pthread_mutex_trylock(&preloadLock)) {
getNextEvent(TimeConversion::now());
pthread_mutex_unlock(&preloadLock);
pthread_mutex_unlock(&nextEventLock);
} else {
pthread_mutex_unlock(&nextEventLock);
}
} }
break; break;

View File

@ -133,6 +133,10 @@ class SchedulerThread : public virtual RunnableInterface
*/ */
bool isPreloading; bool isPreloading;
pthread_mutex_t nextEventLock;
pthread_mutex_t preloadLock;
pthread_mutexattr_t mutexAttr;
/** /**
* Default constructor. * Default constructor.
*/ */