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

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

Complete restructuration of kernel spinlocks.

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