source: trunk/kernel/libk/remote_fifo.c

Last change on this file was 683, checked in by alain, 9 months ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 5.4 KB
Line 
1/*
2 * remote_fifo.c  Implement a lock-less FIFO, multiple-remote-writers / single-local-reader
3 *
4 * Authors : Mohamed Lamine Karaoui (2015)
5 *           Alain Greiner          (2016,2017)
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
25#include <hal_kernel_types.h>
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
36/////////////////////////////////////////////
37void remote_fifo_init( remote_fifo_t * fifo )
38{
39    uint32_t  slot;
40
41    fifo->owner     = 0;
42        fifo->wr_id     = 0;
43        fifo->rd_id     = 0;
44    for( slot = 0 ; slot < CONFIG_RPC_FIFO_SLOTS ; slot++ )
45    {
46        fifo->valid[slot] = 0;
47    }
48}
49
50/////////////////////////////////////////////////
51error_t remote_fifo_put_item( xptr_t     fifo_xp, 
52                              uint64_t   item )
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
61    cxy_t           fifo_cxy = GET_CXY( fifo_xp );
62    remote_fifo_t * fifo_ptr = GET_PTR( fifo_xp );
63
64    // initialise watchdog for contention detection
65    watchdog = 0;
66
67    // get write slot index with atomic increment QQQQQQQQQQ
68        wr_id = hal_remote_atomic_add( XPTR( fifo_cxy , &fifo_ptr->wr_id ) , 1 );
69
70    // wait until allocated slot is empty in remote FIFO
71    // max retry = CONFIG_RPC_FIFO_MAX_ITERATIONS 
72    // return error if watchdog is reached
73    while( 1 )
74    {
75        // return error if contention detected by watchdog
76        if( watchdog > CONFIG_RPC_FIFO_MAX_ITERATIONS )  return EBUSY;
77
78        // read remote rd_id value
79        rd_id = hal_remote_l32( XPTR( fifo_cxy , &fifo_ptr->rd_id ) );
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
85        // exit waiting loop as soon as fifo not full
86        if ( nslots < CONFIG_RPC_FIFO_SLOTS )  break;
87       
88        // retry later if fifo full:
89        // - deschedule without blocking if possible
90        // - wait ~1000 cycles otherwise
91        if( CURRENT_THREAD->busylocks == 0 ) sched_yield( "wait RPC fifo" );
92        else                                 hal_fixed_delay( 1000 );
93
94        // increment watchdog
95        watchdog++;
96    }
97
98    // compute actual write slot pointer
99    ptw = wr_id % CONFIG_RPC_FIFO_SLOTS;
100
101    // copy item to fifo
102        hal_remote_s64( XPTR( fifo_cxy , &fifo_ptr->data[ptw] ), item );
103        hal_fence();
104
105    // set the slot valid flag
106        hal_remote_s32( XPTR( fifo_cxy , &fifo_ptr->valid[ptw] ) , 1 );
107        hal_fence();
108
109    return 0;
110 
111} // end remote_fifo_put_item()
112
113///////////////////////////////////////////////////
114error_t remote_fifo_get_item( remote_fifo_t * fifo,
115                              uint64_t      * item )
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_RPC_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
131    *item = fifo->data[ptr];
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
141} // end remote_fifo_get_item()
142
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
149    cxy_t           cxy = GET_CXY( fifo );
150    remote_fifo_t * ptr = GET_PTR( fifo );
151   
152    // get read and write pointers
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 ) );
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_RPC_FIFO_SLOTS );
161}
162
163///////////////////////////////////////////////////
164bool_t remote_fifo_is_empty( remote_fifo_t * fifo )
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
175    cxy_t           cxy = GET_CXY( fifo );
176    remote_fifo_t * ptr = GET_PTR( fifo );
177   
178    // get read and write pointers
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 ) );
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.