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