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