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