2019-03-25 12:03:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-22 19:21:57 +00:00
|
|
|
#ifdef __serenity__
|
|
|
|
|
2019-03-25 12:03:49 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Types.h>
|
2019-10-12 17:17:34 +00:00
|
|
|
#include <AK/Atomic.h>
|
2019-03-25 12:03:49 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
namespace LibThread {
|
|
|
|
|
|
|
|
class Lock {
|
2019-03-25 12:03:49 +00:00
|
|
|
public:
|
2019-08-25 20:51:27 +00:00
|
|
|
Lock() {}
|
|
|
|
~Lock() {}
|
2019-03-25 12:03:49 +00:00
|
|
|
|
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
private:
|
2019-10-12 17:17:34 +00:00
|
|
|
AK::Atomic<bool> m_lock { false };
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 m_level { 0 };
|
2019-03-25 12:03:49 +00:00
|
|
|
int m_holder { -1 };
|
|
|
|
};
|
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
class Locker {
|
2019-03-25 12:03:49 +00:00
|
|
|
public:
|
2019-08-25 20:51:27 +00:00
|
|
|
[[gnu::always_inline]] inline explicit Locker(Lock& l)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_lock(l)
|
|
|
|
{
|
|
|
|
lock();
|
|
|
|
}
|
2019-08-25 20:51:27 +00:00
|
|
|
[[gnu::always_inline]] inline ~Locker() { unlock(); }
|
2019-03-25 12:03:49 +00:00
|
|
|
[[gnu::always_inline]] inline void unlock() { m_lock.unlock(); }
|
|
|
|
[[gnu::always_inline]] inline void lock() { m_lock.lock(); }
|
|
|
|
|
|
|
|
private:
|
2019-08-25 20:51:27 +00:00
|
|
|
Lock& m_lock;
|
2019-03-25 12:03:49 +00:00
|
|
|
};
|
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
[[gnu::always_inline]] inline void Lock::lock()
|
2019-03-25 12:03:49 +00:00
|
|
|
{
|
2019-05-01 17:11:08 +00:00
|
|
|
int tid = gettid();
|
2019-03-25 12:03:49 +00:00
|
|
|
for (;;) {
|
2019-10-12 17:17:34 +00:00
|
|
|
bool expected = false;
|
|
|
|
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
|
2019-05-01 17:11:08 +00:00
|
|
|
if (m_holder == -1 || m_holder == tid) {
|
|
|
|
m_holder = tid;
|
2019-03-25 12:03:49 +00:00
|
|
|
++m_level;
|
2019-10-12 17:17:34 +00:00
|
|
|
m_lock.store(false, AK::memory_order_release);
|
2019-03-25 12:03:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-10-12 17:17:34 +00:00
|
|
|
m_lock.store(false, AK::memory_order_release);
|
2019-03-25 12:03:49 +00:00
|
|
|
}
|
2019-03-25 12:05:24 +00:00
|
|
|
donate(m_holder);
|
2019-03-25 12:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
inline void Lock::unlock()
|
2019-03-25 12:03:49 +00:00
|
|
|
{
|
|
|
|
for (;;) {
|
2019-10-12 17:17:34 +00:00
|
|
|
bool expected = false;
|
|
|
|
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
|
2019-03-25 12:03:49 +00:00
|
|
|
ASSERT(m_holder == gettid());
|
|
|
|
ASSERT(m_level);
|
|
|
|
--m_level;
|
|
|
|
if (m_level) {
|
2019-10-12 17:17:34 +00:00
|
|
|
m_lock.store(false, AK::memory_order_release);
|
2019-03-25 12:03:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_holder = -1;
|
2019-10-12 17:17:34 +00:00
|
|
|
m_lock.store(false, AK::memory_order_release);
|
2019-03-25 12:03:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
donate(m_holder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
#define LOCKER(lock) LibThread::Locker locker(lock)
|
2019-03-25 12:03:49 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2019-08-25 20:51:27 +00:00
|
|
|
class Lockable {
|
2019-03-25 12:03:49 +00:00
|
|
|
public:
|
2019-08-25 20:51:27 +00:00
|
|
|
Lockable() {}
|
|
|
|
Lockable(T&& resource)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_resource(move(resource))
|
|
|
|
{
|
|
|
|
}
|
2019-08-25 20:51:27 +00:00
|
|
|
Lock& lock() { return m_lock; }
|
2019-03-25 12:03:49 +00:00
|
|
|
T& resource() { return m_resource; }
|
|
|
|
|
|
|
|
T lock_and_copy()
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
return m_resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_resource;
|
2019-08-25 20:51:27 +00:00
|
|
|
Lock m_lock;
|
2019-03-25 12:03:49 +00:00
|
|
|
};
|
2019-06-22 19:21:57 +00:00
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 19:21:57 +00:00
|
|
|
#else
|
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
namespace LibThread {
|
|
|
|
|
|
|
|
class Lock {
|
2019-06-22 19:21:57 +00:00
|
|
|
public:
|
2019-08-25 20:51:27 +00:00
|
|
|
Lock() { }
|
|
|
|
~Lock() { }
|
2019-06-22 19:21:57 +00:00
|
|
|
};
|
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 19:21:57 +00:00
|
|
|
#define LOCKER(x)
|
|
|
|
|
|
|
|
#endif
|