33#define FUN_THREAD_CALL WINAPI
34#define fun_sleep_ms(ms) Sleep((DWORD)(ms))
40#define FUN_THREAD_CALL
44 ts.tv_sec =
ms / 1000;
45 ts.tv_nsec = (
ms % 1000) * 1000000L;
50#ifndef FUN_MAX_THREADS
51#define FUN_MAX_THREADS 64
68static int g_thr_lock_inited = 0;
69static void fun_thr_lock_init(
void) {
70 if (!g_thr_lock_inited) {
72 g_thr_lock_inited = 1;
76 if (!g_thr_lock_inited) fun_thr_lock_init();
83static pthread_mutex_t
g_thr_lock = PTHREAD_MUTEX_INITIALIZER;
106 VM *tvm = (
VM *)malloc(
sizeof(
VM));
108 fprintf(stderr,
"Runtime error: failed to allocate thread VM\n");
126 for (
int i = 0; i < task->
argc; ++i) {
142 for (
int i = 0; i < task->
argc; ++i) {
183 fprintf(stderr,
"Runtime error: thread_spawn expects Function as first argument\n");
197 for (
int i = 0; i <
n; ++i) {
219 fprintf(stderr,
"Runtime error: too many threads\n");
221 for (
int i = 0; i < argc; ++i)
229 for (
int i = 0; i < argc; ++i)
245 fprintf(stderr,
"Runtime error: CreateThread failed\n");
246 for (
int i = 0; i < argc; ++i)
262 fprintf(stderr,
"Runtime error: pthread_create failed\n");
263 for (
int i = 0; i < argc; ++i)
282 fprintf(stderr,
"Runtime error: thread_join invalid id %d\n",
tid);
293 WaitForSingleObject(
h, INFINITE);
301 pthread_join(
h, NULL);
Bytecode * bytecode_new(void)
Allocate and initialize an empty Bytecode object.
int bytecode_add_instruction(Bytecode *bc, OpCode op, int32_t operand)
Append a single instruction to the instruction stream.
void bytecode_free(Bytecode *bc)
Free a Bytecode and all memory it owns.
int bytecode_add_constant(Bytecode *bc, Value v)
Append a constant to a Bytecode's constant table.
fun_thread_handle_t handle
The Fun virtual machine state.
Tagged union representing a Fun value.
static pthread_mutex_t g_thr_lock
static Value fun_thread_join(int tid)
static int fun_alloc_thread_slot(void)
static fun_thread_ret_t fun_thread_main(void *param)
static void fun_unlock(void)
pthread_t fun_thread_handle_t
static FunThreadEntry g_threads[FUN_MAX_THREADS]
static int fun_thread_spawn(Value fnVal, Value argsMaybe, int hasArgs)
static void fun_sleep_ms(long ms)
static void fun_lock(void)
Value make_nil(void)
Construct a nil Value.
int array_length(const Value *v)
Get the element count of an array Value.
Value make_function(struct Bytecode *fn)
Construct a function Value referencing bytecode.
Value deep_copy_value(const Value *v)
Deep copy a Value, recursively copying arrays and maps.
void free_value(Value v)
Free dynamic storage owned by a Value.
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
void vm_init(VM *vm)
Initialize a VM instance to its default state.
void vm_reset(VM *vm)
Reset the VM to a clean state.
void vm_run(VM *vm, Bytecode *entry)
Execute the provided entry bytecode in the VM. Pushes an initial frame and runs until HALT or an unre...