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

Last change on this file since 666 was 666, checked in by alain, 14 months ago

Cosmetic.

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