diff --git a/CMakeLists.txt b/CMakeLists.txt index a9ef221..a0af996 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,15 @@ set(CMAKE_COLOR_DIAGNOSTICS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) option(ASAN "Use -fsanitize=address." OFF) +option(USE_PTHREAD "Use pthread instead of C11 threads" OFF) find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared) +if(USE_PTHREAD) + set(THREADS_PREFER_PTHREAD_FLAG TRUE) + find_package(Threads REQUIRED) +endif() + add_subdirectory(src) add_subdirectory(boot) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ef338ab..e3c2d5a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,6 +30,12 @@ target_include_directories(isa PUBLIC .) add_executable(emu WIN32 emu.c) target_link_libraries(emu PRIVATE opts isa SDL3::SDL3) +if(USE_PTHREAD) + target_compile_definitions(emu PRIVATE USE_PTHREAD) + target_sources(emu PRIVATE phtread_threads_adapter.c) + target_link_libraries(emu PRIVATE Threads::Threads) +endif() + add_executable(asm asm.c) target_link_libraries(asm PRIVATE opts isa) diff --git a/src/emu.c b/src/emu.c index a176a52..17007be 100644 --- a/src/emu.c +++ b/src/emu.c @@ -13,7 +13,12 @@ #include #include #include + +#ifdef USE_PTHREAD +#include "phtread_threads_adapter.h" +#else #include +#endif static void line_dump(size_t i, uint16_t line) { diff --git a/src/phtread_threads_adapter.c b/src/phtread_threads_adapter.c new file mode 100644 index 0000000..051898a --- /dev/null +++ b/src/phtread_threads_adapter.c @@ -0,0 +1,41 @@ +#include "phtread_threads_adapter.h" +#include +#include +#include + +typedef struct { + thrd_start_t func; + void *arg; +} ThreadCreateArgs; + +static void *thread_create_fn(void *arg) +{ + ThreadCreateArgs *args = arg; + args->func(args->arg); + free(args); + return nullptr; +} + +void thrd_create(thrd_t *t, thrd_start_t func, void *arg) +{ + ThreadCreateArgs *args = malloc(sizeof(ThreadCreateArgs)); + *args = (ThreadCreateArgs) { func, arg }; + pthread_create(t, nullptr, thread_create_fn, args); +} + +void thrd_join(thrd_t t, int *res) +{ + assert(res == 0); + pthread_join(t, nullptr); +} + +void mtx_init(mtx_t *m, int type) +{ + assert(type == mtx_plain); + *m = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; +} + +void cnd_init(cnd_t *c) +{ + *c = (pthread_cond_t)PTHREAD_COND_INITIALIZER; +} diff --git a/src/phtread_threads_adapter.h b/src/phtread_threads_adapter.h new file mode 100644 index 0000000..79adabf --- /dev/null +++ b/src/phtread_threads_adapter.h @@ -0,0 +1,30 @@ +#ifndef PTHREAD_THREADS_ADAPTER_H +#define PTHREAD_THREADS_ADAPTER_H + +#include + +typedef pthread_t thrd_t; + +typedef int (*thrd_start_t)(void *); + +void thrd_create(thrd_t *t, thrd_start_t func, void *arg); +void thrd_join(thrd_t t, int *res); + +typedef pthread_mutex_t mtx_t; + +enum { mtx_plain }; + +void mtx_init(mtx_t *m, int type); +#define mtx_destroy pthread_mutex_destroy +#define mtx_lock pthread_mutex_lock +#define mtx_unlock pthread_mutex_unlock + +typedef pthread_cond_t cnd_t; + +void cnd_init(cnd_t *c); +#define cnd_destroy pthread_cond_destroy +#define cnd_wait pthread_cond_wait +#define cnd_signal pthread_cond_signal +#define cnd_broadcast pthread_cond_broadcast + +#endif