source: libcfa/src/concurrency/invoke.h@ 9b1dcc2

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9b1dcc2 was 1b143de, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Several fixes to relaxed_ready queue

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