mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Add Data:performAtomic to lock a mutex while using a Data object.
This commit is contained in:
@@ -20,10 +20,27 @@
|
||||
|
||||
// LOVE
|
||||
#include "Data.h"
|
||||
#include "thread/threads.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
|
||||
love::Type Data::type("Data", &Object::type);
|
||||
|
||||
Data::~Data()
|
||||
{
|
||||
delete mutex;
|
||||
}
|
||||
|
||||
static void createMutex(love::thread::Mutex **mutexAddress)
|
||||
{
|
||||
*mutexAddress = love::thread::newMutex();
|
||||
}
|
||||
|
||||
love::thread::Mutex *Data::getMutex()
|
||||
{
|
||||
std::call_once(mutexCreated, createMutex, &mutex);
|
||||
return mutex;
|
||||
}
|
||||
|
||||
} // love
|
||||
|
||||
+19
-5
@@ -22,15 +22,20 @@
|
||||
#define LOVE_DATA_H
|
||||
|
||||
// LOVE
|
||||
#include "config.h"
|
||||
#include "Object.h"
|
||||
|
||||
// C
|
||||
#include <stddef.h>
|
||||
#include <mutex>
|
||||
|
||||
namespace love
|
||||
{
|
||||
|
||||
namespace thread
|
||||
{
|
||||
class Mutex;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is a simple abstraction over all objects which contain data.
|
||||
**/
|
||||
@@ -40,10 +45,8 @@ public:
|
||||
|
||||
static love::Type type;
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
**/
|
||||
virtual ~Data() {}
|
||||
Data() {};
|
||||
virtual ~Data();
|
||||
|
||||
/**
|
||||
* Creates a duplicate of Data derived class instance.
|
||||
@@ -60,6 +63,17 @@ public:
|
||||
**/
|
||||
virtual size_t getSize() const = 0;
|
||||
|
||||
/**
|
||||
* Gets the Mutex associated with this Data object. Creates it in a thread-
|
||||
* safe manner if necessary.
|
||||
**/
|
||||
love::thread::Mutex *getMutex();
|
||||
|
||||
private:
|
||||
|
||||
love::thread::Mutex *mutex = nullptr;
|
||||
std::once_flag mutexCreated;
|
||||
|
||||
}; // Data
|
||||
|
||||
} // love
|
||||
|
||||
Reference in New Issue
Block a user