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

« back to all changes in this revision

Viewing changes to srclib/apr/threadproc/netware/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_strings.h"
 
20
#include "apr_arch_threadproc.h"
 
21
 
 
22
static int thread_count = 0;
 
23
 
 
24
apr_status_t apr_threadattr_create(apr_threadattr_t **new,
 
25
                                                apr_pool_t *pool)
 
26
{
 
27
    (*new) = (apr_threadattr_t *)apr_palloc(pool, 
 
28
              sizeof(apr_threadattr_t));
 
29
 
 
30
    if ((*new) == NULL) {
 
31
        return APR_ENOMEM;
 
32
    }
 
33
 
 
34
    (*new)->pool = pool;
 
35
    (*new)->stack_size = APR_DEFAULT_STACK_SIZE;
 
36
    (*new)->detach = 0;
 
37
    (*new)->thread_name = NULL;
 
38
    return APR_SUCCESS;
 
39
}
 
40
 
 
41
apr_status_t apr_threadattr_detach_set(apr_threadattr_t *attr,apr_int32_t on)
 
42
{
 
43
    attr->detach = on;
 
44
        return APR_SUCCESS;   
 
45
}
 
46
 
 
47
apr_status_t apr_threadattr_detach_get(apr_threadattr_t *attr)
 
48
{
 
49
    if (attr->detach == 1)
 
50
        return APR_DETACH;
 
51
    return APR_NOTDETACH;
 
52
}
 
53
 
 
54
APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
 
55
                                                       apr_size_t stacksize)
 
56
{
 
57
    attr->stack_size = stacksize;
 
58
    return APR_SUCCESS;
 
59
}
 
60
 
 
61
APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
 
62
                                                       apr_size_t size)
 
63
{
 
64
    return APR_ENOTIMPL;
 
65
}
 
66
 
 
67
static void *dummy_worker(void *opaque)
 
68
{
 
69
    apr_thread_t *thd = (apr_thread_t *)opaque;
 
70
    return thd->func(thd, thd->data);
 
71
}
 
72
 
 
73
apr_status_t apr_thread_create(apr_thread_t **new,
 
74
                                                                                        apr_threadattr_t *attr, 
 
75
                                                        apr_thread_start_t func,
 
76
                                                                                        void *data,
 
77
                                                                                        apr_pool_t *pool)
 
78
{
 
79
    apr_status_t stat;
 
80
    long flags = NX_THR_BIND_CONTEXT;
 
81
        char threadName[NX_MAX_OBJECT_NAME_LEN+1];
 
82
    size_t stack_size = APR_DEFAULT_STACK_SIZE;
 
83
 
 
84
    if (attr && attr->thread_name) {
 
85
        strncpy (threadName, attr->thread_name, NX_MAX_OBJECT_NAME_LEN);
 
86
    }
 
87
    else {
 
88
            sprintf(threadName, "APR_thread %04ld", ++thread_count);
 
89
    }
 
90
 
 
91
    /* An original stack size of 0 will allow NXCreateThread() to
 
92
    *   assign a default system stack size.  An original stack
 
93
    *   size of less than 0 will assign the APR default stack size.
 
94
    *   anything else will be taken as is.
 
95
    */
 
96
    if (attr && (attr->stack_size >= 0)) {
 
97
        stack_size = attr->stack_size;
 
98
    }
 
99
    
 
100
    (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
 
101
 
 
102
    if ((*new) == NULL) {
 
103
        return APR_ENOMEM;
 
104
    }
 
105
    
 
106
    (*new)->pool = pool;
 
107
    (*new)->data = data;
 
108
    (*new)->func = func;
 
109
    (*new)->thread_name = (char*)apr_pstrdup(pool, threadName);
 
110
    
 
111
    stat = apr_pool_create(&(*new)->pool, pool);
 
112
    if (stat != APR_SUCCESS) {
 
113
        return stat;
 
114
    }
 
115
    
 
116
    if (attr && attr->detach) {
 
117
        flags |= NX_THR_DETACHED;
 
118
    }
 
119
    
 
120
    (*new)->ctx = NXContextAlloc(
 
121
        /* void(*start_routine)(void *arg)*/(void (*)(void *)) dummy_worker,
 
122
        /* void *arg */                                                                            (*new),
 
123
        /* int priority */                                                                         NX_PRIO_MED,
 
124
        /* NXSize_t stackSize */                                                           stack_size,
 
125
        /* long flags */                                                                           NX_CTX_NORMAL,
 
126
        /* int *error */                                                                           &stat);
 
127
                
 
128
                                                                           
 
129
        stat = NXContextSetName(
 
130
                        /* NXContext_t ctx */                   (*new)->ctx,
 
131
                        /* const char *name */                  threadName);
 
132
 
 
133
        stat = NXThreadCreate(
 
134
                /* NXContext_t context */               (*new)->ctx,
 
135
                /* long flags */                                flags,
 
136
                /* NXThreadId_t *thread_id */   &(*new)->td);
 
137
 
 
138
    if(stat==0)
 
139
        return APR_SUCCESS;
 
140
        
 
141
        return(stat);// if error    
 
142
}
 
143
 
 
144
apr_os_thread_t apr_os_thread_current()
 
145
{
 
146
    return NXThreadGetId();
 
147
}
 
148
 
 
149
int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
 
150
{
 
151
    return (tid1 == tid2);
 
152
}
 
153
 
 
154
void apr_thread_yield()
 
155
{
 
156
    NXThreadYield();
 
157
}
 
158
 
 
159
apr_status_t apr_thread_exit(apr_thread_t *thd,
 
160
                             apr_status_t retval)
 
161
{
 
162
    thd->exitval = retval;
 
163
    apr_pool_destroy(thd->pool);
 
164
    NXThreadExit(NULL);
 
165
    return APR_SUCCESS;
 
166
}
 
167
 
 
168
apr_status_t apr_thread_join(apr_status_t *retval,
 
169
                                          apr_thread_t *thd)
 
170
{
 
171
    apr_status_t  stat;    
 
172
    NXThreadId_t dthr;
 
173
 
 
174
    if ((stat = NXThreadJoin(thd->td, &dthr, NULL)) == 0) {
 
175
        *retval = thd->exitval;
 
176
        return APR_SUCCESS;
 
177
    }
 
178
    else {
 
179
        return stat;
 
180
    }
 
181
}
 
182
 
 
183
apr_status_t apr_thread_detach(apr_thread_t *thd)
 
184
{
 
185
    return APR_SUCCESS;
 
186
}
 
187
 
 
188
apr_status_t apr_thread_data_get(void **data, const char *key,
 
189
                                             apr_thread_t *thread)
 
190
{
 
191
    if (thread != NULL) {
 
192
            return apr_pool_userdata_get(data, key, thread->pool);
 
193
    }
 
194
    else {
 
195
        data = NULL;
 
196
        return APR_ENOTHREAD;
 
197
    }
 
198
}
 
199
 
 
200
apr_status_t apr_thread_data_set(void *data, const char *key,
 
201
                              apr_status_t (*cleanup) (void *),
 
202
                              apr_thread_t *thread)
 
203
{
 
204
    if (thread != NULL) {
 
205
       return apr_pool_userdata_set(data, key, cleanup, thread->pool);
 
206
    }
 
207
    else {
 
208
        data = NULL;
 
209
        return APR_ENOTHREAD;
 
210
    }
 
211
}
 
212
 
 
213
APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
 
214
                                            apr_thread_t *thd)
 
215
{
 
216
    if (thd == NULL) {
 
217
        return APR_ENOTHREAD;
 
218
    }
 
219
    *thethd = &(thd->td);
 
220
    return APR_SUCCESS;
 
221
}
 
222
 
 
223
APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
 
224
                                            apr_os_thread_t *thethd,
 
225
                                            apr_pool_t *pool)
 
226
{
 
227
    if (pool == NULL) {
 
228
        return APR_ENOPOOL;
 
229
    }
 
230
    if ((*thd) == NULL) {
 
231
        (*thd) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
 
232
        (*thd)->pool = pool;
 
233
    }
 
234
    (*thd)->td = *thethd;
 
235
    return APR_SUCCESS;
 
236
}
 
237
 
 
238
APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
 
239
                                               apr_pool_t *p)
 
240
{
 
241
    (*control) = apr_pcalloc(p, sizeof(**control));
 
242
    return APR_SUCCESS;
 
243
}
 
244
 
 
245
APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
 
246
                                          void (*func)(void))
 
247
{
 
248
    if (!atomic_xchg(&control->value, 1)) {
 
249
        func();
 
250
    }
 
251
    return APR_SUCCESS;
 
252
}
 
253
 
 
254
APR_POOL_IMPLEMENT_ACCESSOR(thread)
 
255
 
 
256