2 * svn_mutex.c: routines for mutual exclusion.
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
24 #include <apr_portable.h>
26 #include "svn_private_config.h"
27 #include "private/svn_atomic.h"
28 #include "private/svn_mutex.h"
30 /* With CHECKED set to TRUE, LOCKED and OWNER must be set *after* acquiring
31 * the MUTEX and be reset *before* releasing it again. This is sufficient
32 * because we only want to check whether the current thread already holds
33 * the lock. And the current thread cannot be acquiring / releasing a lock
34 * *while* checking for recursion at the same time.
40 apr_thread_mutex_t *mutex;
44 /* Truly empty structs are not allowed. */
51 svn_mutex__init(svn_mutex__t **mutex_p,
52 svn_boolean_t mutex_required,
53 apr_pool_t *result_pool)
55 /* always initialize the mutex pointer, even though it is not
56 strictly necessary if APR_HAS_THREADS has not been set */
61 svn_mutex__t *mutex = apr_pcalloc(result_pool, sizeof(*mutex));
65 apr_thread_mutex_create(&mutex->mutex,
66 APR_THREAD_MUTEX_DEFAULT,
69 return svn_error_wrap_apr(status, _("Can't create mutex"));
79 svn_mutex__lock(svn_mutex__t *mutex)
84 apr_status_t status = apr_thread_mutex_lock(mutex->mutex);
86 return svn_error_wrap_apr(status, _("Can't lock mutex"));
94 svn_mutex__unlock(svn_mutex__t *mutex,
100 apr_status_t status = apr_thread_mutex_unlock(mutex->mutex);
102 return svn_error_wrap_apr(status, _("Can't unlock mutex"));