From 5f93e157d55d8cda399326257c90f17fbc13db0c Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 4 Aug 2012 13:50:21 +0200 Subject: [PATCH] Thread secured refcount class. --- sortix/include/sortix/kernel/refcount.h | 3 +++ sortix/refcount.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/sortix/include/sortix/kernel/refcount.h b/sortix/include/sortix/kernel/refcount.h index afa43fc4..0321bb25 100644 --- a/sortix/include/sortix/kernel/refcount.h +++ b/sortix/include/sortix/kernel/refcount.h @@ -25,6 +25,8 @@ #ifndef SORTIX_REFCOUNT_H #define SORTIX_REFCOUNT_H +#include + namespace Sortix { class Refcounted @@ -39,6 +41,7 @@ public: inline size_t Refcount() const { return refcount; } private: + kthread_mutex_t reflock; size_t refcount; }; diff --git a/sortix/refcount.cpp b/sortix/refcount.cpp index 66133d0d..575cfc8d 100644 --- a/sortix/refcount.cpp +++ b/sortix/refcount.cpp @@ -23,12 +23,14 @@ *******************************************************************************/ #include +#include #include namespace Sortix { Refcounted::Refcounted() { + reflock = KTHREAD_MUTEX_INITIALIZER; refcount = 1; } @@ -41,12 +43,16 @@ Refcounted::~Refcounted() void Refcounted::Refer() { + ScopedLock lock(&reflock); refcount++; } void Refcounted::Unref() { - if ( !--refcount ) + kthread_mutex_lock(&reflock); + bool deleteme = !--refcount; + kthread_mutex_unlock(&reflock); + if ( deleteme ) delete this; }