[1] | 1 | /* |
---|
[68] | 2 | * remote_fifo.h - Lock-less Single-Reader Multiple-Writers FIFO |
---|
[1] | 3 | * |
---|
[68] | 4 | * Authors : Mohamed Lamine Karaoui (2015) |
---|
| 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-MHH 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 | #ifndef _REMOTE_FIFO_H_ |
---|
| 26 | #define _REMOTE_FIFO_H_ |
---|
| 27 | |
---|
[14] | 28 | #include <kernel_config.h> |
---|
[457] | 29 | #include <hal_kernel_types.h> |
---|
[407] | 30 | #include <printk.h> |
---|
[1] | 31 | #include <errno.h> |
---|
| 32 | #include <hal_remote.h> |
---|
| 33 | |
---|
| 34 | /************************************************************************************ |
---|
[407] | 35 | * This structure defines a generic, single reader, multiple writers FIFO, |
---|
| 36 | * that is used for - RPC based - inter cluster communications. |
---|
| 37 | * Each FIF0 slot can contain one 64 bits integer (or one extended pointer). |
---|
| 38 | * The number of slots is defined by the CONFIG_REMOTE_FIFO_SLOTS parameter. |
---|
| 39 | * - The write accesses are implemented using a lock-free algorithm, as it uses |
---|
| 40 | * a ticket based mechanism to handle concurrent access between multiple writers. |
---|
| 41 | * In case of FIFO full, the writer deschedule without blocking, to retry later. |
---|
| 42 | * - The reader must take the try_lock implemented by the "owner" field, using |
---|
| 43 | * an atomic_add(). The TRDID is a good owner identifier, because all |
---|
| 44 | * RPC threads in a given cluster belong to the same kernel process, |
---|
| 45 | * and RPC threads cannot have local index LTID = 0. |
---|
| 46 | * |
---|
| 47 | * WARNING : Each FIFO requires 12 + (12 * CONFIG_REMOTE_FIFO_SLOTS) bytes. |
---|
[1] | 48 | ***********************************************************************************/ |
---|
[68] | 49 | |
---|
[1] | 50 | typedef struct remote_fifo_s |
---|
| 51 | { |
---|
[407] | 52 | uint32_t owner; /*! owner thread trdid */ |
---|
[1] | 53 | volatile uint32_t wr_id; /*! write slot index */ |
---|
| 54 | volatile uint32_t rd_id; /*! read slot index */ |
---|
[68] | 55 | volatile uint32_t valid[CONFIG_REMOTE_FIFO_SLOTS]; /*! empty slot if 0 */ |
---|
| 56 | uint64_t data[CONFIG_REMOTE_FIFO_SLOTS]; /*! fifo slot content */ |
---|
[1] | 57 | } |
---|
| 58 | remote_fifo_t; |
---|
| 59 | |
---|
| 60 | /************************************************************************************ |
---|
| 61 | * This function initializes the local FIFO as empty. |
---|
| 62 | * It can only initialise a local FIFO. |
---|
| 63 | ************************************************************************************ |
---|
| 64 | * @ fifo : pointer to the local fifo. |
---|
| 65 | ***********************************************************************************/ |
---|
| 66 | void local_fifo_init( remote_fifo_t * fifo ); |
---|
| 67 | |
---|
| 68 | /************************************************************************************ |
---|
| 69 | * This non blocking function tries to get one item from the local fifo. |
---|
[407] | 70 | * The reader must get exclusive access for read before calling this function. |
---|
[1] | 71 | * The read slot index is incremented. |
---|
| 72 | ************************************************************************************ |
---|
| 73 | * @ fifo : pointer to the local fifo. |
---|
[279] | 74 | * @ item : [out] pointer on buffer for extracted item. |
---|
[1] | 75 | * @ return 0 on success, EAGAIN if the buffer is empty. |
---|
| 76 | ***********************************************************************************/ |
---|
| 77 | error_t local_fifo_get_item( remote_fifo_t * fifo, |
---|
[68] | 78 | uint64_t * item ); |
---|
[1] | 79 | |
---|
| 80 | /************************************************************************************ |
---|
| 81 | * This blocking function puts one item to a remote fifo identified |
---|
[407] | 82 | * by an extended pointer. It gets a write ticket on the slot to be written, |
---|
| 83 | * using a remote_atomic_add() on the write slot index. Then, it waits until |
---|
| 84 | * the slot is empty, using a descheduling policy without blocking if required. |
---|
| 85 | * It implements a watchdog, returning when the item has been successfully |
---|
| 86 | * registered, or after CONFIG_REMOTE_FIFO_MAX_ITERATIONS failures. |
---|
[1] | 87 | ************************************************************************************ |
---|
| 88 | * @ fifo : extended pointer to the fifo in remote cluster. |
---|
[279] | 89 | * @ item : item to be stored. |
---|
[1] | 90 | * @ return 0 on success / EBUSY if a contention has been detected. |
---|
| 91 | ***********************************************************************************/ |
---|
| 92 | error_t remote_fifo_put_item( xptr_t fifo, |
---|
[407] | 93 | uint64_t item ); |
---|
[1] | 94 | |
---|
| 95 | /************************************************************************************ |
---|
| 96 | * Query if local fifo is empty |
---|
| 97 | ************************************************************************************ |
---|
| 98 | * @ fifo : pointer to the fifo. |
---|
| 99 | * @ return true if the fifo is empty, false otherwise. |
---|
| 100 | ***********************************************************************************/ |
---|
| 101 | bool_t local_fifo_is_empty( remote_fifo_t * fifo ); |
---|
| 102 | |
---|
| 103 | /************************************************************************************ |
---|
| 104 | * Query if remote fifo is full |
---|
| 105 | ************************************************************************************ |
---|
| 106 | * @ fifo : pointer to the fifo in remote cluster. |
---|
| 107 | * @ cxy : remote cluster index. |
---|
| 108 | * @ return true if the fifo is full, false otherwise. |
---|
| 109 | ***********************************************************************************/ |
---|
| 110 | bool_t remote_fifo_is_full( xptr_t fifo ); |
---|
| 111 | |
---|
| 112 | /************************************************************************************ |
---|
| 113 | * Query number ot items in remote fifo. |
---|
| 114 | ************************************************************************************ |
---|
| 115 | * @ fifo : pointer to the fifo in remote cluster. |
---|
| 116 | * @ cxy : remote cluster index. |
---|
| 117 | * @ return number of items. |
---|
| 118 | ***********************************************************************************/ |
---|
| 119 | uint32_t remote_fifo_items( xptr_t fifo ); |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | #endif /* _REMOTE_FIFO_H_ */ |
---|