mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:27:45 +00:00
Kernel: Introduce contended and locked resource concepts
This commit is contained in:
parent
aea98a85d1
commit
3d684316c2
1 changed files with 53 additions and 0 deletions
53
Kernel/Locking/ContendedResource.h
Normal file
53
Kernel/Locking/ContendedResource.h
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, the SerenityOS developers.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/StdLibExtras.h>
|
||||||
|
#include <Kernel/Locking/Mutex.h>
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
template<typename T, LockMode LockingMode>
|
||||||
|
class LockedResource {
|
||||||
|
AK_MAKE_NONCOPYABLE(LockedResource);
|
||||||
|
|
||||||
|
public:
|
||||||
|
LockedResource(T* value, Mutex& mutex)
|
||||||
|
: m_value(value)
|
||||||
|
, m_mutex_locker(mutex, LockingMode)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ALWAYS_INLINE T const* operator->() const { return m_value; }
|
||||||
|
ALWAYS_INLINE T const& operator*() const { return *m_value; }
|
||||||
|
|
||||||
|
ALWAYS_INLINE T* operator->() requires(!IsConst<T>) { return m_value; }
|
||||||
|
ALWAYS_INLINE T& operator*() requires(!IsConst<T>) { return *m_value; }
|
||||||
|
|
||||||
|
ALWAYS_INLINE T const* get() const { return m_value; }
|
||||||
|
ALWAYS_INLINE T* get() requires(!IsConst<T>) { return m_value; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
T* m_value;
|
||||||
|
MutexLocker m_mutex_locker;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ContendedResource {
|
||||||
|
template<typename, LockMode>
|
||||||
|
friend class LockedResource;
|
||||||
|
|
||||||
|
AK_MAKE_NONCOPYABLE(ContendedResource);
|
||||||
|
AK_MAKE_NONMOVABLE(ContendedResource);
|
||||||
|
|
||||||
|
public:
|
||||||
|
ContendedResource() = default;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
mutable Mutex m_mutex;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue