-int main() {
- pthread_mq_t myq;
- pthread_mq_init(&myq, 6, 5);
- pthread_mq_send_generic(&myq, "AHOJ1", false, true, NULL);
- pthread_mq_send_generic(&myq, "AHOJ2", false, true, NULL);
- pthread_mq_send_generic(&myq, "AHOJ3", true, true, NULL);
- pthread_mq_send_generic(&myq, "AHOJ4", true, true, NULL);
- pthread_mq_send_generic(&myq, "AHOJ5", false, true, NULL);
- //pthread_mq_send_generic(&myq, "AHOJ6", false, true, NULL);
-
- for(int i = 0; i<5; i++) {
- printf("%.6s\n", (char *)(myq.data+i*6));
+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
+ if(pthread_mutex_timedlock(&mq->lock, abs_timeout)) return false;
+
+ //Wait for queue to be in readable condition
+ while(!pthread_mq_readable(mq)) {
+ if(abs_timeout == NULL) {
+ ret = pthread_cond_wait(&mq->cond_readable, &mq->lock);
+ } else {
+ //printf("RTimed: %p\n", abs_timeout);
+ //assert(abs_timeout != NULL);
+ ret = pthread_cond_timedwait(&mq->cond_readable, &mq->lock, abs_timeout);
+ }
+ if(ret) {
+ pthread_mutex_unlock(&mq->lock);
+ return false;
+ }
+ }
+
+ //Read data from queue
+ void *ptr = mq->data + (mq->head_idx * mq->msg_size);
+ 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;