Add a MutexRef class to clean up Mutex handling code

This commit is contained in:
Bart van Strien
2015-07-06 11:58:08 +02:00
parent dd1167d736
commit 7e44034757
10 changed files with 31 additions and 21 deletions
-2
View File
@@ -36,13 +36,11 @@ Audio::PoolThread::PoolThread(Pool *pool)
: pool(pool)
, finish(false)
{
mutex = thread::newMutex();
threadName = "AudioPool";
}
Audio::PoolThread::~PoolThread()
{
delete mutex;
}
+1 -1
View File
@@ -126,7 +126,7 @@ private:
volatile bool finish;
// finish lock
thread::Mutex *mutex;
love::thread::MutexRef mutex;
public:
PoolThread(Pool *pool);
-6
View File
@@ -32,7 +32,6 @@ namespace openal
Pool::Pool()
: sources()
, totalSources(0)
, mutex(nullptr)
{
// Clear errors.
alGetError();
@@ -53,9 +52,6 @@ Pool::Pool()
if (totalSources < 4)
throw love::Exception("Could not generate sources.");
// Create the mutex.
mutex = thread::newMutex();
#ifdef AL_SOFT_direct_channels
ALboolean hasext = alIsExtensionPresent("AL_SOFT_direct_channels");
#endif
@@ -79,8 +75,6 @@ Pool::~Pool()
{
stop();
delete mutex;
// Free all sources.
alDeleteSources(totalSources, sources);
}
+1 -1
View File
@@ -124,7 +124,7 @@ private:
// Only one thread can access this object at the same time. This mutex will
// make sure of that.
thread::Mutex *mutex;
love::thread::MutexRef mutex;
}; // Pool
-6
View File
@@ -81,14 +81,8 @@ Message *Message::fromLua(lua_State *L, int n)
return new Message(name, vargs);
}
Event::Event()
{
mutex = thread::newMutex();
}
Event::~Event()
{
delete mutex;
}
void Event::push(Message *msg)
+1 -2
View File
@@ -59,7 +59,6 @@ private:
class Event : public Module
{
public:
Event();
virtual ~Event();
// Implements Module.
@@ -73,7 +72,7 @@ public:
virtual Message *wait() = 0;
protected:
thread::Mutex *mutex;
love::thread::MutexRef mutex;
std::queue<Message *> queue;
}; // Event
-2
View File
@@ -30,12 +30,10 @@ namespace image
ImageData::ImageData()
: data(nullptr)
{
mutex = thread::newMutex();
}
ImageData::~ImageData()
{
delete mutex;
}
size_t ImageData::getSize() const
+1 -1
View File
@@ -146,7 +146,7 @@ protected:
// We need to be thread-safe
// so we lock when we're accessing our
// data
Mutex *mutex;
love::thread::MutexRef mutex;
private:
+15
View File
@@ -104,5 +104,20 @@ const char *Threadable::getThreadName() const
return threadName.empty() ? nullptr : threadName.c_str();
}
MutexRef::MutexRef()
: mutex(newMutex())
{
}
MutexRef::~MutexRef()
{
delete mutex;
}
MutexRef::operator Mutex*() const
{
return mutex;
}
} // thread
} // love
+12
View File
@@ -96,6 +96,18 @@ protected:
};
class MutexRef
{
public:
MutexRef();
~MutexRef();
operator Mutex*() const;
private:
Mutex *mutex;
};
Mutex *newMutex();
Conditional *newConditional();
Thread *newThread(Threadable *t);