Porting to WASM

This commit is contained in:
Krzosa Karol
2025-11-24 22:59:11 +01:00
parent 166f06d1fb
commit 56cdb9557d
19 changed files with 414 additions and 654 deletions

View File

@@ -17,6 +17,8 @@
#elif defined(__linux__)
#define OS_POSIX 1
#define OS_LINUX 1
#elif defined(__EMSCRIPTEN__)
#define OS_WASM 1
#else
#error Unsupported platform
#endif
@@ -31,6 +33,10 @@
#error Unsupported compiler
#endif
#ifndef OS_WASM
#define OS_WASM 0
#endif
#ifndef OS_MAC
#define OS_MAC 0
#endif
@@ -60,14 +66,18 @@
#endif
#if OS_WINDOWS
#define DebugBreak() __debugbreak()
#define BREAK() __debugbreak()
#elif OS_LINUX
#define DebugBreak() raise(SIGTRAP)
#define BREAK() raise(SIGTRAP)
#elif OS_WASM
#include <emscripten.h>
extern "C" void JS_Breakpoint(void);
#define BREAK() JS_Breakpoint()
#endif
#define Assert(x) \
if (!(x)) { \
DebugBreak(); \
BREAK(); \
}
#define InvalidCodepath() Assert(!"invalid codepath")
#define ElseInvalidCodepath() else {InvalidCodepath()}
@@ -1035,8 +1045,8 @@ bool VCommit(void *p, size_t size);
bool VRelease(void *p, size_t size);
bool VDecommit(void *p, size_t size);
void InitArena(Arena *arena, size_t reserve = GiB(4));
Arena *AllocArena(size_t reserve = GiB(4));
void InitArena(Arena *arena, size_t reserve = MiB(256));
Arena *AllocArena(size_t reserve = MiB(256));
Arena *AllocArena(Allocator allocator, size_t size);
void *PushSize(Arena *arena, size_t size);
void Release(Arena *arena);
@@ -1707,11 +1717,22 @@ void *ArenaAllocatorProc(void *object, int kind, void *p, size_t size) {
}
thread_local Arena *ScratchArenaPool[4];
void InitScratch() {
#if OS_WASM
void InitScratch() {
Allocator sys_allocator = GetSystemAllocator();
ScratchArenaPool[0] = AllocArena(sys_allocator, MiB(8));
ScratchArenaPool[1] = AllocArena(sys_allocator, MiB(2));
ScratchArenaPool[3] = AllocArena(sys_allocator, MiB(1));
ScratchArenaPool[3] = AllocArena(sys_allocator, KiB(512));
}
#else
void InitScratch() {
for (int i = 0; i < Lengthof(ScratchArenaPool); i += 1) {
ScratchArenaPool[i] = AllocArena();
}
}
#endif
TempArena GetScratchEx(Arena **conflicts, int conflict_count) {
Arena *unoccupied = 0;

View File

@@ -15,7 +15,6 @@
#include <spawn.h>
#include <poll.h>
void (*Error)(const char *, ...);
void *VReserve(size_t size) {
@@ -112,7 +111,7 @@ String GetAbsolutePath(Allocator al, String path) {
char *buffer = AllocArray(al, char, PATH_MAX);
realpath(null_term.data, buffer);
String result = buffer;
return buffer;
return result;
}
bool FileExists(String path) {

View File

@@ -14,11 +14,7 @@ void PushWork(WorkQueue *wq, void *data, WorkQueueCallback *callback) {
entry->data = data;
entry->callback = callback;
wq->completion_goal += 1;dex_to_read + 1) % Lengthof(wq->entries);
if (original_index_to_read != wq->index_to_write) {
int64_t index = AtomicCompareAndSwap(&wq->index_to_read, new_index_to_read, original_index_to_read);
if (index == original_index_to_read) {
WorkQueueEntry *entry = wq->entries +
wq->completion_goal += 1;
_WriteBarrier();
wq->index_to_write = new_index;
ReleaseSemaphore(wq->semaphore, 1, 0);
@@ -27,7 +23,11 @@ void PushWork(WorkQueue *wq, void *data, WorkQueueCallback *callback) {
bool TryDoingWork(WorkQueue *wq) {
bool should_sleep = false;
int64_t original_index_to_read = wq->index_to_read;
int64_t new_index_to_read = (original_inindex;
int64_t new_index_to_read = (original_index_to_read + 1) % Lengthof(wq->entries);
if (original_index_to_read != wq->index_to_write) {
int64_t index = AtomicCompareAndSwap(&wq->index_to_read, new_index_to_read, original_index_to_read);
if (index == original_index_to_read) {
WorkQueueEntry *entry = wq->entries + index;
entry->callback(entry->data);
AtomicIncrement(&wq->completion_index);
}