Thread secured refcount class.
This commit is contained in:
parent
61dbb4a2ec
commit
5f93e157d5
|
@ -25,6 +25,8 @@
|
||||||
#ifndef SORTIX_REFCOUNT_H
|
#ifndef SORTIX_REFCOUNT_H
|
||||||
#define SORTIX_REFCOUNT_H
|
#define SORTIX_REFCOUNT_H
|
||||||
|
|
||||||
|
#include <sortix/kernel/kthread.h>
|
||||||
|
|
||||||
namespace Sortix {
|
namespace Sortix {
|
||||||
|
|
||||||
class Refcounted
|
class Refcounted
|
||||||
|
@ -39,6 +41,7 @@ public:
|
||||||
inline size_t Refcount() const { return refcount; }
|
inline size_t Refcount() const { return refcount; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
kthread_mutex_t reflock;
|
||||||
size_t refcount;
|
size_t refcount;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -23,12 +23,14 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
#include <sortix/kernel/platform.h>
|
#include <sortix/kernel/platform.h>
|
||||||
|
#include <sortix/kernel/kthread.h>
|
||||||
#include <sortix/kernel/refcount.h>
|
#include <sortix/kernel/refcount.h>
|
||||||
|
|
||||||
namespace Sortix {
|
namespace Sortix {
|
||||||
|
|
||||||
Refcounted::Refcounted()
|
Refcounted::Refcounted()
|
||||||
{
|
{
|
||||||
|
reflock = KTHREAD_MUTEX_INITIALIZER;
|
||||||
refcount = 1;
|
refcount = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,12 +43,16 @@ Refcounted::~Refcounted()
|
||||||
|
|
||||||
void Refcounted::Refer()
|
void Refcounted::Refer()
|
||||||
{
|
{
|
||||||
|
ScopedLock lock(&reflock);
|
||||||
refcount++;
|
refcount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Refcounted::Unref()
|
void Refcounted::Unref()
|
||||||
{
|
{
|
||||||
if ( !--refcount )
|
kthread_mutex_lock(&reflock);
|
||||||
|
bool deleteme = !--refcount;
|
||||||
|
kthread_mutex_unlock(&reflock);
|
||||||
|
if ( deleteme )
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue