Changeset 786bd56 in mainline for uspace/drv/char/i8042/buffer.h


Ignore:
Timestamp:
2012-01-24T09:26:36Z (12 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
cd1cc4e6
Parents:
304faab (diff), 2df6f6fe (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge with mainline

File:
1 edited

Legend:

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

    r304faab r786bd56  
    2626 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2727 */
     28
    2829/**
    2930 * @addtogroup kbd
    3031 * @{
    3132 */
     33
    3234/** @file
    3335 * @brief Cyclic buffer structure.
     
    4648 * Attempt to insert byte into the full buffer will block until it can succeed.
    4749 * Attempt to read from empty buffer will block until it can succeed.
     50 *
    4851 */
    4952typedef struct {
    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. */
     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. */
    5659} buffer_t;
    5760
    5861/** Initialize cyclic buffer using provided memory space.
     62 *
    5963 * @param buffer Cyclic buffer structure to initialize.
    60  * @param data Memory space to use.
    61  * @param size Size of the memory place.
     64 * @param data   Memory space to use.
     65 * @param size   Size of the memory place.
     66 *
    6267 */
    6368static inline void buffer_init(buffer_t *buffer, uint8_t *data, size_t size)
    6469{
    6570        assert(buffer);
     71       
    6672        fibril_mutex_initialize(&buffer->guard);
    6773        fibril_condvar_initialize(&buffer->change);
     
    7480
    7581/** Write byte to cyclic buffer.
     82 *
    7683 * @param buffer Cyclic buffer to write to.
    77  * @param data Data to write.
     84 * @param data   Data to write.
     85 *
    7886 */
    7987static inline void buffer_write(buffer_t *buffer, uint8_t data)
    8088{
    8189        fibril_mutex_lock(&buffer->guard);
    82 
     90       
    8391        /* Next position. */
    8492        uint8_t *new_head = buffer->write_head + 1;
    8593        if (new_head == buffer->buffer_end)
    8694                new_head = buffer->buffer;
    87 
     95       
    8896        /* Buffer full. */
    8997        while (new_head == buffer->read_head)
    9098                fibril_condvar_wait(&buffer->change, &buffer->guard);
    91 
     99       
    92100        /* Write data. */
    93101        *buffer->write_head = data;
    94 
     102       
    95103        /* Buffer was empty. */
    96104        if (buffer->write_head == buffer->read_head)
    97105                fibril_condvar_broadcast(&buffer->change);
    98 
     106       
    99107        /* Move head */
    100108        buffer->write_head = new_head;
     
    103111
    104112/** Read byte from cyclic buffer.
     113 *
    105114 * @param buffer Cyclic buffer to read from.
     115 *
    106116 * @return Byte read.
     117 *
    107118 */
    108119static inline uint8_t buffer_read(buffer_t *buffer)
    109120{
    110121        fibril_mutex_lock(&buffer->guard);
     122       
    111123        /* Buffer is empty. */
    112124        while (buffer->write_head == buffer->read_head)
    113125                fibril_condvar_wait(&buffer->change, &buffer->guard);
    114 
     126       
    115127        /* Next position. */
    116128        uint8_t *new_head = buffer->read_head + 1;
    117129        if (new_head == buffer->buffer_end)
    118130                new_head = buffer->buffer;
    119 
     131       
    120132        /* Read data. */
    121133        const uint8_t data = *buffer->read_head;
    122 
     134       
    123135        /* Buffer was full. */
    124136        uint8_t *new_write_head = buffer->write_head + 1;
     
    127139        if (new_write_head == buffer->read_head)
    128140                fibril_condvar_broadcast(&buffer->change);
    129 
     141       
    130142        /* Move head */
    131143        buffer->read_head = new_head;
    132 
     144       
    133145        fibril_mutex_unlock(&buffer->guard);
    134146        return data;
    135147}
     148
    136149#endif
     150
    137151/**
    138152 * @}
Note: See TracChangeset for help on using the changeset viewer.