Create RB_ACQUIRE and RB_RELEASE

RB_RELEASE is actually RB_DROP_DATA renamed, but RB_ACQUIRE and
RB_RELEASE work well when working with more structured ring buffer
contents.
This commit is contained in:
Bill Currie 2021-01-23 11:56:45 +09:00
parent e6704b85e1
commit 328a529a94
2 changed files with 25 additions and 5 deletions

View File

@ -52,8 +52,8 @@
/** Return the amount of space available for writing in the ring buffer. /** Return the amount of space available for writing in the ring buffer.
* *
* Use this to know how much data can be written (RB_WRITE_DATA()), or just * Use this to know how much data can be written (RB_WRITE_DATA()) or acquired
* to see if any space is available. * (RB_ACQUIRE()), or just to see if any space is available.
* *
* \note Does NOT affect buffer state. * \note Does NOT affect buffer state.
* *
@ -67,7 +67,7 @@
/** Return the number of objects available in the ring buffer. /** Return the number of objects available in the ring buffer.
* *
* Use this to know how much data can be read (RB_READ_DATA()) or discarded * Use this to know how much data can be read (RB_READ_DATA()) or discarded
* (RB_DROP_DATA()), or just to see if any data is available. * (RB_RELEASE()), or just to see if any data is available.
* *
* \note Does NOT affect buffer state. * \note Does NOT affect buffer state.
* *
@ -106,6 +106,26 @@
memcpy (rb->buffer + h, d, c * sizeof (rb->buffer[0])); \ memcpy (rb->buffer + h, d, c * sizeof (rb->buffer[0])); \
}) })
/** Acquire \a count objects from the buffer, wrapping if necessary.
*
* \note Affects buffer state (advances the head).
*
* \note Does NOT check that the space is available. It is the caller's
* responsitiblity to do so using RB_SPACE_AVAILABLE().
*
* \param ring_buffer The ring buffer to which the data will be written.
* \param count unsigned int. The number of objects to copy from
* \a data.
* \return Address of the first object acquired.
*/
#define RB_ACQUIRE(ring_buffer, count) \
({ __auto_type rb = &(ring_buffer); \
unsigned c = (count); \
unsigned h = rb->head; \
rb->head = (h + c) % RB_buffer_size (rb); \
&rb->buffer[h]; \
})
/** Read \a count objects from the buffer to \a data, wrapping if necessary. /** Read \a count objects from the buffer to \a data, wrapping if necessary.
* *
* \note Affects buffer state (advances the tail). * \note Affects buffer state (advances the tail).
@ -147,7 +167,7 @@
* discarded. * discarded.
* \param count The number of objects to discard. * \param count The number of objects to discard.
*/ */
#define RB_DROP_DATA(ring_buffer, count) \ #define RB_RELEASE(ring_buffer, count) \
({ __auto_type rb = &(ring_buffer); \ ({ __auto_type rb = &(ring_buffer); \
unsigned c = (count); \ unsigned c = (count); \
unsigned t = rb->tail; \ unsigned t = rb->tail; \

View File

@ -795,7 +795,7 @@ process_commands (qwaq_resources_t *res)
break; break;
} }
pthread_mutex_lock (&res->command_cond.mut); pthread_mutex_lock (&res->command_cond.mut);
RB_DROP_DATA (res->command_queue, len); RB_RELEASE (res->command_queue, len);
pthread_cond_broadcast (&res->command_cond.wcond); pthread_cond_broadcast (&res->command_cond.wcond);
// unlocked at top of top if there's more data, otherwise just after // unlocked at top of top if there's more data, otherwise just after
// the loop // the loop