source: libcfa/src/concurrency/kernel_private.hfa @ 00e9be9

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 00e9be9 was 254ad1b, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Separate schedule_thread from the scheduler lock

  • Property mode set to 100644
File size: 10.0 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
[fd9b524]12// Last Modified On : Wed Aug 12 08:21:33 2020
13// Update Count     : 9
[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
[75f3522]24//-----------------------------------------------------------------------------
25// Scheduler
[1c273d0]26
[37ba662]27struct __attribute__((aligned(128))) __scheduler_lock_id_t;
[9b1dcc2]28
[1c273d0]29extern "C" {
[2026bb6]30        void disable_interrupts() OPTIONAL_THREAD;
[a3821fa]31        void enable_interrupts( bool poll = true );
[1c273d0]32}
33
[254ad1b]34void schedule_thread$( $thread * ) __attribute__((nonnull (1)));
[75f3522]35
[8fc652e0]36extern bool __preemption_enabled();
37
[5afb49a]38//release/wake-up the following resources
39void __thread_finish( $thread * thrd );
[db6f06a]40
[75f3522]41//-----------------------------------------------------------------------------
42// Processor
43void main(processorCtx_t *);
[85b1deb]44
[8c50aed]45void * __create_pthread( pthread_t *, void * (*)(void *), void * );
[bfcf6b9]46void __destroy_pthread( pthread_t pthread, void * stack, void ** retval );
[1805b1b]47
[85b1deb]48
[75f3522]49
[6502a2b]50extern cluster * mainCluster;
51
[75f3522]52//-----------------------------------------------------------------------------
53// Threads
54extern "C" {
[c7a900a]55      void __cfactx_invoke_thread(void (*main)(void *), void * this);
[75f3522]56}
57
[f7d6bb0]58__cfaabi_dbg_debug_do(
[ac2b598]59        extern void __cfaabi_dbg_thread_register  ( $thread * thrd );
60        extern void __cfaabi_dbg_thread_unregister( $thread * thrd );
[f7d6bb0]61)
62
[6a77224]63#define TICKET_BLOCKED (-1) // thread is blocked
64#define TICKET_RUNNING ( 0) // thread is running
65#define TICKET_UNBLOCK ( 1) // thread should ignore next block
66
[969b3fe]67//-----------------------------------------------------------------------------
68// Utils
[ac2b598]69void doregister( struct cluster * cltr, struct $thread & thrd );
70void unregister( struct cluster * cltr, struct $thread & thrd );
[de94a60]71
[f00b26d4]72//-----------------------------------------------------------------------------
73// I/O
[78da4ab]74$io_arbiter * create(void);
75void destroy($io_arbiter *);
[f00b26d4]76
[7768b8d]77//=======================================================================
78// Cluster lock API
79//=======================================================================
80// Lock-Free registering/unregistering of threads
81// Register a processor to a given cluster and get its unique id in return
[a33c113]82void register_proc_id( struct __processor_id_t * );
[7768b8d]83
84// Unregister a processor from a given cluster using its id, getting back the original pointer
[a33c113]85void unregister_proc_id( struct __processor_id_t * proc );
[7768b8d]86
87//=======================================================================
88// Reader-writer lock implementation
89// Concurrent with doregister/unregister,
90//    i.e., threads can be added at any point during or between the entry/exit
[dca5802]91
92//-----------------------------------------------------------------------
93// simple spinlock underlying the RWLock
94// Blocking acquire
[7768b8d]95static inline void __atomic_acquire(volatile bool * ll) {
96        while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) {
97                while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED))
[fd9b524]98                        Pause();
[7768b8d]99        }
100        /* paranoid */ verify(*ll);
101}
102
[dca5802]103// Non-Blocking acquire
[7768b8d]104static inline bool __atomic_try_acquire(volatile bool * ll) {
[b798713]105        return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST);
[7768b8d]106}
107
[dca5802]108// Release
[7768b8d]109static inline void __atomic_unlock(volatile bool * ll) {
110        /* paranoid */ verify(*ll);
111        __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE);
112}
113
[a33c113]114// Cells use by the reader writer lock
115// while not generic it only relies on a opaque pointer
116struct __attribute__((aligned(128))) __scheduler_lock_id_t {
117        // Spin lock used as the underlying lock
118        volatile bool lock;
119
120        // Handle pointing to the proc owning this cell
121        // Used for allocating cells and debugging
122        __processor_id_t * volatile handle;
123
124        #ifdef __CFA_WITH_VERIFY__
125                // Debug, check if this is owned for reading
126                bool owned;
127        #endif
128};
129
130static_assert( sizeof(struct __scheduler_lock_id_t) <= __alignof(struct __scheduler_lock_id_t));
131
[b388ee81]132//-----------------------------------------------------------------------
133// Reader-Writer lock protecting the ready-queues
134// while this lock is mostly generic some aspects
135// have been hard-coded to for the ready-queue for
136// simplicity and performance
137struct __scheduler_RWLock_t {
138        // total cachelines allocated
139        unsigned int max;
140
141        // cachelines currently in use
142        volatile unsigned int alloc;
143
144        // cachelines ready to itereate over
145        // (!= to alloc when thread is in second half of doregister)
146        volatile unsigned int ready;
147
148        // writer lock
149        volatile bool lock;
150
151        // data pointer
[9b1dcc2]152        __scheduler_lock_id_t * data;
[b388ee81]153};
154
155void  ?{}(__scheduler_RWLock_t & this);
156void ^?{}(__scheduler_RWLock_t & this);
157
158extern __scheduler_RWLock_t * __scheduler_lock;
159
[7768b8d]160//-----------------------------------------------------------------------
161// Reader side : acquire when using the ready queue to schedule but not
162//  creating/destroying queues
[e873838]163static inline void ready_schedule_lock(void) with(*__scheduler_lock) {
[8fc652e0]164        /* paranoid */ verify( ! __preemption_enabled() );
165        /* paranoid */ verify( kernelTLS().this_proc_id );
[e873838]166
[8fc652e0]167        unsigned iproc = kernelTLS().this_proc_id->id;
168        /*paranoid*/ verify(data[iproc].handle == kernelTLS().this_proc_id);
[7768b8d]169        /*paranoid*/ verify(iproc < ready);
170
171        // Step 1 : make sure no writer are in the middle of the critical section
172        while(__atomic_load_n(&lock, (int)__ATOMIC_RELAXED))
[fd9b524]173                Pause();
[7768b8d]174
175        // Fence needed because we don't want to start trying to acquire the lock
176        // before we read a false.
177        // Not needed on x86
178        // std::atomic_thread_fence(std::memory_order_seq_cst);
179
180        // Step 2 : acquire our local lock
181        __atomic_acquire( &data[iproc].lock );
182        /*paranoid*/ verify(data[iproc].lock);
[64a7146]183
184        #ifdef __CFA_WITH_VERIFY__
185                // Debug, check if this is owned for reading
186                data[iproc].owned = true;
187        #endif
[7768b8d]188}
189
[e873838]190static inline void ready_schedule_unlock(void) with(*__scheduler_lock) {
[8fc652e0]191        /* paranoid */ verify( ! __preemption_enabled() );
192        /* paranoid */ verify( kernelTLS().this_proc_id );
[e873838]193
[8fc652e0]194        unsigned iproc = kernelTLS().this_proc_id->id;
195        /*paranoid*/ verify(data[iproc].handle == kernelTLS().this_proc_id);
[7768b8d]196        /*paranoid*/ verify(iproc < ready);
197        /*paranoid*/ verify(data[iproc].lock);
[64a7146]198        /*paranoid*/ verify(data[iproc].owned);
199        #ifdef __CFA_WITH_VERIFY__
200                // Debug, check if this is owned for reading
201                data[iproc].owned = false;
202        #endif
[dca5802]203        __atomic_unlock(&data[iproc].lock);
[7768b8d]204}
205
[64a7146]206#ifdef __CFA_WITH_VERIFY__
[e873838]207        static inline bool ready_schedule_islocked(void) {
[8fc652e0]208                /* paranoid */ verify( ! __preemption_enabled() );
209                /*paranoid*/ verify( kernelTLS().this_proc_id );
210                __processor_id_t * proc = kernelTLS().this_proc_id;
[64a7146]211                return __scheduler_lock->data[proc->id].owned;
212        }
213
214        static inline bool ready_mutate_islocked() {
215                return __scheduler_lock->lock;
216        }
217#endif
218
[7768b8d]219//-----------------------------------------------------------------------
220// Writer side : acquire when changing the ready queue, e.g. adding more
221//  queues or removing them.
[b388ee81]222uint_fast32_t ready_mutate_lock( void );
[7768b8d]223
[b388ee81]224void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ );
[7768b8d]225
[a33c113]226//-----------------------------------------------------------------------
227// Lock-Free registering/unregistering of threads
228// Register a processor to a given cluster and get its unique id in return
229// For convenience, also acquires the lock
230static inline uint_fast32_t ready_mutate_register( struct __processor_id_t * proc ) {
231        register_proc_id( proc );
232        return ready_mutate_lock();
233}
234
235// Unregister a processor from a given cluster using its id, getting back the original pointer
236// assumes the lock is acquired
237static inline void ready_mutate_unregister( struct __processor_id_t * proc, uint_fast32_t last_s ) {
238        ready_mutate_unlock( last_s );
239        unregister_proc_id( proc );
240}
241
[a7504db]242//-----------------------------------------------------------------------
243// Cluster idle lock/unlock
[6a9b12b]244static inline void lock(__cluster_proc_list & this) {
[a7504db]245        /* paranoid */ verify( ! __preemption_enabled() );
246
247        // Start by locking the global RWlock so that we know no-one is
248        // adding/removing processors while we mess with the idle lock
249        ready_schedule_lock();
250
251        // Simple counting lock, acquired, acquired by incrementing the counter
252        // to an odd number
253        for() {
254                uint64_t l = this.lock;
255                if(
256                        (0 == (l % 2))
257                        && __atomic_compare_exchange_n(&this.lock, &l, l + 1, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
258                ) return;
259                Pause();
260        }
261
262        /* paranoid */ verify( ! __preemption_enabled() );
263}
264
[6a9b12b]265static inline void unlock(__cluster_proc_list & this) {
[a7504db]266        /* paranoid */ verify( ! __preemption_enabled() );
267
268        /* paranoid */ verify( 1 == (this.lock % 2) );
269        // Simple couting lock, release by incrementing to an even number
270        __atomic_fetch_add( &this.lock, 1, __ATOMIC_SEQ_CST );
271
272        // Release the global lock, which we acquired when locking
273        ready_schedule_unlock();
274
275        /* paranoid */ verify( ! __preemption_enabled() );
276}
277
[b798713]278//=======================================================================
279// Ready-Queue API
[dca5802]280//-----------------------------------------------------------------------
281// push thread onto a ready queue for a cluster
282// returns true if the list was previously empty, false otherwise
[9cc3a18]283__attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd);
[dca5802]284
285//-----------------------------------------------------------------------
286// pop thread from the ready queue of a cluster
287// returns 0p if empty
[1eb239e4]288// May return 0p spuriously
[431cd4f]289__attribute__((hot)) struct $thread * pop_fast(struct cluster * cltr);
[dca5802]290
[1eb239e4]291//-----------------------------------------------------------------------
292// pop thread from the ready queue of a cluster
293// returns 0p if empty
294// guaranteed to find any threads added before this call
295__attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr);
296
[dca5802]297//-----------------------------------------------------------------------
298// Increase the width of the ready queue (number of lanes) by 4
[a017ee7]299void ready_queue_grow  (struct cluster * cltr);
[dca5802]300
301//-----------------------------------------------------------------------
302// Decrease the width of the ready queue (number of lanes) by 4
[a017ee7]303void ready_queue_shrink(struct cluster * cltr);
[b798713]304
[de94a60]305
[75f3522]306// Local Variables: //
307// mode: c //
308// tab-width: 4 //
[4aa2fb2]309// End: //
Note: See TracBrowser for help on using the repository browser.