1 | #include <kernel> |
---|
2 | #include <stdlib> |
---|
3 | #include <threads> |
---|
4 | |
---|
5 | // Start coroutine routines |
---|
6 | extern "C" { |
---|
7 | forall(dtype T | is_coroutine(T)) |
---|
8 | void CtxInvokeCoroutine(T * this); |
---|
9 | |
---|
10 | forall(dtype T | is_coroutine(T)) |
---|
11 | void CtxStart(T * this, void ( *invoke)(T *)); |
---|
12 | |
---|
13 | forall(dtype T | is_coroutine(T)) |
---|
14 | void CtxInvokeThread(T * this); |
---|
15 | } |
---|
16 | |
---|
17 | struct MyThread { |
---|
18 | thread_h t; |
---|
19 | int value; |
---|
20 | }; |
---|
21 | |
---|
22 | void ?{}( MyThread * this, int value ) { |
---|
23 | this->value = value; |
---|
24 | } |
---|
25 | |
---|
26 | void ^?{}( MyThread * this ) {} |
---|
27 | |
---|
28 | void main(MyThread* this) { |
---|
29 | printf("Main called with %p\n", this); |
---|
30 | printf("Thread value is %d\n", this->value); |
---|
31 | } |
---|
32 | |
---|
33 | thread_h* get_thread(MyThread* this) { |
---|
34 | return &this->t; |
---|
35 | } |
---|
36 | |
---|
37 | coroutine* get_coroutine(MyThread* this) { |
---|
38 | return &this->t.c; |
---|
39 | } |
---|
40 | |
---|
41 | void ?{}( MyThread * this ) { |
---|
42 | this->value = 1; |
---|
43 | printf("MyThread created\n"); |
---|
44 | printf("Address %p\n", this); |
---|
45 | printf("handle %p\n", get_thread(this)); |
---|
46 | printf("main %p\n", main); |
---|
47 | printf("get_t %p\n", get_thread); |
---|
48 | printf("invoke %p\n", CtxInvokeThread); |
---|
49 | } |
---|
50 | |
---|
51 | int main() { |
---|
52 | |
---|
53 | printf("Main is %p\n", this_coroutine()); |
---|
54 | |
---|
55 | printf("Creating thread\n"); |
---|
56 | |
---|
57 | thread(MyThread) thread1; |
---|
58 | |
---|
59 | printf("Main is %p\n", this_coroutine()); |
---|
60 | |
---|
61 | printf("First thread created\n"); |
---|
62 | |
---|
63 | kernel_run(); |
---|
64 | |
---|
65 | return 0; |
---|
66 | } |
---|