Kernel: Introduce contended and locked resource concepts
This commit is contained in:
parent
aea98a85d1
commit
3d684316c2
Notes:
sideshowbarker
2024-07-18 07:20:48 +09:00
Author: https://github.com/boricj Commit: https://github.com/SerenityOS/serenity/commit/3d684316c2d Pull-request: https://github.com/SerenityOS/serenity/pull/8851 Reviewed-by: https://github.com/awesomekling ✅ Reviewed-by: https://github.com/bgianfo
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
Reference in a new issue