source: libcfa/src/concurrency/kernel_private.hfa@ bdce852

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 bdce852 was bdce852, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Switched RNG from lehmer64 to wyhash64 for 32-bit builts

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[75f3522]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//
[73abe95]7// kernel_private.hfa --
[75f3522]8//
9// Author : Thierry Delisle
10// Created On : Mon Feb 13 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[1805b1b]12// Last Modified On : Sat Nov 30 19:25:02 2019
13// Update Count : 8
[75f3522]14//
15
[6b0b624]16#pragma once
[75f3522]17
[58b6d1b]18#include "kernel.hfa"
19#include "thread.hfa"
[75f3522]20
[73abe95]21#include "alarm.hfa"
[8834751]22#include "stats.hfa"
[fa21ac9]23
[13c5e19]24#include "bits/random.hfa"
25
[4aa2fb2]26
[75f3522]27//-----------------------------------------------------------------------------
28// Scheduler
[1c273d0]29
[37ba662]30struct __attribute__((aligned(128))) __scheduler_lock_id_t;
[9b1dcc2]31
[1c273d0]32extern "C" {
[2026bb6]33 void disable_interrupts() OPTIONAL_THREAD;
[969b3fe]34 void enable_interrupts_noPoll();
[36982fc]35 void enable_interrupts( __cfaabi_dbg_ctx_param );
[1c273d0]36}
37
[9b1dcc2]38void __schedule_thread( struct __processor_id_t *, $thread * ) __attribute__((nonnull (2)));
[75f3522]39
[e60e0dc]40//Block current thread and release/wake-up the following resources
[b0c7419]41void __leave_thread() __attribute__((noreturn));
[db6f06a]42
[75f3522]43//-----------------------------------------------------------------------------
44// Processor
45void main(processorCtx_t *);
[85b1deb]46
[8c50aed]47void * __create_pthread( pthread_t *, void * (*)(void *), void * );
[1805b1b]48
[85b1deb]49
[75f3522]50
[e60e0dc]51struct event_kernel_t {
[fa21ac9]52 alarm_list_t alarms;
[ea7d2b0]53 __spinlock_t lock;
[fa21ac9]54};
55
[e60e0dc]56extern event_kernel_t * event_kernel;
57
[d8548e2]58struct __cfa_kernel_preemption_state_t {
[b69ea6b]59 bool enabled;
60 bool in_progress;
61 unsigned short disable_count;
62};
63
[afc2427]64extern volatile thread_local __cfa_kernel_preemption_state_t preemption_state __attribute__ ((tls_model ( "initial-exec" )));
[c81ebf9]65
[6502a2b]66extern cluster * mainCluster;
67
[75f3522]68//-----------------------------------------------------------------------------
69// Threads
70extern "C" {
[c7a900a]71 void __cfactx_invoke_thread(void (*main)(void *), void * this);
[75f3522]72}
73
[f7d6bb0]74__cfaabi_dbg_debug_do(
[ac2b598]75 extern void __cfaabi_dbg_thread_register ( $thread * thrd );
76 extern void __cfaabi_dbg_thread_unregister( $thread * thrd );
[f7d6bb0]77)
78
[2d8f7b0]79// KERNEL ONLY unpark with out disabling interrupts
[9b1dcc2]80void __unpark( struct __processor_id_t *, $thread * thrd __cfaabi_dbg_ctx_param2 );
[2d8f7b0]81
[92976d9]82//-----------------------------------------------------------------------------
83// I/O
[dd4e2d7]84void __kernel_io_startup ( cluster &, unsigned, bool );
[f6660520]85void __kernel_io_finish_start( cluster & );
86void __kernel_io_prepare_stop( cluster & );
87void __kernel_io_shutdown ( cluster &, bool );
[92976d9]88
[969b3fe]89//-----------------------------------------------------------------------------
90// Utils
[7768b8d]91#define KERNEL_STORAGE(T,X) __attribute((aligned(__alignof__(T)))) static char storage_##X[sizeof(T)]
[969b3fe]92
[13c5e19]93static inline uint64_t __tls_rand() {
94 // kernelTLS.rand_seed ^= kernelTLS.rand_seed << 6;
95 // kernelTLS.rand_seed ^= kernelTLS.rand_seed >> 21;
96 // kernelTLS.rand_seed ^= kernelTLS.rand_seed << 7;
97 // return kernelTLS.rand_seed;
[bdce852]98 #if defined(__SIZEOF_INT128__)
99 return __lehmer64( kernelTLS.rand_seed );
100 #else
101 return __wyhash64( kernelTLS.rand_seed );
102 #endif
[21184e3]103}
104
[de94a60]105
[a1a17a74]106void doregister( struct cluster & cltr );
107void unregister( struct cluster & cltr );
[de94a60]108
[ac2b598]109void doregister( struct cluster * cltr, struct $thread & thrd );
110void unregister( struct cluster * cltr, struct $thread & thrd );
[de94a60]111
[7768b8d]112//=======================================================================
113// Cluster lock API
114//=======================================================================
[b388ee81]115// Cells use by the reader writer lock
116// while not generic it only relies on a opaque pointer
[37ba662]117struct __attribute__((aligned(128))) __scheduler_lock_id_t {
[64a7146]118 // Spin lock used as the underlying lock
[7768b8d]119 volatile bool lock;
[64a7146]120
121 // Handle pointing to the proc owning this cell
122 // Used for allocating cells and debugging
123 __processor_id_t * volatile handle;
124
125 #ifdef __CFA_WITH_VERIFY__
126 // Debug, check if this is owned for reading
127 bool owned;
128 #endif
[7768b8d]129};
130
[64a7146]131static_assert( sizeof(struct __scheduler_lock_id_t) <= __alignof(struct __scheduler_lock_id_t));
132
[7768b8d]133// Lock-Free registering/unregistering of threads
134// Register a processor to a given cluster and get its unique id in return
[9b1dcc2]135unsigned doregister( struct __processor_id_t * proc );
[7768b8d]136
137// Unregister a processor from a given cluster using its id, getting back the original pointer
[9b1dcc2]138void unregister( struct __processor_id_t * proc );
[7768b8d]139
140//=======================================================================
141// Reader-writer lock implementation
142// Concurrent with doregister/unregister,
143// i.e., threads can be added at any point during or between the entry/exit
[dca5802]144
145//-----------------------------------------------------------------------
146// simple spinlock underlying the RWLock
147// Blocking acquire
[7768b8d]148static inline void __atomic_acquire(volatile bool * ll) {
149 while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) {
150 while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED))
151 asm volatile("pause");
152 }
153 /* paranoid */ verify(*ll);
154}
155
[dca5802]156// Non-Blocking acquire
[7768b8d]157static inline bool __atomic_try_acquire(volatile bool * ll) {
[b798713]158 return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST);
[7768b8d]159}
160
[dca5802]161// Release
[7768b8d]162static inline void __atomic_unlock(volatile bool * ll) {
163 /* paranoid */ verify(*ll);
164 __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE);
165}
166
[b388ee81]167//-----------------------------------------------------------------------
168// Reader-Writer lock protecting the ready-queues
169// while this lock is mostly generic some aspects
170// have been hard-coded to for the ready-queue for
171// simplicity and performance
172struct __scheduler_RWLock_t {
173 // total cachelines allocated
174 unsigned int max;
175
176 // cachelines currently in use
177 volatile unsigned int alloc;
178
179 // cachelines ready to itereate over
180 // (!= to alloc when thread is in second half of doregister)
181 volatile unsigned int ready;
182
183 // writer lock
184 volatile bool lock;
185
186 // data pointer
[9b1dcc2]187 __scheduler_lock_id_t * data;
[b388ee81]188};
189
190void ?{}(__scheduler_RWLock_t & this);
191void ^?{}(__scheduler_RWLock_t & this);
192
193extern __scheduler_RWLock_t * __scheduler_lock;
194
[7768b8d]195//-----------------------------------------------------------------------
196// Reader side : acquire when using the ready queue to schedule but not
197// creating/destroying queues
[9b1dcc2]198static inline void ready_schedule_lock( struct __processor_id_t * proc) with(*__scheduler_lock) {
[7768b8d]199 unsigned iproc = proc->id;
200 /*paranoid*/ verify(data[iproc].handle == proc);
201 /*paranoid*/ verify(iproc < ready);
202
203 // Step 1 : make sure no writer are in the middle of the critical section
204 while(__atomic_load_n(&lock, (int)__ATOMIC_RELAXED))
205 asm volatile("pause");
206
207 // Fence needed because we don't want to start trying to acquire the lock
208 // before we read a false.
209 // Not needed on x86
210 // std::atomic_thread_fence(std::memory_order_seq_cst);
211
212 // Step 2 : acquire our local lock
213 __atomic_acquire( &data[iproc].lock );
214 /*paranoid*/ verify(data[iproc].lock);
[64a7146]215
216 #ifdef __CFA_WITH_VERIFY__
217 // Debug, check if this is owned for reading
218 data[iproc].owned = true;
219 #endif
[7768b8d]220}
221
[9b1dcc2]222static inline void ready_schedule_unlock( struct __processor_id_t * proc) with(*__scheduler_lock) {
[7768b8d]223 unsigned iproc = proc->id;
224 /*paranoid*/ verify(data[iproc].handle == proc);
225 /*paranoid*/ verify(iproc < ready);
226 /*paranoid*/ verify(data[iproc].lock);
[64a7146]227 /*paranoid*/ verify(data[iproc].owned);
228 #ifdef __CFA_WITH_VERIFY__
229 // Debug, check if this is owned for reading
230 data[iproc].owned = false;
231 #endif
[dca5802]232 __atomic_unlock(&data[iproc].lock);
[7768b8d]233}
234
[64a7146]235#ifdef __CFA_WITH_VERIFY__
236 static inline bool ready_schedule_islocked( struct __processor_id_t * proc) {
237 return __scheduler_lock->data[proc->id].owned;
238 }
239
240 static inline bool ready_mutate_islocked() {
241 return __scheduler_lock->lock;
242 }
243#endif
244
[7768b8d]245//-----------------------------------------------------------------------
246// Writer side : acquire when changing the ready queue, e.g. adding more
247// queues or removing them.
[b388ee81]248uint_fast32_t ready_mutate_lock( void );
[7768b8d]249
[b388ee81]250void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ );
[7768b8d]251
[b798713]252//=======================================================================
253// Ready-Queue API
[64a7146]254//-----------------------------------------------------------------------
255// pop thread from the ready queue of a cluster
256// returns 0p if empty
257__attribute__((hot)) bool query(struct cluster * cltr);
258
[dca5802]259//-----------------------------------------------------------------------
260// push thread onto a ready queue for a cluster
261// returns true if the list was previously empty, false otherwise
[504a7dc]262__attribute__((hot)) bool push(struct cluster * cltr, struct $thread * thrd);
[dca5802]263
264//-----------------------------------------------------------------------
265// pop thread from the ready queue of a cluster
266// returns 0p if empty
[504a7dc]267__attribute__((hot)) struct $thread * pop(struct cluster * cltr);
[dca5802]268
[13c5e19]269//-----------------------------------------------------------------------
270// remove thread from the ready queue of a cluster
271// returns bool if it wasn't found
272bool remove_head(struct cluster * cltr, struct $thread * thrd);
273
[dca5802]274//-----------------------------------------------------------------------
275// Increase the width of the ready queue (number of lanes) by 4
[b798713]276void ready_queue_grow (struct cluster * cltr);
[dca5802]277
278//-----------------------------------------------------------------------
279// Decrease the width of the ready queue (number of lanes) by 4
[b798713]280void ready_queue_shrink(struct cluster * cltr);
281
[dca5802]282//-----------------------------------------------------------------------
283// Statics call at the end of each thread to register statistics
[b798713]284#if !defined(__CFA_NO_STATISTICS__)
[8834751]285static inline struct __stats_t * __tls_stats() {
286 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
287 /* paranoid */ verify( kernelTLS.this_stats );
288 return kernelTLS.this_stats;
289}
[b798713]290#endif
[de94a60]291
[75f3522]292// Local Variables: //
293// mode: c //
294// tab-width: 4 //
[4aa2fb2]295// End: //
Note: See TracBrowser for help on using the repository browser.