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

Last change on this file since 36 was 1, checked in by alain, 8 years ago

First import

File size: 5.8 KB
RevLine 
[1]1/*
2 * remote_fifo.c  Implement a lock-less FIFO,
3                  multiple-remote-writers / single-local-reader
4 *
5 * Authors : Mohamed Lamine Karaoui (2015)
6 *           Alain Greiner          (2016)
7 *
8 * Copyright (c) UPMC Sorbonne Universites
9 *
10 * This file is part of ALMOS-MKH.
11 *
12 * ALMOS-MKH is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2.0 of the License.
15 *
16 * ALMOS-MKH is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <hal_types.h>
27#include <hal_irqmask.h>
28#include <hal_remote.h>
29#include <hal_special.h>
30#include <bits.h>
31#include <memcpy.h>
32#include <kmem.h>
33#include <thread.h>
34#include <cluster.h>
35#include <remote_fifo.h>
36
37////////////////////////////////////////////
38void local_fifo_init( remote_fifo_t * fifo )
39{
40    uint32_t  slot;
41
42        fifo->wr_id     = 0;
43        fifo->rd_id     = 0;
44    for( slot = 0 ; slot < CONFIG_REMOTE_FIFO_SLOTS ; slot++ )
45    {
46        fifo->valid[slot] = false;
47    }
48}
49
50////////////////////////////////////////////
51error_t remote_fifo_put_item( xptr_t   fifo, 
52                              void   * item,
53                              uint32_t size,
54                              bool_t * first )
55{
56    uint32_t        wr_id;
57    uint32_t        rd_id;
58    uint32_t        ptw;
59    uint32_t        watchdog;
60    uint32_t        save_sr;
61    uint32_t        nslots;
62
63    // get remote cluster identifier and pointer on FIFO
64    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
65    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
66
67    // initialise watchdog for contention detection
68    watchdog = 0;
69
70    // no descheduling during put access
71        hal_disable_irq( &save_sr );
72       
73    // get write slot index and increment
74        wr_id = hal_remote_atomic_add( XPTR( cxy , &ptr->wr_id ) , 1 );
75
76    // check fifo state
77    while( 1 )
78    {
79        // return error if contention detected by watchdog
80        if( watchdog > CONFIG_REMOTE_FIFO_MAX_ITERATIONS ) 
81        {
82            // restore scheduling
83                hal_restore_irq( save_sr );
84
85            // return error
86            return EBUSY;
87        }
88
89        // read remote rd_id value
90        rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
91
92        // compute number of full slots
93        if( wr_id >= rd_id ) nslots = wr_id - rd_id;
94        else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
95
96        // exit if fifo not full
97        if ( nslots < CONFIG_REMOTE_FIFO_SLOTS )  break;
98       
99        // restore interrupts
100            hal_restore_irq( save_sr );
101
102        // deschedule without blocking
103        if( thread_can_yield() ) sched_yield();
104
105        // disable interrupts
106            hal_disable_irq( &save_sr );
107
108        // increment watchdog
109        watchdog++;
110    }
111
112    // compute actual write slot pointer
113    ptw = wr_id % CONFIG_REMOTE_FIFO_SLOTS;
114
115    // copy item to fifo
116        hal_remote_memcpy( XPTR( cxy       , &ptr->data[ptw] ),
117                       XPTR( local_cxy , item ) , size ); 
118        hal_wbflush();
119
120    // set the slot valid flag
121        hal_remote_sw( XPTR( cxy , &ptr->valid[ptw] ) , 1 );
122
123        hal_wbflush();
124
125    // return the first RPC flag
126    *first = ( wr_id == rd_id );
127   
128    // restore scheduling
129        hal_restore_irq( save_sr );
130
131    return 0;
132 
133} // end remote_fifo_put_item()
134
135//////////////////////////////////////////////////
136error_t local_fifo_get_item( remote_fifo_t * fifo,
137                             void          * item, 
138                             uint32_t        size )
139{
140    // get fifo state
141        uint32_t rd_id     = fifo->rd_id;
142        uint32_t wr_id     = fifo->wr_id;
143
144        // return if fifo empty
145        if( rd_id == wr_id ) return EAGAIN;
146       
147    // compute actual read slot pointer
148        uint32_t ptr = rd_id % CONFIG_REMOTE_FIFO_SLOTS;
149       
150        // wait slot filled by the writer
151        while( fifo->valid[ptr] == 0 ) {}
152       
153    // copy item from FIFO to local buffer
154    memcpy( item , &fifo->data[ptr] , size );
155
156    // reset valid slot flag
157    fifo->valid[ptr] = 0;
158
159    // increment the read index
160        fifo->rd_id += 1;
161
162        return 0;
163} // end local_fifo_get_item()
164
165/////////////////////////////////////////
166bool_t remote_fifo_is_full( xptr_t fifo )
167{
168    uint32_t nslots;
169
170    // get remote cluster identifier and pointer on FIFO
171    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
172    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
173   
174    // get read and write pointers
175        uint32_t wr_id = hal_remote_lw( XPTR( cxy , &ptr->wr_id ) );
176        uint32_t rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
177
178    // compute number of full slots
179    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
180    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
181
182    return ( nslots >= CONFIG_REMOTE_FIFO_SLOTS );
183}
184
185//////////////////////////////////////////////////
186bool_t local_fifo_is_empty( remote_fifo_t * fifo )
187{
188    return ( fifo->wr_id == fifo->rd_id );
189}
190
191/////////////////////////////////////////
192uint32_t remote_fifo_items( xptr_t fifo )
193{
194    uint32_t nslots;
195
196    // get remote cluster identifier and pointer on FIFO
197    cxy_t           cxy = (cxy_t)GET_CXY( fifo );
198    remote_fifo_t * ptr = (remote_fifo_t *)GET_PTR( fifo );
199   
200    // get read and write pointers
201        uint32_t wr_id = hal_remote_lw( XPTR( cxy , &ptr->wr_id ) );
202        uint32_t rd_id = hal_remote_lw( XPTR( cxy , &ptr->rd_id ) );
203
204    // compute number of full slots
205    if( wr_id >= rd_id ) nslots = wr_id - rd_id;
206    else                 nslots = (0xFFFFFFFF - rd_id) + wr_id;
207
208    return nslots;
209}
210
Note: See TracBrowser for help on using the repository browser.