bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / threadproc / beos / 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_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 static void *dummy_worker(void *opaque)
60 {
61     apr_thread_t *thd = (apr_thread_t*)opaque;
62     return thd->func(thd, thd->data);
63 }
64
65 APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr,
66                                             apr_thread_start_t func, void *data,
67                                             apr_pool_t *pool)
68 {
69     int32 temp;
70     apr_status_t stat;
71     
72     (*new) = (apr_thread_t *)apr_palloc(pool, sizeof(apr_thread_t));
73     if ((*new) == NULL) {
74         return APR_ENOMEM;
75     }
76
77     (*new)->pool = pool;
78     (*new)->data = data;
79     (*new)->func = func;
80     (*new)->exitval = -1;
81
82     /* First we create the new thread...*/
83         if (attr)
84             temp = attr->attr;
85         else
86             temp = B_NORMAL_PRIORITY;
87
88     stat = apr_pool_create(&(*new)->pool, pool);
89     if (stat != APR_SUCCESS) {
90         return stat;
91     }
92
93     (*new)->td = spawn_thread((thread_func)dummy_worker, "apr thread", temp, (*new));
94     /* Now we try to run it...*/
95     if (resume_thread((*new)->td) == B_NO_ERROR) {
96         return APR_SUCCESS;
97     }
98     else {
99         return errno;
100     } 
101 }
102
103 APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
104 {
105     return find_thread(NULL);
106 }
107
108 int apr_os_thread_equal(apr_os_thread_t tid1, apr_os_thread_t tid2)
109 {
110     return tid1 == tid2;
111 }
112
113 APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
114 {
115     apr_pool_destroy(thd->pool);
116     thd->exitval = retval;
117     exit_thread ((status_t)(retval));
118     /* This will never be reached... */
119     return APR_SUCCESS;
120 }
121
122 APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval, apr_thread_t *thd)
123 {
124     status_t rv = 0, ret;
125     if ((ret = wait_for_thread(thd->td, &rv)) == B_NO_ERROR) {
126         *retval = rv;
127         return APR_SUCCESS;
128     }
129     else {
130         /* if we've missed the thread's death, did we set an exit value prior
131          * to it's demise?  If we did return that.
132          */
133         if (thd->exitval != -1) {
134             *retval = thd->exitval;
135             return APR_SUCCESS;
136         } else 
137             return ret;
138     }
139 }
140
141 APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
142 {
143         if (suspend_thread(thd->td) == B_NO_ERROR){
144         return APR_SUCCESS;
145     }
146     else {
147         return errno;
148     }
149 }
150
151 void apr_thread_yield()
152 {
153 }
154
155 APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key, apr_thread_t *thread)
156 {
157     return apr_pool_userdata_get(data, key, thread->pool);
158 }
159
160 APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
161                                               apr_status_t (*cleanup) (void *),
162                                               apr_thread_t *thread)
163 {
164     return apr_pool_userdata_set(data, key, cleanup, thread->pool);
165 }
166
167 APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd, apr_thread_t *thd)
168 {
169     *thethd = &thd->td;
170     return APR_SUCCESS;
171 }
172
173 APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd, apr_os_thread_t *thethd, 
174                                             apr_pool_t *pool)
175 {
176     if (pool == NULL) {
177         return APR_ENOPOOL;
178     }
179     if ((*thd) == NULL) {
180         (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
181         (*thd)->pool = pool;
182     }
183     (*thd)->td = *thethd;
184     return APR_SUCCESS;
185 }
186
187 static apr_status_t thread_once_cleanup(void *vcontrol)
188 {
189     apr_thread_once_t *control = (apr_thread_once_t *)vcontrol;
190
191     if (control->sem) {
192         release_sem(control->sem);
193         delete_sem(control->sem);
194     }
195
196     return APR_SUCCESS;
197 }
198
199 APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
200                                                apr_pool_t *p)
201 {
202     int rc;
203     *control = (apr_thread_once_t *)apr_pcalloc(p, sizeof(apr_thread_once_t));
204     (*control)->hit = 0; /* we haven't done it yet... */
205     rc = ((*control)->sem = create_sem(1, "thread_once"));
206     if (rc != 0) {
207         return rc;
208     }
209     apr_pool_cleanup_register(p, control, thread_once_cleanup, apr_pool_cleanup_null);
210     return APR_SUCCESS;
211 }
212
213
214
215 APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control, 
216                                           void (*func)(void))
217 {
218     if (!control->hit) {
219         if (acquire_sem(control->sem) == B_OK) {
220             control->hit = 1;
221             func();
222         }
223     }
224     return APR_SUCCESS;
225 }
226
227 APR_POOL_IMPLEMENT_ACCESSOR(thread)