apr_global_mutex.h revision 251886
1265038Sganbold/* Licensed to the Apache Software Foundation (ASF) under one or more
2265038Sganbold * contributor license agreements.  See the NOTICE file distributed with
3265038Sganbold * this work for additional information regarding copyright ownership.
4265038Sganbold * The ASF licenses this file to You under the Apache License, Version 2.0
5265038Sganbold * (the "License"); you may not use this file except in compliance with
6265038Sganbold * the License.  You may obtain a copy of the License at
7265038Sganbold *
8265038Sganbold *     http://www.apache.org/licenses/LICENSE-2.0
9265038Sganbold *
10265038Sganbold * Unless required by applicable law or agreed to in writing, software
11265038Sganbold * distributed under the License is distributed on an "AS IS" BASIS,
12265038Sganbold * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13265038Sganbold * See the License for the specific language governing permissions and
14265038Sganbold * limitations under the License.
15265038Sganbold */
16265038Sganbold
17265038Sganbold#ifndef APR_GLOBAL_MUTEX_H
18265038Sganbold#define APR_GLOBAL_MUTEX_H
19265038Sganbold
20265038Sganbold/**
21265038Sganbold * @file apr_global_mutex.h
22265038Sganbold * @brief APR Global Locking Routines
23265038Sganbold */
24265038Sganbold
25265038Sganbold#include "apr.h"
26265038Sganbold#include "apr_proc_mutex.h"    /* only for apr_lockmech_e */
27265038Sganbold#include "apr_pools.h"
28265038Sganbold#include "apr_errno.h"
29265038Sganbold#if APR_PROC_MUTEX_IS_GLOBAL
30265038Sganbold#include "apr_proc_mutex.h"
31265038Sganbold#endif
32265038Sganbold
33265038Sganbold#ifdef __cplusplus
34265038Sganboldextern "C" {
35265038Sganbold#endif /* __cplusplus */
36265038Sganbold
37265038Sganbold/**
38265038Sganbold * @defgroup APR_GlobalMutex Global Locking Routines
39265038Sganbold * @ingroup APR
40265038Sganbold * @{
41265038Sganbold */
42265038Sganbold
43265038Sganbold#if !APR_PROC_MUTEX_IS_GLOBAL || defined(DOXYGEN)
44265038Sganbold
45265038Sganbold/** Opaque global mutex structure. */
46265038Sganboldtypedef struct apr_global_mutex_t apr_global_mutex_t;
47265038Sganbold
48265038Sganbold/*   Function definitions */
49265038Sganbold
50265038Sganbold/**
51265038Sganbold * Create and initialize a mutex that can be used to synchronize both
52265038Sganbold * processes and threads. Note: There is considerable overhead in using
53265038Sganbold * this API if only cross-process or cross-thread mutual exclusion is
54265038Sganbold * required. See apr_proc_mutex.h and apr_thread_mutex.h for more
55265038Sganbold * specialized lock routines.
56265038Sganbold * @param mutex the memory address where the newly created mutex will be
57265038Sganbold *        stored.
58265038Sganbold * @param fname A file name to use if the lock mechanism requires one.  This
59265038Sganbold *        argument should always be provided.  The lock code itself will
60265038Sganbold *        determine if it should be used.
61265038Sganbold * @param mech The mechanism to use for the interprocess lock, if any; one of
62265038Sganbold * <PRE>
63265038Sganbold *            APR_LOCK_FCNTL
64265038Sganbold *            APR_LOCK_FLOCK
65265038Sganbold *            APR_LOCK_SYSVSEM
66265038Sganbold *            APR_LOCK_POSIXSEM
67265038Sganbold *            APR_LOCK_PROC_PTHREAD
68265038Sganbold *            APR_LOCK_DEFAULT     pick the default mechanism for the platform
69265038Sganbold * </PRE>
70265038Sganbold * @param pool the pool from which to allocate the mutex.
71265038Sganbold * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
72265038Sganbold *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
73265038Sganbold */
74265038SganboldAPR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
75265038Sganbold                                                  const char *fname,
76265038Sganbold                                                  apr_lockmech_e mech,
77265038Sganbold                                                  apr_pool_t *pool);
78265038Sganbold
79265038Sganbold/**
80265038Sganbold * Re-open a mutex in a child process.
81265038Sganbold * @param mutex The newly re-opened mutex structure.
82265038Sganbold * @param fname A file name to use if the mutex mechanism requires one.  This
83265038Sganbold *              argument should always be provided.  The mutex code itself will
84265038Sganbold *              determine if it should be used.  This filename should be the
85265038Sganbold *              same one that was passed to apr_global_mutex_create().
86265038Sganbold * @param pool The pool to operate on.
87265038Sganbold * @remark This function must be called to maintain portability, even
88265038Sganbold *         if the underlying lock mechanism does not require it.
89265038Sganbold */
90265038SganboldAPR_DECLARE(apr_status_t) apr_global_mutex_child_init(
91265038Sganbold                              apr_global_mutex_t **mutex,
92265038Sganbold                              const char *fname,
93265038Sganbold                              apr_pool_t *pool);
94265038Sganbold
95265038Sganbold/**
96265038Sganbold * Acquire the lock for the given mutex. If the mutex is already locked,
97265038Sganbold * the current thread will be put to sleep until the lock becomes available.
98265038Sganbold * @param mutex the mutex on which to acquire the lock.
99265038Sganbold */
100265038SganboldAPR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex);
101265038Sganbold
102265038Sganbold/**
103265038Sganbold * Attempt to acquire the lock for the given mutex. If the mutex has already
104265038Sganbold * been acquired, the call returns immediately with APR_EBUSY. Note: it
105265038Sganbold * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
106265038Sganbold * if the return value was APR_EBUSY, for portability reasons.
107265038Sganbold * @param mutex the mutex on which to attempt the lock acquiring.
108265038Sganbold */
109265038SganboldAPR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex);
110265038Sganbold
111265038Sganbold/**
112265038Sganbold * Release the lock for the given mutex.
113265038Sganbold * @param mutex the mutex from which to release the lock.
114265038Sganbold */
115265038SganboldAPR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex);
116265038Sganbold
117265038Sganbold/**
118265038Sganbold * Destroy the mutex and free the memory associated with the lock.
119265038Sganbold * @param mutex the mutex to destroy.
120265038Sganbold */
121265038SganboldAPR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex);
122265038Sganbold
123265038Sganbold/**
124265038Sganbold * Return the name of the lockfile for the mutex, or NULL
125265038Sganbold * if the mutex doesn't use a lock file
126265038Sganbold */
127265038SganboldAPR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex);
128265038Sganbold
129265038Sganbold/**
130265038Sganbold * Display the name of the mutex, as it relates to the actual method used
131265038Sganbold * for the underlying apr_proc_mutex_t, if any.  NULL is returned if
132265038Sganbold * there is no underlying apr_proc_mutex_t.
133265038Sganbold * @param mutex the name of the mutex
134265038Sganbold */
135265038SganboldAPR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex);
136265038Sganbold
137265038Sganbold/**
138265038Sganbold * Get the pool used by this global_mutex.
139265038Sganbold * @return apr_pool_t the pool
140 */
141APR_POOL_DECLARE_ACCESSOR(global_mutex);
142
143#else /* APR_PROC_MUTEX_IS_GLOBAL */
144
145/* Some platforms [e.g. Win32] have cross process locks that are truly
146 * global locks, since there isn't the concept of cross-process locks.
147 * Define these platforms in terms of an apr_proc_mutex_t.
148 */
149
150#define apr_global_mutex_t          apr_proc_mutex_t
151#define apr_global_mutex_create     apr_proc_mutex_create
152#define apr_global_mutex_child_init apr_proc_mutex_child_init
153#define apr_global_mutex_lock       apr_proc_mutex_lock
154#define apr_global_mutex_trylock    apr_proc_mutex_trylock
155#define apr_global_mutex_unlock     apr_proc_mutex_unlock
156#define apr_global_mutex_destroy    apr_proc_mutex_destroy
157#define apr_global_mutex_lockfile   apr_proc_mutex_lockfile
158#define apr_global_mutex_name       apr_proc_mutex_name
159#define apr_global_mutex_pool_get   apr_proc_mutex_pool_get
160
161#endif
162
163/** @} */
164
165#ifdef __cplusplus
166}
167#endif
168
169#endif  /* ndef APR_GLOBAL_MUTEX_H */
170