Thread registry is now protected by mutex
[mirrors/Programs.git] / c / pthread_extra / pthread_extra.h
1 #ifndef __PTHREAD_EXTRA_H__
2 #define __PTHREAD_EXTRA_H__
3
4 #include <pthread.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <signal.h>
8 //#include <time.h>
9
10 #define PTHREAD_XTIME_NOBLOCK (&(struct timespec){ .tv_sec = 0, .tv_nsec = 0 })
11 #define PTHREAD_XTIME_FOREVER NULL
12
13 // User data
14
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 void *usr; //User pointer
22 //Internaly used members:
23 sig_atomic_t running; //pthread_pause
24 } pthread_user_data_internal_t;
25
26 pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread);
27 #endif //__PTHREAD_EXTRA_INTERNAL
28
29 void** pthread_user_data_ptr(pthread_t thread);
30 void* pthread_user_data_get(pthread_t thread);
31 void pthread_user_data_set(pthread_t thread, void *usr);
32 void pthread_user_data_cleanup(void * arg);
33
34 // Pausing
35
36 //GDB: handle SIG34 nostop noprint
37 #define PTHREAD_XSIG_STOP (SIGRTMIN+0)
38 #define PTHREAD_XSIG_CONT (SIGRTMIN+1)
39 #define PTHREAD_XSIGRTMIN (SIGRTMIN+2) //First unused RT signal
40
41 int pthread_extra_create(pthread_t *restrict thread,
42 const pthread_attr_t *restrict attr,
43 void *(*start_routine)(void *),
44 void *restrict arg);
45 void pthread_unpause_handler();
46 void pthread_pause_handler();
47 void pthread_pause_enable();
48 void pthread_pause_disable();
49 int pthread_pause(pthread_t thread);
50 int pthread_unpause(pthread_t thread);
51 int pthread_pause_reschedule(pthread_t thread);
52 int pthread_extra_yield();
53
54 // Message queues
55
56 //Flags
57 typedef uint8_t pthread_mq_flags_t;
58 #define PTHREAD_XMQ_NONE 0 ///< No flags specified (default behaviour)
59 #define PTHREAD_XMQ_FRONT 1 ///< Send to front of the queue (scheduled for next receive)
60 #define PTHREAD_XMQ_BACK PTHREAD_XMQ_NONE ///< Send to back of the queue (default)
61 #define PTHREAD_XMQ_PEEK 2 ///< Only peek, do not remove received item
62 #define PTHREAD_XMQ_RECV PTHREAD_XMQ_NONE ///< Remove received item from queue (default)
63 #define PTHREAD_XMQ_OVERW 4 ///< Overwrite item if queue full
64
65 typedef struct pthread_mq_t {
66 pthread_mutex_t lock;
67 pthread_cond_t cond_readable;
68 pthread_cond_t cond_writable;
69 uint8_t * data;
70 size_t msg_size;
71 size_t msg_count;
72 size_t msg_count_max;
73 size_t head_idx;
74 char * name;
75 } pthread_mq_t;
76
77 bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max);
78 void pthread_mq_free(pthread_mq_t *mq);
79
80 bool pthread_mq_reset(pthread_mq_t *mq);
81 bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout);
82 bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout);
83
84 size_t pthread_mq_waiting(pthread_mq_t *mq);
85 size_t pthread_mq_vacant(pthread_mq_t *mq);
86
87 // Multi mutex locking
88
89 #define pthread_mutex_swap(a, b) { pthread_mutex_t *s; s = (a); a = (b); b = s; }
90
91 #define pthread_mutex_lock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, NULL)
92 #define pthread_mutex_timedlock_two(a,b,tm) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, true, (tm))
93 #define pthread_mutex_trylock_two(a,b) pthread_mutex_timedlock_multi_generic((pthread_mutex_t *[2]){(a), (b)}, 2, false, NULL)
94
95 #define pthread_mutex_lock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,NULL)
96 #define pthread_mutex_timedlock_multi(lcks,cnt,tm) pthread_mutex_timedlock_multi_generic((lcks),(cnt),true,(tm))
97 #define pthread_mutex_trylock_multi(lcks,cnt) pthread_mutex_timedlock_multi_generic((lcks),(cnt),false,NULL)
98
99 int pthread_mutex_timedlock_multi_generic(pthread_mutex_t **lck, int cnt, bool block, const struct timespec *restrict abs_timeout);
100
101
102 #endif //__PTHREAD_EXTRA_H__
This page took 0.322472 seconds and 4 git commands to generate.