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 #define INCL_DOSERRORS
19 #include "apr_arch_threadproc.h"
20 #include "apr_thread_proc.h"
21 #include "apr_general.h"
23 #include "apr_portable.h"
24 #include "apr_arch_file_io.h"
27 APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new, apr_pool_t *pool)
29 (*new) = (apr_threadattr_t *)apr_palloc(pool, sizeof(apr_threadattr_t));
37 (*new)->stacksize = 0;
43 APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr, apr_int32_t on)
45 attr->attr |= APR_THREADATTR_DETACHED;
51 APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
53 return (attr->attr & APR_THREADATTR_DETACHED) ? APR_DETACH : APR_NOTDETACH;
58 APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
61 attr->stacksize = stacksize;
67 static void apr_thread_begin(void *arg)
69 apr_thread_t *thread = (apr_thread_t *)arg;
70 thread->exitval = thread->func(thread, thread->data);
75 APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr,
76 apr_thread_start_t func, void *data,
82 thread = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
93 stat = apr_pool_create(&thread->pool, pool);
95 if (stat != APR_SUCCESS) {
100 stat = apr_threadattr_create(&thread->attr, thread->pool);
102 if (stat != APR_SUCCESS) {
107 thread->tid = _beginthread(apr_thread_begin, NULL,
108 thread->attr->stacksize > 0 ?
109 thread->attr->stacksize : APR_THREAD_STACKSIZE,
112 if (thread->tid < 0) {
121 APR_DECLARE(apr_os_thread_t) apr_os_thread_current()
125 DosGetInfoBlocks(&ptib, &ppib);
126 return ptib->tib_ptib2->tib2_ultid;
131 APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
133 thd->exitval = retval;
135 return -1; /* If we get here something's wrong */
140 APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd)
143 TID waittid = thd->tid;
145 if (thd->attr->attr & APR_THREADATTR_DETACHED)
148 rc = DosWaitThread(&waittid, DCWW_WAIT);
150 if (rc == ERROR_INVALID_THREADID)
151 rc = 0; /* Thread had already terminated */
153 *retval = thd->exitval;
154 return APR_OS2_STATUS(rc);
159 APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
161 thd->attr->attr |= APR_THREADATTR_DETACHED;
167 void apr_thread_yield()
174 APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd, apr_thread_t *thd)
182 APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd, apr_os_thread_t *thethd,
185 if ((*thd) == NULL) {
186 (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
189 (*thd)->tid = *thethd;
195 int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
202 APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key, apr_thread_t *thread)
204 return apr_pool_userdata_get(data, key, thread->pool);
209 APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
210 apr_status_t (*cleanup) (void *),
211 apr_thread_t *thread)
213 return apr_pool_userdata_set(data, key, cleanup, thread->pool);
216 APR_POOL_IMPLEMENT_ACCESSOR(thread)
220 static apr_status_t thread_once_cleanup(void *vcontrol)
222 apr_thread_once_t *control = (apr_thread_once_t *)vcontrol;
225 DosCloseEventSem(control->sem);
233 APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
237 *control = (apr_thread_once_t *)apr_pcalloc(p, sizeof(apr_thread_once_t));
238 rc = DosCreateEventSem(NULL, &(*control)->sem, 0, TRUE);
239 apr_pool_cleanup_register(p, control, thread_once_cleanup, apr_pool_cleanup_null);
240 return APR_FROM_OS_ERROR(rc);
245 APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
250 rc = DosResetEventSem(control->sem, &count);
252 if (rc == 0 && count) {