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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_general.h"
19 #include "apr_strings.h"
20 #include "apr_portable.h"
21 #include "apr_arch_proc_mutex.h"
22 #include "apr_arch_file_io.h"
26 #define CurrentTid (*_threadid)
28 static char *fixed_name(const char *fname, apr_pool_t *pool)
35 /* Semaphores don't live in the file system, fix up the name */
36 while (*fname == '/' || *fname == '\\') {
40 semname = apr_pstrcat(pool, "/SEM32/", fname, NULL);
42 if (semname[8] == ':') {
52 APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *vmutex)
54 apr_proc_mutex_t *mutex = vmutex;
55 return apr_proc_mutex_destroy(mutex);
58 APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
63 APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
68 APR_DECLARE(const char *) apr_proc_mutex_defname(void)
74 APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
79 apr_proc_mutex_t *new;
83 if (mech != APR_LOCK_DEFAULT) {
87 new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
93 semname = fixed_name(fname, pool);
94 rc = DosCreateMutexSem(semname, &(new->hMutex), DC_SEM_SHARED, FALSE);
97 apr_pool_cleanup_register(pool, new, apr_proc_mutex_cleanup, apr_pool_cleanup_null);
100 return APR_FROM_OS_ERROR(rc);
105 APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
109 apr_proc_mutex_t *new;
113 new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
118 semname = fixed_name(fname, pool);
119 rc = DosOpenMutexSem(semname, &(new->hMutex));
123 apr_pool_cleanup_register(pool, new, apr_proc_mutex_cleanup, apr_pool_cleanup_null);
126 return APR_FROM_OS_ERROR(rc);
131 APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
133 ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT);
136 mutex->owner = CurrentTid;
140 return APR_FROM_OS_ERROR(rc);
145 APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
147 ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
150 mutex->owner = CurrentTid;
154 return APR_FROM_OS_ERROR(rc);
159 APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
163 if (mutex->owner == CurrentTid && mutex->lock_count > 0) {
165 rc = DosReleaseMutexSem(mutex->hMutex);
166 return APR_FROM_OS_ERROR(rc);
174 APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
177 apr_status_t status = APR_SUCCESS;
179 if (mutex->owner == CurrentTid) {
180 while (mutex->lock_count > 0 && status == APR_SUCCESS) {
181 status = apr_proc_mutex_unlock(mutex);
185 if (status != APR_SUCCESS) {
189 if (mutex->hMutex == 0) {
193 rc = DosCloseMutexSem(mutex->hMutex);
199 return APR_FROM_OS_ERROR(rc);
204 APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
208 /* Implement OS-specific accessors defined in apr_portable.h */
210 APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
211 apr_proc_mutex_t *pmutex)
213 *ospmutex = pmutex->hMutex;
219 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
220 apr_os_proc_mutex_t *ospmutex,
223 apr_proc_mutex_t *new;
225 new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
229 new->hMutex = *ospmutex;