[09800e9] | 1 | |
---|
| 2 | // -*- Mode: CFA -*- |
---|
| 3 | // |
---|
| 4 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 5 | // |
---|
| 6 | // The contents of this file are covered under the licence agreement in the |
---|
| 7 | // file "LICENCE" distributed with Cforall. |
---|
| 8 | // |
---|
| 9 | // mutex.c -- |
---|
| 10 | // |
---|
| 11 | // Author : Thierry Delisle |
---|
| 12 | // Created On : Fri May 25 01:37:11 2018 |
---|
[121be3e] | 13 | // Last Modified By : Peter A. Buhr |
---|
| 14 | // Last Modified On : Wed Dec 4 09:16:39 2019 |
---|
| 15 | // Update Count : 1 |
---|
[09800e9] | 16 | // |
---|
| 17 | |
---|
[2026bb6] | 18 | #define __cforall_thread__ |
---|
| 19 | |
---|
[58b6d1b] | 20 | #include "mutex.hfa" |
---|
[09800e9] | 21 | |
---|
[73abe95] | 22 | #include "kernel_private.hfa" |
---|
[09800e9] | 23 | |
---|
| 24 | //----------------------------------------------------------------------------- |
---|
| 25 | // Locks |
---|
| 26 | |
---|
| 27 | // Exclusive lock - non-recursive |
---|
| 28 | // --- |
---|
| 29 | void ?{}(mutex_lock & this) { |
---|
| 30 | this.lock{}; |
---|
| 31 | this.blocked_threads{}; |
---|
[519f11c] | 32 | this.is_locked = false; |
---|
[09800e9] | 33 | } |
---|
| 34 | |
---|
| 35 | void ^?{}(mutex_lock & this) { |
---|
| 36 | // default |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void lock(mutex_lock & this) with(this) { |
---|
| 40 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 41 | if( is_locked ) { |
---|
| 42 | append( blocked_threads, kernelTLS.this_thread ); |
---|
[3381ed7] | 43 | unlock( lock ); |
---|
[ae66348] | 44 | park( __cfaabi_dbg_ctx ); |
---|
[09800e9] | 45 | } |
---|
| 46 | else { |
---|
| 47 | is_locked = true; |
---|
| 48 | unlock( lock ); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | bool try_lock(mutex_lock & this) with(this) { |
---|
| 53 | bool ret = false; |
---|
| 54 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 55 | if( !is_locked ) { |
---|
| 56 | ret = true; |
---|
| 57 | is_locked = true; |
---|
| 58 | } |
---|
| 59 | unlock( lock ); |
---|
| 60 | return ret; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void unlock(mutex_lock & this) { |
---|
| 64 | lock( this.lock __cfaabi_dbg_ctx2 ); |
---|
[6c7099a] | 65 | this.is_locked = (this.blocked_threads != 0); |
---|
[3381ed7] | 66 | unpark( |
---|
[ae66348] | 67 | pop_head( this.blocked_threads ) __cfaabi_dbg_ctx2 |
---|
[09800e9] | 68 | ); |
---|
| 69 | unlock( this.lock ); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // Exclusive lock - non-recursive |
---|
| 73 | // --- |
---|
| 74 | void ?{}(recursive_mutex_lock & this) { |
---|
| 75 | this.lock{}; |
---|
| 76 | this.blocked_threads{}; |
---|
[121be3e] | 77 | this.owner = 0p; |
---|
[09800e9] | 78 | this.recursion_count = 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void ^?{}(recursive_mutex_lock & this) { |
---|
| 82 | // default |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void lock(recursive_mutex_lock & this) with(this) { |
---|
| 86 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[121be3e] | 87 | if( owner == 0p ) { |
---|
[09800e9] | 88 | owner = kernelTLS.this_thread; |
---|
| 89 | recursion_count = 1; |
---|
| 90 | unlock( lock ); |
---|
| 91 | } |
---|
| 92 | else if( owner == kernelTLS.this_thread ) { |
---|
| 93 | recursion_count++; |
---|
| 94 | unlock( lock ); |
---|
| 95 | } |
---|
| 96 | else { |
---|
| 97 | append( blocked_threads, kernelTLS.this_thread ); |
---|
[3381ed7] | 98 | unlock( lock ); |
---|
[ae66348] | 99 | park( __cfaabi_dbg_ctx ); |
---|
[09800e9] | 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | bool try_lock(recursive_mutex_lock & this) with(this) { |
---|
| 104 | bool ret = false; |
---|
| 105 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[121be3e] | 106 | if( owner == 0p ) { |
---|
[09800e9] | 107 | owner = kernelTLS.this_thread; |
---|
| 108 | recursion_count = 1; |
---|
| 109 | ret = true; |
---|
| 110 | } |
---|
| 111 | else if( owner == kernelTLS.this_thread ) { |
---|
| 112 | recursion_count++; |
---|
| 113 | ret = true; |
---|
| 114 | } |
---|
| 115 | unlock( lock ); |
---|
| 116 | return ret; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void unlock(recursive_mutex_lock & this) with(this) { |
---|
| 120 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 121 | recursion_count--; |
---|
| 122 | if( recursion_count == 0 ) { |
---|
[ac2b598] | 123 | $thread * thrd = pop_head( blocked_threads ); |
---|
[09800e9] | 124 | owner = thrd; |
---|
| 125 | recursion_count = (thrd ? 1 : 0); |
---|
[ae66348] | 126 | unpark( thrd __cfaabi_dbg_ctx2 ); |
---|
[09800e9] | 127 | } |
---|
| 128 | unlock( lock ); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | //----------------------------------------------------------------------------- |
---|
| 132 | // Conditions |
---|
| 133 | void ?{}(condition_variable & this) { |
---|
| 134 | this.blocked_threads{}; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void ^?{}(condition_variable & this) { |
---|
| 138 | // default |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | void notify_one(condition_variable & this) with(this) { |
---|
| 142 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[3381ed7] | 143 | unpark( |
---|
[ae66348] | 144 | pop_head( this.blocked_threads ) __cfaabi_dbg_ctx2 |
---|
[09800e9] | 145 | ); |
---|
| 146 | unlock( lock ); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | void notify_all(condition_variable & this) with(this) { |
---|
| 150 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 151 | while(this.blocked_threads) { |
---|
[3381ed7] | 152 | unpark( |
---|
[ae66348] | 153 | pop_head( this.blocked_threads ) __cfaabi_dbg_ctx2 |
---|
[09800e9] | 154 | ); |
---|
| 155 | } |
---|
| 156 | unlock( lock ); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | void wait(condition_variable & this) { |
---|
| 160 | lock( this.lock __cfaabi_dbg_ctx2 ); |
---|
| 161 | append( this.blocked_threads, kernelTLS.this_thread ); |
---|
[3381ed7] | 162 | unlock( this.lock ); |
---|
[ae66348] | 163 | park( __cfaabi_dbg_ctx ); |
---|
[09800e9] | 164 | } |
---|
| 165 | |
---|
| 166 | forall(dtype L | is_lock(L)) |
---|
| 167 | void wait(condition_variable & this, L & l) { |
---|
| 168 | lock( this.lock __cfaabi_dbg_ctx2 ); |
---|
| 169 | append( this.blocked_threads, kernelTLS.this_thread ); |
---|
[3381ed7] | 170 | unlock(l); |
---|
| 171 | unlock(this.lock); |
---|
[ae66348] | 172 | park( __cfaabi_dbg_ctx ); |
---|
[8cc7dd1] | 173 | lock(l); |
---|
[09800e9] | 174 | } |
---|
| 175 | |
---|
| 176 | //----------------------------------------------------------------------------- |
---|
| 177 | // Scopes |
---|
| 178 | forall(dtype L | is_lock(L)) |
---|
| 179 | void lock_all ( L * locks[], size_t count) { |
---|
| 180 | // Sort locks based on addresses |
---|
| 181 | __libcfa_small_sort(locks, count); |
---|
| 182 | |
---|
| 183 | // Lock all |
---|
| 184 | for(size_t i = 0; i < count; i++) { |
---|
| 185 | L * l = locks[i]; |
---|
| 186 | lock( *l ); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | forall(dtype L | is_lock(L)) |
---|
| 191 | void unlock_all( L * locks[], size_t count) { |
---|
| 192 | // Lock all |
---|
| 193 | for(size_t i = 0; i < count; i++) { |
---|
| 194 | L * l = locks[i]; |
---|
| 195 | unlock( *l ); |
---|
| 196 | } |
---|
[121be3e] | 197 | } |
---|