source: src/libcfa/concurrency/monitor.c@ d6ff3ff

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since d6ff3ff was f2b12406, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Preemption is now stable enough to push, some clean-up needed

  • Property mode set to 100644
File size: 15.3 KB
RevLine 
[f07e037]1// -*- Mode: CFA -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
[84c52a8]8// monitor_desc.c --
[f07e037]9//
10// Author : Thierry Delisle
11// Created On : Thd Feb 23 12:27:26 2017
12// Last Modified By : Thierry Delisle
13// Last Modified On : --
14// Update Count : 0
15//
16
17#include "monitor"
18
[a933dcf4]19#include <stdlib>
20
[5ea06d6]21#include "libhdr.h"
[2ac095d]22#include "kernel_private.h"
[f07e037]23
[0c78741]24//-----------------------------------------------------------------------------
25// Forward declarations
26static inline void set_owner( monitor_desc * this, thread_desc * owner );
27static inline thread_desc * next_thread( monitor_desc * this );
28
29static inline void lock_all( spinlock ** locks, unsigned short count );
30static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count );
31static inline void unlock_all( spinlock ** locks, unsigned short count );
32static inline void unlock_all( monitor_desc ** locks, unsigned short count );
33
34static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count );
35static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count );
36
37static inline thread_desc * check_condition( __condition_criterion_t * );
38static inline void brand_condition( condition * );
39static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val );
40
41//-----------------------------------------------------------------------------
42// Enter/Leave routines
[690f13c]43
44
[cb0e6de]45extern "C" {
[1c273d0]46 void __enter_monitor_desc( monitor_desc * this ) {
[b227f68]47 lock_yield( &this->lock DEBUG_CTX2 );
[1c273d0]48 thread_desc * thrd = this_thread;
[f07e037]49
[1c273d0]50 // LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion);
[5ea06d6]51
[cb0e6de]52 if( !this->owner ) {
53 //No one has the monitor, just take it
[cd348e7]54 set_owner( this, thrd );
[cb0e6de]55 }
56 else if( this->owner == thrd) {
57 //We already have the monitor, just not how many times we took it
[4aa2fb2]58 verify( this->recursion > 0 );
[cb0e6de]59 this->recursion += 1;
60 }
61 else {
62 //Some one else has the monitor, wait in line for it
63 append( &this->entry_queue, thrd );
[1c273d0]64 // LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd);
[82ff5845]65 BlockInternal( &this->lock );
[cc7f4b1]66
[82ff5845]67 //BlockInternal will unlock spinlock, no need to unlock ourselves
[2ac095d]68 return;
[cb0e6de]69 }
[f07e037]70
[cb0e6de]71 unlock( &this->lock );
[5ea06d6]72 return;
[cb0e6de]73 }
[f07e037]74
[690f13c]75 // leave pseudo code :
[0c78741]76 // TODO
[1c273d0]77 void __leave_monitor_desc( monitor_desc * this ) {
[b227f68]78 lock_yield( &this->lock DEBUG_CTX2 );
[f07e037]79
[1c273d0]80 // LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i). ", this_thread, this, this->owner, this->recursion);
81 verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread, this->owner, this->recursion );
[cc7f4b1]82
[cb0e6de]83 //Leaving a recursion level, decrement the counter
84 this->recursion -= 1;
[f07e037]85
[690f13c]86 //If we haven't left the last level of recursion
87 //it means we don't need to do anything
88 if( this->recursion != 0) {
89 unlock( &this->lock );
90 return;
91 }
[f07e037]92
[0c78741]93 thread_desc * new_owner = next_thread( this );
[5ea06d6]94
[690f13c]95 //We can now let other threads in safely
[cb0e6de]96 unlock( &this->lock );
[51f3798]97
[1c273d0]98 // LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner);
[44264c5]99
[690f13c]100 //We need to wake-up the thread
[1c273d0]101 WakeThread( new_owner );
102 }
103
104 void __leave_thread_monitor( thread_desc * thrd ) {
105 monitor_desc * this = &thrd->mon;
[b227f68]106 lock_yield( &this->lock DEBUG_CTX2 );
[1c273d0]107
108 disable_interrupts();
109
110 thrd->cor.state = Halted;
111
112 verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
113
114 //Leaving a recursion level, decrement the counter
115 this->recursion -= 1;
116
117 //If we haven't left the last level of recursion
118 //it means we don't need to do anything
119 if( this->recursion != 0) {
120 unlock( &this->lock );
121 return;
122 }
123
124 thread_desc * new_owner = next_thread( this );
125
[f2b12406]126 LeaveThread( &this->lock, new_owner );
[cc7f4b1]127 }
[2781e65]128}
129
[5ea06d6]130static inline void enter(monitor_desc ** monitors, int count) {
[0c78741]131 for(int i = 0; i < count; i++) {
132 __enter_monitor_desc( monitors[i] );
[2781e65]133 }
134}
135
[5ea06d6]136static inline void leave(monitor_desc ** monitors, int count) {
[0c78741]137 for(int i = count - 1; i >= 0; i--) {
138 __leave_monitor_desc( monitors[i] );
[2781e65]139 }
[5ea06d6]140}
141
142void ?{}( monitor_guard_t * this, monitor_desc ** m, int count ) {
143 this->m = m;
144 this->count = count;
145 qsort(this->m, count);
146 enter( this->m, this->count );
147
[1c273d0]148 this->prev_mntrs = this_thread->current_monitors;
149 this->prev_count = this_thread->current_monitor_count;
[5ea06d6]150
[1c273d0]151 this_thread->current_monitors = m;
152 this_thread->current_monitor_count = count;
[5ea06d6]153}
154
155void ^?{}( monitor_guard_t * this ) {
156 leave( this->m, this->count );
157
[1c273d0]158 this_thread->current_monitors = this->prev_mntrs;
159 this_thread->current_monitor_count = this->prev_count;
[5ea06d6]160}
161
[be3d020]162void ?{}(__condition_node_t * this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) {
163 this->waiting_thread = waiting_thread;
164 this->count = count;
165 this->next = NULL;
166 this->user_info = user_info;
167}
168
169void ?{}(__condition_criterion_t * this ) {
170 this->ready = false;
171 this->target = NULL;
172 this->owner = NULL;
173 this->next = NULL;
174}
175
176void ?{}(__condition_criterion_t * this, monitor_desc * target, __condition_node_t * owner ) {
177 this->ready = false;
178 this->target = target;
179 this->owner = owner;
180 this->next = NULL;
[ad1a8dd]181}
182
[5ea06d6]183//-----------------------------------------------------------------------------
184// Internal scheduling
[be3d020]185void wait( condition * this, uintptr_t user_info = 0 ) {
[b227f68]186 // LIB_DEBUG_PRINT_SAFE("Waiting\n");
[5ea06d6]187
[0c78741]188 brand_condition( this );
[5ea06d6]189
190 //Check that everything is as expected
[0c78741]191 assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
[4aa2fb2]192 verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
193 verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
[5ea06d6]194
[0c78741]195 unsigned short count = this->monitor_count;
[9c59cd4]196 unsigned int recursions[ count ]; //Save the current recursion levels to restore them later
[82ff5845]197 spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal
[0c78741]198
[b227f68]199 // LIB_DEBUG_PRINT_SAFE("count %i\n", count);
[0c78741]200
[1c273d0]201 __condition_node_t waiter = { (thread_desc*)this_thread, count, user_info };
[0c78741]202
203 __condition_criterion_t criteria[count];
204 for(int i = 0; i < count; i++) {
[be3d020]205 (&criteria[i]){ this->monitors[i], &waiter };
[b227f68]206 // LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
[0c78741]207 }
[5ea06d6]208
[0c78741]209 waiter.criteria = criteria;
210 append( &this->blocked, &waiter );
[5ea06d6]211
[9c59cd4]212 lock_all( this->monitors, locks, count );
213 save_recursion( this->monitors, recursions, count );
[0c78741]214 //DON'T unlock, ask the kernel to do it
[5ea06d6]215
[0c78741]216 //Find the next thread(s) to run
[ad1a8dd]217 unsigned short thread_count = 0;
[0c78741]218 thread_desc * threads[ count ];
[ad1a8dd]219 for(int i = 0; i < count; i++) {
220 threads[i] = 0;
221 }
222
[0c78741]223 for( int i = 0; i < count; i++) {
224 thread_desc * new_owner = next_thread( this->monitors[i] );
[ad1a8dd]225 thread_count = insert_unique( threads, thread_count, new_owner );
[5ea06d6]226 }
227
[b227f68]228 // LIB_DEBUG_PRINT_SAFE("Will unblock: ");
[0c78741]229 for(int i = 0; i < thread_count; i++) {
[b227f68]230 // LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
[0c78741]231 }
[b227f68]232 // LIB_DEBUG_PRINT_SAFE("\n");
[5ea06d6]233
[9c59cd4]234 // Everything is ready to go to sleep
[82ff5845]235 BlockInternal( locks, count, threads, thread_count );
[5ea06d6]236
[c81ebf9]237
[5ea06d6]238 //WE WOKE UP
239
240
241 //We are back, restore the owners and recursions
[9c59cd4]242 lock_all( locks, count );
243 restore_recursion( this->monitors, recursions, count );
244 unlock_all( locks, count );
[5ea06d6]245}
246
[be3d020]247bool signal( condition * this ) {
248 if( is_empty( this ) ) {
[b227f68]249 // LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
[be3d020]250 return false;
[0c78741]251 }
[5ea06d6]252
253 //Check that everything is as expected
[4aa2fb2]254 verify( this->monitors );
255 verify( this->monitor_count != 0 );
[0c78741]256
257 unsigned short count = this->monitor_count;
[2ac095d]258
[44264c5]259 //Some more checking in debug
[5ea06d6]260 LIB_DEBUG_DO(
[1c273d0]261 thread_desc * this_thrd = this_thread;
[0c78741]262 if ( this->monitor_count != this_thrd->current_monitor_count ) {
263 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 );
[5ea06d6]264 } // if
[0c78741]265
266 for(int i = 0; i < this->monitor_count; i++) {
267 if ( this->monitors[i] != this_thrd->current_monitors[i] ) {
268 abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->current_monitors[i] );
269 } // if
270 }
[5ea06d6]271 );
272
[44264c5]273 //Lock all the monitors
[0c78741]274 lock_all( this->monitors, NULL, count );
[b227f68]275 // LIB_DEBUG_PRINT_SAFE("Signalling");
[0c78741]276
[44264c5]277 //Pop the head of the waiting queue
[0c78741]278 __condition_node_t * node = pop_head( &this->blocked );
[44264c5]279
280 //Add the thread to the proper AS stack
[0c78741]281 for(int i = 0; i < count; i++) {
282 __condition_criterion_t * crit = &node->criteria[i];
[b227f68]283 // LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
[0c78741]284 assert( !crit->ready );
285 push( &crit->target->signal_stack, crit );
[5ea06d6]286 }
[0c78741]287
[b227f68]288 // LIB_DEBUG_PRINT_SAFE("\n");
[0c78741]289
[44264c5]290 //Release
[0c78741]291 unlock_all( this->monitors, count );
[be3d020]292
293 return true;
[5ea06d6]294}
295
[be3d020]296bool signal_block( condition * this ) {
[44264c5]297 if( !this->blocked.head ) {
298 LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
[be3d020]299 return false;
[44264c5]300 }
301
302 //Check that everything is as expected
[4aa2fb2]303 verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
304 verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
[44264c5]305
306 unsigned short count = this->monitor_count;
307 unsigned int recursions[ count ]; //Save the current recursion levels to restore them later
[82ff5845]308 spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal
[44264c5]309
310 lock_all( this->monitors, locks, count );
311
312 //create creteria
[1c273d0]313 __condition_node_t waiter = { (thread_desc*)this_thread, count, 0 };
[44264c5]314
315 __condition_criterion_t criteria[count];
316 for(int i = 0; i < count; i++) {
[be3d020]317 (&criteria[i]){ this->monitors[i], &waiter };
[b227f68]318 // LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
[44264c5]319 push( &criteria[i].target->signal_stack, &criteria[i] );
320 }
321
322 waiter.criteria = criteria;
323
324 //save contexts
325 save_recursion( this->monitors, recursions, count );
326
327 //Find the thread to run
328 thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
329 for(int i = 0; i < count; i++) {
330 set_owner( this->monitors[i], signallee );
331 }
332
333 LIB_DEBUG_PRINT_SAFE( "Waiting on signal block\n" );
334
335 //Everything is ready to go to sleep
[82ff5845]336 BlockInternal( locks, count, &signallee, 1 );
[44264c5]337
[c81ebf9]338
339
340
[44264c5]341 LIB_DEBUG_PRINT_SAFE( "Back from signal block\n" );
342
343 //We are back, restore the owners and recursions
344 lock_all( locks, count );
345 restore_recursion( this->monitors, recursions, count );
346 unlock_all( locks, count );
[be3d020]347
348 return true;
349}
350
351uintptr_t front( condition * this ) {
[2ac095d]352 verifyf( !is_empty(this),
[4aa2fb2]353 "Attempt to access user data on an empty condition.\n"
354 "Possible cause is not checking if the condition is empty before reading stored data."
[be3d020]355 );
356 return this->blocked.head->user_info;
[44264c5]357}
358
[c81ebf9]359//-----------------------------------------------------------------------------
360// Internal scheduling
361void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ) {
[1c273d0]362 // thread_desc * this = this_thread;
[c81ebf9]363
364 // unsigned short count = this->current_monitor_count;
365 // unsigned int recursions[ count ]; //Save the current recursion levels to restore them later
[82ff5845]366 // spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal
[c81ebf9]367
368 // lock_all( this->current_monitors, locks, count );
369
370
371
372
373
374 // // // Everything is ready to go to sleep
[82ff5845]375 // // BlockInternal( locks, count, threads, thread_count );
[c81ebf9]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( this->monitors, recursions, count );
384 // unlock_all( locks, count );
385}
386
[0c78741]387//-----------------------------------------------------------------------------
388// Utilities
389
390static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
391 //Pass the monitor appropriately
392 this->owner = owner;
393
394 //We are passing the monitor to someone else, which means recursion level is not 0
395 this->recursion = owner ? 1 : 0;
396}
397
398static inline thread_desc * next_thread( monitor_desc * this ) {
399 //Check the signaller stack
400 __condition_criterion_t * urgent = pop( &this->signal_stack );
401 if( urgent ) {
402 //The signaller stack is not empty,
403 //regardless of if we are ready to baton pass,
404 //we need to set the monitor as in use
405 set_owner( this, urgent->owner->waiting_thread );
406
407 return check_condition( urgent );
408 }
409
410 // No signaller thread
411 // Get the next thread in the entry_queue
412 thread_desc * new_owner = pop_head( &this->entry_queue );
413 set_owner( this, new_owner );
414
415 return new_owner;
416}
417
418static inline void lock_all( spinlock ** locks, unsigned short count ) {
419 for( int i = 0; i < count; i++ ) {
[b227f68]420 lock_yield( locks[i] DEBUG_CTX2 );
[0c78741]421 }
422}
423
424static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
425 for( int i = 0; i < count; i++ ) {
426 spinlock * l = &source[i]->lock;
[b227f68]427 lock_yield( l DEBUG_CTX2 );
[0c78741]428 if(locks) locks[i] = l;
429 }
430}
431
432static inline void unlock_all( spinlock ** locks, unsigned short count ) {
433 for( int i = 0; i < count; i++ ) {
434 unlock( locks[i] );
435 }
436}
437
438static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
439 for( int i = 0; i < count; i++ ) {
440 unlock( &locks[i]->lock );
441 }
442}
443
444
445static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) {
446 for( int i = 0; i < count; i++ ) {
447 recursions[i] = ctx[i]->recursion;
448 }
449}
450
451static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) {
452 for( int i = 0; i < count; i++ ) {
453 ctx[i]->recursion = recursions[i];
454 }
455}
456
457// Function has 2 different behavior
458// 1 - Marks a monitors as being ready to run
459// 2 - Checks if all the monitors are ready to run
460// if so return the thread to run
461static inline thread_desc * check_condition( __condition_criterion_t * target ) {
462 __condition_node_t * node = target->owner;
463 unsigned short count = node->count;
464 __condition_criterion_t * criteria = node->criteria;
465
466 bool ready2run = true;
467
468 for( int i = 0; i < count; i++ ) {
[44264c5]469
[b227f68]470 // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
[0c78741]471 if( &criteria[i] == target ) {
472 criteria[i].ready = true;
[b227f68]473 // LIB_DEBUG_PRINT_SAFE( "True\n" );
[0c78741]474 }
475
476 ready2run = criteria[i].ready && ready2run;
477 }
478
[b227f68]479 // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
[0c78741]480 return ready2run ? node->waiting_thread : NULL;
481}
482
483static inline void brand_condition( condition * this ) {
[1c273d0]484 thread_desc * thrd = this_thread;
[0c78741]485 if( !this->monitors ) {
[b227f68]486 // LIB_DEBUG_PRINT_SAFE("Branding\n");
[0c78741]487 assertf( thrd->current_monitors != NULL, "No current monitor to brand condition", thrd->current_monitors );
488 this->monitor_count = thrd->current_monitor_count;
[a933dcf4]489
490 this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
491 for( int i = 0; i < this->monitor_count; i++ ) {
492 this->monitors[i] = thrd->current_monitors[i];
493 }
[0c78741]494 }
495}
496
497static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
[ad1a8dd]498 if( !val ) return end;
499
500 for(int i = 0; i <= end; i++) {
[0c78741]501 if( thrds[i] == val ) return end;
502 }
503
504 thrds[end] = val;
505 return end + 1;
506}
507
508void ?{}( __condition_blocked_queue_t * this ) {
509 this->head = NULL;
510 this->tail = &this->head;
511}
512
513void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
[4aa2fb2]514 verify(this->tail != NULL);
[0c78741]515 *this->tail = c;
516 this->tail = &c->next;
517}
518
519__condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
520 __condition_node_t * head = this->head;
521 if( head ) {
522 this->head = head->next;
523 if( !head->next ) {
524 this->tail = &this->head;
525 }
526 head->next = NULL;
527 }
528 return head;
[4aa2fb2]529}
Note: See TracBrowser for help on using the repository browser.