Project reordering and mpmc code

This commit is contained in:
2026-05-04 13:39:49 +01:00
parent 73aa4808f2
commit 759fdfda1e
8 changed files with 1043 additions and 78 deletions

61
base.h
View File

@@ -113,9 +113,7 @@ typedef double f64;
------------------------------------------------------------ */
#if defined(_WIN32) || defined(_WIN64)
// Memory allocation
static u32 plat_get_pagesize(void) {
SYSTEM_INFO sysinfo = {0};
GetSystemInfo(&sysinfo);
@@ -140,43 +138,11 @@ static b32 plat_mem_release(void *ptr, u64 size) {
return VirtualFree(ptr, size, MEM_RELEASE);
}
// Semaphores
typedef struct plat_sem {
HANDLE handle;
} plat_sem;
static b32 plat_sem_init(plat_sem *s, u32 initial) {
s->handle = CreateSemaphore(NULL, initial, LONG_MAX, NULL);
return s->handle != NULL;
}
static void plat_sem_wait(plat_sem *s) {
WaitForSingleObject(s->handle, INFINITE);
}
// static b32 plat_sem_trywait(HANDLE sem) { // Comment to prevent warning: unused function
// DWORD r = WaitForSingleObject(sem, 0);
// return r == WAIT_OBJECT_0;
// }
static void plat_sem_post(plat_sem *s, u32 count) {
ReleaseSemaphore(s->handle, count, NULL);
}
// static void plat_sem_destroy(plat_sem *s) { // Comment to prevent warning: unused function
// if (s->handle) {
// CloseHandle(s->handle);
// s->handle = NULL;
// }
// }
// Sleep
static void sleep_ms(int ms) { Sleep(ms); }
#elif defined(__linux__)
// Memory allocation
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
@@ -212,33 +178,6 @@ static b32 plat_mem_release(void *ptr, u64 size) {
return ret == 0;
}
// Semaphores
#include <semaphore.h>
typedef struct plat_sem {
sem_t sem;
} plat_sem;
static b32 plat_sem_init(plat_sem *s, u32 initial) {
return sem_init(&s->sem, 0, initial) == 0;
}
static void plat_sem_wait(plat_sem *s) {
while (sem_wait(&s->sem) == -1 && errno == EINTR) {
}
}
// static b32 plat_sem_trywait(sem_t *sem) { return sem_trywait(sem) == 0; } // Comment to prevent warning: unused function
static void plat_sem_post(plat_sem *s, u32 count) {
for (u32 i = 0; i < count; i++) {
sem_post(&s->sem);
}
}
// static void plat_sem_destroy(plat_sem *s) { sem_destroy(&s->sem); } // Comment to prevent warning: unused function
// Sleep
static void sleep_ms(int ms) { usleep(ms * 1000); }
#endif