source: libcfa/src/concurrency/kernel_private.hfa @ 8157bde

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 8157bde was d3605f8, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Reworked io_uring idle sleep to work with either read or readv depending on what's available.

  • Property mode set to 100644
File size: 11.4 KB
Line 
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//
7// kernel_private.hfa --
8//
9// Author           : Thierry Delisle
10// Created On       : Mon Feb 13 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Aug 12 08:21:33 2020
13// Update Count     : 9
14//
15
16#pragma once
17
18#if !defined(__cforall_thread__)
19        #error kernel_private.hfa should only be included in libcfathread source
20#endif
21
22#include "kernel.hfa"
23#include "thread.hfa"
24
25#include "alarm.hfa"
26#include "stats.hfa"
27
28extern "C" {
29#if   defined(CFA_HAVE_LINUX_LIBRSEQ)
30        #include <rseq/rseq.h>
31#elif defined(CFA_HAVE_LINUX_RSEQ_H)
32        #include <linux/rseq.h>
33#else
34        #ifndef _GNU_SOURCE
35        #error kernel_private requires gnu_source
36        #endif
37        #include <sched.h>
38#endif
39}
40
41// Defines whether or not we *want* to use io_uring_enter as the idle_sleep blocking call
42#define CFA_WANT_IO_URING_IDLE
43
44// Defines whether or not we *can* use io_uring_enter as the idle_sleep blocking call
45#if defined(CFA_WANT_IO_URING_IDLE) && defined(CFA_HAVE_LINUX_IO_URING_H)
46        #if defined(CFA_HAVE_IORING_OP_READ) || (defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV))
47                #define CFA_WITH_IO_URING_IDLE
48        #endif
49#endif
50
51//-----------------------------------------------------------------------------
52// Scheduler
53extern "C" {
54        void disable_interrupts() OPTIONAL_THREAD;
55        void enable_interrupts( bool poll = true );
56}
57
58void schedule_thread$( thread$ *, unpark_hint hint ) __attribute__((nonnull (1)));
59
60extern bool __preemption_enabled();
61
62//release/wake-up the following resources
63void __thread_finish( thread$ * thrd );
64
65//-----------------------------------------------------------------------------
66// Hardware
67
68#if   defined(CFA_HAVE_LINUX_LIBRSEQ)
69        // No data needed
70#elif defined(CFA_HAVE_LINUX_RSEQ_H)
71        extern "Cforall" {
72                extern __attribute__((aligned(128))) thread_local volatile struct rseq __cfaabi_rseq;
73        }
74#else
75        // No data needed
76#endif
77
78static inline int __kernel_getcpu() {
79        /* paranoid */ verify( ! __preemption_enabled() );
80#if   defined(CFA_HAVE_LINUX_LIBRSEQ)
81        return rseq_current_cpu();
82#elif defined(CFA_HAVE_LINUX_RSEQ_H)
83        int r = __cfaabi_rseq.cpu_id;
84        /* paranoid */ verify( r >= 0 );
85        return r;
86#else
87        return sched_getcpu();
88#endif
89}
90
91//-----------------------------------------------------------------------------
92// Processor
93void main(processorCtx_t *);
94
95void * __create_pthread( pthread_t *, void * (*)(void *), void * );
96void __destroy_pthread( pthread_t pthread, void * stack, void ** retval );
97
98extern cluster * mainCluster;
99
100//-----------------------------------------------------------------------------
101// Threads
102extern "C" {
103      void __cfactx_invoke_thread(void (*main)(void *), void * this);
104}
105
106__cfaabi_dbg_debug_do(
107        extern void __cfaabi_dbg_thread_register  ( thread$ * thrd );
108        extern void __cfaabi_dbg_thread_unregister( thread$ * thrd );
109)
110
111#define TICKET_BLOCKED (-1) // thread is blocked
112#define TICKET_RUNNING ( 0) // thread is running
113#define TICKET_UNBLOCK ( 1) // thread should ignore next block
114
115//-----------------------------------------------------------------------------
116// Utils
117void doregister( struct cluster * cltr, struct thread$ & thrd );
118void unregister( struct cluster * cltr, struct thread$ & thrd );
119
120//-----------------------------------------------------------------------------
121// I/O
122$io_arbiter * create(void);
123void destroy($io_arbiter *);
124
125//=======================================================================
126// Cluster lock API
127//=======================================================================
128// Lock-Free registering/unregistering of threads
129// Register a processor to a given cluster and get its unique id in return
130unsigned register_proc_id( void );
131
132// Unregister a processor from a given cluster using its id, getting back the original pointer
133void unregister_proc_id( unsigned );
134
135//=======================================================================
136// Reader-writer lock implementation
137// Concurrent with doregister/unregister,
138//    i.e., threads can be added at any point during or between the entry/exit
139
140//-----------------------------------------------------------------------
141// simple spinlock underlying the RWLock
142// Blocking acquire
143static inline void __atomic_acquire(volatile bool * ll) {
144        while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) {
145                while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED))
146                        Pause();
147        }
148        /* paranoid */ verify(*ll);
149}
150
151// Non-Blocking acquire
152static inline bool __atomic_try_acquire(volatile bool * ll) {
153        return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST);
154}
155
156// Release
157static inline void __atomic_unlock(volatile bool * ll) {
158        /* paranoid */ verify(*ll);
159        __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE);
160}
161
162//-----------------------------------------------------------------------
163// Reader-Writer lock protecting the ready-queues
164// while this lock is mostly generic some aspects
165// have been hard-coded to for the ready-queue for
166// simplicity and performance
167struct __scheduler_RWLock_t {
168        // total cachelines allocated
169        unsigned int max;
170
171        // cachelines currently in use
172        volatile unsigned int alloc;
173
174        // cachelines ready to itereate over
175        // (!= to alloc when thread is in second half of doregister)
176        volatile unsigned int ready;
177
178        // writer lock
179        volatile bool write_lock;
180
181        // data pointer
182        volatile bool * volatile * data;
183};
184
185void  ?{}(__scheduler_RWLock_t & this);
186void ^?{}(__scheduler_RWLock_t & this);
187
188extern __scheduler_RWLock_t * __scheduler_lock;
189
190//-----------------------------------------------------------------------
191// Reader side : acquire when using the ready queue to schedule but not
192//  creating/destroying queues
193static inline void ready_schedule_lock(void) with(*__scheduler_lock) {
194        /* paranoid */ verify( ! __preemption_enabled() );
195        /* paranoid */ verify( ! kernelTLS().in_sched_lock );
196        /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
197        /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
198
199        // Step 1 : make sure no writer are in the middle of the critical section
200        while(__atomic_load_n(&write_lock, (int)__ATOMIC_RELAXED))
201                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( &kernelTLS().sched_lock );
210        /*paranoid*/ verify(kernelTLS().sched_lock);
211
212        #ifdef __CFA_WITH_VERIFY__
213                // Debug, check if this is owned for reading
214                kernelTLS().in_sched_lock = true;
215        #endif
216}
217
218static inline void ready_schedule_unlock(void) with(*__scheduler_lock) {
219        /* paranoid */ verify( ! __preemption_enabled() );
220        /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
221        /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
222        /* paranoid */ verify( kernelTLS().sched_lock );
223        /* paranoid */ verify( kernelTLS().in_sched_lock );
224        #ifdef __CFA_WITH_VERIFY__
225                // Debug, check if this is owned for reading
226                kernelTLS().in_sched_lock = false;
227        #endif
228        __atomic_unlock(&kernelTLS().sched_lock);
229}
230
231#ifdef __CFA_WITH_VERIFY__
232        static inline bool ready_schedule_islocked(void) {
233                /* paranoid */ verify( ! __preemption_enabled() );
234                /* paranoid */ verify( (!kernelTLS().in_sched_lock) || kernelTLS().sched_lock );
235                return kernelTLS().sched_lock;
236        }
237
238        static inline bool ready_mutate_islocked() {
239                return __scheduler_lock->write_lock;
240        }
241#endif
242
243//-----------------------------------------------------------------------
244// Writer side : acquire when changing the ready queue, e.g. adding more
245//  queues or removing them.
246uint_fast32_t ready_mutate_lock( void );
247
248void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ );
249
250//-----------------------------------------------------------------------
251// Lock-Free registering/unregistering of threads
252// Register a processor to a given cluster and get its unique id in return
253// For convenience, also acquires the lock
254static inline [unsigned, uint_fast32_t] ready_mutate_register() {
255        unsigned id = register_proc_id();
256        uint_fast32_t last = ready_mutate_lock();
257        return [id, last];
258}
259
260// Unregister a processor from a given cluster using its id, getting back the original pointer
261// assumes the lock is acquired
262static inline void ready_mutate_unregister( unsigned id, uint_fast32_t last_s ) {
263        ready_mutate_unlock( last_s );
264        unregister_proc_id( id );
265}
266
267//-----------------------------------------------------------------------
268// Cluster idle lock/unlock
269static inline void lock(__cluster_proc_list & this) {
270        /* paranoid */ verify( ! __preemption_enabled() );
271
272        // Start by locking the global RWlock so that we know no-one is
273        // adding/removing processors while we mess with the idle lock
274        ready_schedule_lock();
275
276        lock( this.lock __cfaabi_dbg_ctx2 );
277
278        /* paranoid */ verify( ! __preemption_enabled() );
279}
280
281static inline bool try_lock(__cluster_proc_list & this) {
282        /* paranoid */ verify( ! __preemption_enabled() );
283
284        // Start by locking the global RWlock so that we know no-one is
285        // adding/removing processors while we mess with the idle lock
286        ready_schedule_lock();
287
288        if(try_lock( this.lock __cfaabi_dbg_ctx2 )) {
289                // success
290                /* paranoid */ verify( ! __preemption_enabled() );
291                return true;
292        }
293
294        // failed to lock
295        ready_schedule_unlock();
296
297        /* paranoid */ verify( ! __preemption_enabled() );
298        return false;
299}
300
301static inline void unlock(__cluster_proc_list & this) {
302        /* paranoid */ verify( ! __preemption_enabled() );
303
304        unlock(this.lock);
305
306        // Release the global lock, which we acquired when locking
307        ready_schedule_unlock();
308
309        /* paranoid */ verify( ! __preemption_enabled() );
310}
311
312//=======================================================================
313// Ready-Queue API
314//-----------------------------------------------------------------------
315// push thread onto a ready queue for a cluster
316// returns true if the list was previously empty, false otherwise
317__attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint);
318
319//-----------------------------------------------------------------------
320// pop thread from the local queues of a cluster
321// returns 0p if empty
322// May return 0p spuriously
323__attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr);
324
325//-----------------------------------------------------------------------
326// pop thread from any ready queue of a cluster
327// returns 0p if empty
328// May return 0p spuriously
329__attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr);
330
331//-----------------------------------------------------------------------
332// search all ready queues of a cluster for any thread
333// returns 0p if empty
334// guaranteed to find any threads added before this call
335__attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr);
336
337//-----------------------------------------------------------------------
338// get preferred ready for new thread
339unsigned ready_queue_new_preferred();
340
341//-----------------------------------------------------------------------
342// Increase the width of the ready queue (number of lanes) by 4
343void ready_queue_grow  (struct cluster * cltr);
344
345//-----------------------------------------------------------------------
346// Decrease the width of the ready queue (number of lanes) by 4
347void ready_queue_shrink(struct cluster * cltr);
348
349
350// Local Variables: //
351// mode: c //
352// tab-width: 4 //
353// End: //
Note: See TracBrowser for help on using the repository browser.