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 Jan 6 16:40:16 2022 |
---|
13 | // Update Count : 7 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <assert.h> |
---|
19 | #include "invoke.h" |
---|
20 | |
---|
21 | #include "coroutine.hfa" |
---|
22 | #include "kernel.hfa" |
---|
23 | #include "monitor.hfa" |
---|
24 | #include "exception.hfa" |
---|
25 | |
---|
26 | //----------------------------------------------------------------------------- |
---|
27 | // thread trait |
---|
28 | trait is_thread(T &) { |
---|
29 | void ^?{}(T& mutex this); |
---|
30 | void main(T& this); |
---|
31 | thread$ * get_thread(T& this); |
---|
32 | }; |
---|
33 | |
---|
34 | EHM_FORALL_EXCEPTION(ThreadCancelled, (thread_t &), (thread_t)) ( |
---|
35 | thread_t * the_thread; |
---|
36 | exception_t * the_exception; |
---|
37 | ); |
---|
38 | |
---|
39 | forall(T &) |
---|
40 | void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src); |
---|
41 | |
---|
42 | forall(T &) |
---|
43 | const char * msg(ThreadCancelled(T) *); |
---|
44 | |
---|
45 | // Inline getters for threads/coroutines/monitors |
---|
46 | forall( T & | is_thread(T) ) |
---|
47 | static inline coroutine$ * get_coroutine(T & this) __attribute__((const)) { return &get_thread(this)->self_cor; } |
---|
48 | |
---|
49 | forall( T & | is_thread(T) ) |
---|
50 | static inline monitor$ * get_monitor (T & this) __attribute__((const)) { return &get_thread(this)->self_mon; } |
---|
51 | |
---|
52 | static inline coroutine$ * get_coroutine(thread$ * this) __attribute__((const)) { return &this->self_cor; } |
---|
53 | static inline monitor$ * get_monitor (thread$ * this) __attribute__((const)) { return &this->self_mon; } |
---|
54 | |
---|
55 | //----------------------------------------------------------------------------- |
---|
56 | // forward declarations needed for threads |
---|
57 | extern struct cluster * mainCluster; |
---|
58 | |
---|
59 | forall( T & | is_thread(T) ) |
---|
60 | void __thrd_start( T & this, void (*)(T &) ); |
---|
61 | |
---|
62 | //----------------------------------------------------------------------------- |
---|
63 | // Ctors and dtors |
---|
64 | void ?{}(thread$ & this, const char * const name, struct cluster & cl, void * storage, size_t storageSize ); |
---|
65 | void ^?{}(thread$ & this); |
---|
66 | |
---|
67 | static inline void ?{}(thread$ & this) { this{ "Anonymous Thread", *mainCluster, 0p, DEFAULT_STACK_SIZE }; } |
---|
68 | static inline void ?{}(thread$ & this, size_t stackSize ) { this{ "Anonymous Thread", *mainCluster, 0p, stackSize }; } |
---|
69 | static inline void ?{}(thread$ & this, void * storage, size_t storageSize ) { this{ "Anonymous Thread", *mainCluster, storage, storageSize }; } |
---|
70 | static inline void ?{}(thread$ & this, struct cluster & cl ) { this{ "Anonymous Thread", cl, 0p, DEFAULT_STACK_SIZE }; } |
---|
71 | static inline void ?{}(thread$ & this, struct cluster & cl, size_t stackSize ) { this{ "Anonymous Thread", cl, 0p, stackSize }; } |
---|
72 | static inline void ?{}(thread$ & this, struct cluster & cl, void * storage, size_t storageSize ) { this{ "Anonymous Thread", cl, storage, storageSize }; } |
---|
73 | static inline void ?{}(thread$ & this, const char * const name) { this{ name, *mainCluster, 0p, DEFAULT_STACK_SIZE }; } |
---|
74 | static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl ) { this{ name, cl, 0p, DEFAULT_STACK_SIZE }; } |
---|
75 | static inline void ?{}(thread$ & this, const char * const name, struct cluster & cl, size_t stackSize ) { this{ name, cl, 0p, stackSize }; } |
---|
76 | |
---|
77 | struct thread_dtor_guard_t { |
---|
78 | monitor_dtor_guard_t mg; |
---|
79 | }; |
---|
80 | |
---|
81 | forall( T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T)) |
---|
82 | | { EHM_DEFAULT_VTABLE(ThreadCancelled, (T)); }) |
---|
83 | void ?{}( thread_dtor_guard_t & this, T & thrd, void(*)(ThreadCancelled(T) &) ); |
---|
84 | void ^?{}( thread_dtor_guard_t & this ); |
---|
85 | |
---|
86 | //----------------------------------------------------------------------------- |
---|
87 | // thread runner |
---|
88 | // Structure that actually start and stop threads |
---|
89 | forall( T & | sized(T) | is_thread(T) ) |
---|
90 | struct scoped { |
---|
91 | T handle; |
---|
92 | }; |
---|
93 | |
---|
94 | forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } ) |
---|
95 | void ?{}( scoped(T)& this ); |
---|
96 | |
---|
97 | forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) |
---|
98 | void ?{}( scoped(T)& this, P params ); |
---|
99 | |
---|
100 | forall( T & | sized(T) | is_thread(T) ) |
---|
101 | void ^?{}( scoped(T)& this ); |
---|
102 | |
---|
103 | //----------------------------------------------------------------------------- |
---|
104 | // Scheduler API |
---|
105 | |
---|
106 | //---------- |
---|
107 | // Park thread: block until corresponding call to unpark, won't block if unpark is already called |
---|
108 | void park( void ); |
---|
109 | |
---|
110 | //---------- |
---|
111 | // Unpark a thread, if the thread is already blocked, schedule it |
---|
112 | // if the thread is not yet block, signal that it should rerun immediately |
---|
113 | void unpark( thread$ * this ); |
---|
114 | |
---|
115 | forall( T & | is_thread(T) ) |
---|
116 | static inline void unpark( T & this ) { if(!&this) return; unpark( get_thread( this ) );} |
---|
117 | |
---|
118 | //---------- |
---|
119 | // Yield: force thread to block and be rescheduled |
---|
120 | bool force_yield( enum __Preemption_Reason ); |
---|
121 | |
---|
122 | //---------- |
---|
123 | // sleep: force thread to block and be rescheduled after Duration duration |
---|
124 | void sleep( Duration duration ); |
---|
125 | |
---|
126 | //---------- |
---|
127 | // join |
---|
128 | forall( T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T)) |
---|
129 | | { EHM_DEFAULT_VTABLE(ThreadCancelled, (T)); }) |
---|
130 | T & join( T & this ); |
---|
131 | |
---|
132 | // Local Variables: // |
---|
133 | // mode: c // |
---|
134 | // tab-width: 4 // |
---|
135 | // End: // |
---|