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_PROC_MUTEX_H
18#define APR_PROC_MUTEX_H
19
20/**
21 * @file apr_proc_mutex.h
22 * @brief APR Process Locking Routines
23 */
24
25#include "apr.h"
26#include "apr_pools.h"
27#include "apr_errno.h"
28#include "apr_perms_set.h"
29#include "apr_time.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif /* __cplusplus */
34
35/**
36 * @defgroup apr_proc_mutex Process Locking Routines
37 * @ingroup APR
38 * @{
39 */
40
41/**
42 * Enumerated potential types for APR process locking methods
43 * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
44 *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
45 */
46typedef enum {
47    APR_LOCK_FCNTL,         /**< fcntl() */
48    APR_LOCK_FLOCK,         /**< flock() */
49    APR_LOCK_SYSVSEM,       /**< System V Semaphores */
50    APR_LOCK_PROC_PTHREAD,  /**< POSIX pthread process-based locking */
51    APR_LOCK_POSIXSEM,      /**< POSIX semaphore process-based locking */
52    APR_LOCK_DEFAULT,       /**< Use the default process lock */
53    APR_LOCK_DEFAULT_TIMED  /**< Use the default process timed lock */
54} apr_lockmech_e;
55
56/** Opaque structure representing a process mutex. */
57typedef struct apr_proc_mutex_t apr_proc_mutex_t;
58
59/*   Function definitions */
60
61/**
62 * Create and initialize a mutex that can be used to synchronize processes.
63 * @param mutex the memory address where the newly created mutex will be
64 *        stored.
65 * @param fname A file name to use if the lock mechanism requires one.  This
66 *        argument should always be provided.  The lock code itself will
67 *        determine if it should be used.
68 * @param mech The mechanism to use for the interprocess lock, if any; one of
69 * <PRE>
70 *            APR_LOCK_FCNTL
71 *            APR_LOCK_FLOCK
72 *            APR_LOCK_SYSVSEM
73 *            APR_LOCK_POSIXSEM
74 *            APR_LOCK_PROC_PTHREAD
75 *            APR_LOCK_DEFAULT     pick the default mechanism for the platform
76 * </PRE>
77 * @param pool the pool from which to allocate the mutex.
78 * @see apr_lockmech_e
79 * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
80 *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
81 */
82APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
83                                                const char *fname,
84                                                apr_lockmech_e mech,
85                                                apr_pool_t *pool);
86
87/**
88 * Re-open a mutex in a child process.
89 * @param mutex The newly re-opened mutex structure.
90 * @param fname A file name to use if the mutex mechanism requires one.  This
91 *              argument should always be provided.  The mutex code itself will
92 *              determine if it should be used.  This filename should be the
93 *              same one that was passed to apr_proc_mutex_create().
94 * @param pool The pool to operate on.
95 * @remark This function must be called to maintain portability, even
96 *         if the underlying lock mechanism does not require it.
97 */
98APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
99                                                    const char *fname,
100                                                    apr_pool_t *pool);
101
102/**
103 * Acquire the lock for the given mutex. If the mutex is already locked,
104 * the current thread will be put to sleep until the lock becomes available.
105 * @param mutex the mutex on which to acquire the lock.
106 */
107APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex);
108
109/**
110 * Attempt to acquire the lock for the given mutex. If the mutex has already
111 * been acquired, the call returns immediately with APR_EBUSY. Note: it
112 * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
113 * if the return value was APR_EBUSY, for portability reasons.
114 * @param mutex the mutex on which to attempt the lock acquiring.
115 */
116APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex);
117
118/**
119 * Attempt to acquire the lock for the given mutex until timeout expires.
120 * If the acquisition time outs, the call returns with APR_TIMEUP.
121 * @param mutex the mutex on which to attempt the lock acquiring.
122 * @param timeout the relative timeout (microseconds).
123 * @note A negative or nul timeout means immediate attempt, returning
124 *       APR_TIMEUP without blocking if it the lock is already acquired.
125 */
126APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex,
127                                               apr_interval_time_t timeout);
128
129/**
130 * Release the lock for the given mutex.
131 * @param mutex the mutex from which to release the lock.
132 */
133APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex);
134
135/**
136 * Destroy the mutex and free the memory associated with the lock.
137 * @param mutex the mutex to destroy.
138 */
139APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex);
140
141/**
142 * Destroy the mutex and free the memory associated with the lock.
143 * @param mutex the mutex to destroy.
144 * @note This function is generally used to kill a cleanup on an already
145 *       created mutex
146 */
147APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex);
148
149/**
150 * Return the name of the lockfile for the mutex, or NULL
151 * if the mutex doesn't use a lock file
152 */
153
154APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex);
155
156/**
157 * Get the mechanism of the mutex, as it relates to the actual method
158 * used for the underlying apr_proc_mutex_t.
159 * @param mutex the mutex to get the mechanism from.
160 */
161APR_DECLARE(apr_lockmech_e) apr_proc_mutex_mech(apr_proc_mutex_t *mutex);
162
163/**
164 * Get the mechanism's name of the mutex, as it relates to the actual method
165 * used for the underlying apr_proc_mutex_t.
166 * @param mutex the mutex to get the mechanism's name from.
167 */
168APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex);
169
170/**
171 * Display the name of the default mutex: APR_LOCK_DEFAULT
172 */
173APR_DECLARE(const char *) apr_proc_mutex_defname(void);
174
175/**
176 * Set mutex permissions.
177 */
178APR_PERMS_SET_IMPLEMENT(proc_mutex);
179
180/**
181 * Get the pool used by this proc_mutex.
182 * @return apr_pool_t the pool
183 */
184APR_POOL_DECLARE_ACCESSOR(proc_mutex);
185
186/** @} */
187
188#ifdef __cplusplus
189}
190#endif
191
192#endif  /* ! APR_PROC_MUTEX_H */
193