mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
LibCore: Add CThread, a simple thread abstraction object.
Currently this is only a simple wrapper around create_thread() that remembers the thread ID of the spawned thread.
This commit is contained in:
parent
ad7ec2bbc7
commit
f1d6a37d5d
Notes:
sideshowbarker
2024-07-19 13:16:53 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f1d6a37d5dc
3 changed files with 48 additions and 0 deletions
29
Libraries/LibCore/CThread.cpp
Normal file
29
Libraries/LibCore/CThread.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include <AK/Assertions.h>
|
||||
#include <LibCore/CEventLoop.h>
|
||||
#include <LibCore/CThread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
CThread& CThread::main_thread()
|
||||
{
|
||||
static CThread* main_thread;
|
||||
if (!main_thread)
|
||||
main_thread = new CThread(MainThread);
|
||||
return *main_thread;
|
||||
}
|
||||
|
||||
CThread::CThread(MainThreadTag)
|
||||
: m_thread_id(0)
|
||||
{
|
||||
}
|
||||
|
||||
CThread::CThread(int (*entry)(void*), void* user_data)
|
||||
{
|
||||
ASSERT(entry);
|
||||
m_thread_id = create_thread(entry, user_data);
|
||||
}
|
||||
|
||||
CThread::~CThread()
|
||||
{
|
||||
}
|
18
Libraries/LibCore/CThread.h
Normal file
18
Libraries/LibCore/CThread.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
|
||||
class CThread {
|
||||
public:
|
||||
static CThread& main_thread();
|
||||
|
||||
CThread(int (*entry)(void*), void* user_data);
|
||||
~CThread();
|
||||
|
||||
bool is_main_thread() const { return m_thread_id == 0; }
|
||||
int thread_id() const { return m_thread_id; }
|
||||
|
||||
private:
|
||||
enum MainThreadTag { MainThread };
|
||||
explicit CThread(MainThreadTag);
|
||||
|
||||
int m_thread_id { -1 };
|
||||
};
|
|
@ -3,6 +3,7 @@ include ../../Makefile.common
|
|||
OBJS = \
|
||||
CArgsParser.o \
|
||||
CIODevice.o \
|
||||
CThread.o \
|
||||
CFile.o \
|
||||
CSocket.o \
|
||||
CLocalSocket.o \
|
||||
|
|
Loading…
Add table
Reference in a new issue