From: Tomas Mudrunka Date: Tue, 29 Jun 2021 12:43:18 +0000 (+0200) Subject: Queues now use flags X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=9e057e1d605cf0960fa40ecf6825aa89d9371f78;p=mirrors%2FPrograms.git Queues now use flags --- diff --git a/c/pthread_extra/pthread_extra.h b/c/pthread_extra/pthread_extra.h index 861b847..faffb3d 100644 --- a/c/pthread_extra/pthread_extra.h +++ b/c/pthread_extra/pthread_extra.h @@ -44,11 +44,14 @@ int pthread_unpause(pthread_t thread); // Message queues -#define PTHREAD_XMQ_FRONT true -#define PTHREAD_XMQ_BACK false - -#define PTHREAD_XMQ_RECV false -#define PTHREAD_XMQ_PEEK true +//Flags +typedef uint8_t pthread_mq_flags_t; +#define PTHREAD_XMQ_NONE 0 ///< No flags specified (default behaviour) +#define PTHREAD_XMQ_FRONT 1 ///< Send to front of the queue (scheduled for next receive) +#define PTHREAD_XMQ_BACK PTHREAD_XMQ_NONE ///< Send to back of the queue (default) +#define PTHREAD_XMQ_PEEK 2 ///< Only peek, do not remove received item +#define PTHREAD_XMQ_RECV PTHREAD_XMQ_NONE ///< Remove received item from queue (default) +#define PTHREAD_XMQ_OVERW 4 ///< Overwrite item if queue full typedef struct pthread_mq_t { pthread_mutex_t lock; @@ -66,8 +69,8 @@ bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max); void pthread_mq_free(pthread_mq_t *mq); bool pthread_mq_reset(pthread_mq_t *mq); -bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const struct timespec *restrict abs_timeout); -bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const struct timespec *restrict abs_timeout); +bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout); +bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout); size_t pthread_mq_waiting(pthread_mq_t *mq); size_t pthread_mq_vacant(pthread_mq_t *mq); diff --git a/c/pthread_extra/pthread_msgqueue.c b/c/pthread_extra/pthread_msgqueue.c index b856296..52395d6 100644 --- a/c/pthread_extra/pthread_msgqueue.c +++ b/c/pthread_extra/pthread_msgqueue.c @@ -52,7 +52,7 @@ bool pthread_mq_reset(pthread_mq_t *mq) { return true; } -bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const struct timespec *restrict abs_timeout) { +bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout) { //printf("S-Timed: %p\n", abs_timeout); int ret; @@ -76,6 +76,7 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const } //Write data to queue + bool to_front = (flags & PTHREAD_XMQ_FRONT); size_t idx = ( ( mq->head_idx + (to_front?mq->msg_count_max-1:mq->msg_count) ) % mq->msg_count_max ); void *ptr = mq->data + (idx * mq->msg_size); mq->msg_count++; @@ -88,7 +89,7 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const return true; } -bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const struct timespec *restrict abs_timeout) { +bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout) { int ret; //Lock queue @@ -114,6 +115,7 @@ bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const memcpy(data, ptr, mq->msg_size); //Delete data from queue if not peeking + bool peek = (flags & PTHREAD_XMQ_PEEK); if(!peek) { mq->msg_count--; mq->head_idx = (mq->head_idx+1) % mq->msg_count_max;