1251875Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2251875Speter * contributor license agreements.  See the NOTICE file distributed with
3251875Speter * this work for additional information regarding copyright ownership.
4251875Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5251875Speter * (the "License"); you may not use this file except in compliance with
6251875Speter * the License.  You may obtain a copy of the License at
7251875Speter *
8251875Speter *     http://www.apache.org/licenses/LICENSE-2.0
9251875Speter *
10251875Speter * Unless required by applicable law or agreed to in writing, software
11251875Speter * distributed under the License is distributed on an "AS IS" BASIS,
12251875Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13251875Speter * See the License for the specific language governing permissions and
14251875Speter * limitations under the License.
15251875Speter */
16251875Speter
17251875Speter#ifndef APR_PROC_MUTEX_H
18251875Speter#define APR_PROC_MUTEX_H
19251875Speter
20251875Speter/**
21251875Speter * @file apr_proc_mutex.h
22251875Speter * @brief APR Process Locking Routines
23251875Speter */
24251875Speter
25251875Speter#include "apr.h"
26251875Speter#include "apr_pools.h"
27251875Speter#include "apr_errno.h"
28362181Sdim#include "apr_perms_set.h"
29362181Sdim#include "apr_time.h"
30251875Speter
31251875Speter#ifdef __cplusplus
32251875Speterextern "C" {
33251875Speter#endif /* __cplusplus */
34251875Speter
35251875Speter/**
36251875Speter * @defgroup apr_proc_mutex Process Locking Routines
37251875Speter * @ingroup APR
38251875Speter * @{
39251875Speter */
40251875Speter
41251875Speter/**
42251875Speter * Enumerated potential types for APR process locking methods
43251875Speter * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
44251875Speter *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
45251875Speter */
46251875Spetertypedef enum {
47251875Speter    APR_LOCK_FCNTL,         /**< fcntl() */
48251875Speter    APR_LOCK_FLOCK,         /**< flock() */
49251875Speter    APR_LOCK_SYSVSEM,       /**< System V Semaphores */
50251875Speter    APR_LOCK_PROC_PTHREAD,  /**< POSIX pthread process-based locking */
51251875Speter    APR_LOCK_POSIXSEM,      /**< POSIX semaphore process-based locking */
52362181Sdim    APR_LOCK_DEFAULT,       /**< Use the default process lock */
53362181Sdim    APR_LOCK_DEFAULT_TIMED  /**< Use the default process timed lock */
54251875Speter} apr_lockmech_e;
55251875Speter
56251875Speter/** Opaque structure representing a process mutex. */
57251875Spetertypedef struct apr_proc_mutex_t apr_proc_mutex_t;
58251875Speter
59251875Speter/*   Function definitions */
60251875Speter
61251875Speter/**
62251875Speter * Create and initialize a mutex that can be used to synchronize processes.
63251875Speter * @param mutex the memory address where the newly created mutex will be
64251875Speter *        stored.
65251875Speter * @param fname A file name to use if the lock mechanism requires one.  This
66251875Speter *        argument should always be provided.  The lock code itself will
67251875Speter *        determine if it should be used.
68251875Speter * @param mech The mechanism to use for the interprocess lock, if any; one of
69251875Speter * <PRE>
70251875Speter *            APR_LOCK_FCNTL
71251875Speter *            APR_LOCK_FLOCK
72251875Speter *            APR_LOCK_SYSVSEM
73251875Speter *            APR_LOCK_POSIXSEM
74251875Speter *            APR_LOCK_PROC_PTHREAD
75251875Speter *            APR_LOCK_DEFAULT     pick the default mechanism for the platform
76251875Speter * </PRE>
77251875Speter * @param pool the pool from which to allocate the mutex.
78251875Speter * @see apr_lockmech_e
79251875Speter * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
80251875Speter *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
81251875Speter */
82251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
83251875Speter                                                const char *fname,
84251875Speter                                                apr_lockmech_e mech,
85251875Speter                                                apr_pool_t *pool);
86251875Speter
87251875Speter/**
88251875Speter * Re-open a mutex in a child process.
89251875Speter * @param mutex The newly re-opened mutex structure.
90251875Speter * @param fname A file name to use if the mutex mechanism requires one.  This
91251875Speter *              argument should always be provided.  The mutex code itself will
92251875Speter *              determine if it should be used.  This filename should be the
93251875Speter *              same one that was passed to apr_proc_mutex_create().
94251875Speter * @param pool The pool to operate on.
95251875Speter * @remark This function must be called to maintain portability, even
96251875Speter *         if the underlying lock mechanism does not require it.
97251875Speter */
98251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
99251875Speter                                                    const char *fname,
100251875Speter                                                    apr_pool_t *pool);
101251875Speter
102251875Speter/**
103251875Speter * Acquire the lock for the given mutex. If the mutex is already locked,
104251875Speter * the current thread will be put to sleep until the lock becomes available.
105251875Speter * @param mutex the mutex on which to acquire the lock.
106251875Speter */
107251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex);
108251875Speter
109251875Speter/**
110251875Speter * Attempt to acquire the lock for the given mutex. If the mutex has already
111251875Speter * been acquired, the call returns immediately with APR_EBUSY. Note: it
112251875Speter * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
113251875Speter * if the return value was APR_EBUSY, for portability reasons.
114251875Speter * @param mutex the mutex on which to attempt the lock acquiring.
115251875Speter */
116251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex);
117251875Speter
118251875Speter/**
119362181Sdim * Attempt to acquire the lock for the given mutex until timeout expires.
120362181Sdim * If the acquisition time outs, the call returns with APR_TIMEUP.
121362181Sdim * @param mutex the mutex on which to attempt the lock acquiring.
122362181Sdim * @param timeout the relative timeout (microseconds).
123362181Sdim * @note A negative or nul timeout means immediate attempt, returning
124362181Sdim *       APR_TIMEUP without blocking if it the lock is already acquired.
125362181Sdim */
126362181SdimAPR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex,
127362181Sdim                                               apr_interval_time_t timeout);
128362181Sdim
129362181Sdim/**
130251875Speter * Release the lock for the given mutex.
131251875Speter * @param mutex the mutex from which to release the lock.
132251875Speter */
133251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex);
134251875Speter
135251875Speter/**
136251875Speter * Destroy the mutex and free the memory associated with the lock.
137251875Speter * @param mutex the mutex to destroy.
138251875Speter */
139251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex);
140251875Speter
141251875Speter/**
142251875Speter * Destroy the mutex and free the memory associated with the lock.
143251875Speter * @param mutex the mutex to destroy.
144251875Speter * @note This function is generally used to kill a cleanup on an already
145251875Speter *       created mutex
146251875Speter */
147251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex);
148251875Speter
149251875Speter/**
150251875Speter * Return the name of the lockfile for the mutex, or NULL
151251875Speter * if the mutex doesn't use a lock file
152251875Speter */
153251875Speter
154251875SpeterAPR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex);
155251875Speter
156251875Speter/**
157362181Sdim * Get the mechanism of the mutex, as it relates to the actual method
158362181Sdim * used for the underlying apr_proc_mutex_t.
159362181Sdim * @param mutex the mutex to get the mechanism from.
160251875Speter */
161362181SdimAPR_DECLARE(apr_lockmech_e) apr_proc_mutex_mech(apr_proc_mutex_t *mutex);
162362181Sdim
163362181Sdim/**
164362181Sdim * Get the mechanism's name of the mutex, as it relates to the actual method
165362181Sdim * used for the underlying apr_proc_mutex_t.
166362181Sdim * @param mutex the mutex to get the mechanism's name from.
167362181Sdim */
168251875SpeterAPR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex);
169251875Speter
170251875Speter/**
171251875Speter * Display the name of the default mutex: APR_LOCK_DEFAULT
172251875Speter */
173251875SpeterAPR_DECLARE(const char *) apr_proc_mutex_defname(void);
174251875Speter
175251875Speter/**
176362181Sdim * Set mutex permissions.
177362181Sdim */
178362181SdimAPR_PERMS_SET_IMPLEMENT(proc_mutex);
179362181Sdim
180362181Sdim/**
181251875Speter * Get the pool used by this proc_mutex.
182251875Speter * @return apr_pool_t the pool
183251875Speter */
184251875SpeterAPR_POOL_DECLARE_ACCESSOR(proc_mutex);
185251875Speter
186251875Speter/** @} */
187251875Speter
188251875Speter#ifdef __cplusplus
189251875Speter}
190251875Speter#endif
191251875Speter
192251875Speter#endif  /* ! APR_PROC_MUTEX_H */
193