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

Last change on this file since 610 was 610, checked in by alain, 3 years ago

Fix several bugs in VFS to support the following
ksh commandis : cp, mv, rm, mkdir, cd, pwd

File size: 4.7 KB
Line 
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
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( DEBUG_QUEUELOCK_TYPE == type )
50printk("\n[%s] thread[%x,%x] initialise lock %s [%x,%x]\n",
51__FUNCTION__, this->process->pid, this->trdid,
52lock_type_str[type], local_cxy, lock );
53#endif
54
55}
56
57////////////////////////////////////////////
58void 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
73uint32_t   lock_type = lock->lock.type;
74if( DEBUG_QUEUELOCK_TYPE == lock_type )
75printk("\n[%s ] thread[%x,%x] BLOCK on q_lock %s [%x,%x]\n",
76__FUNCTION__, this->process->pid, this->trdid,
77lock_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
99if( DEBUG_QUEUELOCK_TYPE == lock_type )
100printk("\n[%s] thread[%x,%x] ACQUIRE q_lock %s [%x,%x]\n",
101__FUNCTION__, this->process->pid, this->trdid,
102lock_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////////////////////////////////////////////
114void 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
123uint32_t   lock_type = lock->lock.type;
124thread_t * this      = CURRENT_THREAD;
125if( DEBUG_QUEUELOCK_TYPE == lock_type )
126printk("\n[%s] thread[%x,%x] RELEASE q_lock %s [%x,%x]\n",
127__FUNCTION__, this->process->pid, this->trdid,
128lock_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
141if( DEBUG_QUEUELOCK_TYPE == lock_type )
142printk("\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,
144lock_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
Note: See TracBrowser for help on using the repository browser.