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

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 b751c8e was b227f68, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Commented some debug messages.
Monitors now yield when spinning.
Debug mode saves more information about previous locks and interrupts

  • Property mode set to 100644
File size: 15.4 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
126 //We can now let other threads in safely
127 unlock( &this->lock );
128
129 //We need to wake-up the thread
130 if( new_owner) ScheduleThread( new_owner );
[cc7f4b1]131 }
[2781e65]132}
133
[5ea06d6]134static inline void enter(monitor_desc ** monitors, int count) {
[0c78741]135 for(int i = 0; i < count; i++) {
136 __enter_monitor_desc( monitors[i] );
[2781e65]137 }
138}
139
[5ea06d6]140static inline void leave(monitor_desc ** monitors, int count) {
[0c78741]141 for(int i = count - 1; i >= 0; i--) {
142 __leave_monitor_desc( monitors[i] );
[2781e65]143 }
[5ea06d6]144}
145
146void ?{}( monitor_guard_t * this, monitor_desc ** m, int count ) {
147 this->m = m;
148 this->count = count;
149 qsort(this->m, count);
150 enter( this->m, this->count );
151
[1c273d0]152 this->prev_mntrs = this_thread->current_monitors;
153 this->prev_count = this_thread->current_monitor_count;
[5ea06d6]154
[1c273d0]155 this_thread->current_monitors = m;
156 this_thread->current_monitor_count = count;
[5ea06d6]157}
158
159void ^?{}( monitor_guard_t * this ) {
160 leave( this->m, this->count );
161
[1c273d0]162 this_thread->current_monitors = this->prev_mntrs;
163 this_thread->current_monitor_count = this->prev_count;
[5ea06d6]164}
165
[be3d020]166void ?{}(__condition_node_t * this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) {
167 this->waiting_thread = waiting_thread;
168 this->count = count;
169 this->next = NULL;
170 this->user_info = user_info;
171}
172
173void ?{}(__condition_criterion_t * this ) {
174 this->ready = false;
175 this->target = NULL;
176 this->owner = NULL;
177 this->next = NULL;
178}
179
180void ?{}(__condition_criterion_t * this, monitor_desc * target, __condition_node_t * owner ) {
181 this->ready = false;
182 this->target = target;
183 this->owner = owner;
184 this->next = NULL;
[ad1a8dd]185}
186
[5ea06d6]187//-----------------------------------------------------------------------------
188// Internal scheduling
[be3d020]189void wait( condition * this, uintptr_t user_info = 0 ) {
[b227f68]190 // LIB_DEBUG_PRINT_SAFE("Waiting\n");
[5ea06d6]191
[0c78741]192 brand_condition( this );
[5ea06d6]193
194 //Check that everything is as expected
[0c78741]195 assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
[4aa2fb2]196 verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
197 verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
[5ea06d6]198
[0c78741]199 unsigned short count = this->monitor_count;
[9c59cd4]200 unsigned int recursions[ count ]; //Save the current recursion levels to restore them later
[82ff5845]201 spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal
[0c78741]202
[b227f68]203 // LIB_DEBUG_PRINT_SAFE("count %i\n", count);
[0c78741]204
[1c273d0]205 __condition_node_t waiter = { (thread_desc*)this_thread, count, user_info };
[0c78741]206
207 __condition_criterion_t criteria[count];
208 for(int i = 0; i < count; i++) {
[be3d020]209 (&criteria[i]){ this->monitors[i], &waiter };
[b227f68]210 // LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
[0c78741]211 }
[5ea06d6]212
[0c78741]213 waiter.criteria = criteria;
214 append( &this->blocked, &waiter );
[5ea06d6]215
[9c59cd4]216 lock_all( this->monitors, locks, count );
217 save_recursion( this->monitors, recursions, count );
[0c78741]218 //DON'T unlock, ask the kernel to do it
[5ea06d6]219
[0c78741]220 //Find the next thread(s) to run
[ad1a8dd]221 unsigned short thread_count = 0;
[0c78741]222 thread_desc * threads[ count ];
[ad1a8dd]223 for(int i = 0; i < count; i++) {
224 threads[i] = 0;
225 }
226
[0c78741]227 for( int i = 0; i < count; i++) {
228 thread_desc * new_owner = next_thread( this->monitors[i] );
[ad1a8dd]229 thread_count = insert_unique( threads, thread_count, new_owner );
[5ea06d6]230 }
231
[b227f68]232 // LIB_DEBUG_PRINT_SAFE("Will unblock: ");
[0c78741]233 for(int i = 0; i < thread_count; i++) {
[b227f68]234 // LIB_DEBUG_PRINT_SAFE("%p ", threads[i]);
[0c78741]235 }
[b227f68]236 // LIB_DEBUG_PRINT_SAFE("\n");
[5ea06d6]237
[9c59cd4]238 // Everything is ready to go to sleep
[82ff5845]239 BlockInternal( locks, count, threads, thread_count );
[5ea06d6]240
[c81ebf9]241
[5ea06d6]242 //WE WOKE UP
243
244
245 //We are back, restore the owners and recursions
[9c59cd4]246 lock_all( locks, count );
247 restore_recursion( this->monitors, recursions, count );
248 unlock_all( locks, count );
[5ea06d6]249}
250
[be3d020]251bool signal( condition * this ) {
252 if( is_empty( this ) ) {
[b227f68]253 // LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
[be3d020]254 return false;
[0c78741]255 }
[5ea06d6]256
257 //Check that everything is as expected
[4aa2fb2]258 verify( this->monitors );
259 verify( this->monitor_count != 0 );
[0c78741]260
261 unsigned short count = this->monitor_count;
[2ac095d]262
[44264c5]263 //Some more checking in debug
[5ea06d6]264 LIB_DEBUG_DO(
[1c273d0]265 thread_desc * this_thrd = this_thread;
[0c78741]266 if ( this->monitor_count != this_thrd->current_monitor_count ) {
267 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]268 } // if
[0c78741]269
270 for(int i = 0; i < this->monitor_count; i++) {
271 if ( this->monitors[i] != this_thrd->current_monitors[i] ) {
272 abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->current_monitors[i] );
273 } // if
274 }
[5ea06d6]275 );
276
[44264c5]277 //Lock all the monitors
[0c78741]278 lock_all( this->monitors, NULL, count );
[b227f68]279 // LIB_DEBUG_PRINT_SAFE("Signalling");
[0c78741]280
[44264c5]281 //Pop the head of the waiting queue
[0c78741]282 __condition_node_t * node = pop_head( &this->blocked );
[44264c5]283
284 //Add the thread to the proper AS stack
[0c78741]285 for(int i = 0; i < count; i++) {
286 __condition_criterion_t * crit = &node->criteria[i];
[b227f68]287 // LIB_DEBUG_PRINT_SAFE(" %p", crit->target);
[0c78741]288 assert( !crit->ready );
289 push( &crit->target->signal_stack, crit );
[5ea06d6]290 }
[0c78741]291
[b227f68]292 // LIB_DEBUG_PRINT_SAFE("\n");
[0c78741]293
[44264c5]294 //Release
[0c78741]295 unlock_all( this->monitors, count );
[be3d020]296
297 return true;
[5ea06d6]298}
299
[be3d020]300bool signal_block( condition * this ) {
[44264c5]301 if( !this->blocked.head ) {
302 LIB_DEBUG_PRINT_SAFE("Nothing to signal\n");
[be3d020]303 return false;
[44264c5]304 }
305
306 //Check that everything is as expected
[4aa2fb2]307 verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
308 verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
[44264c5]309
310 unsigned short count = this->monitor_count;
311 unsigned int recursions[ count ]; //Save the current recursion levels to restore them later
[82ff5845]312 spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal
[44264c5]313
314 lock_all( this->monitors, locks, count );
315
316 //create creteria
[1c273d0]317 __condition_node_t waiter = { (thread_desc*)this_thread, count, 0 };
[44264c5]318
319 __condition_criterion_t criteria[count];
320 for(int i = 0; i < count; i++) {
[be3d020]321 (&criteria[i]){ this->monitors[i], &waiter };
[b227f68]322 // LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] );
[44264c5]323 push( &criteria[i].target->signal_stack, &criteria[i] );
324 }
325
326 waiter.criteria = criteria;
327
328 //save contexts
329 save_recursion( this->monitors, recursions, count );
330
331 //Find the thread to run
332 thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
333 for(int i = 0; i < count; i++) {
334 set_owner( this->monitors[i], signallee );
335 }
336
337 LIB_DEBUG_PRINT_SAFE( "Waiting on signal block\n" );
338
339 //Everything is ready to go to sleep
[82ff5845]340 BlockInternal( locks, count, &signallee, 1 );
[44264c5]341
[c81ebf9]342
343
344
[44264c5]345 LIB_DEBUG_PRINT_SAFE( "Back from signal block\n" );
346
347 //We are back, restore the owners and recursions
348 lock_all( locks, count );
349 restore_recursion( this->monitors, recursions, count );
350 unlock_all( locks, count );
[be3d020]351
352 return true;
353}
354
355uintptr_t front( condition * this ) {
[2ac095d]356 verifyf( !is_empty(this),
[4aa2fb2]357 "Attempt to access user data on an empty condition.\n"
358 "Possible cause is not checking if the condition is empty before reading stored data."
[be3d020]359 );
360 return this->blocked.head->user_info;
[44264c5]361}
362
[c81ebf9]363//-----------------------------------------------------------------------------
364// Internal scheduling
365void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ) {
[1c273d0]366 // thread_desc * this = this_thread;
[c81ebf9]367
368 // unsigned short count = this->current_monitor_count;
369 // unsigned int recursions[ count ]; //Save the current recursion levels to restore them later
[82ff5845]370 // spinlock * locks [ count ]; //We need to pass-in an array of locks to BlockInternal
[c81ebf9]371
372 // lock_all( this->current_monitors, locks, count );
373
374
375
376
377
378 // // // Everything is ready to go to sleep
[82ff5845]379 // // BlockInternal( locks, count, threads, thread_count );
[c81ebf9]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( this->monitors, recursions, count );
388 // unlock_all( locks, count );
389}
390
[0c78741]391//-----------------------------------------------------------------------------
392// Utilities
393
394static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
395 //Pass the monitor appropriately
396 this->owner = owner;
397
398 //We are passing the monitor to someone else, which means recursion level is not 0
399 this->recursion = owner ? 1 : 0;
400}
401
402static inline thread_desc * next_thread( monitor_desc * this ) {
403 //Check the signaller stack
404 __condition_criterion_t * urgent = pop( &this->signal_stack );
405 if( urgent ) {
406 //The signaller stack is not empty,
407 //regardless of if we are ready to baton pass,
408 //we need to set the monitor as in use
409 set_owner( this, urgent->owner->waiting_thread );
410
411 return check_condition( urgent );
412 }
413
414 // No signaller thread
415 // Get the next thread in the entry_queue
416 thread_desc * new_owner = pop_head( &this->entry_queue );
417 set_owner( this, new_owner );
418
419 return new_owner;
420}
421
422static inline void lock_all( spinlock ** locks, unsigned short count ) {
423 for( int i = 0; i < count; i++ ) {
[b227f68]424 lock_yield( locks[i] DEBUG_CTX2 );
[0c78741]425 }
426}
427
428static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
429 for( int i = 0; i < count; i++ ) {
430 spinlock * l = &source[i]->lock;
[b227f68]431 lock_yield( l DEBUG_CTX2 );
[0c78741]432 if(locks) locks[i] = l;
433 }
434}
435
436static inline void unlock_all( spinlock ** locks, unsigned short count ) {
437 for( int i = 0; i < count; i++ ) {
438 unlock( locks[i] );
439 }
440}
441
442static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
443 for( int i = 0; i < count; i++ ) {
444 unlock( &locks[i]->lock );
445 }
446}
447
448
449static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) {
450 for( int i = 0; i < count; i++ ) {
451 recursions[i] = ctx[i]->recursion;
452 }
453}
454
455static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) {
456 for( int i = 0; i < count; i++ ) {
457 ctx[i]->recursion = recursions[i];
458 }
459}
460
461// Function has 2 different behavior
462// 1 - Marks a monitors as being ready to run
463// 2 - Checks if all the monitors are ready to run
464// if so return the thread to run
465static inline thread_desc * check_condition( __condition_criterion_t * target ) {
466 __condition_node_t * node = target->owner;
467 unsigned short count = node->count;
468 __condition_criterion_t * criteria = node->criteria;
469
470 bool ready2run = true;
471
472 for( int i = 0; i < count; i++ ) {
[44264c5]473
[b227f68]474 // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
[0c78741]475 if( &criteria[i] == target ) {
476 criteria[i].ready = true;
[b227f68]477 // LIB_DEBUG_PRINT_SAFE( "True\n" );
[0c78741]478 }
479
480 ready2run = criteria[i].ready && ready2run;
481 }
482
[b227f68]483 // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
[0c78741]484 return ready2run ? node->waiting_thread : NULL;
485}
486
487static inline void brand_condition( condition * this ) {
[1c273d0]488 thread_desc * thrd = this_thread;
[0c78741]489 if( !this->monitors ) {
[b227f68]490 // LIB_DEBUG_PRINT_SAFE("Branding\n");
[0c78741]491 assertf( thrd->current_monitors != NULL, "No current monitor to brand condition", thrd->current_monitors );
492 this->monitor_count = thrd->current_monitor_count;
[a933dcf4]493
494 this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
495 for( int i = 0; i < this->monitor_count; i++ ) {
496 this->monitors[i] = thrd->current_monitors[i];
497 }
[0c78741]498 }
499}
500
501static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
[ad1a8dd]502 if( !val ) return end;
503
504 for(int i = 0; i <= end; i++) {
[0c78741]505 if( thrds[i] == val ) return end;
506 }
507
508 thrds[end] = val;
509 return end + 1;
510}
511
512void ?{}( __condition_blocked_queue_t * this ) {
513 this->head = NULL;
514 this->tail = &this->head;
515}
516
517void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
[4aa2fb2]518 verify(this->tail != NULL);
[0c78741]519 *this->tail = c;
520 this->tail = &c->next;
521}
522
523__condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
524 __condition_node_t * head = this->head;
525 if( head ) {
526 this->head = head->next;
527 if( !head->next ) {
528 this->tail = &this->head;
529 }
530 head->next = NULL;
531 }
532 return head;
[4aa2fb2]533}
Note: See TracBrowser for help on using the repository browser.