[1] | 1 | /* |
---|
[563] | 2 | * rwlock.c - kernel local read/write lock implementation. |
---|
[1] | 3 | * |
---|
[436] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[1] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <hal_atomic.h> |
---|
| 27 | #include <hal_special.h> |
---|
| 28 | #include <hal_irqmask.h> |
---|
| 29 | #include <thread.h> |
---|
| 30 | #include <printk.h> |
---|
| 31 | #include <rwlock.h> |
---|
| 32 | |
---|
[563] | 33 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 34 | // Extern global variables |
---|
| 35 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | |
---|
| 37 | extern char * lock_type_str[]; // allocated in kernel_init.c |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | ////////////////////////////////// |
---|
| 41 | void rwlock_init( rwlock_t * lock, |
---|
| 42 | uint32_t type ) |
---|
[1] | 43 | { |
---|
[563] | 44 | lock->taken = 0; |
---|
[1] | 45 | lock->count = 0; |
---|
[409] | 46 | |
---|
[563] | 47 | list_root_init( &lock->rd_root ); |
---|
| 48 | list_root_init( &lock->wr_root ); |
---|
[409] | 49 | |
---|
[563] | 50 | busylock_init( &lock->lock , type ); |
---|
[603] | 51 | |
---|
[610] | 52 | #if DEBUG_RWLOCK_TYPE |
---|
[603] | 53 | thread_t * this = CURRENT_THREAD; |
---|
[610] | 54 | if( DEBUG_RWLOCK_TYPE == type ) |
---|
[603] | 55 | printk("\n[%s] thread[%x,%x] initialise lock %s [%x,%x]\n", |
---|
| 56 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 57 | lock_type_str[type], local_cxy, lock ); |
---|
| 58 | #endif |
---|
| 59 | |
---|
[1] | 60 | } |
---|
| 61 | |
---|
[563] | 62 | ///////////////////////////////////////// |
---|
| 63 | void rwlock_rd_acquire( rwlock_t * lock ) |
---|
[1] | 64 | { |
---|
[563] | 65 | thread_t * this = CURRENT_THREAD; |
---|
[1] | 66 | |
---|
[563] | 67 | // check calling thread can yield |
---|
| 68 | thread_assert_can_yield( this , __FUNCTION__ ); |
---|
[1] | 69 | |
---|
[563] | 70 | // get busylock |
---|
| 71 | busylock_acquire( &lock->lock ); |
---|
| 72 | |
---|
| 73 | // block and deschedule if lock already taken |
---|
| 74 | while( lock->taken ) |
---|
[1] | 75 | { |
---|
[563] | 76 | |
---|
[610] | 77 | #if DEBUG_RWLOCK_TYPE |
---|
| 78 | uint32_t lock_type = lock->lock.type; |
---|
| 79 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[603] | 80 | printk("\n[%s] thread[%x,%x] READ BLOCK on rwlock %s [%x,%x] / taken %d / count %d\n", |
---|
[600] | 81 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 82 | lock_type_str[lock_type], local_cxy, lock, lock->taken, lock->count ); |
---|
[563] | 83 | #endif |
---|
| 84 | // register reader thread in waiting queue |
---|
| 85 | list_add_last( &lock->rd_root , &this->wait_list ); |
---|
| 86 | |
---|
| 87 | // block reader thread |
---|
| 88 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_LOCK ); |
---|
| 89 | |
---|
| 90 | // release busylock |
---|
| 91 | busylock_release( &lock->lock ); |
---|
| 92 | |
---|
| 93 | // deschedule |
---|
| 94 | sched_yield("reader wait rwlock"); |
---|
| 95 | |
---|
| 96 | // get busylock |
---|
| 97 | busylock_acquire( &lock->lock ); |
---|
[1] | 98 | } |
---|
| 99 | |
---|
[603] | 100 | // increment number of readers |
---|
| 101 | lock->count++; |
---|
| 102 | |
---|
[610] | 103 | #if DEBUG_RWLOCK_TYPE |
---|
| 104 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[603] | 105 | printk("\n[%s] thread[%x,%x] READ ACQUIRE rwlock %s [%x,%x] / taken %d / count %d\n", |
---|
[600] | 106 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 107 | lock_type_str[lock_type], local_cxy, lock, lock->taken, lock->count ); |
---|
[563] | 108 | #endif |
---|
[1] | 109 | |
---|
[563] | 110 | // release busylock |
---|
| 111 | busylock_release( &lock->lock ); |
---|
[409] | 112 | |
---|
[563] | 113 | } // end rwlock_rd_acquire() |
---|
[1] | 114 | |
---|
[563] | 115 | ///////////////////////////////////////// |
---|
| 116 | void rwlock_wr_acquire( rwlock_t * lock ) |
---|
| 117 | { |
---|
| 118 | thread_t * this = CURRENT_THREAD; |
---|
[1] | 119 | |
---|
[563] | 120 | // check calling thread can yield |
---|
| 121 | thread_assert_can_yield( this , __FUNCTION__ ); |
---|
[1] | 122 | |
---|
[563] | 123 | // get busylock |
---|
| 124 | busylock_acquire( &lock->lock ); |
---|
[1] | 125 | |
---|
[563] | 126 | // block and deschedule if lock already taken or existing read access |
---|
| 127 | while( lock->taken || lock->count ) |
---|
| 128 | { |
---|
| 129 | |
---|
[610] | 130 | #if DEBUG_RWLOCK_TYPE |
---|
| 131 | uint32_t lock_type = lock->lock.type; |
---|
| 132 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[603] | 133 | printk("\n[%s] thread[%x,%x] WRITE BLOCK on rwlock %s [%x,%x] / taken %d / count %d\n", |
---|
[600] | 134 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 135 | lock_type_str[lock_type], local_cxy, lock, lock->taken, lock->count ); |
---|
[563] | 136 | #endif |
---|
| 137 | // register writer in waiting queue |
---|
| 138 | list_add_last( &lock->wr_root , &this->wait_list ); |
---|
[1] | 139 | |
---|
[603] | 140 | // block writer thread |
---|
[563] | 141 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_LOCK ); |
---|
| 142 | |
---|
| 143 | // release busylock |
---|
| 144 | busylock_release( &lock->lock ); |
---|
| 145 | |
---|
| 146 | // deschedule |
---|
| 147 | sched_yield("writer wait rwlock"); |
---|
| 148 | |
---|
| 149 | // get busylock |
---|
| 150 | busylock_acquire( &lock->lock ); |
---|
| 151 | } |
---|
| 152 | |
---|
[603] | 153 | // take the rwlock |
---|
| 154 | lock->taken = 1; |
---|
| 155 | |
---|
[610] | 156 | #if DEBUG_RWLOCK_TYPE |
---|
| 157 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[603] | 158 | printk("\n[%s] thread[%x,%x] WRITE ACQUIRE rwlock %s [%x,%x] / taken %d / count %d\n", |
---|
[600] | 159 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 160 | lock_type_str[lock_type], local_cxy, lock, lock->taken, lock->count ); |
---|
[409] | 161 | #endif |
---|
| 162 | |
---|
[563] | 163 | // release busylock |
---|
| 164 | busylock_release( &lock->lock ); |
---|
| 165 | |
---|
| 166 | } // end rwlock_wr_acquire() |
---|
| 167 | |
---|
| 168 | ///////////////////////////////////////// |
---|
| 169 | void rwlock_rd_release( rwlock_t * lock ) |
---|
| 170 | { |
---|
| 171 | // synchronize memory before lock release |
---|
| 172 | hal_fence(); |
---|
| 173 | |
---|
| 174 | // get busylock |
---|
| 175 | busylock_acquire( &lock->lock ); |
---|
| 176 | |
---|
[603] | 177 | // decrement number of readers |
---|
| 178 | lock->count--; |
---|
| 179 | |
---|
[610] | 180 | #if DEBUG_RWLOCK_TYPE |
---|
[600] | 181 | thread_t * this = CURRENT_THREAD; |
---|
[610] | 182 | uint32_t lock_type = lock->lock.type; |
---|
| 183 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[603] | 184 | printk("\n[%s] thread[%x,%x] READ RELEASE rwlock %s [%x,%x] / taken %d / count %d\n", |
---|
[600] | 185 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 186 | lock_type_str[lock_type], local_cxy, lock, lock->taken, lock->count ); |
---|
[563] | 187 | #endif |
---|
[1] | 188 | |
---|
[563] | 189 | // release first writer in waiting queue if no current readers |
---|
| 190 | // and writers waiting queue non empty |
---|
| 191 | if( (lock->count == 0) && (list_is_empty( &lock->wr_root ) == false) ) |
---|
| 192 | { |
---|
| 193 | // get first writer thread |
---|
| 194 | thread_t * thread = LIST_FIRST( &lock->wr_root , thread_t , wait_list ); |
---|
| 195 | |
---|
[610] | 196 | #if DEBUG_RWLOCK_TYPE |
---|
| 197 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[600] | 198 | printk("\n[%s] thread[%x,%x] UNBLOCK thread[%x,%x] / rwlock %s [%x,%x]\n", |
---|
| 199 | __FUNCTION__, this->process->pid, this->trdid, thread->process->pid, thread->trdid, |
---|
[610] | 200 | lock_type_str[lock_type], local_cxy, lock ); |
---|
[563] | 201 | #endif |
---|
[1] | 202 | |
---|
[563] | 203 | // remove this waiting thread from waiting list |
---|
| 204 | list_unlink( &thread->wait_list ); |
---|
| 205 | |
---|
| 206 | // unblock this waiting thread |
---|
| 207 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_LOCK ); |
---|
| 208 | } |
---|
| 209 | // release all readers in waiting queue if writers waiting queue empty |
---|
| 210 | // and readers waiting queue non empty |
---|
| 211 | else if( list_is_empty( &lock->wr_root ) && (list_is_empty( &lock->rd_root ) == false) ) |
---|
[1] | 212 | { |
---|
[563] | 213 | while( list_is_empty( &lock->rd_root ) == false ) |
---|
| 214 | { |
---|
| 215 | // get first reader thread |
---|
| 216 | thread_t * thread = LIST_FIRST( &lock->wr_root , thread_t , wait_list ); |
---|
[1] | 217 | |
---|
[610] | 218 | #if DEBUG_RWLOCK_TYPE |
---|
| 219 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[600] | 220 | printk("\n[%s] thread[%x,%x] UNBLOCK thread[%x,%x] / rwlock %s [%x,%x]\n", |
---|
| 221 | __FUNCTION__, this->process->pid, this->trdid, thread->process->pid, thread->trdid, |
---|
[610] | 222 | lock_type_str[lock_type], local_cxy, lock ); |
---|
[563] | 223 | #endif |
---|
| 224 | |
---|
| 225 | // remove this waiting thread from waiting list |
---|
| 226 | list_unlink( &thread->wait_list ); |
---|
[1] | 227 | |
---|
[563] | 228 | // unblock this waiting thread |
---|
| 229 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_LOCK ); |
---|
| 230 | } |
---|
[1] | 231 | } |
---|
| 232 | |
---|
[563] | 233 | // release busylock |
---|
| 234 | busylock_release( &lock->lock ); |
---|
[1] | 235 | |
---|
[563] | 236 | } // end rwlock_rd_release() |
---|
| 237 | |
---|
| 238 | ///////////////////////////////////////// |
---|
| 239 | void rwlock_wr_release( rwlock_t * lock ) |
---|
| 240 | { |
---|
| 241 | // synchronize memory before lock release |
---|
| 242 | hal_fence(); |
---|
| 243 | |
---|
| 244 | // get busylock |
---|
| 245 | busylock_acquire( &lock->lock ); |
---|
| 246 | |
---|
[603] | 247 | // release the rwlock |
---|
| 248 | lock->taken = 0; |
---|
| 249 | |
---|
[610] | 250 | #if DEBUG_RWLOCK_TYPE |
---|
[600] | 251 | thread_t * this = CURRENT_THREAD; |
---|
[610] | 252 | uint32_t lock_type = lock->lock.type; |
---|
| 253 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[603] | 254 | printk("\n[%s] thread[%x,%x] WRITE RELEASE rwlock %s [%x,%x] / taken %d / count %d\n", |
---|
[600] | 255 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 256 | lock_type_str[lock_type], local_cxy, lock, lock->taken, lock->count ); |
---|
[409] | 257 | #endif |
---|
| 258 | |
---|
[563] | 259 | // release first waiting writer thread if writers waiting queue non empty |
---|
| 260 | if( list_is_empty( &lock->wr_root ) == false ) |
---|
| 261 | { |
---|
| 262 | // get first writer thread |
---|
| 263 | thread_t * thread = LIST_FIRST( &lock->wr_root , thread_t , wait_list ); |
---|
[1] | 264 | |
---|
[610] | 265 | #if DEBUG_RWLOCK_TYPE |
---|
| 266 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[600] | 267 | printk("\n[%s] thread[%x,%x] UNBLOCK thread[%x,%x] / rwlock %s [%x,%x]\n", |
---|
| 268 | __FUNCTION__, this->process->pid, this->trdid, thread->process->pid, thread->trdid, |
---|
[610] | 269 | lock_type_str[lock_type], local_cxy, lock ); |
---|
[409] | 270 | #endif |
---|
[563] | 271 | // remove this waiting thread from waiting list |
---|
| 272 | list_unlink( &thread->wait_list ); |
---|
[409] | 273 | |
---|
[563] | 274 | // unblock this waiting thread |
---|
| 275 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_LOCK ); |
---|
| 276 | } |
---|
[337] | 277 | |
---|
[563] | 278 | // check readers waiting queue and release all if writers waiting queue empty |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | while( list_is_empty( &lock->rd_root ) == false ) |
---|
| 282 | { |
---|
| 283 | // get first reader thread |
---|
| 284 | thread_t * thread = LIST_FIRST( &lock->rd_root , thread_t , wait_list ); |
---|
| 285 | |
---|
[610] | 286 | #if DEBUG_RWLOCK_TYPE |
---|
| 287 | if( DEBUG_RWLOCK_TYPE == lock_type ) |
---|
[600] | 288 | printk("\n[%s] thread[%x,%x] UNBLOCK thread[%x,%x] / rwlock %s [%x,%x]\n", |
---|
| 289 | __FUNCTION__, this->process->pid, this->trdid, thread->process->pid, thread->trdid, |
---|
[610] | 290 | lock_type_str[lock_type], local_cxy, lock ); |
---|
[563] | 291 | #endif |
---|
| 292 | // remove this waiting thread from waiting list |
---|
| 293 | list_unlink( &thread->wait_list ); |
---|
[1] | 294 | |
---|
[563] | 295 | // unblock this waiting thread |
---|
| 296 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_LOCK ); |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | // release busylock |
---|
| 301 | busylock_release( &lock->lock ); |
---|
| 302 | |
---|
| 303 | } // end rwlock_wr_release() |
---|
| 304 | |
---|
| 305 | |
---|