| 1 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File : mwmr_channel.h |
|---|
| 3 | // Date : 01/04/2012 |
|---|
| 4 | // Author : alain greiner |
|---|
| 5 | // Copyright (c) UPMC-LIP6 |
|---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 7 | // The mwmr_channel.c and mwmr_channel.h files are part of the GIET nano-kernel. |
|---|
| 8 | // This middleware implements a user level Multi-Writers / Multi-Readers |
|---|
| 9 | // communication channel, that can be used by parallel multi-tasks applications |
|---|
| 10 | // respecting the TCG (Tasks and Communications Graph) formalism. |
|---|
| 11 | // |
|---|
| 12 | // The mwmr_read() and mwmr_write() functions do not require a system call. |
|---|
| 13 | // The channel must have been allocated in a non cacheable segment, |
|---|
| 14 | // if the platform does not provide hardware cache coherence. |
|---|
| 15 | // |
|---|
| 16 | // An MWMR transaction transfer an integer number of items, and an item is |
|---|
| 17 | // an integer number of uint32_t (32 bits words). |
|---|
| 18 | // The max number of words that can be stored in a MWMR channel is defined by the |
|---|
| 19 | // "depth" parameter, and the "width" parameter define the minimal number of |
|---|
| 20 | // word contained in an atomic item. Therefore, the "depth" parameter must be |
|---|
| 21 | // a multiple of the "width" parameter. |
|---|
| 22 | // |
|---|
| 23 | // Both the mwmr_read() and mwmr_write() functions are blocking functions. |
|---|
| 24 | // A queuing lock provides exclusive access to the MWMR channel. |
|---|
| 25 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 26 | |
|---|
| 27 | #ifndef _MWMR_CHANNEL_H_ |
|---|
| 28 | #define _MWMR_CHANNEL_H_ |
|---|
| 29 | |
|---|
| 30 | #include "user_lock.h" |
|---|
| 31 | #include "stdint.h" |
|---|
| 32 | |
|---|
| 33 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 34 | // MWMR channel |
|---|
| 35 | // This structure define a - shared - communication buffer between threads. |
|---|
| 36 | // It must be declared as a global variable. |
|---|
| 37 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | |
|---|
| 39 | typedef struct mwmr_channel_s |
|---|
| 40 | { |
|---|
| 41 | user_lock_t lock; // exclusive access lock |
|---|
| 42 | uint32_t sts; // number of words available |
|---|
| 43 | uint32_t ptr; // index of the first valid data word |
|---|
| 44 | uint32_t ptw; // index of the first empty slot |
|---|
| 45 | uint32_t depth; // max number of words in the channel |
|---|
| 46 | uint32_t width; // number of words in an item |
|---|
| 47 | uint32_t* data; // circular buffer base address |
|---|
| 48 | } mwmr_channel_t; |
|---|
| 49 | |
|---|
| 50 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 51 | // MWMR bufio |
|---|
| 52 | // This structure define a - private - input or output buffer, that can be used |
|---|
| 53 | // to move data to (or ftom a shared MWMR communication channel. |
|---|
| 54 | // It is a local variable in the stack of the reader/writer thread. |
|---|
| 55 | // It can be used to simplify access to a MWMR channel when the transfered |
|---|
| 56 | // data have a non fixed format, and must be analysed or produced byte per byte. |
|---|
| 57 | // An input buffer is automatically refill when it becomes empty. |
|---|
| 58 | // An output buffer is automatically flushed when it becomes full. |
|---|
| 59 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 60 | |
|---|
| 61 | typedef struct mwmr_bufio_s |
|---|
| 62 | { |
|---|
| 63 | uint32_t is_input; // input buffer if non zero |
|---|
| 64 | uint32_t ptr; // current byte index (0 to max-1) |
|---|
| 65 | uint8_t* base; // data buffer base address |
|---|
| 66 | uint32_t max; // number of bytes after refill (only for input bufio) |
|---|
| 67 | uint32_t nitems; // buffer size (number of items) |
|---|
| 68 | uint32_t nbytes; // buffer size (number of bytes) |
|---|
| 69 | mwmr_channel_t* mwmr; // associated MWMR channel |
|---|
| 70 | } mwmr_bufio_t; |
|---|
| 71 | |
|---|
| 72 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 73 | // MWMR channel access functions |
|---|
| 74 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 75 | |
|---|
| 76 | void mwmr_init( mwmr_channel_t* mwmr, |
|---|
| 77 | uint32_t* buffer, |
|---|
| 78 | uint32_t width, // number of words per item |
|---|
| 79 | uint32_t nitems ); // max number of items |
|---|
| 80 | |
|---|
| 81 | void mwmr_dump( mwmr_channel_t* mwmr ); |
|---|
| 82 | |
|---|
| 83 | void mwmr_read( mwmr_channel_t* mwmr, |
|---|
| 84 | uint32_t* buffer, |
|---|
| 85 | uint32_t items ); |
|---|
| 86 | |
|---|
| 87 | void mwmr_write( mwmr_channel_t* mwmr, |
|---|
| 88 | uint32_t* buffer, |
|---|
| 89 | uint32_t items ); |
|---|
| 90 | |
|---|
| 91 | uint32_t nb_mwmr_read ( mwmr_channel_t* mwmr, |
|---|
| 92 | uint32_t* buffer, |
|---|
| 93 | uint32_t items ); |
|---|
| 94 | |
|---|
| 95 | uint32_t nb_mwmr_write( mwmr_channel_t* mwmr, |
|---|
| 96 | uint32_t* buffer, |
|---|
| 97 | uint32_t items ); |
|---|
| 98 | |
|---|
| 99 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 100 | // MWMR bufio access functions |
|---|
| 101 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 102 | |
|---|
| 103 | void mwmr_bufio_init( mwmr_bufio_t* bufio, |
|---|
| 104 | uint8_t* buffer, |
|---|
| 105 | uint32_t size, // number of bytes |
|---|
| 106 | uint32_t is_input, |
|---|
| 107 | mwmr_channel_t* mwmr ); |
|---|
| 108 | |
|---|
| 109 | void mwmr_bufio_dump( mwmr_bufio_t* bufio ); |
|---|
| 110 | |
|---|
| 111 | uint8_t mwmr_bufio_read_byte( mwmr_bufio_t* bufio ); |
|---|
| 112 | |
|---|
| 113 | void mwmr_bufio_skip( mwmr_bufio_t* bufio, |
|---|
| 114 | uint32_t length ); |
|---|
| 115 | |
|---|
| 116 | void mwmr_bufio_align( mwmr_bufio_t* bufio ); |
|---|
| 117 | |
|---|
| 118 | void mwmr_bufio_write_byte( mwmr_bufio_t* bufio, |
|---|
| 119 | uint8_t value ); |
|---|
| 120 | |
|---|
| 121 | void mwmr_bufio_flush( mwmr_bufio_t* bufio ); |
|---|
| 122 | |
|---|
| 123 | #endif |
|---|
| 124 | |
|---|
| 125 | // Local Variables: |
|---|
| 126 | // tab-width: 4 |
|---|
| 127 | // c-basic-offset: 4 |
|---|
| 128 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 129 | // indent-tabs-mode: nil |
|---|
| 130 | // End: |
|---|
| 131 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 132 | |
|---|