diff options
| author | 0scar <qgt268@alumni.ku.dk> | 2024-02-06 13:35:37 +0000 |
|---|---|---|
| committer | 0scar <qgt268@alumni.ku.dk> | 2024-02-06 13:35:37 +0000 |
| commit | ccf5583e6494fe5dd7730f00ea1295dae8154757 (patch) | |
| tree | 1f38737c31f6e4d4aad7aeb5a4f65c9089654fff /src/core | |
| parent | 5aeb4975d9bbbcea73eab014e92c69efa3da2f9f (diff) | |
Add threads (?)
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/core/include/engine/core/thread.h | 12 | ||||
| -rw-r--r-- | src/core/src/loop.c | 26 | ||||
| -rw-r--r-- | src/core/src/thread.c | 38 |
4 files changed, 51 insertions, 26 deletions
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 <engine/core/types.h> + +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 <pthread.h> + +#include <engine/core/logging.h> +#include <engine/core/thread.h> + +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"); + } +} |
