Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
thread_join.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_join.c
12 * @brief Implements OP_THREAD_JOIN to wait for a spawned thread and get its result.
13 *
14 * Behavior:
15 * - Pops thread id (int); waits for the thread to finish; pushes the result Value produced by the thread.
16 * - If the thread failed or id is invalid, pushes Nil.
17 *
18 * Errors:
19 * - If argument type is wrong, prints an error and pushes Nil.
20 */
21
23 Value vtid = pop_value(vm);
24 if (vtid.type != VAL_INT) {
25 fprintf(stderr, "Runtime type error: thread_join expects thread id (int)\n");
26 push_value(vm, make_nil());
27 free_value(vtid);
28 break;
29 }
30 Value res = fun_thread_join((int)vtid.i);
32 push_value(vm, res); /* takes ownership */
33 break;
34}
@ OP_THREAD_JOIN
Definition bytecode.h:152
const char * res
Definition get_string.c:38
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
static Value fun_thread_join(int tid)
free_value(vtid)
push_value(vm, res)
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
#define fprintf
Definition vm.c:200