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; }