Commit | Line | Data |
---|---|---|
3aac2619 TM |
1 | #ifndef __PTHREAD_EXTRA_H__ |
2 | #define __PTHREAD_EXTRA_H__ | |
3 | ||
4 | #include <pthread.h> | |
3aac2619 TM |
5 | #include <stdbool.h> |
6 | #include <stdint.h> | |
a8e71e8f | 7 | #include <signal.h> |
88342701 | 8 | //#include <time.h> |
3aac2619 TM |
9 | |
10 | #define PTHREAD_XTIME_NOBLOCK (&(struct timespec){ .tv_sec = 0, .tv_nsec = 0 }) | |
11 | #define PTHREAD_XTIME_FOREVER NULL | |
12 | ||
88342701 | 13 | // User data |
a8e71e8f | 14 | |
88342701 TM |
15 | #define PTHREAD_XTHREADS_MAX 65535 |
16 | #define PTHREAD_XNULL ((pthread_t)NULL) | |
17 | ||
18 | #ifdef __PTHREAD_EXTRA_INTERNAL | |
19 | typedef struct pthread_user_data_internal_t { | |
20 | pthread_t tid; //Thread ID | |
21 | sig_atomic_t running; //Internaly used by pthread_pause | |
22 | void *usr; //User pointer | |
23 | } pthread_user_data_internal_t; | |
24 | ||
25 | pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread); | |
26 | #endif //__PTHREAD_EXTRA_INTERNAL | |
27 | ||
28 | void** pthread_user_data_ptr(pthread_t thread); | |
29 | void* pthread_user_data_get(pthread_t thread); | |
30 | void pthread_user_data_set(pthread_t thread, void *usr); | |
31 | ||
32 | // Pausing | |
33 | ||
34 | //GDB: handle SIG34 nostop noprint | |
a8e71e8f TM |
35 | #define PTHREAD_XSIG_STOP (SIGRTMIN+0) |
36 | #define PTHREAD_XSIG_CONT (SIGRTMIN+1) | |
37 | #define PTHREAD_XSIGRTMIN (SIGRTMIN+2) //First unused RT signal | |
38 | ||
a8e71e8f TM |
39 | void pthread_unpause_handler(); |
40 | void pthread_pause_handler(); | |
41 | void pthread_pause_enable(); | |
88342701 TM |
42 | int pthread_pause(pthread_t thread); |
43 | int pthread_unpause(pthread_t thread); | |
a8e71e8f | 44 | |
3aac2619 TM |
45 | // Message queues |
46 | ||
9e057e1d TM |
47 | //Flags |
48 | typedef uint8_t pthread_mq_flags_t; | |
49 | #define PTHREAD_XMQ_NONE 0 ///< No flags specified (default behaviour) | |
50 | #define PTHREAD_XMQ_FRONT 1 ///< Send to front of the queue (scheduled for next receive) | |
51 | #define PTHREAD_XMQ_BACK PTHREAD_XMQ_NONE ///< Send to back of the queue (default) | |
52 | #define PTHREAD_XMQ_PEEK 2 ///< Only peek, do not remove received item | |
53 | #define PTHREAD_XMQ_RECV PTHREAD_XMQ_NONE ///< Remove received item from queue (default) | |
54 | #define PTHREAD_XMQ_OVERW 4 ///< Overwrite item if queue full | |
3aac2619 TM |
55 | |
56 | typedef struct pthread_mq_t { | |
57 | pthread_mutex_t lock; | |
58 | pthread_cond_t cond_readable; | |
59 | pthread_cond_t cond_writable; | |
5251b63c | 60 | uint8_t * data; |
3aac2619 TM |
61 | size_t msg_size; |
62 | size_t msg_count; | |
63 | size_t msg_count_max; | |
64 | size_t head_idx; | |
65 | char * name; | |
66 | } pthread_mq_t; | |
67 | ||
68 | bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max); | |
69 | void pthread_mq_free(pthread_mq_t *mq); | |
77c7dc7f | 70 | |
3aac2619 | 71 | bool pthread_mq_reset(pthread_mq_t *mq); |
9e057e1d TM |
72 | bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout); |
73 | bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout); | |
3aac2619 | 74 | |
77c7dc7f TM |
75 | size_t pthread_mq_waiting(pthread_mq_t *mq); |
76 | size_t pthread_mq_vacant(pthread_mq_t *mq); | |
77 | ||
3aac2619 TM |
78 | // Multi mutex locking |
79 | ||
5251b63c | 80 | #define pthread_mutex_swap(a, b) { pthread_mutex_t *s; s = (a); a = (b); b = s; } |
3aac2619 TM |
81 | |
82 | #define pthread_mutex_lock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, NULL) | |
83 | #define pthread_mutex_timedlock_two(a,b,tm) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, (tm)) | |
84 | #define pthread_mutex_trylock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, false, NULL) | |
85 | ||
86 | #define pthread_mutex_lock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,NULL) | |
87 | #define pthread_mutex_timedlock_multi(lcks,cnt,tm) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,(tm)) | |
88 | #define pthread_mutex_trylock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),false,NULL) | |
89 | ||
90 | int pthread_mutex_timedlock_multi_generic(pthread_mutex_t **lck, int cnt, bool block, const struct timespec *restrict abs_timeout); | |
91 | ||
92 | ||
93 | #endif //__PTHREAD_EXTRA_H__ |