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.
18 #include "apr_private.h"
19 #include "apr_general.h"
20 #include "apr_strings.h"
21 #include "apr_arch_thread_mutex.h"
22 #include "apr_thread_mutex.h"
23 #include "apr_portable.h"
24 #include "apr_arch_misc.h"
26 static apr_status_t thread_mutex_cleanup(void *data)
28 apr_thread_mutex_t *lock = data;
30 if (lock->type == thread_mutex_critical_section) {
32 DeleteCriticalSection(&lock->section);
35 if (!CloseHandle(lock->handle)) {
36 return apr_get_os_error();
42 APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
46 (*mutex) = (apr_thread_mutex_t *)apr_palloc(pool, sizeof(**mutex));
48 (*mutex)->pool = pool;
50 if (flags & APR_THREAD_MUTEX_UNNESTED) {
51 /* Use an auto-reset signaled event, ready to accept one
54 (*mutex)->type = thread_mutex_unnested_event;
55 (*mutex)->handle = CreateEvent(NULL, FALSE, TRUE, NULL);
58 #if APR_HAS_UNICODE_FS
59 /* Critical Sections are terrific, performance-wise, on NT.
60 * On Win9x, we cannot 'try' on a critical section, so we
61 * use a [slower] mutex object, instead.
63 IF_WIN_OS_IS_UNICODE {
64 InitializeCriticalSection(&(*mutex)->section);
65 (*mutex)->type = thread_mutex_critical_section;
70 (*mutex)->type = thread_mutex_nested_mutex;
71 (*mutex)->handle = CreateMutex(NULL, FALSE, NULL);
77 apr_pool_cleanup_register((*mutex)->pool, (*mutex), thread_mutex_cleanup,
78 apr_pool_cleanup_null);
82 APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
84 if (mutex->type == thread_mutex_critical_section) {
85 EnterCriticalSection(&mutex->section);
88 DWORD rv = WaitForSingleObject(mutex->handle, INFINITE);
89 if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) {
90 return (rv == WAIT_TIMEOUT) ? APR_EBUSY : apr_get_os_error();
96 APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
98 if (mutex->type == thread_mutex_critical_section) {
99 if (!TryEnterCriticalSection(&mutex->section)) {
104 DWORD rv = WaitForSingleObject(mutex->handle, 0);
105 if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) {
106 return (rv == WAIT_TIMEOUT) ? APR_EBUSY : apr_get_os_error();
112 APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
114 if (mutex->type == thread_mutex_critical_section) {
115 LeaveCriticalSection(&mutex->section);
117 else if (mutex->type == thread_mutex_unnested_event) {
118 if (!SetEvent(mutex->handle)) {
119 return apr_get_os_error();
122 else if (mutex->type == thread_mutex_nested_mutex) {
123 if (!ReleaseMutex(mutex->handle)) {
124 return apr_get_os_error();
130 APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
132 return apr_pool_cleanup_run(mutex->pool, mutex, thread_mutex_cleanup);
135 APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)