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

« back to all changes in this revision

Viewing changes to srclib/apr/threadproc/os2/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
#define INCL_DOSERRORS
 
18
#define INCL_DOS
 
19
#include "apr_arch_threadproc.h"
 
20
#include "apr_thread_proc.h"
 
21
#include "apr_general.h"
 
22
#include "apr_lib.h"
 
23
#include "apr_portable.h"
 
24
#include "apr_arch_file_io.h"
 
25
#include <stdlib.h>
 
26
 
 
27
APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new, apr_pool_t *pool)
 
28
{
 
29
    (*new) = (apr_threadattr_t *)apr_palloc(pool, sizeof(apr_threadattr_t));
 
30
 
 
31
    if ((*new) == NULL) {
 
32
        return APR_ENOMEM;
 
33
    }
 
34
 
 
35
    (*new)->pool = pool;
 
36
    (*new)->attr = 0;
 
37
    (*new)->stacksize = 0;
 
38
    return APR_SUCCESS;
 
39
}
 
40
 
 
41
 
 
42
 
 
43
APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr, apr_int32_t on)
 
44
{
 
45
    attr->attr |= APR_THREADATTR_DETACHED;
 
46
    return APR_SUCCESS;
 
47
}
 
48
 
 
49
 
 
50
 
 
51
APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
 
52
{
 
53
    return (attr->attr & APR_THREADATTR_DETACHED) ? APR_DETACH : APR_NOTDETACH;
 
54
}
 
55
 
 
56
APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
 
57
                                                       apr_size_t stacksize)
 
58
{
 
59
    attr->stacksize = stacksize;
 
60
    return APR_SUCCESS;
 
61
}
 
62
 
 
63
APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
 
64
                                                       apr_size_t size)
 
65
{
 
66
    return APR_ENOTIMPL;
 
67
}
 
68
 
 
69
static void apr_thread_begin(void *arg)
 
70
{
 
71
  apr_thread_t *thread = (apr_thread_t *)arg;
 
72
  thread->exitval = thread->func(thread, thread->data);
 
73
}
 
74
 
 
75
 
 
76
 
 
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, 
 
79
                                            apr_pool_t *pool)
 
80
{
 
81
    apr_status_t stat;
 
82
    apr_thread_t *thread;
 
83
 
 
84
    thread = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
 
85
    *new = thread;
 
86
 
 
87
    if (thread == NULL) {
 
88
        return APR_ENOMEM;
 
89
    }
 
90
 
 
91
    thread->pool = pool;
 
92
    thread->attr = attr;
 
93
    thread->func = func;
 
94
    thread->data = data;
 
95
    stat = apr_pool_create(&thread->pool, pool);
 
96
    
 
97
    if (stat != APR_SUCCESS) {
 
98
        return stat;
 
99
    }
 
100
 
 
101
    if (attr == NULL) {
 
102
        stat = apr_threadattr_create(&thread->attr, thread->pool);
 
103
        
 
104
        if (stat != APR_SUCCESS) {
 
105
            return stat;
 
106
        }
 
107
    }
 
108
 
 
109
    thread->tid = _beginthread(apr_thread_begin, NULL, 
 
110
                               thread->attr->stacksize > 0 ?
 
111
                               thread->attr->stacksize : APR_THREAD_STACKSIZE,
 
112
                               thread);
 
113
        
 
114
    if (thread->tid < 0) {
 
115
        return errno;
 
116
    }
 
117
 
 
118
    return APR_SUCCESS;
 
119
}
 
120
 
 
121
 
 
122
 
 
123
APR_DECLARE(apr_os_thread_t) apr_os_thread_current()
 
124
{
 
125
    PIB *ppib;
 
126
    TIB *ptib;
 
127
    DosGetInfoBlocks(&ptib, &ppib);
 
128
    return ptib->tib_ptib2->tib2_ultid;
 
129
}
 
130
 
 
131
 
 
132
 
 
133
APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
 
134
{
 
135
    thd->exitval = retval;
 
136
    _endthread();
 
137
    return -1; /* If we get here something's wrong */
 
138
}
 
139
 
 
140
 
 
141
 
 
142
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd)
 
143
{
 
144
    ULONG rc;
 
145
    TID waittid = thd->tid;
 
146
 
 
147
    if (thd->attr->attr & APR_THREADATTR_DETACHED)
 
148
        return APR_EINVAL;
 
149
 
 
150
    rc = DosWaitThread(&waittid, DCWW_WAIT);
 
151
 
 
152
    if (rc == ERROR_INVALID_THREADID)
 
153
        rc = 0; /* Thread had already terminated */
 
154
 
 
155
    *retval = thd->exitval;
 
156
    return APR_OS2_STATUS(rc);
 
157
}
 
158
 
 
159
 
 
160
 
 
161
APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
 
162
{
 
163
    thd->attr->attr |= APR_THREADATTR_DETACHED;
 
164
    return APR_SUCCESS;
 
165
}
 
166
 
 
167
 
 
168
 
 
169
void apr_thread_yield()
 
170
{
 
171
    DosSleep(0);
 
172
}
 
173
 
 
174
 
 
175
 
 
176
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd, apr_thread_t *thd)
 
177
{
 
178
    *thethd = &thd->tid;
 
179
    return APR_SUCCESS;
 
180
}
 
181
 
 
182
 
 
183
 
 
184
APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd, apr_os_thread_t *thethd, 
 
185
                                            apr_pool_t *pool)
 
186
{
 
187
    if ((*thd) == NULL) {
 
188
        (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
 
189
        (*thd)->pool = pool;
 
190
    }
 
191
    (*thd)->tid = *thethd;
 
192
    return APR_SUCCESS;
 
193
}
 
194
 
 
195
 
 
196
 
 
197
int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
 
198
{
 
199
    return tid1 == tid2;
 
200
}
 
201
 
 
202
 
 
203
 
 
204
APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key, apr_thread_t *thread)
 
205
{
 
206
    return apr_pool_userdata_get(data, key, thread->pool);
 
207
}
 
208
 
 
209
 
 
210
 
 
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)
 
214
{
 
215
    return apr_pool_userdata_set(data, key, cleanup, thread->pool);
 
216
}
 
217
 
 
218
APR_POOL_IMPLEMENT_ACCESSOR(thread)
 
219
 
 
220
 
 
221
 
 
222
static apr_status_t thread_once_cleanup(void *vcontrol)
 
223
{
 
224
    apr_thread_once_t *control = (apr_thread_once_t *)vcontrol;
 
225
 
 
226
    if (control->sem) {
 
227
        DosCloseEventSem(control->sem);
 
228
    }
 
229
 
 
230
    return APR_SUCCESS;
 
231
}
 
232
 
 
233
 
 
234
 
 
235
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
 
236
                                               apr_pool_t *p)
 
237
{
 
238
    ULONG rc;
 
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);
 
243
}
 
244
 
 
245
 
 
246
 
 
247
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control, 
 
248
                                          void (*func)(void))
 
249
{
 
250
    if (!control->hit) {
 
251
        ULONG count, rc;
 
252
        rc = DosResetEventSem(control->sem, &count);
 
253
 
 
254
        if (rc == 0 && count) {
 
255
            control->hit = 1;
 
256
            func();
 
257
        }
 
258
    }
 
259
 
 
260
    return APR_SUCCESS;
 
261
}