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

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

Remove the "giant" rwlock protecting the GPT, and
use the GPT_LOCKED attribute in each PTE to prevent
concurrent modifications of one GPT entry.
The version number has been incremented to 2.1.

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