Apache HTTPD
thread_mutex.c
Go to the documentation of this file.
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/*Read/Write locking implementation based on the MultiLock code from
18 * Stephen Beaulieu <[email protected]>
19 */
20
21#include "apr_arch_thread_mutex.h"
22#include "apr_strings.h"
23#include "apr_portable.h"
24
26{
28 if (lock->LockCount != 0) {
29 /* we're still locked... */
30 while (atomic_add(&lock->LockCount , -1) > 1){
31 /* OK we had more than one person waiting on the lock so
32 * the sem is also locked. Release it until we have no more
33 * locks left.
34 */
35 release_sem (lock->Lock);
36 }
37 }
38 delete_sem(lock->Lock);
39 return APR_SUCCESS;
40}
41
43 unsigned int flags,
45{
48
50 if (new_m == NULL){
51 return APR_ENOMEM;
52 }
53
54 if ((stat = create_sem(0, "APR_Lock")) < B_NO_ERROR) {
56 return stat;
57 }
58 new_m->LockCount = 0;
59 new_m->Lock = stat;
60 new_m->pool = pool;
61
62 /* Optimal default is APR_THREAD_MUTEX_UNNESTED,
63 * no additional checks required for either flag.
64 */
66
69
70 (*mutex) = new_m;
71 return APR_SUCCESS;
72}
73
74#if APR_HAS_CREATE_LOCKS_NP
76 const char *fname,
79{
80 return APR_ENOTIMPL;
81}
82#endif
83
85{
86 int32 stat;
88
89 if (mutex->nested && mutex->owner == me) {
90 mutex->owner_ref++;
91 return APR_SUCCESS;
92 }
93
94 if (atomic_add(&mutex->LockCount, 1) > 0) {
95 if ((stat = acquire_sem(mutex->Lock)) < B_NO_ERROR) {
96 /* Oh dear, acquire_sem failed!! */
97 atomic_add(&mutex->LockCount, -1);
98 return stat;
99 }
100 }
101
102 mutex->owner = me;
103 mutex->owner_ref = 1;
104
105 return APR_SUCCESS;
106}
107
109{
110 int32 stat;
112
113 if (mutex->nested && mutex->owner == me) {
114 mutex->owner_ref++;
115 return APR_SUCCESS;
116 }
117
118 if (atomic_add(&mutex->LockCount, 1) > 0) {
119 if ((stat = acquire_sem_etc(mutex->Lock, 1, 0, 0)) < B_NO_ERROR) {
120 atomic_add(&mutex->LockCount, -1);
121 if (stat == B_WOULD_BLOCK) {
122 stat = APR_EBUSY;
123 }
124 return stat;
125 }
126 }
127
128 mutex->owner = me;
129 mutex->owner_ref = 1;
130
131 return APR_SUCCESS;
132}
133
136{
137 int32 stat;
139
140 if (mutex->nested && mutex->owner == me) {
141 mutex->owner_ref++;
142 return APR_SUCCESS;
143 }
144
145 if (atomic_add(&mutex->LockCount, 1) > 0) {
146 if (timeout <= 0) {
148 }
149 else {
151 timeout);
152 }
153 if (stat < B_NO_ERROR) {
154 atomic_add(&mutex->LockCount, -1);
155 if (stat == B_TIMED_OUT) {
157 }
158 return stat;
159 }
160 }
161
162 mutex->owner = me;
163 mutex->owner_ref = 1;
164
165 return APR_SUCCESS;
166}
167
169{
170 int32 stat;
171
172 if (mutex->nested && mutex->owner == find_thread(NULL)) {
173 mutex->owner_ref--;
174 if (mutex->owner_ref > 0)
175 return APR_SUCCESS;
176 }
177
178 if (atomic_add(&mutex->LockCount, -1) > 1) {
179 if ((stat = release_sem(mutex->Lock)) < B_NO_ERROR) {
180 atomic_add(&mutex->LockCount, 1);
181 return stat;
182 }
183 }
184
185 mutex->owner = -1;
186 mutex->owner_ref = 0;
187
188 return APR_SUCCESS;
189}
190
192{
194 if ((stat = _thread_mutex_cleanup(mutex)) == APR_SUCCESS) {
196 return APR_SUCCESS;
197 }
198 return stat;
199}
200
202
APR Portability Routines.
APR Strings library.
static apr_status_t _thread_mutex_cleanup(void *data)
static sem_id lock
Definition threadpriv.c:21
#define APR_ENOMEM
Definition apr_errno.h:683
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_TIMEUP
Definition apr_errno.h:450
#define APR_EBUSY
Definition apr_errno.h:480
const char apr_lockmech_e mech
const char apr_ssize_t int flags
Definition apr_encode.h:168
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
const char * fname
#define APR_POOL_IMPLEMENT_ACCESSOR(type)
Definition apr_pools.h:91
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
return NULL
Definition mod_so.c:359
IN ULONG IN INT timeout