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); | |
7e0f3dba | 31 | void pthread_user_data_cleanup(void * arg); |
88342701 TM |
32 | |
33 | // Pausing | |
34 | ||
35 | //GDB: handle SIG34 nostop noprint | |
a8e71e8f TM |
36 | #define PTHREAD_XSIG_STOP (SIGRTMIN+0) |
37 | #define PTHREAD_XSIG_CONT (SIGRTMIN+1) | |
38 | #define PTHREAD_XSIGRTMIN (SIGRTMIN+2) //First unused RT signal | |
39 | ||
7e0f3dba TM |
40 | int pthread_extra_create(pthread_t *restrict thread, |
41 | const pthread_attr_t *restrict attr, | |
42 | void *(*start_routine)(void *), | |
43 | void *restrict arg); | |
a8e71e8f TM |
44 | void pthread_unpause_handler(); |
45 | void pthread_pause_handler(); | |
46 | void pthread_pause_enable(); | |
88342701 TM |
47 | int pthread_pause(pthread_t thread); |
48 | int pthread_unpause(pthread_t thread); | |
7e0f3dba | 49 | int pthread_pause_reschedule(pthread_t thread); |
8df6da88 | 50 | int pthread_extra_yield(); |
a8e71e8f | 51 | |
3aac2619 TM |
52 | // Message queues |
53 | ||
9e057e1d TM |
54 | //Flags |
55 | typedef uint8_t pthread_mq_flags_t; | |
56 | #define PTHREAD_XMQ_NONE 0 ///< No flags specified (default behaviour) | |
57 | #define PTHREAD_XMQ_FRONT 1 ///< Send to front of the queue (scheduled for next receive) | |
58 | #define PTHREAD_XMQ_BACK PTHREAD_XMQ_NONE ///< Send to back of the queue (default) | |
59 | #define PTHREAD_XMQ_PEEK 2 ///< Only peek, do not remove received item | |
60 | #define PTHREAD_XMQ_RECV PTHREAD_XMQ_NONE ///< Remove received item from queue (default) | |
61 | #define PTHREAD_XMQ_OVERW 4 ///< Overwrite item if queue full | |
3aac2619 TM |
62 | |
63 | typedef struct pthread_mq_t { | |
64 | pthread_mutex_t lock; | |
65 | pthread_cond_t cond_readable; | |
66 | pthread_cond_t cond_writable; | |
5251b63c | 67 | uint8_t * data; |
3aac2619 TM |
68 | size_t msg_size; |
69 | size_t msg_count; | |
70 | size_t msg_count_max; | |
71 | size_t head_idx; | |
72 | char * name; | |
73 | } pthread_mq_t; | |
74 | ||
75 | bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max); | |
76 | void pthread_mq_free(pthread_mq_t *mq); | |
77c7dc7f | 77 | |
3aac2619 | 78 | bool pthread_mq_reset(pthread_mq_t *mq); |
9e057e1d TM |
79 | bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout); |
80 | bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout); | |
3aac2619 | 81 | |
77c7dc7f TM |
82 | size_t pthread_mq_waiting(pthread_mq_t *mq); |
83 | size_t pthread_mq_vacant(pthread_mq_t *mq); | |
84 | ||
3aac2619 TM |
85 | // Multi mutex locking |
86 | ||
5251b63c | 87 | #define pthread_mutex_swap(a, b) { pthread_mutex_t *s; s = (a); a = (b); b = s; } |
3aac2619 TM |
88 | |
89 | #define pthread_mutex_lock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, NULL) | |
90 | #define pthread_mutex_timedlock_two(a,b,tm) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, (tm)) | |
91 | #define pthread_mutex_trylock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, false, NULL) | |
92 | ||
93 | #define pthread_mutex_lock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,NULL) | |
94 | #define pthread_mutex_timedlock_multi(lcks,cnt,tm) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,(tm)) | |
95 | #define pthread_mutex_trylock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),false,NULL) | |
96 | ||
97 | int pthread_mutex_timedlock_multi_generic(pthread_mutex_t **lck, int cnt, bool block, const struct timespec *restrict abs_timeout); | |
98 | ||
99 | ||
100 | #endif //__PTHREAD_EXTRA_H__ |