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 | // thread -- |
---|
9 | // |
---|
10 | // Author : Thierry Delisle |
---|
11 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
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 "coroutine" |
---|
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) { |
---|
30 | void ^?{}(T* this); |
---|
31 | void main(T* this); |
---|
32 | thread_desc* get_thread(T* this); |
---|
33 | }; |
---|
34 | |
---|
35 | #define DECL_THREAD(X) thread_desc* get_thread(X* this) { return &this->__thrd; } void main(X* this) |
---|
36 | |
---|
37 | forall( dtype T | is_thread(T) ) |
---|
38 | static inline coroutine_desc* get_coroutine(T* this) { |
---|
39 | return &get_thread(this)->cor; |
---|
40 | } |
---|
41 | |
---|
42 | static inline coroutine_desc* get_coroutine(thread_desc* this) { |
---|
43 | return &this->cor; |
---|
44 | } |
---|
45 | |
---|
46 | thread_desc * this_thread(void); |
---|
47 | |
---|
48 | //----------------------------------------------------------------------------- |
---|
49 | // Ctors and dtors |
---|
50 | void ?{}(thread_desc* this); |
---|
51 | void ^?{}(thread_desc* this); |
---|
52 | |
---|
53 | //----------------------------------------------------------------------------- |
---|
54 | // thread runner |
---|
55 | // Structure that actually start and stop threads |
---|
56 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
57 | struct scoped { |
---|
58 | T handle; |
---|
59 | }; |
---|
60 | |
---|
61 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } ) |
---|
62 | void ?{}( scoped(T)* this ); |
---|
63 | |
---|
64 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } ) |
---|
65 | void ?{}( scoped(T)* this, P params ); |
---|
66 | |
---|
67 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
68 | void ^?{}( scoped(T)* this ); |
---|
69 | |
---|
70 | void yield(); |
---|
71 | |
---|
72 | #endif //THREADS_H |
---|
73 | |
---|
74 | // Local Variables: // |
---|
75 | // mode: c // |
---|
76 | // tab-width: 4 // |
---|
77 | // End: // |
---|