From b419f3e159212c9998b3f86c1fee332620472f4e Mon Sep 17 00:00:00 2001 From: Pixel Date: Mon, 5 Dec 2011 00:44:55 -0800 Subject: Making the ref counter on Handles thread-safe. Not that it renders the usage of Handles thread-safe, but at least one can now transmit Handles over threads safely without risking a refcount bug. --- includes/Handle.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'includes/Handle.h') diff --git a/includes/Handle.h b/includes/Handle.h index e3ff356..1b04556 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -3,6 +3,7 @@ #include #include #include +#include namespace Balau { @@ -57,9 +58,9 @@ class Handle { protected: Handle() : m_refCount(0) { } private: - void addRef() { m_refCount++; } + void addRef() { Atomic::Increment(&m_refCount); } void delRef() { - if (--m_refCount == 0) { + if (Atomic::Decrement(&m_refCount) == 0) { if (!isClosed()) close(); delete this; @@ -69,7 +70,7 @@ class Handle { template friend class IO; - int m_refCount; + volatile int m_refCount; }; class HPrinter : public Handle { -- cgit v1.2.3