source: libcfa/src/concurrency/invoke.h @ d29255c

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

Fixed park unpark to support park as first step of main()
Fixes #170?

  • Property mode set to 100644
File size: 8.2 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    * 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, Primed, Blocked, Ready, Active };
95        enum __Preemption_Reason { __NO_PREEMPTION, __ALARM_PREEMPTION, __POLL_PREEMPTION, __MANUAL_PREEMPTION };
96
97        struct $coroutine {
98                // context that is switch during a __cfactx_switch
99                struct __stack_context_t context;
100
101                // stack information of the coroutine
102                struct __stack_info_t stack;
103
104                // textual name for coroutine/task
105                const char * name;
106
107                // current execution status for coroutine
108                enum __Coroutine_State state;
109
110                // first coroutine to resume this one
111                struct $coroutine * starter;
112
113                // last coroutine to resume this one
114                struct $coroutine * last;
115
116                // If non-null stack must be unwound with this exception
117                struct _Unwind_Exception * cancellation;
118
119        };
120
121        static inline struct __stack_t * __get_stack( struct $coroutine * cor ) { return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2)); }
122
123        // struct which calls the monitor is accepting
124        struct __waitfor_mask_t {
125                // the index of the accepted function, -1 if none
126                short * accepted;
127
128                // list of acceptable functions, null if any
129                __cfa_anonymous_object( __small_array_t(struct __acceptable_t) );
130        };
131
132        struct $monitor {
133                // spinlock to protect internal data
134                struct __spinlock_t lock;
135
136                // current owner of the monitor
137                struct $thread * owner;
138
139                // queue of threads that are blocked waiting for the monitor
140                __queue_t(struct $thread) entry_queue;
141
142                // stack of conditions to run next once we exit the monitor
143                __stack_t(struct __condition_criterion_t) signal_stack;
144
145                // monitor routines can be called recursively, we need to keep track of that
146                unsigned int recursion;
147
148                // mask used to know if some thread is waiting for something while holding the monitor
149                struct __waitfor_mask_t mask;
150
151                // node used to signal the dtor in a waitfor dtor
152                struct __condition_node_t * dtor_node;
153        };
154
155        struct __monitor_group_t {
156                // currently held monitors
157                __cfa_anonymous_object( __small_array_t($monitor*) );
158
159                // last function that acquired monitors
160                fptr_t func;
161        };
162
163        // Link lists fields
164        // instrusive link field for threads
165        struct __thread_desc_link {
166                struct $thread * next;
167                struct $thread * prev;
168                volatile unsigned long long ts;
169        };
170
171        struct $thread {
172                // Core threading fields
173                // context that is switch during a __cfactx_switch
174                struct __stack_context_t context;
175
176                // current execution status for coroutine
177                volatile int ticket;
178                enum __Coroutine_State state:8;
179                enum __Preemption_Reason preempted:8;
180
181                //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it
182
183                // coroutine body used to store context
184                struct $coroutine  self_cor;
185
186                // current active context
187                struct $coroutine * curr_cor;
188
189                // monitor body used for mutual exclusion
190                struct $monitor    self_mon;
191
192                // pointer to monitor with sufficient lifetime for current monitors
193                struct $monitor *  self_mon_p;
194
195                // pointer to the cluster on which the thread is running
196                struct cluster * curr_cluster;
197
198                // monitors currently held by this thread
199                struct __monitor_group_t monitors;
200
201                // Link lists fields
202                // instrusive link field for threads
203                struct __thread_desc_link link;
204
205                struct {
206                        struct $thread * next;
207                        struct $thread * prev;
208                } node;
209
210                #ifdef __CFA_DEBUG__
211                        // previous function to park/unpark the thread
212                        const char * park_caller;
213                        int park_result;
214                        enum __Coroutine_State park_state;
215                        bool park_stale;
216                        const char * unpark_caller;
217                        int unpark_result;
218                        enum __Coroutine_State unpark_state;
219                        bool unpark_stale;
220                #endif
221        };
222
223        #ifdef __CFA_DEBUG__
224                void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]);
225        #else
226                #define __cfaabi_dbg_record_thrd(x, y, z)
227        #endif
228
229        #ifdef __cforall
230        extern "Cforall" {
231
232                static inline $thread *& get_next( $thread & this ) __attribute__((const)) {
233                        return this.link.next;
234                }
235
236                static inline [$thread *&, $thread *& ] __get( $thread & this ) __attribute__((const)) {
237                        return this.node.[next, prev];
238                }
239
240                static inline void ?{}(__monitor_group_t & this) {
241                        (this.data){0p};
242                        (this.size){0};
243                        (this.func){NULL};
244                }
245
246                static inline void ?{}(__monitor_group_t & this, struct $monitor ** data, __lock_size_t size, fptr_t func) {
247                        (this.data){data};
248                        (this.size){size};
249                        (this.func){func};
250                }
251
252                static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) __attribute__((const)) {
253                        if( (lhs.data != 0) != (rhs.data != 0) ) return false;
254                        if( lhs.size != rhs.size ) return false;
255                        if( lhs.func != rhs.func ) return false;
256
257                        // Check that all the monitors match
258                        for( int i = 0; i < lhs.size; i++ ) {
259                                // If not a match, check next function
260                                if( lhs[i] != rhs[i] ) return false;
261                        }
262
263                        return true;
264                }
265
266                static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) {
267                        lhs.data = rhs.data;
268                        lhs.size = rhs.size;
269                        lhs.func = rhs.func;
270                }
271        }
272        #endif
273
274#endif //_INVOKE_H_
275#else //! defined(__CFA_INVOKE_PRIVATE__)
276#ifndef _INVOKE_PRIVATE_H_
277#define _INVOKE_PRIVATE_H_
278
279        struct machine_context_t {
280                void *SP;
281                void *FP;
282                void *PC;
283        };
284
285        // assembler routines that performs the context switch
286        extern void __cfactx_invoke_stub( void );
287        extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
288        // void CtxStore ( void * this ) asm ("CtxStore");
289        // void CtxRet   ( void * dst  ) asm ("CtxRet");
290
291#endif //_INVOKE_PRIVATE_H_
292#endif //! defined(__CFA_INVOKE_PRIVATE__)
293#ifdef __cforall
294}
295#endif
296
297// Local Variables: //
298// mode: c //
299// tab-width: 4 //
300// End: //
Note: See TracBrowser for help on using the repository browser.