1 | /* |
---|
2 | * queuelock.c - local kernel lock with waiting queue implementation. |
---|
3 | * |
---|
4 | * Authors Alain Greiner (2016,2017,2018) |
---|
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 ); |
---|
46 | |
---|
47 | #if DEBUG_QUEUELOCK_TYPE |
---|
48 | thread_t * this = CURRENT_THREAD; |
---|
49 | if( DEBUG_QUEUELOCK_TYPE == type ) |
---|
50 | printk("\n[%s] thread[%x,%x] initialise lock %s [%x,%x]\n", |
---|
51 | __FUNCTION__, this->process->pid, this->trdid, |
---|
52 | lock_type_str[type], local_cxy, lock ); |
---|
53 | #endif |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | //////////////////////////////////////////// |
---|
58 | void queuelock_acquire( queuelock_t * lock ) |
---|
59 | { |
---|
60 | thread_t * this = CURRENT_THREAD; |
---|
61 | |
---|
62 | // check calling thread can yield |
---|
63 | thread_assert_can_yield( this , __FUNCTION__ ); |
---|
64 | |
---|
65 | // get busylock protecting access to queuelock state |
---|
66 | busylock_acquire( &lock->lock ); |
---|
67 | |
---|
68 | // block and deschedule if lock already taken |
---|
69 | while( lock->taken ) |
---|
70 | { |
---|
71 | |
---|
72 | #if DEBUG_QUEUELOCK_TYPE |
---|
73 | uint32_t lock_type = lock->lock.type; |
---|
74 | if( DEBUG_QUEUELOCK_TYPE == lock_type ) |
---|
75 | printk("\n[%s ] thread[%x,%x] BLOCK on q_lock %s [%x,%x]\n", |
---|
76 | __FUNCTION__, this->process->pid, this->trdid, |
---|
77 | lock_type_str[lock_type], local_cxy, lock ); |
---|
78 | #endif |
---|
79 | // get pointer on calling thread |
---|
80 | thread_t * this = CURRENT_THREAD; |
---|
81 | |
---|
82 | // register calling thread in waiting queue |
---|
83 | list_add_last( &lock->root , &this->wait_list ); |
---|
84 | |
---|
85 | // block calling thread |
---|
86 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_LOCK ); |
---|
87 | |
---|
88 | // release busylock |
---|
89 | busylock_release( &lock->lock ); |
---|
90 | |
---|
91 | // deschedule |
---|
92 | sched_yield("reader wait queuelock"); |
---|
93 | |
---|
94 | // get busylock |
---|
95 | busylock_acquire( &lock->lock ); |
---|
96 | } |
---|
97 | |
---|
98 | #if DEBUG_QUEUELOCK_TYPE |
---|
99 | if( DEBUG_QUEUELOCK_TYPE == lock_type ) |
---|
100 | printk("\n[%s] thread[%x,%x] ACQUIRE q_lock %s [%x,%x]\n", |
---|
101 | __FUNCTION__, this->process->pid, this->trdid, |
---|
102 | lock_type_str[lock_type], local_cxy, lock ); |
---|
103 | #endif |
---|
104 | |
---|
105 | // update queuelock state |
---|
106 | lock->taken = 1; |
---|
107 | |
---|
108 | // release busylock |
---|
109 | busylock_release( &lock->lock ); |
---|
110 | |
---|
111 | } // end queuelock_acquire() |
---|
112 | |
---|
113 | //////////////////////////////////////////// |
---|
114 | void queuelock_release( queuelock_t * lock ) |
---|
115 | { |
---|
116 | // memory barrier before lock release |
---|
117 | hal_fence(); |
---|
118 | |
---|
119 | // get busylock protecting access to queuelock state |
---|
120 | busylock_acquire( &lock->lock ); |
---|
121 | |
---|
122 | #if DEBUG_QUEUELOCK_TYPE |
---|
123 | uint32_t lock_type = lock->lock.type; |
---|
124 | thread_t * this = CURRENT_THREAD; |
---|
125 | if( DEBUG_QUEUELOCK_TYPE == lock_type ) |
---|
126 | printk("\n[%s] thread[%x,%x] RELEASE q_lock %s [%x,%x]\n", |
---|
127 | __FUNCTION__, this->process->pid, this->trdid, |
---|
128 | lock_type_str[lock_type], local_cxy, lock ); |
---|
129 | #endif |
---|
130 | |
---|
131 | // update queuelock state |
---|
132 | lock->taken = 0; |
---|
133 | |
---|
134 | // unblock first waiting thread if waiting list not empty |
---|
135 | if( list_is_empty( &lock->root ) == false ) |
---|
136 | { |
---|
137 | // get first waiting thread |
---|
138 | thread_t * thread = LIST_FIRST( &lock->root , thread_t , wait_list ); |
---|
139 | |
---|
140 | #if DEBUG_QUEUELOCK_TYPE |
---|
141 | if( DEBUG_QUEUELOCK_TYPE == lock_type ) |
---|
142 | printk("\n[%s] thread[%x,%x] UNBLOCK thread [%x,%x] / q_lock %s [%x,%x]\n", |
---|
143 | __FUNCTION__, this->process->pid, this->trdid, thread->process->pid, thread->trdid, |
---|
144 | lock_type_str[lock_type], local_cxy, lock ); |
---|
145 | #endif |
---|
146 | // remove this waiting thread from waiting list |
---|
147 | list_unlink( &thread->wait_list ); |
---|
148 | |
---|
149 | // unblock this waiting thread |
---|
150 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_LOCK ); |
---|
151 | } |
---|
152 | |
---|
153 | // release busylock |
---|
154 | busylock_release( &lock->lock ); |
---|
155 | |
---|
156 | } // end queuelock_release() |
---|
157 | |
---|
158 | |
---|
159 | |
---|