source: libcfa/src/concurrency/kernel.hfa@ 4fa44e7

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 4fa44e7 was 504a7dc, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Some fixes after the merge, compiles but still has livelocks

  • 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
[c84e80a]18#include <stdbool.h>
[92976d9]19#include <stdint.h>
[8118303]20
[bd98b58]21#include "invoke.h"
[73abe95]22#include "time_t.hfa"
[d76bd79]23#include "coroutine.hfa"
[bd98b58]24
[8def349]25extern "C" {
26#include <pthread.h>
[6b4cdd3]27#include <semaphore.h>
[8def349]28}
29
[db6f06a]30//-----------------------------------------------------------------------------
31// Locks
[bdeba0b]32struct semaphore {
[ea7d2b0]33 __spinlock_t lock;
[bdeba0b]34 int count;
[ac2b598]35 __queue_t($thread) waiting;
[9c31349]36};
37
[242a902]38void ?{}(semaphore & this, int count = 1);
39void ^?{}(semaphore & this);
[71c8b7e]40bool P (semaphore & this);
[f0ce5f4]41bool V (semaphore & this);
[d384787]42bool V (semaphore & this, unsigned count);
[9c31349]43
[db6f06a]44
[bd98b58]45//-----------------------------------------------------------------------------
[de94a60]46// Processor
[de6319f]47extern struct cluster * mainCluster;
[bd98b58]48
[e60e0dc]49// Processor
[094476d]50coroutine processorCtx_t {
51 struct processor * proc;
52};
53
[e60e0dc]54// Wrapper around kernel threads
[c84e80a]55struct processor {
[e60e0dc]56 // Main state
[025278e]57 // Coroutine ctx who does keeps the state of the processor
[094476d]58 struct processorCtx_t runner;
[025278e]59
60 // Cluster from which to get threads
[de94a60]61 struct cluster * cltr;
[7768b8d]62 unsigned int id;
[025278e]63
[de6319f]64 // Name of the processor
65 const char * name;
66
[025278e]67 // Handle to pthreads
68 pthread_t kernel_thread;
[2ac095d]69
[e60e0dc]70 // RunThread data
[025278e]71 // Action to do after a thread is ran
[ac2b598]72 $thread * destroyer;
[c81ebf9]73
[e60e0dc]74 // Preemption data
[025278e]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;
[c81ebf9]80
[92e7631]81 // Idle lock (kernel semaphore)
82 __bin_sem_t idle;
[85b1deb]83
84 // Termination
85 // Set to true to notify the processor should terminate
86 volatile bool do_terminate;
87
[92e7631]88 // Termination synchronisation (user semaphore)
[85b1deb]89 semaphore terminated;
[de94a60]90
[27f5f71]91 // pthread Stack
92 void * stack;
93
[de94a60]94 // Link lists fields
[504a7dc]95 struct __dbg_node_cltr {
96 processor * next;
97 processor * prev;
[de94a60]98 } node;
[14a61b5]99
[e60e0dc]100#ifdef __CFA_DEBUG__
[025278e]101 // Last function to enable preemption on this processor
[cdbfab0]102 const char * last_enable;
[e60e0dc]103#endif
[c84e80a]104};
105
[e3fea42]106void ?{}(processor & this, const char name[], struct cluster & cltr);
[242a902]107void ^?{}(processor & this);
[c84e80a]108
[de6319f]109static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
[de94a60]110static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
[e3fea42]111static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
[de6319f]112
[c7a900a]113static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]114
[92976d9]115//-----------------------------------------------------------------------------
116// I/O
[61dd73d]117struct __io_data;
[92976d9]118
[b6f2b213]119#define CFA_CLUSTER_IO_POLLER_USER_THREAD 1 << 0
120// #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 1
[de94a60]121
[7768b8d]122
123//-----------------------------------------------------------------------------
124// Cluster Tools
[dca5802]125
126// Cells use by the reader writer lock
127// while not generic it only relies on a opaque pointer
[7768b8d]128struct __processor_id;
129
130// Reader-Writer lock protecting the ready-queue
[dca5802]131// while this lock is mostly generic some aspects
132// have been hard-coded to for the ready-queue for
133// simplicity and performance
[7768b8d]134struct __clusterRWLock_t {
135 // total cachelines allocated
136 unsigned int max;
137
138 // cachelines currently in use
139 volatile unsigned int alloc;
140
141 // cachelines ready to itereate over
142 // (!= to alloc when thread is in second half of doregister)
143 volatile unsigned int ready;
144
145 // writer lock
146 volatile bool lock;
147
148 // data pointer
149 __processor_id * data;
150};
151
152void ?{}(__clusterRWLock_t & this);
153void ^?{}(__clusterRWLock_t & this);
154
[dca5802]155// Intrusives lanes which are used by the relaxed ready queue
156struct __attribute__((aligned(128))) __intrusive_lane_t {
[7768b8d]157 // spin lock protecting the queue
158 volatile bool lock;
159
160 // anchor for the head and the tail of the queue
161 struct __sentinel_t {
[b798713]162 // Link lists fields
163 // instrusive link field for threads
[504a7dc]164 // must be exactly as in $thread
[b798713]165 __thread_desc_link link;
[7768b8d]166 } before, after;
167
[dca5802]168#if defined(__CFA_WITH_VERIFY__)
169 // id of last processor to acquire the lock
170 // needed only to check for mutual exclusion violations
171 unsigned int last_id;
172
173 // number of items on this list
174 // needed only to check for deadlocks
175 unsigned int count;
176#endif
177
[7768b8d]178 // Optional statistic counters
[b798713]179 #if !defined(__CFA_NO_SCHED_STATS__)
[7768b8d]180 struct __attribute__((aligned(64))) {
181 // difference between number of push and pops
182 ssize_t diff;
183
184 // total number of pushes and pops
185 size_t push;
186 size_t pop ;
187 } stat;
188 #endif
189};
190
[dca5802]191void ?{}(__intrusive_lane_t & this);
192void ^?{}(__intrusive_lane_t & this);
[7768b8d]193
[b798713]194typedef unsigned long long __cfa_readyQ_mask_t;
195
196// enum {
197// __cfa_ready_queue_mask_size = (64 - sizeof(size_t)) / sizeof(size_t),
198// __cfa_max_ready_queues = __cfa_ready_queue_mask_size * 8 * sizeof(size_t)
199// };
200
[dca5802]201#define __cfa_lane_mask_size ((64 - sizeof(size_t)) / sizeof(__cfa_readyQ_mask_t))
202#define __cfa_max_lanes (__cfa_lane_mask_size * 8 * sizeof(__cfa_readyQ_mask_t))
[b798713]203
204//TODO adjust cache size to ARCHITECTURE
[dca5802]205// Structure holding the relaxed ready queue
[b798713]206struct __attribute__((aligned(128))) __ready_queue_t {
[dca5802]207 // Data tracking how many/which lanes are used
208 // Aligned to 128 for cache locality
[b798713]209 struct {
[dca5802]210 // number of non-empty lanes
[b798713]211 volatile size_t count;
212
[dca5802]213 // bit mask, set bits indentify which lanes are non-empty
214 volatile __cfa_readyQ_mask_t mask[ __cfa_lane_mask_size ];
215 } used;
216
217 // Data tracking the actual lanes
218 // On a seperate cacheline from the used struct since
219 // used can change on each push/pop but this data
220 // only changes on shrink/grow
[b798713]221 struct __attribute__((aligned(64))) {
[dca5802]222 // Arary of lanes
223 __intrusive_lane_t * volatile data;
224
225 // Number of lanes (empty or not)
[b798713]226 volatile size_t count;
[dca5802]227 } lanes;
[b798713]228
[dca5802]229 // Statistics
[b798713]230 #if !defined(__CFA_NO_STATISTICS__)
231 __attribute__((aligned(64))) struct {
232 struct {
[dca5802]233 // Push statistic
[b798713]234 struct {
[dca5802]235 // number of attemps at pushing something
[b798713]236 volatile size_t attempt;
[dca5802]237
238 // number of successes at pushing
[b798713]239 volatile size_t success;
240 } push;
[dca5802]241
242 // Pop statistic
[b798713]243 struct {
[dca5802]244 // number of reads of the mask
245 // picking an empty __cfa_readyQ_mask_t counts here
246 // but not as an attempt
[b798713]247 volatile size_t maskrds;
[dca5802]248
249 // number of attemps at poping something
[b798713]250 volatile size_t attempt;
[dca5802]251
252 // number of successes at poping
[b798713]253 volatile size_t success;
254 } pop;
255 } pick;
[dca5802]256
257 // stats on the "used" struct of the queue
258 // tracks average number of queues that are not empty
259 // when pushing / poping
[b798713]260 struct {
261 volatile size_t value;
262 volatile size_t count;
[dca5802]263 } used;
[b798713]264 } global_stats;
265
266 #endif
267};
268
269void ?{}(__ready_queue_t & this);
270void ^?{}(__ready_queue_t & this);
271
[de94a60]272//-----------------------------------------------------------------------------
273// Cluster
274struct cluster {
275 // Ready queue locks
[7768b8d]276 __clusterRWLock_t ready_lock;
[de94a60]277
278 // Ready queue for threads
[b798713]279 __ready_queue_t ready_queue;
[de94a60]280
281 // Name of the cluster
282 const char * name;
283
284 // Preemption rate on this cluster
285 Duration preemption_rate;
286
287 // List of processors
[504a7dc]288 __spinlock_t idle_lock;
289 __dllist_t(struct processor) procs;
[de94a60]290 __dllist_t(struct processor) idles;
[504a7dc]291 unsigned int nprocessors;
[de94a60]292
[d4e68a6]293 // List of threads
[a1a17a74]294 __spinlock_t thread_list_lock;
[ac2b598]295 __dllist_t(struct $thread) threads;
[d4e68a6]296 unsigned int nthreads;
[a1a17a74]297
[de94a60]298 // Link lists fields
[ea8b2f7]299 struct __dbg_node_cltr {
[de94a60]300 cluster * next;
301 cluster * prev;
302 } node;
[92976d9]303
[61dd73d]304 struct __io_data * io;
[038be32]305
306 #if !defined(__CFA_NO_STATISTICS__)
307 bool print_stats;
308 #endif
[de94a60]309};
310extern Duration default_preemption();
311
[b6f2b213]312void ?{} (cluster & this, const char name[], Duration preemption_rate, int flags);
[de94a60]313void ^?{}(cluster & this);
314
[b6f2b213]315static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption(), 0}; }
316static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate, 0}; }
317static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption(), 0}; }
318static inline void ?{} (cluster & this, int flags) { this{"Anonymous Cluster", default_preemption(), flags}; }
319static inline void ?{} (cluster & this, Duration preemption_rate, int flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
320static inline void ?{} (cluster & this, const char name[], int flags) { this{name, default_preemption(), flags}; }
[de94a60]321
[c7a900a]322static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]323
[d4e68a6]324static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
325static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; }
326
[038be32]327#if !defined(__CFA_NO_STATISTICS__)
328 static inline void print_stats_at_exit( cluster & this ) {
329 this.print_stats = true;
330 }
331#endif
332
[8118303]333// Local Variables: //
[6b0b624]334// mode: c //
335// tab-width: 4 //
[8118303]336// End: //
Note: See TracBrowser for help on using the repository browser.