source: trunk/kernel/libk/remote_fifo.c @ 582

Last change on this file since 582 was 563, checked in by alain, 6 years ago

Complete restructuration of kernel spinlocks.

File size: 5.4 KB
RevLine 
[1]1/*
[68]2 * remote_fifo.c  Implement a lock-less FIFO, multiple-remote-writers / single-local-reader
[1]3 *
4 * Authors : Mohamed Lamine Karaoui (2015)
[68]5 *           Alain Greiner          (2016,2017)
[1]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
[457]25#include <hal_kernel_types.h>
[1]26#include <hal_irqmask.h>
27#include <hal_remote.h>
28#include <hal_special.h>
29#include <bits.h>
30#include <memcpy.h>
31#include <kmem.h>
32#include <thread.h>
33#include <cluster.h>
34#include <remote_fifo.h>
35
[563]36/////////////////////////////////////////////
37void remote_fifo_init( remote_fifo_t * fifo )
[1]38{
39    uint32_t  slot;
40
[407]41    fifo->owner     = 0;
[1]42        fifo->wr_id     = 0;
43        fifo->rd_id     = 0;
44    for( slot = 0 ; slot < CONFIG_REMOTE_FIFO_SLOTS ; slot++ )
45    {
[68]46        fifo->valid[slot] = 0;
[1]47    }
48}
49
[407]50/////////////////////////////////////////////////
51error_t remote_fifo_put_item( xptr_t     fifo_xp, 
52                              uint64_t   item )
[1]53{
54    uint32_t        wr_id;
55    uint32_t        rd_id;
56    uint32_t        ptw;
57    uint32_t        watchdog;
58    uint32_t        nslots;
59
60    // get remote cluster identifier and pointer on FIFO
[563]61    cxy_t           fifo_cxy = GET_CXY( fifo_xp );
62    remote_fifo_t * fifo_ptr = GET_PTR( fifo_xp );
[1]63
64    // initialise watchdog for contention detection
65    watchdog = 0;
66
[407]67    // get write slot index with atomic increment
68        wr_id = hal_remote_atomic_add( XPTR( fifo_cxy , &fifo_ptr->wr_id ) , 1 );
[1]69
[407]70    // wait until allocated slot is empty in remote FIFO
71    // max retry = CONFIG_REMOTE_FIFO_MAX_ITERATIONS 
72    // return error if watchdog is reached
[1]73    while( 1 )
74    {
75        // return error if contention detected by watchdog
[407]76        if( watchdog > CONFIG_REMOTE_FIFO_MAX_ITERATIONS )  return EBUSY;
[1]77
78        // read remote rd_id value
[563]79        rd_id = hal_remote_l32( XPTR( fifo_cxy , &fifo_ptr->rd_id ) );
[1]80
81        // compute number of full slots
82        if( wr_id >= rd_id ) nslots = wr_id - rd_id;
83        else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
84
[407]85        // exit waiting loop as soon as fifo not full
[1]86        if ( nslots < CONFIG_REMOTE_FIFO_SLOTS )  break;
87       
[407]88        // retry later if fifo full:
89        // - deschedule without blocking if possible
90        // - wait ~1000 cycles otherwise
[563]91        if( CURRENT_THREAD->busylocks == 0 ) sched_yield( "wait RPC fifo" );
92        else                                 hal_fixed_delay( 1000 );
[1]93
94        // increment watchdog
95        watchdog++;
96    }
97
98    // compute actual write slot pointer
99    ptw = wr_id % CONFIG_REMOTE_FIFO_SLOTS;
100
101    // copy item to fifo
[563]102        hal_remote_s64( XPTR( fifo_cxy , &fifo_ptr->data[ptw] ), item );
[124]103        hal_fence();
[1]104
105    // set the slot valid flag
[563]106        hal_remote_s32( XPTR( fifo_cxy , &fifo_ptr->valid[ptw] ) , 1 );
[124]107        hal_fence();
[1]108
109    return 0;
110 
111} // end remote_fifo_put_item()
112
[563]113///////////////////////////////////////////////////
114error_t remote_fifo_get_item( remote_fifo_t * fifo,
115                              uint64_t      * item )
[1]116{
117    // get fifo state
118        uint32_t rd_id     = fifo->rd_id;
119        uint32_t wr_id     = fifo->wr_id;
120
121        // return if fifo empty
122        if( rd_id == wr_id ) return EAGAIN;
123       
124    // compute actual read slot pointer
125        uint32_t ptr = rd_id % CONFIG_REMOTE_FIFO_SLOTS;
126       
127        // wait slot filled by the writer
128        while( fifo->valid[ptr] == 0 ) {}
129       
130    // copy item from FIFO to local buffer
[68]131    *item = fifo->data[ptr];
[1]132
133    // reset valid slot flag
134    fifo->valid[ptr] = 0;
135
136    // increment the read index
137        fifo->rd_id += 1;
138
139        return 0;
140
[563]141} // end remote_fifo_get_item()
142
[1]143/////////////////////////////////////////
144bool_t remote_fifo_is_full( xptr_t fifo )
145{
146    uint32_t nslots;
147
148    // get remote cluster identifier and pointer on FIFO
[563]149    cxy_t           cxy = GET_CXY( fifo );
150    remote_fifo_t * ptr = GET_PTR( fifo );
[1]151   
152    // get read and write pointers
[563]153        uint32_t wr_id = hal_remote_l32( XPTR( cxy , &ptr->wr_id ) );
154        uint32_t rd_id = hal_remote_l32( XPTR( cxy , &ptr->rd_id ) );
[1]155
156    // compute number of full slots
157    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
158    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
159
160    return ( nslots >= CONFIG_REMOTE_FIFO_SLOTS );
161}
162
[563]163///////////////////////////////////////////////////
164bool_t remote_fifo_is_empty( remote_fifo_t * fifo )
[1]165{
166    return ( fifo->wr_id == fifo->rd_id );
167}
168
169/////////////////////////////////////////
170uint32_t remote_fifo_items( xptr_t fifo )
171{
172    uint32_t nslots;
173
174    // get remote cluster identifier and pointer on FIFO
[563]175    cxy_t           cxy = GET_CXY( fifo );
176    remote_fifo_t * ptr = GET_PTR( fifo );
[1]177   
178    // get read and write pointers
[563]179        uint32_t wr_id = hal_remote_l32( XPTR( cxy , &ptr->wr_id ) );
180        uint32_t rd_id = hal_remote_l32( XPTR( cxy , &ptr->rd_id ) );
[1]181
182    // compute number of full slots
183    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
184    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
185
186    return nslots;
187}
188
Note: See TracBrowser for help on using the repository browser.