source: libcfa/src/concurrency/invoke.h @ 75ca7f4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 75ca7f4 was f80f840, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Merge branch 'master' into relaxed_ready

  • Property mode set to 100644
File size: 7.4 KB
Line 
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// invoke.h --
8//
9// Author           : Thierry Delisle
10// Created On       : Tue Jan 17 12:27:26 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Dec  5 16:26:03 2019
13// Update Count     : 44
14//
15
16#include "bits/containers.hfa"
17#include "bits/defs.hfa"
18#include "bits/locks.hfa"
19
20#ifdef __cforall
21extern "C" {
22#endif
23
24#if ! defined(__CFA_INVOKE_PRIVATE__)
25#ifndef _INVOKE_H_
26#define _INVOKE_H_
27
28#ifdef __ARM_ARCH
29        // function prototypes are only really used by these macros on ARM
30        void disable_global_interrupts();
31        void enable_global_interrupts();
32
33        #define TL_GET( member ) ( { __typeof__( kernelTLS.member ) target; \
34                disable_global_interrupts(); \
35                target = kernelTLS.member; \
36                enable_global_interrupts(); \
37                target; } )
38        #define TL_SET( member, value ) disable_global_interrupts(); \
39                kernelTLS.member = value; \
40                enable_global_interrupts();
41#else
42        #define TL_GET( member ) kernelTLS.member
43        #define TL_SET( member, value ) kernelTLS.member = value;
44#endif
45
46        #ifdef __cforall
47        extern "Cforall" {
48                extern __attribute__((aligned(128))) thread_local struct KernelThreadData {
49                        struct thread_desc    * volatile this_thread;
50                        struct processor      * volatile this_processor;
51
52                        struct {
53                                volatile unsigned short disable_count;
54                                volatile bool enabled;
55                                volatile bool in_progress;
56                        } preemption_state;
57
58                        uint32_t rand_seed;
59                } kernelTLS __attribute__ ((tls_model ( "initial-exec" )));
60        }
61        #endif
62
63        struct __stack_context_t {
64                void * SP;
65                void * FP;
66        };
67
68        // low adresses  :           +----------------------+ <- start of allocation
69        //                           |  optional guard page |
70        //                           +----------------------+ <- __stack_t.limit
71        //                           |                      |
72        //                           |       /\ /\ /\       |
73        //                           |       || || ||       |
74        //                           |                      |
75        //                           |    program  stack    |
76        //                           |                      |
77        // __stack_info_t.storage -> +----------------------+ <- __stack_t.base
78        //                           |      __stack_t       |
79        // high adresses :           +----------------------+ <- end of allocation
80
81        struct __stack_t {
82                // stack grows towards stack limit
83                void * limit;
84
85                // base of stack
86                void * base;
87        };
88
89        struct __stack_info_t {
90                // pointer to stack
91                struct __stack_t * storage;
92        };
93
94        enum coroutine_state { Halted, Start, Inactive, Active, Primed };
95
96        struct coroutine_desc {
97                // context that is switch during a CtxSwitch
98                struct __stack_context_t context;
99
100                // stack information of the coroutine
101                struct __stack_info_t stack;
102
103                // textual name for coroutine/task
104                const char * name;
105
106                // current execution status for coroutine
107                enum coroutine_state state;
108
109                // first coroutine to resume this one
110                struct coroutine_desc * starter;
111
112                // last coroutine to resume this one
113                struct coroutine_desc * last;
114
115                // If non-null stack must be unwound with this exception
116                struct _Unwind_Exception * cancellation;
117
118        };
119
120        // struct which calls the monitor is accepting
121        struct __waitfor_mask_t {
122                // the index of the accepted function, -1 if none
123                short * accepted;
124
125                // list of acceptable functions, null if any
126                __cfa_anonymous_object( __small_array_t(struct __acceptable_t) );
127        };
128
129        struct monitor_desc {
130                // spinlock to protect internal data
131                struct __spinlock_t lock;
132
133                // current owner of the monitor
134                struct thread_desc * owner;
135
136                // queue of threads that are blocked waiting for the monitor
137                __queue_t(struct thread_desc) entry_queue;
138
139                // stack of conditions to run next once we exit the monitor
140                __stack_t(struct __condition_criterion_t) signal_stack;
141
142                // monitor routines can be called recursively, we need to keep track of that
143                unsigned int recursion;
144
145                // mask used to know if some thread is waiting for something while holding the monitor
146                struct __waitfor_mask_t mask;
147
148                // node used to signal the dtor in a waitfor dtor
149                struct __condition_node_t * dtor_node;
150        };
151
152        struct __monitor_group_t {
153                // currently held monitors
154                __cfa_anonymous_object( __small_array_t(monitor_desc*) );
155
156                // last function that acquired monitors
157                fptr_t func;
158        };
159
160        // Link lists fields
161        // instrusive link field for threads
162        struct __thread_desc_link {
163                struct thread_desc * next;
164                struct thread_desc * prev;
165                unsigned long long ts;
166        };
167
168        struct thread_desc {
169                // Core threading fields
170                // context that is switch during a CtxSwitch
171                struct __stack_context_t context;
172
173                // current execution status for coroutine
174                enum coroutine_state state;
175
176                //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it
177
178                // coroutine body used to store context
179                struct coroutine_desc  self_cor;
180
181                // current active context
182                struct coroutine_desc * curr_cor;
183
184                // monitor body used for mutual exclusion
185                struct monitor_desc    self_mon;
186
187                // pointer to monitor with sufficient lifetime for current monitors
188                struct monitor_desc *  self_mon_p;
189
190                // pointer to the cluster on which the thread is running
191                struct cluster * curr_cluster;
192
193                // monitors currently held by this thread
194                struct __monitor_group_t monitors;
195
196                // Link lists fields
197                // instrusive link field for threads
198                struct __thread_desc_link link;
199
200                struct {
201                        struct thread_desc * next;
202                        struct thread_desc * prev;
203                } node;
204        };
205
206        #ifdef __cforall
207        extern "Cforall" {
208                static inline thread_desc *& get_next( thread_desc & this ) {
209                        return this.link.next;
210                }
211
212                static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) {
213                        return this.node.[next, prev];
214                }
215
216                static inline void ?{}(__monitor_group_t & this) {
217                        (this.data){0p};
218                        (this.size){0};
219                        (this.func){NULL};
220                }
221
222                static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) {
223                        (this.data){data};
224                        (this.size){size};
225                        (this.func){func};
226                }
227
228                static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) {
229                        if( (lhs.data != 0) != (rhs.data != 0) ) return false;
230                        if( lhs.size != rhs.size ) return false;
231                        if( lhs.func != rhs.func ) return false;
232
233                        // Check that all the monitors match
234                        for( int i = 0; i < lhs.size; i++ ) {
235                                // If not a match, check next function
236                                if( lhs[i] != rhs[i] ) return false;
237                        }
238
239                        return true;
240                }
241
242                static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) {
243                        lhs.data = rhs.data;
244                        lhs.size = rhs.size;
245                        lhs.func = rhs.func;
246                }
247        }
248        #endif
249
250#endif //_INVOKE_H_
251#else //! defined(__CFA_INVOKE_PRIVATE__)
252#ifndef _INVOKE_PRIVATE_H_
253#define _INVOKE_PRIVATE_H_
254
255        struct machine_context_t {
256                void *SP;
257                void *FP;
258                void *PC;
259        };
260
261        // assembler routines that performs the context switch
262        extern void CtxInvokeStub( void );
263        extern void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch");
264        // void CtxStore ( void * this ) asm ("CtxStore");
265        // void CtxRet   ( void * dst  ) asm ("CtxRet");
266
267#endif //_INVOKE_PRIVATE_H_
268#endif //! defined(__CFA_INVOKE_PRIVATE__)
269#ifdef __cforall
270}
271#endif
272
273// Local Variables: //
274// mode: c //
275// tab-width: 4 //
276// End: //
Note: See TracBrowser for help on using the repository browser.