diff options
author | dzwdz | 2022-08-19 18:30:35 +0200 |
---|---|---|
committer | dzwdz | 2022-08-19 18:30:35 +0200 |
commit | 6bea8cd391125734339dfb83db498a8651c9f7f7 (patch) | |
tree | 1eb30cda98318ff4fc63652425ef725903619766 /src/user | |
parent | 7a4bc281958c639cd52ff4f192933aa161ba81a4 (diff) |
syscall/fork: allow sharing handles between threads
Diffstat (limited to 'src/user')
-rw-r--r-- | src/user/app/tests/kernel/threads.c | 25 | ||||
-rw-r--r-- | src/user/lib/thread.c | 2 |
2 files changed, 24 insertions, 3 deletions
diff --git a/src/user/app/tests/kernel/threads.c b/src/user/app/tests/kernel/threads.c index 9f08c39..2964883 100644 --- a/src/user/app/tests/kernel/threads.c +++ b/src/user/app/tests/kernel/threads.c @@ -1,16 +1,15 @@ #include "../tests.h" #include <camellia/flags.h> #include <camellia/syscalls.h> +#include <string.h> #include <user/lib/esemaphore.h> #include <user/lib/thread.h> int global_n; - static void basic_thread(void *sem) { global_n = 10; esem_signal(sem); } - static void test_basic_thread(void) { struct evil_sem *sem = esem_new(0); global_n = 0; @@ -19,6 +18,28 @@ static void test_basic_thread(void) { test(global_n == 10); } + +handle_t global_h; +static void shared_handle(void *sem) { + handle_t ends[2]; + test(_syscall_pipe(ends, 0) >= 0); + global_h = ends[0]; + esem_signal(sem); + _syscall_write(ends[1], "Hello!", 7, -1, 0); +} +static void test_shared_handle(void) { + struct evil_sem *sem = esem_new(0); + char buf[16]; + global_h = -1; + thread_create(FORK_NOREAP, shared_handle, sem); + esem_wait(sem); + + test(global_h >= 0); + test(_syscall_read(global_h, buf, sizeof buf, 0) == 7); + test(!strcmp("Hello!", buf)); +} + void r_k_threads(void) { run_test(test_basic_thread); + run_test(test_shared_handle); } diff --git a/src/user/lib/thread.c b/src/user/lib/thread.c index 25d98a9..f7353ad 100644 --- a/src/user/lib/thread.c +++ b/src/user/lib/thread.c @@ -4,7 +4,7 @@ #include <user/lib/thread.h> void thread_create(int flags, void (*fn)(void*), void *arg) { - if (!_syscall_fork(flags | FORK_SHAREMEM, NULL)) { + if (!_syscall_fork(flags | FORK_SHAREMEM | FORK_SHAREHANDLE, NULL)) { void *stack = malloc(4096); chstack(arg, fn, stack + 4096); } |