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.
17 #include "apr_arch_threadproc.h"
18 #include "apr_portable.h"
20 APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new, apr_pool_t *pool)
22 (*new) = (apr_threadattr_t *)apr_palloc(pool,
23 sizeof(apr_threadattr_t));
30 (*new)->attr = (int32)B_NORMAL_PRIORITY;
35 APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr, apr_int32_t on)
45 APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
47 if (attr->detached == 1){
53 APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
59 APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
65 static void *dummy_worker(void *opaque)
67 apr_thread_t *thd = (apr_thread_t*)opaque;
68 return thd->func(thd, thd->data);
71 APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr,
72 apr_thread_start_t func, void *data,
78 (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
87 /* First we create the new thread...*/
91 temp = B_NORMAL_PRIORITY;
93 stat = apr_pool_create(&(*new)->pool, pool);
94 if (stat != APR_SUCCESS) {
98 (*new)->td = spawn_thread((thread_func)dummy_worker,
103 /* Now we try to run it...*/
104 if (resume_thread((*new)->td) == B_NO_ERROR) {
112 APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
114 return find_thread(NULL);
117 int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
122 APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
124 apr_pool_destroy(thd->pool);
125 thd->exitval = retval;
126 exit_thread ((status_t)(retval));
127 /* This will never be reached... */
131 APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd)
133 status_t rv = 0, ret;
134 ret = wait_for_thread(thd->td, &rv);
135 if (ret == B_NO_ERROR) {
140 /* if we've missed the thread's death, did we set an exit value prior
141 * to it's demise? If we did return that.
143 if (thd->exitval != -1) {
144 *retval = thd->exitval;
151 APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
153 if (suspend_thread(thd->td) == B_NO_ERROR){
161 void apr_thread_yield()
165 APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key, apr_thread_t *thread)
167 return apr_pool_userdata_get(data, key, thread->pool);
170 APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
171 apr_status_t (*cleanup) (void *),
172 apr_thread_t *thread)
174 return apr_pool_userdata_set(data, key, cleanup, thread->pool);
177 APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd, apr_thread_t *thd)
183 APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd, apr_os_thread_t *thethd,
189 if ((*thd) == NULL) {
190 (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
193 (*thd)->td = *thethd;
197 static apr_status_t thread_once_cleanup(void *vcontrol)
199 apr_thread_once_t *control = (apr_thread_once_t *)vcontrol;
202 release_sem(control->sem);
203 delete_sem(control->sem);
209 APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
213 *control = (apr_thread_once_t *)apr_pcalloc(p, sizeof(apr_thread_once_t));
214 (*control)->hit = 0; /* we haven't done it yet... */
215 rc = ((*control)->sem = create_sem(1, "thread_once"));
219 apr_pool_cleanup_register(p, control, thread_once_cleanup, apr_pool_cleanup_null);
225 APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
229 if (acquire_sem(control->sem) == B_OK) {
237 APR_POOL_IMPLEMENT_ACCESSOR(thread)