source: libcfa/src/concurrency/kernel.hfa@ 3e1a705

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 3e1a705 was a67c5b6, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Processors now have a pointer to their tls.
This means the tls is no longer 100% private.
Use with care.

  • Property mode set to 100644
File size: 8.4 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#ifdef __CFA_WITH_VERIFY__
31 extern bool __cfaabi_dbg_in_kernel();
32#endif
33
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
46//-----------------------------------------------------------------------------
47// Processor
48extern struct cluster * mainCluster;
49
50// Processor id, required for scheduling threads
51
52
53coroutine processorCtx_t {
54 struct processor * proc;
55};
56
57// Wrapper around kernel threads
58struct __attribute__((aligned(128))) processor {
59 // Cluster from which to get threads
60 struct cluster * cltr;
61
62 // Ready Queue state per processor
63 struct {
64 unsigned short its;
65 unsigned short itr;
66 unsigned id;
67 unsigned target;
68 unsigned long long int cutoff;
69 } rdq;
70
71 // Set to true to notify the processor should terminate
72 volatile bool do_terminate;
73
74 // Coroutine ctx who does keeps the state of the processor
75 struct processorCtx_t runner;
76
77 // Name of the processor
78 const char * name;
79
80 // Handle to pthreads
81 pthread_t kernel_thread;
82
83 // Unique id for the processor (not per cluster)
84 unsigned unique_id;
85
86 struct {
87 $io_context * ctx;
88 bool pending;
89 bool dirty;
90 } io;
91
92 // Preemption data
93 // Node which is added in the discrete event simulaiton
94 struct alarm_node_t * preemption_alarm;
95
96 // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
97 bool pending_preemption;
98
99 // Idle lock (kernel semaphore)
100 int idle;
101
102 // Termination synchronisation (user semaphore)
103 oneshot terminated;
104
105 // pthread Stack
106 void * stack;
107
108 // Link lists fields
109 DLISTED_MGD_IMPL_IN(processor)
110
111 // special init fields
112 // This is needed for memcached integration
113 // once memcached experiments are done this should probably be removed
114 // it is not a particularly safe scheme as it can make processors less homogeneous
115 struct {
116 $thread * thrd;
117 } init;
118
119 struct KernelThreadData * local_data;
120
121 #if !defined(__CFA_NO_STATISTICS__)
122 int print_stats;
123 bool print_halts;
124 #endif
125
126#ifdef __CFA_DEBUG__
127 // Last function to enable preemption on this processor
128 const char * last_enable;
129#endif
130};
131
132void ?{}(processor & this, const char name[], struct cluster & cltr);
133void ^?{}(processor & this);
134
135static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
136static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
137static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster}; }
138
139DLISTED_MGD_IMPL_OUT(processor)
140
141//-----------------------------------------------------------------------------
142// Cluster Tools
143
144// Intrusives lanes which are used by the ready queue
145struct __attribute__((aligned(128))) __intrusive_lane_t;
146void ?{}(__intrusive_lane_t & this);
147void ^?{}(__intrusive_lane_t & this);
148
149// Aligned timestamps which are used by the relaxed ready queue
150struct __attribute__((aligned(128))) __timestamp_t;
151void ?{}(__timestamp_t & this);
152void ^?{}(__timestamp_t & this);
153
154//TODO adjust cache size to ARCHITECTURE
155// Structure holding the relaxed ready queue
156struct __ready_queue_t {
157 // Data tracking the actual lanes
158 // On a seperate cacheline from the used struct since
159 // used can change on each push/pop but this data
160 // only changes on shrink/grow
161 struct {
162 // Arary of lanes
163 __intrusive_lane_t * volatile data;
164
165 // Array of times
166 __timestamp_t * volatile tscs;
167
168 // Number of lanes (empty or not)
169 volatile size_t count;
170 } lanes;
171};
172
173void ?{}(__ready_queue_t & this);
174void ^?{}(__ready_queue_t & this);
175
176// Idle Sleep
177struct __cluster_proc_list {
178 // Spin lock protecting the queue
179 volatile uint64_t lock;
180
181 // Total number of processors
182 unsigned total;
183
184 // Total number of idle processors
185 unsigned idle;
186
187 // List of idle processors
188 dlist(processor, processor) idles;
189
190 // List of active processors
191 dlist(processor, processor) actives;
192};
193
194//-----------------------------------------------------------------------------
195// Cluster
196struct __attribute__((aligned(128))) cluster {
197 // Ready queue for threads
198 __ready_queue_t ready_queue;
199
200 // Name of the cluster
201 const char * name;
202
203 // Preemption rate on this cluster
204 Duration preemption_rate;
205
206 // List of idle processors
207 __cluster_proc_list procs;
208
209 // List of threads
210 __spinlock_t thread_list_lock;
211 __dllist_t(struct $thread) threads;
212 unsigned int nthreads;
213
214 // Link lists fields
215 struct __dbg_node_cltr {
216 cluster * next;
217 cluster * prev;
218 } node;
219
220 struct {
221 $io_arbiter * arbiter;
222 io_context_params params;
223 } io;
224
225 #if !defined(__CFA_NO_STATISTICS__)
226 struct __stats_t * stats;
227 int print_stats;
228 #endif
229};
230extern Duration default_preemption();
231
232void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
233void ^?{}(cluster & this);
234
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}; }
247
248static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
249
250static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE
251static inline struct cluster * active_cluster () { return publicTLS_get( this_processor )->cltr; }
252
253#if !defined(__CFA_NO_STATISTICS__)
254 void print_stats_now( cluster & this, int flags );
255
256 static inline void print_stats_at_exit( cluster & this, int flags ) {
257 this.print_stats |= flags;
258 }
259
260 static inline void print_stats_at_exit( processor & this, int flags ) {
261 this.print_stats |= flags;
262 }
263
264 void print_halts( processor & this );
265#endif
266
267// Local Variables: //
268// mode: c //
269// tab-width: 4 //
270// End: //
Note: See TracBrowser for help on using the repository browser.