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 | |
---|
28 | extern "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 |
---|
53 | extern "C" { |
---|
54 | void disable_interrupts() OPTIONAL_THREAD; |
---|
55 | void enable_interrupts( bool poll = true ); |
---|
56 | } |
---|
57 | |
---|
58 | void schedule_thread$( thread$ *, unpark_hint hint ) __attribute__((nonnull (1))); |
---|
59 | |
---|
60 | extern bool __preemption_enabled(); |
---|
61 | |
---|
62 | enum { |
---|
63 | PREEMPT_NORMAL = 0, |
---|
64 | PREEMPT_TERMINATE = 1, |
---|
65 | PREEMPT_IO = 2, |
---|
66 | }; |
---|
67 | |
---|
68 | static inline void __disable_interrupts_checked() { |
---|
69 | /* paranoid */ verify( __preemption_enabled() ); |
---|
70 | disable_interrupts(); |
---|
71 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
72 | } |
---|
73 | |
---|
74 | static inline void __enable_interrupts_checked( bool poll = true ) { |
---|
75 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
76 | enable_interrupts( poll ); |
---|
77 | /* paranoid */ verify( __preemption_enabled() ); |
---|
78 | } |
---|
79 | |
---|
80 | //release/wake-up the following resources |
---|
81 | void __thread_finish( thread$ * thrd ); |
---|
82 | |
---|
83 | //----------------------------------------------------------------------------- |
---|
84 | // Hardware |
---|
85 | |
---|
86 | #if defined(CFA_HAVE_LINUX_LIBRSEQ) |
---|
87 | // No data needed |
---|
88 | #elif defined(CFA_HAVE_LINUX_RSEQ_H) |
---|
89 | extern "Cforall" { |
---|
90 | extern __attribute__((aligned(128))) thread_local volatile struct rseq __cfaabi_rseq; |
---|
91 | } |
---|
92 | #else |
---|
93 | // No data needed |
---|
94 | #endif |
---|
95 | |
---|
96 | static inline int __kernel_getcpu() { |
---|
97 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
98 | #if defined(CFA_HAVE_LINUX_LIBRSEQ) |
---|
99 | return rseq_current_cpu(); |
---|
100 | #elif defined(CFA_HAVE_LINUX_RSEQ_H) |
---|
101 | int r = __cfaabi_rseq.cpu_id; |
---|
102 | /* paranoid */ verify( r >= 0 ); |
---|
103 | return r; |
---|
104 | #else |
---|
105 | return sched_getcpu(); |
---|
106 | #endif |
---|
107 | } |
---|
108 | |
---|
109 | //----------------------------------------------------------------------------- |
---|
110 | // Processor |
---|
111 | void main(processorCtx_t &); |
---|
112 | static inline coroutine$* get_coroutine(processorCtx_t & this) { return &this.self; } |
---|
113 | |
---|
114 | void * __create_pthread( pthread_t *, void * (*)(void *), void * ); |
---|
115 | void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ); |
---|
116 | |
---|
117 | extern cluster * mainCluster; |
---|
118 | |
---|
119 | //----------------------------------------------------------------------------- |
---|
120 | // Threads |
---|
121 | extern "C" { |
---|
122 | void __cfactx_invoke_thread(void (*main)(void *), void * this); |
---|
123 | } |
---|
124 | |
---|
125 | __cfaabi_dbg_debug_do( |
---|
126 | extern void __cfaabi_dbg_thread_register ( thread$ * thrd ); |
---|
127 | extern void __cfaabi_dbg_thread_unregister( thread$ * thrd ); |
---|
128 | ) |
---|
129 | |
---|
130 | #define TICKET_BLOCKED (-1) // thread is blocked |
---|
131 | #define TICKET_RUNNING ( 0) // thread is running |
---|
132 | #define TICKET_UNBLOCK ( 1) // thread should ignore next block |
---|
133 | |
---|
134 | //----------------------------------------------------------------------------- |
---|
135 | // Utils |
---|
136 | void doregister( struct cluster * cltr, struct thread$ & thrd ); |
---|
137 | void unregister( struct cluster * cltr, struct thread$ & thrd ); |
---|
138 | |
---|
139 | //----------------------------------------------------------------------------- |
---|
140 | // I/O |
---|
141 | $io_arbiter * create(void); |
---|
142 | void destroy($io_arbiter *); |
---|
143 | |
---|
144 | //======================================================================= |
---|
145 | // Cluster lock API |
---|
146 | //======================================================================= |
---|
147 | // Lock-Free registering/unregistering of threads |
---|
148 | // Register a processor to a given cluster and get its unique id in return |
---|
149 | unsigned register_proc_id( void ); |
---|
150 | |
---|
151 | // Unregister a processor from a given cluster using its id, getting back the original pointer |
---|
152 | void unregister_proc_id( unsigned ); |
---|
153 | |
---|
154 | //======================================================================= |
---|
155 | // Reader-writer lock implementation |
---|
156 | // Concurrent with doregister/unregister, |
---|
157 | // i.e., threads can be added at any point during or between the entry/exit |
---|
158 | |
---|
159 | //----------------------------------------------------------------------- |
---|
160 | // simple spinlock underlying the RWLock |
---|
161 | // Blocking acquire |
---|
162 | static inline void __atomic_acquire(volatile bool * ll) { |
---|
163 | while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) { |
---|
164 | while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED)) |
---|
165 | Pause(); |
---|
166 | } |
---|
167 | /* paranoid */ verify(*ll); |
---|
168 | } |
---|
169 | |
---|
170 | // Non-Blocking acquire |
---|
171 | static inline bool __atomic_try_acquire(volatile bool * ll) { |
---|
172 | return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST); |
---|
173 | } |
---|
174 | |
---|
175 | // Release |
---|
176 | static inline void __atomic_unlock(volatile bool * ll) { |
---|
177 | /* paranoid */ verify(*ll); |
---|
178 | __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE); |
---|
179 | } |
---|
180 | |
---|
181 | //----------------------------------------------------------------------- |
---|
182 | // Reader-Writer lock protecting the ready-queues |
---|
183 | // while this lock is mostly generic some aspects |
---|
184 | // have been hard-coded to for the ready-queue for |
---|
185 | // simplicity and performance |
---|
186 | struct __scheduler_RWLock_t { |
---|
187 | // total cachelines allocated |
---|
188 | unsigned int max; |
---|
189 | |
---|
190 | // cachelines currently in use |
---|
191 | volatile unsigned int alloc; |
---|
192 | |
---|
193 | // cachelines ready to itereate over |
---|
194 | // (!= to alloc when thread is in second half of doregister) |
---|
195 | volatile unsigned int ready; |
---|
196 | |
---|
197 | // writer lock |
---|
198 | volatile bool write_lock; |
---|
199 | |
---|
200 | // data pointer |
---|
201 | volatile bool * volatile * data; |
---|
202 | }; |
---|
203 | |
---|
204 | void ?{}(__scheduler_RWLock_t & this); |
---|
205 | void ^?{}(__scheduler_RWLock_t & this); |
---|
206 | |
---|
207 | extern __scheduler_RWLock_t * __scheduler_lock; |
---|
208 | |
---|
209 | //----------------------------------------------------------------------- |
---|
210 | // Reader side : acquire when using the ready queue to schedule but not |
---|
211 | // creating/destroying queues |
---|
212 | static inline void ready_schedule_lock(void) with(*__scheduler_lock) { |
---|
213 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
214 | /* paranoid */ verify( ! kernelTLS().in_sched_lock ); |
---|
215 | /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock ); |
---|
216 | /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id ); |
---|
217 | |
---|
218 | // Step 1 : make sure no writer are in the middle of the critical section |
---|
219 | while(__atomic_load_n(&write_lock, (int)__ATOMIC_RELAXED)) |
---|
220 | Pause(); |
---|
221 | |
---|
222 | // Fence needed because we don't want to start trying to acquire the lock |
---|
223 | // before we read a false. |
---|
224 | // Not needed on x86 |
---|
225 | // std::atomic_thread_fence(std::memory_order_seq_cst); |
---|
226 | |
---|
227 | // Step 2 : acquire our local lock |
---|
228 | __atomic_acquire( &kernelTLS().sched_lock ); |
---|
229 | /*paranoid*/ verify(kernelTLS().sched_lock); |
---|
230 | |
---|
231 | #ifdef __CFA_WITH_VERIFY__ |
---|
232 | // Debug, check if this is owned for reading |
---|
233 | kernelTLS().in_sched_lock = true; |
---|
234 | #endif |
---|
235 | } |
---|
236 | |
---|
237 | static inline void ready_schedule_unlock(void) with(*__scheduler_lock) { |
---|
238 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
239 | /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock ); |
---|
240 | /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id ); |
---|
241 | /* paranoid */ verify( kernelTLS().sched_lock ); |
---|
242 | /* paranoid */ verify( kernelTLS().in_sched_lock ); |
---|
243 | #ifdef __CFA_WITH_VERIFY__ |
---|
244 | // Debug, check if this is owned for reading |
---|
245 | kernelTLS().in_sched_lock = false; |
---|
246 | #endif |
---|
247 | __atomic_unlock(&kernelTLS().sched_lock); |
---|
248 | } |
---|
249 | |
---|
250 | #ifdef __CFA_WITH_VERIFY__ |
---|
251 | static inline bool ready_schedule_islocked(void) { |
---|
252 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
253 | /* paranoid */ verify( (!kernelTLS().in_sched_lock) || kernelTLS().sched_lock ); |
---|
254 | return kernelTLS().sched_lock; |
---|
255 | } |
---|
256 | |
---|
257 | static inline bool ready_mutate_islocked() { |
---|
258 | return __scheduler_lock->write_lock; |
---|
259 | } |
---|
260 | #endif |
---|
261 | |
---|
262 | //----------------------------------------------------------------------- |
---|
263 | // Writer side : acquire when changing the ready queue, e.g. adding more |
---|
264 | // queues or removing them. |
---|
265 | uint_fast32_t ready_mutate_lock( void ); |
---|
266 | |
---|
267 | void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ ); |
---|
268 | |
---|
269 | //----------------------------------------------------------------------- |
---|
270 | // Lock-Free registering/unregistering of threads |
---|
271 | // Register a processor to a given cluster and get its unique id in return |
---|
272 | // For convenience, also acquires the lock |
---|
273 | static inline [unsigned, uint_fast32_t] ready_mutate_register() { |
---|
274 | unsigned id = register_proc_id(); |
---|
275 | uint_fast32_t last = ready_mutate_lock(); |
---|
276 | return [id, last]; |
---|
277 | } |
---|
278 | |
---|
279 | // Unregister a processor from a given cluster using its id, getting back the original pointer |
---|
280 | // assumes the lock is acquired |
---|
281 | static inline void ready_mutate_unregister( unsigned id, uint_fast32_t last_s ) { |
---|
282 | ready_mutate_unlock( last_s ); |
---|
283 | unregister_proc_id( id ); |
---|
284 | } |
---|
285 | |
---|
286 | //----------------------------------------------------------------------- |
---|
287 | // Cluster idle lock/unlock |
---|
288 | static inline void lock(__cluster_proc_list & this) { |
---|
289 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
290 | |
---|
291 | // Start by locking the global RWlock so that we know no-one is |
---|
292 | // adding/removing processors while we mess with the idle lock |
---|
293 | ready_schedule_lock(); |
---|
294 | |
---|
295 | lock( this.lock __cfaabi_dbg_ctx2 ); |
---|
296 | |
---|
297 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
298 | } |
---|
299 | |
---|
300 | static inline bool try_lock(__cluster_proc_list & this) { |
---|
301 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
302 | |
---|
303 | // Start by locking the global RWlock so that we know no-one is |
---|
304 | // adding/removing processors while we mess with the idle lock |
---|
305 | ready_schedule_lock(); |
---|
306 | |
---|
307 | if(try_lock( this.lock __cfaabi_dbg_ctx2 )) { |
---|
308 | // success |
---|
309 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
310 | return true; |
---|
311 | } |
---|
312 | |
---|
313 | // failed to lock |
---|
314 | ready_schedule_unlock(); |
---|
315 | |
---|
316 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
317 | return false; |
---|
318 | } |
---|
319 | |
---|
320 | static inline void unlock(__cluster_proc_list & this) { |
---|
321 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
322 | |
---|
323 | unlock(this.lock); |
---|
324 | |
---|
325 | // Release the global lock, which we acquired when locking |
---|
326 | ready_schedule_unlock(); |
---|
327 | |
---|
328 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
329 | } |
---|
330 | |
---|
331 | //======================================================================= |
---|
332 | // Ready-Queue API |
---|
333 | //----------------------------------------------------------------------- |
---|
334 | // push thread onto a ready queue for a cluster |
---|
335 | // returns true if the list was previously empty, false otherwise |
---|
336 | __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint); |
---|
337 | |
---|
338 | //----------------------------------------------------------------------- |
---|
339 | // pop thread from the local queues of a cluster |
---|
340 | // returns 0p if empty |
---|
341 | // May return 0p spuriously |
---|
342 | __attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr); |
---|
343 | |
---|
344 | //----------------------------------------------------------------------- |
---|
345 | // pop thread from any ready queue of a cluster |
---|
346 | // returns 0p if empty |
---|
347 | // May return 0p spuriously |
---|
348 | __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr); |
---|
349 | |
---|
350 | //----------------------------------------------------------------------- |
---|
351 | // search all ready queues of a cluster for any thread |
---|
352 | // returns 0p if empty |
---|
353 | // guaranteed to find any threads added before this call |
---|
354 | __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr); |
---|
355 | |
---|
356 | //----------------------------------------------------------------------- |
---|
357 | // get preferred ready for new thread |
---|
358 | unsigned ready_queue_new_preferred(); |
---|
359 | |
---|
360 | //----------------------------------------------------------------------- |
---|
361 | // Increase the width of the ready queue (number of lanes) by 4 |
---|
362 | void ready_queue_grow (struct cluster * cltr); |
---|
363 | |
---|
364 | //----------------------------------------------------------------------- |
---|
365 | // Decrease the width of the ready queue (number of lanes) by 4 |
---|
366 | void ready_queue_shrink(struct cluster * cltr); |
---|
367 | |
---|
368 | //----------------------------------------------------------------------- |
---|
369 | // Decrease the width of the ready queue (number of lanes) by 4 |
---|
370 | void ready_queue_close(struct cluster * cltr); |
---|
371 | |
---|
372 | // Local Variables: // |
---|
373 | // mode: c // |
---|
374 | // tab-width: 4 // |
---|
375 | // End: // |
---|