Add a mutex around Lime Bytes

This commit is contained in:
Joshua Granick
2018-11-05 11:27:36 -08:00
parent c7b96e0be8
commit ab4dcf243a

View File

@@ -1,3 +1,4 @@
#include <system/Mutex.h>
#include <system/System.h>
#include <utils/Bytes.h>
#include <map>
@@ -12,6 +13,7 @@ namespace lime {
static bool useBuffer = false;
static std::map<Bytes*, bool> hadValue;
static std::map<Bytes*, bool> usingValue;
static Mutex mutex;
inline void _initializeBytes () {
@@ -60,6 +62,8 @@ namespace lime {
Bytes::~Bytes () {
mutex.Lock ();
if (hadValue.find (this) != hadValue.end ()) {
hadValue.erase (this);
@@ -78,6 +82,8 @@ namespace lime {
}
mutex.Unlock ();
}
@@ -111,6 +117,8 @@ namespace lime {
if (size != length || (length > 0 && !b)) {
mutex.Lock ();
if (size <= 0) {
if (b) {
@@ -159,6 +167,8 @@ namespace lime {
}
mutex.Unlock ();
}
}
@@ -166,6 +176,8 @@ namespace lime {
void Bytes::Set (value bytes) {
mutex.Lock ();
if (val_is_null (bytes)) {
if (usingValue.find (this) != usingValue.end ()) {
@@ -206,6 +218,8 @@ namespace lime {
}
mutex.Unlock ();
}
@@ -220,12 +234,16 @@ namespace lime {
} else {
mutex.Lock ();
if (usingValue.find (this) != usingValue.end ()) {
usingValue.erase (this);
}
mutex.Unlock ();
b = 0;
length = 0;
@@ -278,7 +296,9 @@ namespace lime {
}
mutex.Lock ();
hadValue[this] = true;
mutex.Unlock ();
return bytes;