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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AXUTIL_THREAD_H
#define AXUTIL_THREAD_H
/**
* @file axutil_thread.h
* @brief axis2 thread api
*/
#include <axutil_allocator.h>
#include <axutil_utils_defines.h>
#include <axutil_error.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @defgroup axutil_thread thread
* @ingroup axis2_util
* @{
*/
/**
* Thread callbacks from axis2 functions must be declared with AXIS2_THREAD_FUNC
* so that they follow the platform's calling convention.
*/
/*#define AXIS2_THREAD_FUNC */
/** Thread structure. */
typedef struct axutil_thread_t axutil_thread_t;
/** Thread attributes structure. */
typedef struct axutil_threadattr_t axutil_threadattr_t;
/** Control variable for one-time atomic variables. */
typedef struct axutil_thread_once_t axutil_thread_once_t;
/**
* The prototype for any AXIS2 thread worker functions.
*/
typedef void *(
AXIS2_THREAD_FUNC * axutil_thread_start_t)(
axutil_thread_t *,
void *);
/** Thread private address space. */
typedef struct axutil_threadkey_t axutil_threadkey_t;
/* Thread Function definitions */
/**
* Create and initialize a new threadattr variable
* @param cont The pool to use
* @return Newly created thread attribute
*/
AXIS2_EXTERN axutil_threadattr_t *AXIS2_CALL
axutil_threadattr_create(
axutil_allocator_t * allocator);
/**
* Set if newly created threads should be created in detached state.
* @param attr The threadattr to affect
* @param on Non-zero if detached threads should be created.
* @return The status of the operation
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_threadattr_detach_set(
axutil_threadattr_t * attr,
axis2_bool_t detached);
/**
* Get the detach state for this threadattr.
* @param attr The threadattr to reference
* @return AXIS2_TRUE if threads are to be detached, or AXIS2_FALSE
* if threads are to be joinable.
*/
AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axutil_threadattr_is_detach(
axutil_threadattr_t * attr,
axutil_allocator_t * allocator);
/**
* Create a new thread of execution
* @param attr The threadattr to use to determine how to create the thread
* @param func The function to start the new thread in
* @param data Any data to be passed to the starting function
* @param cont The pool to use
* @return The newly created thread handle.
*/
AXIS2_EXTERN axutil_thread_t *AXIS2_CALL
axutil_thread_create(
axutil_allocator_t * allocator,
axutil_threadattr_t * attr,
axutil_thread_start_t func,
void *data);
/**
* Stop the current thread
* @param thd The thread to stop
* @return The status of the operation
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_exit(
axutil_thread_t * thd,
axutil_allocator_t * allocator);
/**
* Block until the desired thread stops executing.
* @param thd The thread to join
* @return The status of the operation
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_join(
axutil_thread_t * thd);
/**
* force the current thread to yield the processor
*/
AXIS2_EXTERN void AXIS2_CALL
axutil_thread_yield(void
);
/**
* function is used to allocate a new key. This key now becomes valid for all threads in our process.
* When a key is created, the value it points to defaults to NULL. Later on each thread may change
* its copy of the value as it wishes.
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_key_create(
axutil_threadkey_t * axis2_key);
/**
* This function is used to get the value of a given key
* @return void*. A key's value is simply a void pointer (void*)
*/
AXIS2_EXTERN void *AXIS2_CALL
axutil_thread_getspecific(
axutil_threadkey_t * axis2_key);
/**
* This function is used to get the value of a given key
* @param keys value. A key's value is simply a void pointer (void*), so we can
* store in it anything that we want
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_setspecific(
axutil_threadkey_t * axis2_key,
void *value);
/**
* This function is used free the tls key.
*/
AXIS2_EXTERN void AXIS2_CALL
axutil_thread_key_free(
axutil_threadkey_t * axis2_key);
/**
* Initialize the control variable for axutil_thread_once.
* @param control The control variable to initialize
* @return The status of the operation
*/
AXIS2_EXTERN axutil_thread_once_t *AXIS2_CALL
axutil_thread_once_init(
axutil_allocator_t * allocator);
/**
* Run the specified function one time, regardless of how many threads
* call it.
* @param control The control variable. The same variable should
* be passed in each time the function is tried to be
* called. This is how the underlying functions determine
* if the function has ever been called before.
* @param func The function to call.
* @return The status of the operation
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_once(
axutil_thread_once_t * control,
void(*func)(void));
/**
* detach a thread
* @param thd The thread to detach
* @return The status of the operation
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_detach(
axutil_thread_t * thd);
/*************************Thread locking functions*****************************/
/** Opaque thread-local mutex structure */
typedef struct axutil_thread_mutex_t axutil_thread_mutex_t;
#define AXIS2_THREAD_MUTEX_DEFAULT 0x0 /**< platform-optimal lock behavior */
#define AXIS2_THREAD_MUTEX_NESTED 0x1 /**< enable nested (recursive) locks */
#define AXIS2_THREAD_MUTEX_UNNESTED 0x2 /**< disable nested locks */
/**
* Create and initialize a mutex that can be used to synchronize threads.
* @param allocator Memory allocator to allocate memory for the mutex
* @warning Be cautious in using AXIS2_THREAD_MUTEX_DEFAULT. While this is the
* most optimal mutex based on a given platform's performance characteristics,
* it will behave as either a nested or an unnested lock.
*/
AXIS2_EXTERN axutil_thread_mutex_t *AXIS2_CALL
axutil_thread_mutex_create(
axutil_allocator_t * allocator,
unsigned int flags);
/**
* Acquire the lock for the given mutex. If the mutex is already locked,
* the current thread will be put to sleep until the lock becomes available.
* @param mutex the mutex on which to acquire the lock.
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_mutex_lock(
axutil_thread_mutex_t * mutex);
/**
* Attempt to acquire the lock for the given mutex. If the mutex has already
* been acquired, the call returns immediately
* @param mutex the mutex on which to attempt the lock acquiring.
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_mutex_trylock(
axutil_thread_mutex_t * mutex);
/**
* Release the lock for the given mutex.
* @param mutex the mutex from which to release the lock.
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_mutex_unlock(
axutil_thread_mutex_t * mutex);
/**
* Destroy the mutex and free the memory associated with the lock.
* @param mutex the mutex to destroy.
*/
AXIS2_EXTERN axis2_status_t AXIS2_CALL
axutil_thread_mutex_destroy(
axutil_thread_mutex_t * mutex);
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* AXIS2_THREAD_H */
|