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

Last change on this file since 1 was 1, checked in by alain, 5 years ago

First import

File size: 4.5 KB
Line 
1/*
2 * remote_spinlock.c - kernel remote spinlock implementation.
3 *
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
33////////////////////////////////////////
34void remote_spinlock_init( xptr_t lock )
35{ 
36    remote_spinlock_t * ptr = (remote_spinlock_t *)GET_PTR( lock );
37    cxy_t               cxy = GET_CXY( lock );
38
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 ) ); 
42}
43
44///////////////////////////////////////////////
45uint32_t remote_spinlock_trylock( xptr_t lock )
46{ 
47        uint32_t            mode;
48        bool_t              isAtomic = false;
49
50    // get cluster and local pointer on remote_spinlock
51    remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock );
52    cxy_t               lock_cxy = GET_CXY( lock );
53
54    // get cluster and local pointer on local thread
55    cxy_t               thread_cxy = local_cxy;
56    thread_t          * thread_ptr = CURRENT_THREAD;
57
58    // disable interrupts
59    hal_disable_irq( &mode );
60
61        if( hal_remote_lw( XPTR( lock_cxy , &lock_ptr->taken ) ) == 0 )
62    {
63                isAtomic = hal_remote_atomic_cas( XPTR( lock_cxy , &lock_ptr->taken ) , 0 , 1 );
64    }
65
66        if( isAtomic == false )    // failure
67        {
68                hal_restore_irq( mode );
69                return 1;
70        }
71    else                      // success : register lock in thread
72    {
73            thread_ptr->remote_locks++;
74
75        hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) ,
76                    (uint64_t)XPTR( thread_cxy , thread_ptr) );
77
78        xlist_add_first( XPTR( thread_cxy , &thread_ptr->xlocks_root ) ,
79                     XPTR( lock_cxy , &lock_ptr->list ) );
80
81            hal_restore_irq(mode); 
82            return 0;
83    }
84}
85
86////////////////////////////////////////
87void remote_spinlock_lock( xptr_t lock )
88{
89    bool_t              isAtomic = false;
90        uint32_t            mode;
91    volatile uint32_t   taken;
92
93    // get cluster and local pointer on remote_spinlock
94    remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock );
95    cxy_t               lock_cxy = GET_CXY( lock );
96
97    // get cluster and local pointer on local thread
98    cxy_t               thread_cxy = local_cxy;
99    thread_t          * thread_ptr = CURRENT_THREAD;
100
101    // disable interrupts
102        hal_disable_irq( &mode );
103 
104    // loop until success
105        while( isAtomic == false )
106        {
107                taken = hal_remote_lw( XPTR( lock_cxy , &lock_ptr->taken ) );
108
109        // deschedule if possible when lock already taken
110                if( taken != 0 )
111                {
112                hal_restore_irq( mode );
113            if( thread_can_yield() ) sched_yield();
114                hal_disable_irq( &mode );
115                        continue;
116                }
117   
118        // try to take the lock if not already taken
119                isAtomic = hal_remote_atomic_cas( XPTR( lock_cxy , &lock_ptr->taken ) , 0 , 1 );
120        }
121
122    // register lock in thread
123        thread_ptr->remote_locks++;
124
125    hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) ,
126                    (uint64_t)XPTR( thread_cxy , thread_ptr) );
127
128    xlist_add_first( XPTR( thread_cxy , &thread_ptr->xlocks_root ) ,
129                     XPTR( lock_cxy , &lock_ptr->list ) );
130
131    // enable interrupts
132        hal_restore_irq( mode );
133}
134
135//////////////////////////////////////////
136void remote_spinlock_unlock( xptr_t lock )
137{
138    // get cluster and local pointer on remote_spinlock
139    remote_spinlock_t * lock_ptr = (remote_spinlock_t *)GET_PTR( lock );
140    cxy_t               lock_cxy = GET_CXY( lock );
141
142    // get pointer on local thread
143    thread_t          * thread_ptr = CURRENT_THREAD;
144
145        hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner ) , XPTR_NULL );
146
147        hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->taken ) , 0 );
148
149        thread_ptr->remote_locks--;
150
151    xlist_unlink( XPTR( lock_cxy , &lock_ptr->list ) );
152}
153
Note: See TracBrowser for help on using the repository browser.