1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // thread -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Mar 29 14:07:11 2018 |
---|
13 | // Update Count : 4 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <assert.h> |
---|
19 | #include "invoke.h" |
---|
20 | |
---|
21 | #include "coroutine" |
---|
22 | #include "monitor" |
---|
23 | |
---|
24 | //----------------------------------------------------------------------------- |
---|
25 | // thread trait |
---|
26 | trait is_thread(dtype T) { |
---|
27 | void ^?{}(T& mutex this); |
---|
28 | void main(T& this); |
---|
29 | thread_desc* get_thread(T& this); |
---|
30 | }; |
---|
31 | |
---|
32 | #define DECL_THREAD(X) thread_desc* get_thread(X& this) { return &this.__thrd; } void main(X& this) |
---|
33 | |
---|
34 | forall( dtype T | is_thread(T) ) |
---|
35 | static inline coroutine_desc* get_coroutine(T & this) { |
---|
36 | return &get_thread(this)->self_cor; |
---|
37 | } |
---|
38 | |
---|
39 | forall( dtype T | is_thread(T) ) |
---|
40 | static inline monitor_desc* get_monitor(T & this) { |
---|
41 | return &get_thread(this)->self_mon; |
---|
42 | } |
---|
43 | |
---|
44 | static inline coroutine_desc* get_coroutine(thread_desc * this) { |
---|
45 | return &this->self_cor; |
---|
46 | } |
---|
47 | |
---|
48 | static inline monitor_desc* get_monitor(thread_desc * this) { |
---|
49 | return &this->self_mon; |
---|
50 | } |
---|
51 | |
---|
52 | extern struct cluster * mainCluster; |
---|
53 | |
---|
54 | forall( dtype T | is_thread(T) ) |
---|
55 | void __thrd_start( T & this ); |
---|
56 | |
---|
57 | //----------------------------------------------------------------------------- |
---|
58 | // Ctors and dtors |
---|
59 | void ?{}(thread_desc & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize ); |
---|
60 | void ^?{}(thread_desc & this); |
---|
61 | |
---|
62 | static inline void ?{}(thread_desc & this) { this{ "Anonymous Thread", *mainCluster, NULL, 0 }; } |
---|
63 | static inline void ?{}(thread_desc & this, size_t stackSize ) { this{ "Anonymous Thread", *mainCluster, NULL, stackSize }; } |
---|
64 | static inline void ?{}(thread_desc & this, void * storage, size_t storageSize ) { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; } |
---|
65 | static inline void ?{}(thread_desc & this, struct cluster & cl ) { this{ "Anonymous Thread", cl, NULL, 0 }; } |
---|
66 | static inline void ?{}(thread_desc & this, struct cluster & cl, size_t stackSize ) { this{ "Anonymous Thread", cl, 0, stackSize }; } |
---|
67 | static inline void ?{}(thread_desc & this, struct cluster & cl, void * storage, size_t storageSize ) { this{ "Anonymous Thread", cl, storage, storageSize }; } |
---|
68 | static inline void ?{}(thread_desc & this, const char * const name) { this{ name, *mainCluster, NULL, 0 }; } |
---|
69 | static inline void ?{}(thread_desc & this, const char * const name, struct cluster & cl ) { this{ name, cl, NULL, 0 }; } |
---|
70 | static inline void ?{}(thread_desc & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, NULL, stackSize }; } |
---|
71 | |
---|
72 | //----------------------------------------------------------------------------- |
---|
73 | // thread runner |
---|
74 | // Structure that actually start and stop threads |
---|
75 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
76 | struct scoped { |
---|
77 | T handle; |
---|
78 | }; |
---|
79 | |
---|
80 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) |
---|
81 | void ?{}( scoped(T)& this ); |
---|
82 | |
---|
83 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) |
---|
84 | void ?{}( scoped(T)& this, P params ); |
---|
85 | |
---|
86 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
87 | void ^?{}( scoped(T)& this ); |
---|
88 | |
---|
89 | void yield(); |
---|
90 | void yield( unsigned times ); |
---|
91 | |
---|
92 | // Local Variables: // |
---|
93 | // mode: c // |
---|
94 | // tab-width: 4 // |
---|
95 | // End: // |
---|