~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/threadproc/win32/thread.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
#include "apr_private.h"
 
18
#include "win32/apr_arch_threadproc.h"
 
19
#include "apr_thread_proc.h"
 
20
#include "apr_general.h"
 
21
#include "apr_lib.h"
 
22
#include "apr_portable.h"
 
23
#if APR_HAVE_PROCESS_H
 
24
#include <process.h>
 
25
#endif
 
26
#include "apr_arch_misc.h"   
 
27
 
 
28
/* Chosen for us by apr_initialize */
 
29
DWORD tls_apr_thread = 0;
 
30
 
 
31
APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new,
 
32
                                                apr_pool_t *pool)
 
33
{
 
34
    (*new) = (apr_threadattr_t *)apr_palloc(pool, 
 
35
              sizeof(apr_threadattr_t));
 
36
 
 
37
    if ((*new) == NULL) {
 
38
        return APR_ENOMEM;
 
39
    }
 
40
 
 
41
    (*new)->pool = pool;
 
42
    (*new)->detach = 0;
 
43
    (*new)->stacksize = 0;
 
44
 
 
45
    return APR_SUCCESS;
 
46
}
 
47
 
 
48
APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
 
49
                                                   apr_int32_t on)
 
50
{
 
51
    attr->detach = on;
 
52
    return APR_SUCCESS;
 
53
}
 
54
 
 
55
APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
 
56
{
 
57
    if (attr->detach == 1)
 
58
        return APR_DETACH;
 
59
    return APR_NOTDETACH;
 
60
}
 
61
 
 
62
APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
 
63
                                                       apr_size_t stacksize)
 
64
{
 
65
    attr->stacksize = stacksize;
 
66
    return APR_SUCCESS;
 
67
}
 
68
 
 
69
APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
 
70
                                                       apr_size_t size)
 
71
{
 
72
    return APR_ENOTIMPL;
 
73
}
 
74
 
 
75
static void *dummy_worker(void *opaque)
 
76
{
 
77
    apr_thread_t *thd = (apr_thread_t *)opaque;
 
78
    TlsSetValue(tls_apr_thread, thd->td);
 
79
    return thd->func(thd, thd->data);
 
80
}
 
81
 
 
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)
 
86
{
 
87
    apr_status_t stat;
 
88
        unsigned temp;
 
89
    HANDLE handle;
 
90
 
 
91
    (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
 
92
 
 
93
    if ((*new) == NULL) {
 
94
        return APR_ENOMEM;
 
95
    }
 
96
 
 
97
    (*new)->pool = pool;
 
98
    (*new)->data = data;
 
99
    (*new)->func = func;
 
100
    (*new)->td   = NULL;
 
101
    stat = apr_pool_create(&(*new)->pool, pool);
 
102
    if (stat != APR_SUCCESS) {
 
103
        return stat;
 
104
    }
 
105
 
 
106
    /* Use 0 for Thread Stack Size, because that will default the stack to the
 
107
     * same size as the calling thread. 
 
108
     */
 
109
#ifndef _WIN32_WCE
 
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);
 
115
    }
 
116
#else
 
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();
 
122
    }
 
123
#endif
 
124
    if (attr && attr->detach) {
 
125
        CloseHandle(handle);
 
126
    }
 
127
    else
 
128
        (*new)->td = handle;
 
129
 
 
130
    return APR_SUCCESS;
 
131
}
 
132
 
 
133
APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
 
134
                                          apr_status_t retval)
 
135
{
 
136
    thd->exitval = retval;
 
137
    apr_pool_destroy(thd->pool);
 
138
    thd->pool = NULL;
 
139
#ifndef _WIN32_WCE
 
140
    _endthreadex(0);
 
141
#else
 
142
    ExitThread(0);
 
143
#endif
 
144
    return APR_SUCCESS;
 
145
}
 
146
 
 
147
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
 
148
                                          apr_thread_t *thd)
 
149
{
 
150
    apr_status_t rv = APR_SUCCESS;
 
151
    
 
152
    if (!thd->td) {
 
153
        /* Can not join on detached threads */
 
154
        return APR_DETACH;
 
155
    }
 
156
    rv = WaitForSingleObject(thd->td, INFINITE);
 
157
    if ( rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
 
158
        /* If the thread_exit has been called */
 
159
        if (!thd->pool)
 
160
            *retval = thd->exitval;
 
161
        else
 
162
            rv = APR_INCOMPLETE;
 
163
    }
 
164
    else
 
165
        rv = apr_get_os_error();
 
166
    CloseHandle(thd->td);
 
167
    thd->td = NULL;
 
168
 
 
169
    return rv;
 
170
}
 
171
 
 
172
APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
 
173
{
 
174
    if (thd->td && CloseHandle(thd->td)) {
 
175
        thd->td = NULL;
 
176
        return APR_SUCCESS;
 
177
    }
 
178
    else {
 
179
        return apr_get_os_error();
 
180
    }
 
181
}
 
182
 
 
183
APR_DECLARE(void) apr_thread_yield()
 
184
{
 
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.
 
189
     */
 
190
#ifndef _WIN32_WCE
 
191
    if (apr_os_level >= APR_WIN_NT) {
 
192
        SwitchToThread();
 
193
    }
 
194
#endif
 
195
}
 
196
 
 
197
APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
 
198
                                             apr_thread_t *thread)
 
199
{
 
200
    return apr_pool_userdata_get(data, key, thread->pool);
 
201
}
 
202
 
 
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)
 
206
{
 
207
    return apr_pool_userdata_set(data, key, cleanup, thread->pool);
 
208
}
 
209
 
 
210
 
 
211
APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
 
212
{
 
213
    HANDLE hthread = (HANDLE)TlsGetValue(tls_apr_thread);
 
214
    HANDLE hproc;
 
215
 
 
216
    if (hthread) {
 
217
        return hthread;
 
218
    }
 
219
    
 
220
    hproc = GetCurrentProcess();
 
221
    hthread = GetCurrentThread();
 
222
    if (!DuplicateHandle(hproc, hthread, 
 
223
                         hproc, &hthread, 0, FALSE, 
 
224
                         DUPLICATE_SAME_ACCESS)) {
 
225
        return NULL;
 
226
    }
 
227
    TlsSetValue(tls_apr_thread, hthread);
 
228
    return hthread;
 
229
}
 
230
 
 
231
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
 
232
                                            apr_thread_t *thd)
 
233
{
 
234
    if (thd == NULL) {
 
235
        return APR_ENOTHREAD;
 
236
    }
 
237
    *thethd = thd->td;
 
238
    return APR_SUCCESS;
 
239
}
 
240
 
 
241
APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
 
242
                                            apr_os_thread_t *thethd,
 
243
                                            apr_pool_t *pool)
 
244
{
 
245
    if (pool == NULL) {
 
246
        return APR_ENOPOOL;
 
247
    }
 
248
    if ((*thd) == NULL) {
 
249
        (*thd) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
 
250
        (*thd)->pool = pool;
 
251
    }
 
252
    (*thd)->td = thethd;
 
253
    return APR_SUCCESS;
 
254
}
 
255
 
 
256
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
 
257
                                               apr_pool_t *p)
 
258
{
 
259
    (*control) = apr_pcalloc(p, sizeof(**control));
 
260
    return APR_SUCCESS;
 
261
}
 
262
 
 
263
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
 
264
                                          void (*func)(void))
 
265
{
 
266
    if (!InterlockedExchange(&control->value, 1)) {
 
267
        func();
 
268
    }
 
269
    return APR_SUCCESS;
 
270
}
 
271
 
 
272
APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
 
273
                                     apr_os_thread_t tid2)
 
274
{
 
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.
 
278
     */
 
279
    return (tid1 == tid2);
 
280
}
 
281
 
 
282
APR_POOL_IMPLEMENT_ACCESSOR(thread)