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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since f23b685 was f23b685, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

added some comments and moved a line to ease coroutine cancellation

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