Commit | Line | Data |
---|---|---|
90c3a0d4 TM |
1 | /* |
2 | * CFLAGS=-lpthread make pthread_msgqueue | |
3 | */ | |
4 | ||
5 | #include <pthread.h> | |
6 | #include <time.h> | |
7 | #include <string.h> | |
8 | #include <stdio.h> | |
9 | #include <stdbool.h> | |
10 | #include <stdlib.h> | |
774d1b14 | 11 | #include <assert.h> |
90c3a0d4 TM |
12 | |
13 | typedef struct pthread_mq_t { | |
774d1b14 TM |
14 | pthread_mutex_t lock; |
15 | pthread_cond_t cond_readable; | |
16 | pthread_cond_t cond_writable; | |
90c3a0d4 TM |
17 | void * data; |
18 | size_t msg_size; | |
19 | size_t msg_count; | |
20 | size_t msg_count_max; | |
21 | size_t head_idx; | |
22 | char * name; | |
23 | } pthread_mq_t; | |
24 | ||
774d1b14 TM |
25 | bool pthread_mq_readable(pthread_mq_t *mq) { return (mq->msg_count > 0); } |
26 | bool pthread_mq_writable(pthread_mq_t *mq) { return (mq->msg_count < mq->msg_count_max); } | |
90c3a0d4 TM |
27 | |
28 | bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max) { | |
774d1b14 TM |
29 | pthread_mutex_init(&mq->lock, NULL); |
30 | pthread_cond_init(&mq->cond_readable, NULL); | |
31 | pthread_cond_init(&mq->cond_writable, NULL); | |
90c3a0d4 TM |
32 | mq->data = malloc(msg_size*msg_count_max); |
33 | mq->msg_size = msg_size; | |
34 | mq->msg_count_max = msg_count_max; | |
35 | mq->msg_count = 0; | |
36 | mq->head_idx = 0; | |
37 | mq->name = NULL; | |
38 | ||
39 | if(((msg_size*msg_count_max) > 0) && mq->data == NULL) return false; | |
40 | return true; | |
41 | } | |
42 | ||
43 | void pthread_mq_free(pthread_mq_t *mq) { | |
44 | free(mq->data); | |
45 | } | |
46 | ||
90c3a0d4 TM |
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); | |
50 | } | |
51 | ||
774d1b14 TM |
52 | bool pthread_mq_reset(pthread_mq_t *mq) { |
53 | if(pthread_mutex_lock(&mq->lock)) return false; | |
54 | mq->msg_count = 0; | |
55 | mq->head_idx = 0; | |
56 | pthread_mq_cond(mq); | |
57 | pthread_mutex_unlock(&mq->lock); | |
58 | return true; | |
59 | } | |
60 | ||
90c3a0d4 | 61 | bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, bool block, const struct timespec *restrict abs_timeout) { |
3297654e TM |
62 | int ret; |
63 | ||
774d1b14 TM |
64 | //Lock queue |
65 | if(pthread_mutex_timedlock(&mq->lock, abs_timeout)) return false; | |
90c3a0d4 | 66 | |
774d1b14 | 67 | //Wait for queue to be in writable condition |
90c3a0d4 | 68 | while(!pthread_mq_writable(mq)) { |
3297654e TM |
69 | if(abs_timeout == NULL) { |
70 | ret = pthread_cond_wait(&mq->cond_writable, &mq->lock); | |
71 | } else { | |
72 | ret = pthread_cond_timedwait(&mq->cond_writable, &mq->lock, abs_timeout); | |
73 | } | |
74 | if(ret) { | |
774d1b14 TM |
75 | pthread_mutex_unlock(&mq->lock); |
76 | return false; | |
77 | } | |
90c3a0d4 | 78 | } |
3297654e | 79 | printf("Writable\n"); |
90c3a0d4 | 80 | |
774d1b14 TM |
81 | //Write data to queue |
82 | size_t idx = ( ( mq->msg_count_max + mq->head_idx + (to_front?-1:mq->msg_count) ) % mq->msg_count_max ); | |
90c3a0d4 TM |
83 | void *ptr = mq->data + (idx * mq->msg_size); |
84 | mq->msg_count++; | |
774d1b14 | 85 | if(to_front) mq->head_idx = (mq->msg_count_max + mq->head_idx - 1) % mq->msg_count_max; |
90c3a0d4 TM |
86 | memcpy(ptr, data, mq->msg_size); |
87 | ||
774d1b14 | 88 | //Signal conditions and unlock |
90c3a0d4 TM |
89 | pthread_mq_cond(mq); |
90 | pthread_mutex_unlock(&mq->lock); | |
91 | return true; | |
92 | } | |
93 | ||
90c3a0d4 | 94 | int main() { |
3297654e TM |
95 | pthread_mq_t myq; |
96 | pthread_mq_init(&myq, 6, 5); | |
97 | pthread_mq_send_generic(&myq, "AHOJ1", false, true, NULL); | |
98 | pthread_mq_send_generic(&myq, "AHOJ2", false, true, NULL); | |
99 | pthread_mq_send_generic(&myq, "AHOJ3", true, true, NULL); | |
100 | pthread_mq_send_generic(&myq, "AHOJ4", true, true, NULL); | |
101 | pthread_mq_send_generic(&myq, "AHOJ5", false, true, NULL); | |
102 | //pthread_mq_send_generic(&myq, "AHOJ6", false, true, NULL); | |
90c3a0d4 | 103 | |
3297654e TM |
104 | for(int i = 0; i<5; i++) { |
105 | printf("%.6s\n", (char *)(myq.data+i*6)); | |
106 | } | |
90c3a0d4 | 107 | } |