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

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

Cosmetic: improve debug.

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