mirror of
https://github.com/love2d/love.git
synced 2026-08-14 10:13:46 +02:00
Add a MutexRef class to clean up Mutex handling code
This commit is contained in:
@@ -36,13 +36,11 @@ Audio::PoolThread::PoolThread(Pool *pool)
|
||||
: pool(pool)
|
||||
, finish(false)
|
||||
{
|
||||
mutex = thread::newMutex();
|
||||
threadName = "AudioPool";
|
||||
}
|
||||
|
||||
Audio::PoolThread::~PoolThread()
|
||||
{
|
||||
delete mutex;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ private:
|
||||
volatile bool finish;
|
||||
|
||||
// finish lock
|
||||
thread::Mutex *mutex;
|
||||
love::thread::MutexRef mutex;
|
||||
|
||||
public:
|
||||
PoolThread(Pool *pool);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -30,12 +30,10 @@ namespace image
|
||||
ImageData::ImageData()
|
||||
: data(nullptr)
|
||||
{
|
||||
mutex = thread::newMutex();
|
||||
}
|
||||
|
||||
ImageData::~ImageData()
|
||||
{
|
||||
delete mutex;
|
||||
}
|
||||
|
||||
size_t ImageData::getSize() const
|
||||
|
||||
@@ -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:
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -96,6 +96,18 @@ protected:
|
||||
|
||||
};
|
||||
|
||||
class MutexRef
|
||||
{
|
||||
public:
|
||||
MutexRef();
|
||||
~MutexRef();
|
||||
|
||||
operator Mutex*() const;
|
||||
|
||||
private:
|
||||
Mutex *mutex;
|
||||
};
|
||||
|
||||
Mutex *newMutex();
|
||||
Conditional *newConditional();
|
||||
Thread *newThread(Threadable *t);
|
||||
|
||||
Reference in New Issue
Block a user