Skip to content

Commit e4dc22b

Browse files
v2 of component model threading intrinsics (#1519)
Co-authored-by: Alex Crichton <alex@alexcrichton.com>
1 parent e3bd7eb commit e4dc22b

File tree

2 files changed

+77
-58
lines changed
  • crates/c/src
  • tests/runtime-async/async/threading-builtins

2 files changed

+77
-58
lines changed

crates/c/src/lib.rs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,13 @@ void {snake}_context_set_1(void* value);
728728
uint32_t {snake}_thread_yield_cancellable(void);
729729
uint32_t {snake}_thread_index(void);
730730
uint32_t {snake}_thread_new_indirect(void (*start_function)(void*), void* arg);
731-
void {snake}_thread_switch_to(uint32_t thread);
732-
uint32_t {snake}_thread_switch_to_cancellable(uint32_t thread);
733-
void {snake}_thread_resume_later(uint32_t thread);
734-
void {snake}_thread_yield_to(uint32_t thread);
735-
uint32_t {snake}_thread_yield_to_cancellable(uint32_t thread);
731+
void {snake}_thread_suspend_to(uint32_t thread);
732+
uint32_t {snake}_thread_suspend_to_cancellable(uint32_t thread);
733+
void {snake}_thread_suspend_to_suspended(uint32_t thread);
734+
uint32_t {snake}_thread_suspend_to_suspended_cancellable(uint32_t thread);
735+
void {snake}_thread_unsuspend(uint32_t thread);
736+
void {snake}_thread_yield_to_suspended(uint32_t thread);
737+
uint32_t {snake}_thread_yield_to_suspended_cancellable(uint32_t thread);
736738
void {snake}_thread_suspend(void);
737739
uint32_t {snake}_thread_suspend_cancellable(void);
738740
"
@@ -776,39 +778,53 @@ uint32_t {snake}_thread_new_indirect(void (*start_function)(void*), void* arg) {
776778
);
777779
}}
778780
779-
__attribute__((__import_module__("$root"), __import_name__("[thread-switch-to]")))
780-
extern uint32_t __thread_switch_to(uint32_t);
781+
__attribute__((__import_module__("$root"), __import_name__("[thread-suspend-to-suspended]")))
782+
extern uint32_t __thread_suspend_to_suspended(uint32_t);
781783
782-
void {snake}_thread_switch_to(uint32_t thread) {{
783-
__thread_switch_to(thread);
784+
void {snake}_thread_suspend_to_suspended(uint32_t thread) {{
785+
__thread_suspend_to_suspended(thread);
784786
}}
785787
786-
__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-switch-to]")))
787-
extern uint32_t __thread_switch_to_cancellable(uint32_t);
788+
__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-suspend-to-suspended]")))
789+
extern uint32_t __thread_suspend_to_suspended_cancellable(uint32_t);
788790
789-
uint32_t {snake}_thread_switch_to_cancellable(uint32_t thread) {{
790-
return __thread_switch_to_cancellable(thread);
791+
uint32_t {snake}_thread_suspend_to_suspended_cancellable(uint32_t thread) {{
792+
return __thread_suspend_to_suspended_cancellable(thread);
791793
}}
792794
793-
__attribute__((__import_module__("$root"), __import_name__("[thread-resume-later]")))
794-
extern void __thread_resume_later(uint32_t);
795+
__attribute__((__import_module__("$root"), __import_name__("[thread-suspend-to]")))
796+
extern uint32_t __thread_suspend_to(uint32_t);
795797
796-
void {snake}_thread_resume_later(uint32_t thread) {{
797-
__thread_resume_later(thread);
798+
void {snake}_thread_suspend_to(uint32_t thread) {{
799+
__thread_suspend_to(thread);
798800
}}
799801
800-
__attribute__((__import_module__("$root"), __import_name__("[thread-yield-to]")))
801-
extern uint32_t __thread_yield_to(uint32_t);
802+
__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-suspend-to]")))
803+
extern uint32_t __thread_suspend_to_cancellable(uint32_t);
802804
803-
void {snake}_thread_yield_to(uint32_t thread) {{
804-
__thread_yield_to(thread);
805+
uint32_t {snake}_thread_suspend_to_cancellable(uint32_t thread) {{
806+
return __thread_suspend_to_cancellable(thread);
805807
}}
806808
807-
__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-yield-to]")))
808-
extern uint32_t __thread_yield_to_cancellable(uint32_t);
809+
__attribute__((__import_module__("$root"), __import_name__("[thread-unsuspend]")))
810+
extern void __thread_unsuspend(uint32_t);
809811
810-
uint32_t {snake}_thread_yield_to_cancellable(uint32_t thread) {{
811-
return __thread_yield_to_cancellable(thread);
812+
void {snake}_thread_unsuspend(uint32_t thread) {{
813+
__thread_unsuspend(thread);
814+
}}
815+
816+
__attribute__((__import_module__("$root"), __import_name__("[thread-yield-to-suspended]")))
817+
extern uint32_t __thread_yield_to_suspended(uint32_t);
818+
819+
void {snake}_thread_yield_to_suspended(uint32_t thread) {{
820+
__thread_yield_to_suspended(thread);
821+
}}
822+
823+
__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-yield-to-suspended]")))
824+
extern uint32_t __thread_yield_to_suspended_cancellable(uint32_t);
825+
826+
uint32_t {snake}_thread_yield_to_suspended_cancellable(uint32_t thread) {{
827+
return __thread_yield_to_suspended_cancellable(thread);
812828
}}
813829
814830
__attribute__((__import_module__("$root"), __import_name__("[thread-suspend]")))

tests/runtime-async/async/threading-builtins/test.c

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,50 @@
44
//@ ldflags = "-Wl,--export-table"
55

66
#include <assert.h>
7-
#include <test.h>
87
#include <stdio.h>
8+
#include <test.h>
99

10-
test_subtask_status_t exports_test_f() {
11-
return TEST_CALLBACK_CODE_YIELD;
12-
}
10+
test_subtask_status_t exports_test_f() { return TEST_CALLBACK_CODE_YIELD; }
1311

1412
uint32_t main_tid = 0;
1513
uint32_t spawned_tid = 0;
1614

17-
void thread_start(void* arg) {
18-
// Call all the threading builtins; the main thread will do the right thing
19-
// to resume us.
20-
test_thread_yield();
21-
test_thread_yield_cancellable();
22-
test_thread_suspend();
23-
test_thread_suspend_cancellable();
24-
test_thread_yield_to(main_tid);
25-
test_thread_yield_to_cancellable(main_tid);
26-
test_thread_switch_to(main_tid);
27-
test_thread_switch_to_cancellable(main_tid);
28-
test_thread_resume_later(main_tid);
15+
void thread_start(void *arg) {
16+
// Call all the threading builtins; the main thread will do the right thing
17+
// to resume us.
18+
test_thread_yield();
19+
test_thread_yield_cancellable();
20+
test_thread_suspend();
21+
test_thread_suspend_cancellable();
22+
test_thread_yield_to_suspended(main_tid);
23+
test_thread_yield_to_suspended_cancellable(main_tid);
24+
test_thread_suspend_to_suspended(main_tid);
25+
test_thread_suspend_to_suspended_cancellable(main_tid);
26+
test_thread_suspend_to(main_tid);
27+
test_thread_suspend_to_cancellable(main_tid);
28+
test_thread_unsuspend(main_tid);
2929
}
3030

3131
test_subtask_status_t exports_test_f_callback(test_event_t *event) {
32-
assert(event->event == TEST_EVENT_NONE);
33-
assert(event->waitable == 0);
34-
assert(event->code == 0);
35-
main_tid = test_thread_index();
36-
spawned_tid = test_thread_new_indirect(thread_start, &main_tid);
32+
assert(event->event == TEST_EVENT_NONE);
33+
assert(event->waitable == 0);
34+
assert(event->code == 0);
35+
main_tid = test_thread_index();
36+
spawned_tid = test_thread_new_indirect(thread_start, &main_tid);
3737

38-
// Now drive the other thread to completion by switching/yielding to it
39-
test_thread_yield_to(spawned_tid); // other yields
40-
test_thread_yield(); // other yields
41-
test_thread_yield(); // other suspends
42-
test_thread_yield_to(spawned_tid); // other suspends
43-
test_thread_switch_to(spawned_tid); // other yields to me
44-
test_thread_suspend(); // other yields to me
45-
test_thread_suspend(); // other switches to me
46-
test_thread_switch_to(spawned_tid); // other switches to me
47-
test_thread_switch_to(spawned_tid); // other resumes me later and terminates
48-
exports_test_f_return();
49-
return TEST_CALLBACK_CODE_EXIT;
38+
// Now drive the other thread to completion by switching/yielding to it
39+
test_thread_yield_to_suspended(spawned_tid); // other yields
40+
test_thread_yield(); // other yields
41+
test_thread_yield(); // other suspends
42+
test_thread_yield_to_suspended(spawned_tid); // other suspends
43+
test_thread_suspend_to_suspended(spawned_tid); // other yields to me
44+
test_thread_suspend(); // other yields to me
45+
test_thread_suspend(); // other suspends to me
46+
test_thread_suspend_to_suspended(spawned_tid); // other suspends to me
47+
test_thread_suspend_to_suspended(spawned_tid); // other suspends to me
48+
test_thread_suspend_to_suspended(spawned_tid); // other suspends to me
49+
test_thread_suspend_to_suspended(
50+
spawned_tid); // other unsuspends me and terminates
51+
exports_test_f_return();
52+
return TEST_CALLBACK_CODE_EXIT;
5053
}

0 commit comments

Comments
 (0)