source: trunk/kernel/libk/remote_barrier.c @ 282

Last change on this file since 282 was 104, checked in by max@…, 7 years ago

style

File size: 9.6 KB
Line 
1/*
2 * remote_barrier.c - Access a POSIX barrier.
3 *
4 * Author   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 <remote_spinlock.h>
28#include <thread.h>
29#include <kmem.h>
30#include <printk.h>
31#include <process.h>
32#include <vmm.h>
33#include <remote_barrier.h>
34
35/////////////////////////////////////////////////
36inline void remote_barrier( xptr_t    barrier_xp,
37                            uint32_t  count )
38{
39    uint32_t  expected;
40
41    remote_barrier_t * ptr = (remote_barrier_t *)GET_PTR( barrier_xp );
42    cxy_t              cxy = GET_CXY( barrier_xp );
43
44    // get barrier sense value
45    uint32_t sense = hal_remote_lw( XPTR( cxy , &ptr->sense ) );
46
47    // compute expected value
48    if ( sense == 0 ) expected = 1;
49    else              expected = 0;
50
51    // atomically increment current
52    uint32_t current = hal_remote_atomic_add( XPTR( cxy , &ptr->current ) , 1 );
53
54    // last task reset current and toggle sense
55    if( current == (count-1) )
56    {
57        hal_remote_sw( XPTR( cxy , &ptr->current) , 0 );
58        hal_remote_sw( XPTR( cxy , &ptr->sense  ) , expected );
59    }
60    else   // other tasks poll the sense
61    {
62        while( hal_remote_lw( XPTR( cxy , &ptr->sense ) ) != expected ) asm volatile ("nop");
63    }
64}
65
66///////////////////////////////////////////////////
67xptr_t remote_barrier_from_ident( intptr_t  ident )
68{
69    // get pointer on local process_descriptor
70    process_t * process = CURRENT_THREAD->process;
71
72    // get extended pointer on reference process
73    xptr_t      ref_xp = process->ref_xp;
74
75    // get cluster and local pointer on reference process
76    cxy_t          ref_cxy = GET_CXY( ref_xp );
77    process_t    * ref_ptr = (process_t *)GET_PTR( ref_xp );
78
79    // get extended pointer on root of barriers list
80    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->barrier_root );
81
82    // scan reference process barriers list
83    xptr_t             iter_xp;
84    xptr_t             barrier_xp;
85    cxy_t              barrier_cxy;
86    remote_barrier_t * barrier_ptr;
87    intptr_t           current;
88    bool_t             found = false;
89
90    XLIST_FOREACH( root_xp , iter_xp )
91    {
92        barrier_xp  = XLIST_ELEMENT( iter_xp , remote_barrier_t , list );
93        barrier_cxy = GET_CXY( barrier_xp );
94        barrier_ptr = (remote_barrier_t *)GET_PTR( barrier_xp );
95        current     = (intptr_t)hal_remote_lpt( XPTR( barrier_cxy , &barrier_ptr->ident ) );
96        if( ident == current )
97        {
98            found = true;
99            break;
100        }
101    }
102
103    if( found == false )  return XPTR_NULL;
104    else                  return barrier_xp;
105}
106
107//////////////////////////////////////////////
108error_t remote_barrier_create( intptr_t ident,
109                               uint32_t count )
110{
111    xptr_t             barrier_xp;
112    remote_barrier_t * barrier_ptr;
113
114    // get pointer on local process descriptor
115    process_t * process = CURRENT_THREAD->process;
116
117    // get extended pointer on reference process
118    xptr_t      ref_xp = process->ref_xp;
119
120    // get reference process cluster and local pointer
121    cxy_t       ref_cxy = GET_CXY( ref_xp );
122    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
123
124    // allocate memory for barrier descriptor
125    if( ref_cxy == local_cxy )                  // local cluster is the reference
126    {
127        kmem_req_t req;
128        req.type      = KMEM_BARRIER;
129        req.flags     = AF_ZERO;
130        barrier_ptr   = kmem_alloc( &req );
131        barrier_xp    = XPTR( local_cxy , barrier_ptr );
132    }
133    else                                       // reference is remote
134    {
135        rpc_kcm_alloc_client( ref_cxy , KMEM_BARRIER , &barrier_xp );
136        barrier_ptr = (remote_barrier_t *)GET_PTR( barrier_xp );
137    }
138
139    if( barrier_ptr == NULL ) return ENOMEM;
140
141    // initialise barrier
142    hal_remote_sw ( XPTR( ref_cxy , &barrier_ptr->nb_threads ) , count );
143    hal_remote_sw ( XPTR( ref_cxy , &barrier_ptr->current    ) , 0 );
144    hal_remote_sw ( XPTR( ref_cxy , &barrier_ptr->sense      ) , 0 );
145    hal_remote_spt( XPTR( ref_cxy , &barrier_ptr->ident      ) , (void*)ident );
146
147    xlist_entry_init( XPTR( ref_cxy , &barrier_ptr->list ) );
148
149    // register  barrier in reference process xlist
150    xptr_t root_xp  = XPTR( ref_cxy , &ref_ptr->barrier_root );
151    xptr_t entry_xp = XPTR( ref_cxy , &barrier_ptr->list );
152
153    remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
154    xlist_add_first( root_xp , entry_xp );
155    remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
156
157    return 0;
158}
159
160////////////////////////////////////////////////
161void remote_barrier_destroy( xptr_t barrier_xp )
162{
163    // get pointer on local process descriptor
164    process_t * process = CURRENT_THREAD->process;
165
166    // get extended pointer on reference process
167    xptr_t      ref_xp = process->ref_xp;
168
169    // get reference process cluster and local pointer
170    cxy_t       ref_cxy = GET_CXY( ref_xp );
171    process_t * ref_ptr = (process_t *)GET_PTR( ref_xp );
172
173    // get barrier cluster and local pointer
174    cxy_t              barrier_cxy = GET_CXY( barrier_xp );
175    remote_barrier_t * barrier_ptr = (remote_barrier_t *)GET_PTR( barrier_xp );
176
177    // remove barrier from reference process xlist
178    remote_spinlock_lock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
179    xlist_unlink( XPTR( barrier_cxy , &barrier_ptr->list ) );
180    remote_spinlock_unlock( XPTR( ref_cxy , &ref_ptr->sync_lock ) );
181
182    // release memory allocated for barrier descriptor
183    if( barrier_cxy == local_cxy )                        // reference is local
184    {
185        kmem_req_t  req;
186        req.type = KMEM_BARRIER;
187        req.ptr  = barrier_ptr;
188        kmem_free( &req );
189    }
190    else                                                  // reference is remote
191    {
192        rpc_kcm_free_client( barrier_cxy , barrier_ptr , KMEM_BARRIER );
193    }
194}
195
196/////////////////////////////////////////////
197void remote_barrier_wait( xptr_t barrier_xp )
198{
199    uint32_t  expected;
200    uint32_t  current;
201    uint32_t  count;
202    uint32_t  sense;
203    reg_t     irq_state;
204    xptr_t    root_xp;
205
206    // get cluster and local pointer on calling thread
207    cxy_t              thread_cxy = local_cxy;
208    thread_t         * thread_ptr = CURRENT_THREAD;
209
210    // get cluster and local pointer on remote barrier
211    remote_barrier_t * barrier_ptr = (remote_barrier_t *)GET_PTR( barrier_xp );
212    cxy_t              barrier_cxy = GET_CXY( barrier_xp );
213
214    // get count and root fields from barrier descriptor
215    count   = hal_remote_lw ( XPTR( barrier_cxy , &barrier_ptr->nb_threads ) );
216    root_xp = hal_remote_lwd( XPTR( barrier_cxy , &barrier_ptr->root ) );
217
218    // get barrier sense value
219    sense = hal_remote_lw( XPTR( barrier_cxy , &barrier_ptr->sense ) );
220
221    // compute expected value
222    if ( sense == 0 ) expected = 1;
223    else              expected = 0;
224
225    // atomically increment current
226    current = hal_remote_atomic_add( XPTR( barrier_cxy , &barrier_ptr->current ) , 1 );
227
228    // last thread reset current, toggle sense, and activate all waiting threads
229    // other threads block, register in queue, and deschedule
230
231    if( current == (count-1) )                       // last thread
232    {
233        hal_remote_sw( XPTR( barrier_cxy , &barrier_ptr->current) , 0 );
234        hal_remote_sw( XPTR( barrier_cxy , &barrier_ptr->sense  ) , expected );
235
236        // activate waiting threads if required
237        if( xlist_is_empty( root_xp ) == false )
238        {
239            // disable interrupts
240            hal_disable_irq( &irq_state );
241
242            xptr_t  iter_xp;
243            xptr_t  thread_xp;
244            XLIST_FOREACH( root_xp , iter_xp )
245            {
246                // get extended pointer on waiting thread
247                thread_xp = XLIST_ELEMENT( iter_xp , thread_t , wait_list );
248
249                // remove waiting thread from queue
250                remote_spinlock_lock( XPTR( barrier_cxy , &barrier_ptr->lock ) );
251                xlist_unlink( XPTR( barrier_cxy , &barrier_ptr->list ) );
252                remote_spinlock_unlock( XPTR( barrier_cxy , &barrier_ptr->lock ) );
253
254                // unblock waiting thread
255                thread_unblock( thread_xp , THREAD_BLOCKED_USERSYNC );
256            }
257
258            // restore interrupts
259            hal_restore_irq( irq_state );
260        }
261    }
262    else                                             // not the last thread
263    {
264        // disable interrupts
265        hal_disable_irq( &irq_state );
266
267        // register calling thread in barrier waiting queue
268        xptr_t entry_xp = XPTR( thread_cxy , &thread_ptr->wait_list );
269
270        remote_spinlock_lock( XPTR( barrier_cxy , &barrier_ptr->lock ) );
271        xlist_add_last( root_xp , entry_xp );
272        remote_spinlock_unlock( XPTR( barrier_cxy , &barrier_ptr->lock ) );
273
274        // block & deschedule the calling thread
275        thread_block( thread_ptr , THREAD_BLOCKED_USERSYNC );
276        sched_yield();
277
278        // restore interrupts
279        hal_restore_irq( irq_state );
280    }
281}
Note: See TracBrowser for help on using the repository browser.