source: trunk/kernel/libk/remote_spinlock.c @ 463

Last change on this file since 463 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 6.4 KB
RevLine 
[1]1/*
2 * remote_spinlock.c - kernel remote spinlock implementation.
[93]3 *
[436]4 * Authors   Alain   Greiner (2016,2017,2018)
[1]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
[457]24#include <hal_kernel_types.h>
[1]25#include <hal_remote.h>
26#include <hal_irqmask.h>
27#include <thread.h>
28#include <cluster.h>
29#include <scheduler.h>
30#include <remote_spinlock.h>
31
[11]32///////////////////////////////////////////
33void remote_spinlock_init( xptr_t lock_xp )
[93]34{
[423]35        remote_spinlock_t * ptr = GET_PTR( lock_xp );
[93]36        cxy_t               cxy = GET_CXY( lock_xp );
[1]37
[93]38        hal_remote_sw ( XPTR( cxy , &ptr->taken ) , 0 );
[409]39
[438]40#if DEBUG_REMOTE_SPINLOCKS
[436]41hal_remote_swd( XPTR( cxy , &ptr->owner ) , XPTR_NULL );
42xlist_entry_init( XPTR( cxy , &ptr->list ) );
[409]43#endif
44
[1]45}
46
[11]47/////////////////////////////////////////////////
48error_t remote_spinlock_trylock( xptr_t lock_xp )
[93]49{
[60]50        reg_t               mode;
[1]51        bool_t              isAtomic = false;
52
[93]53        // get cluster and local pointer on remote_spinlock
[423]54        remote_spinlock_t * lock_ptr = GET_PTR( lock_xp );
[93]55        cxy_t               lock_cxy = GET_CXY( lock_xp );
[1]56
[409]57        // get local pointer on local thread
[93]58        thread_t          * thread_ptr = CURRENT_THREAD;
[1]59
[93]60        // disable interrupts
61        hal_disable_irq( &mode );
[1]62
63        if( hal_remote_lw( XPTR( lock_cxy , &lock_ptr->taken ) ) == 0 )
[93]64        {
[1]65                isAtomic = hal_remote_atomic_cas( XPTR( lock_cxy , &lock_ptr->taken ) , 0 , 1 );
[93]66        }
[1]67
[93]68        if( isAtomic == false )    // failure
[1]69        {
70                hal_restore_irq( mode );
71                return 1;
72        }
[423]73        else                      // success : register lock in local thread
[93]74        {
75                thread_ptr->remote_locks++;
[1]76
[438]77#if DEBUG_REMOTE_SPINLOCKS
[436]78hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) ,
79                XPTR( local_cxy , thread_ptr) );
80xlist_add_first( XPTR( local_cxy , &thread_ptr->xlocks_root ) ,
81                         XPTR( lock_cxy , &lock_ptr->list ) );
[409]82#endif
[1]83
[93]84                hal_restore_irq(mode);
85                return 0;
86        }
[1]87}
88
[11]89///////////////////////////////////////////////////
90void remote_spinlock_lock_busy( xptr_t     lock_xp,
91                                uint32_t * irq_state )
[1]92{
[93]93        bool_t              isAtomic = false;
[60]94        reg_t               mode;
[93]95        volatile uint32_t   taken;
[1]96
[93]97        // get cluster and local pointer on remote_spinlock
[423]98        remote_spinlock_t * lock_ptr = GET_PTR( lock_xp );
[93]99        cxy_t               lock_cxy = GET_CXY( lock_xp );
[1]100
[409]101        // get local pointer on local thread
[93]102        thread_t          * thread_ptr = CURRENT_THREAD;
[1]103
[93]104        // disable interrupts
[1]105        hal_disable_irq( &mode );
[93]106
107        // loop until success
[1]108        while( isAtomic == false )
109        {
110                taken = hal_remote_lw( XPTR( lock_cxy , &lock_ptr->taken ) );
111
[93]112                // try to take the lock if not already taken
[11]113                if( taken == 0 )
114                {
[93]115                        isAtomic = hal_remote_atomic_cas( XPTR( lock_cxy , &lock_ptr->taken ) , 0 , 1 );
116                }
[11]117        }
118
[93]119        // register lock in thread
[11]120        thread_ptr->remote_locks++;
121
[438]122#if DEBUG_REMOTE_SPINLOCKS
[436]123hal_remote_swd( XPTR( lock_cxy  , &lock_ptr->owner ) ,
124                XPTR( local_cxy , thread_ptr) );
125xlist_add_first( XPTR( local_cxy , &thread_ptr->xlocks_root ) ,
126                 XPTR( lock_cxy  , &lock_ptr->list ) );
[409]127#endif
[11]128
[93]129        // irq_state must be restored when lock is released
130        *irq_state = mode;
[11]131
132}  // end remote_spinlock_lock_busy()
133
134////////////////////////////////////////////////////
135void remote_spinlock_unlock_busy( xptr_t    lock_xp,
136                                  uint32_t  irq_state )
137{
[93]138        // get cluster and local pointer on remote_spinlock
[423]139        remote_spinlock_t * lock_ptr = GET_PTR( lock_xp );
[93]140        cxy_t               lock_cxy = GET_CXY( lock_xp );
[11]141
[93]142        // get pointer on local thread
143        thread_t          * thread_ptr = CURRENT_THREAD;
[11]144
[438]145#if DEBUG_REMOTE_SPINLOCKS
[436]146hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) , XPTR_NULL );
147xlist_unlink( XPTR( lock_cxy , &lock_ptr->list ) );
[409]148#endif
149
[11]150        hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->taken ) , 0 );
151        thread_ptr->remote_locks--;
152
[337]153    // deschedule if pending request
154    thread_check_sched();
155 
156    // restore IRQs
[93]157        hal_restore_irq( irq_state );
[11]158}
159
160///////////////////////////////////////////
161void remote_spinlock_lock( xptr_t lock_xp )
162{
[93]163        bool_t              isAtomic = false;
[60]164        reg_t               mode;
[93]165        volatile uint32_t   taken;
[11]166
[93]167        // get cluster and local pointer on remote_spinlock
[423]168        remote_spinlock_t * lock_ptr = GET_PTR( lock_xp );
[93]169        cxy_t               lock_cxy = GET_CXY( lock_xp );
[11]170
[409]171    // get local pointer on calling thread
[101]172    thread_t          * thread_ptr = CURRENT_THREAD;
[11]173
[93]174        // disable interrupts
[11]175        hal_disable_irq( &mode );
[93]176
177        // loop until success
[11]178        while( isAtomic == false )
179        {
180                taken = hal_remote_lw( XPTR( lock_cxy , &lock_ptr->taken ) );
181
[93]182                // deschedule if possible when lock already taken
[1]183                if( taken != 0 )
184                {
[93]185                        hal_restore_irq( mode );
[408]186                        if( thread_can_yield() ) sched_yield("waiting spinlock");
[93]187                        hal_disable_irq( &mode );
[1]188                        continue;
189                }
[93]190
191                // try to take the lock if not already taken
[1]192                isAtomic = hal_remote_atomic_cas( XPTR( lock_cxy , &lock_ptr->taken ) , 0 , 1 );
193        }
194
[93]195        // register lock in thread
[1]196        thread_ptr->remote_locks++;
197
[438]198#if DEBUG_REMOTE_SPINLOCKS
[436]199hal_remote_swd( XPTR( lock_cxy  , &lock_ptr->owner ),
200                XPTR( local_cxy , thread_ptr) );
201xlist_add_first( XPTR( local_cxy , &thread_ptr->xlocks_root ),
202                 XPTR( lock_cxy  , &lock_ptr->list ) );
[409]203#endif
[1]204
[93]205        // enable interrupts
[1]206        hal_restore_irq( mode );
207}
208
[11]209/////////////////////////////////////////////
210void remote_spinlock_unlock( xptr_t lock_xp )
[1]211{
[93]212        // get cluster and local pointer on remote_spinlock
[423]213        remote_spinlock_t * lock_ptr = GET_PTR( lock_xp );
[93]214        cxy_t               lock_cxy = GET_CXY( lock_xp );
[1]215
[93]216        // get pointer on local thread
217        thread_t          * thread_ptr = CURRENT_THREAD;
[1]218
[438]219#if DEBUG_REMOTE_SPINLOCKS
[436]220hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) , XPTR_NULL );
221xlist_unlink( XPTR( lock_cxy , &lock_ptr->list ) );
[409]222#endif
223
[1]224        hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->taken ) , 0 );
225        thread_ptr->remote_locks--;
226
[337]227    // deschedule if pending request
228    thread_check_sched();
[1]229}
230
Note: See TracBrowser for help on using the repository browser.