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

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

Merge branch 'master' into relaxed_ready

  • Property mode set to 100644
File size: 9.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 {
159        // spin lock protecting the queue
160        volatile bool lock;
161
162        // anchor for the head and the tail of the queue
163        struct __sentinel_t {
164                // Link lists fields
165                // instrusive link field for threads
166                // must be exactly as in $thread
167                __thread_desc_link link;
168        } before, after;
169
170#if defined(__CFA_WITH_VERIFY__)
171        // id of last processor to acquire the lock
172        // needed only to check for mutual exclusion violations
173        unsigned int last_id;
174
175        // number of items on this list
176        // needed only to check for deadlocks
177        unsigned int count;
178#endif
179
180        // Optional statistic counters
181        #if !defined(__CFA_NO_SCHED_STATS__)
182                struct __attribute__((aligned(64))) {
183                        // difference between number of push and pops
184                        ssize_t diff;
185
186                        // total number of pushes and pops
187                        size_t  push;
188                        size_t  pop ;
189                } stat;
190        #endif
191};
192
193void  ?{}(__intrusive_lane_t & this);
194void ^?{}(__intrusive_lane_t & this);
195
196typedef unsigned long long __cfa_readyQ_mask_t;
197
198// enum {
199//      __cfa_ready_queue_mask_size = (64 - sizeof(size_t)) / sizeof(size_t),
200//      __cfa_max_ready_queues = __cfa_ready_queue_mask_size * 8 * sizeof(size_t)
201// };
202
203#define __cfa_lane_mask_size ((64 - sizeof(size_t)) / sizeof(__cfa_readyQ_mask_t))
204#define __cfa_max_lanes (__cfa_lane_mask_size * 8 * sizeof(__cfa_readyQ_mask_t))
205
206//TODO adjust cache size to ARCHITECTURE
207// Structure holding the relaxed ready queue
208struct __attribute__((aligned(128))) __ready_queue_t {
209        // Data tracking how many/which lanes are used
210        // Aligned to 128 for cache locality
211        struct {
212                // number of non-empty lanes
213                volatile size_t count;
214
215                // bit mask, set bits indentify which lanes are non-empty
216                volatile __cfa_readyQ_mask_t mask[ __cfa_lane_mask_size ];
217        } used;
218
219        // Data tracking the actual lanes
220        // On a seperate cacheline from the used struct since
221        // used can change on each push/pop but this data
222        // only changes on shrink/grow
223        struct __attribute__((aligned(64))) {
224                // Arary of lanes
225                __intrusive_lane_t * volatile data;
226
227                // Number of lanes (empty or not)
228                volatile size_t count;
229        } lanes;
230
231        // Statistics
232        #if !defined(__CFA_NO_STATISTICS__)
233                __attribute__((aligned(64))) struct {
234                        struct {
235                                // Push statistic
236                                struct {
237                                        // number of attemps at pushing something
238                                        volatile size_t attempt;
239
240                                        // number of successes at pushing
241                                        volatile size_t success;
242                                } push;
243
244                                // Pop statistic
245                                struct {
246                                        // number of reads of the mask
247                                        // picking an empty __cfa_readyQ_mask_t counts here
248                                        // but not as an attempt
249                                        volatile size_t maskrds;
250
251                                        // number of attemps at poping something
252                                        volatile size_t attempt;
253
254                                        // number of successes at poping
255                                        volatile size_t success;
256                                } pop;
257                        } pick;
258
259                        // stats on the "used" struct of the queue
260                        // tracks average number of queues that are not empty
261                        // when pushing / poping
262                        struct {
263                                volatile size_t value;
264                                volatile size_t count;
265                        } used;
266                } global_stats;
267
268        #endif
269};
270
271void  ?{}(__ready_queue_t & this);
272void ^?{}(__ready_queue_t & this);
273
274//-----------------------------------------------------------------------------
275// Cluster
276struct cluster {
277        // Ready queue locks
278        __clusterRWLock_t ready_lock;
279
280        // Ready queue for threads
281        __ready_queue_t ready_queue;
282
283        // Name of the cluster
284        const char * name;
285
286        // Preemption rate on this cluster
287        Duration preemption_rate;
288
289        // List of processors
290        __spinlock_t idle_lock;
291        __dllist_t(struct processor) procs;
292        __dllist_t(struct processor) idles;
293        unsigned int nprocessors;
294
295        // List of threads
296        __spinlock_t thread_list_lock;
297        __dllist_t(struct $thread) threads;
298        unsigned int nthreads;
299
300        // Link lists fields
301        struct __dbg_node_cltr {
302                cluster * next;
303                cluster * prev;
304        } node;
305
306        struct __io_data * io;
307
308        #if !defined(__CFA_NO_STATISTICS__)
309                bool print_stats;
310        #endif
311};
312extern Duration default_preemption();
313
314void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags);
315void ^?{}(cluster & this);
316
317static inline void ?{} (cluster & this)                                           { this{"Anonymous Cluster", default_preemption(), 0}; }
318static inline void ?{} (cluster & this, Duration preemption_rate)                 { this{"Anonymous Cluster", preemption_rate, 0}; }
319static inline void ?{} (cluster & this, const char name[])                        { this{name, default_preemption(), 0}; }
320static inline void ?{} (cluster & this, unsigned flags)                           { this{"Anonymous Cluster", default_preemption(), flags}; }
321static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
322static inline void ?{} (cluster & this, const char name[], unsigned flags)        { this{name, default_preemption(), flags}; }
323
324static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
325
326static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
327static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
328
329#if !defined(__CFA_NO_STATISTICS__)
330        static inline void print_stats_at_exit( cluster & this ) {
331                this.print_stats = true;
332        }
333#endif
334
335// Local Variables: //
336// mode: c //
337// tab-width: 4 //
338// End: //
Note: See TracBrowser for help on using the repository browser.