haiku/headers/private/kernel/util/AutoLock.h
Augustin Cavalier 057fe1910d kernel: Break thread-related AutoLockers into a separate header.
Including thread.h brings a massive array of things with it from
the kernel thread arch headers, team and thread definitions,
hash tables, linked lists, Referenceable, etc. that the vast majority
of AutoLock.h consumers neither want nor need.

So, put these in a separate header, and adjust all consumers of these
lockers to include the new file.

This change exposes the fact that a lot of files were inadvertently
making use of headers included indirectly through thread.h. Those
will be fixed in the next commit.
2021-09-01 13:08:49 -04:00

319 lines
5.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>
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 ReadSpinLocking {
public:
inline bool Lock(rw_spinlock* lockable)
{
acquire_read_spinlock(lockable);
return true;
}
inline void Unlock(rw_spinlock* lockable)
{
release_read_spinlock(lockable);
}
};
typedef AutoLocker<rw_spinlock, ReadSpinLocking> ReadSpinLocker;
class InterruptsReadSpinLocking {
public:
InterruptsReadSpinLocking()
:
fState(0)
{
}
inline bool Lock(rw_spinlock* lockable)
{
fState = disable_interrupts();
acquire_read_spinlock(lockable);
return true;
}
inline void Unlock(rw_spinlock* lockable)
{
release_read_spinlock(lockable);
restore_interrupts(fState);
}
private:
int fState;
};
typedef AutoLocker<rw_spinlock, InterruptsReadSpinLocking>
InterruptsReadSpinLocker;
class WriteSpinLocking {
public:
inline bool Lock(rw_spinlock* lockable)
{
acquire_write_spinlock(lockable);
return true;
}
inline void Unlock(rw_spinlock* lockable)
{
release_write_spinlock(lockable);
}
};
typedef AutoLocker<rw_spinlock, WriteSpinLocking> WriteSpinLocker;
class InterruptsWriteSpinLocking {
public:
InterruptsWriteSpinLocking()
:
fState(0)
{
}
inline bool Lock(rw_spinlock* lockable)
{
fState = disable_interrupts();
acquire_write_spinlock(lockable);
return true;
}
inline void Unlock(rw_spinlock* lockable)
{
release_write_spinlock(lockable);
restore_interrupts(fState);
}
private:
int fState;
};
typedef AutoLocker<rw_spinlock, InterruptsWriteSpinLocking>
InterruptsWriteSpinLocker;
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;
} // 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::ReadSpinLocker;
using BPrivate::InterruptsReadSpinLocker;
using BPrivate::WriteSpinLocker;
using BPrivate::InterruptsWriteSpinLocker;
using BPrivate::WriteSequentialLocker;
using BPrivate::InterruptsWriteSequentialLocker;
#endif // KERNEL_UTIL_AUTO_LOCKER_H