Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Makefile.libretro
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ else ifeq ($(platform), android)
fpic := -fPIC
SHARED := -Wl,--fix-cortex-a8 -llog -lz -shared -Wl,--version-script=$(LIBRETRO_DIR)/link.T -Wl,--no-undefined
PLATFLAGS := -DAND -DLSB_FIRST -DALIGN_DWORD

else ifeq ($(platform), emscripten)
pthread=0
TARGET := $(TARGET_NAME)_libretro_$(platform).bc
EXTERNAL_ZLIB=1
STATIC_LINKING=1
STATIC_LINKING_LINK=1
ifneq ($(pthread),0)
SHARED := -lpthread
CFLAGS += -pthread
endif

# Classic Platforms ####################
# Platform affix = classic_<ISA>_<µARCH>
# Help at https://modmyclassic.com/comp
Expand Down
64 changes: 64 additions & 0 deletions libretro/libretro-common/libco/emscripten_fiber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
libco.emscripten (2020-02-27)
authors: Toad King
license: public domain
*/

#define LIBCO_C
#include <libco.h>
#include <stdlib.h>
#include <stddef.h>
#include <malloc.h>
#include <emscripten/fiber.h>

#define ASYNCIFY_STACK_SIZE (12582912)

static thread_local emscripten_fiber_t *co_active_;

static void co_thunk(void *coentry)
{
((void (*)(void))coentry)();
}

static void co_init(void)
{
if (!co_active_)
{
emscripten_fiber_t *co_primary = calloc(1, sizeof(emscripten_fiber_t));
void *asyncify_stack = malloc(ASYNCIFY_STACK_SIZE);

emscripten_fiber_init_from_current_context(co_primary, asyncify_stack, ASYNCIFY_STACK_SIZE);
co_active_ = co_primary;
}
}

cothread_t co_active(void)
{
co_init();
return co_active_;
}

cothread_t co_create(unsigned int stacksize, void (*coentry)(void))
{
co_init();

emscripten_fiber_t *fiber = calloc(1, sizeof(emscripten_fiber_t));
void *asyncify_stack = malloc(ASYNCIFY_STACK_SIZE);
void *c_stack = memalign(16, stacksize);
emscripten_fiber_init(fiber, co_thunk, coentry, c_stack, stacksize, asyncify_stack, ASYNCIFY_STACK_SIZE);

return (cothread_t)fiber;
}

void co_delete(cothread_t cothread)
{
free(cothread);
}

void co_switch(cothread_t cothread)
{
emscripten_fiber_t *old_fiber = co_active_;
co_active_ = (emscripten_fiber_t *)cothread;

emscripten_fiber_swap(old_fiber, co_active_);
}
4 changes: 3 additions & 1 deletion libretro/libretro-common/libco/libco.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ void *genode_alloc_secondary_stack(unsigned long stack_size);
void genode_free_secondary_stack(void *stack);
#endif

#if defined _MSC_VER
#ifdef EMSCRIPTEN
#include "emscripten_fiber.c"
#elif defined _MSC_VER
#include <Windows.h>
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
#include "fiber.c"
Expand Down