upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / threadproc / win32 / thread.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  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 static void *dummy_worker(void *opaque)
70 {
71     apr_thread_t *thd = (apr_thread_t *)opaque;
72     TlsSetValue(tls_apr_thread, thd->td);
73     return thd->func(thd, thd->data);
74 }
75
76 APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
77                                             apr_threadattr_t *attr,
78                                             apr_thread_start_t func,
79                                             void *data, apr_pool_t *pool)
80 {
81     apr_status_t stat;
82         unsigned temp;
83     HANDLE   handle;
84     
85     (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
86
87     if ((*new) == NULL) {
88         return APR_ENOMEM;
89     }
90
91     (*new)->pool = pool;
92     (*new)->data = data;
93     (*new)->func = func;
94     (*new)->td   = NULL;
95
96     stat = apr_pool_create(&(*new)->pool, pool);
97     if (stat != APR_SUCCESS) {
98         return stat;
99     }
100
101     /* Use 0 for Thread Stack Size, because that will default the stack to the
102      * same size as the calling thread. 
103      */
104 #ifndef _WIN32_WCE
105     if ((handle = (HANDLE)_beginthreadex(NULL,
106                         attr && attr->stacksize > 0 ? attr->stacksize : 0,
107                         (unsigned int (APR_THREAD_FUNC *)(void *))dummy_worker,
108                         (*new), 0, &temp)) == 0) {
109         return APR_FROM_OS_ERROR(_doserrno);
110     }
111 #else
112    if ((handle = CreateThread(NULL,
113                         attr && attr->stacksize > 0 ? attr->stacksize : 0,
114                         (unsigned int (APR_THREAD_FUNC *)(void *))dummy_worker,
115                         (*new), 0, &temp)) == 0) {
116         return apr_get_os_error();
117     }
118 #endif
119     if (attr && attr->detach) {
120         CloseHandle(handle);
121     }
122     else
123         (*new)->td = handle;
124
125     return APR_SUCCESS;
126 }
127
128 APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
129                                           apr_status_t retval)
130 {
131     thd->exitval = retval;
132     apr_pool_destroy(thd->pool);
133     thd->pool = NULL;
134 #ifndef _WIN32_WCE
135     _endthreadex(0);
136 #else
137     ExitThread(0);
138 #endif
139     return APR_SUCCESS;
140 }
141
142 APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
143                                           apr_thread_t *thd)
144 {
145     apr_status_t rv = APR_SUCCESS;
146     
147     if (!thd->td) {
148         /* Can not join on detached threads */
149         return APR_DETACH;
150     }
151     rv = WaitForSingleObject(thd->td, INFINITE);
152     if ( rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) {
153         /* If the thread_exit has been called */
154         if (!thd->pool)
155             *retval = thd->exitval;
156         else
157             rv = APR_INCOMPLETE;
158     }
159     else
160         rv = apr_get_os_error();
161     CloseHandle(thd->td);
162     thd->td = NULL;
163
164     return rv;
165 }
166
167 APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
168 {
169     if (thd->td && CloseHandle(thd->td)) {
170         thd->td = NULL;
171         return APR_SUCCESS;
172     }
173     else {
174         return apr_get_os_error();
175     }
176 }
177
178 APR_DECLARE(void) apr_thread_yield()
179 {
180     /* SwitchToThread is not supported on Win9x, but since it's
181      * primarily a noop (entering time consuming code, therefore
182      * providing more critical threads a bit larger timeslice)
183      * we won't worry too much if it's not available.
184      */
185 #ifndef _WIN32_WCE
186     if (apr_os_level >= APR_WIN_NT) {
187         SwitchToThread();
188     }
189 #endif
190 }
191
192 APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
193                                              apr_thread_t *thread)
194 {
195     return apr_pool_userdata_get(data, key, thread->pool);
196 }
197
198 APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
199                                              apr_status_t (*cleanup) (void *),
200                                              apr_thread_t *thread)
201 {
202     return apr_pool_userdata_set(data, key, cleanup, thread->pool);
203 }
204
205
206 APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
207 {
208     HANDLE hthread = (HANDLE)TlsGetValue(tls_apr_thread);
209     HANDLE hproc;
210
211     if (hthread) {
212         return hthread;
213     }
214     
215     hproc = GetCurrentProcess();
216     hthread = GetCurrentThread();
217     if (!DuplicateHandle(hproc, hthread, 
218                          hproc, &hthread, 0, FALSE, 
219                          DUPLICATE_SAME_ACCESS)) {
220         return NULL;
221     }
222     TlsSetValue(tls_apr_thread, hthread);
223     return hthread;
224 }
225
226 APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
227                                             apr_thread_t *thd)
228 {
229     if (thd == NULL) {
230         return APR_ENOTHREAD;
231     }
232     *thethd = thd->td;
233     return APR_SUCCESS;
234 }
235
236 APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
237                                             apr_os_thread_t *thethd,
238                                             apr_pool_t *pool)
239 {
240     if (pool == NULL) {
241         return APR_ENOPOOL;
242     }
243     if ((*thd) == NULL) {
244         (*thd) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
245         (*thd)->pool = pool;
246     }
247     (*thd)->td = thethd;
248     return APR_SUCCESS;
249 }
250
251 APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
252                                                apr_pool_t *p)
253 {
254     (*control) = apr_pcalloc(p, sizeof(**control));
255     return APR_SUCCESS;
256 }
257
258 APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
259                                           void (*func)(void))
260 {
261     if (!InterlockedExchange(&control->value, 1)) {
262         func();
263     }
264     return APR_SUCCESS;
265 }
266
267 APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
268                                      apr_os_thread_t tid2)
269 {
270     /* Since the only tid's we support our are own, and
271      * apr_os_thread_current returns the identical handle
272      * to the one we created initially, the test is simple.
273      */
274     return (tid1 == tid2);
275 }
276
277 APR_POOL_IMPLEMENT_ACCESSOR(thread)