source: src/libcfa/concurrency/monitor.c@ 9d85038

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 9d85038 was 82ff5845, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

First implementation of preemption, does not appear to work with scheduling

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