Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/char/i8042/buffer.h

    r2df6f6fe r65ffec3  
    2626 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2727 */
    28 
    2928/**
    3029 * @addtogroup kbd
    3130 * @{
    3231 */
    33 
    3432/** @file
    3533 * @brief Cyclic buffer structure.
     
    4846 * Attempt to insert byte into the full buffer will block until it can succeed.
    4947 * Attempt to read from empty buffer will block until it can succeed.
    50  *
    5148 */
    5249typedef struct {
    53         uint8_t *buffer;          /**< Storage space. */
    54         uint8_t *buffer_end;      /**< End of storage place. */
    55         fibril_mutex_t guard;     /**< Protects buffer structures. */
    56         fibril_condvar_t change;  /**< Indicates change (empty/full). */
    57         uint8_t *read_head;       /**< Place of the next readable element. */
    58         uint8_t *write_head;      /**< Pointer to the next writable place. */
     50        uint8_t *buffer;         /**< Storage space. */
     51        uint8_t *buffer_end;     /**< End of storage place. */
     52        fibril_mutex_t guard;    /**< Protects buffer structures. */
     53        fibril_condvar_t change; /**< Indicates change (empty/full). */
     54        uint8_t *read_head;      /**< Place of the next readable element. */
     55        uint8_t *write_head;     /**< Pointer to the next writable place. */
    5956} buffer_t;
    6057
    6158/** Initialize cyclic buffer using provided memory space.
    62  *
    6359 * @param buffer Cyclic buffer structure to initialize.
    64  * @param data   Memory space to use.
    65  * @param size   Size of the memory place.
    66  *
     60 * @param data Memory space to use.
     61 * @param size Size of the memory place.
    6762 */
    6863static inline void buffer_init(buffer_t *buffer, uint8_t *data, size_t size)
    6964{
    7065        assert(buffer);
    71        
    7266        fibril_mutex_initialize(&buffer->guard);
    7367        fibril_condvar_initialize(&buffer->change);
     
    8074
    8175/** Write byte to cyclic buffer.
    82  *
    8376 * @param buffer Cyclic buffer to write to.
    84  * @param data   Data to write.
    85  *
     77 * @param data Data to write.
    8678 */
    8779static inline void buffer_write(buffer_t *buffer, uint8_t data)
    8880{
    8981        fibril_mutex_lock(&buffer->guard);
    90        
     82
    9183        /* Next position. */
    9284        uint8_t *new_head = buffer->write_head + 1;
    9385        if (new_head == buffer->buffer_end)
    9486                new_head = buffer->buffer;
    95        
     87
    9688        /* Buffer full. */
    9789        while (new_head == buffer->read_head)
    9890                fibril_condvar_wait(&buffer->change, &buffer->guard);
    99        
     91
    10092        /* Write data. */
    10193        *buffer->write_head = data;
    102        
     94
    10395        /* Buffer was empty. */
    10496        if (buffer->write_head == buffer->read_head)
    10597                fibril_condvar_broadcast(&buffer->change);
    106        
     98
    10799        /* Move head */
    108100        buffer->write_head = new_head;
     
    111103
    112104/** Read byte from cyclic buffer.
    113  *
    114105 * @param buffer Cyclic buffer to read from.
    115  *
    116106 * @return Byte read.
    117  *
    118107 */
    119108static inline uint8_t buffer_read(buffer_t *buffer)
    120109{
    121110        fibril_mutex_lock(&buffer->guard);
    122        
    123111        /* Buffer is empty. */
    124112        while (buffer->write_head == buffer->read_head)
    125113                fibril_condvar_wait(&buffer->change, &buffer->guard);
    126        
     114
    127115        /* Next position. */
    128116        uint8_t *new_head = buffer->read_head + 1;
    129117        if (new_head == buffer->buffer_end)
    130118                new_head = buffer->buffer;
    131        
     119
    132120        /* Read data. */
    133121        const uint8_t data = *buffer->read_head;
    134        
     122
    135123        /* Buffer was full. */
    136124        uint8_t *new_write_head = buffer->write_head + 1;
     
    139127        if (new_write_head == buffer->read_head)
    140128                fibril_condvar_broadcast(&buffer->change);
    141        
     129
    142130        /* Move head */
    143131        buffer->read_head = new_head;
    144        
     132
    145133        fibril_mutex_unlock(&buffer->guard);
    146134        return data;
    147135}
    148 
    149136#endif
    150 
    151137/**
    152138 * @}
Note: See TracChangeset for help on using the changeset viewer.