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#ifndef APR_THREAD_MUTEX_H
18#define APR_THREAD_MUTEX_H
19
20/**
21 * @file apr_thread_mutex.h
22 * @brief APR Thread Mutex Routines
23 */
24
25#include "apr.h"
26#include "apr_errno.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif /* __cplusplus */
31
32#if APR_HAS_THREADS || defined(DOXYGEN)
33
34/**
35 * @defgroup apr_thread_mutex Thread Mutex Routines
36 * @ingroup APR
37 * @{
38 */
39
40/** Opaque thread-local mutex structure */
41typedef struct apr_thread_mutex_t apr_thread_mutex_t;
42
43#define APR_THREAD_MUTEX_DEFAULT  0x0   /**< platform-optimal lock behavior */
44#define APR_THREAD_MUTEX_NESTED   0x1   /**< enable nested (recursive) locks */
45#define APR_THREAD_MUTEX_UNNESTED 0x2   /**< disable nested locks */
46#define APR_THREAD_MUTEX_TIMED    0x4   /**< enable timed locks */
47
48/* Delayed the include to avoid a circular reference */
49#include "apr_pools.h"
50#include "apr_time.h"
51
52/**
53 * Create and initialize a mutex that can be used to synchronize threads.
54 * @param mutex the memory address where the newly created mutex will be
55 *        stored.
56 * @param flags Or'ed value of:
57 * <PRE>
58 *           APR_THREAD_MUTEX_DEFAULT   platform-optimal lock behavior.
59 *           APR_THREAD_MUTEX_NESTED    enable nested (recursive) locks.
60 *           APR_THREAD_MUTEX_UNNESTED  disable nested locks (non-recursive).
61 * </PRE>
62 * @param pool the pool from which to allocate the mutex.
63 * @warning Be cautious in using APR_THREAD_MUTEX_DEFAULT.  While this is the
64 * most optimal mutex based on a given platform's performance characteristics,
65 * it will behave as either a nested or an unnested lock.
66 */
67APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
68                                                  unsigned int flags,
69                                                  apr_pool_t *pool);
70/**
71 * Acquire the lock for the given mutex. If the mutex is already locked,
72 * the current thread will be put to sleep until the lock becomes available.
73 * @param mutex the mutex on which to acquire the lock.
74 */
75APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex);
76
77/**
78 * Attempt to acquire the lock for the given mutex. If the mutex has already
79 * been acquired, the call returns immediately with APR_EBUSY. Note: it
80 * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
81 * if the return value was APR_EBUSY, for portability reasons.
82 * @param mutex the mutex on which to attempt the lock acquiring.
83 */
84APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex);
85
86/**
87 * Attempt to acquire the lock for the given mutex until timeout expires.
88 * If the acquisition time outs, the call returns with APR_TIMEUP.
89 * @param mutex the mutex on which to attempt the lock acquiring.
90 * @param timeout the relative timeout (microseconds).
91 * @note A timeout negative or nul means immediate attempt, returning
92 *       APR_TIMEUP without blocking if it the lock is already acquired.
93 */
94APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex,
95                                                 apr_interval_time_t timeout);
96
97/**
98 * Release the lock for the given mutex.
99 * @param mutex the mutex from which to release the lock.
100 */
101APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex);
102
103/**
104 * Destroy the mutex and free the memory associated with the lock.
105 * @param mutex the mutex to destroy.
106 */
107APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex);
108
109/**
110 * Get the pool used by this thread_mutex.
111 * @return apr_pool_t the pool
112 */
113APR_POOL_DECLARE_ACCESSOR(thread_mutex);
114
115#endif /* APR_HAS_THREADS */
116
117/** @} */
118
119#ifdef __cplusplus
120}
121#endif
122
123#endif  /* ! APR_THREAD_MUTEX_H */
124