Skip to content

Commit

Permalink
Update thread_spawn to take an argument value
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Sep 4, 2024
1 parent ed62948 commit 32a6d8f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
6 changes: 5 additions & 1 deletion api/syscalls.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@
[
"void*",
"fptr"
],
[
"void*",
"arg"
]
],
"returns": [
Expand All @@ -140,7 +144,7 @@
],
"permission": "default_allowed",
"const_idx": 29,
"description": "Spawn a new thread running the given function."
"description": "Spawn a new thread running the given function with the argument value `arg`."
},
{
"name": "thread_id",
Expand Down
4 changes: 2 additions & 2 deletions doc/syscalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ Grow the heap to a new size given in bytes. This is similar to the `brk()` syste
## thread_spawn

```
u64 thread_spawn(void* fptr)
u64 thread_spawn(void* fptr, void* arg)
```

**Returns:** `u64 tid`

Spawn a new thread running the given function.
Spawn a new thread running the given function with the argument value `arg`.

## thread_id

Expand Down
6 changes: 3 additions & 3 deletions ncc/include/uvm/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
// Grow the heap to a new size given in bytes. This is similar to the `brk()` system call on POSIX systems. Note that the heap may be resized to a size larger than requested. The heap size is guaranteed to be a multiple of 8 bytes. If the requested size is smaller than the current heap size, this is a no-op. Returns the new heap size in bytes.
#define vm_grow_heap(__num_bytes) asm (__num_bytes) -> u64 { syscall vm_grow_heap; }

// u64 thread_spawn(void* fptr)
// Spawn a new thread running the given function.
#define thread_spawn(__fptr) asm (__fptr) -> u64 { syscall thread_spawn; }
// u64 thread_spawn(void* fptr, void* arg)
// Spawn a new thread running the given function with the argument value `arg`.
#define thread_spawn(__fptr, __arg) asm (__fptr, __arg) -> u64 { syscall thread_spawn; }

// u64 thread_id()
// Get the id of the current thread.
Expand Down
8 changes: 4 additions & 4 deletions ncc/tests/threads.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include <assert.h>
#include <uvm/syscalls.h>

u64 thread_fn()
u64 thread_fn(u64 arg)
{
thread_sleep(5);
return 7;
return arg + 1;
}

int main()
{
u64 tid = thread_spawn(thread_fn);
u64 tid = thread_spawn(thread_fn, 7);
u64 ret = thread_join(tid);
assert(ret == 7);
assert(ret == 8);

return 0;
}
2 changes: 1 addition & 1 deletion vm/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub const SYSCALL_DESCS: [Option<SysCallDesc>; SYSCALL_TBL_LEN] = [
Some(SysCallDesc { name: "putchar", const_idx: 26, argc: 1, has_ret: true }),
Some(SysCallDesc { name: "memcmp", const_idx: 27, argc: 3, has_ret: true }),
Some(SysCallDesc { name: "thread_id", const_idx: 28, argc: 0, has_ret: true }),
Some(SysCallDesc { name: "thread_spawn", const_idx: 29, argc: 1, has_ret: true }),
Some(SysCallDesc { name: "thread_spawn", const_idx: 29, argc: 2, has_ret: true }),
Some(SysCallDesc { name: "thread_sleep", const_idx: 30, argc: 1, has_ret: false }),
Some(SysCallDesc { name: "thread_join", const_idx: 31, argc: 1, has_ret: true }),
];
Expand Down
6 changes: 3 additions & 3 deletions vm/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn get_syscall(const_idx: u16) -> HostFn
MEMCPY => HostFn::Fn3_0(memcpy),
MEMCMP => HostFn::Fn3_1(memcmp),

THREAD_SPAWN => HostFn::Fn1_1(thread_spawn),
THREAD_SPAWN => HostFn::Fn2_1(thread_spawn),
THREAD_JOIN => HostFn::Fn1_1(thread_join),
THREAD_ID => HostFn::Fn0_1(thread_id),
THREAD_SLEEP => HostFn::Fn1_0(thread_sleep),
Expand Down Expand Up @@ -154,10 +154,10 @@ fn thread_sleep(thread: &mut Thread, msecs: Value)
// Spawn a new thread
// Takes a function to call as argument
// Returns a thread id
fn thread_spawn(thread: &mut Thread, fun: Value) -> Value
fn thread_spawn(thread: &mut Thread, fun: Value, arg: Value) -> Value
{
let callee_pc = fun.as_u64();
let tid = VM::spawn_thread(&thread.vm, callee_pc, vec![]);
let tid = VM::spawn_thread(&thread.vm, callee_pc, vec![arg]);
Value::from(tid)
}

Expand Down

0 comments on commit 32a6d8f

Please sign in to comment.