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

Last change on this file since 321 was 318, checked in by alain, 7 years ago

Bug fix : we don't need an atomic access to update the thread.remote_locks counter.

File size: 7.2 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 <printk.h>
29#include <cluster.h>
30#include <scheduler.h>
31#include <remote_rwlock.h>
32
33///////////////////////////////////////////
34void remote_rwlock_init( xptr_t lock_xp )
35{ 
36    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
37    cxy_t             lock_cxy = GET_CXY( lock_xp );
38
39    hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->ticket )  , 0 );
40    hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->current ) , 0 );
41    hal_remote_sw ( XPTR( lock_cxy , &lock_ptr->count )   , 0 );
42    hal_remote_swd( XPTR( lock_cxy , &lock_ptr->owner )   , XPTR_NULL );
43}
44
45//////////////////////////////////////////////
46void remote_rwlock_rd_lock( xptr_t lock_xp )
47{ 
48        reg_t      mode;
49    uint32_t   ticket;
50
51    // get cluster and local pointer on remote_rwlock
52    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
53    cxy_t             lock_cxy = GET_CXY( lock_xp );
54
55    // get cluster and local pointer on local thread
56    thread_t          * thread_ptr = CURRENT_THREAD;
57
58    // extended pointers on ticket, current, count
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
63    // disable interrupts
64    hal_disable_irq( &mode );
65
66    // get next free ticket
67    ticket = hal_remote_atomic_add( ticket_xp , 1 );
68
69    // busy waiting loop to take the lock
70        while( ticket != hal_remote_lw( current_xp ) )
71        {
72        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
73        }
74
75    ////////// From here we have the lock  ////////////
76
77    // increment count
78    hal_remote_atomic_add( count_xp , 1 );
79
80    // increment thread.remote_locks
81    thread_ptr->remote_locks++;
82
83    // sync
84    hal_fence();
85
86    // release lock to allow several simultaneous readers
87    hal_remote_atomic_add( current_xp , 1 );
88
89    // enable interrupts
90        hal_restore_irq( mode );
91
92}  // end remote_rwlock_rd_lock()
93
94////////////////////////////////////////////////
95void remote_rwlock_rd_unlock( xptr_t lock_xp )
96{
97        reg_t               mode;
98
99    // get cluster and local pointer on remote_rwlock
100    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
101    cxy_t             lock_cxy = GET_CXY( lock_xp );
102
103    // get cluster and local pointer on local thread
104    thread_t          * thread_ptr = CURRENT_THREAD;
105
106    // extended pointers on lock->count
107    xptr_t              count_xp = XPTR( lock_cxy   , &lock_ptr->count );
108
109    // disable interrupts
110        hal_disable_irq( &mode );
111 
112    // decrement count
113    hal_remote_atomic_add( count_xp , -1 );
114
115    // decrement thread.remote_locks
116        thread_ptr->remote_locks--;
117
118    // enable interrupts
119        hal_restore_irq( mode );
120
121}  // end remote_rwlock_rd_unlock()
122
123//////////////////////////////////////////////
124void remote_rwlock_wr_lock( xptr_t lock_xp )
125{ 
126        reg_t      mode;
127    uint32_t   ticket;
128
129    // get cluster and local pointer on remote_rwlock
130    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
131    cxy_t             lock_cxy = GET_CXY( lock_xp );
132
133    // get cluster and local pointer on local thread
134    cxy_t               thread_cxy = local_cxy;
135    thread_t          * thread_ptr = CURRENT_THREAD;
136
137    // compute extended pointers on lock->ticket, lock->owner
138    xptr_t              ticket_xp  = XPTR( lock_cxy   , &lock_ptr->ticket );
139    xptr_t              count_xp   = XPTR( lock_cxy   , &lock_ptr->count );
140    xptr_t              current_xp = XPTR( lock_cxy   , &lock_ptr->current );
141    xptr_t              owner_xp   = XPTR( lock_cxy   , &lock_ptr->owner );
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
165    hal_remote_swd( owner_xp , thread_xp );
166
167    // increment thread.remote_locks
168    thread_ptr->remote_locks++;
169
170    // enable interrupts
171        hal_restore_irq( mode );
172
173}  // end remote_rwlock_wr_lock()
174
175//////////////////////////////////////////////
176void remote_rwlock_wr_unlock( xptr_t lock_xp )
177{
178        reg_t               mode;
179
180    // get cluster and local pointer on remote_rwlock
181    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
182    cxy_t             lock_cxy = GET_CXY( lock_xp );
183
184    // get cluster and local pointer on local thread
185    thread_t          * thread_ptr = CURRENT_THREAD;
186
187    // compute extended pointers on lock->ticket, lock->owner
188    xptr_t              current_xp = XPTR( lock_cxy   , &lock_ptr->current );
189    xptr_t              owner_xp   = XPTR( lock_cxy   , &lock_ptr->owner );
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
198    // decrement thread.remote_locks
199        thread_ptr->remote_locks--;
200
201    // enable interrupts
202        hal_restore_irq( mode );
203
204}  // end remote_rwlock_wr_unlock()
205
206///////////////////////////////////////////
207void remote_rwlock_print( xptr_t   lock_xp,
208                          char   * comment )
209{
210    uint32_t     ticket;                // first free ticket index
211    uint32_t     current;               // ticket index of current owner
212    uint32_t     count;                 // current number of reader threads
213    xptr_t       owner;                 // extended pointer on writer thread
214
215    // get cluster and local pointer on remote_rwlock
216    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
217    cxy_t             lock_cxy = GET_CXY( lock_xp );
218
219    ticket  = hal_remote_lw ( XPTR( lock_cxy , &lock_ptr->ticket ) );
220    current = hal_remote_lw ( XPTR( lock_cxy , &lock_ptr->current ) );
221    count   = hal_remote_lw ( XPTR( lock_cxy , &lock_ptr->count ) );
222    owner   = hal_remote_lwd( XPTR( lock_cxy , &lock_ptr->owner ) );
223
224    printk("\n*** rwlock <%l> %s : ticket = %d / current = %d / count = %d / owner = %l\n",
225           lock_xp , comment , ticket , current , count , owner ); 
226
227}  // end remote_rwlock_print()
228
Note: See TracBrowser for help on using the repository browser.