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