haiku/headers/private/kernel/util/AutoLock.h
Ingo Weinhold 24df65921b Merged signals-merge branch into trunk with the following changes:
* Reorganized the kernel locking related to threads and teams.
* We now discriminate correctly between process and thread signals. Signal
  handlers have been moved to teams. Fixes #5679.
* Implemented real-time signal support, including signal queuing, SA_SIGINFO
  support, sigqueue(), sigwaitinfo(), sigtimedwait(), waitid(), and the addition
  of the real-time signal range. Closes #1935 and #2695.
* Gave SIGBUS a separate signal number. Fixes #6704.
* Implemented <time.h> clock and timer support, and fixed/completed alarm() and
  [set]itimer(). Closes #5682.
* Implemented support for thread cancellation. Closes #5686.
* Moved send_signal() from <signal.h> to <OS.h>. Fixes #7554.
* Lots over smaller more or less related changes.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42116 a95241bf-73f2-0310-859d-f6bbb57e9c96
2011-06-12 00:00:23 +00:00

200 lines
3.4 KiB
C++

/*
* Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de.
* Copyright 2005-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
*
* Distributed under the terms of the MIT License.
*/
#ifndef KERNEL_UTIL_AUTO_LOCKER_H
#define KERNEL_UTIL_AUTO_LOCKER_H
#include <KernelExport.h>
#include <shared/AutoLocker.h>
#include <int.h>
#include <lock.h>
#include <thread.h>
namespace BPrivate {
class MutexLocking {
public:
inline bool Lock(mutex *lockable)
{
return mutex_lock(lockable) == B_OK;
}
inline void Unlock(mutex *lockable)
{
mutex_unlock(lockable);
}
};
typedef AutoLocker<mutex, MutexLocking> MutexLocker;
class RecursiveLockLocking {
public:
inline bool Lock(recursive_lock *lockable)
{
return recursive_lock_lock(lockable) == B_OK;
}
inline void Unlock(recursive_lock *lockable)
{
recursive_lock_unlock(lockable);
}
};
typedef AutoLocker<recursive_lock, RecursiveLockLocking> RecursiveLocker;
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;
class InterruptsLocking {
public:
inline bool Lock(int* lockable)
{
*lockable = disable_interrupts();
return true;
}
inline void Unlock(int* lockable)
{
restore_interrupts(*lockable);
}
};
class InterruptsLocker : public AutoLocker<int, InterruptsLocking> {
public:
inline InterruptsLocker(bool alreadyLocked = false,
bool lockIfNotLocked = true)
: AutoLocker<int, InterruptsLocking>(&fState, alreadyLocked,
lockIfNotLocked)
{
}
private:
int fState;
};
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;
class InterruptsSpinLocking {
public:
// NOTE: work-around for annoying GCC 4 "fState may be used uninitialized"
// warning.
#if __GNUC__ == 4
InterruptsSpinLocking()
:
fState(0)
{
}
#endif
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;
class ThreadCPUPinLocking {
public:
inline bool Lock(Thread* thread)
{
thread_pin_to_current_cpu(thread);
return true;
}
inline void Unlock(Thread* thread)
{
thread_unpin_from_current_cpu(thread);
}
};
typedef AutoLocker<Thread, ThreadCPUPinLocking> ThreadCPUPinner;
typedef AutoLocker<Team> TeamLocker;
typedef AutoLocker<Thread> ThreadLocker;
} // namespace BPrivate
using BPrivate::AutoLocker;
using BPrivate::MutexLocker;
using BPrivate::RecursiveLocker;
using BPrivate::ReadLocker;
using BPrivate::WriteLocker;
using BPrivate::InterruptsLocker;
using BPrivate::SpinLocker;
using BPrivate::InterruptsSpinLocker;
using BPrivate::ThreadCPUPinner;
using BPrivate::TeamLocker;
using BPrivate::ThreadLocker;
#endif // KERNEL_UTIL_AUTO_LOCKER_H