source: libcfa/src/concurrency/kernel/private.hfa @ 6a4ef0c

ADTast-experimental
Last change on this file since 6a4ef0c was 28372f7, checked in by Thierry Delisle <tdelisle@…>, 21 months ago

Changed to park/unpark permit/ticketing to make sure no one unparks a dead thread

  • Property mode set to 100644
File size: 14.2 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//
[708ae38]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
[3489ea6]18#if !defined(__cforall_thread__)
[708ae38]19        #error kernel/private.hfa should only be included in libcfathread source
[3489ea6]20#endif
21
[9cd5bd2]22#include <signal.h>
23
[58b6d1b]24#include "kernel.hfa"
25#include "thread.hfa"
[75f3522]26
[73abe95]27#include "alarm.hfa"
[8834751]28#include "stats.hfa"
[fa21ac9]29
[3489ea6]30extern "C" {
31#if   defined(CFA_HAVE_LINUX_LIBRSEQ)
32        #include <rseq/rseq.h>
33#elif defined(CFA_HAVE_LINUX_RSEQ_H)
[f558b5f]34        #include <linux/rseq.h>
[3489ea6]35#else
36        #ifndef _GNU_SOURCE
[708ae38]37        #error kernel/private requires gnu_source
[3489ea6]38        #endif
39        #include <sched.h>
40#endif
41}
42
[d3605f8]43// Defines whether or not we *want* to use io_uring_enter as the idle_sleep blocking call
[f870e257]44// #define CFA_WANT_IO_URING_IDLE
[d3605f8]45
46// Defines whether or not we *can* use io_uring_enter as the idle_sleep blocking call
47#if defined(CFA_WANT_IO_URING_IDLE) && defined(CFA_HAVE_LINUX_IO_URING_H)
48        #if defined(CFA_HAVE_IORING_OP_READ) || (defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV))
49                #define CFA_WITH_IO_URING_IDLE
50        #endif
51#endif
[31c967b]52// #define READYQ_USE_LINEAR_AVG
53#define READYQ_USE_LOGDBL_AVG
54// #define READYQ_USE_LOGINT_AVG
55
56#if   defined(READYQ_USE_LINEAR_AVG)
57typedef unsigned long long __readyQ_avg_t;
58#elif defined(READYQ_USE_LOGDBL_AVG)
59typedef double __readyQ_avg_t;
60#elif defined(READYQ_USE_LOGDBL_AVG)
61typedef unsigned long long __readyQ_avg_t;
62#else
63#error must pick a scheme for averaging
64#endif
[059ad16]65
[95dab9e]66extern "C" {
67        __attribute__((visibility("protected"))) int __cfaabi_pthread_create(pthread_t *_thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
68        __attribute__((visibility("protected"))) int __cfaabi_pthread_join(pthread_t _thread, void **retval);
69        __attribute__((visibility("protected"))) pthread_t __cfaabi_pthread_self(void);
70        __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_init(pthread_attr_t *attr);
71        __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_destroy(pthread_attr_t *attr);
72        __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_setstack( pthread_attr_t *attr, void *stackaddr, size_t stacksize );
73        __attribute__((visibility("protected"))) int __cfaabi_pthread_attr_getstacksize( const pthread_attr_t *attr, size_t *stacksize );
74        __attribute__((visibility("protected"))) int __cfaabi_pthread_sigqueue(pthread_t _thread, int sig, const union sigval value);
75        __attribute__((visibility("protected"))) int __cfaabi_pthread_sigmask( int how, const sigset_t *set, sigset_t *oset);
76}
77
[75f3522]78//-----------------------------------------------------------------------------
79// Scheduler
[31c967b]80union __attribute__((aligned(64))) __timestamp_t {
81        struct {
82                volatile unsigned long long tv;
83                volatile __readyQ_avg_t ma;
84        } t;
85        char __padding[192];
86};
87
[1c273d0]88extern "C" {
[2026bb6]89        void disable_interrupts() OPTIONAL_THREAD;
[a3821fa]90        void enable_interrupts( bool poll = true );
[1c273d0]91}
92
[24e321c]93void schedule_thread$( thread$ *, unpark_hint hint ) __attribute__((nonnull (1)));
[75f3522]94
[8fc652e0]95extern bool __preemption_enabled();
96
[13fdf86]97enum {
98        PREEMPT_NORMAL    = 0,
99        PREEMPT_TERMINATE = 1,
100        PREEMPT_IO = 2,
101};
102
[7d0ebd0]103static inline void __disable_interrupts_checked() {
104        /* paranoid */ verify( __preemption_enabled() );
105        disable_interrupts();
106        /* paranoid */ verify( ! __preemption_enabled() );
107}
108
109static inline void __enable_interrupts_checked( bool poll = true ) {
110        /* paranoid */ verify( ! __preemption_enabled() );
111        enable_interrupts( poll );
112        /* paranoid */ verify( __preemption_enabled() );
113}
114
[5afb49a]115//release/wake-up the following resources
[e84ab3d]116void __thread_finish( thread$ * thrd );
[db6f06a]117
[3489ea6]118//-----------------------------------------------------------------------------
119// Hardware
120
121#if   defined(CFA_HAVE_LINUX_LIBRSEQ)
122        // No data needed
123#elif defined(CFA_HAVE_LINUX_RSEQ_H)
124        extern "Cforall" {
[1bcbf02]125                extern __attribute__((aligned(64))) __thread volatile struct rseq __cfaabi_rseq;
[3489ea6]126        }
127#else
128        // No data needed
129#endif
130
131static inline int __kernel_getcpu() {
132        /* paranoid */ verify( ! __preemption_enabled() );
133#if   defined(CFA_HAVE_LINUX_LIBRSEQ)
[f558b5f]134        return rseq_current_cpu();
[3489ea6]135#elif defined(CFA_HAVE_LINUX_RSEQ_H)
[f558b5f]136        int r = __cfaabi_rseq.cpu_id;
137        /* paranoid */ verify( r >= 0 );
138        return r;
[3489ea6]139#else
140        return sched_getcpu();
141#endif
142}
143
[75f3522]144//-----------------------------------------------------------------------------
145// Processor
[c18bf9e]146void main(processorCtx_t &);
147static inline coroutine$* get_coroutine(processorCtx_t & this) { return &this.self; }
[85b1deb]148
[8c50aed]149void * __create_pthread( pthread_t *, void * (*)(void *), void * );
[bfcf6b9]150void __destroy_pthread( pthread_t pthread, void * stack, void ** retval );
[1805b1b]151
[6502a2b]152extern cluster * mainCluster;
153
[75f3522]154//-----------------------------------------------------------------------------
155// Threads
156extern "C" {
[c7a900a]157      void __cfactx_invoke_thread(void (*main)(void *), void * this);
[75f3522]158}
159
[f7d6bb0]160__cfaabi_dbg_debug_do(
[e84ab3d]161        extern void __cfaabi_dbg_thread_register  ( thread$ * thrd );
162        extern void __cfaabi_dbg_thread_unregister( thread$ * thrd );
[f7d6bb0]163)
164
[6a77224]165#define TICKET_BLOCKED (-1) // thread is blocked
166#define TICKET_RUNNING ( 0) // thread is running
167#define TICKET_UNBLOCK ( 1) // thread should ignore next block
[28372f7]168#define TICKET_DEAD    (0xDEAD) // thread should never be unparked
[6a77224]169
[969b3fe]170//-----------------------------------------------------------------------------
171// Utils
[e84ab3d]172void doregister( struct cluster * cltr, struct thread$ & thrd );
173void unregister( struct cluster * cltr, struct thread$ & thrd );
[de94a60]174
[f00b26d4]175//-----------------------------------------------------------------------------
176// I/O
[8bee858]177io_arbiter$ * create(void);
178void destroy(io_arbiter$ *);
[f00b26d4]179
[7768b8d]180//=======================================================================
181// Cluster lock API
182//=======================================================================
183// Lock-Free registering/unregistering of threads
184// Register a processor to a given cluster and get its unique id in return
[c993b15]185unsigned register_proc_id( void );
[7768b8d]186
187// Unregister a processor from a given cluster using its id, getting back the original pointer
[c993b15]188void unregister_proc_id( unsigned );
[7768b8d]189
190//=======================================================================
191// Reader-writer lock implementation
192// Concurrent with doregister/unregister,
193//    i.e., threads can be added at any point during or between the entry/exit
[dca5802]194
195//-----------------------------------------------------------------------
196// simple spinlock underlying the RWLock
197// Blocking acquire
[7768b8d]198static inline void __atomic_acquire(volatile bool * ll) {
[2284d20]199        /* paranoid */ verify( ! __preemption_enabled() );
200        /* paranoid */ verify(ll);
201
[7768b8d]202        while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) {
203                while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED))
[fd9b524]204                        Pause();
[7768b8d]205        }
206        /* paranoid */ verify(*ll);
[2284d20]207        /* paranoid */ verify( ! __preemption_enabled() );
[7768b8d]208}
209
[dca5802]210// Non-Blocking acquire
[7768b8d]211static inline bool __atomic_try_acquire(volatile bool * ll) {
[2284d20]212        /* paranoid */ verify( ! __preemption_enabled() );
213        /* paranoid */ verify(ll);
214
[b798713]215        return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST);
[7768b8d]216}
217
[dca5802]218// Release
[7768b8d]219static inline void __atomic_unlock(volatile bool * ll) {
[2284d20]220        /* paranoid */ verify( ! __preemption_enabled() );
221        /* paranoid */ verify(ll);
[7768b8d]222        /* paranoid */ verify(*ll);
223        __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE);
224}
225
[b388ee81]226//-----------------------------------------------------------------------
227// Reader-Writer lock protecting the ready-queues
228// while this lock is mostly generic some aspects
229// have been hard-coded to for the ready-queue for
230// simplicity and performance
[741e22c]231union __attribute__((aligned(64))) __scheduler_RWLock_t {
232        struct {
[cd3fc46]233                __attribute__((aligned(64))) char padding;
234
[741e22c]235                // total cachelines allocated
[cd3fc46]236                __attribute__((aligned(64))) unsigned int max;
[b388ee81]237
[741e22c]238                // cachelines currently in use
239                volatile unsigned int alloc;
[b388ee81]240
[741e22c]241                // cachelines ready to itereate over
242                // (!= to alloc when thread is in second half of doregister)
243                volatile unsigned int ready;
[b388ee81]244
[741e22c]245                // writer lock
246                volatile bool write_lock;
[b388ee81]247
[741e22c]248                // data pointer
249                volatile bool * volatile * data;
250        } lock;
251        char pad[192];
[b388ee81]252};
253
254void  ?{}(__scheduler_RWLock_t & this);
255void ^?{}(__scheduler_RWLock_t & this);
256
[cd3fc46]257extern __scheduler_RWLock_t __scheduler_lock;
[b388ee81]258
[7768b8d]259//-----------------------------------------------------------------------
260// Reader side : acquire when using the ready queue to schedule but not
261//  creating/destroying queues
[cd3fc46]262static inline void ready_schedule_lock(void) with(__scheduler_lock.lock) {
[8fc652e0]263        /* paranoid */ verify( ! __preemption_enabled() );
[c993b15]264        /* paranoid */ verify( ! kernelTLS().in_sched_lock );
265        /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
266        /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
[7768b8d]267
268        // Step 1 : make sure no writer are in the middle of the critical section
[c993b15]269        while(__atomic_load_n(&write_lock, (int)__ATOMIC_RELAXED))
[fd9b524]270                Pause();
[7768b8d]271
272        // Fence needed because we don't want to start trying to acquire the lock
273        // before we read a false.
274        // Not needed on x86
275        // std::atomic_thread_fence(std::memory_order_seq_cst);
276
277        // Step 2 : acquire our local lock
[c993b15]278        __atomic_acquire( &kernelTLS().sched_lock );
279        /*paranoid*/ verify(kernelTLS().sched_lock);
[64a7146]280
281        #ifdef __CFA_WITH_VERIFY__
282                // Debug, check if this is owned for reading
[c993b15]283                kernelTLS().in_sched_lock = true;
[64a7146]284        #endif
[7768b8d]285}
286
[cd3fc46]287static inline void ready_schedule_unlock(void) with(__scheduler_lock.lock) {
[8fc652e0]288        /* paranoid */ verify( ! __preemption_enabled() );
[c993b15]289        /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
290        /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
291        /* paranoid */ verify( kernelTLS().sched_lock );
292        /* paranoid */ verify( kernelTLS().in_sched_lock );
[64a7146]293        #ifdef __CFA_WITH_VERIFY__
294                // Debug, check if this is owned for reading
[c993b15]295                kernelTLS().in_sched_lock = false;
[64a7146]296        #endif
[c993b15]297        __atomic_unlock(&kernelTLS().sched_lock);
[7768b8d]298}
299
[64a7146]300#ifdef __CFA_WITH_VERIFY__
[e873838]301        static inline bool ready_schedule_islocked(void) {
[8fc652e0]302                /* paranoid */ verify( ! __preemption_enabled() );
[c993b15]303                /* paranoid */ verify( (!kernelTLS().in_sched_lock) || kernelTLS().sched_lock );
304                return kernelTLS().sched_lock;
[64a7146]305        }
306
307        static inline bool ready_mutate_islocked() {
[cd3fc46]308                return __scheduler_lock.lock.write_lock;
[64a7146]309        }
310#endif
311
[7768b8d]312//-----------------------------------------------------------------------
313// Writer side : acquire when changing the ready queue, e.g. adding more
314//  queues or removing them.
[b388ee81]315uint_fast32_t ready_mutate_lock( void );
[7768b8d]316
[b388ee81]317void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ );
[7768b8d]318
[a33c113]319//-----------------------------------------------------------------------
320// Lock-Free registering/unregistering of threads
321// Register a processor to a given cluster and get its unique id in return
322// For convenience, also acquires the lock
[c993b15]323static inline [unsigned, uint_fast32_t] ready_mutate_register() {
324        unsigned id = register_proc_id();
325        uint_fast32_t last = ready_mutate_lock();
326        return [id, last];
[a33c113]327}
328
329// Unregister a processor from a given cluster using its id, getting back the original pointer
330// assumes the lock is acquired
[c993b15]331static inline void ready_mutate_unregister( unsigned id, uint_fast32_t last_s ) {
[a33c113]332        ready_mutate_unlock( last_s );
[c993b15]333        unregister_proc_id( id );
[a33c113]334}
335
[a7504db]336//-----------------------------------------------------------------------
337// Cluster idle lock/unlock
[6a9b12b]338static inline void lock(__cluster_proc_list & this) {
[a7504db]339        /* paranoid */ verify( ! __preemption_enabled() );
340
341        // Start by locking the global RWlock so that we know no-one is
342        // adding/removing processors while we mess with the idle lock
343        ready_schedule_lock();
344
[a633f6f]345        lock( this.lock __cfaabi_dbg_ctx2 );
[a7504db]346
347        /* paranoid */ verify( ! __preemption_enabled() );
348}
349
[5f5a729]350static inline bool try_lock(__cluster_proc_list & this) {
351        /* paranoid */ verify( ! __preemption_enabled() );
352
353        // Start by locking the global RWlock so that we know no-one is
354        // adding/removing processors while we mess with the idle lock
355        ready_schedule_lock();
356
[a633f6f]357        if(try_lock( this.lock __cfaabi_dbg_ctx2 )) {
[5f5a729]358                // success
359                /* paranoid */ verify( ! __preemption_enabled() );
360                return true;
361        }
362
363        // failed to lock
364        ready_schedule_unlock();
365
366        /* paranoid */ verify( ! __preemption_enabled() );
367        return false;
368}
369
[6a9b12b]370static inline void unlock(__cluster_proc_list & this) {
[a7504db]371        /* paranoid */ verify( ! __preemption_enabled() );
372
[a633f6f]373        unlock(this.lock);
[a7504db]374
375        // Release the global lock, which we acquired when locking
376        ready_schedule_unlock();
377
378        /* paranoid */ verify( ! __preemption_enabled() );
379}
380
[b798713]381//=======================================================================
382// Ready-Queue API
[dca5802]383//-----------------------------------------------------------------------
384// push thread onto a ready queue for a cluster
385// returns true if the list was previously empty, false otherwise
[24e321c]386__attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint);
[dca5802]387
388//-----------------------------------------------------------------------
[fc59df78]389// pop thread from the local queues of a cluster
[dca5802]390// returns 0p if empty
[1eb239e4]391// May return 0p spuriously
[e84ab3d]392__attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr);
[dca5802]393
[1eb239e4]394//-----------------------------------------------------------------------
[fc59df78]395// pop thread from any ready queue of a cluster
[1eb239e4]396// returns 0p if empty
[fc59df78]397// May return 0p spuriously
[e84ab3d]398__attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr);
[1eb239e4]399
[fc59df78]400//-----------------------------------------------------------------------
401// search all ready queues of a cluster for any thread
402// returns 0p if empty
403// guaranteed to find any threads added before this call
[e84ab3d]404__attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr);
[fc59df78]405
[24e321c]406//-----------------------------------------------------------------------
407// get preferred ready for new thread
408unsigned ready_queue_new_preferred();
409
[dca5802]410//-----------------------------------------------------------------------
411// Increase the width of the ready queue (number of lanes) by 4
[a017ee7]412void ready_queue_grow  (struct cluster * cltr);
[dca5802]413
414//-----------------------------------------------------------------------
415// Decrease the width of the ready queue (number of lanes) by 4
[a017ee7]416void ready_queue_shrink(struct cluster * cltr);
[b798713]417
[884f3f67]418//-----------------------------------------------------------------------
419// Decrease the width of the ready queue (number of lanes) by 4
420void ready_queue_close(struct cluster * cltr);
421
[75f3522]422// Local Variables: //
423// mode: c //
424// tab-width: 4 //
[4aa2fb2]425// End: //
Note: See TracBrowser for help on using the repository browser.