1 | //----------------------------------------------------------------------------- |
---|
2 | // Coroutine trait |
---|
3 | // Anything that implements this trait can be resumed. |
---|
4 | // Anything that is resumed is a coroutine. |
---|
5 | trait is_coroutine(dtype T) { |
---|
6 | void main(T* this); |
---|
7 | coroutine_handle* get_handle(T* this); |
---|
8 | } |
---|
9 | |
---|
10 | //----------------------------------------------------------------------------- |
---|
11 | forall(dtype T | {coroutine_handle* T.c}) |
---|
12 | coroutine_handle* get_handle(T* this) { |
---|
13 | return this->c |
---|
14 | } |
---|
15 | |
---|
16 | //----------------------------------------------------------------------------- |
---|
17 | struct myCoroutine { |
---|
18 | int bla; |
---|
19 | coroutine_handle c; |
---|
20 | }; |
---|
21 | |
---|
22 | void main(myCoroutine* this) { |
---|
23 | sout | this->bla | endl; |
---|
24 | } |
---|
25 | |
---|
26 | void foo() { |
---|
27 | //Run the coroutine |
---|
28 | myCoroutine myc; |
---|
29 | resume(myc); |
---|
30 | } |
---|
31 | |
---|
32 | //----------------------------------------------------------------------------- |
---|
33 | // Thread trait |
---|
34 | // Alternative 1 |
---|
35 | trait is_thread(dtype T) { |
---|
36 | void main(T* this); |
---|
37 | thread_handle* get_handle(T* this); |
---|
38 | thread T; |
---|
39 | }; |
---|
40 | |
---|
41 | //----------------------------------------------------------------------------- |
---|
42 | forall(dtype T | {thread_handle* T.t}) |
---|
43 | thread_handle* get_handle(T* this) { |
---|
44 | return this->t |
---|
45 | } |
---|
46 | |
---|
47 | //----------------------------------------------------------------------------- |
---|
48 | thread myThread { |
---|
49 | int bla; |
---|
50 | thread_handle c; |
---|
51 | }; |
---|
52 | |
---|
53 | void main(myThread* this) { |
---|
54 | sout | this->bla | endl; |
---|
55 | } |
---|
56 | |
---|
57 | void foo() { |
---|
58 | //Run the thread |
---|
59 | myThread myc; |
---|
60 | } |
---|
61 | |
---|
62 | //----------------------------------------------------------------------------- |
---|
63 | // Thread trait |
---|
64 | // Alternative 2 |
---|
65 | trait is_thread(dtype T) { |
---|
66 | void main(T* this); |
---|
67 | thread_handle* get_handle(T* this); |
---|
68 | |
---|
69 | }; |
---|
70 | |
---|
71 | //----------------------------------------------------------------------------- |
---|
72 | forall(dtype T | {thread_handle* T.t}) |
---|
73 | thread_handle* get_handle(T* this) { |
---|
74 | return this->t |
---|
75 | } |
---|
76 | |
---|
77 | //----------------------------------------------------------------------------- |
---|
78 | struct myThread { |
---|
79 | int bla; |
---|
80 | thread_handle c; |
---|
81 | }; |
---|
82 | |
---|
83 | void main(myThread* this) { |
---|
84 | sout | this->bla | endl; |
---|
85 | } |
---|
86 | |
---|
87 | void foo() { |
---|
88 | //Run the thread |
---|
89 | thread(myThread) myc; |
---|
90 | } |
---|