Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
thread_spawn.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file thread_spawn.c
12 * @brief Implements OP_THREAD_SPAWN to run a function in a background thread.
13 *
14 * Behavior:
15 * - Pops function and optionally one argument or an array of arguments (controlled by operand).
16 * - Spawns a new thread that invokes the function; pushes a thread id (int) or 0 on failure.
17 *
18 * Errors:
19 * - If types are wrong or spawning fails, returns 0.
20 */
21
23 /* operand: 0 -> no args; 1 -> has args array or single arg */
24 Value argsMaybe = make_nil();
25 if (inst.operand == 1) {
26 argsMaybe = pop_value(vm); /* maybe array or scalar */
27 }
29 int tid = fun_thread_spawn(fnv, argsMaybe, inst.operand == 1);
31 if (inst.operand == 1) free_value(argsMaybe);
33 break;
34}
@ OP_THREAD_SPAWN
Definition bytecode.h:151
Value fnv
Definition call.c:40
Tagged union representing a Fun value.
Definition value.h:68
static int fun_thread_spawn(Value fnVal, Value argsMaybe, int hasArgs)
push_value(vm, make_int(tid))
int tid
free_value(fnv)
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580