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 | ||
47 | #define PTHREAD_XMQ_FRONT true | |
48 | #define PTHREAD_XMQ_BACK false | |
49 | ||
50 | #define PTHREAD_XMQ_RECV false | |
51 | #define PTHREAD_XMQ_PEEK true | |
52 | ||
53 | typedef struct pthread_mq_t { | |
54 | pthread_mutex_t lock; | |
55 | pthread_cond_t cond_readable; | |
56 | pthread_cond_t cond_writable; | |
5251b63c | 57 | uint8_t * data; |
3aac2619 TM |
58 | size_t msg_size; |
59 | size_t msg_count; | |
60 | size_t msg_count_max; | |
61 | size_t head_idx; | |
62 | char * name; | |
63 | } pthread_mq_t; | |
64 | ||
65 | bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max); | |
66 | void pthread_mq_free(pthread_mq_t *mq); | |
77c7dc7f | 67 | |
3aac2619 TM |
68 | bool pthread_mq_reset(pthread_mq_t *mq); |
69 | bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const struct timespec *restrict abs_timeout); | |
70 | bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const struct timespec *restrict abs_timeout); | |
71 | ||
77c7dc7f TM |
72 | size_t pthread_mq_waiting(pthread_mq_t *mq); |
73 | size_t pthread_mq_vacant(pthread_mq_t *mq); | |
74 | ||
3aac2619 TM |
75 | // Multi mutex locking |
76 | ||
5251b63c | 77 | #define pthread_mutex_swap(a, b) { pthread_mutex_t *s; s = (a); a = (b); b = s; } |
3aac2619 TM |
78 | |
79 | #define pthread_mutex_lock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, NULL) | |
80 | #define pthread_mutex_timedlock_two(a,b,tm) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, (tm)) | |
81 | #define pthread_mutex_trylock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, false, NULL) | |
82 | ||
83 | #define pthread_mutex_lock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,NULL) | |
84 | #define pthread_mutex_timedlock_multi(lcks,cnt,tm) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,(tm)) | |
85 | #define pthread_mutex_trylock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),false,NULL) | |
86 | ||
87 | int pthread_mutex_timedlock_multi_generic(pthread_mutex_t **lck, int cnt, bool block, const struct timespec *restrict abs_timeout); | |
88 | ||
89 | ||
90 | #endif //__PTHREAD_EXTRA_H__ |