1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
#include "apr_portable.h"
19
#include "apr_arch_threadproc.h"
23
#if APR_HAVE_PTHREAD_H
25
/* Destroy the threadattr object */
26
static apr_status_t threadattr_cleanup(void *data)
28
apr_threadattr_t *attr = data;
31
rv = pthread_attr_destroy(&attr->attr);
32
#ifdef PTHREAD_SETS_ERRNO
40
APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new,
45
(*new) = apr_palloc(pool, sizeof(apr_threadattr_t));
47
stat = pthread_attr_init(&(*new)->attr);
50
apr_pool_cleanup_register(pool, *new, threadattr_cleanup,
51
apr_pool_cleanup_null);
54
#ifdef PTHREAD_SETS_ERRNO
61
#define DETACH_ARG(v) ((v) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE)
63
APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
67
#ifdef PTHREAD_ATTR_SETDETACHSTATE_ARG2_ADDR
68
int arg = DETACH_ARG(v);
70
if ((stat = pthread_attr_setdetachstate(&attr->attr, &arg)) == 0) {
72
if ((stat = pthread_attr_setdetachstate(&attr->attr,
73
DETACH_ARG(on))) == 0) {
78
#ifdef PTHREAD_SETS_ERRNO
86
APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
90
#ifdef PTHREAD_ATTR_GETDETACHSTATE_TAKES_ONE_ARG
91
state = pthread_attr_getdetachstate(&attr->attr);
93
pthread_attr_getdetachstate(&attr->attr, &state);
100
APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
101
apr_size_t stacksize)
105
stat = pthread_attr_setstacksize(&attr->attr, stacksize);
109
#ifdef PTHREAD_SETS_ERRNO
116
APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
119
#ifdef HAVE_PTHREAD_ATTR_SETGUARDSIZE
122
rv = pthread_attr_setguardsize(&attr->attr, size);
126
#ifdef PTHREAD_SETS_ERRNO
135
static void *dummy_worker(void *opaque)
137
apr_thread_t *thread = (apr_thread_t*)opaque;
138
return thread->func(thread, thread->data);
141
APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
142
apr_threadattr_t *attr,
143
apr_thread_start_t func,
148
pthread_attr_t *temp;
150
(*new) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
152
if ((*new) == NULL) {
156
(*new)->td = (pthread_t *)apr_pcalloc(pool, sizeof(pthread_t));
158
if ((*new)->td == NULL) {
171
stat = apr_pool_create(&(*new)->pool, pool);
172
if (stat != APR_SUCCESS) {
176
if ((stat = pthread_create((*new)->td, temp, dummy_worker, (*new))) == 0) {
180
#ifdef PTHREAD_SETS_ERRNO
188
APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
190
return pthread_self();
193
APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
194
apr_os_thread_t tid2)
196
return pthread_equal(tid1, tid2);
199
APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
202
thd->exitval = retval;
203
apr_pool_destroy(thd->pool);
208
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
212
apr_status_t *thread_stat;
214
if ((stat = pthread_join(*thd->td,(void *)&thread_stat)) == 0) {
215
*retval = thd->exitval;
219
#ifdef PTHREAD_SETS_ERRNO
227
APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
231
#ifdef PTHREAD_DETACH_ARG1_ADDR
232
if ((stat = pthread_detach(thd->td)) == 0) {
234
if ((stat = pthread_detach(*thd->td)) == 0) {
240
#ifdef PTHREAD_SETS_ERRNO
248
void apr_thread_yield()
252
APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
253
apr_thread_t *thread)
255
return apr_pool_userdata_get(data, key, thread->pool);
258
APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
259
apr_status_t (*cleanup)(void *),
260
apr_thread_t *thread)
262
return apr_pool_userdata_set(data, key, cleanup, thread->pool);
265
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
272
APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
273
apr_os_thread_t *thethd,
280
if ((*thd) == NULL) {
281
(*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
289
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
292
static const pthread_once_t once_init = PTHREAD_ONCE_INIT;
294
*control = apr_palloc(p, sizeof(**control));
295
(*control)->once = once_init;
299
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
302
return pthread_once(&control->once, func);
305
APR_POOL_IMPLEMENT_ACCESSOR(thread)
307
#endif /* HAVE_PTHREAD_H */
308
#endif /* APR_HAS_THREADS */
312
/* avoid warning for no prototype */
313
APR_DECLARE(apr_status_t) apr_os_thread_get(void);
315
APR_DECLARE(apr_status_t) apr_os_thread_get(void)