source: libcfa/src/concurrency/kernel.hfa @ 1d17939

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

Added stats macro to reduce typing when using stats
Added new macros for Complete polling and submit polling (not implemented yet)

  • Property mode set to 100644
File size: 6.9 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#define CFA_CLUSTER_IO_POLLER_USER_THREAD    (1 << 0) // 0x01
132#define CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS (1 << 1) // 0x02
133#define CFA_CLUSTER_IO_EAGER_SUBMITS         (1 << 2) // 0x04
134#define CFA_CLUSTER_IO_KERNEL_POLL_SUBMITS   (1 << 3) // 0x08
135#define CFA_CLUSTER_IO_KERNEL_POLL_COMPLETES (1 << 4) // 0x10
136#define CFA_CLUSTER_IO_BUFFLEN_OFFSET        16
137
138
139//-----------------------------------------------------------------------------
140// Cluster Tools
141
142// Intrusives lanes which are used by the relaxed ready queue
143struct __attribute__((aligned(128))) __intrusive_lane_t;
144void  ?{}(__intrusive_lane_t & this);
145void ^?{}(__intrusive_lane_t & this);
146
147// Counter used for wether or not the lanes are all empty
148struct __attribute__((aligned(128))) __snzi_node_t;
149struct __snzi_t {
150        unsigned mask;
151        int root;
152        __snzi_node_t * nodes;
153};
154
155void  ?{}( __snzi_t & this, unsigned depth );
156void ^?{}( __snzi_t & this );
157
158//TODO adjust cache size to ARCHITECTURE
159// Structure holding the relaxed ready queue
160struct __ready_queue_t {
161        // Data tracking how many/which lanes are used
162        // Aligned to 128 for cache locality
163        __snzi_t snzi;
164
165        // Data tracking the actual lanes
166        // On a seperate cacheline from the used struct since
167        // used can change on each push/pop but this data
168        // only changes on shrink/grow
169        struct {
170                // Arary of lanes
171                __intrusive_lane_t * volatile data;
172
173                // Number of lanes (empty or not)
174                volatile size_t count;
175        } lanes;
176};
177
178void  ?{}(__ready_queue_t & this);
179void ^?{}(__ready_queue_t & this);
180
181//-----------------------------------------------------------------------------
182// Cluster
183struct __attribute__((aligned(128))) cluster {
184        // Ready queue for threads
185        __ready_queue_t ready_queue;
186
187        // Name of the cluster
188        const char * name;
189
190        // Preemption rate on this cluster
191        Duration preemption_rate;
192
193        // List of idle processors
194        StackLF(processor) idles;
195        volatile unsigned int nprocessors;
196
197        // List of threads
198        __spinlock_t thread_list_lock;
199        __dllist_t(struct $thread) threads;
200        unsigned int nthreads;
201
202        // Link lists fields
203        struct __dbg_node_cltr {
204                cluster * next;
205                cluster * prev;
206        } node;
207
208        struct __io_data * io;
209
210        #if !defined(__CFA_NO_STATISTICS__)
211                struct __stats_t * stats;
212                int print_stats;
213        #endif
214};
215extern Duration default_preemption();
216
217void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags);
218void ^?{}(cluster & this);
219
220static inline void ?{} (cluster & this)                                           { this{"Anonymous Cluster", default_preemption(), 0}; }
221static inline void ?{} (cluster & this, Duration preemption_rate)                 { this{"Anonymous Cluster", preemption_rate, 0}; }
222static inline void ?{} (cluster & this, const char name[])                        { this{name, default_preemption(), 0}; }
223static inline void ?{} (cluster & this, unsigned flags)                           { this{"Anonymous Cluster", default_preemption(), flags}; }
224static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; }
225static inline void ?{} (cluster & this, const char name[], unsigned flags)        { this{name, default_preemption(), flags}; }
226
227static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; }
228
229static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE
230static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; }
231
232#if !defined(__CFA_NO_STATISTICS__)
233        static inline void print_stats_at_exit( cluster & this, int flags ) {
234                this.print_stats |= flags;
235        }
236
237        static inline void print_stats_at_exit( processor & this, int flags ) {
238                this.print_stats |= flags;
239        }
240
241        void print_halts( processor & this );
242#endif
243
244// Local Variables: //
245// mode: c //
246// tab-width: 4 //
247// End: //
Note: See TracBrowser for help on using the repository browser.