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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef APR_PROC_MUTEX_H
18 #define APR_PROC_MUTEX_H
21 * @file apr_proc_mutex.h
22 * @brief APR Process Locking Routines
26 #include "apr_pools.h"
27 #include "apr_errno.h"
28 #include "apr_perms_set.h"
33 #endif /* __cplusplus */
36 * @defgroup apr_proc_mutex Process Locking Routines
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.
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 */
56 /** Opaque structure representing a process mutex. */
57 typedef struct apr_proc_mutex_t apr_proc_mutex_t;
59 /* Function definitions */
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
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
74 * APR_LOCK_PROC_PTHREAD
75 * APR_LOCK_DEFAULT pick the default mechanism for the platform
77 * @param pool the pool from which to allocate the mutex.
79 * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
80 * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable.
82 APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
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.
98 APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
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.
107 APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex);
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.
116 APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex);
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.
126 APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex,
127 apr_interval_time_t timeout);
130 * Release the lock for the given mutex.
131 * @param mutex the mutex from which to release the lock.
133 APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex);
136 * Destroy the mutex and free the memory associated with the lock.
137 * @param mutex the mutex to destroy.
139 APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex);
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
147 APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex);
150 * Return the name of the lockfile for the mutex, or NULL
151 * if the mutex doesn't use a lock file
154 APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex);
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.
161 APR_DECLARE(apr_lockmech_e) apr_proc_mutex_mech(apr_proc_mutex_t *mutex);
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.
168 APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex);
171 * Display the name of the default mutex: APR_LOCK_DEFAULT
173 APR_DECLARE(const char *) apr_proc_mutex_defname(void);
176 * Set mutex permissions.
178 APR_PERMS_SET_IMPLEMENT(proc_mutex);
181 * Get the pool used by this proc_mutex.
182 * @return apr_pool_t the pool
184 APR_POOL_DECLARE_ACCESSOR(proc_mutex);
192 #endif /* ! APR_PROC_MUTEX_H */