source: src/libcfa/concurrency/invoke.h @ 1dbc8590

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 1dbc8590 was afd550c, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Some more work on TLS macros

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