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

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since bee653c was 76e069f, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Implemented stack unwinding on coroutine termination

  • Property mode set to 100644
File size: 7.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 : Sat May 19 08:23:21 2018
13// Update Count     : 31
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                static inline struct thread_desc             * & get_next( struct thread_desc             & this );
49                static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this );
50
51                extern thread_local struct KernelThreadData {
52                        struct coroutine_desc * volatile this_coroutine;
53                        struct thread_desc    * volatile this_thread;
54                        struct processor      * volatile this_processor;
55
56                        struct {
57                                volatile unsigned short disable_count;
58                                volatile bool enabled;
59                                volatile bool in_progress;
60                        } preemption_state;
61                } kernelTLS __attribute__ ((tls_model ( "initial-exec" )));
62        }
63
64        static inline struct coroutine_desc * volatile active_coroutine() { return TL_GET( this_coroutine ); }
65        static inline struct thread_desc    * volatile active_thread   () { return TL_GET( this_thread    ); }
66        static inline struct processor      * volatile active_processor() { return TL_GET( this_processor ); } // UNSAFE
67        #endif
68
69        struct coStack_t {
70                size_t size;                                                                    // size of stack
71                void * storage;                                                                 // pointer to stack
72                void * limit;                                                                   // stack grows towards stack limit
73                void * base;                                                                    // base of stack
74                void * context;                                                                 // address of cfa_context_t
75                void * top;                                                                             // address of top of storage
76                bool userStack;                                                                 // whether or not the user allocated the stack
77        };
78
79        enum coroutine_state { Halted, Start, Inactive, Active, Primed };
80
81        struct coroutine_desc {
82                // stack information of the coroutine
83                struct coStack_t stack;
84
85                // textual name for coroutine/task, initialized by uC++ generated code
86                const char * name;
87
88                // copy of global UNIX variable errno
89                int errno_;
90
91                // current execution status for coroutine
92                enum coroutine_state state;
93                // first coroutine to resume this one
94                struct coroutine_desc * starter;
95
96                // last coroutine to resume this one
97                struct coroutine_desc * last;
98
99                // If non-null stack must be unwound with this exception
100                struct _Unwind_Exception * cancellation;
101
102        };
103
104        // struct which calls the monitor is accepting
105        struct __waitfor_mask_t {
106                // the index of the accepted function, -1 if none
107                short * accepted;
108
109                // list of acceptable functions, null if any
110                __cfa_anonymous_object( __small_array_t(struct __acceptable_t) );
111        };
112
113        struct monitor_desc {
114                // spinlock to protect internal data
115                struct __spinlock_t lock;
116
117                // current owner of the monitor
118                struct thread_desc * owner;
119
120                // queue of threads that are blocked waiting for the monitor
121                __queue_t(struct thread_desc) entry_queue;
122
123                // stack of conditions to run next once we exit the monitor
124                __stack_t(struct __condition_criterion_t) signal_stack;
125
126                // monitor routines can be called recursively, we need to keep track of that
127                unsigned int recursion;
128
129                // mask used to know if some thread is waiting for something while holding the monitor
130                struct __waitfor_mask_t mask;
131
132                // node used to signal the dtor in a waitfor dtor
133                struct __condition_node_t * dtor_node;
134        };
135
136        struct __monitor_group_t {
137                // currently held monitors
138                __cfa_anonymous_object( __small_array_t(monitor_desc*) );
139
140                // last function that acquired monitors
141                fptr_t func;
142        };
143
144        struct thread_desc {
145                // Core threading fields
146                // coroutine body used to store context
147                struct coroutine_desc  self_cor;
148
149                // current active context
150                struct coroutine_desc * curr_cor;
151
152                // monitor body used for mutual exclusion
153                struct monitor_desc    self_mon;
154
155                // pointer to monitor with sufficient lifetime for current monitors
156                struct monitor_desc *  self_mon_p;
157
158                // pointer to the cluster on which the thread is running
159                struct cluster * curr_cluster;
160
161                // monitors currently held by this thread
162                struct __monitor_group_t monitors;
163
164                // Link lists fields
165                // instrusive link field for threads
166                struct thread_desc * next;
167
168                struct {
169                        struct thread_desc * next;
170                        struct thread_desc * prev;
171                } node;
172     };
173
174     #ifdef __cforall
175     extern "Cforall" {
176                static inline thread_desc * & get_next( thread_desc & this ) {
177                        return this.next;
178                }
179
180                static inline [thread_desc *&, thread_desc *& ] __get( thread_desc & this ) {
181                        return this.node.[next, prev];
182                }
183
184                static inline struct __condition_criterion_t * & get_next( struct __condition_criterion_t & this );
185
186                static inline void ?{}(__monitor_group_t & this) {
187                        (this.data){NULL};
188                        (this.size){0};
189                        (this.func){NULL};
190                }
191
192                static inline void ?{}(__monitor_group_t & this, struct monitor_desc ** data, __lock_size_t size, fptr_t func) {
193                        (this.data){data};
194                        (this.size){size};
195                        (this.func){func};
196                }
197
198                static inline bool ?==?( const __monitor_group_t & lhs, const __monitor_group_t & rhs ) {
199                        if( (lhs.data != 0) != (rhs.data != 0) ) return false;
200                        if( lhs.size != rhs.size ) return false;
201                        if( lhs.func != rhs.func ) return false;
202
203                        // Check that all the monitors match
204                        for( int i = 0; i < lhs.size; i++ ) {
205                                // If not a match, check next function
206                                if( lhs[i] != rhs[i] ) return false;
207                        }
208
209                        return true;
210                }
211
212                static inline void ?=?(__monitor_group_t & lhs, const __monitor_group_t & rhs) {
213                        lhs.data = rhs.data;
214                        lhs.size = rhs.size;
215                        lhs.func = rhs.func;
216                }
217        }
218        #endif
219
220#endif //_INVOKE_H_
221#else //! defined(__CFA_INVOKE_PRIVATE__)
222#ifndef _INVOKE_PRIVATE_H_
223#define _INVOKE_PRIVATE_H_
224
225        struct machine_context_t {
226                void *SP;
227                void *FP;
228                void *PC;
229        };
230
231        // assembler routines that performs the context switch
232        extern void CtxInvokeStub( void );
233        void CtxSwitch( void * from, void * to ) asm ("CtxSwitch");
234
235        #if   defined( __i386 )
236        #define CtxGet( ctx ) __asm__ ( \
237                        "movl %%esp,%0\n"   \
238                        "movl %%ebp,%1\n"   \
239                : "=rm" (ctx.SP), "=rm" (ctx.FP) )
240        #elif defined( __x86_64 )
241        #define CtxGet( ctx ) __asm__ ( \
242                        "movq %%rsp,%0\n"   \
243                        "movq %%rbp,%1\n"   \
244                : "=rm" (ctx.SP), "=rm" (ctx.FP) )
245        #elif defined( __ARM_ARCH )
246        #define CtxGet( ctx ) __asm__ ( \
247                        "mov %0,%%sp\n"   \
248                        "mov %1,%%r11\n"   \
249                : "=rm" (ctx.SP), "=rm" (ctx.FP) )
250        #else
251                #error unknown hardware architecture
252        #endif
253
254#endif //_INVOKE_PRIVATE_H_
255#endif //! defined(__CFA_INVOKE_PRIVATE__)
256#ifdef __cforall
257}
258#endif
259
260// Local Variables: //
261// mode: c //
262// tab-width: 4 //
263// End: //
Note: See TracBrowser for help on using the repository browser.