source: trunk/kernel/libk/remote_busylock.c @ 676

Last change on this file since 676 was 666, checked in by alain, 4 years ago

Cosmetic.

File size: 4.7 KB
RevLine 
[563]1/*
2 * remote_busylock.c - remote kernel busy-waiting lock implementation.
3 *
[666]4 * Authors     Alain Greiner (2016,2017,2018,2019,2020)
[563]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_irqmask.h>
27#include <hal_special.h>
28#include <hal_remote.h>
29#include <thread.h>
30#include <remote_busylock.h>
31
32//////////////////////////////////////////////////////////////////////////////
33//                Extern global variables
34//////////////////////////////////////////////////////////////////////////////
35
36extern char               * lock_type_str[];    // allocated in kernel_init.c
37extern chdev_directory_t    chdev_dir;          // allocated in kernel_init.c
38
39
40////////////////////////////////////////////
41void remote_busylock_init( xptr_t   lock_xp,
42                           uint32_t type )
43{
44    // get remote lock cluster and local pointer
45    cxy_t        lock_cxy = GET_CXY( lock_xp );
46    busylock_t * lock_ptr = GET_PTR( lock_xp );
47
48    hal_remote_s32( XPTR( lock_cxy , &lock_ptr->ticket  ) , 0 );
49    hal_remote_s32( XPTR( lock_cxy , &lock_ptr->current ) , 0 );
50    hal_remote_s32( XPTR( lock_cxy , &lock_ptr->type    ) , type );
51
[666]52#if DEBUG_BUSYLOCK_TYPE
[563]53    xlist_entry_init( XPTR( lock_cxy , &lock_ptr->xlist ) ); 
54#endif
55
56}
57
58////////////////////////////////////////////////
59void remote_busylock_acquire( xptr_t   lock_xp )
60{
61    reg_t      save_sr;
62    thread_t * this = CURRENT_THREAD;
63
64    // get remote lock cluster and local pointer
65    cxy_t        lock_cxy = GET_CXY( lock_xp );
66    busylock_t * lock_ptr = GET_PTR( lock_xp );
67
68    // enter critical section
69    hal_disable_irq( &save_sr );
70 
71    // get one ticket
72    uint32_t ticket = hal_remote_atomic_add( XPTR( lock_cxy , &lock_ptr->ticket ) , 1 );
73
74    // poll current until success
75    xptr_t current_xp = XPTR( lock_cxy , &lock_ptr->current );
76    while( hal_remote_l32( current_xp ) != ticket ) asm volatile ("nop");
77
78    // increment thread locks counter
79    this->busylocks++;
80
81    // save SR in lock descriptor
82    hal_remote_s32( XPTR( lock_cxy , &lock_ptr->save_sr ) , save_sr );
83
84    // memory barrier to update busylock and thread state
85    hal_fence();
86   
[666]87#if DEBUG_BUSYLOCK_TYPE
[563]88uint32_t type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) );
[624]89if( type != LOCK_CHDEV_TXT0 )
[563]90{
[624]91    // update thread list of busyslocks
[563]92    xptr_t root_xp = XPTR( local_cxy , &this->busylocks_root );
93    xlist_add_last( root_xp , XPTR( lock_cxy  , &lock_ptr->xlist ) );
[580]94}
[666]95if( (type               == DEBUG_BUSYLOCK_TYPE) && 
[624]96    (this->process->pid == DEBUG_BUSYLOCK_PID) &&
[666]97    (this->trdid        == DEBUG_BUSYLOCK_TRDID) )
[580]98{
[600]99    printk("\n[%s] thread[%x,%x] ACQUIRE lock %s\n",
[619]100    __FUNCTION__, this->process->pid, this->trdid, lock_type_str[type] );
[563]101}
102#endif
103
104}  // end remote_busylock_acquire()
105
106///////////////////////////////////////////////
107void remote_busylock_release( xptr_t  lock_xp )
108{
109    thread_t * this = CURRENT_THREAD;
110
111    // memory barrier to update the protected object
112    hal_fence();
113
114    // get remote lock cluster and local pointer
115    cxy_t        lock_cxy = GET_CXY( lock_xp );
116    busylock_t * lock_ptr = GET_PTR( lock_xp );
117
118    // update lock state
119    hal_remote_atomic_add( XPTR( lock_cxy , &lock_ptr->current ) , 1 );
120
121    // decrement thread locks counter
122    this->busylocks--;
123
124    // memory barrier to update busylock and thread state
125    hal_fence();
126
[666]127#if DEBUG_BUSYLOCK_TYPE
[563]128uint32_t type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) );
[624]129if( type != LOCK_CHDEV_TXT0 ) 
[563]130{
131    // remove lock from thread list of busyslocks
132    xlist_unlink( XPTR( lock_cxy  , &lock_ptr->xlist ) );
[580]133}
[666]134if( (type               == DEBUG_BUSYLOCK_TYPE) && 
[624]135    (this->process->pid == DEBUG_BUSYLOCK_PID) &&
[666]136    (this->trdid        == DEBUG_BUSYLOCK_TRDID) )
[580]137{
[600]138    printk("\n[%s] thread[%x,%x] RELEASE lock %s\n",
139    __FUNCTION__, this->process->pid, this->trdid, lock_type_str[type] );
[563]140}
141#endif
142                                 
143    // exit critical section
144    hal_restore_irq( hal_remote_l32( XPTR( lock_cxy , &lock_ptr->save_sr ) ) );
145 
146}  // end remote_busylock_release()
147
148
Note: See TracBrowser for help on using the repository browser.