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