Make mutex locks slightly more safe in some places (and correct in another...)

This commit is contained in:
Bart van Strien
2013-08-03 22:13:33 +02:00
parent 3e57cfd174
commit 1602c6de7a
2 changed files with 11 additions and 17 deletions
+6 -13
View File
@@ -125,13 +125,11 @@ void Channel::supply(Variant *var)
if (!var) if (!var)
return; return;
mutex->lock(); Lock l(mutex);
unsigned long id = push(var); unsigned long id = push(var);
while (!past(id, received)) while (!past(id, received))
cond->wait(mutex); cond->wait(mutex);
mutex->unlock();
} }
Variant *Channel::pop() Variant *Channel::pop()
@@ -157,11 +155,10 @@ Variant *Channel::pop()
Variant *Channel::demand() Variant *Channel::demand()
{ {
Variant *var; Variant *var;
mutex->lock(); Lock l(mutex);
while (!(var = pop())) while (!(var = pop()))
cond->wait(mutex); cond->wait(mutex);
mutex->unlock();
return var; return var;
} }
@@ -218,24 +215,20 @@ void Channel::unlockMutex()
void Channel::retain() void Channel::retain()
{ {
EmptyLock l;
if (named) if (named)
namedChannelMutex->lock(); l.setLock(namedChannelMutex);
Object::retain(); Object::retain();
if (named)
namedChannelMutex->unlock();
} }
void Channel::release() void Channel::release()
{ {
EmptyLock l;
if (named) if (named)
namedChannelMutex->lock(); l.setLock(namedChannelMutex);
Object::release(); Object::release();
if (named)
namedChannelMutex->unlock();
} }
} // thread } // thread
} // love } // love
+5 -4
View File
@@ -58,10 +58,11 @@ bool Thread::start()
void Thread::wait() void Thread::wait()
{ {
mutex.lock(); {
if (!thread) Lock l(mutex);
return; if (!thread)
mutex.unlock(); return;
}
SDL_WaitThread(thread, 0); SDL_WaitThread(thread, 0);
Lock l(mutex); Lock l(mutex);
running = false; running = false;