[563] | 1 | /* |
---|
| 2 | * queuelock.c - local kernel lock with waiting queue implementation. |
---|
| 3 | * |
---|
[629] | 4 | * Authors Alain Greiner (2016,2017,2018,2019) |
---|
[563] | 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_kernel_types.h> |
---|
| 26 | #include <hal_atomic.h> |
---|
| 27 | #include <thread.h> |
---|
| 28 | #include <scheduler.h> |
---|
| 29 | #include <busylock.h> |
---|
| 30 | #include <queuelock.h> |
---|
| 31 | |
---|
| 32 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 33 | // Extern global variables |
---|
| 34 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 35 | |
---|
| 36 | extern char * lock_type_str[]; // allocated in kernel_init.c |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | //////////////////////////////////////// |
---|
| 40 | void queuelock_init( queuelock_t * lock, |
---|
| 41 | uint32_t type ) |
---|
| 42 | { |
---|
| 43 | lock->taken = 0; |
---|
| 44 | list_root_init( &lock->root ); |
---|
| 45 | busylock_init( &lock->lock , type ); |
---|
[603] | 46 | |
---|
[610] | 47 | #if DEBUG_QUEUELOCK_TYPE |
---|
[603] | 48 | thread_t * this = CURRENT_THREAD; |
---|
[666] | 49 | bool_t cond = (type == DEBUG_QUEUELOCK_TYPE) && |
---|
| 50 | (((local_cxy == (cxy_t)DEBUG_QUEUELOCK_CXY) && |
---|
| 51 | (lock == (queuelock_t*)DEBUG_QUEUELOCK_PTR)) || |
---|
| 52 | ((DEBUG_QUEUELOCK_CXY == 0) && |
---|
| 53 | (DEBUG_QUEUELOCK_PTR == 0))); |
---|
| 54 | if( cond ) printk("\n[%s] thread[%x,%x] initialise lock %s [%x,%x]\n", |
---|
[603] | 55 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 56 | lock_type_str[type], local_cxy, lock ); |
---|
| 57 | #endif |
---|
| 58 | |
---|
[563] | 59 | } |
---|
| 60 | |
---|
| 61 | //////////////////////////////////////////// |
---|
| 62 | void queuelock_acquire( queuelock_t * lock ) |
---|
| 63 | { |
---|
| 64 | thread_t * this = CURRENT_THREAD; |
---|
| 65 | |
---|
| 66 | // check calling thread can yield |
---|
| 67 | thread_assert_can_yield( this , __FUNCTION__ ); |
---|
| 68 | |
---|
| 69 | // get busylock protecting access to queuelock state |
---|
| 70 | busylock_acquire( &lock->lock ); |
---|
| 71 | |
---|
[623] | 72 | #if DEBUG_QUEUELOCK_TYPE |
---|
| 73 | uint32_t lock_type = lock->lock.type; |
---|
[666] | 74 | bool_t cond = (lock_type == DEBUG_QUEUELOCK_TYPE) && |
---|
| 75 | (((local_cxy == (cxy_t)DEBUG_QUEUELOCK_CXY) && |
---|
| 76 | (lock == (queuelock_t*)DEBUG_QUEUELOCK_PTR)) || |
---|
| 77 | ((DEBUG_QUEUELOCK_CXY == 0) && |
---|
| 78 | (DEBUG_QUEUELOCK_PTR == 0))); |
---|
[623] | 79 | #endif |
---|
| 80 | |
---|
[563] | 81 | // block and deschedule if lock already taken |
---|
| 82 | while( lock->taken ) |
---|
| 83 | { |
---|
| 84 | |
---|
[610] | 85 | #if DEBUG_QUEUELOCK_TYPE |
---|
[666] | 86 | if( cond ) printk("\n[%s ] thread[%x,%x] BLOCK on q_lock %s [%x,%x]\n", |
---|
[603] | 87 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 88 | lock_type_str[lock_type], local_cxy, lock ); |
---|
[563] | 89 | #endif |
---|
| 90 | // get pointer on calling thread |
---|
| 91 | thread_t * this = CURRENT_THREAD; |
---|
| 92 | |
---|
| 93 | // register calling thread in waiting queue |
---|
| 94 | list_add_last( &lock->root , &this->wait_list ); |
---|
| 95 | |
---|
| 96 | // block calling thread |
---|
| 97 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_LOCK ); |
---|
| 98 | |
---|
| 99 | // release busylock |
---|
| 100 | busylock_release( &lock->lock ); |
---|
| 101 | |
---|
| 102 | // deschedule |
---|
| 103 | sched_yield("reader wait queuelock"); |
---|
| 104 | |
---|
| 105 | // get busylock |
---|
| 106 | busylock_acquire( &lock->lock ); |
---|
| 107 | } |
---|
| 108 | |
---|
[610] | 109 | #if DEBUG_QUEUELOCK_TYPE |
---|
[666] | 110 | if( cond ) printk("\n[%s] thread[%x,%x] ACQUIRE q_lock %s [%x,%x]\n", |
---|
[603] | 111 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 112 | lock_type_str[lock_type], local_cxy, lock ); |
---|
[563] | 113 | #endif |
---|
| 114 | |
---|
| 115 | // update queuelock state |
---|
| 116 | lock->taken = 1; |
---|
| 117 | |
---|
| 118 | // release busylock |
---|
| 119 | busylock_release( &lock->lock ); |
---|
| 120 | |
---|
| 121 | } // end queuelock_acquire() |
---|
| 122 | |
---|
| 123 | //////////////////////////////////////////// |
---|
| 124 | void queuelock_release( queuelock_t * lock ) |
---|
| 125 | { |
---|
| 126 | // memory barrier before lock release |
---|
| 127 | hal_fence(); |
---|
| 128 | |
---|
| 129 | // get busylock protecting access to queuelock state |
---|
| 130 | busylock_acquire( &lock->lock ); |
---|
| 131 | |
---|
[610] | 132 | #if DEBUG_QUEUELOCK_TYPE |
---|
| 133 | uint32_t lock_type = lock->lock.type; |
---|
| 134 | thread_t * this = CURRENT_THREAD; |
---|
[666] | 135 | bool_t cond = (lock_type == DEBUG_QUEUELOCK_TYPE) && |
---|
| 136 | (((local_cxy == (cxy_t)DEBUG_QUEUELOCK_CXY) && |
---|
| 137 | (lock == (queuelock_t*)DEBUG_QUEUELOCK_PTR)) || |
---|
| 138 | ((DEBUG_QUEUELOCK_CXY == 0) && |
---|
| 139 | (DEBUG_QUEUELOCK_PTR == 0))); |
---|
| 140 | #endif |
---|
| 141 | |
---|
| 142 | #if DEBUG_QUEUELOCK_TYPE |
---|
| 143 | if( cond ) printk("\n[%s] thread[%x,%x] RELEASE q_lock %s [%x,%x]\n", |
---|
[603] | 144 | __FUNCTION__, this->process->pid, this->trdid, |
---|
[610] | 145 | lock_type_str[lock_type], local_cxy, lock ); |
---|
[563] | 146 | #endif |
---|
| 147 | |
---|
| 148 | // update queuelock state |
---|
| 149 | lock->taken = 0; |
---|
| 150 | |
---|
| 151 | // unblock first waiting thread if waiting list not empty |
---|
| 152 | if( list_is_empty( &lock->root ) == false ) |
---|
| 153 | { |
---|
| 154 | // get first waiting thread |
---|
| 155 | thread_t * thread = LIST_FIRST( &lock->root , thread_t , wait_list ); |
---|
| 156 | |
---|
[610] | 157 | #if DEBUG_QUEUELOCK_TYPE |
---|
[666] | 158 | if( cond ) printk("\n[%s] thread[%x,%x] UNBLOCK thread [%x,%x] / q_lock %s [%x,%x]\n", |
---|
[600] | 159 | __FUNCTION__, this->process->pid, this->trdid, thread->process->pid, thread->trdid, |
---|
[610] | 160 | lock_type_str[lock_type], local_cxy, lock ); |
---|
[563] | 161 | #endif |
---|
| 162 | // remove this waiting thread from waiting list |
---|
| 163 | list_unlink( &thread->wait_list ); |
---|
| 164 | |
---|
| 165 | // unblock this waiting thread |
---|
| 166 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_LOCK ); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // release busylock |
---|
| 170 | busylock_release( &lock->lock ); |
---|
| 171 | |
---|
| 172 | } // end queuelock_release() |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | |
---|