Commit | Line | Data |
---|---|---|
3aac2619 TM |
1 | #ifndef __PTHREAD_EXTRA_H__ |
2 | #define __PTHREAD_EXTRA_H__ | |
3 | ||
4 | #include <pthread.h> | |
5 | #include <time.h> | |
6 | #include <stdbool.h> | |
7 | #include <stdint.h> | |
8 | ||
9 | #define PTHREAD_XTIME_NOBLOCK (&(struct timespec){ .tv_sec = 0, .tv_nsec = 0 }) | |
10 | #define PTHREAD_XTIME_FOREVER NULL | |
11 | ||
12 | // Message queues | |
13 | ||
14 | #define PTHREAD_XMQ_FRONT true | |
15 | #define PTHREAD_XMQ_BACK false | |
16 | ||
17 | #define PTHREAD_XMQ_RECV false | |
18 | #define PTHREAD_XMQ_PEEK true | |
19 | ||
20 | typedef struct pthread_mq_t { | |
21 | pthread_mutex_t lock; | |
22 | pthread_cond_t cond_readable; | |
23 | pthread_cond_t cond_writable; | |
5251b63c | 24 | uint8_t * data; |
3aac2619 TM |
25 | size_t msg_size; |
26 | size_t msg_count; | |
27 | size_t msg_count_max; | |
28 | size_t head_idx; | |
29 | char * name; | |
30 | } pthread_mq_t; | |
31 | ||
32 | bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max); | |
33 | void pthread_mq_free(pthread_mq_t *mq); | |
34 | size_t pthread_mq_waiting(pthread_mq_t *mq); | |
35 | bool pthread_mq_reset(pthread_mq_t *mq); | |
36 | bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const struct timespec *restrict abs_timeout); | |
37 | bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const struct timespec *restrict abs_timeout); | |
38 | ||
39 | // Multi mutex locking | |
40 | ||
5251b63c | 41 | #define pthread_mutex_swap(a, b) { pthread_mutex_t *s; s = (a); a = (b); b = s; } |
3aac2619 TM |
42 | |
43 | #define pthread_mutex_lock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, NULL) | |
44 | #define pthread_mutex_timedlock_two(a,b,tm) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, (tm)) | |
45 | #define pthread_mutex_trylock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, false, NULL) | |
46 | ||
47 | #define pthread_mutex_lock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,NULL) | |
48 | #define pthread_mutex_timedlock_multi(lcks,cnt,tm) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,(tm)) | |
49 | #define pthread_mutex_trylock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),false,NULL) | |
50 | ||
51 | int pthread_mutex_timedlock_multi_generic(pthread_mutex_t **lck, int cnt, bool block, const struct timespec *restrict abs_timeout); | |
52 | ||
53 | ||
54 | #endif //__PTHREAD_EXTRA_H__ |