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.
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;
56
APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
59
attr->stacksize = stacksize;
63
APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
69
static void apr_thread_begin(void *arg)
71
apr_thread_t *thread = (apr_thread_t *)arg;
72
thread->exitval = thread->func(thread, thread->data);
77
APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr,
78
apr_thread_start_t func, void *data,
84
thread = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
95
stat = apr_pool_create(&thread->pool, pool);
97
if (stat != APR_SUCCESS) {
102
stat = apr_threadattr_create(&thread->attr, thread->pool);
104
if (stat != APR_SUCCESS) {
109
thread->tid = _beginthread(apr_thread_begin, NULL,
110
thread->attr->stacksize > 0 ?
111
thread->attr->stacksize : APR_THREAD_STACKSIZE,
114
if (thread->tid < 0) {
123
APR_DECLARE(apr_os_thread_t) apr_os_thread_current()
127
DosGetInfoBlocks(&ptib, &ppib);
128
return ptib->tib_ptib2->tib2_ultid;
133
APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
135
thd->exitval = retval;
137
return -1; /* If we get here something's wrong */
142
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd)
145
TID waittid = thd->tid;
147
if (thd->attr->attr & APR_THREADATTR_DETACHED)
150
rc = DosWaitThread(&waittid, DCWW_WAIT);
152
if (rc == ERROR_INVALID_THREADID)
153
rc = 0; /* Thread had already terminated */
155
*retval = thd->exitval;
156
return APR_OS2_STATUS(rc);
161
APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
163
thd->attr->attr |= APR_THREADATTR_DETACHED;
169
void apr_thread_yield()
176
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd, apr_thread_t *thd)
184
APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd, apr_os_thread_t *thethd,
187
if ((*thd) == NULL) {
188
(*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
191
(*thd)->tid = *thethd;
197
int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
204
APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key, apr_thread_t *thread)
206
return apr_pool_userdata_get(data, key, thread->pool);
211
APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
212
apr_status_t (*cleanup) (void *),
213
apr_thread_t *thread)
215
return apr_pool_userdata_set(data, key, cleanup, thread->pool);
218
APR_POOL_IMPLEMENT_ACCESSOR(thread)
222
static apr_status_t thread_once_cleanup(void *vcontrol)
224
apr_thread_once_t *control = (apr_thread_once_t *)vcontrol;
227
DosCloseEventSem(control->sem);
235
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
239
*control = (apr_thread_once_t *)apr_pcalloc(p, sizeof(apr_thread_once_t));
240
rc = DosCreateEventSem(NULL, &(*control)->sem, 0, TRUE);
241
apr_pool_cleanup_register(p, control, thread_once_cleanup, apr_pool_cleanup_null);
242
return APR_FROM_OS_ERROR(rc);
247
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
252
rc = DosResetEventSem(control->sem, &count);
254
if (rc == 0 && count) {