upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / locks / unix / thread_rwlock.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_thread_rwlock.h"
18 #include "apr_private.h"
19
20 #if APR_HAS_THREADS
21
22 #ifdef HAVE_PTHREAD_RWLOCKS
23
24 static apr_status_t thread_rwlock_cleanup(void *data)
25 {
26     apr_thread_rwlock_t *rwlock = (apr_thread_rwlock_t *)data;
27     apr_status_t stat;
28
29     pthread_rwlock_unlock(rwlock->rwlock);
30     stat = pthread_rwlock_destroy(rwlock->rwlock);
31 #ifdef PTHREAD_SETS_ERRNO
32     if (stat) {
33         stat = errno;
34     }
35 #endif
36     return stat;
37
38
39 APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
40                                                    apr_pool_t *pool)
41 {
42     apr_thread_rwlock_t *new_rwlock;
43     apr_status_t stat;
44
45     new_rwlock = (apr_thread_rwlock_t *)apr_pcalloc(pool,
46                                                   sizeof(apr_thread_rwlock_t));
47
48     if (new_rwlock == NULL) {
49         return APR_ENOMEM;
50     }
51
52     new_rwlock->pool = pool;
53     new_rwlock->rwlock = (pthread_rwlock_t *)apr_palloc(pool, 
54                                                      sizeof(pthread_rwlock_t));
55
56     if (new_rwlock->rwlock == NULL) {
57         return APR_ENOMEM;
58     }
59
60     if ((stat = pthread_rwlock_init(new_rwlock->rwlock, NULL))) {
61 #ifdef PTHREAD_SETS_ERRNO
62         stat = errno;
63 #endif
64         thread_rwlock_cleanup(new_rwlock);
65         return stat;
66     }
67
68     apr_pool_cleanup_register(new_rwlock->pool,
69                               (void *)new_rwlock, thread_rwlock_cleanup,
70                               apr_pool_cleanup_null);
71
72     *rwlock = new_rwlock;
73     return APR_SUCCESS;
74 }
75
76 APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
77 {
78     apr_status_t stat;
79
80     stat = pthread_rwlock_rdlock(rwlock->rwlock);
81 #ifdef PTHREAD_SETS_ERRNO
82     if (stat) {
83         stat = errno;
84     }
85 #endif
86     return stat;
87 }
88
89 APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
90 {
91     apr_status_t stat;
92
93     stat = pthread_rwlock_tryrdlock(rwlock->rwlock);
94 #ifdef PTHREAD_SETS_ERRNO
95     if (stat) {
96         stat = errno;
97     }
98 #endif
99     /* Normalize the return code. */
100     if (stat == EBUSY)
101         stat = APR_EBUSY;
102     return stat;
103 }
104
105 APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
106 {
107     apr_status_t stat;
108
109     stat = pthread_rwlock_wrlock(rwlock->rwlock);
110 #ifdef PTHREAD_SETS_ERRNO
111     if (stat) {
112         stat = errno;
113     }
114 #endif
115     return stat;
116 }
117
118 APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
119 {
120     apr_status_t stat;
121
122     stat = pthread_rwlock_trywrlock(rwlock->rwlock);
123 #ifdef PTHREAD_SETS_ERRNO
124     if (stat) {
125         stat = errno;
126     }
127 #endif
128     /* Normalize the return code. */
129     if (stat == EBUSY)
130         stat = APR_EBUSY;
131     return stat;
132 }
133
134 APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
135 {
136     apr_status_t stat;
137
138     stat = pthread_rwlock_unlock(rwlock->rwlock);
139 #ifdef PTHREAD_SETS_ERRNO
140     if (stat) {
141         stat = errno;
142     }
143 #endif
144     return stat;
145 }
146
147 APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
148 {
149     apr_status_t stat;
150     if ((stat = thread_rwlock_cleanup(rwlock)) == APR_SUCCESS) {
151         apr_pool_cleanup_kill(rwlock->pool, rwlock, thread_rwlock_cleanup);
152         return APR_SUCCESS;
153     }
154     return stat;
155 }
156
157 #else  /* HAVE_PTHREAD_RWLOCKS */
158
159 APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
160                                                    apr_pool_t *pool)
161 {
162     return APR_ENOTIMPL;
163 }
164
165 APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
166 {
167     return APR_ENOTIMPL;
168 }
169
170 APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
171 {
172     return APR_ENOTIMPL;
173 }
174
175 APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
176 {
177     return APR_ENOTIMPL;
178 }
179
180 APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
181 {
182     return APR_ENOTIMPL;
183 }
184
185 APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
186 {
187     return APR_ENOTIMPL;
188 }
189
190 APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
191 {
192     return APR_ENOTIMPL;
193 }
194
195 #endif /* HAVE_PTHREAD_RWLOCKS */
196 APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)
197
198 #endif /* APR_HAS_THREADS */