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

Last change on this file since 374 was 337, checked in by alain, 7 years ago

Introduce the delayed context switch if current thread has a lock.

File size: 6.8 KB
RevLine 
[1]1/*
2 * remote_spinlock.c - kernel remote spinlock implementation.
[93]3 *
[1]4 * Authors  Mohamed Karaoui (2015)
5 *          Alain   Greiner (2016)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <hal_types.h>
26#include <hal_remote.h>
27#include <hal_irqmask.h>
28#include <thread.h>
29#include <cluster.h>
30#include <scheduler.h>
31#include <remote_spinlock.h>
32
[11]33///////////////////////////////////////////
34void remote_spinlock_init( xptr_t lock_xp )
[93]35{
36        remote_spinlock_t * ptr = (remote_spinlock_t *)GET_PTR( lock_xp );
37        cxy_t               cxy = GET_CXY( lock_xp );
[1]38
[93]39        hal_remote_sw ( XPTR( cxy , &ptr->taken ) , 0 );
40        hal_remote_swd( XPTR( cxy , &ptr->owner ) , XPTR_NULL );
41        xlist_entry_init( XPTR( cxy , &ptr->list ) );
[1]42}
43
[11]44/////////////////////////////////////////////////
45error_t remote_spinlock_trylock( xptr_t lock_xp )
[93]46{
[60]47        reg_t               mode;
[1]48        bool_t              isAtomic = false;
49
[93]50        // get cluster and local pointer on remote_spinlock
51        remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock_xp );
52        cxy_t               lock_cxy = GET_CXY( lock_xp );
[1]53
[93]54        // get cluster and local pointer on local thread
55        cxy_t               thread_cxy = local_cxy;
56        thread_t          * thread_ptr = CURRENT_THREAD;
[1]57
[93]58        // disable interrupts
59        hal_disable_irq( &mode );
[1]60
61        if( hal_remote_lw( XPTR( lock_cxy , &lock_ptr->taken ) ) == 0 )
[93]62        {
[1]63                isAtomic = hal_remote_atomic_cas( XPTR( lock_cxy , &lock_ptr->taken ) , 0 , 1 );
[93]64        }
[1]65
[93]66        if( isAtomic == false )    // failure
[1]67        {
68                hal_restore_irq( mode );
69                return 1;
70        }
[93]71        else                      // success : register lock in thread
72        {
73                thread_ptr->remote_locks++;
[1]74
[93]75                hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) ,
76                            (uint64_t)XPTR( thread_cxy , thread_ptr) );
[1]77
[93]78                xlist_add_first( XPTR( thread_cxy , &thread_ptr->xlocks_root ) ,
79                             XPTR( lock_cxy , &lock_ptr->list ) );
[1]80
[93]81                hal_restore_irq(mode);
82                return 0;
83        }
[1]84}
85
[11]86///////////////////////////////////////////////////
87void remote_spinlock_lock_busy( xptr_t     lock_xp,
88                                uint32_t * irq_state )
[1]89{
[93]90        bool_t              isAtomic = false;
[60]91        reg_t               mode;
[93]92        volatile uint32_t   taken;
[1]93
[93]94        // get cluster and local pointer on remote_spinlock
95        remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock_xp );
96        cxy_t               lock_cxy = GET_CXY( lock_xp );
[1]97
[93]98        // get cluster and local pointer on local thread
99        cxy_t               thread_cxy = local_cxy;
100        thread_t          * thread_ptr = CURRENT_THREAD;
[1]101
[93]102        // disable interrupts
[1]103        hal_disable_irq( &mode );
[93]104
105        // loop until success
[1]106        while( isAtomic == false )
107        {
108                taken = hal_remote_lw( XPTR( lock_cxy , &lock_ptr->taken ) );
109
[93]110                // try to take the lock if not already taken
[11]111                if( taken == 0 )
112                {
[93]113                        isAtomic = hal_remote_atomic_cas( XPTR( lock_cxy , &lock_ptr->taken ) , 0 , 1 );
114                }
[11]115        }
116
[93]117        // register lock in thread
[11]118        thread_ptr->remote_locks++;
119
[93]120        hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) ,
121                        (uint64_t)XPTR( thread_cxy , thread_ptr) );
[11]122
[93]123        xlist_add_first( XPTR( thread_cxy , &thread_ptr->xlocks_root ) ,
124                         XPTR( lock_cxy , &lock_ptr->list ) );
[11]125
[93]126        // irq_state must be restored when lock is released
127        *irq_state = mode;
[11]128
129}  // end remote_spinlock_lock_busy()
130
131////////////////////////////////////////////////////
132void remote_spinlock_unlock_busy( xptr_t    lock_xp,
133                                  uint32_t  irq_state )
134{
[93]135        // get cluster and local pointer on remote_spinlock
136        remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock_xp );
137        cxy_t               lock_cxy = GET_CXY( lock_xp );
[11]138
[93]139        // get pointer on local thread
140        thread_t          * thread_ptr = CURRENT_THREAD;
[11]141
142        hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) , XPTR_NULL );
143        hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->taken ) , 0 );
144        thread_ptr->remote_locks--;
[93]145        xlist_unlink( XPTR( lock_cxy , &lock_ptr->list ) );
[11]146
[337]147    // deschedule if pending request
148    thread_check_sched();
149 
150    // restore IRQs
[93]151        hal_restore_irq( irq_state );
[11]152}
153
154///////////////////////////////////////////
155void remote_spinlock_lock( xptr_t lock_xp )
156{
[93]157        bool_t              isAtomic = false;
[60]158        reg_t               mode;
[93]159        volatile uint32_t   taken;
[11]160
[93]161        // get cluster and local pointer on remote_spinlock
162        remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock_xp );
163        cxy_t               lock_cxy = GET_CXY( lock_xp );
[11]164
[101]165    // get cluster and local pointer on calling thread
166    cxy_t               thread_cxy = local_cxy;
167    thread_t          * thread_ptr = CURRENT_THREAD;
[11]168
[93]169        // disable interrupts
[11]170        hal_disable_irq( &mode );
[93]171
172        // loop until success
[11]173        while( isAtomic == false )
174        {
175                taken = hal_remote_lw( XPTR( lock_cxy , &lock_ptr->taken ) );
176
[93]177                // deschedule if possible when lock already taken
[1]178                if( taken != 0 )
179                {
[93]180                        hal_restore_irq( mode );
[296]181                        if( thread_can_yield() ) sched_yield( NULL );
[93]182                        hal_disable_irq( &mode );
[1]183                        continue;
184                }
[93]185
186                // try to take the lock if not already taken
[1]187                isAtomic = hal_remote_atomic_cas( XPTR( lock_cxy , &lock_ptr->taken ) , 0 , 1 );
188        }
189
[93]190        // register lock in thread
[1]191        thread_ptr->remote_locks++;
192
[93]193        hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) ,
194                        (uint64_t)XPTR( thread_cxy , thread_ptr) );
[1]195
[93]196        xlist_add_first( XPTR( thread_cxy , &thread_ptr->xlocks_root ) ,
197                         XPTR( lock_cxy , &lock_ptr->list ) );
[1]198
[93]199        // enable interrupts
[1]200        hal_restore_irq( mode );
201}
202
[11]203/////////////////////////////////////////////
204void remote_spinlock_unlock( xptr_t lock_xp )
[1]205{
[93]206        // get cluster and local pointer on remote_spinlock
207        remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock_xp );
208        cxy_t               lock_cxy = GET_CXY( lock_xp );
[1]209
[93]210        // get pointer on local thread
211        thread_t          * thread_ptr = CURRENT_THREAD;
[1]212
213        hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) , XPTR_NULL );
214        hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->taken ) , 0 );
215        thread_ptr->remote_locks--;
[337]216        xlist_unlink( XPTR( lock_cxy , &lock_ptr->list ) );
[1]217
[337]218    // deschedule if pending request
219    thread_check_sched();
[1]220}
221
[101]222//////////////////////////////////////////////
223xptr_t remote_spinlock_owner( xptr_t lock_xp )
224{
225    // get cluster and local pointer on remote_spinlock
226    remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock_xp );
227    cxy_t               lock_cxy = GET_CXY( lock_xp );
228
229    return hal_remote_lw( XPTR( lock_cxy , &lock_ptr->owner ) );
230}
Note: See TracBrowser for help on using the repository browser.