source: libcfa/src/concurrency/kernel.hfa@ 930e57e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 930e57e was c402739f, checked in by Thierry Delisle <tdelisle@…>, 5 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
RevLine 
[8118303]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//
[75a17f1]7// kernel --
[8118303]8//
9// Author : Thierry Delisle
[75f3522]10// Created On : Tue Jan 17 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[e3fea42]12// Last Modified On : Tue Feb 4 12:29:26 2020
13// Update Count : 22
[8118303]14//
15
[6b0b624]16#pragma once
[8118303]17
[bd98b58]18#include "invoke.h"
[73abe95]19#include "time_t.hfa"
[d76bd79]20#include "coroutine.hfa"
[bd98b58]21
[1eb239e4]22#include "containers/list.hfa"
[64a7146]23
[8def349]24extern "C" {
[c402739f]25 #include <bits/pthreadtypes.h>
26 #include <linux/types.h>
[8def349]27}
28
[db6f06a]29//-----------------------------------------------------------------------------
30// Locks
[bdeba0b]31struct semaphore {
[ea7d2b0]32 __spinlock_t lock;
[bdeba0b]33 int count;
[ac2b598]34 __queue_t($thread) waiting;
[9c31349]35};
36
[242a902]37void ?{}(semaphore & this, int count = 1);
38void ^?{}(semaphore & this);
[71c8b7e]39bool P (semaphore & this);
[f0ce5f4]40bool V (semaphore & this);
[d384787]41bool V (semaphore & this, unsigned count);
[9c31349]42
[db6f06a]43
[bd98b58]44//-----------------------------------------------------------------------------
[de94a60]45// Processor
[de6319f]46extern struct cluster * mainCluster;
[bd98b58]47
[9b1dcc2]48// Processor id, required for scheduling threads
49struct __processor_id_t {
50 unsigned id;
[8834751]51
52 #if !defined(__CFA_NO_STATISTICS__)
53 struct __stats_t * stats;
54 #endif
[9b1dcc2]55};
56
[094476d]57coroutine processorCtx_t {
58 struct processor * proc;
59};
60
[e60e0dc]61// Wrapper around kernel threads
[37ba662]62struct __attribute__((aligned(128))) processor {
[e60e0dc]63 // Main state
[37ba662]64 inline __processor_id_t;
[025278e]65
66 // Cluster from which to get threads
[de94a60]67 struct cluster * cltr;
[025278e]68
[37ba662]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
[de6319f]75 // Name of the processor
76 const char * name;
77
[025278e]78 // Handle to pthreads
79 pthread_t kernel_thread;
[2ac095d]80
[e60e0dc]81 // RunThread data
[025278e]82 // Action to do after a thread is ran
[ac2b598]83 $thread * destroyer;
[c81ebf9]84
[e60e0dc]85 // Preemption data
[025278e]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;
[c81ebf9]91
[92e7631]92 // Idle lock (kernel semaphore)
93 __bin_sem_t idle;
[85b1deb]94
[92e7631]95 // Termination synchronisation (user semaphore)
[85b1deb]96 semaphore terminated;
[de94a60]97
[27f5f71]98 // pthread Stack
99 void * stack;
100
[de94a60]101 // Link lists fields
[1eb239e4]102 DLISTED_MGD_IMPL_IN(processor)
[14a61b5]103
[c34ebf2]104 #if !defined(__CFA_NO_STATISTICS__)
[69fbc61]105 int print_stats;
[c34ebf2]106 bool print_halts;
107 #endif
108
[e60e0dc]109#ifdef __CFA_DEBUG__
[025278e]110 // Last function to enable preemption on this processor
[cdbfab0]111 const char * last_enable;
[e60e0dc]112#endif
[c84e80a]113};
114
[e3fea42]115void ?{}(processor & this, const char name[], struct cluster & cltr);
[242a902]116void ^?{}(processor & this);
[c84e80a]117
[de6319f]118static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
[de94a60]119static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
[e3fea42]120static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
[de6319f]121
[1eb239e4]122DLISTED_MGD_IMPL_OUT(processor)
[de94a60]123
[92976d9]124//-----------------------------------------------------------------------------
125// I/O
[61dd73d]126struct __io_data;
[92976d9]127
[f00b26d4]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};
[de94a60]152
[f00b26d4]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 {
[c402739f]160 __u64 target;
[f00b26d4]161};
162
163static inline void ?{}(io_cancellation & this) { this.target = -1u; }
164static inline void ^?{}(io_cancellation & this) {}
165bool cancel(io_cancellation & this);
[7768b8d]166
167//-----------------------------------------------------------------------------
168// Cluster Tools
[dca5802]169
170// Intrusives lanes which are used by the relaxed ready queue
[61d7bec]171struct __attribute__((aligned(128))) __intrusive_lane_t;
[dca5802]172void ?{}(__intrusive_lane_t & this);
173void ^?{}(__intrusive_lane_t & this);
[7768b8d]174
[61d7bec]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};
[b798713]182
[61d7bec]183void ?{}( __snzi_t & this, unsigned depth );
184void ^?{}( __snzi_t & this );
[b798713]185
186//TODO adjust cache size to ARCHITECTURE
[dca5802]187// Structure holding the relaxed ready queue
[37ba662]188struct __ready_queue_t {
[dca5802]189 // Data tracking how many/which lanes are used
190 // Aligned to 128 for cache locality
[61d7bec]191 __snzi_t snzi;
[dca5802]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
[37ba662]197 struct {
[dca5802]198 // Arary of lanes
199 __intrusive_lane_t * volatile data;
200
201 // Number of lanes (empty or not)
[b798713]202 volatile size_t count;
[dca5802]203 } lanes;
[b798713]204};
205
206void ?{}(__ready_queue_t & this);
207void ^?{}(__ready_queue_t & this);
208
[1eb239e4]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
[de94a60]224//-----------------------------------------------------------------------------
225// Cluster
[37ba662]226struct __attribute__((aligned(128))) cluster {
[de94a60]227 // Ready queue for threads
[b798713]228 __ready_queue_t ready_queue;
[de94a60]229
230 // Name of the cluster
231 const char * name;
232
233 // Preemption rate on this cluster
234 Duration preemption_rate;
235
[64a7146]236 // List of idle processors
[1eb239e4]237 __cluster_idles idles;
[de94a60]238
[d4e68a6]239 // List of threads
[a1a17a74]240 __spinlock_t thread_list_lock;
[ac2b598]241 __dllist_t(struct $thread) threads;
[d4e68a6]242 unsigned int nthreads;
[a1a17a74]243
[de94a60]244 // Link lists fields
[ea8b2f7]245 struct __dbg_node_cltr {
[de94a60]246 cluster * next;
247 cluster * prev;
248 } node;
[92976d9]249
[f00b26d4]250 struct {
251 io_context * ctxs;
252 unsigned cnt;
253 } io;
[038be32]254
255 #if !defined(__CFA_NO_STATISTICS__)
[8834751]256 struct __stats_t * stats;
[69fbc61]257 int print_stats;
[038be32]258 #endif
[de94a60]259};
260extern Duration default_preemption();
261
[f00b26d4]262void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
[de94a60]263void ^?{}(cluster & this);
264
[f00b26d4]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}; }
[de94a60]277
[c7a900a]278static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]279
[d4e68a6]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
[038be32]283#if !defined(__CFA_NO_STATISTICS__)
[69fbc61]284 static inline void print_stats_at_exit( cluster & this, int flags ) {
285 this.print_stats |= flags;
[038be32]286 }
[c34ebf2]287
[69fbc61]288 static inline void print_stats_at_exit( processor & this, int flags ) {
289 this.print_stats |= flags;
[c34ebf2]290 }
291
292 void print_halts( processor & this );
[038be32]293#endif
294
[8118303]295// Local Variables: //
[6b0b624]296// mode: c //
297// tab-width: 4 //
[8118303]298// End: //
Note: See TracBrowser for help on using the repository browser.