source: libcfa/src/concurrency/invoke.h @ 04b5cef

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

Minor improvements to alignments and memory layout

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[8def349]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
[6b0b624]11// Last Modified By : Peter A. Buhr
[09d4b22]12// Last Modified On : Thu Dec  5 16:26:03 2019
13// Update Count     : 44
[8def349]14//
15
[73abe95]16#include "bits/containers.hfa"
17#include "bits/defs.hfa"
18#include "bits/locks.hfa"
[78b3f52]19
[0cf5b79]20#ifdef __cforall
[5c81105]21extern "C" {
22#endif
23
24#if ! defined(__CFA_INVOKE_PRIVATE__)
[78b3f52]25#ifndef _INVOKE_H_
26#define _INVOKE_H_
27
[597c34a3]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
[0cf5b79]46        #ifdef __cforall
[025278e]47        extern "Cforall" {
[21184e3]48                extern __attribute__((aligned(128))) thread_local struct KernelThreadData {
[ac2b598]49                        struct $thread    * volatile this_thread;
[8834751]50                        struct processor  * volatile this_processor;
51                        struct __stats_t  * volatile this_stats;
[b10affd]52
53                        struct {
54                                volatile unsigned short disable_count;
55                                volatile bool enabled;
56                                volatile bool in_progress;
57                        } preemption_state;
[21184e3]58
59                        uint32_t rand_seed;
[afc2427]60                } kernelTLS __attribute__ ((tls_model ( "initial-exec" )));
[025278e]61        }
[4c1b48f3]62        #endif
[025278e]63
[b2f6113]64        struct __stack_context_t {
65                void * SP;
66                void * FP;
67        };
68
69        // low adresses  :           +----------------------+ <- start of allocation
70        //                           |  optional guard page |
71        //                           +----------------------+ <- __stack_t.limit
72        //                           |                      |
73        //                           |       /\ /\ /\       |
74        //                           |       || || ||       |
75        //                           |                      |
76        //                           |    program  stack    |
77        //                           |                      |
78        // __stack_info_t.storage -> +----------------------+ <- __stack_t.base
79        //                           |      __stack_t       |
80        // high adresses :           +----------------------+ <- end of allocation
81
82        struct __stack_t {
83                // stack grows towards stack limit
84                void * limit;
85
86                // base of stack
87                void * base;
88        };
89
90        struct __stack_info_t {
91                // pointer to stack
92                struct __stack_t * storage;
[025278e]93        };
94
[ff79d5e]95        enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active };
[b0c7419]96        enum __Preemption_Reason { __NO_PREEMPTION, __ALARM_PREEMPTION, __POLL_PREEMPTION, __MANUAL_PREEMPTION };
[025278e]97
[ac2b598]98        struct $coroutine {
[c7a900a]99                // context that is switch during a __cfactx_switch
[b2f6113]100                struct __stack_context_t context;
101
[76e069f]102                // stack information of the coroutine
[b2f6113]103                struct __stack_info_t stack;
[76e069f]104
[e8e457e]105                // textual name for coroutine/task
[76e069f]106                const char * name;
107
108                // current execution status for coroutine
[ff79d5e]109                enum __Coroutine_State state;
[b2f6113]110
[76e069f]111                // first coroutine to resume this one
[ac2b598]112                struct $coroutine * starter;
[76e069f]113
114                // last coroutine to resume this one
[ac2b598]115                struct $coroutine * last;
[76e069f]116
117                // If non-null stack must be unwound with this exception
118                struct _Unwind_Exception * cancellation;
119
[025278e]120        };
121
[210b8b3]122        static inline struct __stack_t * __get_stack( struct $coroutine * cor ) { return (struct __stack_t*)(((uintptr_t)cor->stack.storage) & ((uintptr_t)-2)); }
123
[f23b685]124        // struct which calls the monitor is accepting
[025278e]125        struct __waitfor_mask_t {
126                // the index of the accepted function, -1 if none
127                short * accepted;
128
129                // list of acceptable functions, null if any
[b1672e1]130                __cfa_anonymous_object( __small_array_t(struct __acceptable_t) );
[025278e]131        };
132
[ac2b598]133        struct $monitor {
[025278e]134                // spinlock to protect internal data
[ea7d2b0]135                struct __spinlock_t lock;
[025278e]136
137                // current owner of the monitor
[ac2b598]138                struct $thread * owner;
[025278e]139
140                // queue of threads that are blocked waiting for the monitor
[ac2b598]141                __queue_t(struct $thread) entry_queue;
[025278e]142
143                // stack of conditions to run next once we exit the monitor
[0cf5b79]144                __stack_t(struct __condition_criterion_t) signal_stack;
[025278e]145
146                // monitor routines can be called recursively, we need to keep track of that
147                unsigned int recursion;
148
149                // mask used to know if some thread is waiting for something while holding the monitor
150                struct __waitfor_mask_t mask;
151
152                // node used to signal the dtor in a waitfor dtor
153                struct __condition_node_t * dtor_node;
154        };
155
156        struct __monitor_group_t {
157                // currently held monitors
[ac2b598]158                __cfa_anonymous_object( __small_array_t($monitor*) );
[025278e]159
160                // last function that acquired monitors
[c1a9c86]161                fptr_t func;
[025278e]162        };
163
[b798713]164        // Link lists fields
165        // instrusive link field for threads
166        struct __thread_desc_link {
[6a490b2]167                struct $thread * next;
168                struct $thread * prev;
[1b143de]169                volatile unsigned long long ts;
[b798713]170        };
171
[ac2b598]172        struct $thread {
[025278e]173                // Core threading fields
[c7a900a]174                // context that is switch during a __cfactx_switch
[e8e457e]175                struct __stack_context_t context;
176
177                // current execution status for coroutine
[ff79d5e]178                volatile int ticket;
179                enum __Coroutine_State state:8;
180                enum __Preemption_Reason preempted:8;
[e8e457e]181
[5c1a531]182                //SKULLDUGGERY errno is not save in the thread data structure because returnToKernel appears to be the only function to require saving and restoring it
183
[37ba662]184                // pointer to the cluster on which the thread is running
185                struct cluster * curr_cluster;
186
187                // Link lists fields
188                // instrusive link field for threads
189                struct __thread_desc_link link;
190
[025278e]191                // coroutine body used to store context
[ac2b598]192                struct $coroutine  self_cor;
[025278e]193
[82c948c]194                // current active context
[ac2b598]195                struct $coroutine * curr_cor;
[82c948c]196
[025278e]197                // monitor body used for mutual exclusion
[ac2b598]198                struct $monitor    self_mon;
[025278e]199
200                // pointer to monitor with sufficient lifetime for current monitors
[ac2b598]201                struct $monitor *  self_mon_p;
[025278e]202
203                // monitors currently held by this thread
204                struct __monitor_group_t monitors;
205
[de94a60]206                struct {
[ac2b598]207                        struct $thread * next;
208                        struct $thread * prev;
[de94a60]209                } node;
[ae66348]210
211                #ifdef __CFA_DEBUG__
212                        // previous function to park/unpark the thread
[ae7be7a]213                        const char * park_caller;
[ff79d5e]214                        int park_result;
215                        enum __Coroutine_State park_state;
[ae66348]216                        bool park_stale;
[ae7be7a]217                        const char * unpark_caller;
[ff79d5e]218                        int unpark_result;
219                        enum __Coroutine_State unpark_state;
[ae66348]220                        bool unpark_stale;
221                #endif
[212c2187]222        };
223
[ae66348]224        #ifdef __CFA_DEBUG__
225                void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]);
226        #else
227                #define __cfaabi_dbg_record_thrd(x, y, z)
228        #endif
229
[212c2187]230        #ifdef __cforall
231        extern "Cforall" {
[6a490b2]232
[ac2b598]233                static inline $thread *& get_next( $thread & this ) __attribute__((const)) {
[b798713]234                        return this.link.next;
[0cf5b79]235                }
236
[ac2b598]237                static inline [$thread *&, $thread *& ] __get( $thread & this ) __attribute__((const)) {
[de94a60]238                        return this.node.[next, prev];
239                }
240
[0cf5b79]241                static inline void ?{}(__monitor_group_t & this) {
[121be3e]242                        (this.data){0p};
[0cf5b79]243                        (this.size){0};
244                        (this.func){NULL};
245                }
246
[ac2b598]247                static inline void ?{}(__monitor_group_t & this, struct $monitor ** data, __lock_size_t size, fptr_t func) {
[0cf5b79]248                        (this.data){data};
249                        (this.size){size};
250                        (this.func){func};
[025278e]251                }
252
[8c50aed]253                static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) __attribute__((const)) {
[0cf5b79]254                        if( (lhs.data != 0) != (rhs.data != 0) ) return false;
[025278e]255                        if( lhs.size != rhs.size ) return false;
256                        if( lhs.func != rhs.func ) return false;
257
258                        // Check that all the monitors match
259                        for( int i = 0; i < lhs.size; i++ ) {
260                                // If not a match, check next function
261                                if( lhs[i] != rhs[i] ) return false;
262                        }
263
264                        return true;
265                }
[0cf5b79]266
267                static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) {
268                        lhs.data = rhs.data;
269                        lhs.size = rhs.size;
270                        lhs.func = rhs.func;
271                }
[025278e]272        }
273        #endif
[b18830e]274
[5c81105]275#endif //_INVOKE_H_
276#else //! defined(__CFA_INVOKE_PRIVATE__)
277#ifndef _INVOKE_PRIVATE_H_
278#define _INVOKE_PRIVATE_H_
[b227f68]279
[025278e]280        struct machine_context_t {
281                void *SP;
282                void *FP;
283                void *PC;
284        };
285
286        // assembler routines that performs the context switch
[c7a900a]287        extern void __cfactx_invoke_stub( void );
288        extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
[212c2187]289        // void CtxStore ( void * this ) asm ("CtxStore");
290        // void CtxRet   ( void * dst  ) asm ("CtxRet");
[025278e]291
[5c81105]292#endif //_INVOKE_PRIVATE_H_
293#endif //! defined(__CFA_INVOKE_PRIVATE__)
[0cf5b79]294#ifdef __cforall
[5c81105]295}
296#endif
[6b0b624]297
298// Local Variables: //
299// mode: c //
300// tab-width: 4 //
301// End: //
Note: See TracBrowser for help on using the repository browser.