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 | |
---|
27 | extern "C" { |
---|
28 | #include <pthread.h> |
---|
29 | #include <semaphore.h> |
---|
30 | } |
---|
31 | |
---|
32 | //----------------------------------------------------------------------------- |
---|
33 | // Locks |
---|
34 | struct semaphore { |
---|
35 | __spinlock_t lock; |
---|
36 | int count; |
---|
37 | __queue_t($thread) waiting; |
---|
38 | }; |
---|
39 | |
---|
40 | void ?{}(semaphore & this, int count = 1); |
---|
41 | void ^?{}(semaphore & this); |
---|
42 | bool P (semaphore & this); |
---|
43 | bool V (semaphore & this); |
---|
44 | bool V (semaphore & this, unsigned count); |
---|
45 | |
---|
46 | |
---|
47 | //----------------------------------------------------------------------------- |
---|
48 | // Processor |
---|
49 | extern struct cluster * mainCluster; |
---|
50 | |
---|
51 | // Processor id, required for scheduling threads |
---|
52 | struct __processor_id_t { |
---|
53 | unsigned id; |
---|
54 | |
---|
55 | #if !defined(__CFA_NO_STATISTICS__) |
---|
56 | struct __stats_t * stats; |
---|
57 | #endif |
---|
58 | }; |
---|
59 | |
---|
60 | coroutine processorCtx_t { |
---|
61 | struct processor * proc; |
---|
62 | }; |
---|
63 | |
---|
64 | // Wrapper around kernel threads |
---|
65 | struct __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 | bool 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 | |
---|
118 | void ?{}(processor & this, const char name[], struct cluster & cltr); |
---|
119 | void ^?{}(processor & this); |
---|
120 | |
---|
121 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
122 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
123 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; } |
---|
124 | |
---|
125 | static inline Link(processor) * ?`next( processor * this ) { return &this->link; } |
---|
126 | |
---|
127 | //----------------------------------------------------------------------------- |
---|
128 | // I/O |
---|
129 | struct __io_data; |
---|
130 | |
---|
131 | #define CFA_CLUSTER_IO_POLLER_USER_THREAD 1 << 0 // 0x1 |
---|
132 | #define CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS 1 << 1 // 0x2 |
---|
133 | // #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 2 // 0x4 |
---|
134 | #define CFA_CLUSTER_IO_BUFFLEN_OFFSET 16 |
---|
135 | |
---|
136 | |
---|
137 | //----------------------------------------------------------------------------- |
---|
138 | // Cluster Tools |
---|
139 | |
---|
140 | // Intrusives lanes which are used by the relaxed ready queue |
---|
141 | struct __attribute__((aligned(128))) __intrusive_lane_t; |
---|
142 | void ?{}(__intrusive_lane_t & this); |
---|
143 | void ^?{}(__intrusive_lane_t & this); |
---|
144 | |
---|
145 | // Counter used for wether or not the lanes are all empty |
---|
146 | struct __attribute__((aligned(128))) __snzi_node_t; |
---|
147 | struct __snzi_t { |
---|
148 | unsigned mask; |
---|
149 | int root; |
---|
150 | __snzi_node_t * nodes; |
---|
151 | }; |
---|
152 | |
---|
153 | void ?{}( __snzi_t & this, unsigned depth ); |
---|
154 | void ^?{}( __snzi_t & this ); |
---|
155 | |
---|
156 | //TODO adjust cache size to ARCHITECTURE |
---|
157 | // Structure holding the relaxed ready queue |
---|
158 | struct __ready_queue_t { |
---|
159 | // Data tracking how many/which lanes are used |
---|
160 | // Aligned to 128 for cache locality |
---|
161 | __snzi_t snzi; |
---|
162 | |
---|
163 | // Data tracking the actual lanes |
---|
164 | // On a seperate cacheline from the used struct since |
---|
165 | // used can change on each push/pop but this data |
---|
166 | // only changes on shrink/grow |
---|
167 | struct { |
---|
168 | // Arary of lanes |
---|
169 | __intrusive_lane_t * volatile data; |
---|
170 | |
---|
171 | // Number of lanes (empty or not) |
---|
172 | volatile size_t count; |
---|
173 | } lanes; |
---|
174 | }; |
---|
175 | |
---|
176 | void ?{}(__ready_queue_t & this); |
---|
177 | void ^?{}(__ready_queue_t & this); |
---|
178 | |
---|
179 | //----------------------------------------------------------------------------- |
---|
180 | // Cluster |
---|
181 | struct __attribute__((aligned(128))) cluster { |
---|
182 | // Ready queue for threads |
---|
183 | __ready_queue_t ready_queue; |
---|
184 | |
---|
185 | // Name of the cluster |
---|
186 | const char * name; |
---|
187 | |
---|
188 | // Preemption rate on this cluster |
---|
189 | Duration preemption_rate; |
---|
190 | |
---|
191 | // List of idle processors |
---|
192 | StackLF(processor) idles; |
---|
193 | volatile unsigned int nprocessors; |
---|
194 | |
---|
195 | // List of threads |
---|
196 | __spinlock_t thread_list_lock; |
---|
197 | __dllist_t(struct $thread) threads; |
---|
198 | unsigned int nthreads; |
---|
199 | |
---|
200 | // Link lists fields |
---|
201 | struct __dbg_node_cltr { |
---|
202 | cluster * next; |
---|
203 | cluster * prev; |
---|
204 | } node; |
---|
205 | |
---|
206 | struct __io_data * io; |
---|
207 | |
---|
208 | #if !defined(__CFA_NO_STATISTICS__) |
---|
209 | bool print_stats; |
---|
210 | struct __stats_t * stats; |
---|
211 | #endif |
---|
212 | }; |
---|
213 | extern Duration default_preemption(); |
---|
214 | |
---|
215 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags); |
---|
216 | void ^?{}(cluster & this); |
---|
217 | |
---|
218 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption(), 0}; } |
---|
219 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate, 0}; } |
---|
220 | static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption(), 0}; } |
---|
221 | static inline void ?{} (cluster & this, unsigned flags) { this{"Anonymous Cluster", default_preemption(), flags}; } |
---|
222 | static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; } |
---|
223 | static inline void ?{} (cluster & this, const char name[], unsigned flags) { this{name, default_preemption(), flags}; } |
---|
224 | |
---|
225 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
226 | |
---|
227 | static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE |
---|
228 | static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; } |
---|
229 | |
---|
230 | #if !defined(__CFA_NO_STATISTICS__) |
---|
231 | static inline void print_stats_at_exit( cluster & this ) { |
---|
232 | this.print_stats = true; |
---|
233 | } |
---|
234 | |
---|
235 | static inline void print_stats_at_exit( processor & this ) { |
---|
236 | this.print_stats = true; |
---|
237 | } |
---|
238 | |
---|
239 | void print_halts( processor & this ); |
---|
240 | #endif |
---|
241 | |
---|
242 | // Local Variables: // |
---|
243 | // mode: c // |
---|
244 | // tab-width: 4 // |
---|
245 | // End: // |
---|