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

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since e7c077a was da3963a, checked in by Thierry Delisle <tdelisle@…>, 10 months ago

Moved bin_sem_t out of kernel.hfa since it's not needed.

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