diff --git a/sortix/device.cpp b/sortix/device.cpp index 4e76025d..2c576647 100644 --- a/sortix/device.cpp +++ b/sortix/device.cpp @@ -1,6 +1,6 @@ -/****************************************************************************** +/******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. This file is part of Sortix. @@ -14,15 +14,16 @@ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along - with Sortix. If not, see . + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . device.cpp A base class for all devices. -******************************************************************************/ +*******************************************************************************/ #include +#include #include #include "device.h" @@ -30,6 +31,7 @@ namespace Sortix { Device::Device() { + refcountlock = KTHREAD_MUTEX_INITIALIZER; refcount = 0; } @@ -40,11 +42,17 @@ namespace Sortix void Device::Unref() { - if ( --refcount == 0 || refcount == SIZE_MAX ) { delete this; } + bool shoulddelete = false; + kthread_mutex_lock(&refcountlock); + shoulddelete = --refcount == 0 || refcount == SIZE_MAX; + kthread_mutex_unlock(&refcountlock); + if ( shoulddelete ) + delete this; } void Device::Refer() { + ScopedLock lock(&refcountlock); refcount++; } } diff --git a/sortix/device.h b/sortix/device.h index 07648954..65a4fdbf 100644 --- a/sortix/device.h +++ b/sortix/device.h @@ -1,6 +1,6 @@ -/****************************************************************************** +/******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. This file is part of Sortix. @@ -14,17 +14,19 @@ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along - with Sortix. If not, see . + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . device.h A base class for all devices. -******************************************************************************/ +*******************************************************************************/ #ifndef SORTIX_DEVICE_H #define SORTIX_DEVICE_H +#include + namespace Sortix { class Device @@ -42,6 +44,7 @@ namespace Sortix virtual ~Device(); private: + kthread_mutex_t refcountlock; size_t refcount; public: