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_desc.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 : Mon Jul 31 14:59:05 2017 |
---|
13 | // Update Count : 3 |
---|
14 | // |
---|
15 | |
---|
16 | #include "monitor" |
---|
17 | |
---|
18 | #include <stdlib> |
---|
19 | |
---|
20 | #include "libhdr.h" |
---|
21 | #include "kernel_private.h" |
---|
22 | |
---|
23 | //----------------------------------------------------------------------------- |
---|
24 | // Forward declarations |
---|
25 | static inline void set_owner ( monitor_desc * this, thread_desc * owner ); |
---|
26 | static inline void set_owner ( monitor_desc ** storage, short count, thread_desc * owner ); |
---|
27 | static inline void set_mask ( monitor_desc ** storage, short count, const __waitfor_mask_t & mask ); |
---|
28 | static inline void reset_mask( monitor_desc * this ); |
---|
29 | |
---|
30 | static inline thread_desc * next_thread( monitor_desc * this ); |
---|
31 | static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors ); |
---|
32 | |
---|
33 | static inline void lock_all( spinlock ** locks, unsigned short count ); |
---|
34 | static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ); |
---|
35 | static inline void unlock_all( spinlock ** locks, unsigned short count ); |
---|
36 | static inline void unlock_all( monitor_desc ** locks, unsigned short count ); |
---|
37 | |
---|
38 | static inline void save ( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ); |
---|
39 | static inline void restore( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*in */ recursions, __waitfor_mask_t * /*in */ masks ); |
---|
40 | |
---|
41 | static inline void init ( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ); |
---|
42 | static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ); |
---|
43 | |
---|
44 | static inline thread_desc * check_condition ( __condition_criterion_t * ); |
---|
45 | static inline void brand_condition ( condition * ); |
---|
46 | static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t &, monitor_desc ** monitors, int count ); |
---|
47 | |
---|
48 | forall(dtype T | sized( T )) |
---|
49 | static inline short insert_unique( T ** array, short & size, T * val ); |
---|
50 | static inline short count_max ( const __waitfor_mask_t & mask ); |
---|
51 | static inline short aggregate ( monitor_desc ** storage, const __waitfor_mask_t & mask ); |
---|
52 | |
---|
53 | //----------------------------------------------------------------------------- |
---|
54 | // Useful defines |
---|
55 | #define wait_ctx(thrd, user_info) /* Create the necessary information to use the signaller stack */ \ |
---|
56 | __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \ |
---|
57 | __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \ |
---|
58 | init( count, monitors, &waiter, criteria ); /* Link everything together */ \ |
---|
59 | |
---|
60 | #define wait_ctx_primed(thrd, user_info) /* Create the necessary information to use the signaller stack */ \ |
---|
61 | __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \ |
---|
62 | __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \ |
---|
63 | init_push( count, monitors, &waiter, criteria ); /* Link everything together and push it to the AS-Stack */ \ |
---|
64 | |
---|
65 | #define monitor_ctx( mons, cnt ) /* Define that create the necessary struct for internal/external scheduling operations */ \ |
---|
66 | monitor_desc ** monitors = mons; /* Save the targeted monitors */ \ |
---|
67 | unsigned short count = cnt; /* Save the count to a local variable */ \ |
---|
68 | unsigned int recursions[ count ]; /* Save the current recursion levels to restore them later */ \ |
---|
69 | __waitfor_mask_t masks[ count ]; /* Save the current waitfor masks to restore them later */ \ |
---|
70 | spinlock * locks [ count ]; /* We need to pass-in an array of locks to BlockInternal */ \ |
---|
71 | |
---|
72 | #define monitor_save save ( monitors, count, locks, recursions, masks ) |
---|
73 | #define monitor_restore restore( monitors, count, locks, recursions, masks ) |
---|
74 | |
---|
75 | |
---|
76 | //----------------------------------------------------------------------------- |
---|
77 | // Enter/Leave routines |
---|
78 | |
---|
79 | |
---|
80 | extern "C" { |
---|
81 | // Enter single monitor |
---|
82 | static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) { |
---|
83 | // Lock the monitor spinlock, lock_yield to reduce contention |
---|
84 | lock_yield( &this->lock DEBUG_CTX2 ); |
---|
85 | thread_desc * thrd = this_thread; |
---|
86 | |
---|
87 | LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner); |
---|
88 | |
---|
89 | if( !this->owner ) { |
---|
90 | // No one has the monitor, just take it |
---|
91 | set_owner( this, thrd ); |
---|
92 | |
---|
93 | LIB_DEBUG_PRINT_SAFE("Kernel : mon is free \n"); |
---|
94 | } |
---|
95 | else if( this->owner == thrd) { |
---|
96 | // We already have the monitor, just not how many times we took it |
---|
97 | verify( this->recursion > 0 ); |
---|
98 | this->recursion += 1; |
---|
99 | |
---|
100 | LIB_DEBUG_PRINT_SAFE("Kernel : mon already owned \n"); |
---|
101 | } |
---|
102 | else if( is_accepted( this, group) ) { |
---|
103 | // Some one was waiting for us, enter |
---|
104 | set_owner( this, thrd ); |
---|
105 | |
---|
106 | // Reset mask |
---|
107 | reset_mask( this ); |
---|
108 | |
---|
109 | LIB_DEBUG_PRINT_SAFE("Kernel : mon accepts \n"); |
---|
110 | } |
---|
111 | else { |
---|
112 | LIB_DEBUG_PRINT_SAFE("Kernel : blocking \n"); |
---|
113 | |
---|
114 | // Some one else has the monitor, wait in line for it |
---|
115 | append( &this->entry_queue, thrd ); |
---|
116 | BlockInternal( &this->lock ); |
---|
117 | |
---|
118 | LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered mon %p\n", thrd, this); |
---|
119 | |
---|
120 | // BlockInternal will unlock spinlock, no need to unlock ourselves |
---|
121 | return; |
---|
122 | } |
---|
123 | |
---|
124 | LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered mon %p\n", thrd, this); |
---|
125 | |
---|
126 | // Release the lock and leave |
---|
127 | unlock( &this->lock ); |
---|
128 | return; |
---|
129 | } |
---|
130 | |
---|
131 | // Leave single monitor |
---|
132 | void __leave_monitor_desc( monitor_desc * this ) { |
---|
133 | // Lock the monitor spinlock, lock_yield to reduce contention |
---|
134 | lock_yield( &this->lock DEBUG_CTX2 ); |
---|
135 | |
---|
136 | LIB_DEBUG_PRINT_SAFE("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner); |
---|
137 | |
---|
138 | verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", this_thread, this->owner, this->recursion, this ); |
---|
139 | |
---|
140 | // Leaving a recursion level, decrement the counter |
---|
141 | this->recursion -= 1; |
---|
142 | |
---|
143 | // If we haven't left the last level of recursion |
---|
144 | // it means we don't need to do anything |
---|
145 | if( this->recursion != 0) { |
---|
146 | unlock( &this->lock ); |
---|
147 | return; |
---|
148 | } |
---|
149 | |
---|
150 | // Get the next thread, will be null on low contention monitor |
---|
151 | thread_desc * new_owner = next_thread( this ); |
---|
152 | |
---|
153 | // We can now let other threads in safely |
---|
154 | unlock( &this->lock ); |
---|
155 | |
---|
156 | //We need to wake-up the thread |
---|
157 | WakeThread( new_owner ); |
---|
158 | } |
---|
159 | |
---|
160 | // Leave the thread monitor |
---|
161 | // last routine called by a thread. |
---|
162 | // Should never return |
---|
163 | void __leave_thread_monitor( thread_desc * thrd ) { |
---|
164 | monitor_desc * this = &thrd->self_mon; |
---|
165 | |
---|
166 | // Lock the monitor now |
---|
167 | lock_yield( &this->lock DEBUG_CTX2 ); |
---|
168 | |
---|
169 | disable_interrupts(); |
---|
170 | |
---|
171 | thrd->self_cor.state = Halted; |
---|
172 | |
---|
173 | verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this ); |
---|
174 | |
---|
175 | // Leaving a recursion level, decrement the counter |
---|
176 | this->recursion -= 1; |
---|
177 | |
---|
178 | // If we haven't left the last level of recursion |
---|
179 | // it must mean there is an error |
---|
180 | if( this->recursion != 0) { abortf("Thread internal monitor has unbalanced recursion"); } |
---|
181 | |
---|
182 | // Fetch the next thread, can be null |
---|
183 | thread_desc * new_owner = next_thread( this ); |
---|
184 | |
---|
185 | // Leave the thread, this will unlock the spinlock |
---|
186 | // Use leave thread instead of BlockInternal which is |
---|
187 | // specialized for this case and supports null new_owner |
---|
188 | LeaveThread( &this->lock, new_owner ); |
---|
189 | |
---|
190 | // Control flow should never reach here! |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | // Enter multiple monitor |
---|
195 | // relies on the monitor array being sorted |
---|
196 | static inline void enter( __monitor_group_t monitors ) { |
---|
197 | for(int i = 0; i < monitors.size; i++) { |
---|
198 | __enter_monitor_desc( monitors.list[i], monitors ); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | // Leave multiple monitor |
---|
203 | // relies on the monitor array being sorted |
---|
204 | static inline void leave(monitor_desc ** monitors, int count) { |
---|
205 | for(int i = count - 1; i >= 0; i--) { |
---|
206 | __leave_monitor_desc( monitors[i] ); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | // Ctor for monitor guard |
---|
211 | // Sorts monitors before entering |
---|
212 | void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() ) { |
---|
213 | // Store current array |
---|
214 | this.m = m; |
---|
215 | this.count = count; |
---|
216 | |
---|
217 | // Sort monitors based on address -> TODO use a sort specialized for small numbers |
---|
218 | qsort(this.m, count); |
---|
219 | |
---|
220 | // Save previous thread context |
---|
221 | this.prev_mntrs = this_thread->monitors.list; |
---|
222 | this.prev_count = this_thread->monitors.size; |
---|
223 | this.prev_func = this_thread->monitors.func; |
---|
224 | |
---|
225 | // Update thread context (needed for conditions) |
---|
226 | this_thread->monitors.list = m; |
---|
227 | this_thread->monitors.size = count; |
---|
228 | this_thread->monitors.func = func; |
---|
229 | |
---|
230 | // LIB_DEBUG_PRINT_SAFE("MGUARD : enter %d\n", count); |
---|
231 | |
---|
232 | // Enter the monitors in order |
---|
233 | __monitor_group_t group = {this.m, this.count, func}; |
---|
234 | enter( group ); |
---|
235 | |
---|
236 | // LIB_DEBUG_PRINT_SAFE("MGUARD : entered\n"); |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | // Dtor for monitor guard |
---|
241 | void ^?{}( monitor_guard_t & this ) { |
---|
242 | // LIB_DEBUG_PRINT_SAFE("MGUARD : leaving %d\n", this.count); |
---|
243 | |
---|
244 | // Leave the monitors in order |
---|
245 | leave( this.m, this.count ); |
---|
246 | |
---|
247 | // LIB_DEBUG_PRINT_SAFE("MGUARD : left\n"); |
---|
248 | |
---|
249 | // Restore thread context |
---|
250 | this_thread->monitors.list = this.prev_mntrs; |
---|
251 | this_thread->monitors.size = this.prev_count; |
---|
252 | this_thread->monitors.func = this.prev_func; |
---|
253 | } |
---|
254 | |
---|
255 | //----------------------------------------------------------------------------- |
---|
256 | // Internal scheduling types |
---|
257 | void ?{}(__condition_node_t & this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) { |
---|
258 | this.waiting_thread = waiting_thread; |
---|
259 | this.count = count; |
---|
260 | this.next = NULL; |
---|
261 | this.user_info = user_info; |
---|
262 | } |
---|
263 | |
---|
264 | void ?{}(__condition_criterion_t & this ) { |
---|
265 | this.ready = false; |
---|
266 | this.target = NULL; |
---|
267 | this.owner = NULL; |
---|
268 | this.next = NULL; |
---|
269 | } |
---|
270 | |
---|
271 | void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner ) { |
---|
272 | this.ready = false; |
---|
273 | this.target = target; |
---|
274 | this.owner = owner; |
---|
275 | this.next = NULL; |
---|
276 | } |
---|
277 | |
---|
278 | //----------------------------------------------------------------------------- |
---|
279 | // Internal scheduling |
---|
280 | void wait( condition * this, uintptr_t user_info = 0 ) { |
---|
281 | brand_condition( this ); |
---|
282 | |
---|
283 | // Check that everything is as expected |
---|
284 | assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors ); |
---|
285 | verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count ); |
---|
286 | verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count ); |
---|
287 | |
---|
288 | // Create storage for monitor context |
---|
289 | monitor_ctx( this->monitors, this->monitor_count ); |
---|
290 | |
---|
291 | // Create the node specific to this wait operation |
---|
292 | wait_ctx( this_thread, user_info ); |
---|
293 | |
---|
294 | // Append the current wait operation to the ones already queued on the condition |
---|
295 | // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion |
---|
296 | append( &this->blocked, &waiter ); |
---|
297 | |
---|
298 | // Lock all monitors (aggregates the locks as well) |
---|
299 | lock_all( monitors, locks, count ); |
---|
300 | |
---|
301 | // Find the next thread(s) to run |
---|
302 | short thread_count = 0; |
---|
303 | thread_desc * threads[ count ]; |
---|
304 | for(int i = 0; i < count; i++) { |
---|
305 | threads[i] = 0; |
---|
306 | } |
---|
307 | |
---|
308 | // Save monitor states |
---|
309 | monitor_save; |
---|
310 | |
---|
311 | // Remove any duplicate threads |
---|
312 | for( int i = 0; i < count; i++) { |
---|
313 | thread_desc * new_owner = next_thread( monitors[i] ); |
---|
314 | insert_unique( threads, thread_count, new_owner ); |
---|
315 | } |
---|
316 | |
---|
317 | // Everything is ready to go to sleep |
---|
318 | BlockInternal( locks, count, threads, thread_count ); |
---|
319 | |
---|
320 | // We are back, restore the owners and recursions |
---|
321 | monitor_restore; |
---|
322 | } |
---|
323 | |
---|
324 | bool signal( condition * this ) { |
---|
325 | if( is_empty( this ) ) { return false; } |
---|
326 | |
---|
327 | //Check that everything is as expected |
---|
328 | verify( this->monitors ); |
---|
329 | verify( this->monitor_count != 0 ); |
---|
330 | |
---|
331 | //Some more checking in debug |
---|
332 | LIB_DEBUG_DO( |
---|
333 | thread_desc * this_thrd = this_thread; |
---|
334 | if ( this->monitor_count != this_thrd->monitors.size ) { |
---|
335 | abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->monitors.size ); |
---|
336 | } |
---|
337 | |
---|
338 | for(int i = 0; i < this->monitor_count; i++) { |
---|
339 | if ( this->monitors[i] != this_thrd->monitors.list[i] ) { |
---|
340 | abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->monitors.list[i] ); |
---|
341 | } |
---|
342 | } |
---|
343 | ); |
---|
344 | |
---|
345 | unsigned short count = this->monitor_count; |
---|
346 | |
---|
347 | // Lock all monitors |
---|
348 | lock_all( this->monitors, NULL, count ); |
---|
349 | |
---|
350 | //Pop the head of the waiting queue |
---|
351 | __condition_node_t * node = pop_head( &this->blocked ); |
---|
352 | |
---|
353 | //Add the thread to the proper AS stack |
---|
354 | for(int i = 0; i < count; i++) { |
---|
355 | __condition_criterion_t * crit = &node->criteria[i]; |
---|
356 | assert( !crit->ready ); |
---|
357 | push( &crit->target->signal_stack, crit ); |
---|
358 | } |
---|
359 | |
---|
360 | //Release |
---|
361 | unlock_all( this->monitors, count ); |
---|
362 | |
---|
363 | return true; |
---|
364 | } |
---|
365 | |
---|
366 | bool signal_block( condition * this ) { |
---|
367 | if( !this->blocked.head ) { return false; } |
---|
368 | |
---|
369 | //Check that everything is as expected |
---|
370 | verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors ); |
---|
371 | verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count ); |
---|
372 | |
---|
373 | // Create storage for monitor context |
---|
374 | monitor_ctx( this->monitors, this->monitor_count ); |
---|
375 | |
---|
376 | // Lock all monitors (aggregates the locks them as well) |
---|
377 | lock_all( monitors, locks, count ); |
---|
378 | |
---|
379 | // Create the node specific to this wait operation |
---|
380 | wait_ctx_primed( this_thread, 0 ) |
---|
381 | |
---|
382 | //save contexts |
---|
383 | monitor_save; |
---|
384 | |
---|
385 | //Find the thread to run |
---|
386 | thread_desc * signallee = pop_head( &this->blocked )->waiting_thread; |
---|
387 | set_owner( monitors, count, signallee ); |
---|
388 | |
---|
389 | //Everything is ready to go to sleep |
---|
390 | BlockInternal( locks, count, &signallee, 1 ); |
---|
391 | |
---|
392 | |
---|
393 | // WE WOKE UP |
---|
394 | |
---|
395 | |
---|
396 | //We are back, restore the masks and recursions |
---|
397 | monitor_restore; |
---|
398 | |
---|
399 | return true; |
---|
400 | } |
---|
401 | |
---|
402 | // Access the user_info of the thread waiting at the front of the queue |
---|
403 | uintptr_t front( condition * this ) { |
---|
404 | verifyf( !is_empty(this), |
---|
405 | "Attempt to access user data on an empty condition.\n" |
---|
406 | "Possible cause is not checking if the condition is empty before reading stored data." |
---|
407 | ); |
---|
408 | return this->blocked.head->user_info; |
---|
409 | } |
---|
410 | |
---|
411 | //----------------------------------------------------------------------------- |
---|
412 | // External scheduling |
---|
413 | // cases to handle : |
---|
414 | // - target already there : |
---|
415 | // block and wake |
---|
416 | // - dtor already there |
---|
417 | // put thread on signaller stack |
---|
418 | // - non-blocking |
---|
419 | // return else |
---|
420 | // - timeout |
---|
421 | // return timeout |
---|
422 | // - block |
---|
423 | // setup mask |
---|
424 | // block |
---|
425 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) { |
---|
426 | // This statment doesn't have a contiguous list of monitors... |
---|
427 | // Create one! |
---|
428 | short max = count_max( mask ); |
---|
429 | monitor_desc * mon_storage[max]; |
---|
430 | short actual_count = aggregate( mon_storage, mask ); |
---|
431 | |
---|
432 | if(actual_count == 0) return; |
---|
433 | |
---|
434 | LIB_DEBUG_PRINT_SAFE("Kernel : waitfor internal proceeding\n"); |
---|
435 | |
---|
436 | // Create storage for monitor context |
---|
437 | monitor_ctx( mon_storage, actual_count ); |
---|
438 | |
---|
439 | // Lock all monitors (aggregates the locks as well) |
---|
440 | lock_all( monitors, locks, count ); |
---|
441 | |
---|
442 | { |
---|
443 | // Check if the entry queue |
---|
444 | thread_desc * next; int index; |
---|
445 | [next, index] = search_entry_queue( mask, monitors, count ); |
---|
446 | |
---|
447 | if( next ) { |
---|
448 | *mask.accepted = index; |
---|
449 | if( mask.clauses[index].is_dtor ) { |
---|
450 | #warning case not implemented |
---|
451 | } |
---|
452 | else { |
---|
453 | LIB_DEBUG_PRINT_SAFE("Kernel : thread present, baton-passing\n"); |
---|
454 | |
---|
455 | // Create the node specific to this wait operation |
---|
456 | wait_ctx_primed( this_thread, 0 ); |
---|
457 | |
---|
458 | // Save monitor states |
---|
459 | monitor_save; |
---|
460 | |
---|
461 | // Set the owners to be the next thread |
---|
462 | set_owner( monitors, count, next ); |
---|
463 | |
---|
464 | // Everything is ready to go to sleep |
---|
465 | BlockInternal( locks, count, &next, 1 ); |
---|
466 | |
---|
467 | // We are back, restore the owners and recursions |
---|
468 | monitor_restore; |
---|
469 | |
---|
470 | LIB_DEBUG_PRINT_SAFE("Kernel : thread present, returned\n"); |
---|
471 | } |
---|
472 | |
---|
473 | LIB_DEBUG_PRINT_SAFE("Kernel : accepted %d\n", *mask.accepted); |
---|
474 | |
---|
475 | return; |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | |
---|
480 | if( duration == 0 ) { |
---|
481 | LIB_DEBUG_PRINT_SAFE("Kernel : non-blocking, exiting\n"); |
---|
482 | |
---|
483 | unlock_all( locks, count ); |
---|
484 | |
---|
485 | LIB_DEBUG_PRINT_SAFE("Kernel : accepted %d\n", *mask.accepted); |
---|
486 | return; |
---|
487 | } |
---|
488 | |
---|
489 | |
---|
490 | verifyf( duration < 0, "Timeout on waitfor statments not supported yet."); |
---|
491 | |
---|
492 | LIB_DEBUG_PRINT_SAFE("Kernel : blocking waitfor\n"); |
---|
493 | |
---|
494 | // Create the node specific to this wait operation |
---|
495 | wait_ctx_primed( this_thread, 0 ); |
---|
496 | |
---|
497 | monitor_save; |
---|
498 | set_mask( monitors, count, mask ); |
---|
499 | |
---|
500 | for(int i = 0; i < count; i++) { |
---|
501 | verify( monitors[i]->owner == this_thread ); |
---|
502 | } |
---|
503 | |
---|
504 | //Everything is ready to go to sleep |
---|
505 | BlockInternal( locks, count ); |
---|
506 | |
---|
507 | |
---|
508 | // WE WOKE UP |
---|
509 | |
---|
510 | |
---|
511 | //We are back, restore the masks and recursions |
---|
512 | monitor_restore; |
---|
513 | |
---|
514 | LIB_DEBUG_PRINT_SAFE("Kernel : exiting\n"); |
---|
515 | |
---|
516 | LIB_DEBUG_PRINT_SAFE("Kernel : accepted %d\n", *mask.accepted); |
---|
517 | } |
---|
518 | |
---|
519 | //----------------------------------------------------------------------------- |
---|
520 | // Utilities |
---|
521 | |
---|
522 | static inline void set_owner( monitor_desc * this, thread_desc * owner ) { |
---|
523 | // LIB_DEBUG_PRINT_SAFE("Kernal : Setting owner of %p to %p ( was %p)\n", this, owner, this->owner ); |
---|
524 | |
---|
525 | //Pass the monitor appropriately |
---|
526 | this->owner = owner; |
---|
527 | |
---|
528 | //We are passing the monitor to someone else, which means recursion level is not 0 |
---|
529 | this->recursion = owner ? 1 : 0; |
---|
530 | } |
---|
531 | |
---|
532 | static inline void set_owner( monitor_desc ** monitors, short count, thread_desc * owner ) { |
---|
533 | for( int i = 0; i < count; i++ ) { |
---|
534 | set_owner( monitors[i], owner ); |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | static inline void set_mask( monitor_desc ** storage, short count, const __waitfor_mask_t & mask ) { |
---|
539 | for(int i = 0; i < count; i++) { |
---|
540 | storage[i]->mask = mask; |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | static inline void reset_mask( monitor_desc * this ) { |
---|
545 | this->mask.accepted = NULL; |
---|
546 | this->mask.clauses = NULL; |
---|
547 | this->mask.size = 0; |
---|
548 | } |
---|
549 | |
---|
550 | static inline thread_desc * next_thread( monitor_desc * this ) { |
---|
551 | //Check the signaller stack |
---|
552 | __condition_criterion_t * urgent = pop( &this->signal_stack ); |
---|
553 | if( urgent ) { |
---|
554 | //The signaller stack is not empty, |
---|
555 | //regardless of if we are ready to baton pass, |
---|
556 | //we need to set the monitor as in use |
---|
557 | set_owner( this, urgent->owner->waiting_thread ); |
---|
558 | |
---|
559 | return check_condition( urgent ); |
---|
560 | } |
---|
561 | |
---|
562 | // No signaller thread |
---|
563 | // Get the next thread in the entry_queue |
---|
564 | thread_desc * new_owner = pop_head( &this->entry_queue ); |
---|
565 | set_owner( this, new_owner ); |
---|
566 | |
---|
567 | return new_owner; |
---|
568 | } |
---|
569 | |
---|
570 | static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & group ) { |
---|
571 | __acceptable_t * it = this->mask.clauses; // Optim |
---|
572 | int count = this->mask.size; |
---|
573 | |
---|
574 | // Check if there are any acceptable functions |
---|
575 | if( !it ) return false; |
---|
576 | |
---|
577 | // If this isn't the first monitor to test this, there is no reason to repeat the test. |
---|
578 | if( this != group[0] ) return group[0]->mask.accepted >= 0; |
---|
579 | |
---|
580 | // For all acceptable functions check if this is the current function. |
---|
581 | for( short i = 0; i < count; i++, it++ ) { |
---|
582 | if( *it == group ) { |
---|
583 | *this->mask.accepted = i; |
---|
584 | return true; |
---|
585 | } |
---|
586 | } |
---|
587 | |
---|
588 | // No function matched |
---|
589 | return false; |
---|
590 | } |
---|
591 | |
---|
592 | static inline void init( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) { |
---|
593 | for(int i = 0; i < count; i++) { |
---|
594 | (criteria[i]){ monitors[i], waiter }; |
---|
595 | } |
---|
596 | |
---|
597 | waiter->criteria = criteria; |
---|
598 | } |
---|
599 | |
---|
600 | static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) { |
---|
601 | for(int i = 0; i < count; i++) { |
---|
602 | (criteria[i]){ monitors[i], waiter }; |
---|
603 | push( &criteria[i].target->signal_stack, &criteria[i] ); |
---|
604 | } |
---|
605 | |
---|
606 | waiter->criteria = criteria; |
---|
607 | } |
---|
608 | |
---|
609 | static inline void lock_all( spinlock ** locks, unsigned short count ) { |
---|
610 | for( int i = 0; i < count; i++ ) { |
---|
611 | lock_yield( locks[i] DEBUG_CTX2 ); |
---|
612 | } |
---|
613 | } |
---|
614 | |
---|
615 | static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) { |
---|
616 | for( int i = 0; i < count; i++ ) { |
---|
617 | spinlock * l = &source[i]->lock; |
---|
618 | lock_yield( l DEBUG_CTX2 ); |
---|
619 | if(locks) locks[i] = l; |
---|
620 | } |
---|
621 | } |
---|
622 | |
---|
623 | static inline void unlock_all( spinlock ** locks, unsigned short count ) { |
---|
624 | for( int i = 0; i < count; i++ ) { |
---|
625 | unlock( locks[i] ); |
---|
626 | } |
---|
627 | } |
---|
628 | |
---|
629 | static inline void unlock_all( monitor_desc ** locks, unsigned short count ) { |
---|
630 | for( int i = 0; i < count; i++ ) { |
---|
631 | unlock( &locks[i]->lock ); |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | static inline void save( monitor_desc ** ctx, short count, __attribute((unused)) spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) { |
---|
636 | for( int i = 0; i < count; i++ ) { |
---|
637 | recursions[i] = ctx[i]->recursion; |
---|
638 | masks[i] = ctx[i]->mask; |
---|
639 | } |
---|
640 | } |
---|
641 | |
---|
642 | static inline void restore( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) { |
---|
643 | lock_all( locks, count ); |
---|
644 | for( int i = 0; i < count; i++ ) { |
---|
645 | ctx[i]->recursion = recursions[i]; |
---|
646 | ctx[i]->mask = masks[i]; |
---|
647 | } |
---|
648 | unlock_all( locks, count ); |
---|
649 | } |
---|
650 | |
---|
651 | // Function has 2 different behavior |
---|
652 | // 1 - Marks a monitors as being ready to run |
---|
653 | // 2 - Checks if all the monitors are ready to run |
---|
654 | // if so return the thread to run |
---|
655 | static inline thread_desc * check_condition( __condition_criterion_t * target ) { |
---|
656 | __condition_node_t * node = target->owner; |
---|
657 | unsigned short count = node->count; |
---|
658 | __condition_criterion_t * criteria = node->criteria; |
---|
659 | |
---|
660 | bool ready2run = true; |
---|
661 | |
---|
662 | for( int i = 0; i < count; i++ ) { |
---|
663 | |
---|
664 | // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target ); |
---|
665 | if( &criteria[i] == target ) { |
---|
666 | criteria[i].ready = true; |
---|
667 | // LIB_DEBUG_PRINT_SAFE( "True\n" ); |
---|
668 | } |
---|
669 | |
---|
670 | ready2run = criteria[i].ready && ready2run; |
---|
671 | } |
---|
672 | |
---|
673 | // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run ); |
---|
674 | return ready2run ? node->waiting_thread : NULL; |
---|
675 | } |
---|
676 | |
---|
677 | static inline void brand_condition( condition * this ) { |
---|
678 | thread_desc * thrd = this_thread; |
---|
679 | if( !this->monitors ) { |
---|
680 | // LIB_DEBUG_PRINT_SAFE("Branding\n"); |
---|
681 | assertf( thrd->monitors.list != NULL, "No current monitor to brand condition %p", thrd->monitors.list ); |
---|
682 | this->monitor_count = thrd->monitors.size; |
---|
683 | |
---|
684 | this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) ); |
---|
685 | for( int i = 0; i < this->monitor_count; i++ ) { |
---|
686 | this->monitors[i] = thrd->monitors.list[i]; |
---|
687 | } |
---|
688 | } |
---|
689 | } |
---|
690 | |
---|
691 | static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc ** monitors, int count ) { |
---|
692 | |
---|
693 | __thread_queue_t * entry_queue = &monitors[0]->entry_queue; |
---|
694 | |
---|
695 | // For each thread in the entry-queue |
---|
696 | for( thread_desc ** thrd_it = &entry_queue->head; |
---|
697 | *thrd_it; |
---|
698 | thrd_it = &(*thrd_it)->next |
---|
699 | ) { |
---|
700 | // For each acceptable check if it matches |
---|
701 | int i = 0; |
---|
702 | __acceptable_t * end = mask.clauses + mask.size; |
---|
703 | for( __acceptable_t * it = mask.clauses; it != end; it++, i++ ) { |
---|
704 | // Check if we have a match |
---|
705 | if( *it == (*thrd_it)->monitors ) { |
---|
706 | |
---|
707 | // If we have a match return it |
---|
708 | // after removeing it from the entry queue |
---|
709 | return [remove( entry_queue, thrd_it ), i]; |
---|
710 | } |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | return [0, -1]; |
---|
715 | } |
---|
716 | |
---|
717 | forall(dtype T | sized( T )) |
---|
718 | static inline short insert_unique( T ** array, short & size, T * val ) { |
---|
719 | if( !val ) return size; |
---|
720 | |
---|
721 | for(int i = 0; i <= size; i++) { |
---|
722 | if( array[i] == val ) return size; |
---|
723 | } |
---|
724 | |
---|
725 | array[size] = val; |
---|
726 | size = size + 1; |
---|
727 | return size; |
---|
728 | } |
---|
729 | |
---|
730 | static inline short count_max( const __waitfor_mask_t & mask ) { |
---|
731 | short max = 0; |
---|
732 | for( int i = 0; i < mask.size; i++ ) { |
---|
733 | max += mask.clauses[i].size; |
---|
734 | } |
---|
735 | return max; |
---|
736 | } |
---|
737 | |
---|
738 | static inline short aggregate( monitor_desc ** storage, const __waitfor_mask_t & mask ) { |
---|
739 | short size = 0; |
---|
740 | for( int i = 0; i < mask.size; i++ ) { |
---|
741 | for( int j = 0; j < mask.clauses[i].size; j++) { |
---|
742 | insert_unique( storage, size, mask.clauses[i].list[j] ); |
---|
743 | } |
---|
744 | } |
---|
745 | qsort( storage, size ); |
---|
746 | return size; |
---|
747 | } |
---|
748 | |
---|
749 | void ?{}( __condition_blocked_queue_t & this ) { |
---|
750 | this.head = NULL; |
---|
751 | this.tail = &this.head; |
---|
752 | } |
---|
753 | |
---|
754 | void append( __condition_blocked_queue_t * this, __condition_node_t * c ) { |
---|
755 | verify(this->tail != NULL); |
---|
756 | *this->tail = c; |
---|
757 | this->tail = &c->next; |
---|
758 | } |
---|
759 | |
---|
760 | __condition_node_t * pop_head( __condition_blocked_queue_t * this ) { |
---|
761 | __condition_node_t * head = this->head; |
---|
762 | if( head ) { |
---|
763 | this->head = head->next; |
---|
764 | if( !head->next ) { |
---|
765 | this->tail = &this->head; |
---|
766 | } |
---|
767 | head->next = NULL; |
---|
768 | } |
---|
769 | return head; |
---|
770 | } |
---|
771 | |
---|
772 | // Local Variables: // |
---|
773 | // mode: c // |
---|
774 | // tab-width: 4 // |
---|
775 | // End: // |
---|