libcortex
Event loop library following a "batteries included" approach
Loading...
Searching...
No Matches
src
threads.h
1
#ifndef CRTX_THREADS_H
2
#define CRTX_THREADS_H
3
4
#ifdef __cplusplus
5
extern
"C"
{
6
#endif
7
8
/*
9
* Mario Kicherer (dev@kicherer.org) 2016
10
*
11
*/
12
13
#include <pthread.h>
14
15
#define MUTEX_TYPE pthread_mutex_t
16
#define INIT_MUTEX(mutex) pthread_mutex_init( &(mutex), 0)
17
#define INIT_REC_MUTEX(mutex) \
18
{ \
19
pthread_mutexattr_t attr; \
20
pthread_mutexattr_init(&attr); \
21
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
22
pthread_mutex_init( &(mutex), &attr); \
23
pthread_mutexattr_destroy(&attr); \
24
}
25
#define LOCK(mutex) pthread_mutex_lock( &(mutex) )
26
#define UNLOCK(mutex) pthread_mutex_unlock( &(mutex) )
27
#define SIGNAL_BCAST(cond) pthread_cond_broadcast( &(cond) )
28
#define SIGNAL_SINGLE(cond) pthread_cond_signal( &(cond) )
29
30
#define CAS(ptr, old_val, new_val) __sync_bool_compare_and_swap( (ptr), (old_val), (new_val) )
31
#define ATOMIC_FETCH_ADD(ptr, val) __sync_fetch_and_add ( &(ptr), (val) );
32
#define ATOMIC_FETCH_SUB(ptr, val) __sync_fetch_and_sub ( &(ptr), (val) );
33
34
typedef
void
*(*thread_fct)(
void
*data);
35
36
struct
crtx_signals
{
37
unsigned
char
*condition;
38
unsigned
char
local_condition;
39
signed
char
bitflag_idx;
40
41
unsigned
char
n_refs;
42
pthread_mutex_t ref_mutex;
43
pthread_cond_t ref_cond;
44
45
pthread_mutex_t mutex;
46
pthread_cond_t cond;
47
int
clockid;
48
};
49
50
struct
crtx_thread
;
51
struct
crtx_thread_job_description
{
52
thread_fct fct;
53
void
*fct_data;
54
55
void (*do_stop)(
struct
crtx_thread
*thread,
void
*data);
56
57
void (*on_finish)(
struct
crtx_thread
*thread,
void
*on_finish_data);
58
void
*on_finish_data;
59
60
// struct crtx_thread *thread;
61
};
62
63
struct
crtx_thread
{
64
pthread_t handle;
65
66
char
stop;
67
68
char
in_use;
69
70
struct
crtx_signals
start;
71
struct
crtx_signals
finished;
72
73
struct
crtx_thread_job_description
*job;
74
75
// MUTEX_TYPE mutex;
76
77
// struct crtx_signals *main_thread_finished;
78
79
struct
crtx_thread
*next;
80
};
81
82
83
struct
crtx_signals
*crtx_alloc_signal();
84
void
crtx_init_signal(
struct
crtx_signals
*signal);
85
int
crtx_wait_on_signal(
struct
crtx_signals
*s,
struct
timespec *ts);
86
void
crtx_send_signal(
struct
crtx_signals
*s,
char
brdcst);
87
void
crtx_shutdown_signal(
struct
crtx_signals
*s);
88
void
crtx_reset_signal(
struct
crtx_signals
*signal);
89
void
crtx_reference_signal(
struct
crtx_signals
*s);
90
void
crtx_dereference_signal(
struct
crtx_signals
*s);
91
char
crtx_signal_is_active(
struct
crtx_signals
*s);
92
93
struct
crtx_thread
*crtx_thread_assign_job(
struct
crtx_thread_job_description
*job);
94
void
crtx_thread_start_job(
struct
crtx_thread
*t);
95
struct
crtx_thread
*spawn_thread(
char
create);
96
struct
crtx_thread
* crtx_get_own_thread();
97
98
void
crtx_threads_cancel(
struct
crtx_thread
*t);
99
void
crtx_threads_stop(
struct
crtx_thread
*t);
100
void
crtx_threads_stop_all();
101
void
crtx_threads_interrupt_thread(
struct
crtx_thread
*t);
102
103
void
crtx_threads_init();
104
void
crtx_threads_finish();
105
106
#ifdef __cplusplus
107
}
108
#endif
109
110
#endif
crtx_signals
Definition
threads.h:36
crtx_thread_job_description
Definition
threads.h:51
crtx_thread
Definition
threads.h:63
Generated by
1.9.8