source: trunk/kernel/libk/remote_rwlock.c @ 35

Last change on this file since 35 was 23, checked in by alain, 7 years ago

Introduce syscalls.

File size: 6.7 KB
Line 
1/*
2 * remote_rwlock.c - kernel remote rwlock implementation.
3 *
4 * Authors    Alain   Greiner (2016,2017)
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 <hal_types.h>
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_rwlock.h>
31
32///////////////////////////////////////////
33void remote_rwlock_init( xptr_t lock_xp )
34{ 
35    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
36    cxy_t             lock_cxy = GET_CXY( lock_xp );
37
38    hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->ticket )  , 0 );
39    hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->current ) , 0 );
40    hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->count )   , 0 );
41    hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner )   , XPTR_NULL );
42}
43
44//////////////////////////////////////////////
45void remote_rwlock_rd_lock( xptr_t lock_xp )
46{ 
47        uint32_t   mode;
48    uint32_t   ticket;
49
50    // get cluster and local pointer on remote_rwlock
51    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
52    cxy_t             lock_cxy = GET_CXY( lock_xp );
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    // extended pointers on ticket, current, count, and thread->remote_locks
59    xptr_t              ticket_xp  = XPTR( lock_cxy   , &lock_ptr->ticket );
60    xptr_t              current_xp = XPTR( lock_cxy   , &lock_ptr->current );
61    xptr_t              count_xp   = XPTR( lock_cxy   , &lock_ptr->count );
62    xptr_t              locks_xp   = XPTR( thread_cxy , &thread_ptr->remote_locks );
63
64    // disable interrupts
65    hal_disable_irq( &mode );
66
67    // get next free ticket
68    ticket = hal_remote_atomic_add( ticket_xp , 1 );
69
70    // busy waiting loop to take the lock
71        while( ticket != hal_remote_lw( current_xp ) )
72        {
73        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
74        }
75
76    ////////// From here we have the lock  ////////////
77
78    // increment count and thead.remote_locks
79    hal_remote_atomic_add( count_xp , 1 );
80    hal_remote_atomic_add( locks_xp , 1 );
81
82    // sync
83    hal_wbflush();
84
85    // release lock to allow several simultaneous readers
86    hal_remote_atomic_add( current_xp , 1 );
87
88    // enable interrupts
89        hal_restore_irq( mode );
90
91}  // end remote_rwlock_rd_lock()
92
93////////////////////////////////////////////////
94void remote_rwlock_rd_unlock( xptr_t lock_xp )
95{
96        uint32_t            mode;
97
98    // get cluster and local pointer on remote_rwlock
99    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
100    cxy_t             lock_cxy = GET_CXY( lock_xp );
101
102    // get cluster and local pointer on local thread
103    cxy_t               thread_cxy = local_cxy;
104    thread_t          * thread_ptr = CURRENT_THREAD;
105
106    // extended pointers on lock->count and thread->remote_locks
107    xptr_t              count_xp = XPTR( lock_cxy   , &lock_ptr->count );
108    xptr_t              locks_xp = XPTR( thread_cxy , &thread_ptr->remote_locks );
109
110    // disable interrupts
111        hal_disable_irq( &mode );
112 
113    // decrement count and thread.remote_locks
114    hal_remote_atomic_add( count_xp , -1 );
115        hal_remote_atomic_add( locks_xp , -1 );
116
117    // enable interrupts
118        hal_restore_irq( mode );
119
120}  // end remote_rwlock_rd_unlock()
121
122//////////////////////////////////////////////
123void remote_rwlock_wr_lock( xptr_t lock_xp )
124{ 
125        uint32_t   mode;
126    uint32_t   ticket;
127
128    // get cluster and local pointer on remote_rwlock
129    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
130    cxy_t             lock_cxy = GET_CXY( lock_xp );
131
132    // get cluster and local pointer on local thread
133    cxy_t               thread_cxy = local_cxy;
134    thread_t          * thread_ptr = CURRENT_THREAD;
135
136    // compute extended pointers on lock->ticket, lock->owner, and thread->remote_locks
137    xptr_t              ticket_xp  = XPTR( lock_cxy   , &lock_ptr->ticket );
138    xptr_t              count_xp   = XPTR( lock_cxy   , &lock_ptr->count );
139    xptr_t              current_xp = XPTR( lock_cxy   , &lock_ptr->current );
140    xptr_t              owner_xp   = XPTR( lock_cxy   , &lock_ptr->owner );
141    xptr_t              locks_xp   = XPTR( thread_cxy , &thread_ptr->remote_locks );
142    xptr_t              thread_xp  = XPTR( thread_cxy , thread_ptr );
143
144    // disable interrupts
145    hal_disable_irq( &mode );
146
147    // get next free ticket
148    ticket = hal_remote_atomic_add( ticket_xp , 1 );
149
150    // loop to take the lock
151        while( ticket != hal_remote_lw( current_xp ) )
152        {
153        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
154        }
155
156    ////////// From here we have the lock  ////////////
157
158    // wait completion of read accesses
159    while( hal_remote_lw( count_xp ) != 0 )
160    {
161        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
162    }
163
164    // register owner thread and increment thread.remote_locks
165    hal_remote_swd( owner_xp , thread_xp );
166    hal_remote_atomic_add( locks_xp , 1 );
167
168    // enable interrupts
169        hal_restore_irq( mode );
170
171}  // end remote_rwlock_wr_lock()
172
173//////////////////////////////////////////////
174void remote_rwlock_wr_unlock( xptr_t lock_xp )
175{
176        uint32_t            mode;
177
178    // get cluster and local pointer on remote_rwlock
179    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
180    cxy_t             lock_cxy = GET_CXY( lock_xp );
181
182    // get cluster and local pointer on local thread
183    cxy_t               thread_cxy = local_cxy;
184    thread_t          * thread_ptr = CURRENT_THREAD;
185
186    // compute extended pointers on lock->ticket, lock->owner and thread->remote_locks
187    xptr_t              current_xp = XPTR( lock_cxy   , &lock_ptr->ticket );
188    xptr_t              owner_xp   = XPTR( lock_cxy   , &lock_ptr->owner );
189    xptr_t              locks_xp   = XPTR( thread_cxy , &thread_ptr->remote_locks );
190
191    // disable interrupts
192        hal_disable_irq( &mode );
193 
194    // unregister owner thread, and release lock
195    hal_remote_swd( owner_xp , XPTR_NULL );
196    hal_remote_atomic_add( current_xp , 1 );
197        hal_remote_atomic_add( locks_xp , -1 );
198
199    // enable interrupts
200        hal_restore_irq( mode );
201
202}  // end remote_rwlock_wr_unlock()
203
204
Note: See TracBrowser for help on using the repository browser.