source: libcfa/src/concurrency/kernel.hfa @ c402739f

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

I/O operations now use futures.
io calls implementation are now generated at configure time.

  • Property mode set to 100644
File size: 9.0 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 --
8//
9// Author           : Thierry Delisle
10// Created On       : Tue Jan 17 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Feb  4 12:29:26 2020
13// Update Count     : 22
14//
15
16#pragma once
17
18#include "invoke.h"
19#include "time_t.hfa"
20#include "coroutine.hfa"
21
22#include "containers/list.hfa"
23
24extern "C" {
25        #include <bits/pthreadtypes.h>
26        #include <linux/types.h>
27}
28
29//-----------------------------------------------------------------------------
30// Locks
31struct semaphore {
32        __spinlock_t lock;
33        int count;
34        __queue_t($thread) waiting;
35};
36
37void  ?{}(semaphore & this, int count = 1);
38void ^?{}(semaphore & this);
39bool   P (semaphore & this);
40bool   V (semaphore & this);
41bool   V (semaphore & this, unsigned count);
42
43
44//-----------------------------------------------------------------------------
45// Processor
46extern struct cluster * mainCluster;
47
48// Processor id, required for scheduling threads
49struct __processor_id_t {
50        unsigned id;
51
52        #if !defined(__CFA_NO_STATISTICS__)
53                struct __stats_t * stats;
54        #endif
55};
56
57coroutine processorCtx_t {
58        struct processor * proc;
59};
60
61// Wrapper around kernel threads
62struct __attribute__((aligned(128))) processor {
63        // Main state
64        inline __processor_id_t;
65
66        // Cluster from which to get threads
67        struct cluster * cltr;
68
69        // Set to true to notify the processor should terminate
70        volatile bool do_terminate;
71
72        // Coroutine ctx who does keeps the state of the processor
73        struct processorCtx_t runner;
74
75        // Name of the processor
76        const char * name;
77
78        // Handle to pthreads
79        pthread_t kernel_thread;
80
81        // RunThread data
82        // Action to do after a thread is ran
83        $thread * destroyer;
84
85        // Preemption data
86        // Node which is added in the discrete event simulaiton
87        struct alarm_node_t * preemption_alarm;
88
89        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
90        bool pending_preemption;
91
92        // Idle lock (kernel semaphore)
93        __bin_sem_t idle;
94
95        // Termination synchronisation (user semaphore)
96        semaphore terminated;
97
98        // pthread Stack
99        void * stack;
100
101        // Link lists fields
102        DLISTED_MGD_IMPL_IN(processor)
103
104        #if !defined(__CFA_NO_STATISTICS__)
105                int print_stats;
106                bool print_halts;
107        #endif
108
109#ifdef __CFA_DEBUG__
110        // Last function to enable preemption on this processor
111        const char * last_enable;
112#endif
113};
114
115void  ?{}(processor & this, const char name[], struct cluster & cltr);
116void ^?{}(processor & this);
117
118static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
119static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
120static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
121
122DLISTED_MGD_IMPL_OUT(processor)
123
124//-----------------------------------------------------------------------------
125// I/O
126struct __io_data;
127
128// IO poller user-thread
129// Not using the "thread" keyword because we want to control
130// more carefully when to start/stop it
131struct $io_ctx_thread {
132        struct __io_data * ring;
133        single_sem sem;
134        volatile bool done;
135        $thread self;
136};
137
138
139struct io_context {
140        $io_ctx_thread thrd;
141};
142
143struct io_context_params {
144        int num_entries;
145        int num_ready;
146        int submit_aff;
147        bool eager_submits:1;
148        bool poller_submits:1;
149        bool poll_submit:1;
150        bool poll_complete:1;
151};
152
153void  ?{}(io_context_params & this);
154
155void  ?{}(io_context & this, struct cluster & cl);
156void  ?{}(io_context & this, struct cluster & cl, const io_context_params & params);
157void ^?{}(io_context & this);
158
159struct io_cancellation {
160        __u64 target;
161};
162
163static inline void  ?{}(io_cancellation & this) { this.target = -1u; }
164static inline void ^?{}(io_cancellation & this) {}
165bool cancel(io_cancellation & this);
166
167//-----------------------------------------------------------------------------
168// Cluster Tools
169
170// Intrusives lanes which are used by the relaxed ready queue
171struct __attribute__((aligned(128))) __intrusive_lane_t;
172void  ?{}(__intrusive_lane_t & this);
173void ^?{}(__intrusive_lane_t & this);
174
175// Counter used for wether or not the lanes are all empty
176struct __attribute__((aligned(128))) __snzi_node_t;
177struct __snzi_t {
178        unsigned mask;
179        int root;
180        __snzi_node_t * nodes;
181};
182
183void  ?{}( __snzi_t & this, unsigned depth );
184void ^?{}( __snzi_t & this );
185
186//TODO adjust cache size to ARCHITECTURE
187// Structure holding the relaxed ready queue
188struct __ready_queue_t {
189        // Data tracking how many/which lanes are used
190        // Aligned to 128 for cache locality
191        __snzi_t snzi;
192
193        // Data tracking the actual lanes
194        // On a seperate cacheline from the used struct since
195        // used can change on each push/pop but this data
196        // only changes on shrink/grow
197        struct {
198                // Arary of lanes
199                __intrusive_lane_t * volatile data;
200
201                // Number of lanes (empty or not)
202                volatile size_t count;
203        } lanes;
204};
205
206void  ?{}(__ready_queue_t & this);
207void ^?{}(__ready_queue_t & this);
208
209// Idle Sleep
210struct __cluster_idles {
211        // Spin lock protecting the queue
212        volatile uint64_t lock;
213
214        // Total number of processors
215        unsigned total;
216
217        // Total number of idle processors
218        unsigned idle;
219
220        // List of idle processors
221        dlist(processor, processor) list;
222};
223
224//-----------------------------------------------------------------------------
225// Cluster
226struct __attribute__((aligned(128))) cluster {
227        // Ready queue for threads
228        __ready_queue_t ready_queue;
229
230        // Name of the cluster
231        const char * name;
232
233        // Preemption rate on this cluster
234        Duration preemption_rate;
235
236        // List of idle processors
237        __cluster_idles idles;
238
239        // List of threads
240        __spinlock_t thread_list_lock;
241        __dllist_t(struct $thread) threads;
242        unsigned int nthreads;
243
244        // Link lists fields
245        struct __dbg_node_cltr {
246                cluster * next;
247                cluster * prev;
248        } node;
249
250        struct {
251                io_context * ctxs;
252                unsigned cnt;
253        } io;
254
255        #if !defined(__CFA_NO_STATISTICS__)
256                struct __stats_t * stats;
257                int print_stats;
258        #endif
259};
260extern Duration default_preemption();
261
262void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
263void ^?{}(cluster & this);
264
265static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; }
266static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; }
267static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; }
268static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; }
269static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io) { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, num_io, default_params}; }
270static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; }
271static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; }
272static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; }
273static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; }
274static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; }
275static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, num_io, io_params}; }
276static inline void ?{} (cluster & this, const char name[], unsigned num_io, const io_context_params & io_params)        { this{name, default_preemption(), num_io, io_params}; }
277
278static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
279
280static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
281static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
282
283#if !defined(__CFA_NO_STATISTICS__)
284        static inline void print_stats_at_exit( cluster & this, int flags ) {
285                this.print_stats |= flags;
286        }
287
288        static inline void print_stats_at_exit( processor & this, int flags ) {
289                this.print_stats |= flags;
290        }
291
292        void print_halts( processor & this );
293#endif
294
295// Local Variables: //
296// mode: c //
297// tab-width: 4 //
298// End: //
Note: See TracBrowser for help on using the repository browser.