diff options
-rw-r--r-- | Libraries/LibCore/CThread.cpp | 29 | ||||
-rw-r--r-- | Libraries/LibCore/CThread.h | 18 | ||||
-rw-r--r-- | Libraries/LibCore/Makefile | 1 |
3 files changed, 48 insertions, 0 deletions
diff --git a/Libraries/LibCore/CThread.cpp b/Libraries/LibCore/CThread.cpp new file mode 100644 index 0000000000..ec51581fef --- /dev/null +++ b/Libraries/LibCore/CThread.cpp @@ -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() +{ +} diff --git a/Libraries/LibCore/CThread.h b/Libraries/LibCore/CThread.h new file mode 100644 index 0000000000..4935c58449 --- /dev/null +++ b/Libraries/LibCore/CThread.h @@ -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 }; +}; diff --git a/Libraries/LibCore/Makefile b/Libraries/LibCore/Makefile index 2e119ee71f..c49f42ff08 100644 --- a/Libraries/LibCore/Makefile +++ b/Libraries/LibCore/Makefile @@ -3,6 +3,7 @@ include ../../Makefile.common OBJS = \ CArgsParser.o \ CIODevice.o \ + CThread.o \ CFile.o \ CSocket.o \ CLocalSocket.o \ |