add pthread adapter
This commit is contained in:
parent
21d4da5851
commit
2a252b2eec
@ -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)
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -13,7 +13,12 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef USE_PTHREAD
|
||||
#include "phtread_threads_adapter.h"
|
||||
#else
|
||||
#include <threads.h>
|
||||
#endif
|
||||
|
||||
static void line_dump(size_t i, uint16_t line)
|
||||
{
|
||||
|
||||
41
src/phtread_threads_adapter.c
Normal file
41
src/phtread_threads_adapter.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include "phtread_threads_adapter.h"
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
30
src/phtread_threads_adapter.h
Normal file
30
src/phtread_threads_adapter.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef PTHREAD_THREADS_ADAPTER_H
|
||||
#define PTHREAD_THREADS_ADAPTER_H
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
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
|
||||
Loading…
x
Reference in New Issue
Block a user