From 811f574ef600ec908252e9bb8e7489897a852270 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 14 Jan 2015 15:52:05 -0400 Subject: [PATCH] Reference counting for love objects in 0.9.2 is now atomic (using SDL's atomic operation functions.) --- src/common/Object.cpp | 12 +++++------- src/common/Object.h | 11 +++++++++-- src/modules/math/wrap_Math.cpp | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/common/Object.cpp b/src/common/Object.cpp index c821569ba..291ae1d71 100644 --- a/src/common/Object.cpp +++ b/src/common/Object.cpp @@ -21,33 +21,31 @@ // LOVE #include "Object.h" -#include - namespace love { Object::Object() - : count(1) { + count.value = 1; } Object::~Object() { } -int Object::getReferenceCount() const +int Object::getReferenceCount() { - return count; + return SDL_AtomicGet(&count); } void Object::retain() { - ++count; + SDL_AtomicIncRef(&count); } void Object::release() { - if (--count <= 0) + if (SDL_AtomicDecRef(&count)) delete this; } diff --git a/src/common/Object.h b/src/common/Object.h index d362c5ce2..3340c609f 100644 --- a/src/common/Object.h +++ b/src/common/Object.h @@ -21,6 +21,13 @@ #ifndef LOVE_OBJECT_H #define LOVE_OBJECT_H +/** + * NOTE: the fact that an SDL header is included in such a widely used header + * file is only temporary - in the LOVE 0.10+ codebase we use atomics from + * C++11's standard library. + **/ +#include + namespace love { @@ -50,7 +57,7 @@ public: * Gets the reference count of this Object. * @returns The reference count. **/ - int getReferenceCount() const; + int getReferenceCount(); /** * Retains the Object, i.e. increases the @@ -155,7 +162,7 @@ public: private: // The reference count. - int count; + SDL_atomic_t count; }; // Object diff --git a/src/modules/math/wrap_Math.cpp b/src/modules/math/wrap_Math.cpp index 1e766d620..047699865 100644 --- a/src/modules/math/wrap_Math.cpp +++ b/src/modules/math/wrap_Math.cpp @@ -247,7 +247,7 @@ int w_isConvex(lua_State *L) } } - lua_pushboolean(L, Math::instance.isConvex(vertices)); + luax_pushboolean(L, Math::instance.isConvex(vertices)); return 1; }