Fixed a big stupid bug that can cause lots of trouble: all locks created

weren't moved to the kernel (as owner), so they would be deleted after
the team which almost randomly created them died.
Benaphores are now only used if USE_BENAPHORES is defined - I have renamed
the Benaphore class to Semaphore to take the changed behaviour into account.
Added a Status() method to the Locker class.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1885 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2002-11-09 02:02:31 +00:00
parent ce088d7b5c
commit dab62fa391
5 changed files with 55 additions and 18 deletions
@@ -47,7 +47,7 @@ class BlockAllocator {
static status_t initialize(BlockAllocator *); static status_t initialize(BlockAllocator *);
Volume *fVolume; Volume *fVolume;
Benaphore fLock; Semaphore fLock;
AllocationGroup *fGroups; AllocationGroup *fGroups;
int32 fNumGroups, fBlocksPerGroup; int32 fNumGroups, fBlocksPerGroup;
uint32 *fCheckBitmap; uint32 *fCheckBitmap;
@@ -20,6 +20,11 @@ BufferPool::BufferPool()
{ {
fLock = create_sem(1, "buffer lock"); fLock = create_sem(1, "buffer lock");
fFreeBuffers = create_sem(0, "free buffers"); fFreeBuffers = create_sem(0, "free buffers");
#ifndef USER
set_sem_owner(fLock, B_SYSTEM_TEAM);
set_sem_owner(fFreeBuffers, B_SYSTEM_TEAM);
#endif
} }
@@ -69,7 +69,7 @@ class Journal {
status_t TransactionDone(bool success); status_t TransactionDone(bool success);
Volume *fVolume; Volume *fVolume;
Benaphore fLock; Semaphore fLock;
Transaction *fOwner; Transaction *fOwner;
thread_id fOwningThread; thread_id fOwningThread;
BlockArray fArray; BlockArray fArray;
+44 -12
View File
@@ -1,6 +1,6 @@
#ifndef LOCK_H #ifndef LOCK_H
#define LOCK_H #define LOCK_H
/* Lock - benaphores, read/write lock implementation /* Lock - simple semaphores, read/write lock implementation
** **
** Initial version by Axel Dörfler, [email protected] ** Initial version by Axel Dörfler, [email protected]
** Roughly based on a Be sample code written by Nathan Schrenk. ** Roughly based on a Be sample code written by Nathan Schrenk.
@@ -12,16 +12,27 @@
#include <KernelExport.h> #include <KernelExport.h>
class Benaphore { // configure here if and when real benaphores should be used
#ifdef USER
# define USE_BENAPHORE 1
#endif
class Semaphore {
public: public:
Benaphore(const char *name = "bfs benaphore") Semaphore(const char *name = "bfs sem")
: :
fSemaphore(create_sem(0, name)), fSemaphore(create_sem(0, name))
fCount(1) #ifdef USE_BENAPHORE
, fCount(1)
#endif
{ {
#ifndef USER
set_sem_owner(fSemaphore, B_SYSTEM_TEAM);
#endif
} }
~Benaphore() ~Semaphore()
{ {
delete_sem(fSemaphore); delete_sem(fSemaphore);
} }
@@ -36,28 +47,38 @@ class Benaphore {
status_t Lock() status_t Lock()
{ {
#ifdef USE_BENAPHORE
if (atomic_add(&fCount, -1) <= 0) if (atomic_add(&fCount, -1) <= 0)
#endif
return acquire_sem(fSemaphore); return acquire_sem(fSemaphore);
#ifdef USE_BENAPHORE
return B_OK; return B_OK;
#endif
} }
void Unlock() status_t Unlock()
{ {
#ifdef USE_BENAPHORE
if (atomic_add(&fCount, 1) < 0) if (atomic_add(&fCount, 1) < 0)
release_sem(fSemaphore); #endif
return release_sem(fSemaphore);
#ifdef USE_BENAPHORE
return B_OK;
#endif
} }
private: private:
sem_id fSemaphore; sem_id fSemaphore;
#ifdef USE_BENAPHORE
vint32 fCount; vint32 fCount;
#endif
}; };
// a convenience class to lock the benaphore // a convenience class to lock the benaphore
class Locker { class Locker {
public: public:
Locker(Benaphore &lock) Locker(Semaphore &lock)
: fLock(lock) : fLock(lock)
{ {
fStatus = lock.Lock(); fStatus = lock.Lock();
@@ -69,8 +90,13 @@ class Locker {
fLock.Unlock(); fLock.Unlock();
} }
status_t Status() const
{
return fStatus;
}
private: private:
Benaphore &fLock; Semaphore &fLock;
status_t fStatus; status_t fStatus;
}; };
@@ -118,6 +144,9 @@ class ReadWriteLock {
fCount(MAX_READERS), fCount(MAX_READERS),
fWriteLock() fWriteLock()
{ {
#ifndef USER
set_sem_owner(fSemaphore, B_SYSTEM_TEAM);
#endif
} }
~ReadWriteLock() ~ReadWriteLock()
@@ -181,7 +210,7 @@ class ReadWriteLock {
sem_id fSemaphore; sem_id fSemaphore;
vint32 fCount; vint32 fCount;
Benaphore fWriteLock; Semaphore fWriteLock;
}; };
#else // FAST_LOCK #else // FAST_LOCK
class ReadWriteLock { class ReadWriteLock {
@@ -190,6 +219,9 @@ class ReadWriteLock {
: :
fSemaphore(create_sem(MAX_READERS, name)) fSemaphore(create_sem(MAX_READERS, name))
{ {
#ifndef USER
set_sem_owner(fSemaphore, B_SYSTEM_TEAM);
#endif
} }
~ReadWriteLock() ~ReadWriteLock()
+3 -3
View File
@@ -43,7 +43,7 @@ class Volume {
bool IsValidSuperBlock(); bool IsValidSuperBlock();
bool IsReadOnly() const; bool IsReadOnly() const;
void Panic(); void Panic();
Benaphore &Lock(); Semaphore &Lock();
block_run Root() const { return fSuperBlock.root_dir; } block_run Root() const { return fSuperBlock.root_dir; }
Inode *RootNode() const { return fRootNode; } Inode *RootNode() const { return fRootNode; }
@@ -114,7 +114,7 @@ class Volume {
int fDevice; int fDevice;
disk_super_block fSuperBlock; disk_super_block fSuperBlock;
BlockAllocator fBlockAllocator; BlockAllocator fBlockAllocator;
Benaphore fLock; Semaphore fLock;
Journal *fJournal; Journal *fJournal;
vint32 fLogStart, fLogEnd; vint32 fLogStart, fLogEnd;
@@ -142,7 +142,7 @@ Volume::IsReadOnly() const
} }
inline Benaphore & inline Semaphore &
Volume::Lock() Volume::Lock()
{ {
return fLock; return fLock;