source: trunk/kernel/libk/remote_queuelock.c @ 601

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

Cosmetic: improve debug.

File size: 6.1 KB
Line 
1/*
2 * remote_queuelock.c - remote 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 <remote_queuelock.h>
31
32//////////////////////////////////////////////////////////////////////////////
33//                Extern global variables
34//////////////////////////////////////////////////////////////////////////////
35
36extern char * lock_type_str[];          // allocated in kernel_init.c
37
38
39/////////////////////////////////////////////
40void remote_queuelock_init( xptr_t   lock_xp,
41                            uint32_t type )
42{
43    // get remote lock cluster and local pointer
44    cxy_t                lock_cxy = GET_CXY( lock_xp );
45    remote_queuelock_t * lock_ptr = GET_PTR( lock_xp );
46
47    // initialise taken field
48    hal_remote_s32( XPTR( lock_cxy , &lock_ptr->taken ), 0 );
49
50    // initialise xroot field
51    xlist_root_init( XPTR( lock_cxy , &lock_ptr->xroot ) );
52
53    // initialise busylock field
54    remote_busylock_init( XPTR( lock_cxy , &lock_ptr->lock ) , type );
55}
56
57///////////////////////////////////////////////
58void remote_queuelock_acquire( xptr_t lock_xp )
59{
60    thread_t * this = CURRENT_THREAD;
61
62    // check calling thread can yield
63    thread_assert_can_yield( this , __FUNCTION__ );
64
65    // get lock cluster and local pointer
66    cxy_t                lock_cxy = GET_CXY( lock_xp );
67    remote_queuelock_t * lock_ptr = GET_PTR( lock_xp );
68
69#if DEBUG_QUEUELOCK
70uint32_t lock_type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->lock.type ) );
71#endif
72
73    // build extended pointer on busylock protecting queuelock
74    xptr_t busylock_xp = XPTR( lock_cxy , &lock_ptr->lock );
75
76    // get busylock
77    remote_busylock_acquire( busylock_xp );
78
79    // block and deschedule if lock already taken
80    while( hal_remote_l32( XPTR( lock_cxy, &lock_ptr->taken ) ) )
81    {
82
83#if DEBUG_QUEUELOCK
84if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
85printk("\n[%s] thread[%x,%x] BLOCK on q_lock %s [%x,%x]\n",
86__FUNCTION__, this->process->pid, this->trdid, 
87lock_type_str[lock_type], lock_cxy, lock_ptr );
88#endif
89        // get pointer on calling thread
90        thread_t * this = CURRENT_THREAD;
91
92        // block calling thread
93        thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_LOCK );
94
95        // register calling thread in waiting list
96        xlist_add_last( XPTR( lock_cxy  , &lock_ptr->xroot ),
97                        XPTR( local_cxy , &this->wait_xlist ) );
98
99        // release busylock
100        remote_busylock_release( busylock_xp );
101
102        // deschedule calling thread
103        sched_yield("wait remote_queuelock");
104
105        // get busylock
106        remote_busylock_acquire( busylock_xp );
107    }
108
109#if DEBUG_QUEUELOCK
110if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
111printk("\n[%s] thread[%x,%x] ACQUIRE q_lock %s [%x,%x]\n",
112__FUNCTION__, this->process->pid, this->trdid, 
113lock_type_str[lock_type], lock_cxy, lock_ptr );
114#endif
115
116    // update remote_queuelock state
117    hal_remote_s32( XPTR( lock_cxy , &lock_ptr->taken ) , 1 );
118
119    // release busylock
120    remote_busylock_release( busylock_xp );
121
122}  // end remote_queuelock_acquire()
123
124////////////////////////////////////////////////
125void remote_queuelock_release( xptr_t  lock_xp )
126{
127    // memory barrier before lock release
128    hal_fence();
129
130    // get lock cluster and local pointer
131    cxy_t                lock_cxy = GET_CXY( lock_xp );
132    remote_queuelock_t * lock_ptr = GET_PTR( lock_xp );
133
134    // build extended pointer on busylock protecting queuelock
135    xptr_t busylock_xp = XPTR( lock_cxy , &lock_ptr->lock );
136
137    // get busylock
138    remote_busylock_acquire( busylock_xp );
139
140#if DEBUG_QUEUELOCK
141thread_t * this      = CURRENT_THREAD;
142uint32_t   lock_type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->lock.type ) );
143if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
144printk("\n[%s] thread[%x,%x] RELEASE q_lock %s (%x,%x)\n",
145__FUNCTION__, this->process->pid, this->trdid,
146lock_type_str[lock_type], lock_cxy, lock_ptr );
147#endif
148
149    // update remote_queuelock state
150    hal_remote_s32( XPTR( lock_cxy , &lock_ptr->taken ) , 0 );
151
152    // unblock first waiting thread if waiting list not empty
153    if( xlist_is_empty( XPTR( lock_cxy, &lock_ptr->xroot ) ) == false )
154    {
155        // get extended pointer on first waiting thread
156        xptr_t root_xp   = XPTR( lock_cxy , &lock_ptr->xroot );
157        xptr_t     thread_xp  = XLIST_FIRST( root_xp , thread_t , wait_xlist );
158        cxy_t      thread_cxy = GET_CXY( thread_xp );
159        thread_t * thread_ptr = GET_PTR( thread_xp );
160
161#if DEBUG_QUEUELOCK
162if( DEBUG_QUEUELOCK < (uint32_t)hal_get_cycles() )
163{
164    trdid_t     trdid   = hal_remote_l32( XPTR( thread_cxy , &thread_ptr->trdid ) );
165    process_t * process = hal_remote_lpt( XPTR( thread_cxy , &thread_ptr->process ) );
166    pid_t       pid     = hal_remote_l32( XPTR( thread_cxy , &process->pid ) );
167    printk("\n[%s] thread[%x,%x] UNBLOCK thread[%x,%x] / q_lock %s [%x,%x]\n",
168    __FUNCTION__, this->process->pid, this->trdid, trdid, pid, 
169    lock_type_str[lock_type], lock_cxy, lock_ptr );
170}
171#endif
172
173        // remove this thread from waiting queue
174        xlist_unlink( XPTR( thread_cxy , &thread_ptr->wait_xlist ) );
175
176        // unblock this waiting thread
177        thread_unblock( thread_xp , THREAD_BLOCKED_LOCK );
178    }
179
180    // release busylock
181    remote_busylock_release( busylock_xp );
182
183}  // end remote_queuelock_release()
184
185
186
Note: See TracBrowser for help on using the repository browser.