[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 | |
---|
| 19 | #include "kernel_private.h" |
---|
| 20 | |
---|
[690f13c] | 21 | void set_owner( monitor_desc * this, thread_desc * owner ) { |
---|
| 22 | //Pass the monitor appropriately |
---|
| 23 | this->owner = owner; |
---|
| 24 | |
---|
| 25 | //We are passing the monitor to someone else, which means recursion level is not 0 |
---|
| 26 | this->recursion = owner ? 1 : 0; |
---|
| 27 | } |
---|
| 28 | |
---|
[cb0e6de] | 29 | extern "C" { |
---|
| 30 | void __enter_monitor_desc(monitor_desc * this) { |
---|
| 31 | lock( &this->lock ); |
---|
| 32 | thread_desc * thrd = this_thread(); |
---|
[f07e037] | 33 | |
---|
[cb0e6de] | 34 | if( !this->owner ) { |
---|
| 35 | //No one has the monitor, just take it |
---|
| 36 | this->owner = thrd; |
---|
| 37 | this->recursion = 1; |
---|
| 38 | } |
---|
| 39 | else if( this->owner == thrd) { |
---|
| 40 | //We already have the monitor, just not how many times we took it |
---|
| 41 | assert( this->recursion > 0 ); |
---|
| 42 | this->recursion += 1; |
---|
| 43 | } |
---|
| 44 | else { |
---|
| 45 | //Some one else has the monitor, wait in line for it |
---|
| 46 | append( &this->entry_queue, thrd ); |
---|
| 47 | ScheduleInternal( &this->lock ); |
---|
[cc7f4b1] | 48 | |
---|
[cb0e6de] | 49 | //ScheduleInternal will unlock spinlock, no need to unlock ourselves |
---|
| 50 | return; |
---|
| 51 | } |
---|
[f07e037] | 52 | |
---|
[cb0e6de] | 53 | unlock( &this->lock ); |
---|
| 54 | } |
---|
[f07e037] | 55 | |
---|
[690f13c] | 56 | // leave pseudo code : |
---|
| 57 | // decrement level |
---|
| 58 | // leve == 0 ? |
---|
| 59 | // no : done |
---|
| 60 | // yes : |
---|
| 61 | // signal stack empty ? |
---|
| 62 | // has leader : |
---|
| 63 | // bulk acquiring means we don't own the signal stack |
---|
| 64 | // ignore it but don't release the monitor |
---|
| 65 | // yes : |
---|
| 66 | // next in entry queue is new owner |
---|
| 67 | // no : |
---|
| 68 | // top of the signal stack is the owner |
---|
| 69 | // context switch to him right away |
---|
| 70 | // |
---|
[cb0e6de] | 71 | void __leave_monitor_desc(monitor_desc * this) { |
---|
| 72 | lock( &this->lock ); |
---|
[f07e037] | 73 | |
---|
[cb0e6de] | 74 | thread_desc * thrd = this_thread(); |
---|
| 75 | assert( thrd == this->owner ); |
---|
[cc7f4b1] | 76 | |
---|
[cb0e6de] | 77 | //Leaving a recursion level, decrement the counter |
---|
| 78 | this->recursion -= 1; |
---|
[f07e037] | 79 | |
---|
[690f13c] | 80 | //If we haven't left the last level of recursion |
---|
| 81 | //it means we don't need to do anything |
---|
| 82 | if( this->recursion != 0) { |
---|
| 83 | unlock( &this->lock ); |
---|
| 84 | return; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | //If we don't own the signal stack then just leave it to the owner |
---|
| 88 | if( this->stack_owner ) { |
---|
| 89 | assert( this->owner == this->stack_owner ); |
---|
| 90 | unlock( &this->lock ); |
---|
| 91 | return; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | //We are the stack owner and have left the last recursion level. |
---|
| 95 | //We are in charge of passing the monitor |
---|
[cb0e6de] | 96 | thread_desc * new_owner = 0; |
---|
[cc7f4b1] | 97 | |
---|
[690f13c] | 98 | //Check the signaller stack |
---|
| 99 | new_owner = pop( &this->signal_stack ); |
---|
| 100 | if( new_owner ) { |
---|
| 101 | //The signaller stack is not empty, |
---|
| 102 | //transfer control immediately |
---|
| 103 | set_owner( this, new_owner ); |
---|
| 104 | ScheduleInternal( &this->lock, new_owner ); |
---|
| 105 | return; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // No signaller thread |
---|
| 109 | // Get the next thread in the entry_queue |
---|
| 110 | new_owner = pop_head( &this->entry_queue ); |
---|
| 111 | set_owner( this, new_owner ); |
---|
[f07e037] | 112 | |
---|
[690f13c] | 113 | //We can now let other threads in safely |
---|
[cb0e6de] | 114 | unlock( &this->lock ); |
---|
[51f3798] | 115 | |
---|
[690f13c] | 116 | //We need to wake-up the thread |
---|
| 117 | ScheduleThread( new_owner ); |
---|
[cc7f4b1] | 118 | } |
---|
[2781e65] | 119 | } |
---|
| 120 | |
---|
[84c52a8] | 121 | void enter(monitor_desc ** monitors, int count) { |
---|
[2781e65] | 122 | for(int i = 0; i < count; i++) { |
---|
[cb0e6de] | 123 | __enter_monitor_desc( monitors[i] ); |
---|
[2781e65] | 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
[84c52a8] | 127 | void leave(monitor_desc ** monitors, int count) { |
---|
[2781e65] | 128 | for(int i = count - 1; i >= 0; i--) { |
---|
[cb0e6de] | 129 | __leave_monitor_desc( monitors[i] ); |
---|
[2781e65] | 130 | } |
---|
[f07e037] | 131 | } |
---|