mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:27:43 +00:00
LibThread: Move CLock to LibThread::Lock
And adapt all the code that uses it.
This commit is contained in:
parent
0826cc5a35
commit
3439a479af
7 changed files with 42 additions and 34 deletions
|
@ -2,7 +2,7 @@
|
||||||
#include <AK/InlineLinkedList.h>
|
#include <AK/InlineLinkedList.h>
|
||||||
#include <AK/ScopedValueRollback.h>
|
#include <AK/ScopedValueRollback.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/CLock.h>
|
#include <LibThread/Lock.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <mallocdefs.h>
|
#include <mallocdefs.h>
|
||||||
#include <serenity.h>
|
#include <serenity.h>
|
||||||
|
@ -19,10 +19,10 @@
|
||||||
#define MAGIC_BIGALLOC_HEADER 0x42697267
|
#define MAGIC_BIGALLOC_HEADER 0x42697267
|
||||||
#define PAGE_ROUND_UP(x) ((((size_t)(x)) + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)))
|
#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)];
|
static u32 lock_storage[sizeof(LibThread::Lock) / sizeof(u32)];
|
||||||
return *reinterpret_cast<CLock*>(&lock_storage);
|
return *reinterpret_cast<LibThread::Lock*>(&lock_storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int number_of_chunked_blocks_to_keep_around_per_size_class = 32;
|
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()
|
void __malloc_init()
|
||||||
{
|
{
|
||||||
new (&malloc_lock()) CLock();
|
new (&malloc_lock()) LibThread::Lock();
|
||||||
if (getenv("LIBC_NOSCRUB_MALLOC"))
|
if (getenv("LIBC_NOSCRUB_MALLOC"))
|
||||||
s_scrub_malloc = false;
|
s_scrub_malloc = false;
|
||||||
if (getenv("LIBC_NOSCRUB_FREE"))
|
if (getenv("LIBC_NOSCRUB_FREE"))
|
||||||
|
|
|
@ -5,10 +5,10 @@
|
||||||
#include <LibCore/CEvent.h>
|
#include <LibCore/CEvent.h>
|
||||||
#include <LibCore/CEventLoop.h>
|
#include <LibCore/CEventLoop.h>
|
||||||
#include <LibCore/CLocalSocket.h>
|
#include <LibCore/CLocalSocket.h>
|
||||||
#include <LibCore/CLock.h>
|
|
||||||
#include <LibCore/CNotifier.h>
|
#include <LibCore/CNotifier.h>
|
||||||
#include <LibCore/CObject.h>
|
#include <LibCore/CObject.h>
|
||||||
#include <LibCore/CSyscallUtils.h>
|
#include <LibCore/CSyscallUtils.h>
|
||||||
|
#include <LibThread/Lock.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#include <AK/WeakPtr.h>
|
#include <AK/WeakPtr.h>
|
||||||
#include <LibCore/CEvent.h>
|
#include <LibCore/CEvent.h>
|
||||||
#include <LibCore/CLocalServer.h>
|
#include <LibCore/CLocalServer.h>
|
||||||
#include <LibCore/CLock.h>
|
#include <LibThread/Lock.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
@ -69,7 +69,7 @@ private:
|
||||||
|
|
||||||
static int s_wake_pipe_fds[2];
|
static int s_wake_pipe_fds[2];
|
||||||
|
|
||||||
CLock m_lock;
|
LibThread::Lock m_lock;
|
||||||
|
|
||||||
struct EventLoopTimer {
|
struct EventLoopTimer {
|
||||||
int timer_id { 0 };
|
int timer_id { 0 };
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
#include <LibThread/BackgroundAction.h>
|
#include <LibThread/BackgroundAction.h>
|
||||||
#include <LibThread/Thread.h>
|
#include <LibThread/Thread.h>
|
||||||
#include <LibCore/CLock.h>
|
#include <LibThread/Lock.h>
|
||||||
#include <AK/Queue.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 LibThread::Thread* s_background_thread;
|
||||||
|
|
||||||
static int background_thread_func()
|
static int background_thread_func()
|
||||||
|
@ -27,13 +27,13 @@ static int background_thread_func()
|
||||||
|
|
||||||
static void init()
|
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 = new LibThread::Thread(background_thread_func);
|
||||||
s_background_thread->set_name("Background thread");
|
s_background_thread->set_name("Background thread");
|
||||||
s_background_thread->start();
|
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)
|
if (s_all_actions == nullptr)
|
||||||
init();
|
init();
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
#include <AK/Queue.h>
|
#include <AK/Queue.h>
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
#include <LibCore/CEventLoop.h>
|
#include <LibCore/CEventLoop.h>
|
||||||
#include <LibCore/CLock.h>
|
|
||||||
#include <LibCore/CObject.h>
|
#include <LibCore/CObject.h>
|
||||||
|
#include <LibThread/Lock.h>
|
||||||
#include <LibThread/Thread.h>
|
#include <LibThread/Thread.h>
|
||||||
|
|
||||||
namespace LibThread {
|
namespace LibThread {
|
||||||
|
@ -22,7 +22,7 @@ class BackgroundActionBase {
|
||||||
private:
|
private:
|
||||||
BackgroundActionBase() {}
|
BackgroundActionBase() {}
|
||||||
|
|
||||||
static CLockable<Queue<Function<void()>>>& all_actions();
|
static Lockable<Queue<Function<void()>>>& all_actions();
|
||||||
static Thread& background_thread();
|
static Thread& background_thread();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -20,10 +20,12 @@ static inline u32 CAS(volatile u32* mem, u32 newval, u32 oldval)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
class CLock {
|
namespace LibThread {
|
||||||
|
|
||||||
|
class Lock {
|
||||||
public:
|
public:
|
||||||
CLock() {}
|
Lock() {}
|
||||||
~CLock() {}
|
~Lock() {}
|
||||||
|
|
||||||
void lock();
|
void lock();
|
||||||
void unlock();
|
void unlock();
|
||||||
|
@ -34,22 +36,22 @@ private:
|
||||||
int m_holder { -1 };
|
int m_holder { -1 };
|
||||||
};
|
};
|
||||||
|
|
||||||
class CLocker {
|
class Locker {
|
||||||
public:
|
public:
|
||||||
[[gnu::always_inline]] inline explicit CLocker(CLock& l)
|
[[gnu::always_inline]] inline explicit Locker(Lock& l)
|
||||||
: m_lock(l)
|
: m_lock(l)
|
||||||
{
|
{
|
||||||
lock();
|
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 unlock() { m_lock.unlock(); }
|
||||||
[[gnu::always_inline]] inline void lock() { m_lock.lock(); }
|
[[gnu::always_inline]] inline void lock() { m_lock.lock(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CLock& m_lock;
|
Lock& m_lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
[[gnu::always_inline]] inline void CLock::lock()
|
[[gnu::always_inline]] inline void Lock::lock()
|
||||||
{
|
{
|
||||||
int tid = gettid();
|
int tid = gettid();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
@ -67,7 +69,7 @@ private:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CLock::unlock()
|
inline void Lock::unlock()
|
||||||
{
|
{
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (CAS(&m_lock, 1, 0) == 0) {
|
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>
|
template<typename T>
|
||||||
class CLockable {
|
class Lockable {
|
||||||
public:
|
public:
|
||||||
CLockable() {}
|
Lockable() {}
|
||||||
CLockable(T&& resource)
|
Lockable(T&& resource)
|
||||||
: m_resource(move(resource))
|
: m_resource(move(resource))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
CLock& lock() { return m_lock; }
|
Lock& lock() { return m_lock; }
|
||||||
T& resource() { return m_resource; }
|
T& resource() { return m_resource; }
|
||||||
|
|
||||||
T lock_and_copy()
|
T lock_and_copy()
|
||||||
|
@ -109,17 +111,23 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T m_resource;
|
T m_resource;
|
||||||
CLock m_lock;
|
Lock m_lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
class CLock {
|
namespace LibThread {
|
||||||
|
|
||||||
|
class Lock {
|
||||||
public:
|
public:
|
||||||
CLock() { }
|
Lock() { }
|
||||||
~CLock() { }
|
~Lock() { }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#define LOCKER(x)
|
#define LOCKER(x)
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -7,7 +7,7 @@
|
||||||
#include <AK/WeakPtr.h>
|
#include <AK/WeakPtr.h>
|
||||||
#include <LibAudio/ABuffer.h>
|
#include <LibAudio/ABuffer.h>
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
#include <LibCore/CLock.h>
|
#include <LibThread/Lock.h>
|
||||||
#include <LibThread/Thread.h>
|
#include <LibThread/Thread.h>
|
||||||
|
|
||||||
class ASClientConnection;
|
class ASClientConnection;
|
||||||
|
@ -64,7 +64,7 @@ private:
|
||||||
Vector<NonnullRefPtr<ASBufferQueue>> m_pending_mixing;
|
Vector<NonnullRefPtr<ASBufferQueue>> m_pending_mixing;
|
||||||
|
|
||||||
CFile m_device;
|
CFile m_device;
|
||||||
CLock m_lock;
|
LibThread::Lock m_lock;
|
||||||
|
|
||||||
LibThread::Thread m_sound_thread;
|
LibThread::Thread m_sound_thread;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue