apr_proc_mutex.h revision 251875
1187767Sluigi/* Licensed to the Apache Software Foundation (ASF) under one or more
2187767Sluigi * contributor license agreements.  See the NOTICE file distributed with
3187767Sluigi * this work for additional information regarding copyright ownership.
4187767Sluigi * The ASF licenses this file to You under the Apache License, Version 2.0
5187767Sluigi * (the "License"); you may not use this file except in compliance with
6187767Sluigi * the License.  You may obtain a copy of the License at
7187767Sluigi *
8187767Sluigi *     http://www.apache.org/licenses/LICENSE-2.0
9187767Sluigi *
10187767Sluigi * Unless required by applicable law or agreed to in writing, software
11187767Sluigi * distributed under the License is distributed on an "AS IS" BASIS,
12187767Sluigi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13187767Sluigi * See the License for the specific language governing permissions and
14187767Sluigi * limitations under the License.
15187767Sluigi */
16187767Sluigi
17187767Sluigi#ifndef APR_PROC_MUTEX_H
18187767Sluigi#define APR_PROC_MUTEX_H
19187767Sluigi
20187767Sluigi/**
21187767Sluigi * @file apr_proc_mutex.h
22187767Sluigi * @brief APR Process Locking Routines
23187767Sluigi */
24187767Sluigi
25187767Sluigi#include "apr.h"
26187767Sluigi#include "apr_pools.h"
27187767Sluigi#include "apr_errno.h"
28187767Sluigi
29187767Sluigi#ifdef __cplusplus
30187767Sluigiextern "C" {
31187767Sluigi#endif /* __cplusplus */
32187767Sluigi
33187767Sluigi/**
34187767Sluigi * @defgroup apr_proc_mutex Process Locking Routines
35187767Sluigi * @ingroup APR
36187767Sluigi * @{
37187767Sluigi */
38204591Sluigi
39187767Sluigi/**
40187767Sluigi * Enumerated potential types for APR process locking methods
41187767Sluigi * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
42187767Sluigi *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
43187767Sluigi */
44187767Sluigitypedef enum {
45187767Sluigi    APR_LOCK_FCNTL,         /**< fcntl() */
46187767Sluigi    APR_LOCK_FLOCK,         /**< flock() */
47187767Sluigi    APR_LOCK_SYSVSEM,       /**< System V Semaphores */
48187767Sluigi    APR_LOCK_PROC_PTHREAD,  /**< POSIX pthread process-based locking */
49187767Sluigi    APR_LOCK_POSIXSEM,      /**< POSIX semaphore process-based locking */
50187767Sluigi    APR_LOCK_DEFAULT        /**< Use the default process lock */
51187767Sluigi} apr_lockmech_e;
52187767Sluigi
53187767Sluigi/** Opaque structure representing a process mutex. */
54187767Sluigitypedef struct apr_proc_mutex_t apr_proc_mutex_t;
55187767Sluigi
56187767Sluigi/*   Function definitions */
57187767Sluigi
58187767Sluigi/**
59187767Sluigi * Create and initialize a mutex that can be used to synchronize processes.
60187767Sluigi * @param mutex the memory address where the newly created mutex will be
61187767Sluigi *        stored.
62187767Sluigi * @param fname A file name to use if the lock mechanism requires one.  This
63187767Sluigi *        argument should always be provided.  The lock code itself will
64187767Sluigi *        determine if it should be used.
65187767Sluigi * @param mech The mechanism to use for the interprocess lock, if any; one of
66187767Sluigi * <PRE>
67187767Sluigi *            APR_LOCK_FCNTL
68187767Sluigi *            APR_LOCK_FLOCK
69187767Sluigi *            APR_LOCK_SYSVSEM
70187767Sluigi *            APR_LOCK_POSIXSEM
71187767Sluigi *            APR_LOCK_PROC_PTHREAD
72187767Sluigi *            APR_LOCK_DEFAULT     pick the default mechanism for the platform
73187767Sluigi * </PRE>
74187769Sluigi * @param pool the pool from which to allocate the mutex.
75187769Sluigi * @see apr_lockmech_e
76187769Sluigi * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
77187769Sluigi *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
78187769Sluigi */
79187769SluigiAPR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
80187769Sluigi                                                const char *fname,
81187769Sluigi                                                apr_lockmech_e mech,
82187769Sluigi                                                apr_pool_t *pool);
83187769Sluigi
84187769Sluigi/**
85204591Sluigi * Re-open a mutex in a child process.
86187769Sluigi * @param mutex The newly re-opened mutex structure.
87204591Sluigi * @param fname A file name to use if the mutex mechanism requires one.  This
88204591Sluigi *              argument should always be provided.  The mutex code itself will
89187769Sluigi *              determine if it should be used.  This filename should be the
90187769Sluigi *              same one that was passed to apr_proc_mutex_create().
91187769Sluigi * @param pool The pool to operate on.
92187769Sluigi * @remark This function must be called to maintain portability, even
93187769Sluigi *         if the underlying lock mechanism does not require it.
94187769Sluigi */
95187769SluigiAPR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
96187769Sluigi                                                    const char *fname,
97187769Sluigi                                                    apr_pool_t *pool);
98187769Sluigi
99187769Sluigi/**
100187769Sluigi * Acquire the lock for the given mutex. If the mutex is already locked,
101190633Spiso * the current thread will be put to sleep until the lock becomes available.
102223666Sae * @param mutex the mutex on which to acquire the lock.
103223666Sae */
104187769SluigiAPR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex);
105187769Sluigi
106187769Sluigi/**
107187769Sluigi * Attempt to acquire the lock for the given mutex. If the mutex has already
108187769Sluigi * been acquired, the call returns immediately with APR_EBUSY. Note: it
109187769Sluigi * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
110187769Sluigi * if the return value was APR_EBUSY, for portability reasons.
111187769Sluigi * @param mutex the mutex on which to attempt the lock acquiring.
112187769Sluigi */
113187769SluigiAPR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex);
114187769Sluigi
115187769Sluigi/**
116187769Sluigi * Release the lock for the given mutex.
117187769Sluigi * @param mutex the mutex from which to release the lock.
118187769Sluigi */
119187769SluigiAPR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex);
120187769Sluigi
121187769Sluigi/**
122187769Sluigi * Destroy the mutex and free the memory associated with the lock.
123187769Sluigi * @param mutex the mutex to destroy.
124187769Sluigi */
125187769SluigiAPR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex);
126187769Sluigi
127187769Sluigi/**
128187769Sluigi * Destroy the mutex and free the memory associated with the lock.
129187769Sluigi * @param mutex the mutex to destroy.
130205169Sluigi * @note This function is generally used to kill a cleanup on an already
131187769Sluigi *       created mutex
132187769Sluigi */
133187769SluigiAPR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex);
134187769Sluigi
135187769Sluigi/**
136187769Sluigi * Return the name of the lockfile for the mutex, or NULL
137187769Sluigi * if the mutex doesn't use a lock file
138187769Sluigi */
139187769Sluigi
140187769SluigiAPR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex);
141187769Sluigi
142187769Sluigi/**
143187769Sluigi * Display the name of the mutex, as it relates to the actual method used.
144187769Sluigi * This matches the valid options for Apache's AcceptMutex directive
145187769Sluigi * @param mutex the name of the mutex
146187769Sluigi */
147187769SluigiAPR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex);
148187769Sluigi
149187769Sluigi/**
150187769Sluigi * Display the name of the default mutex: APR_LOCK_DEFAULT
151187769Sluigi */
152187769SluigiAPR_DECLARE(const char *) apr_proc_mutex_defname(void);
153187769Sluigi
154187769Sluigi/**
155187769Sluigi * Get the pool used by this proc_mutex.
156187769Sluigi * @return apr_pool_t the pool
157187769Sluigi */
158187769SluigiAPR_POOL_DECLARE_ACCESSOR(proc_mutex);
159187769Sluigi
160204591Sluigi/** @} */
161204591Sluigi
162187769Sluigi#ifdef __cplusplus
163187769Sluigi}
164204591Sluigi#endif
165194930Soleg
166187769Sluigi#endif  /* ! APR_PROC_MUTEX_H */
167187769Sluigi