source: libcfa/src/concurrency/kernel.hfa@ 7eb6eb5

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

Re-arranged and commented low-level headers.
Main goal was for better support of weakso locks that are comming.

  • Property mode set to 100644
File size: 10.2 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
[db6f06a]30//-----------------------------------------------------------------------------
[454f478]31// Underlying Locks
32#ifdef __CFA_WITH_VERIFY__
33 extern bool __cfaabi_dbg_in_kernel();
34#endif
35
36extern "C" {
37 char * strerror(int);
38}
39#define CHECKED(x) { int err = x; if( err != 0 ) abort("KERNEL ERROR: Operation \"" #x "\" return error %d - %s\n", err, strerror(err)); }
40
41struct __bin_sem_t {
42 pthread_mutex_t lock;
43 pthread_cond_t cond;
44 int val;
[9c31349]45};
46
[454f478]47static inline void ?{}(__bin_sem_t & this) with( this ) {
48 // Create the mutex with error checking
49 pthread_mutexattr_t mattr;
50 pthread_mutexattr_init( &mattr );
51 pthread_mutexattr_settype( &mattr, PTHREAD_MUTEX_ERRORCHECK_NP);
52 pthread_mutex_init(&lock, &mattr);
53
54 pthread_cond_init (&cond, (const pthread_condattr_t *)0p); // workaround trac#208: cast should not be required
55 val = 0;
56}
57
58static inline void ^?{}(__bin_sem_t & this) with( this ) {
59 CHECKED( pthread_mutex_destroy(&lock) );
60 CHECKED( pthread_cond_destroy (&cond) );
61}
62
63static inline void wait(__bin_sem_t & this) with( this ) {
64 verify(__cfaabi_dbg_in_kernel());
65 CHECKED( pthread_mutex_lock(&lock) );
66 while(val < 1) {
67 pthread_cond_wait(&cond, &lock);
68 }
69 val -= 1;
70 CHECKED( pthread_mutex_unlock(&lock) );
71}
72
73static inline bool post(__bin_sem_t & this) with( this ) {
74 bool needs_signal = false;
75
76 CHECKED( pthread_mutex_lock(&lock) );
77 if(val < 1) {
78 val += 1;
79 pthread_cond_signal(&cond);
80 needs_signal = true;
81 }
82 CHECKED( pthread_mutex_unlock(&lock) );
83
84 return needs_signal;
85}
86
87#undef CHECKED
[9c31349]88
[db6f06a]89
[bd98b58]90//-----------------------------------------------------------------------------
[de94a60]91// Processor
[de6319f]92extern struct cluster * mainCluster;
[bd98b58]93
[9b1dcc2]94// Processor id, required for scheduling threads
95struct __processor_id_t {
[58d64a4]96 unsigned id:24;
97 bool full_proc:1;
[8834751]98
99 #if !defined(__CFA_NO_STATISTICS__)
100 struct __stats_t * stats;
101 #endif
[9b1dcc2]102};
103
[094476d]104coroutine processorCtx_t {
105 struct processor * proc;
106};
107
[e60e0dc]108// Wrapper around kernel threads
[37ba662]109struct __attribute__((aligned(128))) processor {
[e60e0dc]110 // Main state
[37ba662]111 inline __processor_id_t;
[025278e]112
113 // Cluster from which to get threads
[de94a60]114 struct cluster * cltr;
[025278e]115
[37ba662]116 // Set to true to notify the processor should terminate
117 volatile bool do_terminate;
118
119 // Coroutine ctx who does keeps the state of the processor
120 struct processorCtx_t runner;
121
[de6319f]122 // Name of the processor
123 const char * name;
124
[025278e]125 // Handle to pthreads
126 pthread_t kernel_thread;
[2ac095d]127
[e60e0dc]128 // Preemption data
[025278e]129 // Node which is added in the discrete event simulaiton
130 struct alarm_node_t * preemption_alarm;
131
132 // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
133 bool pending_preemption;
[c81ebf9]134
[92e7631]135 // Idle lock (kernel semaphore)
136 __bin_sem_t idle;
[85b1deb]137
[92e7631]138 // Termination synchronisation (user semaphore)
[454f478]139 oneshot terminated;
[de94a60]140
[27f5f71]141 // pthread Stack
142 void * stack;
143
[de94a60]144 // Link lists fields
[1eb239e4]145 DLISTED_MGD_IMPL_IN(processor)
[14a61b5]146
[c34ebf2]147 #if !defined(__CFA_NO_STATISTICS__)
[69fbc61]148 int print_stats;
[c34ebf2]149 bool print_halts;
150 #endif
151
[e60e0dc]152#ifdef __CFA_DEBUG__
[025278e]153 // Last function to enable preemption on this processor
[cdbfab0]154 const char * last_enable;
[e60e0dc]155#endif
[c84e80a]156};
157
[e3fea42]158void ?{}(processor & this, const char name[], struct cluster & cltr);
[242a902]159void ^?{}(processor & this);
[c84e80a]160
[de6319f]161static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; }
[de94a60]162static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; }
[e3fea42]163static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
[de6319f]164
[1eb239e4]165DLISTED_MGD_IMPL_OUT(processor)
[de94a60]166
[92976d9]167//-----------------------------------------------------------------------------
168// I/O
[61dd73d]169struct __io_data;
[92976d9]170
[f00b26d4]171// IO poller user-thread
172// Not using the "thread" keyword because we want to control
173// more carefully when to start/stop it
174struct $io_ctx_thread {
175 struct __io_data * ring;
176 single_sem sem;
177 volatile bool done;
178 $thread self;
179};
180
181
182struct io_context {
183 $io_ctx_thread thrd;
184};
185
186struct io_context_params {
187 int num_entries;
188 int num_ready;
189 int submit_aff;
190 bool eager_submits:1;
191 bool poller_submits:1;
192 bool poll_submit:1;
193 bool poll_complete:1;
194};
[de94a60]195
[f00b26d4]196void ?{}(io_context_params & this);
197
198void ?{}(io_context & this, struct cluster & cl);
199void ?{}(io_context & this, struct cluster & cl, const io_context_params & params);
200void ^?{}(io_context & this);
201
202struct io_cancellation {
[c402739f]203 __u64 target;
[f00b26d4]204};
205
206static inline void ?{}(io_cancellation & this) { this.target = -1u; }
[6d1790c]207static inline void ^?{}(io_cancellation &) {}
[f00b26d4]208bool cancel(io_cancellation & this);
[7768b8d]209
210//-----------------------------------------------------------------------------
211// Cluster Tools
[dca5802]212
213// Intrusives lanes which are used by the relaxed ready queue
[61d7bec]214struct __attribute__((aligned(128))) __intrusive_lane_t;
[dca5802]215void ?{}(__intrusive_lane_t & this);
216void ^?{}(__intrusive_lane_t & this);
[7768b8d]217
[61d7bec]218// Counter used for wether or not the lanes are all empty
219struct __attribute__((aligned(128))) __snzi_node_t;
220struct __snzi_t {
221 unsigned mask;
222 int root;
223 __snzi_node_t * nodes;
224};
[b798713]225
[61d7bec]226void ?{}( __snzi_t & this, unsigned depth );
227void ^?{}( __snzi_t & this );
[b798713]228
229//TODO adjust cache size to ARCHITECTURE
[dca5802]230// Structure holding the relaxed ready queue
[37ba662]231struct __ready_queue_t {
[dca5802]232 // Data tracking how many/which lanes are used
233 // Aligned to 128 for cache locality
[61d7bec]234 __snzi_t snzi;
[dca5802]235
236 // Data tracking the actual lanes
237 // On a seperate cacheline from the used struct since
238 // used can change on each push/pop but this data
239 // only changes on shrink/grow
[37ba662]240 struct {
[dca5802]241 // Arary of lanes
242 __intrusive_lane_t * volatile data;
243
244 // Number of lanes (empty or not)
[b798713]245 volatile size_t count;
[dca5802]246 } lanes;
[b798713]247};
248
249void ?{}(__ready_queue_t & this);
250void ^?{}(__ready_queue_t & this);
251
[1eb239e4]252// Idle Sleep
253struct __cluster_idles {
254 // Spin lock protecting the queue
255 volatile uint64_t lock;
256
257 // Total number of processors
258 unsigned total;
259
260 // Total number of idle processors
261 unsigned idle;
262
263 // List of idle processors
264 dlist(processor, processor) list;
265};
266
[de94a60]267//-----------------------------------------------------------------------------
268// Cluster
[37ba662]269struct __attribute__((aligned(128))) cluster {
[de94a60]270 // Ready queue for threads
[b798713]271 __ready_queue_t ready_queue;
[de94a60]272
273 // Name of the cluster
274 const char * name;
275
276 // Preemption rate on this cluster
277 Duration preemption_rate;
278
[64a7146]279 // List of idle processors
[1eb239e4]280 __cluster_idles idles;
[de94a60]281
[d4e68a6]282 // List of threads
[a1a17a74]283 __spinlock_t thread_list_lock;
[ac2b598]284 __dllist_t(struct $thread) threads;
[d4e68a6]285 unsigned int nthreads;
[a1a17a74]286
[de94a60]287 // Link lists fields
[ea8b2f7]288 struct __dbg_node_cltr {
[de94a60]289 cluster * next;
290 cluster * prev;
291 } node;
[92976d9]292
[f00b26d4]293 struct {
294 io_context * ctxs;
295 unsigned cnt;
296 } io;
[038be32]297
298 #if !defined(__CFA_NO_STATISTICS__)
[8834751]299 struct __stats_t * stats;
[69fbc61]300 int print_stats;
[038be32]301 #endif
[de94a60]302};
303extern Duration default_preemption();
304
[f00b26d4]305void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
[de94a60]306void ^?{}(cluster & this);
307
[f00b26d4]308static inline void ?{} (cluster & this) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), 1, default_params}; }
309static inline void ?{} (cluster & this, Duration preemption_rate) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, 1, default_params}; }
310static inline void ?{} (cluster & this, const char name[]) { io_context_params default_params; this{name, default_preemption(), 1, default_params}; }
311static inline void ?{} (cluster & this, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), num_io, default_params}; }
312static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, num_io, default_params}; }
313static inline void ?{} (cluster & this, const char name[], unsigned num_io) { io_context_params default_params; this{name, default_preemption(), num_io, default_params}; }
314static inline void ?{} (cluster & this, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), 1, io_params}; }
315static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, 1, io_params}; }
316static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params) { this{name, default_preemption(), 1, io_params}; }
317static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; }
318static 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}; }
319static 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]320
[c7a900a]321static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
[de94a60]322
[8fc652e0]323static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE
324static inline struct cluster * active_cluster () { return publicTLS_get( this_processor )->cltr; }
[d4e68a6]325
[038be32]326#if !defined(__CFA_NO_STATISTICS__)
[8fc652e0]327 void print_stats_now( cluster & this, int flags );
328
[69fbc61]329 static inline void print_stats_at_exit( cluster & this, int flags ) {
330 this.print_stats |= flags;
[038be32]331 }
[c34ebf2]332
[69fbc61]333 static inline void print_stats_at_exit( processor & this, int flags ) {
334 this.print_stats |= flags;
[c34ebf2]335 }
336
337 void print_halts( processor & this );
[038be32]338#endif
339
[8118303]340// Local Variables: //
[6b0b624]341// mode: c //
342// tab-width: 4 //
[8118303]343// End: //
Note: See TracBrowser for help on using the repository browser.