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

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

Removed kernelTLS.this_coroutine which was redundant and some preleminary work for breaking into 2 step the context-switch

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