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

« back to all changes in this revision

Viewing changes to srclib/apr/threadproc/beos/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_arch_threadproc.h"
 
18
#include "apr_portable.h"
 
19
 
 
20
APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new, apr_pool_t *pool)
 
21
{
 
22
    (*new) = (apr_threadattr_t *)apr_palloc(pool, 
 
23
              sizeof(apr_threadattr_t));
 
24
 
 
25
    if ((*new) == NULL) {
 
26
        return APR_ENOMEM;
 
27
    }
 
28
 
 
29
    (*new)->pool = pool;
 
30
        (*new)->attr = (int32)B_NORMAL_PRIORITY;
 
31
 
 
32
    return APR_SUCCESS;
 
33
}
 
34
 
 
35
APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr, apr_int32_t on)
 
36
{
 
37
        if (on == 1){
 
38
                attr->detached = 1;
 
39
        } else {
 
40
                attr->detached = 0;
 
41
        }    
 
42
    return APR_SUCCESS;
 
43
}
 
44
 
 
45
APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
 
46
{
 
47
        if (attr->detached == 1){
 
48
                return APR_DETACH;
 
49
        }
 
50
        return APR_NOTDETACH;
 
51
}
 
52
 
 
53
APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
 
54
                                                       apr_size_t stacksize)
 
55
{
 
56
    return APR_ENOTIMPL;
 
57
}
 
58
 
 
59
APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
 
60
                                                       apr_size_t size)
 
61
{
 
62
    return APR_ENOTIMPL;
 
63
}
 
64
 
 
65
static void *dummy_worker(void *opaque)
 
66
{
 
67
    apr_thread_t *thd = (apr_thread_t*)opaque;
 
68
    return thd->func(thd, thd->data);
 
69
}
 
70
 
 
71
APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr,
 
72
                                            apr_thread_start_t func, void *data,
 
73
                                            apr_pool_t *pool)
 
74
{
 
75
    int32 temp;
 
76
    apr_status_t stat;
 
77
    
 
78
    (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
 
79
    if ((*new) == NULL) {
 
80
        return APR_ENOMEM;
 
81
    }
 
82
 
 
83
    (*new)->pool = pool;
 
84
    (*new)->data = data;
 
85
    (*new)->func = func;
 
86
    (*new)->exitval = -1;
 
87
 
 
88
    /* First we create the new thread...*/
 
89
        if (attr)
 
90
            temp = attr->attr;
 
91
        else
 
92
            temp = B_NORMAL_PRIORITY;
 
93
 
 
94
    stat = apr_pool_create(&(*new)->pool, pool);
 
95
    if (stat != APR_SUCCESS) {
 
96
        return stat;
 
97
    }
 
98
 
 
99
    (*new)->td = spawn_thread((thread_func)dummy_worker, 
 
100
                              "apr thread", 
 
101
                              temp, 
 
102
                              (*new));
 
103
 
 
104
    /* Now we try to run it...*/
 
105
    if (resume_thread((*new)->td) == B_NO_ERROR) {
 
106
        return APR_SUCCESS;
 
107
    }
 
108
    else {
 
109
        return errno;
 
110
    } 
 
111
}
 
112
 
 
113
APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
 
114
{
 
115
    return find_thread(NULL);
 
116
}
 
117
 
 
118
int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
 
119
{
 
120
    return tid1 == tid2;
 
121
}
 
122
 
 
123
APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
 
124
{
 
125
    apr_pool_destroy(thd->pool);
 
126
    thd->exitval = retval;
 
127
    exit_thread ((status_t)(retval));
 
128
    /* This will never be reached... */
 
129
    return APR_SUCCESS;
 
130
}
 
131
 
 
132
APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd)
 
133
{
 
134
    status_t rv = 0, ret;
 
135
    ret = wait_for_thread(thd->td, &rv);
 
136
    if (ret == B_NO_ERROR) {
 
137
        *retval = rv;
 
138
        return APR_SUCCESS;
 
139
    }
 
140
    else {
 
141
        /* if we've missed the thread's death, did we set an exit value prior
 
142
         * to it's demise?  If we did return that.
 
143
         */
 
144
        if (thd->exitval != -1) {
 
145
            *retval = thd->exitval;
 
146
            return APR_SUCCESS;
 
147
        } else 
 
148
            return ret;
 
149
    }
 
150
}
 
151
 
 
152
APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
 
153
{
 
154
        if (suspend_thread(thd->td) == B_NO_ERROR){
 
155
        return APR_SUCCESS;
 
156
    }
 
157
    else {
 
158
        return errno;
 
159
    }
 
160
}
 
161
 
 
162
void apr_thread_yield()
 
163
{
 
164
}
 
165
 
 
166
APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key, apr_thread_t *thread)
 
167
{
 
168
    return apr_pool_userdata_get(data, key, thread->pool);
 
169
}
 
170
 
 
171
APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
 
172
                                              apr_status_t (*cleanup) (void *),
 
173
                                              apr_thread_t *thread)
 
174
{
 
175
    return apr_pool_userdata_set(data, key, cleanup, thread->pool);
 
176
}
 
177
 
 
178
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd, apr_thread_t *thd)
 
179
{
 
180
    *thethd = &thd->td;
 
181
    return APR_SUCCESS;
 
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 (pool == NULL) {
 
188
        return APR_ENOPOOL;
 
189
    }
 
190
    if ((*thd) == NULL) {
 
191
        (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
 
192
        (*thd)->pool = pool;
 
193
    }
 
194
    (*thd)->td = *thethd;
 
195
    return APR_SUCCESS;
 
196
}
 
197
 
 
198
static apr_status_t thread_once_cleanup(void *vcontrol)
 
199
{
 
200
    apr_thread_once_t *control = (apr_thread_once_t *)vcontrol;
 
201
 
 
202
    if (control->sem) {
 
203
        release_sem(control->sem);
 
204
        delete_sem(control->sem);
 
205
    }
 
206
 
 
207
    return APR_SUCCESS;
 
208
}
 
209
 
 
210
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
 
211
                                               apr_pool_t *p)
 
212
{
 
213
    int rc;
 
214
    *control = (apr_thread_once_t *)apr_pcalloc(p, sizeof(apr_thread_once_t));
 
215
    (*control)->hit = 0; /* we haven't done it yet... */
 
216
    rc = ((*control)->sem = create_sem(1, "thread_once"));
 
217
    if (rc < 0)
 
218
        return rc;
 
219
 
 
220
    apr_pool_cleanup_register(p, control, thread_once_cleanup, apr_pool_cleanup_null);
 
221
    return APR_SUCCESS;
 
222
}
 
223
 
 
224
 
 
225
 
 
226
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control, 
 
227
                                          void (*func)(void))
 
228
{
 
229
    if (!control->hit) {
 
230
        if (acquire_sem(control->sem) == B_OK) {
 
231
            control->hit = 1;
 
232
            func();
 
233
        }
 
234
    }
 
235
    return APR_SUCCESS;
 
236
}
 
237
 
 
238
APR_POOL_IMPLEMENT_ACCESSOR(thread)