apr_thread_mutex.h revision 302408
1129198Scognet/* Licensed to the Apache Software Foundation (ASF) under one or more
2129198Scognet * contributor license agreements.  See the NOTICE file distributed with
3139735Simp * this work for additional information regarding copyright ownership.
4129198Scognet * The ASF licenses this file to You under the Apache License, Version 2.0
5129198Scognet * (the "License"); you may not use this file except in compliance with
6129198Scognet * the License.  You may obtain a copy of the License at
7129198Scognet *
8129198Scognet *     http://www.apache.org/licenses/LICENSE-2.0
9129198Scognet *
10129198Scognet * Unless required by applicable law or agreed to in writing, software
11129198Scognet * distributed under the License is distributed on an "AS IS" BASIS,
12129198Scognet * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13129198Scognet * See the License for the specific language governing permissions and
14129198Scognet * limitations under the License.
15129198Scognet */
16129198Scognet
17129198Scognet#ifndef APR_THREAD_MUTEX_H
18129198Scognet#define APR_THREAD_MUTEX_H
19129198Scognet
20129198Scognet/**
21129198Scognet * @file apr_thread_mutex.h
22129198Scognet * @brief APR Thread Mutex Routines
23129198Scognet */
24129198Scognet
25129198Scognet#include "apr.h"
26129198Scognet#include "apr_errno.h"
27129198Scognet
28129198Scognet#ifdef __cplusplus
29129198Scognetextern "C" {
30129198Scognet#endif /* __cplusplus */
31129198Scognet
32129198Scognet#if APR_HAS_THREADS || defined(DOXYGEN)
33129198Scognet
34129198Scognet/**
35129198Scognet * @defgroup apr_thread_mutex Thread Mutex Routines
36129198Scognet * @ingroup APR
37129198Scognet * @{
38129198Scognet */
39129198Scognet
40129198Scognet/** Opaque thread-local mutex structure */
41129198Scognettypedef struct apr_thread_mutex_t apr_thread_mutex_t;
42276190Sian
43129198Scognet#define APR_THREAD_MUTEX_DEFAULT  0x0   /**< platform-optimal lock behavior */
44129198Scognet#define APR_THREAD_MUTEX_NESTED   0x1   /**< enable nested (recursive) locks */
45129198Scognet#define APR_THREAD_MUTEX_UNNESTED 0x2   /**< disable nested locks */
46129198Scognet
47276190Sian/* Delayed the include to avoid a circular reference */
48129198Scognet#include "apr_pools.h"
49129198Scognet
50276190Sian/**
51129198Scognet * Create and initialize a mutex that can be used to synchronize threads.
52129198Scognet * @param mutex the memory address where the newly created mutex will be
53129198Scognet *        stored.
54129198Scognet * @param flags Or'ed value of:
55290979Szbb * <PRE>
56290979Szbb *           APR_THREAD_MUTEX_DEFAULT   platform-optimal lock behavior.
57290979Szbb *           APR_THREAD_MUTEX_NESTED    enable nested (recursive) locks.
58290979Szbb *           APR_THREAD_MUTEX_UNNESTED  disable nested locks (non-recursive).
59290979Szbb * </PRE>
60290979Szbb * @param pool the pool from which to allocate the mutex.
61290979Szbb * @warning Be cautious in using APR_THREAD_MUTEX_DEFAULT.  While this is the
62276190Sian * most optimal mutex based on a given platform's performance characteristics,
63276190Sian * it will behave as either a nested or an unnested lock.
64276190Sian */
65290979SzbbAPR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
66239268Sgonzo                                                  unsigned int flags,
67239268Sgonzo                                                  apr_pool_t *pool);
68290648Smmel/**
69257231Scognet * Acquire the lock for the given mutex. If the mutex is already locked,
70276190Sian * the current thread will be put to sleep until the lock becomes available.
71257231Scognet * @param mutex the mutex on which to acquire the lock.
72257231Scognet */
73129198ScognetAPR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex);
74129198Scognet
75129198Scognet/**
76129198Scognet * Attempt to acquire the lock for the given mutex. If the mutex has already
77129198Scognet * been acquired, the call returns immediately with APR_EBUSY. Note: it
78129198Scognet * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
79129198Scognet * if the return value was APR_EBUSY, for portability reasons.
80129198Scognet * @param mutex the mutex on which to attempt the lock acquiring.
81132053Scognet */
82132053ScognetAPR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex);
83129198Scognet
84132516Scognet/**
85234785Sdim * Release the lock for the given mutex.
86129198Scognet * @param mutex the mutex from which to release the lock.
87129198Scognet */
88129198ScognetAPR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex);
89
90/**
91 * Destroy the mutex and free the memory associated with the lock.
92 * @param mutex the mutex to destroy.
93 */
94APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex);
95
96/**
97 * Get the pool used by this thread_mutex.
98 * @return apr_pool_t the pool
99 */
100APR_POOL_DECLARE_ACCESSOR(thread_mutex);
101
102#endif /* APR_HAS_THREADS */
103
104/** @} */
105
106#ifdef __cplusplus
107}
108#endif
109
110#endif  /* ! APR_THREAD_MUTEX_H */
111