1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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
24 APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new,
29 (*new) = (apr_threadattr_t *)apr_pcalloc(pool, sizeof(apr_threadattr_t));
30 (*new)->attr = (pthread_attr_t *)apr_pcalloc(pool, sizeof(pthread_attr_t));
32 if ((*new) == NULL || (*new)->attr == NULL) {
37 stat = pthread_attr_init((*new)->attr);
42 #ifdef PTHREAD_SETS_ERRNO
49 APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
53 #ifdef PTHREAD_ATTR_SETDETACHSTATE_ARG2_ADDR
56 if ((stat = pthread_attr_setdetachstate(attr->attr, &arg)) == 0) {
58 if ((stat = pthread_attr_setdetachstate(attr->attr, on)) == 0) {
64 #ifdef PTHREAD_SETS_ERRNO
72 APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
76 #ifdef PTHREAD_ATTR_GETDETACHSTATE_TAKES_ONE_ARG
77 state = pthread_attr_getdetachstate(attr->attr);
79 pthread_attr_getdetachstate(attr->attr, &state);
86 APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
91 stat = pthread_attr_setstacksize(attr->attr, stacksize);
95 #ifdef PTHREAD_SETS_ERRNO
102 static void *dummy_worker(void *opaque)
104 apr_thread_t *thread = (apr_thread_t*)opaque;
105 return thread->func(thread, thread->data);
108 APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
109 apr_threadattr_t *attr,
110 apr_thread_start_t func,
115 pthread_attr_t *temp;
117 (*new) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
119 if ((*new) == NULL) {
123 (*new)->td = (pthread_t *)apr_pcalloc(pool, sizeof(pthread_t));
125 if ((*new)->td == NULL) {
138 stat = apr_pool_create(&(*new)->pool, pool);
139 if (stat != APR_SUCCESS) {
143 if ((stat = pthread_create((*new)->td, temp, dummy_worker, (*new))) == 0) {
147 #ifdef PTHREAD_SETS_ERRNO
155 APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
157 return pthread_self();
160 APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
161 apr_os_thread_t tid2)
163 return pthread_equal(tid1, tid2);
166 APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
169 thd->exitval = retval;
170 apr_pool_destroy(thd->pool);
175 APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
179 apr_status_t *thread_stat;
181 if ((stat = pthread_join(*thd->td,(void *)&thread_stat)) == 0) {
182 *retval = thd->exitval;
186 #ifdef PTHREAD_SETS_ERRNO
194 APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
198 #ifdef PTHREAD_DETACH_ARG1_ADDR
199 if ((stat = pthread_detach(thd->td)) == 0) {
201 if ((stat = pthread_detach(*thd->td)) == 0) {
207 #ifdef PTHREAD_SETS_ERRNO
215 void apr_thread_yield()
219 APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
220 apr_thread_t *thread)
222 return apr_pool_userdata_get(data, key, thread->pool);
225 APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
226 apr_status_t (*cleanup)(void *),
227 apr_thread_t *thread)
229 return apr_pool_userdata_set(data, key, cleanup, thread->pool);
232 APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
239 APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
240 apr_os_thread_t *thethd,
247 if ((*thd) == NULL) {
248 (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
256 APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
259 static const pthread_once_t once_init = PTHREAD_ONCE_INIT;
261 *control = apr_palloc(p, sizeof(**control));
262 (*control)->once = once_init;
266 APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
269 return pthread_once(&control->once, func);
272 APR_POOL_IMPLEMENT_ACCESSOR(thread)
274 #endif /* HAVE_PTHREAD_H */
275 #endif /* APR_HAS_THREADS */
279 /* avoid warning for no prototype */
280 APR_DECLARE(apr_status_t) apr_os_thread_get(void);
282 APR_DECLARE(apr_status_t) apr_os_thread_get(void)