1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* Copyright (C) 2009 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* @defgroup thread thread
* @{ @ingroup threading
*/
#ifndef THREADING_THREAD_H_
#define THREADING_THREAD_H_
#include <utils/utils.h>
typedef struct thread_t thread_t;
/**
* Main function of a thread.
*
* @param arg argument provided to constructor
* @return value provided to threads joining the terminating thread
*/
typedef void *(*thread_main_t)(void *arg);
/**
* Cleanup callback function for a thread.
*
* @param arg argument provided to thread_cleanup_push
*/
typedef void (*thread_cleanup_t)(void *arg);
/**
* Thread wrapper implements simple, portable and advanced thread functions.
*
* @note All threads other than the main thread need either to be joined or
* detached by calling the corresponding method.
*/
struct thread_t {
/**
* Cancel this thread.
*/
void (*cancel)(thread_t *this);
/**
* Send a signal to this thread.
*
* @param sig the signal to be sent to this thread
*/
void (*kill)(thread_t *this, int sig);
/**
* Detach this thread, this automatically destroys the thread object after
* the thread returned from its main function.
*
* @note Calling detach is like calling destroy on other objects.
*/
void (*detach)(thread_t *this);
/**
* Join this thread, this automatically destroys the thread object
* afterwards.
*
* @note Calling join is like calling destroy on other objects.
*
* @return the value returned from the thread's main function or
* a call to exit.
*/
void *(*join)(thread_t *this);
};
/**
* Create a new thread instance.
*
* @param main thread main function
* @param arg argument provided to the main function
* @return thread instance
*/
thread_t *thread_create(thread_main_t main, void *arg);
/**
* Get a thread object for the current thread.
*
* @return thread instance
*/
thread_t *thread_current();
/**
* Get the ID of the current thread.
*
* Depending on the build configuration thread IDs are either assigned
* incrementally starting from 1, or equal the value returned by an appropriate
* syscall (like gettid() or GetCurrentThreadId()), if available.
*
* @return ID of the current thread
*/
u_int thread_current_id();
/**
* Push a function onto the current thread's cleanup handler stack.
* The callback function is called whenever the thread is cancelled, exits or
* thread_cleanup_pop is called with TRUE as execute argument.
*
* @param cleanup function called on thread exit
* @param arg argument provided to the callback
*/
void thread_cleanup_push(thread_cleanup_t cleanup, void *arg);
/**
* Remove the top function from the current thread's cleanup handler stack
* and optionally execute it.
*
* @param execute TRUE to execute the function
*/
void thread_cleanup_pop(bool execute);
/**
* Pop and execute all cleanup handlers in reverse order of registration.
*
* This function is for very special purposes only, where the caller exactly
* knows which cleanup handlers have been pushed. For regular use, a caller
* should thread_cleanup_pop() exactly the number of handlers it pushed
* using thread_cleanup_push().
*/
void thread_cleanup_popall();
/**
* Enable or disable the cancelability of the current thread. The current
* value is returned.
*
* @param enable TRUE to enable cancelability
* @return the current state of the cancelability
*/
bool thread_cancelability(bool enable);
/**
* Force creation of a cancellation point in the calling thread.
*
* This temporarily enables thread cancelability, tests for a pending
* cancellation request and then disables cancelability again if it was
* disabled before the call to thread_cancellation_point().
*/
void thread_cancellation_point();
/**
* Exit the current thread.
*
* @param val value provided to threads joining the current thread
*/
void thread_exit(void *val);
/**
* Called by the main thread to initialize the thread management.
*/
void threads_init();
/**
* Called by the main thread to deinitialize the thread management.
*/
void threads_deinit();
#endif /** THREADING_THREAD_H_ @} */
|