source: libcfa/src/concurrency/kernel_private.hfa @ 31bb2e1

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

Moved implementations of cfa_[io call] to iocall.cfa

  • 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() {
[bdce852]94        #if defined(__SIZEOF_INT128__)
95                return __lehmer64( kernelTLS.rand_seed );
96        #else
[7812a7b5]97                return __xorshift64( kernelTLS.rand_seed );
[bdce852]98        #endif
[21184e3]99}
100
[de94a60]101
[a1a17a7]102void doregister( struct cluster & cltr );
103void unregister( struct cluster & cltr );
[de94a60]104
[ac2b598]105void doregister( struct cluster * cltr, struct $thread & thrd );
106void unregister( struct cluster * cltr, struct $thread & thrd );
[de94a60]107
[7768b8d]108//=======================================================================
109// Cluster lock API
110//=======================================================================
[b388ee81]111// Cells use by the reader writer lock
112// while not generic it only relies on a opaque pointer
[37ba662]113struct __attribute__((aligned(128))) __scheduler_lock_id_t {
[64a7146]114        // Spin lock used as the underlying lock
[7768b8d]115        volatile bool lock;
[64a7146]116
117        // Handle pointing to the proc owning this cell
118        // Used for allocating cells and debugging
119        __processor_id_t * volatile handle;
120
121        #ifdef __CFA_WITH_VERIFY__
122                // Debug, check if this is owned for reading
123                bool owned;
124        #endif
[7768b8d]125};
126
[64a7146]127static_assert( sizeof(struct __scheduler_lock_id_t) <= __alignof(struct __scheduler_lock_id_t));
128
[7768b8d]129// Lock-Free registering/unregistering of threads
130// Register a processor to a given cluster and get its unique id in return
[9b1dcc2]131unsigned doregister( struct __processor_id_t * proc );
[7768b8d]132
133// Unregister a processor from a given cluster using its id, getting back the original pointer
[9b1dcc2]134void     unregister( struct __processor_id_t * proc );
[7768b8d]135
136//=======================================================================
137// Reader-writer lock implementation
138// Concurrent with doregister/unregister,
139//    i.e., threads can be added at any point during or between the entry/exit
[dca5802]140
141//-----------------------------------------------------------------------
142// simple spinlock underlying the RWLock
143// Blocking acquire
[7768b8d]144static inline void __atomic_acquire(volatile bool * ll) {
145        while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) {
146                while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED))
147                        asm volatile("pause");
148        }
149        /* paranoid */ verify(*ll);
150}
151
[dca5802]152// Non-Blocking acquire
[7768b8d]153static inline bool __atomic_try_acquire(volatile bool * ll) {
[b798713]154        return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST);
[7768b8d]155}
156
[dca5802]157// Release
[7768b8d]158static inline void __atomic_unlock(volatile bool * ll) {
159        /* paranoid */ verify(*ll);
160        __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE);
161}
162
[b388ee81]163//-----------------------------------------------------------------------
164// Reader-Writer lock protecting the ready-queues
165// while this lock is mostly generic some aspects
166// have been hard-coded to for the ready-queue for
167// simplicity and performance
168struct __scheduler_RWLock_t {
169        // total cachelines allocated
170        unsigned int max;
171
172        // cachelines currently in use
173        volatile unsigned int alloc;
174
175        // cachelines ready to itereate over
176        // (!= to alloc when thread is in second half of doregister)
177        volatile unsigned int ready;
178
179        // writer lock
180        volatile bool lock;
181
182        // data pointer
[9b1dcc2]183        __scheduler_lock_id_t * data;
[b388ee81]184};
185
186void  ?{}(__scheduler_RWLock_t & this);
187void ^?{}(__scheduler_RWLock_t & this);
188
189extern __scheduler_RWLock_t * __scheduler_lock;
190
[7768b8d]191//-----------------------------------------------------------------------
192// Reader side : acquire when using the ready queue to schedule but not
193//  creating/destroying queues
[9b1dcc2]194static inline void ready_schedule_lock( struct __processor_id_t * proc) with(*__scheduler_lock) {
[7768b8d]195        unsigned iproc = proc->id;
196        /*paranoid*/ verify(data[iproc].handle == proc);
197        /*paranoid*/ verify(iproc < ready);
198
199        // Step 1 : make sure no writer are in the middle of the critical section
200        while(__atomic_load_n(&lock, (int)__ATOMIC_RELAXED))
201                asm volatile("pause");
202
203        // Fence needed because we don't want to start trying to acquire the lock
204        // before we read a false.
205        // Not needed on x86
206        // std::atomic_thread_fence(std::memory_order_seq_cst);
207
208        // Step 2 : acquire our local lock
209        __atomic_acquire( &data[iproc].lock );
210        /*paranoid*/ verify(data[iproc].lock);
[64a7146]211
212        #ifdef __CFA_WITH_VERIFY__
213                // Debug, check if this is owned for reading
214                data[iproc].owned = true;
215        #endif
[7768b8d]216}
217
[9b1dcc2]218static inline void ready_schedule_unlock( struct __processor_id_t * proc) with(*__scheduler_lock) {
[7768b8d]219        unsigned iproc = proc->id;
220        /*paranoid*/ verify(data[iproc].handle == proc);
221        /*paranoid*/ verify(iproc < ready);
222        /*paranoid*/ verify(data[iproc].lock);
[64a7146]223        /*paranoid*/ verify(data[iproc].owned);
224        #ifdef __CFA_WITH_VERIFY__
225                // Debug, check if this is owned for reading
226                data[iproc].owned = false;
227        #endif
[dca5802]228        __atomic_unlock(&data[iproc].lock);
[7768b8d]229}
230
[64a7146]231#ifdef __CFA_WITH_VERIFY__
232        static inline bool ready_schedule_islocked( struct __processor_id_t * proc) {
233                return __scheduler_lock->data[proc->id].owned;
234        }
235
236        static inline bool ready_mutate_islocked() {
237                return __scheduler_lock->lock;
238        }
239#endif
240
[7768b8d]241//-----------------------------------------------------------------------
242// Writer side : acquire when changing the ready queue, e.g. adding more
243//  queues or removing them.
[b388ee81]244uint_fast32_t ready_mutate_lock( void );
[7768b8d]245
[b388ee81]246void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ );
[7768b8d]247
[b798713]248//=======================================================================
249// Ready-Queue API
[64a7146]250//-----------------------------------------------------------------------
251// pop thread from the ready queue of a cluster
252// returns 0p if empty
253__attribute__((hot)) bool query(struct cluster * cltr);
254
[dca5802]255//-----------------------------------------------------------------------
256// push thread onto a ready queue for a cluster
257// returns true if the list was previously empty, false otherwise
[504a7dc]258__attribute__((hot)) bool push(struct cluster * cltr, struct $thread * thrd);
[dca5802]259
260//-----------------------------------------------------------------------
261// pop thread from the ready queue of a cluster
262// returns 0p if empty
[504a7dc]263__attribute__((hot)) struct $thread * pop(struct cluster * cltr);
[dca5802]264
[13c5e19]265//-----------------------------------------------------------------------
266// remove thread from the ready queue of a cluster
267// returns bool if it wasn't found
268bool remove_head(struct cluster * cltr, struct $thread * thrd);
269
[dca5802]270//-----------------------------------------------------------------------
271// Increase the width of the ready queue (number of lanes) by 4
[b798713]272void ready_queue_grow  (struct cluster * cltr);
[dca5802]273
274//-----------------------------------------------------------------------
275// Decrease the width of the ready queue (number of lanes) by 4
[b798713]276void ready_queue_shrink(struct cluster * cltr);
277
[31bb2e1]278//-----------------------------------------------------------------------
279// IO user data
280struct __io_user_data_t {
281        int32_t result;
282        $thread * thrd;
283};
284
[dca5802]285//-----------------------------------------------------------------------
286// Statics call at the end of each thread to register statistics
[b798713]287#if !defined(__CFA_NO_STATISTICS__)
[8834751]288static inline struct __stats_t * __tls_stats() {
289        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
290        /* paranoid */ verify( kernelTLS.this_stats );
291        return kernelTLS.this_stats;
292}
[b798713]293#endif
[de94a60]294
[75f3522]295// Local Variables: //
296// mode: c //
297// tab-width: 4 //
[4aa2fb2]298// End: //
Note: See TracBrowser for help on using the repository browser.