2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-23 23:32:53 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-16 13:18:22 +01:00
|
|
|
#include <AK/Assertions.h>
|
2019-10-12 11:17:34 -06:00
|
|
|
#include <AK/Atomic.h>
|
2019-12-01 11:57:20 +01:00
|
|
|
#include <AK/Types.h>
|
2019-06-07 20:02:01 +02:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-05-14 11:50:15 +02:00
|
|
|
#include <Kernel/KSyms.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/Scheduler.h>
|
2019-12-01 11:57:20 +01:00
|
|
|
#include <Kernel/WaitQueue.h>
|
2019-01-16 16:03:50 +01:00
|
|
|
|
2019-03-23 22:03:17 +01:00
|
|
|
class Thread;
|
|
|
|
extern Thread* current;
|
2019-01-16 16:03:50 +01:00
|
|
|
|
2019-01-17 16:25:02 +01:00
|
|
|
class Lock {
|
2018-10-23 23:32:53 +02:00
|
|
|
public:
|
2019-05-28 11:53:16 +02:00
|
|
|
Lock(const char* name = nullptr)
|
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~Lock() {}
|
2018-10-23 23:32:53 +02:00
|
|
|
|
2019-01-16 16:03:50 +01:00
|
|
|
void lock();
|
|
|
|
void unlock();
|
2020-01-12 22:53:20 +01:00
|
|
|
bool force_unlock_if_locked();
|
2019-12-26 11:43:23 +01:00
|
|
|
bool is_locked() const { return m_holder; }
|
2018-10-23 23:32:53 +02:00
|
|
|
|
2019-02-07 11:12:23 +01:00
|
|
|
const char* name() const { return m_name; }
|
|
|
|
|
2018-10-23 23:32:53 +02:00
|
|
|
private:
|
2019-10-12 11:17:34 -06:00
|
|
|
Atomic<bool> m_lock { false };
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 m_level { 0 };
|
2019-03-23 22:03:17 +01:00
|
|
|
Thread* m_holder { nullptr };
|
2019-02-07 11:12:23 +01:00
|
|
|
const char* m_name { nullptr };
|
2019-12-01 11:57:20 +01:00
|
|
|
WaitQueue m_queue;
|
2018-10-23 23:32:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Locker {
|
|
|
|
public:
|
2019-05-28 11:53:16 +02:00
|
|
|
[[gnu::always_inline]] inline explicit Locker(Lock& l)
|
|
|
|
: m_lock(l)
|
|
|
|
{
|
|
|
|
lock();
|
|
|
|
}
|
2019-02-20 13:09:59 +01:00
|
|
|
[[gnu::always_inline]] inline ~Locker() { unlock(); }
|
|
|
|
[[gnu::always_inline]] inline void unlock() { m_lock.unlock(); }
|
|
|
|
[[gnu::always_inline]] inline void lock() { m_lock.lock(); }
|
2018-10-23 23:32:53 +02:00
|
|
|
|
|
|
|
private:
|
2019-01-17 16:25:02 +01:00
|
|
|
Lock& m_lock;
|
2018-10-23 23:32:53 +02:00
|
|
|
};
|
|
|
|
|
2019-01-16 16:03:50 +01:00
|
|
|
#define LOCKER(lock) Locker locker(lock)
|
2018-10-29 21:54:11 +01:00
|
|
|
|
2019-02-08 09:46:13 +01:00
|
|
|
template<typename T>
|
|
|
|
class Lockable {
|
|
|
|
public:
|
2019-05-28 11:53:16 +02:00
|
|
|
Lockable() {}
|
|
|
|
Lockable(T&& resource)
|
|
|
|
: m_resource(move(resource))
|
|
|
|
{
|
|
|
|
}
|
2019-02-08 09:46:13 +01:00
|
|
|
Lock& lock() { return m_lock; }
|
|
|
|
T& resource() { return m_resource; }
|
|
|
|
|
2019-02-08 16:18:24 +01:00
|
|
|
T lock_and_copy()
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
return m_resource;
|
|
|
|
}
|
|
|
|
|
2019-02-08 09:46:13 +01:00
|
|
|
private:
|
|
|
|
T m_resource;
|
|
|
|
Lock m_lock;
|
|
|
|
};
|