2007-02-07 17:07:31 +03:00
|
|
|
/*
|
2009-12-01 19:32:48 +03:00
|
|
|
* Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de.
|
2011-01-11 00:54:38 +03:00
|
|
|
* Copyright 2005-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
|
2008-07-07 22:12:33 +04:00
|
|
|
*
|
2005-01-30 19:09:06 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef KERNEL_UTIL_AUTO_LOCKER_H
|
|
|
|
#define KERNEL_UTIL_AUTO_LOCKER_H
|
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
|
2007-08-09 23:47:22 +04:00
|
|
|
#include <KernelExport.h>
|
|
|
|
|
2007-03-27 16:05:33 +04:00
|
|
|
#include <shared/AutoLocker.h>
|
2005-01-30 19:09:06 +03:00
|
|
|
|
2007-08-09 23:47:22 +04:00
|
|
|
#include <int.h>
|
|
|
|
#include <lock.h>
|
2008-10-17 01:41:11 +04:00
|
|
|
#include <thread.h>
|
2007-08-09 23:47:22 +04:00
|
|
|
|
2007-02-07 17:07:31 +03:00
|
|
|
|
2005-01-30 19:09:06 +03:00
|
|
|
namespace BPrivate {
|
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2005-01-30 19:09:06 +03:00
|
|
|
class MutexLocking {
|
|
|
|
public:
|
|
|
|
inline bool Lock(mutex *lockable)
|
|
|
|
{
|
2007-02-07 17:07:31 +03:00
|
|
|
return mutex_lock(lockable) == B_OK;
|
2005-01-30 19:09:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(mutex *lockable)
|
|
|
|
{
|
|
|
|
mutex_unlock(lockable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<mutex, MutexLocking> MutexLocker;
|
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2005-01-30 19:09:06 +03:00
|
|
|
class RecursiveLockLocking {
|
|
|
|
public:
|
|
|
|
inline bool Lock(recursive_lock *lockable)
|
|
|
|
{
|
2007-02-07 17:07:31 +03:00
|
|
|
return recursive_lock_lock(lockable) == B_OK;
|
2005-01-30 19:09:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(recursive_lock *lockable)
|
|
|
|
{
|
|
|
|
recursive_lock_unlock(lockable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<recursive_lock, RecursiveLockLocking> RecursiveLocker;
|
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2008-07-07 22:12:33 +04:00
|
|
|
class ReadWriteLockReadLocking {
|
|
|
|
public:
|
|
|
|
inline bool Lock(rw_lock *lockable)
|
|
|
|
{
|
|
|
|
return rw_lock_read_lock(lockable) == B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(rw_lock *lockable)
|
|
|
|
{
|
|
|
|
rw_lock_read_unlock(lockable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadWriteLockWriteLocking {
|
|
|
|
public:
|
|
|
|
inline bool Lock(rw_lock *lockable)
|
|
|
|
{
|
|
|
|
return rw_lock_write_lock(lockable) == B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(rw_lock *lockable)
|
|
|
|
{
|
|
|
|
rw_lock_write_unlock(lockable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<rw_lock, ReadWriteLockReadLocking> ReadLocker;
|
|
|
|
typedef AutoLocker<rw_lock, ReadWriteLockWriteLocking> WriteLocker;
|
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2007-08-09 23:47:22 +04:00
|
|
|
class InterruptsLocking {
|
|
|
|
public:
|
|
|
|
inline bool Lock(int* lockable)
|
|
|
|
{
|
|
|
|
*lockable = disable_interrupts();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(int* lockable)
|
|
|
|
{
|
|
|
|
restore_interrupts(*lockable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2007-08-28 06:23:11 +04:00
|
|
|
class InterruptsLocker : public AutoLocker<int, InterruptsLocking> {
|
2007-08-09 23:47:22 +04:00
|
|
|
public:
|
|
|
|
inline InterruptsLocker(bool alreadyLocked = false,
|
|
|
|
bool lockIfNotLocked = true)
|
|
|
|
: AutoLocker<int, InterruptsLocking>(&fState, alreadyLocked,
|
|
|
|
lockIfNotLocked)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fState;
|
|
|
|
};
|
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2007-08-09 23:47:22 +04:00
|
|
|
class SpinLocking {
|
|
|
|
public:
|
|
|
|
inline bool Lock(spinlock* lockable)
|
|
|
|
{
|
|
|
|
acquire_spinlock(lockable);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(spinlock* lockable)
|
|
|
|
{
|
|
|
|
release_spinlock(lockable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<spinlock, SpinLocking> SpinLocker;
|
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2007-08-28 06:23:11 +04:00
|
|
|
class InterruptsSpinLocking {
|
|
|
|
public:
|
2009-06-23 05:32:46 +04:00
|
|
|
// NOTE: work-around for annoying GCC 4 "fState may be used uninitialized"
|
|
|
|
// warning.
|
|
|
|
#if __GNUC__ == 4
|
|
|
|
InterruptsSpinLocking()
|
|
|
|
:
|
|
|
|
fState(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
2007-08-28 06:23:11 +04:00
|
|
|
|
|
|
|
inline bool Lock(spinlock* lockable)
|
|
|
|
{
|
|
|
|
fState = disable_interrupts();
|
|
|
|
acquire_spinlock(lockable);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(spinlock* lockable)
|
|
|
|
{
|
|
|
|
release_spinlock(lockable);
|
|
|
|
restore_interrupts(fState);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fState;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<spinlock, InterruptsSpinLocking> InterruptsSpinLocker;
|
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2013-11-05 07:16:13 +04:00
|
|
|
class WriteSequentialLocking {
|
|
|
|
public:
|
|
|
|
inline bool Lock(seqlock* lockable)
|
|
|
|
{
|
|
|
|
acquire_write_seqlock(lockable);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(seqlock* lockable)
|
|
|
|
{
|
|
|
|
release_write_seqlock(lockable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<seqlock, WriteSequentialLocking> WriteSequentialLocker;
|
|
|
|
|
|
|
|
|
|
|
|
class InterruptsWriteSequentialLocking {
|
|
|
|
public:
|
|
|
|
InterruptsWriteSequentialLocking()
|
|
|
|
:
|
|
|
|
fState(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Lock(seqlock* lockable)
|
|
|
|
{
|
|
|
|
fState = disable_interrupts();
|
|
|
|
acquire_write_seqlock(lockable);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(seqlock* lockable)
|
|
|
|
{
|
|
|
|
release_write_seqlock(lockable);
|
|
|
|
restore_interrupts(fState);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fState;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<seqlock, InterruptsWriteSequentialLocking>
|
|
|
|
InterruptsWriteSequentialLocker;
|
|
|
|
|
|
|
|
|
2008-10-17 01:41:11 +04:00
|
|
|
class ThreadCPUPinLocking {
|
|
|
|
public:
|
2011-01-11 00:54:38 +03:00
|
|
|
inline bool Lock(Thread* thread)
|
2008-10-17 01:41:11 +04:00
|
|
|
{
|
|
|
|
thread_pin_to_current_cpu(thread);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-11 00:54:38 +03:00
|
|
|
inline void Unlock(Thread* thread)
|
2008-10-17 01:41:11 +04:00
|
|
|
{
|
|
|
|
thread_unpin_from_current_cpu(thread);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-01-11 00:54:38 +03:00
|
|
|
typedef AutoLocker<Thread, ThreadCPUPinLocking> ThreadCPUPinner;
|
2008-10-17 01:41:11 +04:00
|
|
|
|
2009-12-01 19:32:48 +03:00
|
|
|
|
2011-06-12 04:00:23 +04:00
|
|
|
typedef AutoLocker<Team> TeamLocker;
|
|
|
|
typedef AutoLocker<Thread> ThreadLocker;
|
|
|
|
|
|
|
|
|
2005-01-30 19:09:06 +03:00
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
using BPrivate::AutoLocker;
|
|
|
|
using BPrivate::MutexLocker;
|
|
|
|
using BPrivate::RecursiveLocker;
|
2008-07-07 22:12:33 +04:00
|
|
|
using BPrivate::ReadLocker;
|
|
|
|
using BPrivate::WriteLocker;
|
2007-08-09 23:47:22 +04:00
|
|
|
using BPrivate::InterruptsLocker;
|
|
|
|
using BPrivate::SpinLocker;
|
2007-08-28 06:23:11 +04:00
|
|
|
using BPrivate::InterruptsSpinLocker;
|
2013-11-05 07:16:13 +04:00
|
|
|
using BPrivate::WriteSequentialLocker;
|
|
|
|
using BPrivate::InterruptsWriteSequentialLocker;
|
2008-10-17 01:41:11 +04:00
|
|
|
using BPrivate::ThreadCPUPinner;
|
2011-06-12 04:00:23 +04:00
|
|
|
using BPrivate::TeamLocker;
|
|
|
|
using BPrivate::ThreadLocker;
|
|
|
|
|
2005-01-30 19:09:06 +03:00
|
|
|
|
|
|
|
#endif // KERNEL_UTIL_AUTO_LOCKER_H
|