mutex.c revision 251881
1/*
2 * svn_mutex.c: routines for mutual exclusion.
3 *
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
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
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
20 *    under the License.
21 * ====================================================================
22 */
23
24#include "svn_private_config.h"
25#include "private/svn_mutex.h"
26
27svn_error_t *
28svn_mutex__init(svn_mutex__t **mutex_p,
29                svn_boolean_t mutex_required,
30                apr_pool_t *result_pool)
31{
32  /* always initialize the mutex pointer, even though it is not
33     strictly necessary if APR_HAS_THREADS has not been set */
34  *mutex_p = NULL;
35
36#if APR_HAS_THREADS
37  if (mutex_required)
38    {
39      apr_thread_mutex_t *apr_mutex;
40      apr_status_t status =
41          apr_thread_mutex_create(&apr_mutex,
42                                  APR_THREAD_MUTEX_DEFAULT,
43                                  result_pool);
44      if (status)
45        return svn_error_wrap_apr(status, _("Can't create mutex"));
46
47      *mutex_p = apr_mutex;
48    }
49#endif
50
51  return SVN_NO_ERROR;
52}
53
54svn_error_t *
55svn_mutex__lock(svn_mutex__t *mutex)
56{
57#if APR_HAS_THREADS
58  if (mutex)
59    {
60      apr_status_t status = apr_thread_mutex_lock(mutex);
61      if (status)
62        return svn_error_wrap_apr(status, _("Can't lock mutex"));
63    }
64#endif
65
66  return SVN_NO_ERROR;
67}
68
69svn_error_t *
70svn_mutex__unlock(svn_mutex__t *mutex,
71                  svn_error_t *err)
72{
73#if APR_HAS_THREADS
74  if (mutex)
75    {
76      apr_status_t status = apr_thread_mutex_unlock(mutex);
77      if (status && !err)
78        return svn_error_wrap_apr(status, _("Can't unlock mutex"));
79    }
80#endif
81
82  return err;
83}
84