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
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 HAVE_ZOS_PTHREADS
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 HAVE_ZOS_PTHREADS
61 #if defined(PTHREAD_CREATE_DETACHED)
62 #define DETACH_ARG(v) ((v) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE)
64 #define DETACH_ARG(v) ((v) ? 1 : 0)
67 APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
71 #ifdef HAVE_ZOS_PTHREADS
72 int arg = DETACH_ARG(on);
74 if ((stat = pthread_attr_setdetachstate(&attr->attr, &arg)) == 0) {
76 if ((stat = pthread_attr_setdetachstate(&attr->attr,
77 DETACH_ARG(on))) == 0) {
82 #ifdef HAVE_ZOS_PTHREADS
90 APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
94 #ifdef PTHREAD_ATTR_GETDETACHSTATE_TAKES_ONE_ARG
95 state = pthread_attr_getdetachstate(&attr->attr);
97 pthread_attr_getdetachstate(&attr->attr, &state);
99 if (state == DETACH_ARG(1))
101 return APR_NOTDETACH;
104 APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
105 apr_size_t stacksize)
109 stat = pthread_attr_setstacksize(&attr->attr, stacksize);
113 #ifdef HAVE_ZOS_PTHREADS
120 APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
123 #ifdef HAVE_PTHREAD_ATTR_SETGUARDSIZE
126 rv = pthread_attr_setguardsize(&attr->attr, size);
130 #ifdef HAVE_ZOS_PTHREADS
139 static void *dummy_worker(void *opaque)
141 apr_thread_t *thread = (apr_thread_t*)opaque;
142 return thread->func(thread, thread->data);
145 APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
146 apr_threadattr_t *attr,
147 apr_thread_start_t func,
152 pthread_attr_t *temp;
154 (*new) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
156 if ((*new) == NULL) {
160 (*new)->td = (pthread_t *)apr_pcalloc(pool, sizeof(pthread_t));
162 if ((*new)->td == NULL) {
174 stat = apr_pool_create(&(*new)->pool, pool);
175 if (stat != APR_SUCCESS) {
179 if ((stat = pthread_create((*new)->td, temp, dummy_worker, (*new))) == 0) {
183 #ifdef HAVE_ZOS_PTHREADS
191 APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
193 return pthread_self();
196 APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
197 apr_os_thread_t tid2)
199 return pthread_equal(tid1, tid2);
202 APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
205 thd->exitval = retval;
206 apr_pool_destroy(thd->pool);
211 APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
215 apr_status_t *thread_stat;
217 if ((stat = pthread_join(*thd->td,(void *)&thread_stat)) == 0) {
218 *retval = thd->exitval;
222 #ifdef HAVE_ZOS_PTHREADS
230 APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
234 #ifdef HAVE_ZOS_PTHREADS
235 if ((stat = pthread_detach(thd->td)) == 0) {
237 if ((stat = pthread_detach(*thd->td)) == 0) {
243 #ifdef HAVE_ZOS_PTHREADS
251 APR_DECLARE(void) apr_thread_yield(void)
253 #ifdef HAVE_PTHREAD_YIELD
254 #ifdef HAVE_ZOS_PTHREADS
258 #endif /* HAVE_ZOS_PTHREADS */
260 #ifdef HAVE_SCHED_YIELD
266 APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
267 apr_thread_t *thread)
269 return apr_pool_userdata_get(data, key, thread->pool);
272 APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
273 apr_status_t (*cleanup)(void *),
274 apr_thread_t *thread)
276 return apr_pool_userdata_set(data, key, cleanup, thread->pool);
279 APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
286 APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
287 apr_os_thread_t *thethd,
294 if ((*thd) == NULL) {
295 (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
303 APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
306 static const pthread_once_t once_init = PTHREAD_ONCE_INIT;
308 *control = apr_palloc(p, sizeof(**control));
309 (*control)->once = once_init;
313 APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
316 return pthread_once(&control->once, func);
319 APR_POOL_IMPLEMENT_ACCESSOR(thread)
321 #endif /* HAVE_PTHREAD_H */
322 #endif /* APR_HAS_THREADS */
326 /* avoid warning for no prototype */
327 APR_DECLARE(apr_status_t) apr_os_thread_get(void);
329 APR_DECLARE(apr_status_t) apr_os_thread_get(void)