From ccf5583e6494fe5dd7730f00ea1295dae8154757 Mon Sep 17 00:00:00 2001 From: 0scar Date: Tue, 6 Feb 2024 14:35:37 +0100 Subject: Add threads (?) --- src/core/CMakeLists.txt | 1 + src/core/include/engine/core/thread.h | 12 +++++++++++ src/core/src/loop.c | 26 ------------------------ src/core/src/thread.c | 38 +++++++++++++++++++++++++++++++++++ src/resources/src/textures.c | 2 ++ 5 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 src/core/include/engine/core/thread.h create mode 100644 src/core/src/thread.c diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index a9e2b6d..c75cdf6 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(daw_core src/loop.c src/memory.c src/state.c + src/thread.c ) set_property(SOURCE src/loop.c APPEND PROPERTY OBJECT_DEPENDS ${GLAD_HEADER}) diff --git a/src/core/include/engine/core/thread.h b/src/core/include/engine/core/thread.h new file mode 100644 index 0000000..ee61893 --- /dev/null +++ b/src/core/include/engine/core/thread.h @@ -0,0 +1,12 @@ +#ifndef THREAD_H +#define THREAD_H + +#include + +typedef u64 thread_t; +typedef void*(thread_fn(void*)); + +thread_t thread_spawn(thread_fn* routine, void* arg); +void thread_join(thread_t thread); + +#endif diff --git a/src/core/src/loop.c b/src/core/src/loop.c index 721e66f..a176394 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -29,32 +29,6 @@ #define DEFAULT_NUM_PROCS 8 -#ifdef BENCHMARK -#define BENCHEXPR(timevar, expr) \ - { \ - f64 t = get_time(); \ - expr timevar += get_time() - t; \ - } - -extern i32 drawcall_len; - -#else -#define BENCHEXPR(timevar, expr) expr -#endif - - - - - - - - - - - - - -static u64 FPS_CAP = 50; Platform* GLOBAL_PLATFORM = NULL; input_callback_t* callbacks[128]; diff --git a/src/core/src/thread.c b/src/core/src/thread.c new file mode 100644 index 0000000..8c76e4a --- /dev/null +++ b/src/core/src/thread.c @@ -0,0 +1,38 @@ +// The backend used :) +#include + +#include +#include + +thread_t thread_spawn(thread_fn* routine, void* arg) { + pthread_t t; + pthread_attr_t attr; + + if (pthread_attr_init(&attr)) { + ERROR("Failed to create thread attribute"); + return -1; + } + + if (pthread_create(&t, &attr, routine, arg)) { + ERROR("Failed to spawn new thread"); + } + + // This compile-time check should only be run once, but i am lazy and have + // spent too much time refactoring + if (sizeof(thread_t) < sizeof(pthread_t)) { + ERROR("THE SIZE AINT BIG ENOUGH!"); + exit(EXIT_FAILURE); + } + + if(pthread_attr_destroy(&attr)) { + ERROR("Failed to destroy thread attribute"); + } + // this little trick might cost us 40 years + return (thread_t)t; +} + +void thread_join(thread_t thread) { + if(pthread_join((pthread_t)thread, NULL)) { + ERROR("Unable to join thread"); + } +} diff --git a/src/resources/src/textures.c b/src/resources/src/textures.c index 622c4b5..cde56b7 100644 --- a/src/resources/src/textures.c +++ b/src/resources/src/textures.c @@ -1,6 +1,8 @@ #include #include +// Use the engine/render.h backend to create a renderer-specific texture + Texture* load_texture(void* render, const Asset_TextureSpec* ts) { Texture* t = NULL; -- cgit v1.3