1 | // -*- Mode: CFA -*- |
---|
2 | // |
---|
3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
4 | // |
---|
5 | // The contents of this file are covered under the licence agreement in the |
---|
6 | // file "LICENCE" distributed with Cforall. |
---|
7 | // |
---|
8 | // threads -- |
---|
9 | // |
---|
10 | // Author : Thierry Delisle |
---|
11 | // Created On : Tue Jan 17 12:27:26 2016 |
---|
12 | // Last Modified By : Thierry Delisle |
---|
13 | // Last Modified On : -- |
---|
14 | // Update Count : 0 |
---|
15 | // |
---|
16 | |
---|
17 | #ifndef THREADS_H |
---|
18 | #define THREADS_H |
---|
19 | |
---|
20 | #include "assert" |
---|
21 | #include "invoke.h" |
---|
22 | |
---|
23 | #include "coroutines" |
---|
24 | |
---|
25 | //----------------------------------------------------------------------------- |
---|
26 | // Coroutine trait |
---|
27 | // Anything that implements this trait can be resumed. |
---|
28 | // Anything that is resumed is a coroutine. |
---|
29 | trait is_thread(dtype T /*| sized(T)*/) { |
---|
30 | void main(T* this); |
---|
31 | thread_h* get_thread(T* this); |
---|
32 | /*void ?{}(T*); |
---|
33 | void ^?{}(T*);*/ |
---|
34 | }; |
---|
35 | |
---|
36 | #define DECL_THREAD(X) static inline thread_h* get_thread(X* this) { return &this->t; } void main(X* this); |
---|
37 | |
---|
38 | forall(otype T | is_thread(T) ) |
---|
39 | static inline coroutine* get_coroutine(T* this) { |
---|
40 | return &get_thread(this)->c; |
---|
41 | } |
---|
42 | |
---|
43 | static inline coroutine* get_coroutine(thread_h* this) { |
---|
44 | return &this->c; |
---|
45 | } |
---|
46 | |
---|
47 | thread_h * this_thread(void); |
---|
48 | |
---|
49 | //----------------------------------------------------------------------------- |
---|
50 | // Ctors and dtors |
---|
51 | void ?{}(thread_h* this); |
---|
52 | void ^?{}(thread_h* this); |
---|
53 | |
---|
54 | //----------------------------------------------------------------------------- |
---|
55 | // thread runner |
---|
56 | // Structure that actually start and stop threads |
---|
57 | forall(otype T | is_thread(T) ) |
---|
58 | struct thread { |
---|
59 | T handle; |
---|
60 | }; |
---|
61 | |
---|
62 | forall(otype T | is_thread(T) ) |
---|
63 | void ?{}( thread(T)* this ); |
---|
64 | |
---|
65 | forall(otype T, ttype P | is_thread(T) | { void ?{}(T*, P); } ) |
---|
66 | void ?{}( thread(T)* this, P params ); |
---|
67 | |
---|
68 | forall(otype T | is_thread(T) ) |
---|
69 | void ^?{}( thread(T)* this ); |
---|
70 | |
---|
71 | void yield(); |
---|
72 | |
---|
73 | #endif //THREADS_H |
---|
74 | |
---|
75 | // Local Variables: // |
---|
76 | // mode: c // |
---|
77 | // tab-width: 4 // |
---|
78 | // End: // |
---|