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

Last change on this file since 60 was 60, checked in by max@…, 4 years ago

use reg_t instead of uint32_t

File size: 7.8 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    cxy_t               thread_cxy = local_cxy;
57    thread_t          * thread_ptr = CURRENT_THREAD;
58
59    // extended pointers on ticket, current, count, and thread->remote_locks
60    xptr_t              ticket_xp  = XPTR( lock_cxy   , &lock_ptr->ticket );
61    xptr_t              current_xp = XPTR( lock_cxy   , &lock_ptr->current );
62    xptr_t              count_xp   = XPTR( lock_cxy   , &lock_ptr->count );
63    xptr_t              locks_xp   = XPTR( thread_cxy , &thread_ptr->remote_locks );
64
65    // disable interrupts
66    hal_disable_irq( &mode );
67
68    // get next free ticket
69    ticket = hal_remote_atomic_add( ticket_xp , 1 );
70
71    // busy waiting loop to take the lock
72        while( ticket != hal_remote_lw( current_xp ) )
73        {
74        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
75        }
76
77    ////////// From here we have the lock  ////////////
78
79    // increment count and thead.remote_locks
80    hal_remote_atomic_add( count_xp , 1 );
81    hal_remote_atomic_add( locks_xp , 1 );
82
83    // sync
84    hal_wbflush();
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    cxy_t               thread_cxy = local_cxy;
105    thread_t          * thread_ptr = CURRENT_THREAD;
106
107    // extended pointers on lock->count and thread->remote_locks
108    xptr_t              count_xp = XPTR( lock_cxy   , &lock_ptr->count );
109    xptr_t              locks_xp = XPTR( thread_cxy , &thread_ptr->remote_locks );
110
111    // disable interrupts
112        hal_disable_irq( &mode );
113 
114    // decrement count and thread.remote_locks
115    hal_remote_atomic_add( count_xp , -1 );
116        hal_remote_atomic_add( locks_xp , -1 );
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, and thread->remote_locks
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              locks_xp   = XPTR( thread_cxy , &thread_ptr->remote_locks );
143    xptr_t              thread_xp  = XPTR( thread_cxy , thread_ptr );
144
145    // disable interrupts
146    hal_disable_irq( &mode );
147
148    // get next free ticket
149    ticket = hal_remote_atomic_add( ticket_xp , 1 );
150
151    // loop to take the lock
152        while( ticket != hal_remote_lw( current_xp ) )
153        {
154        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
155        }
156
157    ////////// From here we have the lock  ////////////
158
159    // wait completion of read accesses
160    while( hal_remote_lw( count_xp ) != 0 )
161    {
162        hal_fixed_delay( CONFIG_RWLOCK_DELAY );
163    }
164
165    // register owner thread and increment thread.remote_locks
166    hal_remote_swd( owner_xp , thread_xp );
167    hal_remote_atomic_add( locks_xp , 1 );
168
169    // enable interrupts
170        hal_restore_irq( mode );
171
172}  // end remote_rwlock_wr_lock()
173
174//////////////////////////////////////////////
175void remote_rwlock_wr_unlock( xptr_t lock_xp )
176{
177        reg_t               mode;
178
179    // get cluster and local pointer on remote_rwlock
180    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
181    cxy_t             lock_cxy = GET_CXY( lock_xp );
182
183    // get cluster and local pointer on local thread
184    cxy_t               thread_cxy = local_cxy;
185    thread_t          * thread_ptr = CURRENT_THREAD;
186
187    // compute extended pointers on lock->ticket, lock->owner and thread->remote_locks
188    xptr_t              current_xp = XPTR( lock_cxy   , &lock_ptr->current );
189    xptr_t              owner_xp   = XPTR( lock_cxy   , &lock_ptr->owner );
190    xptr_t              locks_xp   = XPTR( thread_cxy , &thread_ptr->remote_locks );
191
192    // disable interrupts
193        hal_disable_irq( &mode );
194 
195    // unregister owner thread, and release lock
196    hal_remote_swd( owner_xp , XPTR_NULL );
197    hal_remote_atomic_add( current_xp , 1 );
198        hal_remote_atomic_add( locks_xp , -1 );
199
200    // enable interrupts
201        hal_restore_irq( mode );
202
203}  // end remote_rwlock_wr_unlock()
204
205///////////////////////////////////////////
206void remote_rwlock_print( xptr_t   lock_xp,
207                          char   * comment )
208{
209    uint32_t     ticket;                // first free ticket index
210    uint32_t     current;               // ticket index of current owner
211    uint32_t     count;                 // current number of reader threads
212    xptr_t       owner;                 // extended pointer on writer thread
213
214    // get cluster and local pointer on remote_rwlock
215    remote_rwlock_t * lock_ptr = (remote_rwlock_t *)GET_PTR( lock_xp );
216    cxy_t             lock_cxy = GET_CXY( lock_xp );
217
218    ticket  = hal_remote_lw ( XPTR( lock_cxy , &lock_ptr->ticket ) );
219    current = hal_remote_lw ( XPTR( lock_cxy , &lock_ptr->current ) );
220    count   = hal_remote_lw ( XPTR( lock_cxy , &lock_ptr->count ) );
221    owner   = hal_remote_lwd( XPTR( lock_cxy , &lock_ptr->owner ) );
222
223    printk("\n*** rwlock <%l> %s : ticket = %d / current = %d / count = %d / owner = %l\n",
224           lock_xp , comment , ticket , current , count , owner ); 
225
226}  // end remote_rwlock_print()
227
Note: See TracBrowser for help on using the repository browser.