source: libcfa/src/concurrency/kernel.hfa @ 37ba662

arm-ehjacob/cs343-translationnew-astnew-ast-unique-expr
Last change on this file since 37ba662 was 37ba662, checked in by Thierry Delisle <tdelisle@…>, 16 months ago

Minor improvements to alignments and memory layout

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