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 | // $monitor.c --
|
---|
8 | //
|
---|
9 | // Author : Thierry Delisle
|
---|
10 | // Created On : Thd Feb 23 12:27:26 2017
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Wed Dec 4 07:55:14 2019
|
---|
13 | // Update Count : 10
|
---|
14 | //
|
---|
15 |
|
---|
16 | #define __cforall_thread__
|
---|
17 |
|
---|
18 | #include "monitor.hfa"
|
---|
19 |
|
---|
20 | #include <stdlib.hfa>
|
---|
21 | #include <inttypes.h>
|
---|
22 |
|
---|
23 | #include "kernel_private.hfa"
|
---|
24 |
|
---|
25 | #include "bits/algorithm.hfa"
|
---|
26 |
|
---|
27 | //-----------------------------------------------------------------------------
|
---|
28 | // Forward declarations
|
---|
29 | static inline void __set_owner ( $monitor * this, $thread * owner );
|
---|
30 | static inline void __set_owner ( $monitor * storage [], __lock_size_t count, $thread * owner );
|
---|
31 | static inline void set_mask ( $monitor * storage [], __lock_size_t count, const __waitfor_mask_t & mask );
|
---|
32 | static inline void reset_mask( $monitor * this );
|
---|
33 |
|
---|
34 | static inline $thread * next_thread( $monitor * this );
|
---|
35 | static inline bool is_accepted( $monitor * this, const __monitor_group_t & monitors );
|
---|
36 |
|
---|
37 | static inline void lock_all ( __spinlock_t * locks [], __lock_size_t count );
|
---|
38 | static inline void lock_all ( $monitor * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count );
|
---|
39 | static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count );
|
---|
40 | static inline void unlock_all( $monitor * locks [], __lock_size_t count );
|
---|
41 |
|
---|
42 | static inline void save ( $monitor * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] );
|
---|
43 | static inline void restore( $monitor * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*in */ recursions [], __waitfor_mask_t /*in */ masks [] );
|
---|
44 |
|
---|
45 | static inline void init ( __lock_size_t count, $monitor * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
|
---|
46 | static inline void init_push( __lock_size_t count, $monitor * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
|
---|
47 |
|
---|
48 | static inline $thread * check_condition ( __condition_criterion_t * );
|
---|
49 | static inline void brand_condition ( condition & );
|
---|
50 | static inline [$thread *, int] search_entry_queue( const __waitfor_mask_t &, $monitor * monitors [], __lock_size_t count );
|
---|
51 |
|
---|
52 | forall(dtype T | sized( T ))
|
---|
53 | static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val );
|
---|
54 | static inline __lock_size_t count_max ( const __waitfor_mask_t & mask );
|
---|
55 | static inline __lock_size_t aggregate ( $monitor * storage [], const __waitfor_mask_t & mask );
|
---|
56 |
|
---|
57 | //-----------------------------------------------------------------------------
|
---|
58 | // Useful defines
|
---|
59 | #define wait_ctx(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
|
---|
60 | __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
|
---|
61 | __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
|
---|
62 | init( count, monitors, waiter, criteria ); /* Link everything together */ \
|
---|
63 |
|
---|
64 | #define wait_ctx_primed(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
|
---|
65 | __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
|
---|
66 | __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
|
---|
67 | init_push( count, monitors, waiter, criteria ); /* Link everything together and push it to the AS-Stack */ \
|
---|
68 |
|
---|
69 | #define monitor_ctx( mons, cnt ) /* Define that create the necessary struct for internal/external scheduling operations */ \
|
---|
70 | $monitor ** monitors = mons; /* Save the targeted monitors */ \
|
---|
71 | __lock_size_t count = cnt; /* Save the count to a local variable */ \
|
---|
72 | unsigned int recursions[ count ]; /* Save the current recursion levels to restore them later */ \
|
---|
73 | __waitfor_mask_t masks [ count ]; /* Save the current waitfor masks to restore them later */ \
|
---|
74 | __spinlock_t * locks [ count ]; /* We need to pass-in an array of locks to BlockInternal */ \
|
---|
75 |
|
---|
76 | #define monitor_save save ( monitors, count, locks, recursions, masks )
|
---|
77 | #define monitor_restore restore( monitors, count, locks, recursions, masks )
|
---|
78 |
|
---|
79 |
|
---|
80 | //-----------------------------------------------------------------------------
|
---|
81 | // Enter/Leave routines
|
---|
82 | // Enter single monitor
|
---|
83 | static void __enter( $monitor * this, const __monitor_group_t & group ) {
|
---|
84 | // Lock the monitor spinlock
|
---|
85 | lock( this->lock __cfaabi_dbg_ctx2 );
|
---|
86 | // Interrupts disable inside critical section
|
---|
87 | $thread * thrd = kernelTLS.this_thread;
|
---|
88 |
|
---|
89 | __cfaabi_dbg_print_safe( "Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
|
---|
90 |
|
---|
91 | if( unlikely(0 != (0x1 & (uintptr_t)this->owner)) ) {
|
---|
92 | abort( "Attempt by thread \"%.256s\" (%p) to access joined monitor %p.", thrd->self_cor.name, thrd, this );
|
---|
93 | }
|
---|
94 | else if( !this->owner ) {
|
---|
95 | // No one has the monitor, just take it
|
---|
96 | __set_owner( this, thrd );
|
---|
97 |
|
---|
98 | __cfaabi_dbg_print_safe( "Kernel : mon is free \n" );
|
---|
99 | }
|
---|
100 | else if( this->owner == thrd) {
|
---|
101 | // We already have the monitor, just note how many times we took it
|
---|
102 | this->recursion += 1;
|
---|
103 |
|
---|
104 | __cfaabi_dbg_print_safe( "Kernel : mon already owned \n" );
|
---|
105 | }
|
---|
106 | else if( is_accepted( this, group) ) {
|
---|
107 | // Some one was waiting for us, enter
|
---|
108 | __set_owner( this, thrd );
|
---|
109 |
|
---|
110 | // Reset mask
|
---|
111 | reset_mask( this );
|
---|
112 |
|
---|
113 | __cfaabi_dbg_print_safe( "Kernel : mon accepts \n" );
|
---|
114 | }
|
---|
115 | else {
|
---|
116 | __cfaabi_dbg_print_safe( "Kernel : blocking \n" );
|
---|
117 |
|
---|
118 | // Some one else has the monitor, wait in line for it
|
---|
119 | /* paranoid */ verify( thrd->link.next == 0p );
|
---|
120 | append( this->entry_queue, thrd );
|
---|
121 | /* paranoid */ verify( thrd->link.next == 1p );
|
---|
122 |
|
---|
123 | unlock( this->lock );
|
---|
124 | park();
|
---|
125 |
|
---|
126 | __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this);
|
---|
127 |
|
---|
128 | /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this);
|
---|
133 |
|
---|
134 | /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
135 | /* paranoid */ verify( this->lock.lock );
|
---|
136 |
|
---|
137 | // Release the lock and leave
|
---|
138 | unlock( this->lock );
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static void __dtor_enter( $monitor * this, fptr_t func, bool join ) {
|
---|
143 | // Lock the monitor spinlock
|
---|
144 | lock( this->lock __cfaabi_dbg_ctx2 );
|
---|
145 | // Interrupts disable inside critical section
|
---|
146 | $thread * thrd = kernelTLS.this_thread;
|
---|
147 |
|
---|
148 | __cfaabi_dbg_print_safe( "Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner);
|
---|
149 |
|
---|
150 |
|
---|
151 | if( !this->owner ) {
|
---|
152 | __cfaabi_dbg_print_safe( "Kernel : Destroying free mon %p\n", this);
|
---|
153 |
|
---|
154 | // No one has the monitor, just take it
|
---|
155 | __set_owner( this, thrd );
|
---|
156 |
|
---|
157 | verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
158 |
|
---|
159 | unlock( this->lock );
|
---|
160 | return;
|
---|
161 | }
|
---|
162 | else if( this->owner == thrd && !join) {
|
---|
163 | // We already have the monitor... but where about to destroy it so the nesting will fail
|
---|
164 | // Abort!
|
---|
165 | abort( "Attempt to destroy monitor %p by thread \"%.256s\" (%p) in nested mutex.", this, thrd->self_cor.name, thrd );
|
---|
166 | }
|
---|
167 | // SKULLDUGGERY: join will act as a dtor so it would normally trigger to above check
|
---|
168 | // to avoid that it sets the owner to the special value thrd | 1p before exiting
|
---|
169 | else if( this->owner == ($thread*)(1 | (uintptr_t)thrd) ) {
|
---|
170 | // restore the owner and just return
|
---|
171 | __cfaabi_dbg_print_safe( "Kernel : Destroying free mon %p\n", this);
|
---|
172 |
|
---|
173 | // No one has the monitor, just take it
|
---|
174 | this->owner = thrd;
|
---|
175 |
|
---|
176 | verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
177 |
|
---|
178 | unlock( this->lock );
|
---|
179 | return;
|
---|
180 | }
|
---|
181 |
|
---|
182 | __lock_size_t count = 1;
|
---|
183 | $monitor ** monitors = &this;
|
---|
184 | __monitor_group_t group = { &this, 1, func };
|
---|
185 | if( is_accepted( this, group) ) {
|
---|
186 | __cfaabi_dbg_print_safe( "Kernel : mon accepts dtor, block and signal it \n" );
|
---|
187 |
|
---|
188 | // Wake the thread that is waiting for this
|
---|
189 | __condition_criterion_t * urgent = pop( this->signal_stack );
|
---|
190 | /* paranoid */ verify( urgent );
|
---|
191 |
|
---|
192 | // Reset mask
|
---|
193 | reset_mask( this );
|
---|
194 |
|
---|
195 | // Create the node specific to this wait operation
|
---|
196 | wait_ctx_primed( thrd, 0 )
|
---|
197 |
|
---|
198 | // Some one else has the monitor, wait for him to finish and then run
|
---|
199 | unlock( this->lock );
|
---|
200 |
|
---|
201 | // Release the next thread
|
---|
202 | /* paranoid */ verifyf( urgent->owner->waiting_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
203 | unpark( urgent->owner->waiting_thread );
|
---|
204 |
|
---|
205 | // Park current thread waiting
|
---|
206 | park();
|
---|
207 |
|
---|
208 | // Some one was waiting for us, enter
|
---|
209 | /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
210 | }
|
---|
211 | else {
|
---|
212 | __cfaabi_dbg_print_safe( "Kernel : blocking \n" );
|
---|
213 |
|
---|
214 | wait_ctx( thrd, 0 )
|
---|
215 | this->dtor_node = &waiter;
|
---|
216 |
|
---|
217 | // Some one else has the monitor, wait in line for it
|
---|
218 | /* paranoid */ verify( thrd->link.next == 0p );
|
---|
219 | append( this->entry_queue, thrd );
|
---|
220 | /* paranoid */ verify( thrd->link.next == 1p );
|
---|
221 | unlock( this->lock );
|
---|
222 |
|
---|
223 | // Park current thread waiting
|
---|
224 | park();
|
---|
225 |
|
---|
226 | /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | __cfaabi_dbg_print_safe( "Kernel : Destroying %p\n", this);
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|
234 | // Leave single monitor
|
---|
235 | void __leave( $monitor * this ) {
|
---|
236 | // Lock the monitor spinlock
|
---|
237 | lock( this->lock __cfaabi_dbg_ctx2 );
|
---|
238 |
|
---|
239 | __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner);
|
---|
240 |
|
---|
241 | /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
242 |
|
---|
243 | // Leaving a recursion level, decrement the counter
|
---|
244 | this->recursion -= 1;
|
---|
245 |
|
---|
246 | // If we haven't left the last level of recursion
|
---|
247 | // it means we don't need to do anything
|
---|
248 | if( this->recursion != 0) {
|
---|
249 | __cfaabi_dbg_print_safe( "Kernel : recursion still %d\n", this->recursion);
|
---|
250 | unlock( this->lock );
|
---|
251 | return;
|
---|
252 | }
|
---|
253 |
|
---|
254 | // Get the next thread, will be null on low contention monitor
|
---|
255 | $thread * new_owner = next_thread( this );
|
---|
256 |
|
---|
257 | // Check the new owner is consistent with who we wake-up
|
---|
258 | // new_owner might be null even if someone owns the monitor when the owner is still waiting for another monitor
|
---|
259 | /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
|
---|
260 |
|
---|
261 | // We can now let other threads in safely
|
---|
262 | unlock( this->lock );
|
---|
263 |
|
---|
264 | //We need to wake-up the thread
|
---|
265 | /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
|
---|
266 | unpark( new_owner );
|
---|
267 | }
|
---|
268 |
|
---|
269 | // Leave single monitor for the last time
|
---|
270 | void __dtor_leave( $monitor * this, bool join ) {
|
---|
271 | __cfaabi_dbg_debug_do(
|
---|
272 | if( TL_GET( this_thread ) != this->owner ) {
|
---|
273 | abort( "Destroyed monitor %p has inconsistent owner, expected %p got %p.\n", this, TL_GET( this_thread ), this->owner);
|
---|
274 | }
|
---|
275 | if( this->recursion != 1 && !join ) {
|
---|
276 | abort( "Destroyed monitor %p has %d outstanding nested calls.\n", this, this->recursion - 1);
|
---|
277 | }
|
---|
278 | )
|
---|
279 |
|
---|
280 | this->owner = ($thread*)(1 | (uintptr_t)this->owner);
|
---|
281 | }
|
---|
282 |
|
---|
283 | extern "C" {
|
---|
284 | // Leave the thread monitor
|
---|
285 | // last routine called by a thread.
|
---|
286 | // Should never return
|
---|
287 | void __cfactx_thrd_leave() {
|
---|
288 | $thread * thrd = TL_GET( this_thread );
|
---|
289 | $monitor * this = &thrd->self_mon;
|
---|
290 |
|
---|
291 | // Lock the monitor now
|
---|
292 | lock( this->lock __cfaabi_dbg_ctx2 );
|
---|
293 |
|
---|
294 | disable_interrupts();
|
---|
295 |
|
---|
296 | thrd->state = Halted;
|
---|
297 |
|
---|
298 | /* paranoid */ verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
|
---|
299 |
|
---|
300 | // Leaving a recursion level, decrement the counter
|
---|
301 | this->recursion -= 1;
|
---|
302 |
|
---|
303 | // If we haven't left the last level of recursion
|
---|
304 | // it must mean there is an error
|
---|
305 | if( this->recursion != 0) { abort( "Thread internal monitor has unbalanced recursion" ); }
|
---|
306 |
|
---|
307 | // Fetch the next thread, can be null
|
---|
308 | $thread * new_owner = next_thread( this );
|
---|
309 |
|
---|
310 | // Release the monitor lock
|
---|
311 | unlock( this->lock );
|
---|
312 |
|
---|
313 | // Unpark the next owner if needed
|
---|
314 | /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
|
---|
315 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
|
---|
316 | /* paranoid */ verify( ! kernelTLS.this_processor->destroyer );
|
---|
317 | /* paranoid */ verify( thrd->state == Halted );
|
---|
318 |
|
---|
319 | kernelTLS.this_processor->destroyer = new_owner;
|
---|
320 |
|
---|
321 | // Leave the thread
|
---|
322 | __leave_thread();
|
---|
323 |
|
---|
324 | // Control flow should never reach here!
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | // Join a thread
|
---|
329 | forall( dtype T | is_thread(T) )
|
---|
330 | T & join( T & this ) {
|
---|
331 | $monitor * m = get_monitor(this);
|
---|
332 | void (*dtor)(T& mutex this) = ^?{};
|
---|
333 | monitor_dtor_guard_t __guard = { &m, (fptr_t)dtor, true };
|
---|
334 | {
|
---|
335 | return this;
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | // Enter multiple monitor
|
---|
340 | // relies on the monitor array being sorted
|
---|
341 | static inline void enter( __monitor_group_t monitors ) {
|
---|
342 | for( __lock_size_t i = 0; i < monitors.size; i++) {
|
---|
343 | __enter( monitors[i], monitors );
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | // Leave multiple monitor
|
---|
348 | // relies on the monitor array being sorted
|
---|
349 | static inline void leave($monitor * monitors [], __lock_size_t count) {
|
---|
350 | for( __lock_size_t i = count - 1; i >= 0; i--) {
|
---|
351 | __leave( monitors[i] );
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | // Ctor for monitor guard
|
---|
356 | // Sorts monitors before entering
|
---|
357 | void ?{}( monitor_guard_t & this, $monitor * m [], __lock_size_t count, fptr_t func ) {
|
---|
358 | $thread * thrd = TL_GET( this_thread );
|
---|
359 |
|
---|
360 | // Store current array
|
---|
361 | this.m = m;
|
---|
362 | this.count = count;
|
---|
363 |
|
---|
364 | // Sort monitors based on address
|
---|
365 | __libcfa_small_sort(this.m, count);
|
---|
366 |
|
---|
367 | // Save previous thread context
|
---|
368 | this.prev = thrd->monitors;
|
---|
369 |
|
---|
370 | // Update thread context (needed for conditions)
|
---|
371 | (thrd->monitors){m, count, func};
|
---|
372 |
|
---|
373 | // __cfaabi_dbg_print_safe( "MGUARD : enter %d\n", count);
|
---|
374 |
|
---|
375 | // Enter the monitors in order
|
---|
376 | __monitor_group_t group = {this.m, this.count, func};
|
---|
377 | enter( group );
|
---|
378 |
|
---|
379 | // __cfaabi_dbg_print_safe( "MGUARD : entered\n" );
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | // Dtor for monitor guard
|
---|
384 | void ^?{}( monitor_guard_t & this ) {
|
---|
385 | // __cfaabi_dbg_print_safe( "MGUARD : leaving %d\n", this.count);
|
---|
386 |
|
---|
387 | // Leave the monitors in order
|
---|
388 | leave( this.m, this.count );
|
---|
389 |
|
---|
390 | // __cfaabi_dbg_print_safe( "MGUARD : left\n" );
|
---|
391 |
|
---|
392 | // Restore thread context
|
---|
393 | TL_GET( this_thread )->monitors = this.prev;
|
---|
394 | }
|
---|
395 |
|
---|
396 | // Ctor for monitor guard
|
---|
397 | // Sorts monitors before entering
|
---|
398 | void ?{}( monitor_dtor_guard_t & this, $monitor * m [], fptr_t func, bool join ) {
|
---|
399 | // optimization
|
---|
400 | $thread * thrd = TL_GET( this_thread );
|
---|
401 |
|
---|
402 | // Store current array
|
---|
403 | this.m = *m;
|
---|
404 |
|
---|
405 | // Save previous thread context
|
---|
406 | this.prev = thrd->monitors;
|
---|
407 |
|
---|
408 | // Save whether we are in a join or not
|
---|
409 | this.join = join;
|
---|
410 |
|
---|
411 | // Update thread context (needed for conditions)
|
---|
412 | (thrd->monitors){m, 1, func};
|
---|
413 |
|
---|
414 | __dtor_enter( this.m, func, join );
|
---|
415 | }
|
---|
416 |
|
---|
417 | // Dtor for monitor guard
|
---|
418 | void ^?{}( monitor_dtor_guard_t & this ) {
|
---|
419 | // Leave the monitors in order
|
---|
420 | __dtor_leave( this.m, this.join );
|
---|
421 |
|
---|
422 | // Restore thread context
|
---|
423 | TL_GET( this_thread )->monitors = this.prev;
|
---|
424 | }
|
---|
425 |
|
---|
426 | //-----------------------------------------------------------------------------
|
---|
427 | // Internal scheduling types
|
---|
428 | void ?{}(__condition_node_t & this, $thread * waiting_thread, __lock_size_t count, uintptr_t user_info ) {
|
---|
429 | this.waiting_thread = waiting_thread;
|
---|
430 | this.count = count;
|
---|
431 | this.next = 0p;
|
---|
432 | this.user_info = user_info;
|
---|
433 | }
|
---|
434 |
|
---|
435 | void ?{}(__condition_criterion_t & this ) with( this ) {
|
---|
436 | ready = false;
|
---|
437 | target = 0p;
|
---|
438 | owner = 0p;
|
---|
439 | next = 0p;
|
---|
440 | }
|
---|
441 |
|
---|
442 | void ?{}(__condition_criterion_t & this, $monitor * target, __condition_node_t & owner ) {
|
---|
443 | this.ready = false;
|
---|
444 | this.target = target;
|
---|
445 | this.owner = &owner;
|
---|
446 | this.next = 0p;
|
---|
447 | }
|
---|
448 |
|
---|
449 | //-----------------------------------------------------------------------------
|
---|
450 | // Internal scheduling
|
---|
451 | void wait( condition & this, uintptr_t user_info = 0 ) {
|
---|
452 | brand_condition( this );
|
---|
453 |
|
---|
454 | // Check that everything is as expected
|
---|
455 | assertf( this.monitors != 0p, "Waiting with no monitors (%p)", this.monitors );
|
---|
456 | verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count );
|
---|
457 | verifyf( this.monitor_count < 32u, "Excessive monitor count (%"PRIiFAST16")", this.monitor_count );
|
---|
458 |
|
---|
459 | // Create storage for monitor context
|
---|
460 | monitor_ctx( this.monitors, this.monitor_count );
|
---|
461 |
|
---|
462 | // Create the node specific to this wait operation
|
---|
463 | wait_ctx( TL_GET( this_thread ), user_info );
|
---|
464 |
|
---|
465 | // Append the current wait operation to the ones already queued on the condition
|
---|
466 | // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
|
---|
467 | /* paranoid */ verify( waiter.next == 0p );
|
---|
468 | append( this.blocked, &waiter );
|
---|
469 | /* paranoid */ verify( waiter.next == 1p );
|
---|
470 |
|
---|
471 | // Lock all monitors (aggregates the locks as well)
|
---|
472 | lock_all( monitors, locks, count );
|
---|
473 |
|
---|
474 | // Find the next thread(s) to run
|
---|
475 | __lock_size_t thread_count = 0;
|
---|
476 | $thread * threads[ count ];
|
---|
477 | __builtin_memset( threads, 0, sizeof( threads ) );
|
---|
478 |
|
---|
479 | // Save monitor states
|
---|
480 | monitor_save;
|
---|
481 |
|
---|
482 | // Remove any duplicate threads
|
---|
483 | for( __lock_size_t i = 0; i < count; i++) {
|
---|
484 | $thread * new_owner = next_thread( monitors[i] );
|
---|
485 | insert_unique( threads, thread_count, new_owner );
|
---|
486 | }
|
---|
487 |
|
---|
488 | // Unlock the locks, we don't need them anymore
|
---|
489 | for(int i = 0; i < count; i++) {
|
---|
490 | unlock( *locks[i] );
|
---|
491 | }
|
---|
492 |
|
---|
493 | // Wake the threads
|
---|
494 | for(int i = 0; i < thread_count; i++) {
|
---|
495 | unpark( threads[i] );
|
---|
496 | }
|
---|
497 |
|
---|
498 | // Everything is ready to go to sleep
|
---|
499 | park();
|
---|
500 |
|
---|
501 | // We are back, restore the owners and recursions
|
---|
502 | monitor_restore;
|
---|
503 | }
|
---|
504 |
|
---|
505 | bool signal( condition & this ) {
|
---|
506 | if( is_empty( this ) ) { return false; }
|
---|
507 |
|
---|
508 | //Check that everything is as expected
|
---|
509 | verify( this.monitors );
|
---|
510 | verify( this.monitor_count != 0 );
|
---|
511 |
|
---|
512 | //Some more checking in debug
|
---|
513 | __cfaabi_dbg_debug_do(
|
---|
514 | $thread * this_thrd = TL_GET( this_thread );
|
---|
515 | if ( this.monitor_count != this_thrd->monitors.size ) {
|
---|
516 | abort( "Signal on condition %p made with different number of monitor(s), expected %zi got %zi", &this, this.monitor_count, this_thrd->monitors.size );
|
---|
517 | }
|
---|
518 |
|
---|
519 | for(int i = 0; i < this.monitor_count; i++) {
|
---|
520 | if ( this.monitors[i] != this_thrd->monitors[i] ) {
|
---|
521 | abort( "Signal on condition %p made with different monitor, expected %p got %p", &this, this.monitors[i], this_thrd->monitors[i] );
|
---|
522 | }
|
---|
523 | }
|
---|
524 | );
|
---|
525 |
|
---|
526 | __lock_size_t count = this.monitor_count;
|
---|
527 |
|
---|
528 | // Lock all monitors
|
---|
529 | lock_all( this.monitors, 0p, count );
|
---|
530 |
|
---|
531 | //Pop the head of the waiting queue
|
---|
532 | __condition_node_t * node = pop_head( this.blocked );
|
---|
533 |
|
---|
534 | //Add the thread to the proper AS stack
|
---|
535 | for(int i = 0; i < count; i++) {
|
---|
536 | __condition_criterion_t * crit = &node->criteria[i];
|
---|
537 | assert( !crit->ready );
|
---|
538 | push( crit->target->signal_stack, crit );
|
---|
539 | }
|
---|
540 |
|
---|
541 | //Release
|
---|
542 | unlock_all( this.monitors, count );
|
---|
543 |
|
---|
544 | return true;
|
---|
545 | }
|
---|
546 |
|
---|
547 | bool signal_block( condition & this ) {
|
---|
548 | if( !this.blocked.head ) { return false; }
|
---|
549 |
|
---|
550 | //Check that everything is as expected
|
---|
551 | verifyf( this.monitors != 0p, "Waiting with no monitors (%p)", this.monitors );
|
---|
552 | verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count );
|
---|
553 |
|
---|
554 | // Create storage for monitor context
|
---|
555 | monitor_ctx( this.monitors, this.monitor_count );
|
---|
556 |
|
---|
557 | // Lock all monitors (aggregates the locks them as well)
|
---|
558 | lock_all( monitors, locks, count );
|
---|
559 |
|
---|
560 |
|
---|
561 | // Create the node specific to this wait operation
|
---|
562 | wait_ctx_primed( kernelTLS.this_thread, 0 )
|
---|
563 |
|
---|
564 | //save contexts
|
---|
565 | monitor_save;
|
---|
566 |
|
---|
567 | //Find the thread to run
|
---|
568 | $thread * signallee = pop_head( this.blocked )->waiting_thread;
|
---|
569 | __set_owner( monitors, count, signallee );
|
---|
570 |
|
---|
571 | __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
|
---|
572 |
|
---|
573 | // unlock all the monitors
|
---|
574 | unlock_all( locks, count );
|
---|
575 |
|
---|
576 | // unpark the thread we signalled
|
---|
577 | unpark( signallee );
|
---|
578 |
|
---|
579 | //Everything is ready to go to sleep
|
---|
580 | park();
|
---|
581 |
|
---|
582 |
|
---|
583 | // WE WOKE UP
|
---|
584 |
|
---|
585 |
|
---|
586 | __cfaabi_dbg_print_buffer_local( "Kernel : signal_block returned\n" );
|
---|
587 |
|
---|
588 | //We are back, restore the masks and recursions
|
---|
589 | monitor_restore;
|
---|
590 |
|
---|
591 | return true;
|
---|
592 | }
|
---|
593 |
|
---|
594 | // Access the user_info of the thread waiting at the front of the queue
|
---|
595 | uintptr_t front( condition & this ) {
|
---|
596 | verifyf( !is_empty(this),
|
---|
597 | "Attempt to access user data on an empty condition.\n"
|
---|
598 | "Possible cause is not checking if the condition is empty before reading stored data."
|
---|
599 | );
|
---|
600 | return ((typeof(this.blocked.head))this.blocked.head)->user_info;
|
---|
601 | }
|
---|
602 |
|
---|
603 | //-----------------------------------------------------------------------------
|
---|
604 | // External scheduling
|
---|
605 | // cases to handle :
|
---|
606 | // - target already there :
|
---|
607 | // block and wake
|
---|
608 | // - dtor already there
|
---|
609 | // put thread on signaller stack
|
---|
610 | // - non-blocking
|
---|
611 | // return else
|
---|
612 | // - timeout
|
---|
613 | // return timeout
|
---|
614 | // - block
|
---|
615 | // setup mask
|
---|
616 | // block
|
---|
617 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) {
|
---|
618 | // This statment doesn't have a contiguous list of monitors...
|
---|
619 | // Create one!
|
---|
620 | __lock_size_t max = count_max( mask );
|
---|
621 | $monitor * mon_storage[max];
|
---|
622 | __builtin_memset( mon_storage, 0, sizeof( mon_storage ) );
|
---|
623 | __lock_size_t actual_count = aggregate( mon_storage, mask );
|
---|
624 |
|
---|
625 | __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %"PRIdFAST16" (s: %"PRIdFAST16", m: %"PRIdFAST16")\n", actual_count, mask.size, (__lock_size_t)max);
|
---|
626 |
|
---|
627 | if(actual_count == 0) return;
|
---|
628 |
|
---|
629 | __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n" );
|
---|
630 |
|
---|
631 | // Create storage for monitor context
|
---|
632 | monitor_ctx( mon_storage, actual_count );
|
---|
633 |
|
---|
634 | // Lock all monitors (aggregates the locks as well)
|
---|
635 | lock_all( monitors, locks, count );
|
---|
636 |
|
---|
637 | {
|
---|
638 | // Check if the entry queue
|
---|
639 | $thread * next; int index;
|
---|
640 | [next, index] = search_entry_queue( mask, monitors, count );
|
---|
641 |
|
---|
642 | if( next ) {
|
---|
643 | *mask.accepted = index;
|
---|
644 | __acceptable_t& accepted = mask[index];
|
---|
645 | if( accepted.is_dtor ) {
|
---|
646 | __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n" );
|
---|
647 | verifyf( accepted.size == 1, "ERROR: Accepted dtor has more than 1 mutex parameter." );
|
---|
648 |
|
---|
649 | $monitor * mon2dtor = accepted[0];
|
---|
650 | verifyf( mon2dtor->dtor_node, "ERROR: Accepted monitor has no dtor_node." );
|
---|
651 |
|
---|
652 | __condition_criterion_t * dtor_crit = mon2dtor->dtor_node->criteria;
|
---|
653 | push( mon2dtor->signal_stack, dtor_crit );
|
---|
654 |
|
---|
655 | unlock_all( locks, count );
|
---|
656 | }
|
---|
657 | else {
|
---|
658 | __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n" );
|
---|
659 |
|
---|
660 | // Create the node specific to this wait operation
|
---|
661 | wait_ctx_primed( kernelTLS.this_thread, 0 );
|
---|
662 |
|
---|
663 | // Save monitor states
|
---|
664 | monitor_save;
|
---|
665 |
|
---|
666 | __cfaabi_dbg_print_buffer_local( "Kernel : baton of %"PRIdFAST16" monitors : ", count );
|
---|
667 | #ifdef __CFA_DEBUG_PRINT__
|
---|
668 | for( int i = 0; i < count; i++) {
|
---|
669 | __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top );
|
---|
670 | }
|
---|
671 | #endif
|
---|
672 | __cfaabi_dbg_print_buffer_local( "\n" );
|
---|
673 |
|
---|
674 | // Set the owners to be the next thread
|
---|
675 | __set_owner( monitors, count, next );
|
---|
676 |
|
---|
677 | // unlock all the monitors
|
---|
678 | unlock_all( locks, count );
|
---|
679 |
|
---|
680 | // unpark the thread we signalled
|
---|
681 | unpark( next );
|
---|
682 |
|
---|
683 | //Everything is ready to go to sleep
|
---|
684 | park();
|
---|
685 |
|
---|
686 | // We are back, restore the owners and recursions
|
---|
687 | monitor_restore;
|
---|
688 |
|
---|
689 | __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n" );
|
---|
690 | }
|
---|
691 |
|
---|
692 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
|
---|
693 | return;
|
---|
694 | }
|
---|
695 | }
|
---|
696 |
|
---|
697 |
|
---|
698 | if( duration == 0 ) {
|
---|
699 | __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n" );
|
---|
700 |
|
---|
701 | unlock_all( locks, count );
|
---|
702 |
|
---|
703 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
|
---|
704 | return;
|
---|
705 | }
|
---|
706 |
|
---|
707 |
|
---|
708 | verifyf( duration < 0, "Timeout on waitfor statments not supported yet." );
|
---|
709 |
|
---|
710 | __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n" );
|
---|
711 |
|
---|
712 | // Create the node specific to this wait operation
|
---|
713 | wait_ctx_primed( kernelTLS.this_thread, 0 );
|
---|
714 |
|
---|
715 | monitor_save;
|
---|
716 | set_mask( monitors, count, mask );
|
---|
717 |
|
---|
718 | for( __lock_size_t i = 0; i < count; i++) {
|
---|
719 | verify( monitors[i]->owner == kernelTLS.this_thread );
|
---|
720 | }
|
---|
721 |
|
---|
722 | // unlock all the monitors
|
---|
723 | unlock_all( locks, count );
|
---|
724 |
|
---|
725 | //Everything is ready to go to sleep
|
---|
726 | park();
|
---|
727 |
|
---|
728 |
|
---|
729 | // WE WOKE UP
|
---|
730 |
|
---|
731 |
|
---|
732 | //We are back, restore the masks and recursions
|
---|
733 | monitor_restore;
|
---|
734 |
|
---|
735 | __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n" );
|
---|
736 |
|
---|
737 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
|
---|
738 | }
|
---|
739 |
|
---|
740 | //-----------------------------------------------------------------------------
|
---|
741 | // Utilities
|
---|
742 |
|
---|
743 | static inline void __set_owner( $monitor * this, $thread * owner ) {
|
---|
744 | /* paranoid */ verify( this->lock.lock );
|
---|
745 |
|
---|
746 | //Pass the monitor appropriately
|
---|
747 | this->owner = owner;
|
---|
748 |
|
---|
749 | //We are passing the monitor to someone else, which means recursion level is not 0
|
---|
750 | this->recursion = owner ? 1 : 0;
|
---|
751 | }
|
---|
752 |
|
---|
753 | static inline void __set_owner( $monitor * monitors [], __lock_size_t count, $thread * owner ) {
|
---|
754 | /* paranoid */ verify ( monitors[0]->lock.lock );
|
---|
755 | /* paranoid */ verifyf( monitors[0]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[0]->owner, monitors[0]->recursion, monitors[0] );
|
---|
756 | monitors[0]->owner = owner;
|
---|
757 | monitors[0]->recursion = 1;
|
---|
758 | for( __lock_size_t i = 1; i < count; i++ ) {
|
---|
759 | /* paranoid */ verify ( monitors[i]->lock.lock );
|
---|
760 | /* paranoid */ verifyf( monitors[i]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[i]->owner, monitors[i]->recursion, monitors[i] );
|
---|
761 | monitors[i]->owner = owner;
|
---|
762 | monitors[i]->recursion = 0;
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | static inline void set_mask( $monitor * storage [], __lock_size_t count, const __waitfor_mask_t & mask ) {
|
---|
767 | for( __lock_size_t i = 0; i < count; i++) {
|
---|
768 | storage[i]->mask = mask;
|
---|
769 | }
|
---|
770 | }
|
---|
771 |
|
---|
772 | static inline void reset_mask( $monitor * this ) {
|
---|
773 | this->mask.accepted = 0p;
|
---|
774 | this->mask.data = 0p;
|
---|
775 | this->mask.size = 0;
|
---|
776 | }
|
---|
777 |
|
---|
778 | static inline $thread * next_thread( $monitor * this ) {
|
---|
779 | //Check the signaller stack
|
---|
780 | __cfaabi_dbg_print_safe( "Kernel : mon %p AS-stack top %p\n", this, this->signal_stack.top);
|
---|
781 | __condition_criterion_t * urgent = pop( this->signal_stack );
|
---|
782 | if( urgent ) {
|
---|
783 | //The signaller stack is not empty,
|
---|
784 | //regardless of if we are ready to baton pass,
|
---|
785 | //we need to set the monitor as in use
|
---|
786 | /* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
787 | __set_owner( this, urgent->owner->waiting_thread );
|
---|
788 |
|
---|
789 | return check_condition( urgent );
|
---|
790 | }
|
---|
791 |
|
---|
792 | // No signaller thread
|
---|
793 | // Get the next thread in the entry_queue
|
---|
794 | $thread * new_owner = pop_head( this->entry_queue );
|
---|
795 | /* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );
|
---|
796 | /* paranoid */ verify( !new_owner || new_owner->link.next == 0p );
|
---|
797 | __set_owner( this, new_owner );
|
---|
798 |
|
---|
799 | return new_owner;
|
---|
800 | }
|
---|
801 |
|
---|
802 | static inline bool is_accepted( $monitor * this, const __monitor_group_t & group ) {
|
---|
803 | __acceptable_t * it = this->mask.data; // Optim
|
---|
804 | __lock_size_t count = this->mask.size;
|
---|
805 |
|
---|
806 | // Check if there are any acceptable functions
|
---|
807 | if( !it ) return false;
|
---|
808 |
|
---|
809 | // If this isn't the first monitor to test this, there is no reason to repeat the test.
|
---|
810 | if( this != group[0] ) return group[0]->mask.accepted >= 0;
|
---|
811 |
|
---|
812 | // For all acceptable functions check if this is the current function.
|
---|
813 | for( __lock_size_t i = 0; i < count; i++, it++ ) {
|
---|
814 | if( *it == group ) {
|
---|
815 | *this->mask.accepted = i;
|
---|
816 | return true;
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | // No function matched
|
---|
821 | return false;
|
---|
822 | }
|
---|
823 |
|
---|
824 | static inline void init( __lock_size_t count, $monitor * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
|
---|
825 | for( __lock_size_t i = 0; i < count; i++) {
|
---|
826 | (criteria[i]){ monitors[i], waiter };
|
---|
827 | }
|
---|
828 |
|
---|
829 | waiter.criteria = criteria;
|
---|
830 | }
|
---|
831 |
|
---|
832 | static inline void init_push( __lock_size_t count, $monitor * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
|
---|
833 | for( __lock_size_t i = 0; i < count; i++) {
|
---|
834 | (criteria[i]){ monitors[i], waiter };
|
---|
835 | __cfaabi_dbg_print_safe( "Kernel : target %p = %p\n", criteria[i].target, &criteria[i] );
|
---|
836 | push( criteria[i].target->signal_stack, &criteria[i] );
|
---|
837 | }
|
---|
838 |
|
---|
839 | waiter.criteria = criteria;
|
---|
840 | }
|
---|
841 |
|
---|
842 | static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
|
---|
843 | for( __lock_size_t i = 0; i < count; i++ ) {
|
---|
844 | lock( *locks[i] __cfaabi_dbg_ctx2 );
|
---|
845 | }
|
---|
846 | }
|
---|
847 |
|
---|
848 | static inline void lock_all( $monitor * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count ) {
|
---|
849 | for( __lock_size_t i = 0; i < count; i++ ) {
|
---|
850 | __spinlock_t * l = &source[i]->lock;
|
---|
851 | lock( *l __cfaabi_dbg_ctx2 );
|
---|
852 | if(locks) locks[i] = l;
|
---|
853 | }
|
---|
854 | }
|
---|
855 |
|
---|
856 | static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ) {
|
---|
857 | for( __lock_size_t i = 0; i < count; i++ ) {
|
---|
858 | unlock( *locks[i] );
|
---|
859 | }
|
---|
860 | }
|
---|
861 |
|
---|
862 | static inline void unlock_all( $monitor * locks [], __lock_size_t count ) {
|
---|
863 | for( __lock_size_t i = 0; i < count; i++ ) {
|
---|
864 | unlock( locks[i]->lock );
|
---|
865 | }
|
---|
866 | }
|
---|
867 |
|
---|
868 | static inline void save(
|
---|
869 | $monitor * ctx [],
|
---|
870 | __lock_size_t count,
|
---|
871 | __attribute((unused)) __spinlock_t * locks [],
|
---|
872 | unsigned int /*out*/ recursions [],
|
---|
873 | __waitfor_mask_t /*out*/ masks []
|
---|
874 | ) {
|
---|
875 | for( __lock_size_t i = 0; i < count; i++ ) {
|
---|
876 | recursions[i] = ctx[i]->recursion;
|
---|
877 | masks[i] = ctx[i]->mask;
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | static inline void restore(
|
---|
882 | $monitor * ctx [],
|
---|
883 | __lock_size_t count,
|
---|
884 | __spinlock_t * locks [],
|
---|
885 | unsigned int /*out*/ recursions [],
|
---|
886 | __waitfor_mask_t /*out*/ masks []
|
---|
887 | ) {
|
---|
888 | lock_all( locks, count );
|
---|
889 | for( __lock_size_t i = 0; i < count; i++ ) {
|
---|
890 | ctx[i]->recursion = recursions[i];
|
---|
891 | ctx[i]->mask = masks[i];
|
---|
892 | }
|
---|
893 | unlock_all( locks, count );
|
---|
894 | }
|
---|
895 |
|
---|
896 | // Function has 2 different behavior
|
---|
897 | // 1 - Marks a monitors as being ready to run
|
---|
898 | // 2 - Checks if all the monitors are ready to run
|
---|
899 | // if so return the thread to run
|
---|
900 | static inline $thread * check_condition( __condition_criterion_t * target ) {
|
---|
901 | __condition_node_t * node = target->owner;
|
---|
902 | unsigned short count = node->count;
|
---|
903 | __condition_criterion_t * criteria = node->criteria;
|
---|
904 |
|
---|
905 | bool ready2run = true;
|
---|
906 |
|
---|
907 | for( int i = 0; i < count; i++ ) {
|
---|
908 |
|
---|
909 | // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target );
|
---|
910 | if( &criteria[i] == target ) {
|
---|
911 | criteria[i].ready = true;
|
---|
912 | // __cfaabi_dbg_print_safe( "True\n" );
|
---|
913 | }
|
---|
914 |
|
---|
915 | ready2run = criteria[i].ready && ready2run;
|
---|
916 | }
|
---|
917 |
|
---|
918 | __cfaabi_dbg_print_safe( "Kernel : Runing %i (%p)\n", ready2run, ready2run ? (thread*)node->waiting_thread : (thread*)0p );
|
---|
919 | return ready2run ? node->waiting_thread : 0p;
|
---|
920 | }
|
---|
921 |
|
---|
922 | static inline void brand_condition( condition & this ) {
|
---|
923 | $thread * thrd = TL_GET( this_thread );
|
---|
924 | if( !this.monitors ) {
|
---|
925 | // __cfaabi_dbg_print_safe( "Branding\n" );
|
---|
926 | assertf( thrd->monitors.data != 0p, "No current monitor to brand condition %p", thrd->monitors.data );
|
---|
927 | this.monitor_count = thrd->monitors.size;
|
---|
928 |
|
---|
929 | this.monitors = ($monitor **)malloc( this.monitor_count * sizeof( *this.monitors ) );
|
---|
930 | for( int i = 0; i < this.monitor_count; i++ ) {
|
---|
931 | this.monitors[i] = thrd->monitors[i];
|
---|
932 | }
|
---|
933 | }
|
---|
934 | }
|
---|
935 |
|
---|
936 | static inline [$thread *, int] search_entry_queue( const __waitfor_mask_t & mask, $monitor * monitors [], __lock_size_t count ) {
|
---|
937 |
|
---|
938 | __queue_t($thread) & entry_queue = monitors[0]->entry_queue;
|
---|
939 |
|
---|
940 | // For each thread in the entry-queue
|
---|
941 | for( $thread ** thrd_it = &entry_queue.head;
|
---|
942 | (*thrd_it) != 1p;
|
---|
943 | thrd_it = &(*thrd_it)->link.next
|
---|
944 | ) {
|
---|
945 | // For each acceptable check if it matches
|
---|
946 | int i = 0;
|
---|
947 | __acceptable_t * end = end (mask);
|
---|
948 | __acceptable_t * begin = begin(mask);
|
---|
949 | for( __acceptable_t * it = begin; it != end; it++, i++ ) {
|
---|
950 | // Check if we have a match
|
---|
951 | if( *it == (*thrd_it)->monitors ) {
|
---|
952 |
|
---|
953 | // If we have a match return it
|
---|
954 | // after removeing it from the entry queue
|
---|
955 | return [remove( entry_queue, thrd_it ), i];
|
---|
956 | }
|
---|
957 | }
|
---|
958 | }
|
---|
959 |
|
---|
960 | return [0, -1];
|
---|
961 | }
|
---|
962 |
|
---|
963 | forall(dtype T | sized( T ))
|
---|
964 | static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val ) {
|
---|
965 | if( !val ) return size;
|
---|
966 |
|
---|
967 | for( __lock_size_t i = 0; i <= size; i++) {
|
---|
968 | if( array[i] == val ) return size;
|
---|
969 | }
|
---|
970 |
|
---|
971 | array[size] = val;
|
---|
972 | size = size + 1;
|
---|
973 | return size;
|
---|
974 | }
|
---|
975 |
|
---|
976 | static inline __lock_size_t count_max( const __waitfor_mask_t & mask ) {
|
---|
977 | __lock_size_t max = 0;
|
---|
978 | for( __lock_size_t i = 0; i < mask.size; i++ ) {
|
---|
979 | __acceptable_t & accepted = mask[i];
|
---|
980 | max += accepted.size;
|
---|
981 | }
|
---|
982 | return max;
|
---|
983 | }
|
---|
984 |
|
---|
985 | static inline __lock_size_t aggregate( $monitor * storage [], const __waitfor_mask_t & mask ) {
|
---|
986 | __lock_size_t size = 0;
|
---|
987 | for( __lock_size_t i = 0; i < mask.size; i++ ) {
|
---|
988 | __acceptable_t & accepted = mask[i];
|
---|
989 | __libcfa_small_sort( accepted.data, accepted.size );
|
---|
990 | for( __lock_size_t j = 0; j < accepted.size; j++) {
|
---|
991 | insert_unique( storage, size, accepted[j] );
|
---|
992 | }
|
---|
993 | }
|
---|
994 | // TODO insertion sort instead of this
|
---|
995 | __libcfa_small_sort( storage, size );
|
---|
996 | return size;
|
---|
997 | }
|
---|
998 |
|
---|
999 | // Local Variables: //
|
---|
1000 | // mode: c //
|
---|
1001 | // tab-width: 4 //
|
---|
1002 | // End: //
|
---|