2 * CFLAGS=-lpthread make pthread_msgqueue
12 typedef struct pthread_mq_t
{
13 static pthread_mutex_t lock
;
14 static pthread_cond_t cond_readable
;
15 static pthread_cond_t cond_writable
;
24 bool pthread_mq_readable(pthread_mq_t
*mq
) { return (mq
->count
> 0); }
25 bool pthread_mq_writable(pthread_mq_t
*mq
) { return (mq
->count
< mq
->count_max
); }
27 bool pthread_mq_init(pthread_mq_t
*mq
, size_t msg_size
, size_t msg_count_max
) {
28 mq
->lock
= PTHREAD_MUTEX_INITIALIZER
;
29 mq
->cond_readable
= PTHREAD_COND_INITIALIZER
;
30 mq
->cond_writable
= PTHREAD_COND_INITIALIZER
;
31 mq
->data
= malloc(msg_size
*msg_count_max
);
32 mq
->msg_size
= msg_size
;
33 mq
->msg_count_max
= msg_count_max
;
38 if(((msg_size
*msg_count_max
) > 0) && mq
->data
== NULL
) return false;
42 void pthread_mq_free(pthread_mq_t
*mq
) {
47 void pthread_mq_cond(pthread_mq_t
*mq
) {
48 if(pthread_mq_readable(mq
)) pthread_cond_broadcast(&mq
->cond_readable
);
49 if(pthread_mq_writable(mq
)) pthread_cond_broadcast(&mq
->cond_writable
);
52 bool pthread_mq_send_generic(pthread_mq_t
*mq
, void * data
, bool to_front
, bool block
, const struct timespec
*restrict abs_timeout
) {
53 pthread_mutex_timedlock(&mq
->lock
, abs_timeout
);
55 while(!pthread_mq_writable(mq
)) {
56 pthread_mutex_unlock(&mq
->lock
);
60 size_t idx
= ( ( mq
->count_max
+ mq
->head_idx
+ (to_front
?-1:mq
->count
) ) % mq
->count_max
);
61 void *ptr
= mq
->data
+ (idx
* mq
->msg_size
);
63 memcpy(ptr
, data
, mq
->msg_size
);
66 pthread_mutex_unlock(&mq
->lock
);
71 mq_timedreceive(3) mq_timedreceive(2)
73 mq_timedsend(3) mq_timedsend(2)
75 void vQueueDelete( QueueHandle_t xQueue );
76 BaseType_t xQueueReset( QueueHandle_t xQueue );
78 BaseType_t xQueueSend(
80 const void * pvItemToQueue,
81 TickType_t xTicksToWait
83 BaseType_t xQueueReceive(
86 TickType_t xTicksToWait