source: libcfa/src/concurrency/kernel_private.hfa @ a3821fa

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

Changed enable interrupts:

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