1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 09:34:57 +09:00

LibThread: Move CLock to LibThread::Lock

And adapt all the code that uses it.
This commit is contained in:
Sergey Bugaev 2019-08-25 23:51:27 +03:00 committed by Andreas Kling
parent 0826cc5a35
commit 3439a479af
Notes: sideshowbarker 2024-07-19 12:30:48 +09:00
7 changed files with 42 additions and 34 deletions

View file

@ -2,7 +2,7 @@
#include <AK/InlineLinkedList.h>
#include <AK/ScopedValueRollback.h>
#include <AK/Vector.h>
#include <LibCore/CLock.h>
#include <LibThread/Lock.h>
#include <assert.h>
#include <mallocdefs.h>
#include <serenity.h>
@ -19,10 +19,10 @@
#define MAGIC_BIGALLOC_HEADER 0x42697267
#define PAGE_ROUND_UP(x) ((((size_t)(x)) + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)))
static CLock& malloc_lock()
static LibThread::Lock& malloc_lock()
{
static u32 lock_storage[sizeof(CLock) / sizeof(u32)];
return *reinterpret_cast<CLock*>(&lock_storage);
static u32 lock_storage[sizeof(LibThread::Lock) / sizeof(u32)];
return *reinterpret_cast<LibThread::Lock*>(&lock_storage);
}
static const int number_of_chunked_blocks_to_keep_around_per_size_class = 32;
@ -317,7 +317,7 @@ void* realloc(void* ptr, size_t size)
void __malloc_init()
{
new (&malloc_lock()) CLock();
new (&malloc_lock()) LibThread::Lock();
if (getenv("LIBC_NOSCRUB_MALLOC"))
s_scrub_malloc = false;
if (getenv("LIBC_NOSCRUB_FREE"))

View file

@ -5,10 +5,10 @@
#include <LibCore/CEvent.h>
#include <LibCore/CEventLoop.h>
#include <LibCore/CLocalSocket.h>
#include <LibCore/CLock.h>
#include <LibCore/CNotifier.h>
#include <LibCore/CObject.h>
#include <LibCore/CSyscallUtils.h>
#include <LibThread/Lock.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>

View file

@ -7,7 +7,7 @@
#include <AK/WeakPtr.h>
#include <LibCore/CEvent.h>
#include <LibCore/CLocalServer.h>
#include <LibCore/CLock.h>
#include <LibThread/Lock.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
@ -69,7 +69,7 @@ private:
static int s_wake_pipe_fds[2];
CLock m_lock;
LibThread::Lock m_lock;
struct EventLoopTimer {
int timer_id { 0 };

View file

@ -1,9 +1,9 @@
#include <LibThread/BackgroundAction.h>
#include <LibThread/Thread.h>
#include <LibCore/CLock.h>
#include <LibThread/Lock.h>
#include <AK/Queue.h>
static CLockable<Queue<Function<void()>>>* s_all_actions;
static LibThread::Lockable<Queue<Function<void()>>>* s_all_actions;
static LibThread::Thread* s_background_thread;
static int background_thread_func()
@ -27,13 +27,13 @@ static int background_thread_func()
static void init()
{
s_all_actions = new CLockable<Queue<Function<void()>>>();
s_all_actions = new LibThread::Lockable<Queue<Function<void()>>>();
s_background_thread = new LibThread::Thread(background_thread_func);
s_background_thread->set_name("Background thread");
s_background_thread->start();
}
CLockable<Queue<Function<void()>>>& LibThread::BackgroundActionBase::all_actions()
LibThread::Lockable<Queue<Function<void()>>>& LibThread::BackgroundActionBase::all_actions()
{
if (s_all_actions == nullptr)
init();

View file

@ -6,8 +6,8 @@
#include <AK/Queue.h>
#include <AK/RefCounted.h>
#include <LibCore/CEventLoop.h>
#include <LibCore/CLock.h>
#include <LibCore/CObject.h>
#include <LibThread/Lock.h>
#include <LibThread/Thread.h>
namespace LibThread {
@ -22,7 +22,7 @@ class BackgroundActionBase {
private:
BackgroundActionBase() {}
static CLockable<Queue<Function<void()>>>& all_actions();
static Lockable<Queue<Function<void()>>>& all_actions();
static Thread& background_thread();
};

View file

@ -20,10 +20,12 @@ static inline u32 CAS(volatile u32* mem, u32 newval, u32 oldval)
return ret;
}
class CLock {
namespace LibThread {
class Lock {
public:
CLock() {}
~CLock() {}
Lock() {}
~Lock() {}
void lock();
void unlock();
@ -34,22 +36,22 @@ private:
int m_holder { -1 };
};
class CLocker {
class Locker {
public:
[[gnu::always_inline]] inline explicit CLocker(CLock& l)
[[gnu::always_inline]] inline explicit Locker(Lock& l)
: m_lock(l)
{
lock();
}
[[gnu::always_inline]] inline ~CLocker() { unlock(); }
[[gnu::always_inline]] inline ~Locker() { unlock(); }
[[gnu::always_inline]] inline void unlock() { m_lock.unlock(); }
[[gnu::always_inline]] inline void lock() { m_lock.lock(); }
private:
CLock& m_lock;
Lock& m_lock;
};
[[gnu::always_inline]] inline void CLock::lock()
[[gnu::always_inline]] inline void Lock::lock()
{
int tid = gettid();
for (;;) {
@ -67,7 +69,7 @@ private:
}
}
inline void CLock::unlock()
inline void Lock::unlock()
{
for (;;) {
if (CAS(&m_lock, 1, 0) == 0) {
@ -88,17 +90,17 @@ inline void CLock::unlock()
}
}
#define LOCKER(lock) CLocker locker(lock)
#define LOCKER(lock) LibThread::Locker locker(lock)
template<typename T>
class CLockable {
class Lockable {
public:
CLockable() {}
CLockable(T&& resource)
Lockable() {}
Lockable(T&& resource)
: m_resource(move(resource))
{
}
CLock& lock() { return m_lock; }
Lock& lock() { return m_lock; }
T& resource() { return m_resource; }
T lock_and_copy()
@ -109,17 +111,23 @@ public:
private:
T m_resource;
CLock m_lock;
Lock m_lock;
};
}
#else
class CLock {
namespace LibThread {
class Lock {
public:
CLock() { }
~CLock() { }
Lock() { }
~Lock() { }
};
}
#define LOCKER(x)
#endif

View file

@ -7,7 +7,7 @@
#include <AK/WeakPtr.h>
#include <LibAudio/ABuffer.h>
#include <LibCore/CFile.h>
#include <LibCore/CLock.h>
#include <LibThread/Lock.h>
#include <LibThread/Thread.h>
class ASClientConnection;
@ -64,7 +64,7 @@ private:
Vector<NonnullRefPtr<ASBufferQueue>> m_pending_mixing;
CFile m_device;
CLock m_lock;
LibThread::Lock m_lock;
LibThread::Thread m_sound_thread;