source: libcfa/src/concurrency/kernel_private.hfa @ 50d529e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 50d529e was efc171d1, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Fixed several concurrency warnings

  • Property mode set to 100644
File size: 9.7 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
[efc171d1]38void __schedule_thread( struct __processor_id_t *, $thread * )
39#if defined(NDEBUG) || (!defined(__CFA_DEBUG__) && !defined(__CFA_VERIFY__))
40        __attribute__((nonnull (2)))
41#endif
42;
[75f3522]43
[e60e0dc]44//Block current thread and release/wake-up the following resources
[b0c7419]45void __leave_thread() __attribute__((noreturn));
[db6f06a]46
[75f3522]47//-----------------------------------------------------------------------------
48// Processor
49void main(processorCtx_t *);
[85b1deb]50
[8c50aed]51void * __create_pthread( pthread_t *, void * (*)(void *), void * );
[1805b1b]52
[85b1deb]53
[75f3522]54
[e60e0dc]55struct event_kernel_t {
[fa21ac9]56        alarm_list_t alarms;
[ea7d2b0]57        __spinlock_t lock;
[fa21ac9]58};
59
[e60e0dc]60extern event_kernel_t * event_kernel;
61
[d8548e2]62struct __cfa_kernel_preemption_state_t {
[b69ea6b]63        bool enabled;
64        bool in_progress;
65        unsigned short disable_count;
66};
67
[afc2427]68extern volatile thread_local __cfa_kernel_preemption_state_t preemption_state __attribute__ ((tls_model ( "initial-exec" )));
[c81ebf9]69
[6502a2b]70extern cluster * mainCluster;
71
[75f3522]72//-----------------------------------------------------------------------------
73// Threads
74extern "C" {
[c7a900a]75      void __cfactx_invoke_thread(void (*main)(void *), void * this);
[75f3522]76}
77
[f7d6bb0]78__cfaabi_dbg_debug_do(
[ac2b598]79        extern void __cfaabi_dbg_thread_register  ( $thread * thrd );
80        extern void __cfaabi_dbg_thread_unregister( $thread * thrd );
[f7d6bb0]81)
82
[2d8f7b0]83// KERNEL ONLY unpark with out disabling interrupts
[9b1dcc2]84void __unpark( struct __processor_id_t *, $thread * thrd __cfaabi_dbg_ctx_param2 );
[2d8f7b0]85
[92976d9]86//-----------------------------------------------------------------------------
87// I/O
[dd4e2d7]88void __kernel_io_startup     ( cluster &, unsigned, bool );
[f6660520]89void __kernel_io_finish_start( cluster & );
90void __kernel_io_prepare_stop( cluster & );
91void __kernel_io_shutdown    ( cluster &, bool );
[92976d9]92
[969b3fe]93//-----------------------------------------------------------------------------
94// Utils
[7768b8d]95#define KERNEL_STORAGE(T,X) __attribute((aligned(__alignof__(T)))) static char storage_##X[sizeof(T)]
[969b3fe]96
[13c5e19]97static inline uint64_t __tls_rand() {
[bdce852]98        #if defined(__SIZEOF_INT128__)
99                return __lehmer64( kernelTLS.rand_seed );
100        #else
[7812a7b5]101                return __xorshift64( kernelTLS.rand_seed );
[bdce852]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
[31bb2e1]282//-----------------------------------------------------------------------
283// IO user data
284struct __io_user_data_t {
285        int32_t result;
286        $thread * thrd;
287};
288
[dca5802]289//-----------------------------------------------------------------------
290// Statics call at the end of each thread to register statistics
[b798713]291#if !defined(__CFA_NO_STATISTICS__)
[47746a2]292        static inline struct __stats_t * __tls_stats() {
293                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
294                /* paranoid */ verify( kernelTLS.this_stats );
295                return kernelTLS.this_stats;
296        }
297
298        #define __STATS__(in_kernel, ...) { \
299                if( !(in_kernel) ) disable_interrupts(); \
300                with( *__tls_stats() ) { \
301                        __VA_ARGS__ \
302                } \
303                if( !(in_kernel) ) enable_interrupts( __cfaabi_dbg_ctx ); \
304        }
305#else
306        #define __STATS__(in_kernel, ...)
[b798713]307#endif
[de94a60]308
[75f3522]309// Local Variables: //
310// mode: c //
311// tab-width: 4 //
[4aa2fb2]312// End: //
Note: See TracBrowser for help on using the repository browser.