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

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since dab09ad was 1eb239e4, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Removed snzi and replaced it with a fast/slow path

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