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_general.h"
18#include "apr_lib.h"
19#include "apr_strings.h"
20#include "apr_portable.h"
21#include "apr_arch_thread_mutex.h"
22#include "apr_arch_file_io.h"
23#include <string.h>
24#include <stddef.h>
25
26static apr_status_t thread_mutex_cleanup(void *themutex)
27{
28    apr_thread_mutex_t *mutex = themutex;
29    return apr_thread_mutex_destroy(mutex);
30}
31
32
33
34/* XXX: Need to respect APR_THREAD_MUTEX_[UN]NESTED flags argument
35 *      or return APR_ENOTIMPL!!!
36 */
37APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
38                                                  unsigned int flags,
39                                                  apr_pool_t *pool)
40{
41    apr_thread_mutex_t *new_mutex;
42    ULONG rc;
43
44    new_mutex = (apr_thread_mutex_t *)apr_palloc(pool, sizeof(apr_thread_mutex_t));
45    new_mutex->pool = pool;
46
47    rc = DosCreateMutexSem(NULL, &(new_mutex->hMutex), 0, FALSE);
48    *mutex = new_mutex;
49
50    if (!rc)
51        apr_pool_cleanup_register(pool, new_mutex, thread_mutex_cleanup, apr_pool_cleanup_null);
52
53    return APR_OS2_STATUS(rc);
54}
55
56
57
58APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
59{
60    ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT);
61    return APR_OS2_STATUS(rc);
62}
63
64
65
66APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
67{
68    ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
69    return APR_OS2_STATUS(rc);
70}
71
72
73
74APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
75{
76    ULONG rc = DosReleaseMutexSem(mutex->hMutex);
77    return APR_OS2_STATUS(rc);
78}
79
80
81
82APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
83{
84    ULONG rc;
85
86    if (mutex->hMutex == 0)
87        return APR_SUCCESS;
88
89    while (DosReleaseMutexSem(mutex->hMutex) == 0);
90
91    rc = DosCloseMutexSem(mutex->hMutex);
92
93    if (!rc) {
94        mutex->hMutex = 0;
95        return APR_SUCCESS;
96    }
97
98    return APR_FROM_OS_ERROR(rc);
99}
100
101APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
102
103