source: trunk/kernel/libk/queuelock.c @ 653

Last change on this file since 653 was 629, checked in by alain, 6 years ago

Remove the "giant" rwlock protecting the GPT, and
use the GPT_LOCKED attribute in each PTE to prevent
concurrent modifications of one GPT entry.
The version number has been incremented to 2.1.

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