| [9f97ffe] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| [2df6f6fe] | 28 |
|
|---|
| [65ffec3] | 29 | /**
|
|---|
| 30 | * @addtogroup kbd
|
|---|
| [9f97ffe] | 31 | * @{
|
|---|
| 32 | */
|
|---|
| [2df6f6fe] | 33 |
|
|---|
| [9f97ffe] | 34 | /** @file
|
|---|
| [65ffec3] | 35 | * @brief Cyclic buffer structure.
|
|---|
| [9f97ffe] | 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #ifndef BUFFER_H_
|
|---|
| 39 | #define BUFFER_H_
|
|---|
| 40 |
|
|---|
| 41 | #include <assert.h>
|
|---|
| 42 | #include <fibril_synch.h>
|
|---|
| 43 |
|
|---|
| [65ffec3] | 44 | /** Cyclic buffer that blocks on full/empty.
|
|---|
| 45 | *
|
|---|
| 46 | * read_head == write_head means that the buffer is empty.
|
|---|
| 47 | * write_head + 1 == read_head means that the buffer is full.
|
|---|
| 48 | * Attempt to insert byte into the full buffer will block until it can succeed.
|
|---|
| 49 | * Attempt to read from empty buffer will block until it can succeed.
|
|---|
| [2df6f6fe] | 50 | *
|
|---|
| [65ffec3] | 51 | */
|
|---|
| [9f97ffe] | 52 | typedef struct {
|
|---|
| [2df6f6fe] | 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. */
|
|---|
| [9f97ffe] | 59 | } buffer_t;
|
|---|
| 60 |
|
|---|
| [65ffec3] | 61 | /** Initialize cyclic buffer using provided memory space.
|
|---|
| [2df6f6fe] | 62 | *
|
|---|
| [65ffec3] | 63 | * @param buffer Cyclic buffer structure to initialize.
|
|---|
| [2df6f6fe] | 64 | * @param data Memory space to use.
|
|---|
| 65 | * @param size Size of the memory place.
|
|---|
| 66 | *
|
|---|
| [65ffec3] | 67 | */
|
|---|
| [9f97ffe] | 68 | static inline void buffer_init(buffer_t *buffer, uint8_t *data, size_t size)
|
|---|
| 69 | {
|
|---|
| 70 | assert(buffer);
|
|---|
| [2df6f6fe] | 71 |
|
|---|
| [9f97ffe] | 72 | fibril_mutex_initialize(&buffer->guard);
|
|---|
| 73 | fibril_condvar_initialize(&buffer->change);
|
|---|
| 74 | buffer->buffer = data;
|
|---|
| 75 | buffer->buffer_end = data + size;
|
|---|
| 76 | buffer->read_head = buffer->buffer;
|
|---|
| 77 | buffer->write_head = buffer->buffer;
|
|---|
| 78 | bzero(buffer->buffer, size);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| [65ffec3] | 81 | /** Write byte to cyclic buffer.
|
|---|
| [2df6f6fe] | 82 | *
|
|---|
| [65ffec3] | 83 | * @param buffer Cyclic buffer to write to.
|
|---|
| [2df6f6fe] | 84 | * @param data Data to write.
|
|---|
| 85 | *
|
|---|
| [65ffec3] | 86 | */
|
|---|
| [9f97ffe] | 87 | static inline void buffer_write(buffer_t *buffer, uint8_t data)
|
|---|
| 88 | {
|
|---|
| 89 | fibril_mutex_lock(&buffer->guard);
|
|---|
| [2df6f6fe] | 90 |
|
|---|
| [9f97ffe] | 91 | /* Next position. */
|
|---|
| 92 | uint8_t *new_head = buffer->write_head + 1;
|
|---|
| 93 | if (new_head == buffer->buffer_end)
|
|---|
| 94 | new_head = buffer->buffer;
|
|---|
| [2df6f6fe] | 95 |
|
|---|
| [9f97ffe] | 96 | /* Buffer full. */
|
|---|
| 97 | while (new_head == buffer->read_head)
|
|---|
| 98 | fibril_condvar_wait(&buffer->change, &buffer->guard);
|
|---|
| [2df6f6fe] | 99 |
|
|---|
| [9f97ffe] | 100 | /* Write data. */
|
|---|
| 101 | *buffer->write_head = data;
|
|---|
| [2df6f6fe] | 102 |
|
|---|
| [9f97ffe] | 103 | /* Buffer was empty. */
|
|---|
| 104 | if (buffer->write_head == buffer->read_head)
|
|---|
| 105 | fibril_condvar_broadcast(&buffer->change);
|
|---|
| [2df6f6fe] | 106 |
|
|---|
| [9f97ffe] | 107 | /* Move head */
|
|---|
| 108 | buffer->write_head = new_head;
|
|---|
| 109 | fibril_mutex_unlock(&buffer->guard);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| [65ffec3] | 112 | /** Read byte from cyclic buffer.
|
|---|
| [2df6f6fe] | 113 | *
|
|---|
| [65ffec3] | 114 | * @param buffer Cyclic buffer to read from.
|
|---|
| [2df6f6fe] | 115 | *
|
|---|
| [65ffec3] | 116 | * @return Byte read.
|
|---|
| [2df6f6fe] | 117 | *
|
|---|
| [65ffec3] | 118 | */
|
|---|
| [9f97ffe] | 119 | static inline uint8_t buffer_read(buffer_t *buffer)
|
|---|
| 120 | {
|
|---|
| 121 | fibril_mutex_lock(&buffer->guard);
|
|---|
| [2df6f6fe] | 122 |
|
|---|
| [9f97ffe] | 123 | /* Buffer is empty. */
|
|---|
| 124 | while (buffer->write_head == buffer->read_head)
|
|---|
| 125 | fibril_condvar_wait(&buffer->change, &buffer->guard);
|
|---|
| [2df6f6fe] | 126 |
|
|---|
| [9f97ffe] | 127 | /* Next position. */
|
|---|
| 128 | uint8_t *new_head = buffer->read_head + 1;
|
|---|
| 129 | if (new_head == buffer->buffer_end)
|
|---|
| 130 | new_head = buffer->buffer;
|
|---|
| [2df6f6fe] | 131 |
|
|---|
| [9f97ffe] | 132 | /* Read data. */
|
|---|
| 133 | const uint8_t data = *buffer->read_head;
|
|---|
| [2df6f6fe] | 134 |
|
|---|
| [9f97ffe] | 135 | /* Buffer was full. */
|
|---|
| 136 | uint8_t *new_write_head = buffer->write_head + 1;
|
|---|
| 137 | if (new_write_head == buffer->buffer_end)
|
|---|
| 138 | new_write_head = buffer->buffer;
|
|---|
| [32ba5a6] | 139 | if (new_write_head == buffer->read_head)
|
|---|
| [9f97ffe] | 140 | fibril_condvar_broadcast(&buffer->change);
|
|---|
| [2df6f6fe] | 141 |
|
|---|
| [9f97ffe] | 142 | /* Move head */
|
|---|
| 143 | buffer->read_head = new_head;
|
|---|
| [2df6f6fe] | 144 |
|
|---|
| [9f97ffe] | 145 | fibril_mutex_unlock(&buffer->guard);
|
|---|
| 146 | return data;
|
|---|
| 147 | }
|
|---|
| [2df6f6fe] | 148 |
|
|---|
| [9f97ffe] | 149 | #endif
|
|---|
| [2df6f6fe] | 150 |
|
|---|
| [9f97ffe] | 151 | /**
|
|---|
| 152 | * @}
|
|---|
| 153 | */
|
|---|