mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:07:35 +00:00
LibThread: Port Lock to foreign environments
This commit is contained in:
parent
9853a4393c
commit
7f498769ac
1 changed files with 28 additions and 28 deletions
|
@ -6,12 +6,15 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef __serenity__
|
#include <AK/Assertions.h>
|
||||||
|
#include <AK/Atomic.h>
|
||||||
|
#include <AK/Types.h>
|
||||||
|
|
||||||
# include <AK/Assertions.h>
|
#ifdef __serenity__
|
||||||
# include <AK/Atomic.h>
|
|
||||||
# include <AK/Types.h>
|
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
|
#else
|
||||||
|
# include <pthread.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace LibThread {
|
namespace LibThread {
|
||||||
|
|
||||||
|
@ -24,7 +27,22 @@ public:
|
||||||
void unlock();
|
void unlock();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Atomic<pid_t> m_holder { 0 };
|
#ifdef __serenity__
|
||||||
|
using ThreadID = int;
|
||||||
|
|
||||||
|
ALWAYS_INLINE static ThreadID self()
|
||||||
|
{
|
||||||
|
return gettid();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
using ThreadID = pthread_t;
|
||||||
|
|
||||||
|
ALWAYS_INLINE static ThreadID self()
|
||||||
|
{
|
||||||
|
return pthread_self();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
Atomic<ThreadID> m_holder { 0 };
|
||||||
u32 m_level { 0 };
|
u32 m_level { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -45,24 +63,26 @@ private:
|
||||||
|
|
||||||
ALWAYS_INLINE void Lock::lock()
|
ALWAYS_INLINE void Lock::lock()
|
||||||
{
|
{
|
||||||
pid_t tid = gettid();
|
ThreadID tid = self();
|
||||||
if (m_holder == tid) {
|
if (m_holder == tid) {
|
||||||
++m_level;
|
++m_level;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int expected = 0;
|
ThreadID expected = 0;
|
||||||
if (m_holder.compare_exchange_strong(expected, tid, AK::memory_order_acq_rel)) {
|
if (m_holder.compare_exchange_strong(expected, tid, AK::memory_order_acq_rel)) {
|
||||||
m_level = 1;
|
m_level = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#ifdef __serenity__
|
||||||
donate(expected);
|
donate(expected);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Lock::unlock()
|
inline void Lock::unlock()
|
||||||
{
|
{
|
||||||
VERIFY(m_holder == gettid());
|
VERIFY(m_holder == self());
|
||||||
VERIFY(m_level);
|
VERIFY(m_level);
|
||||||
if (m_level == 1)
|
if (m_level == 1)
|
||||||
m_holder.store(0, AK::memory_order_release);
|
m_holder.store(0, AK::memory_order_release);
|
||||||
|
@ -100,23 +120,3 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
namespace LibThread {
|
|
||||||
|
|
||||||
class Lock {
|
|
||||||
public:
|
|
||||||
Lock() { }
|
|
||||||
~Lock() { }
|
|
||||||
};
|
|
||||||
|
|
||||||
class Locker {
|
|
||||||
public:
|
|
||||||
explicit Locker(Lock&) { }
|
|
||||||
~Locker() { }
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue