Changes in uspace/drv/char/i8042/buffer.h [65ffec3:2df6f6fe] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/char/i8042/buffer.h
r65ffec3 r2df6f6fe 26 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 27 */ 28 28 29 /** 29 30 * @addtogroup kbd 30 31 * @{ 31 32 */ 33 32 34 /** @file 33 35 * @brief Cyclic buffer structure. … … 46 48 * Attempt to insert byte into the full buffer will block until it can succeed. 47 49 * Attempt to read from empty buffer will block until it can succeed. 50 * 48 51 */ 49 52 typedef 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. */ 56 59 } buffer_t; 57 60 58 61 /** Initialize cyclic buffer using provided memory space. 62 * 59 63 * @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 * 62 67 */ 63 68 static inline void buffer_init(buffer_t *buffer, uint8_t *data, size_t size) 64 69 { 65 70 assert(buffer); 71 66 72 fibril_mutex_initialize(&buffer->guard); 67 73 fibril_condvar_initialize(&buffer->change); … … 74 80 75 81 /** Write byte to cyclic buffer. 82 * 76 83 * @param buffer Cyclic buffer to write to. 77 * @param data Data to write. 84 * @param data Data to write. 85 * 78 86 */ 79 87 static inline void buffer_write(buffer_t *buffer, uint8_t data) 80 88 { 81 89 fibril_mutex_lock(&buffer->guard); 82 90 83 91 /* Next position. */ 84 92 uint8_t *new_head = buffer->write_head + 1; 85 93 if (new_head == buffer->buffer_end) 86 94 new_head = buffer->buffer; 87 95 88 96 /* Buffer full. */ 89 97 while (new_head == buffer->read_head) 90 98 fibril_condvar_wait(&buffer->change, &buffer->guard); 91 99 92 100 /* Write data. */ 93 101 *buffer->write_head = data; 94 102 95 103 /* Buffer was empty. */ 96 104 if (buffer->write_head == buffer->read_head) 97 105 fibril_condvar_broadcast(&buffer->change); 98 106 99 107 /* Move head */ 100 108 buffer->write_head = new_head; … … 103 111 104 112 /** Read byte from cyclic buffer. 113 * 105 114 * @param buffer Cyclic buffer to read from. 115 * 106 116 * @return Byte read. 117 * 107 118 */ 108 119 static inline uint8_t buffer_read(buffer_t *buffer) 109 120 { 110 121 fibril_mutex_lock(&buffer->guard); 122 111 123 /* Buffer is empty. */ 112 124 while (buffer->write_head == buffer->read_head) 113 125 fibril_condvar_wait(&buffer->change, &buffer->guard); 114 126 115 127 /* Next position. */ 116 128 uint8_t *new_head = buffer->read_head + 1; 117 129 if (new_head == buffer->buffer_end) 118 130 new_head = buffer->buffer; 119 131 120 132 /* Read data. */ 121 133 const uint8_t data = *buffer->read_head; 122 134 123 135 /* Buffer was full. */ 124 136 uint8_t *new_write_head = buffer->write_head + 1; … … 127 139 if (new_write_head == buffer->read_head) 128 140 fibril_condvar_broadcast(&buffer->change); 129 141 130 142 /* Move head */ 131 143 buffer->read_head = new_head; 132 144 133 145 fibril_mutex_unlock(&buffer->guard); 134 146 return data; 135 147 } 148 136 149 #endif 150 137 151 /** 138 152 * @}
Note:
See TracChangeset
for help on using the changeset viewer.