source: libcfa/src/concurrency/kernel.hfa @ f00b26d4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since f00b26d4 was f00b26d4, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Re-worked IO to use epoll and support multiple io_contexts per cluster.
Also redid how cluster options are handled.
Changed how iofwd calls are passed to support future features and io_contexts rework.

  • Property mode set to 100644
File size: 8.8 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
25#include "containers/stackLockFree.hfa"
26
27extern "C" {
28#include <pthread.h>
29#include <semaphore.h>
30}
31
32//-----------------------------------------------------------------------------
33// Locks
34struct semaphore {
35        __spinlock_t lock;
36        int count;
37        __queue_t($thread) waiting;
38};
39
40void  ?{}(semaphore & this, int count = 1);
41void ^?{}(semaphore & this);
42bool   P (semaphore & this);
43bool   V (semaphore & this);
44bool   V (semaphore & this, unsigned count);
45
46
47//-----------------------------------------------------------------------------
48// Processor
49extern struct cluster * mainCluster;
50
51// Processor id, required for scheduling threads
52struct __processor_id_t {
53        unsigned id;
54
55        #if !defined(__CFA_NO_STATISTICS__)
56                struct __stats_t * stats;
57        #endif
58};
59
60coroutine processorCtx_t {
61        struct processor * proc;
62};
63
64// Wrapper around kernel threads
65struct __attribute__((aligned(128))) processor {
66        // Main state
67        inline __processor_id_t;
68
69        // Cluster from which to get threads
70        struct cluster * cltr;
71
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
78        // Name of the processor
79        const char * name;
80
81        // Handle to pthreads
82        pthread_t kernel_thread;
83
84        // RunThread data
85        // Action to do after a thread is ran
86        $thread * destroyer;
87
88        // Preemption data
89        // Node which is added in the discrete event simulaiton
90        struct alarm_node_t * preemption_alarm;
91
92        // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible
93        bool pending_preemption;
94
95        // Idle lock (kernel semaphore)
96        __bin_sem_t idle;
97
98        // Termination synchronisation (user semaphore)
99        semaphore terminated;
100
101        // pthread Stack
102        void * stack;
103
104        // Link lists fields
105        Link(processor) link;
106
107        #if !defined(__CFA_NO_STATISTICS__)
108                int print_stats;
109                bool print_halts;
110        #endif
111
112#ifdef __CFA_DEBUG__
113        // Last function to enable preemption on this processor
114        const char * last_enable;
115#endif
116};
117
118void  ?{}(processor & this, const char name[], struct cluster & cltr);
119void ^?{}(processor & this);
120
121static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; }
122static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; }
123static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; }
124
125static inline Link(processor) * ?`next( processor * this ) { return &this->link; }
126
127//-----------------------------------------------------------------------------
128// I/O
129struct __io_data;
130
131// IO poller user-thread
132// Not using the "thread" keyword because we want to control
133// more carefully when to start/stop it
134struct $io_ctx_thread {
135        struct __io_data * ring;
136        single_sem sem;
137        volatile bool done;
138        $thread self;
139};
140
141
142struct io_context {
143        $io_ctx_thread thrd;
144};
145
146struct io_context_params {
147        int num_entries;
148        int num_ready;
149        int submit_aff;
150        bool eager_submits:1;
151        bool poller_submits:1;
152        bool poll_submit:1;
153        bool poll_complete:1;
154};
155
156void  ?{}(io_context_params & this);
157
158void  ?{}(io_context & this, struct cluster & cl);
159void  ?{}(io_context & this, struct cluster & cl, const io_context_params & params);
160void ^?{}(io_context & this);
161
162struct io_cancellation {
163        uint32_t target;
164};
165
166static inline void  ?{}(io_cancellation & this) { this.target = -1u; }
167static inline void ^?{}(io_cancellation & this) {}
168bool cancel(io_cancellation & this);
169
170//-----------------------------------------------------------------------------
171// Cluster Tools
172
173// Intrusives lanes which are used by the relaxed ready queue
174struct __attribute__((aligned(128))) __intrusive_lane_t;
175void  ?{}(__intrusive_lane_t & this);
176void ^?{}(__intrusive_lane_t & this);
177
178// Counter used for wether or not the lanes are all empty
179struct __attribute__((aligned(128))) __snzi_node_t;
180struct __snzi_t {
181        unsigned mask;
182        int root;
183        __snzi_node_t * nodes;
184};
185
186void  ?{}( __snzi_t & this, unsigned depth );
187void ^?{}( __snzi_t & this );
188
189//TODO adjust cache size to ARCHITECTURE
190// Structure holding the relaxed ready queue
191struct __ready_queue_t {
192        // Data tracking how many/which lanes are used
193        // Aligned to 128 for cache locality
194        __snzi_t snzi;
195
196        // Data tracking the actual lanes
197        // On a seperate cacheline from the used struct since
198        // used can change on each push/pop but this data
199        // only changes on shrink/grow
200        struct {
201                // Arary of lanes
202                __intrusive_lane_t * volatile data;
203
204                // Number of lanes (empty or not)
205                volatile size_t count;
206        } lanes;
207};
208
209void  ?{}(__ready_queue_t & this);
210void ^?{}(__ready_queue_t & this);
211
212//-----------------------------------------------------------------------------
213// Cluster
214struct __attribute__((aligned(128))) cluster {
215        // Ready queue for threads
216        __ready_queue_t ready_queue;
217
218        // Name of the cluster
219        const char * name;
220
221        // Preemption rate on this cluster
222        Duration preemption_rate;
223
224        // List of idle processors
225        StackLF(processor) idles;
226        volatile unsigned int nprocessors;
227
228        // List of threads
229        __spinlock_t thread_list_lock;
230        __dllist_t(struct $thread) threads;
231        unsigned int nthreads;
232
233        // Link lists fields
234        struct __dbg_node_cltr {
235                cluster * next;
236                cluster * prev;
237        } node;
238
239        struct {
240                io_context * ctxs;
241                unsigned cnt;
242        } io;
243
244        #if !defined(__CFA_NO_STATISTICS__)
245                struct __stats_t * stats;
246                int print_stats;
247        #endif
248};
249extern Duration default_preemption();
250
251void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params);
252void ^?{}(cluster & this);
253
254static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; }
255static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; }
256static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; }
257static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; }
258static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io) { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, num_io, default_params}; }
259static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; }
260static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; }
261static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; }
262static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; }
263static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; }
264static 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}; }
265static inline void ?{} (cluster & this, const char name[], unsigned num_io, const io_context_params & io_params)        { this{name, default_preemption(), num_io, io_params}; }
266
267static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
268
269static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
270static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
271
272#if !defined(__CFA_NO_STATISTICS__)
273        static inline void print_stats_at_exit( cluster & this, int flags ) {
274                this.print_stats |= flags;
275        }
276
277        static inline void print_stats_at_exit( processor & this, int flags ) {
278                this.print_stats |= flags;
279        }
280
281        void print_halts( processor & this );
282#endif
283
284// Local Variables: //
285// mode: c //
286// tab-width: 4 //
287// End: //
Note: See TracBrowser for help on using the repository browser.