source: libcfa/src/concurrency/kernel.hfa@ 61d7bec

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

Replaced the bitmask approached for the ready-queue with a SNZI

  • Property mode set to 100644
File size: 8.1 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 <stdbool.h>
19#include <stdint.h>
20
21#include "invoke.h"
22#include "time_t.hfa"
23#include "coroutine.hfa"
24
25extern "C" {
26#include <pthread.h>
27#include <semaphore.h>
28}
29
30//-----------------------------------------------------------------------------
31// Locks
32struct semaphore {
33 __spinlock_t lock;
34 int count;
35 __queue_t($thread) waiting;
36};
37
38void ?{}(semaphore & this, int count = 1);
39void ^?{}(semaphore & this);
40bool P (semaphore & this);
41bool V (semaphore & this);
42bool V (semaphore & this, unsigned count);
43
44
45//-----------------------------------------------------------------------------
46// Processor
47extern struct cluster * mainCluster;
48
49// Processor
50coroutine processorCtx_t {
51 struct processor * proc;
52};
53
54// Wrapper around kernel threads
55struct processor {
56 // Main state
57 // Coroutine ctx who does keeps the state of the processor
58 struct processorCtx_t runner;
59
60 // Cluster from which to get threads
61 struct cluster * cltr;
62 unsigned int id;
63
64 // Name of the processor
65 const char * name;
66
67 // Handle to pthreads
68 pthread_t kernel_thread;
69
70 // RunThread data
71 // Action to do after a thread is ran
72 $thread * destroyer;
73
74 // Preemption data
75 // Node which is added in the discrete event simulaiton
76 struct alarm_node_t * preemption_alarm;
77
78 // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
79 bool pending_preemption;
80
81 // Idle lock (kernel semaphore)
82 __bin_sem_t idle;
83
84 // Termination
85 // Set to true to notify the processor should terminate
86 volatile bool do_terminate;
87
88 // Termination synchronisation (user semaphore)
89 semaphore terminated;
90
91 // pthread Stack
92 void * stack;
93
94 // Link lists fields
95 struct __dbg_node_cltr {
96 processor * next;
97 processor * prev;
98 } node;
99
100#ifdef __CFA_DEBUG__
101 // Last function to enable preemption on this processor
102 const char * last_enable;
103#endif
104};
105
106void ?{}(processor & this, const char name[], struct cluster & cltr);
107void ^?{}(processor & this);
108
109static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
110static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
111static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
112
113static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; }
114
115//-----------------------------------------------------------------------------
116// I/O
117struct __io_data;
118
119#define CFA_CLUSTER_IO_POLLER_USER_THREAD 1 << 0 // 0x1
120#define CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS 1 << 1 // 0x2
121// #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 2 // 0x4
122#define CFA_CLUSTER_IO_BUFFLEN_OFFSET 16
123
124
125//-----------------------------------------------------------------------------
126// Cluster Tools
127
128// Cells use by the reader writer lock
129// while not generic it only relies on a opaque pointer
130struct __processor_id;
131
132// Reader-Writer lock protecting the ready-queue
133// while this lock is mostly generic some aspects
134// have been hard-coded to for the ready-queue for
135// simplicity and performance
136struct __clusterRWLock_t {
137 // total cachelines allocated
138 unsigned int max;
139
140 // cachelines currently in use
141 volatile unsigned int alloc;
142
143 // cachelines ready to itereate over
144 // (!= to alloc when thread is in second half of doregister)
145 volatile unsigned int ready;
146
147 // writer lock
148 volatile bool lock;
149
150 // data pointer
151 __processor_id * data;
152};
153
154void ?{}(__clusterRWLock_t & this);
155void ^?{}(__clusterRWLock_t & this);
156
157// Intrusives lanes which are used by the relaxed ready queue
158struct __attribute__((aligned(128))) __intrusive_lane_t;
159void ?{}(__intrusive_lane_t & this);
160void ^?{}(__intrusive_lane_t & this);
161
162// Counter used for wether or not the lanes are all empty
163struct __attribute__((aligned(128))) __snzi_node_t;
164struct __snzi_t {
165 unsigned mask;
166 int root;
167 __snzi_node_t * nodes;
168};
169
170void ?{}( __snzi_t & this, unsigned depth );
171void ^?{}( __snzi_t & this );
172
173//TODO adjust cache size to ARCHITECTURE
174// Structure holding the relaxed ready queue
175struct __attribute__((aligned(128))) __ready_queue_t {
176 // Data tracking how many/which lanes are used
177 // Aligned to 128 for cache locality
178 __snzi_t snzi;
179
180 // Data tracking the actual lanes
181 // On a seperate cacheline from the used struct since
182 // used can change on each push/pop but this data
183 // only changes on shrink/grow
184 struct __attribute__((aligned(64))) {
185 // Arary of lanes
186 __intrusive_lane_t * volatile data;
187
188 // Number of lanes (empty or not)
189 volatile size_t count;
190 } lanes;
191
192 // Statistics
193 #if !defined(__CFA_NO_STATISTICS__)
194 struct __attribute__((aligned(64))) {
195 struct {
196 // Push statistic
197 struct {
198 // number of attemps at pushing something
199 volatile size_t attempt;
200
201 // number of successes at pushing
202 volatile size_t success;
203 } push;
204
205 // Pop statistic
206 struct {
207 // number of reads of the mask
208 // picking an empty __cfa_readyQ_mask_t counts here
209 // but not as an attempt
210 volatile size_t maskrds;
211
212 // number of attemps at poping something
213 volatile size_t attempt;
214
215 // number of successes at poping
216 volatile size_t success;
217 } pop;
218 } pick;
219
220 // stats on the "used" struct of the queue
221 // tracks average number of queues that are not empty
222 // when pushing / poping
223 struct {
224 volatile size_t value;
225 volatile size_t count;
226 } used;
227 } global_stats;
228
229 #endif
230};
231
232void ?{}(__ready_queue_t & this);
233void ^?{}(__ready_queue_t & this);
234
235//-----------------------------------------------------------------------------
236// Cluster
237struct cluster {
238 // Ready queue locks
239 __clusterRWLock_t ready_lock;
240
241 // Ready queue for threads
242 __ready_queue_t ready_queue;
243
244 // Name of the cluster
245 const char * name;
246
247 // Preemption rate on this cluster
248 Duration preemption_rate;
249
250 // List of processors
251 __spinlock_t idle_lock;
252 __dllist_t(struct processor) procs;
253 __dllist_t(struct processor) idles;
254 unsigned int nprocessors;
255
256 // List of threads
257 __spinlock_t thread_list_lock;
258 __dllist_t(struct $thread) threads;
259 unsigned int nthreads;
260
261 // Link lists fields
262 struct __dbg_node_cltr {
263 cluster * next;
264 cluster * prev;
265 } node;
266
267 struct __io_data * io;
268
269 #if !defined(__CFA_NO_STATISTICS__)
270 bool print_stats;
271 #endif
272};
273extern Duration default_preemption();
274
275void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags);
276void ^?{}(cluster & this);
277
278static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption(), 0}; }
279static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate, 0}; }
280static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption(), 0}; }
281static inline void ?{} (cluster & this, unsigned flags) { this{"Anonymous Cluster", default_preemption(), flags}; }
282static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
283static inline void ?{} (cluster & this, const char name[], unsigned flags) { this{name, default_preemption(), flags}; }
284
285static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
286
287static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
288static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; }
289
290#if !defined(__CFA_NO_STATISTICS__)
291 static inline void print_stats_at_exit( cluster & this ) {
292 this.print_stats = true;
293 }
294#endif
295
296// Local Variables: //
297// mode: c //
298// tab-width: 4 //
299// End: //
Note: See TracBrowser for help on using the repository browser.