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

Last change on this file since 608 was 600, checked in by alain, 6 years ago

Cosmetic: improve debug.

File size: 5.3 KB
Line 
1/*
2 * remote_busylock.c - remote kernel busy-waiting lock 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_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
52#if DEBUG_BUSYLOCK
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   
87#if DEBUG_BUSYLOCK
88uint32_t type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) );
89if( (type != LOCK_CHDEV_TXT0) &&
90    ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) )
91{
92    xptr_t root_xp = XPTR( local_cxy , &this->busylocks_root );
93
94    // update thread list of busyslocks
95    xlist_add_last( root_xp , XPTR( lock_cxy  , &lock_ptr->xlist ) );
96}
97#endif
98
99#if( DEBUG_BUSYLOCK && DEBUG_BUSYLOCK_THREAD_XP )
100if( (type != LOCK_CHDEV_TXT0) && 
101    (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) )
102{
103    // get cluster and local pointer of target thread
104    cxy_t      thread_cxy = GET_CXY( DEBUG_BUSYLOCK_THREAD_XP );
105    thread_t * thread_ptr = GET_PTR( DEBUG_BUSYLOCK_THREAD_XP );
106
107    // display message on kernel TXT0
108    printk("\n[%s] thread[%x,%x] ACQUIRE lock %s\n",
109    __FUNCTION_, this->process->pid, this->trdid, lock_type_str[type] );
110}
111#endif
112
113}  // end remote_busylock_acquire()
114
115///////////////////////////////////////////////
116void remote_busylock_release( xptr_t  lock_xp )
117{
118    thread_t * this = CURRENT_THREAD;
119
120    // memory barrier to update the protected object
121    hal_fence();
122
123    // get remote lock cluster and local pointer
124    cxy_t        lock_cxy = GET_CXY( lock_xp );
125    busylock_t * lock_ptr = GET_PTR( lock_xp );
126
127    // update lock state
128    hal_remote_atomic_add( XPTR( lock_cxy , &lock_ptr->current ) , 1 );
129
130    // decrement thread locks counter
131    this->busylocks--;
132
133    // memory barrier to update busylock and thread state
134    hal_fence();
135
136#if DEBUG_BUSYLOCK
137uint32_t type = hal_remote_l32( XPTR( lock_cxy , &lock_ptr->type ) );
138if( (type != LOCK_CHDEV_TXT0) && 
139    (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) &&
140    ((uint32_t)hal_get_cycles() > DEBUG_BUSYLOCK) )
141{
142    // remove lock from thread list of busyslocks
143    xlist_unlink( XPTR( lock_cxy  , &lock_ptr->xlist ) );
144}
145#endif
146
147#if (DEBUG_BUSYLOCK && DEBUG_BUSYLOCK_THREAD_XP )
148if( (type != LOCK_CHDEV_TXT0) && 
149    (XPTR( local_cxy , this ) == DEBUG_BUSYLOCK_THREAD_XP) )
150{
151    // get cluster and local pointer of target thread
152    cxy_t      thread_cxy = GET_CXY( DEBUG_BUSYLOCK_THREAD_XP );
153    thread_t * thread_ptr = GET_PTR( DEBUG_BUSYLOCK_THREAD_XP );
154
155    // display message on kernel TXT0
156    printk("\n[%s] thread[%x,%x] RELEASE lock %s\n",
157    __FUNCTION__, this->process->pid, this->trdid, lock_type_str[type] );
158}
159#endif
160                                 
161    // exit critical section
162    hal_restore_irq( hal_remote_l32( XPTR( lock_cxy , &lock_ptr->save_sr ) ) );
163 
164}  // end remote_busylock_release()
165
166
Note: See TracBrowser for help on using the repository browser.