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
#include "apr_private.h"
18
#include "win32/apr_arch_threadproc.h"
19
#include "apr_thread_proc.h"
20
#include "apr_general.h"
22
#include "apr_portable.h"
23
#if APR_HAVE_PROCESS_H
26
#include "apr_arch_misc.h"
28
/* Chosen for us by apr_initialize */
29
DWORD tls_apr_thread = 0;
31
APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new,
34
(*new) = (apr_threadattr_t *)apr_palloc(pool,
35
sizeof(apr_threadattr_t));
43
(*new)->stacksize = 0;
48
APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
55
APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
57
if (attr->detach == 1)
62
APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
65
attr->stacksize = stacksize;
69
APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
75
static void *dummy_worker(void *opaque)
77
apr_thread_t *thd = (apr_thread_t *)opaque;
78
TlsSetValue(tls_apr_thread, thd->td);
79
return thd->func(thd, thd->data);
82
APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
83
apr_threadattr_t *attr,
84
apr_thread_start_t func,
85
void *data, apr_pool_t *pool)
91
(*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
101
stat = apr_pool_create(&(*new)->pool, pool);
102
if (stat != APR_SUCCESS) {
106
/* Use 0 for Thread Stack Size, because that will default the stack to the
107
* same size as the calling thread.
110
if ((handle = (HANDLE)_beginthreadex(NULL,
111
attr && attr->stacksize > 0 ? attr->stacksize : 0,
112
(unsigned int (APR_THREAD_FUNC *)(void *))dummy_worker,
113
(*new), 0, &temp)) == 0) {
114
return APR_FROM_OS_ERROR(_doserrno);
117
if ((handle = CreateThread(NULL,
118
attr && attr->stacksize > 0 ? attr->stacksize : 0,
119
(unsigned int (APR_THREAD_FUNC *)(void *))dummy_worker,
120
(*new), 0, &temp)) == 0) {
121
return apr_get_os_error();
124
if (attr && attr->detach) {
133
APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
136
thd->exitval = retval;
137
apr_pool_destroy(thd->pool);
147
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
150
apr_status_t rv = APR_SUCCESS;
153
/* Can not join on detached threads */
156
rv = WaitForSingleObject(thd->td, INFINITE);
157
if ( rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
158
/* If the thread_exit has been called */
160
*retval = thd->exitval;
165
rv = apr_get_os_error();
166
CloseHandle(thd->td);
172
APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
174
if (thd->td && CloseHandle(thd->td)) {
179
return apr_get_os_error();
183
APR_DECLARE(void) apr_thread_yield()
185
/* SwitchToThread is not supported on Win9x, but since it's
186
* primarily a noop (entering time consuming code, therefore
187
* providing more critical threads a bit larger timeslice)
188
* we won't worry too much if it's not available.
191
if (apr_os_level >= APR_WIN_NT) {
197
APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
198
apr_thread_t *thread)
200
return apr_pool_userdata_get(data, key, thread->pool);
203
APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
204
apr_status_t (*cleanup) (void *),
205
apr_thread_t *thread)
207
return apr_pool_userdata_set(data, key, cleanup, thread->pool);
211
APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
213
HANDLE hthread = (HANDLE)TlsGetValue(tls_apr_thread);
220
hproc = GetCurrentProcess();
221
hthread = GetCurrentThread();
222
if (!DuplicateHandle(hproc, hthread,
223
hproc, &hthread, 0, FALSE,
224
DUPLICATE_SAME_ACCESS)) {
227
TlsSetValue(tls_apr_thread, hthread);
231
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
235
return APR_ENOTHREAD;
241
APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
242
apr_os_thread_t *thethd,
248
if ((*thd) == NULL) {
249
(*thd) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
256
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
259
(*control) = apr_pcalloc(p, sizeof(**control));
263
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
266
if (!InterlockedExchange(&control->value, 1)) {
272
APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
273
apr_os_thread_t tid2)
275
/* Since the only tid's we support our are own, and
276
* apr_os_thread_current returns the identical handle
277
* to the one we created initially, the test is simple.
279
return (tid1 == tid2);
282
APR_POOL_IMPLEMENT_ACCESSOR(thread)