| 1 | /* |
|---|
| 2 | * rwlock.c - kernel read/write lock synchronization. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016} |
|---|
| 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 | |
|---|
| 24 | #include <kernel_config.h> |
|---|
| 25 | #include <hal_types.h> |
|---|
| 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 | |
|---|
| 33 | /////////////////////////////////////// |
|---|
| 34 | void rwlock_init( rwlock_t * lock ) |
|---|
| 35 | { |
|---|
| 36 | lock->ticket = 0; |
|---|
| 37 | lock->current = 0; |
|---|
| 38 | lock->count = 0; |
|---|
| 39 | |
|---|
| 40 | #if CONFIG_DEBUG_LOCKS |
|---|
| 41 | lock->owner = NULL; |
|---|
| 42 | list_entry_init( &lock->list ); |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | ////////////////////////////////////// |
|---|
| 48 | void rwlock_rd_lock( rwlock_t * lock ) |
|---|
| 49 | { |
|---|
| 50 | reg_t mode; |
|---|
| 51 | uint32_t ticket; |
|---|
| 52 | thread_t * this = CURRENT_THREAD; |
|---|
| 53 | |
|---|
| 54 | // disable IRQs |
|---|
| 55 | hal_disable_irq( &mode ); |
|---|
| 56 | |
|---|
| 57 | // get next free ticket |
|---|
| 58 | ticket = hal_atomic_add( &lock->ticket , 1 ); |
|---|
| 59 | |
|---|
| 60 | // poll the current ticket value |
|---|
| 61 | while( lock->current != ticket ) |
|---|
| 62 | { |
|---|
| 63 | hal_fixed_delay( CONFIG_RWLOCK_DELAY ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | ////////// From here we have the lock //////////// |
|---|
| 67 | |
|---|
| 68 | // increment number of readers |
|---|
| 69 | lock->count++; |
|---|
| 70 | this->local_locks++; |
|---|
| 71 | |
|---|
| 72 | #if CONFIG_DEBUG_LOCKS |
|---|
| 73 | list_add_first( &this->locks_root , &lock->list ); |
|---|
| 74 | #endif |
|---|
| 75 | |
|---|
| 76 | // consistency |
|---|
| 77 | hal_fence(); |
|---|
| 78 | |
|---|
| 79 | // release the lock to allow several simultaneous readers |
|---|
| 80 | lock->current++; |
|---|
| 81 | |
|---|
| 82 | // enable IRQs |
|---|
| 83 | hal_restore_irq( mode ); |
|---|
| 84 | |
|---|
| 85 | } // end rwlock_rd_lock() |
|---|
| 86 | |
|---|
| 87 | //////////////////////////////////////// |
|---|
| 88 | void rwlock_rd_unlock( rwlock_t * lock ) |
|---|
| 89 | { |
|---|
| 90 | reg_t mode; |
|---|
| 91 | thread_t * this = CURRENT_THREAD; |
|---|
| 92 | |
|---|
| 93 | // disable IRQs |
|---|
| 94 | hal_disable_irq( &mode ); |
|---|
| 95 | |
|---|
| 96 | // decrement number of readers |
|---|
| 97 | hal_atomic_add( &lock->count , -1 ); |
|---|
| 98 | this->local_locks--; |
|---|
| 99 | |
|---|
| 100 | #if CONFIG_DEBUG_LOCKS |
|---|
| 101 | list_unlink( &lock->list ); |
|---|
| 102 | #endif |
|---|
| 103 | |
|---|
| 104 | // enable IRQs |
|---|
| 105 | hal_restore_irq( mode ); |
|---|
| 106 | |
|---|
| 107 | // deschedule if pending request |
|---|
| 108 | thread_check_sched(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | ////////////////////////////////////// |
|---|
| 112 | void rwlock_wr_lock( rwlock_t * lock ) |
|---|
| 113 | { |
|---|
| 114 | reg_t mode; |
|---|
| 115 | uint32_t ticket; |
|---|
| 116 | thread_t * this = CURRENT_THREAD; |
|---|
| 117 | |
|---|
| 118 | // disable IRQs |
|---|
| 119 | hal_disable_irq( &mode ); |
|---|
| 120 | |
|---|
| 121 | // get next free ticket |
|---|
| 122 | ticket = hal_atomic_add( &lock->ticket , 1 ); |
|---|
| 123 | |
|---|
| 124 | // poll the current ticket value |
|---|
| 125 | while( lock->current != ticket ) |
|---|
| 126 | { |
|---|
| 127 | hal_fixed_delay( CONFIG_RWLOCK_DELAY ); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | ////////// From here we have the lock //////////// |
|---|
| 131 | |
|---|
| 132 | // wait completion of existing read access |
|---|
| 133 | while( lock->count != 0 ) |
|---|
| 134 | { |
|---|
| 135 | hal_fixed_delay( CONFIG_RWLOCK_DELAY ); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | this->local_locks++; |
|---|
| 139 | |
|---|
| 140 | #if CONFIG_DEBUG_LOCKS |
|---|
| 141 | lock->owner = this; |
|---|
| 142 | list_add_first( &this->locks_root , &lock->list ); |
|---|
| 143 | #endif |
|---|
| 144 | |
|---|
| 145 | // enable IRQs |
|---|
| 146 | hal_restore_irq( mode ); |
|---|
| 147 | |
|---|
| 148 | } // end rwlock_wr_lock() |
|---|
| 149 | |
|---|
| 150 | //////////////////////////////////////////// |
|---|
| 151 | void rwlock_wr_unlock( rwlock_t * lock ) |
|---|
| 152 | { |
|---|
| 153 | reg_t mode; |
|---|
| 154 | thread_t * this = CURRENT_THREAD; |
|---|
| 155 | |
|---|
| 156 | // disable IRQs |
|---|
| 157 | hal_disable_irq( &mode ); |
|---|
| 158 | |
|---|
| 159 | #if CONFIG_DEBUG_LOCKS |
|---|
| 160 | lock->owner = NULL; |
|---|
| 161 | list_unlink( &lock->list ); |
|---|
| 162 | #endif |
|---|
| 163 | |
|---|
| 164 | // release lock |
|---|
| 165 | lock->current++; |
|---|
| 166 | this->local_locks--; |
|---|
| 167 | |
|---|
| 168 | // enable IRQs |
|---|
| 169 | hal_restore_irq( mode ); |
|---|
| 170 | |
|---|
| 171 | // deschedule if pending request |
|---|
| 172 | thread_check_sched(); |
|---|
| 173 | } |
|---|
| 174 | |
|---|