source: libcfa/src/concurrency/kernel.hfa@ 357fae8

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 357fae8 was a5e7233, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

C interface now runs worker init routine in dedicated thread.
Also added test for this case.

  • Property mode set to 100644
File size: 8.4 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//
[454f478]7// kernel -- Header containing the core of the kernel API
[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>
[454f478]26 #include <pthread.h>
[c402739f]27 #include <linux/types.h>
[8def349]28}
29
[454f478]30#ifdef __CFA_WITH_VERIFY__
31 extern bool __cfaabi_dbg_in_kernel();
32#endif
33
[78da4ab]34//-----------------------------------------------------------------------------
35// I/O
36struct cluster;
37struct $io_context;
38struct $io_arbiter;
39
40struct io_context_params {
41 int num_entries;
42};
43
44void ?{}(io_context_params & this);
45
[bd98b58]46//-----------------------------------------------------------------------------
[de94a60]47// Processor
[de6319f]48extern struct cluster * mainCluster;
[bd98b58]49
[9b1dcc2]50// Processor id, required for scheduling threads
51struct __processor_id_t {
[58d64a4]52 unsigned id:24;
53 bool full_proc:1;
[8834751]54
55 #if !defined(__CFA_NO_STATISTICS__)
56 struct __stats_t * stats;
57 #endif
[9b1dcc2]58};
59
[094476d]60coroutine processorCtx_t {
61 struct processor * proc;
62};
63
[e60e0dc]64// Wrapper around kernel threads
[37ba662]65struct __attribute__((aligned(128))) processor {
[e60e0dc]66 // Main state
[37ba662]67 inline __processor_id_t;
[025278e]68
69 // Cluster from which to get threads
[de94a60]70 struct cluster * cltr;
[025278e]71
[37ba662]72 // Set to true to notify the processor should terminate
73 volatile bool do_terminate;
74
75 // Coroutine ctx who does keeps the state of the processor
76 struct processorCtx_t runner;
77
[de6319f]78 // Name of the processor
79 const char * name;
80
[025278e]81 // Handle to pthreads
82 pthread_t kernel_thread;
[2ac095d]83
[78da4ab]84 struct {
[dddb3dd0]85 $io_context * ctx;
86 bool pending;
87 bool dirty;
[78da4ab]88 } io;
89
[e60e0dc]90 // Preemption data
[025278e]91 // Node which is added in the discrete event simulaiton
92 struct alarm_node_t * preemption_alarm;
93
94 // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
95 bool pending_preemption;
[c81ebf9]96
[92e7631]97 // Idle lock (kernel semaphore)
[dddb3dd0]98 int idle;
[85b1deb]99
[92e7631]100 // Termination synchronisation (user semaphore)
[454f478]101 oneshot terminated;
[de94a60]102
[27f5f71]103 // pthread Stack
104 void * stack;
105
[de94a60]106 // Link lists fields
[1eb239e4]107 DLISTED_MGD_IMPL_IN(processor)
[14a61b5]108
[a1538cd]109 // special init fields
110 // This is needed for memcached integration
111 // once memcached experiments are done this should probably be removed
112 // it is not a particularly safe scheme as it can make processors less homogeneous
113 struct {
[a5e7233]114 $thread * thrd;
[a1538cd]115 } init;
116
[c34ebf2]117 #if !defined(__CFA_NO_STATISTICS__)
[69fbc61]118 int print_stats;
[c34ebf2]119 bool print_halts;
120 #endif
121
[e60e0dc]122#ifdef __CFA_DEBUG__
[025278e]123 // Last function to enable preemption on this processor
[cdbfab0]124 const char * last_enable;
[e60e0dc]125#endif
[c84e80a]126};
127
[a5e7233]128void ?{}(processor & this, const char name[], struct cluster & cltr);
[242a902]129void ^?{}(processor & this);
[c84e80a]130
[a5e7233]131static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
132static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
133static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster}; }
[de6319f]134
[1eb239e4]135DLISTED_MGD_IMPL_OUT(processor)
[de94a60]136
[7768b8d]137//-----------------------------------------------------------------------------
138// Cluster Tools
[dca5802]139
140// Intrusives lanes which are used by the relaxed ready queue
[61d7bec]141struct __attribute__((aligned(128))) __intrusive_lane_t;
[dca5802]142void ?{}(__intrusive_lane_t & this);
143void ^?{}(__intrusive_lane_t & this);
[7768b8d]144
[61d7bec]145// Counter used for wether or not the lanes are all empty
146struct __attribute__((aligned(128))) __snzi_node_t;
147struct __snzi_t {
148 unsigned mask;
149 int root;
150 __snzi_node_t * nodes;
151};
[b798713]152
[61d7bec]153void ?{}( __snzi_t & this, unsigned depth );
154void ^?{}( __snzi_t & this );
[b798713]155
156//TODO adjust cache size to ARCHITECTURE
[dca5802]157// Structure holding the relaxed ready queue
[37ba662]158struct __ready_queue_t {
[dca5802]159 // Data tracking how many/which lanes are used
160 // Aligned to 128 for cache locality
[61d7bec]161 __snzi_t snzi;
[dca5802]162
163 // Data tracking the actual lanes
164 // On a seperate cacheline from the used struct since
165 // used can change on each push/pop but this data
166 // only changes on shrink/grow
[37ba662]167 struct {
[dca5802]168 // Arary of lanes
169 __intrusive_lane_t * volatile data;
170
171 // Number of lanes (empty or not)
[b798713]172 volatile size_t count;
[dca5802]173 } lanes;
[b798713]174};
175
176void ?{}(__ready_queue_t & this);
177void ^?{}(__ready_queue_t & this);
178
[1eb239e4]179// Idle Sleep
180struct __cluster_idles {
181 // Spin lock protecting the queue
182 volatile uint64_t lock;
183
184 // Total number of processors
185 unsigned total;
186
187 // Total number of idle processors
188 unsigned idle;
189
190 // List of idle processors
191 dlist(processor, processor) list;
192};
193
[de94a60]194//-----------------------------------------------------------------------------
195// Cluster
[37ba662]196struct __attribute__((aligned(128))) cluster {
[de94a60]197 // Ready queue for threads
[b798713]198 __ready_queue_t ready_queue;
[de94a60]199
200 // Name of the cluster
201 const char * name;
202
203 // Preemption rate on this cluster
204 Duration preemption_rate;
205
[64a7146]206 // List of idle processors
[1eb239e4]207 __cluster_idles idles;
[de94a60]208
[d4e68a6]209 // List of threads
[a1a17a74]210 __spinlock_t thread_list_lock;
[ac2b598]211 __dllist_t(struct $thread) threads;
[d4e68a6]212 unsigned int nthreads;
[a1a17a74]213
[de94a60]214 // Link lists fields
[ea8b2f7]215 struct __dbg_node_cltr {
[de94a60]216 cluster * next;
217 cluster * prev;
218 } node;
[92976d9]219
[f00b26d4]220 struct {
[78da4ab]221 $io_arbiter * arbiter;
222 io_context_params params;
[f00b26d4]223 } io;
[038be32]224
225 #if !defined(__CFA_NO_STATISTICS__)
[8834751]226 struct __stats_t * stats;
[69fbc61]227 int print_stats;
[038be32]228 #endif
[de94a60]229};
230extern Duration default_preemption();
231
[f00b26d4]232void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
[de94a60]233void ^?{}(cluster & this);
234
[f00b26d4]235static inline void ?{} (cluster & this) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), 1, default_params}; }
236static inline void ?{} (cluster & this, Duration preemption_rate) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, 1, default_params}; }
237static inline void ?{} (cluster & this, const char name[]) { io_context_params default_params; this{name, default_preemption(), 1, default_params}; }
238static inline void ?{} (cluster & this, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), num_io, default_params}; }
239static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, num_io, default_params}; }
240static inline void ?{} (cluster & this, const char name[], unsigned num_io) { io_context_params default_params; this{name, default_preemption(), num_io, default_params}; }
241static inline void ?{} (cluster & this, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), 1, io_params}; }
242static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, 1, io_params}; }
243static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params) { this{name, default_preemption(), 1, io_params}; }
244static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; }
245static 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}; }
246static 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]247
[c7a900a]248static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]249
[8fc652e0]250static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE
251static inline struct cluster * active_cluster () { return publicTLS_get( this_processor )->cltr; }
[d4e68a6]252
[038be32]253#if !defined(__CFA_NO_STATISTICS__)
[8fc652e0]254 void print_stats_now( cluster & this, int flags );
255
[69fbc61]256 static inline void print_stats_at_exit( cluster & this, int flags ) {
257 this.print_stats |= flags;
[038be32]258 }
[c34ebf2]259
[69fbc61]260 static inline void print_stats_at_exit( processor & this, int flags ) {
261 this.print_stats |= flags;
[c34ebf2]262 }
263
264 void print_halts( processor & this );
[038be32]265#endif
266
[8118303]267// Local Variables: //
[6b0b624]268// mode: c //
269// tab-width: 4 //
[8118303]270// End: //
Note: See TracBrowser for help on using the repository browser.