Commit | Line | Data |
---|---|---|
90c3a0d4 | 1 | #include <pthread.h> |
3aac2619 | 2 | #include <pthread_extra.h> |
90c3a0d4 TM |
3 | #include <time.h> |
4 | #include <string.h> | |
5 | #include <stdio.h> | |
6 | #include <stdbool.h> | |
7 | #include <stdlib.h> | |
774d1b14 | 8 | #include <assert.h> |
90c3a0d4 | 9 | |
774d1b14 TM |
10 | bool pthread_mq_readable(pthread_mq_t *mq) { return (mq->msg_count > 0); } |
11 | bool pthread_mq_writable(pthread_mq_t *mq) { return (mq->msg_count < mq->msg_count_max); } | |
90c3a0d4 TM |
12 | |
13 | bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max) { | |
774d1b14 TM |
14 | pthread_mutex_init(&mq->lock, NULL); |
15 | pthread_cond_init(&mq->cond_readable, NULL); | |
16 | pthread_cond_init(&mq->cond_writable, NULL); | |
63658368 | 17 | //pthread_mutexattr_setclock(&mq->lock, CLOCK_MONOTONIC); |
90c3a0d4 TM |
18 | mq->data = malloc(msg_size*msg_count_max); |
19 | mq->msg_size = msg_size; | |
20 | mq->msg_count_max = msg_count_max; | |
21 | mq->msg_count = 0; | |
22 | mq->head_idx = 0; | |
23 | mq->name = NULL; | |
24 | ||
25 | if(((msg_size*msg_count_max) > 0) && mq->data == NULL) return false; | |
26 | return true; | |
27 | } | |
28 | ||
29 | void pthread_mq_free(pthread_mq_t *mq) { | |
30 | free(mq->data); | |
31 | } | |
32 | ||
90c3a0d4 TM |
33 | void pthread_mq_cond(pthread_mq_t *mq) { |
34 | if(pthread_mq_readable(mq)) pthread_cond_broadcast(&mq->cond_readable); | |
35 | if(pthread_mq_writable(mq)) pthread_cond_broadcast(&mq->cond_writable); | |
36 | } | |
37 | ||
63658368 TM |
38 | size_t pthread_mq_waiting(pthread_mq_t *mq) { |
39 | return mq->msg_count; | |
40 | } | |
41 | ||
77c7dc7f TM |
42 | size_t pthread_mq_vacant(pthread_mq_t *mq) { |
43 | return (mq->msg_count_max - mq->msg_count); | |
44 | } | |
45 | ||
774d1b14 TM |
46 | bool pthread_mq_reset(pthread_mq_t *mq) { |
47 | if(pthread_mutex_lock(&mq->lock)) return false; | |
48 | mq->msg_count = 0; | |
49 | mq->head_idx = 0; | |
50 | pthread_mq_cond(mq); | |
51 | pthread_mutex_unlock(&mq->lock); | |
52 | return true; | |
53 | } | |
54 | ||
9e057e1d | 55 | bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout) { |
5251b63c | 56 | //printf("S-Timed: %p\n", abs_timeout); |
3297654e TM |
57 | int ret; |
58 | ||
774d1b14 TM |
59 | //Lock queue |
60 | if(pthread_mutex_timedlock(&mq->lock, abs_timeout)) return false; | |
90c3a0d4 | 61 | |
774d1b14 | 62 | //Wait for queue to be in writable condition |
90c3a0d4 | 63 | while(!pthread_mq_writable(mq)) { |
5251b63c | 64 | //printf("S+Timed: %p\n", abs_timeout); |
3297654e TM |
65 | if(abs_timeout == NULL) { |
66 | ret = pthread_cond_wait(&mq->cond_writable, &mq->lock); | |
67 | } else { | |
5251b63c | 68 | //printf("STimed: %p\n", abs_timeout); |
e7d1ce5c | 69 | //assert(abs_timeout != NULL); |
3297654e TM |
70 | ret = pthread_cond_timedwait(&mq->cond_writable, &mq->lock, abs_timeout); |
71 | } | |
72 | if(ret) { | |
774d1b14 TM |
73 | pthread_mutex_unlock(&mq->lock); |
74 | return false; | |
75 | } | |
90c3a0d4 TM |
76 | } |
77 | ||
774d1b14 | 78 | //Write data to queue |
9e057e1d | 79 | bool to_front = (flags & PTHREAD_XMQ_FRONT); |
5251b63c | 80 | size_t idx = ( ( mq->head_idx + (to_front?mq->msg_count_max-1:mq->msg_count) ) % mq->msg_count_max ); |
90c3a0d4 TM |
81 | void *ptr = mq->data + (idx * mq->msg_size); |
82 | mq->msg_count++; | |
774d1b14 | 83 | if(to_front) mq->head_idx = (mq->msg_count_max + mq->head_idx - 1) % mq->msg_count_max; |
90c3a0d4 TM |
84 | memcpy(ptr, data, mq->msg_size); |
85 | ||
774d1b14 | 86 | //Signal conditions and unlock |
90c3a0d4 TM |
87 | pthread_mq_cond(mq); |
88 | pthread_mutex_unlock(&mq->lock); | |
89 | return true; | |
90 | } | |
91 | ||
9e057e1d | 92 | bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout) { |
63658368 TM |
93 | int ret; |
94 | ||
95 | //Lock queue | |
96 | if(pthread_mutex_timedlock(&mq->lock, abs_timeout)) return false; | |
97 | ||
98 | //Wait for queue to be in readable condition | |
99 | while(!pthread_mq_readable(mq)) { | |
100 | if(abs_timeout == NULL) { | |
101 | ret = pthread_cond_wait(&mq->cond_readable, &mq->lock); | |
102 | } else { | |
5251b63c | 103 | //printf("RTimed: %p\n", abs_timeout); |
e7d1ce5c | 104 | //assert(abs_timeout != NULL); |
63658368 TM |
105 | ret = pthread_cond_timedwait(&mq->cond_readable, &mq->lock, abs_timeout); |
106 | } | |
107 | if(ret) { | |
108 | pthread_mutex_unlock(&mq->lock); | |
109 | return false; | |
110 | } | |
111 | } | |
112 | ||
113 | //Read data from queue | |
114 | void *ptr = mq->data + (mq->head_idx * mq->msg_size); | |
115 | memcpy(data, ptr, mq->msg_size); | |
116 | ||
117 | //Delete data from queue if not peeking | |
9e057e1d | 118 | bool peek = (flags & PTHREAD_XMQ_PEEK); |
63658368 TM |
119 | if(!peek) { |
120 | mq->msg_count--; | |
121 | mq->head_idx = (mq->head_idx+1) % mq->msg_count_max; | |
122 | } | |
123 | ||
124 | //Signal conditions and unlock | |
125 | pthread_mq_cond(mq); | |
126 | pthread_mutex_unlock(&mq->lock); | |
127 | return true; | |
128 | } |