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

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

Improve the FAT32 file system to support cat, rm, cp commands.

File size: 4.6 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
48thread_t * this = CURRENT_THREAD;
49if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
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
73if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
74printk("\n[%s ] thread[%x,%x] BLOCK on q_lock %s [%x,%x]\n",
75__FUNCTION__, this->process->pid, this->trdid,
76lock_type_str[lock->lock.type], local_cxy, lock );
77#endif
78        // get pointer on calling thread
79        thread_t * this = CURRENT_THREAD;
80
81        // register calling thread in waiting queue
82        list_add_last( &lock->root , &this->wait_list );
83
84        // block calling thread
85        thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_LOCK );
86       
87        // release busylock
88        busylock_release( &lock->lock );
89
90        // deschedule
91        sched_yield("reader wait queuelock");
92       
93        // get busylock
94        busylock_acquire( &lock->lock );
95    }
96
97#if DEBUG_QUEUELOCK
98if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
99printk("\n[%s] thread[%x,%x] ACQUIRE q_lock %s [%x,%x]\n",
100__FUNCTION__, this->process->pid, this->trdid,
101lock_type_str[lock->lock.type], local_cxy, lock );
102#endif
103
104    // update queuelock state
105    lock->taken = 1;
106
107    // release busylock
108    busylock_release( &lock->lock );
109
110}  // end queuelock_acquire()
111
112////////////////////////////////////////////
113void queuelock_release( queuelock_t * lock )
114{
115    // memory barrier before lock release
116    hal_fence();
117
118    // get busylock protecting access to queuelock state
119    busylock_acquire( &lock->lock );
120
121#if DEBUG_QUEUELOCK
122thread_t * this = CURRENT_THREAD;
123if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
124printk("\n[%s] thread[%x,%x] RELEASE q_lock %s [%x,%x]\n",
125__FUNCTION__, this->process->pid, this->trdid,
126lock_type_str[lock->lock.type], local_cxy, lock );
127#endif
128
129    // update queuelock state
130    lock->taken = 0;
131
132    // unblock first waiting thread if waiting list not empty
133    if( list_is_empty( &lock->root ) == false )
134    {
135        // get first waiting thread
136        thread_t * thread = LIST_FIRST( &lock->root , thread_t , wait_list );
137
138#if DEBUG_QUEUELOCK
139if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
140printk("\n[%s] thread[%x,%x] UNBLOCK thread [%x,%x] / q_lock %s [%x,%x]\n",
141__FUNCTION__, this->process->pid, this->trdid, thread->process->pid, thread->trdid,
142lock_type_str[lock->lock.type], local_cxy, lock );
143#endif
144        // remove this waiting thread from waiting list
145        list_unlink( &thread->wait_list );
146
147        // unblock this waiting thread
148        thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_LOCK );
149    }
150
151    // release busylock
152    busylock_release( &lock->lock );
153
154}  // end queuelock_release()
155
156
157
Note: See TracBrowser for help on using the repository browser.