source: libcfa/src/concurrency/kernel/private.hfa@ fa2e183

ADT ast-experimental
Last change on this file since fa2e183 was df6cc9d, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Merge branch 'master' into pthread-emulation

  • 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
168
[969b3fe]169//-----------------------------------------------------------------------------
170// Utils
[e84ab3d]171void doregister( struct cluster * cltr, struct thread$ & thrd );
172void unregister( struct cluster * cltr, struct thread$ & thrd );
[de94a60]173
[f00b26d4]174//-----------------------------------------------------------------------------
175// I/O
[8bee858]176io_arbiter$ * create(void);
177void destroy(io_arbiter$ *);
[f00b26d4]178
[7768b8d]179//=======================================================================
180// Cluster lock API
181//=======================================================================
182// Lock-Free registering/unregistering of threads
183// Register a processor to a given cluster and get its unique id in return
[c993b15]184unsigned register_proc_id( void );
[7768b8d]185
186// Unregister a processor from a given cluster using its id, getting back the original pointer
[c993b15]187void unregister_proc_id( unsigned );
[7768b8d]188
189//=======================================================================
190// Reader-writer lock implementation
191// Concurrent with doregister/unregister,
192// i.e., threads can be added at any point during or between the entry/exit
[dca5802]193
194//-----------------------------------------------------------------------
195// simple spinlock underlying the RWLock
196// Blocking acquire
[7768b8d]197static inline void __atomic_acquire(volatile bool * ll) {
[2284d20]198 /* paranoid */ verify( ! __preemption_enabled() );
199 /* paranoid */ verify(ll);
200
[7768b8d]201 while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) {
202 while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED))
[fd9b524]203 Pause();
[7768b8d]204 }
205 /* paranoid */ verify(*ll);
[2284d20]206 /* paranoid */ verify( ! __preemption_enabled() );
[7768b8d]207}
208
[dca5802]209// Non-Blocking acquire
[7768b8d]210static inline bool __atomic_try_acquire(volatile bool * ll) {
[2284d20]211 /* paranoid */ verify( ! __preemption_enabled() );
212 /* paranoid */ verify(ll);
213
[b798713]214 return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST);
[7768b8d]215}
216
[dca5802]217// Release
[7768b8d]218static inline void __atomic_unlock(volatile bool * ll) {
[2284d20]219 /* paranoid */ verify( ! __preemption_enabled() );
220 /* paranoid */ verify(ll);
[7768b8d]221 /* paranoid */ verify(*ll);
222 __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE);
223}
224
[b388ee81]225//-----------------------------------------------------------------------
226// Reader-Writer lock protecting the ready-queues
227// while this lock is mostly generic some aspects
228// have been hard-coded to for the ready-queue for
229// simplicity and performance
[741e22c]230union __attribute__((aligned(64))) __scheduler_RWLock_t {
231 struct {
[cd3fc46]232 __attribute__((aligned(64))) char padding;
233
[741e22c]234 // total cachelines allocated
[cd3fc46]235 __attribute__((aligned(64))) unsigned int max;
[b388ee81]236
[741e22c]237 // cachelines currently in use
238 volatile unsigned int alloc;
[b388ee81]239
[741e22c]240 // cachelines ready to itereate over
241 // (!= to alloc when thread is in second half of doregister)
242 volatile unsigned int ready;
[b388ee81]243
[741e22c]244 // writer lock
245 volatile bool write_lock;
[b388ee81]246
[741e22c]247 // data pointer
248 volatile bool * volatile * data;
249 } lock;
250 char pad[192];
[b388ee81]251};
252
253void ?{}(__scheduler_RWLock_t & this);
254void ^?{}(__scheduler_RWLock_t & this);
255
[cd3fc46]256extern __scheduler_RWLock_t __scheduler_lock;
[b388ee81]257
[7768b8d]258//-----------------------------------------------------------------------
259// Reader side : acquire when using the ready queue to schedule but not
260// creating/destroying queues
[cd3fc46]261static inline void ready_schedule_lock(void) with(__scheduler_lock.lock) {
[8fc652e0]262 /* paranoid */ verify( ! __preemption_enabled() );
[c993b15]263 /* paranoid */ verify( ! kernelTLS().in_sched_lock );
264 /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
265 /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
[7768b8d]266
267 // Step 1 : make sure no writer are in the middle of the critical section
[c993b15]268 while(__atomic_load_n(&write_lock, (int)__ATOMIC_RELAXED))
[fd9b524]269 Pause();
[7768b8d]270
271 // Fence needed because we don't want to start trying to acquire the lock
272 // before we read a false.
273 // Not needed on x86
274 // std::atomic_thread_fence(std::memory_order_seq_cst);
275
276 // Step 2 : acquire our local lock
[c993b15]277 __atomic_acquire( &kernelTLS().sched_lock );
278 /*paranoid*/ verify(kernelTLS().sched_lock);
[64a7146]279
280 #ifdef __CFA_WITH_VERIFY__
281 // Debug, check if this is owned for reading
[c993b15]282 kernelTLS().in_sched_lock = true;
[64a7146]283 #endif
[7768b8d]284}
285
[cd3fc46]286static inline void ready_schedule_unlock(void) with(__scheduler_lock.lock) {
[8fc652e0]287 /* paranoid */ verify( ! __preemption_enabled() );
[c993b15]288 /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
289 /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
290 /* paranoid */ verify( kernelTLS().sched_lock );
291 /* paranoid */ verify( kernelTLS().in_sched_lock );
[64a7146]292 #ifdef __CFA_WITH_VERIFY__
293 // Debug, check if this is owned for reading
[c993b15]294 kernelTLS().in_sched_lock = false;
[64a7146]295 #endif
[c993b15]296 __atomic_unlock(&kernelTLS().sched_lock);
[7768b8d]297}
298
[64a7146]299#ifdef __CFA_WITH_VERIFY__
[e873838]300 static inline bool ready_schedule_islocked(void) {
[8fc652e0]301 /* paranoid */ verify( ! __preemption_enabled() );
[c993b15]302 /* paranoid */ verify( (!kernelTLS().in_sched_lock) || kernelTLS().sched_lock );
303 return kernelTLS().sched_lock;
[64a7146]304 }
305
306 static inline bool ready_mutate_islocked() {
[cd3fc46]307 return __scheduler_lock.lock.write_lock;
[64a7146]308 }
309#endif
310
[7768b8d]311//-----------------------------------------------------------------------
312// Writer side : acquire when changing the ready queue, e.g. adding more
313// queues or removing them.
[b388ee81]314uint_fast32_t ready_mutate_lock( void );
[7768b8d]315
[b388ee81]316void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ );
[7768b8d]317
[a33c113]318//-----------------------------------------------------------------------
319// Lock-Free registering/unregistering of threads
320// Register a processor to a given cluster and get its unique id in return
321// For convenience, also acquires the lock
[c993b15]322static inline [unsigned, uint_fast32_t] ready_mutate_register() {
323 unsigned id = register_proc_id();
324 uint_fast32_t last = ready_mutate_lock();
325 return [id, last];
[a33c113]326}
327
328// Unregister a processor from a given cluster using its id, getting back the original pointer
329// assumes the lock is acquired
[c993b15]330static inline void ready_mutate_unregister( unsigned id, uint_fast32_t last_s ) {
[a33c113]331 ready_mutate_unlock( last_s );
[c993b15]332 unregister_proc_id( id );
[a33c113]333}
334
[a7504db5]335//-----------------------------------------------------------------------
336// Cluster idle lock/unlock
[6a9b12b]337static inline void lock(__cluster_proc_list & this) {
[a7504db5]338 /* paranoid */ verify( ! __preemption_enabled() );
339
340 // Start by locking the global RWlock so that we know no-one is
341 // adding/removing processors while we mess with the idle lock
342 ready_schedule_lock();
343
[a633f6f]344 lock( this.lock __cfaabi_dbg_ctx2 );
[a7504db5]345
346 /* paranoid */ verify( ! __preemption_enabled() );
347}
348
[5f5a729]349static inline bool try_lock(__cluster_proc_list & this) {
350 /* paranoid */ verify( ! __preemption_enabled() );
351
352 // Start by locking the global RWlock so that we know no-one is
353 // adding/removing processors while we mess with the idle lock
354 ready_schedule_lock();
355
[a633f6f]356 if(try_lock( this.lock __cfaabi_dbg_ctx2 )) {
[5f5a729]357 // success
358 /* paranoid */ verify( ! __preemption_enabled() );
359 return true;
360 }
361
362 // failed to lock
363 ready_schedule_unlock();
364
365 /* paranoid */ verify( ! __preemption_enabled() );
366 return false;
367}
368
[6a9b12b]369static inline void unlock(__cluster_proc_list & this) {
[a7504db5]370 /* paranoid */ verify( ! __preemption_enabled() );
371
[a633f6f]372 unlock(this.lock);
[a7504db5]373
374 // Release the global lock, which we acquired when locking
375 ready_schedule_unlock();
376
377 /* paranoid */ verify( ! __preemption_enabled() );
378}
379
[b798713]380//=======================================================================
381// Ready-Queue API
[dca5802]382//-----------------------------------------------------------------------
383// push thread onto a ready queue for a cluster
384// returns true if the list was previously empty, false otherwise
[24e321c]385__attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint);
[dca5802]386
387//-----------------------------------------------------------------------
[fc59df78]388// pop thread from the local queues of a cluster
[dca5802]389// returns 0p if empty
[1eb239e4]390// May return 0p spuriously
[e84ab3d]391__attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr);
[dca5802]392
[1eb239e4]393//-----------------------------------------------------------------------
[fc59df78]394// pop thread from any ready queue of a cluster
[1eb239e4]395// returns 0p if empty
[fc59df78]396// May return 0p spuriously
[e84ab3d]397__attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr);
[1eb239e4]398
[fc59df78]399//-----------------------------------------------------------------------
400// search all ready queues of a cluster for any thread
401// returns 0p if empty
402// guaranteed to find any threads added before this call
[e84ab3d]403__attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr);
[fc59df78]404
[24e321c]405//-----------------------------------------------------------------------
406// get preferred ready for new thread
407unsigned ready_queue_new_preferred();
408
[dca5802]409//-----------------------------------------------------------------------
410// Increase the width of the ready queue (number of lanes) by 4
[a017ee7]411void ready_queue_grow (struct cluster * cltr);
[dca5802]412
413//-----------------------------------------------------------------------
414// Decrease the width of the ready queue (number of lanes) by 4
[a017ee7]415void ready_queue_shrink(struct cluster * cltr);
[b798713]416
[884f3f67]417//-----------------------------------------------------------------------
418// Decrease the width of the ready queue (number of lanes) by 4
419void ready_queue_close(struct cluster * cltr);
420
[75f3522]421// Local Variables: //
422// mode: c //
423// tab-width: 4 //
[4aa2fb2]424// End: //
Note: See TracBrowser for help on using the repository browser.