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

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

Changed scheduling API to adapt to non-Processors scheduling threads.

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