Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 41 additions & 25 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,13 @@ void {snake}_context_set_1(void* value);
uint32_t {snake}_thread_yield_cancellable(void);
uint32_t {snake}_thread_index(void);
uint32_t {snake}_thread_new_indirect(void (*start_function)(void*), void* arg);
void {snake}_thread_switch_to(uint32_t thread);
uint32_t {snake}_thread_switch_to_cancellable(uint32_t thread);
void {snake}_thread_resume_later(uint32_t thread);
void {snake}_thread_yield_to(uint32_t thread);
uint32_t {snake}_thread_yield_to_cancellable(uint32_t thread);
void {snake}_thread_suspend_to(uint32_t thread);
uint32_t {snake}_thread_suspend_to_cancellable(uint32_t thread);
void {snake}_thread_suspend_to_suspended(uint32_t thread);
uint32_t {snake}_thread_suspend_to_suspended_cancellable(uint32_t thread);
void {snake}_thread_unsuspend(uint32_t thread);
void {snake}_thread_yield_to_suspended(uint32_t thread);
uint32_t {snake}_thread_yield_to_suspended_cancellable(uint32_t thread);
void {snake}_thread_suspend(void);
uint32_t {snake}_thread_suspend_cancellable(void);
"
Expand Down Expand Up @@ -776,39 +778,53 @@ uint32_t {snake}_thread_new_indirect(void (*start_function)(void*), void* arg) {
);
}}

__attribute__((__import_module__("$root"), __import_name__("[thread-switch-to]")))
extern uint32_t __thread_switch_to(uint32_t);
__attribute__((__import_module__("$root"), __import_name__("[thread-suspend-to-suspended]")))
extern uint32_t __thread_suspend_to_suspended(uint32_t);

void {snake}_thread_switch_to(uint32_t thread) {{
__thread_switch_to(thread);
void {snake}_thread_suspend_to_suspended(uint32_t thread) {{
__thread_suspend_to_suspended(thread);
}}

__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-switch-to]")))
extern uint32_t __thread_switch_to_cancellable(uint32_t);
__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-suspend-to-suspended]")))
extern uint32_t __thread_suspend_to_suspended_cancellable(uint32_t);

uint32_t {snake}_thread_switch_to_cancellable(uint32_t thread) {{
return __thread_switch_to_cancellable(thread);
uint32_t {snake}_thread_suspend_to_suspended_cancellable(uint32_t thread) {{
return __thread_suspend_to_suspended_cancellable(thread);
}}

__attribute__((__import_module__("$root"), __import_name__("[thread-resume-later]")))
extern void __thread_resume_later(uint32_t);
__attribute__((__import_module__("$root"), __import_name__("[thread-suspend-to]")))
extern uint32_t __thread_suspend_to(uint32_t);

void {snake}_thread_resume_later(uint32_t thread) {{
__thread_resume_later(thread);
void {snake}_thread_suspend_to(uint32_t thread) {{
__thread_suspend_to(thread);
}}

__attribute__((__import_module__("$root"), __import_name__("[thread-yield-to]")))
extern uint32_t __thread_yield_to(uint32_t);
__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-suspend-to]")))
extern uint32_t __thread_suspend_to_cancellable(uint32_t);

void {snake}_thread_yield_to(uint32_t thread) {{
__thread_yield_to(thread);
uint32_t {snake}_thread_suspend_to_cancellable(uint32_t thread) {{
return __thread_suspend_to_cancellable(thread);
}}

__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-yield-to]")))
extern uint32_t __thread_yield_to_cancellable(uint32_t);
__attribute__((__import_module__("$root"), __import_name__("[thread-unsuspend]")))
extern void __thread_unsuspend(uint32_t);

uint32_t {snake}_thread_yield_to_cancellable(uint32_t thread) {{
return __thread_yield_to_cancellable(thread);
void {snake}_thread_unsuspend(uint32_t thread) {{
__thread_unsuspend(thread);
}}

__attribute__((__import_module__("$root"), __import_name__("[thread-yield-to-suspended]")))
extern uint32_t __thread_yield_to_suspended(uint32_t);

void {snake}_thread_yield_to_suspended(uint32_t thread) {{
__thread_yield_to_suspended(thread);
}}

__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-yield-to-suspended]")))
extern uint32_t __thread_yield_to_suspended_cancellable(uint32_t);

uint32_t {snake}_thread_yield_to_suspended_cancellable(uint32_t thread) {{
return __thread_yield_to_suspended_cancellable(thread);
}}

__attribute__((__import_module__("$root"), __import_name__("[thread-suspend]")))
Expand Down
69 changes: 36 additions & 33 deletions tests/runtime-async/async/threading-builtins/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,50 @@
//@ ldflags = "-Wl,--export-table"

#include <assert.h>
#include <test.h>
#include <stdio.h>
#include <test.h>

test_subtask_status_t exports_test_f() {
return TEST_CALLBACK_CODE_YIELD;
}
test_subtask_status_t exports_test_f() { return TEST_CALLBACK_CODE_YIELD; }

uint32_t main_tid = 0;
uint32_t spawned_tid = 0;

void thread_start(void* arg) {
// Call all the threading builtins; the main thread will do the right thing
// to resume us.
test_thread_yield();
test_thread_yield_cancellable();
test_thread_suspend();
test_thread_suspend_cancellable();
test_thread_yield_to(main_tid);
test_thread_yield_to_cancellable(main_tid);
test_thread_switch_to(main_tid);
test_thread_switch_to_cancellable(main_tid);
test_thread_resume_later(main_tid);
void thread_start(void *arg) {
// Call all the threading builtins; the main thread will do the right thing
// to resume us.
test_thread_yield();
test_thread_yield_cancellable();
test_thread_suspend();
test_thread_suspend_cancellable();
test_thread_yield_to_suspended(main_tid);
test_thread_yield_to_suspended_cancellable(main_tid);
test_thread_suspend_to_suspended(main_tid);
test_thread_suspend_to_suspended_cancellable(main_tid);
test_thread_suspend_to(main_tid);
test_thread_suspend_to_cancellable(main_tid);
test_thread_unsuspend(main_tid);
}

test_subtask_status_t exports_test_f_callback(test_event_t *event) {
assert(event->event == TEST_EVENT_NONE);
assert(event->waitable == 0);
assert(event->code == 0);
main_tid = test_thread_index();
spawned_tid = test_thread_new_indirect(thread_start, &main_tid);
assert(event->event == TEST_EVENT_NONE);
assert(event->waitable == 0);
assert(event->code == 0);
main_tid = test_thread_index();
spawned_tid = test_thread_new_indirect(thread_start, &main_tid);

// Now drive the other thread to completion by switching/yielding to it
test_thread_yield_to(spawned_tid); // other yields
test_thread_yield(); // other yields
test_thread_yield(); // other suspends
test_thread_yield_to(spawned_tid); // other suspends
test_thread_switch_to(spawned_tid); // other yields to me
test_thread_suspend(); // other yields to me
test_thread_suspend(); // other switches to me
test_thread_switch_to(spawned_tid); // other switches to me
test_thread_switch_to(spawned_tid); // other resumes me later and terminates
exports_test_f_return();
return TEST_CALLBACK_CODE_EXIT;
// Now drive the other thread to completion by switching/yielding to it
test_thread_yield_to_suspended(spawned_tid); // other yields
test_thread_yield(); // other yields
test_thread_yield(); // other suspends
test_thread_yield_to_suspended(spawned_tid); // other suspends
test_thread_suspend_to_suspended(spawned_tid); // other yields to me
test_thread_suspend(); // other yields to me
test_thread_suspend(); // other suspends to me
test_thread_suspend_to_suspended(spawned_tid); // other suspends to me
test_thread_suspend_to_suspended(spawned_tid); // other suspends to me
test_thread_suspend_to_suspended(spawned_tid); // other suspends to me
test_thread_suspend_to_suspended(
spawned_tid); // other unsuspends me and terminates
exports_test_f_return();
return TEST_CALLBACK_CODE_EXIT;
}
Loading