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

« back to all changes in this revision

Viewing changes to srclib/apr/threadproc/unix/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.h"
 
18
#include "apr_portable.h"
 
19
#include "apr_arch_threadproc.h"
 
20
 
 
21
#if APR_HAS_THREADS
 
22
 
 
23
#if APR_HAVE_PTHREAD_H
 
24
 
 
25
/* Destroy the threadattr object */
 
26
static apr_status_t threadattr_cleanup(void *data)
 
27
{
 
28
    apr_threadattr_t *attr = data;
 
29
    apr_status_t rv;
 
30
 
 
31
    rv = pthread_attr_destroy(&attr->attr);
 
32
#ifdef PTHREAD_SETS_ERRNO
 
33
    if (rv) {
 
34
        rv = errno;
 
35
    }
 
36
#endif
 
37
    return rv;
 
38
}
 
39
 
 
40
APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new,
 
41
                                                apr_pool_t *pool)
 
42
{
 
43
    apr_status_t stat;
 
44
 
 
45
    (*new) = apr_palloc(pool, sizeof(apr_threadattr_t));
 
46
    (*new)->pool = pool;
 
47
    stat = pthread_attr_init(&(*new)->attr);
 
48
 
 
49
    if (stat == 0) {
 
50
        apr_pool_cleanup_register(pool, *new, threadattr_cleanup,
 
51
                                  apr_pool_cleanup_null);
 
52
        return APR_SUCCESS;
 
53
    }
 
54
#ifdef PTHREAD_SETS_ERRNO
 
55
    stat = errno;
 
56
#endif
 
57
 
 
58
    return stat;
 
59
}
 
60
 
 
61
#define DETACH_ARG(v) ((v) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE)
 
62
 
 
63
APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
 
64
                                                    apr_int32_t on)
 
65
{
 
66
    apr_status_t stat;
 
67
#ifdef PTHREAD_ATTR_SETDETACHSTATE_ARG2_ADDR
 
68
    int arg = DETACH_ARG(v);
 
69
 
 
70
    if ((stat = pthread_attr_setdetachstate(&attr->attr, &arg)) == 0) {
 
71
#else
 
72
    if ((stat = pthread_attr_setdetachstate(&attr->attr, 
 
73
                                            DETACH_ARG(on))) == 0) {
 
74
#endif
 
75
        return APR_SUCCESS;
 
76
    }
 
77
    else {
 
78
#ifdef PTHREAD_SETS_ERRNO
 
79
        stat = errno;
 
80
#endif
 
81
 
 
82
        return stat;
 
83
    }
 
84
}
 
85
 
 
86
APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
 
87
{
 
88
    int state;
 
89
 
 
90
#ifdef PTHREAD_ATTR_GETDETACHSTATE_TAKES_ONE_ARG
 
91
    state = pthread_attr_getdetachstate(&attr->attr);
 
92
#else
 
93
    pthread_attr_getdetachstate(&attr->attr, &state);
 
94
#endif
 
95
    if (state == 1)
 
96
        return APR_DETACH;
 
97
    return APR_NOTDETACH;
 
98
}
 
99
 
 
100
APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
 
101
                                                       apr_size_t stacksize)
 
102
{
 
103
    int stat;
 
104
 
 
105
    stat = pthread_attr_setstacksize(&attr->attr, stacksize);
 
106
    if (stat == 0) {
 
107
        return APR_SUCCESS;
 
108
    }
 
109
#ifdef PTHREAD_SETS_ERRNO
 
110
    stat = errno;
 
111
#endif
 
112
 
 
113
    return stat;
 
114
}
 
115
 
 
116
APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
 
117
                                                       apr_size_t size)
 
118
{
 
119
#ifdef HAVE_PTHREAD_ATTR_SETGUARDSIZE
 
120
    apr_status_t rv;
 
121
 
 
122
    rv = pthread_attr_setguardsize(&attr->attr, size);
 
123
    if (rv == 0) {
 
124
        return APR_SUCCESS;
 
125
    }
 
126
#ifdef PTHREAD_SETS_ERRNO
 
127
    rv = errno;
 
128
#endif
 
129
    return rv;
 
130
#else
 
131
    return APR_ENOTIMPL;
 
132
#endif
 
133
}
 
134
 
 
135
static void *dummy_worker(void *opaque)
 
136
{
 
137
    apr_thread_t *thread = (apr_thread_t*)opaque;
 
138
    return thread->func(thread, thread->data);
 
139
}
 
140
 
 
141
APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
 
142
                                            apr_threadattr_t *attr,
 
143
                                            apr_thread_start_t func,
 
144
                                            void *data,
 
145
                                            apr_pool_t *pool)
 
146
{
 
147
    apr_status_t stat;
 
148
    pthread_attr_t *temp;
 
149
 
 
150
    (*new) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
 
151
 
 
152
    if ((*new) == NULL) {
 
153
        return APR_ENOMEM;
 
154
    }
 
155
 
 
156
    (*new)->td = (pthread_t *)apr_pcalloc(pool, sizeof(pthread_t));
 
157
 
 
158
    if ((*new)->td == NULL) {
 
159
        return APR_ENOMEM;
 
160
    }
 
161
 
 
162
    (*new)->pool = pool;
 
163
    (*new)->data = data;
 
164
    (*new)->func = func;
 
165
 
 
166
    if (attr)
 
167
        temp = &attr->attr;
 
168
    else
 
169
        temp = NULL;
 
170
 
 
171
    stat = apr_pool_create(&(*new)->pool, pool);
 
172
    if (stat != APR_SUCCESS) {
 
173
        return stat;
 
174
    }
 
175
 
 
176
    if ((stat = pthread_create((*new)->td, temp, dummy_worker, (*new))) == 0) {
 
177
        return APR_SUCCESS;
 
178
    }
 
179
    else {
 
180
#ifdef PTHREAD_SETS_ERRNO
 
181
        stat = errno;
 
182
#endif
 
183
 
 
184
        return stat;
 
185
    }
 
186
}
 
187
 
 
188
APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
 
189
{
 
190
    return pthread_self();
 
191
}
 
192
 
 
193
APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
 
194
                                     apr_os_thread_t tid2)
 
195
{
 
196
    return pthread_equal(tid1, tid2);
 
197
}
 
198
 
 
199
APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
 
200
                                          apr_status_t retval)
 
201
{
 
202
    thd->exitval = retval;
 
203
    apr_pool_destroy(thd->pool);
 
204
    pthread_exit(NULL);
 
205
    return APR_SUCCESS;
 
206
}
 
207
 
 
208
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
 
209
                                          apr_thread_t *thd)
 
210
{
 
211
    apr_status_t stat;
 
212
    apr_status_t *thread_stat;
 
213
 
 
214
    if ((stat = pthread_join(*thd->td,(void *)&thread_stat)) == 0) {
 
215
        *retval = thd->exitval;
 
216
        return APR_SUCCESS;
 
217
    }
 
218
    else {
 
219
#ifdef PTHREAD_SETS_ERRNO
 
220
        stat = errno;
 
221
#endif
 
222
 
 
223
        return stat;
 
224
    }
 
225
}
 
226
 
 
227
APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
 
228
{
 
229
    apr_status_t stat;
 
230
 
 
231
#ifdef PTHREAD_DETACH_ARG1_ADDR
 
232
    if ((stat = pthread_detach(thd->td)) == 0) {
 
233
#else
 
234
    if ((stat = pthread_detach(*thd->td)) == 0) {
 
235
#endif
 
236
 
 
237
        return APR_SUCCESS;
 
238
    }
 
239
    else {
 
240
#ifdef PTHREAD_SETS_ERRNO
 
241
        stat = errno;
 
242
#endif
 
243
 
 
244
        return stat;
 
245
    }
 
246
}
 
247
 
 
248
void apr_thread_yield()
 
249
{
 
250
}
 
251
 
 
252
APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
 
253
                                              apr_thread_t *thread)
 
254
{
 
255
    return apr_pool_userdata_get(data, key, thread->pool);
 
256
}
 
257
 
 
258
APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
 
259
                              apr_status_t (*cleanup)(void *),
 
260
                              apr_thread_t *thread)
 
261
{
 
262
    return apr_pool_userdata_set(data, key, cleanup, thread->pool);
 
263
}
 
264
 
 
265
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
 
266
                                            apr_thread_t *thd)
 
267
{
 
268
    *thethd = thd->td;
 
269
    return APR_SUCCESS;
 
270
}
 
271
 
 
272
APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
 
273
                                            apr_os_thread_t *thethd,
 
274
                                            apr_pool_t *pool)
 
275
{
 
276
    if (pool == NULL) {
 
277
        return APR_ENOPOOL;
 
278
    }
 
279
 
 
280
    if ((*thd) == NULL) {
 
281
        (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
 
282
        (*thd)->pool = pool;
 
283
    }
 
284
 
 
285
    (*thd)->td = thethd;
 
286
    return APR_SUCCESS;
 
287
}
 
288
 
 
289
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
 
290
                                               apr_pool_t *p)
 
291
{
 
292
    static const pthread_once_t once_init = PTHREAD_ONCE_INIT;
 
293
 
 
294
    *control = apr_palloc(p, sizeof(**control));
 
295
    (*control)->once = once_init;
 
296
    return APR_SUCCESS;
 
297
}
 
298
 
 
299
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
 
300
                                          void (*func)(void))
 
301
{
 
302
    return pthread_once(&control->once, func);
 
303
}
 
304
 
 
305
APR_POOL_IMPLEMENT_ACCESSOR(thread)
 
306
 
 
307
#endif  /* HAVE_PTHREAD_H */
 
308
#endif  /* APR_HAS_THREADS */
 
309
 
 
310
#if !APR_HAS_THREADS
 
311
 
 
312
/* avoid warning for no prototype */
 
313
APR_DECLARE(apr_status_t) apr_os_thread_get(void);
 
314
 
 
315
APR_DECLARE(apr_status_t) apr_os_thread_get(void)
 
316
{
 
317
    return APR_ENOTIMPL;
 
318
}
 
319
 
 
320
#endif