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_GLOBAL_MUTEX_H
18251875Speter#define APR_GLOBAL_MUTEX_H
19251875Speter
20251875Speter/**
21251875Speter * @file apr_global_mutex.h
22251875Speter * @brief APR Global Locking Routines
23251875Speter */
24251875Speter
25251875Speter#include "apr.h"
26251875Speter#include "apr_proc_mutex.h"    /* only for apr_lockmech_e */
27251875Speter#include "apr_pools.h"
28251875Speter#include "apr_errno.h"
29251875Speter#if APR_PROC_MUTEX_IS_GLOBAL
30251875Speter#include "apr_proc_mutex.h"
31251875Speter#endif
32251875Speter
33251875Speter#ifdef __cplusplus
34251875Speterextern "C" {
35251875Speter#endif /* __cplusplus */
36251875Speter
37251875Speter/**
38251875Speter * @defgroup APR_GlobalMutex Global Locking Routines
39251875Speter * @ingroup APR
40251875Speter * @{
41251875Speter */
42251875Speter
43251875Speter#if !APR_PROC_MUTEX_IS_GLOBAL || defined(DOXYGEN)
44251875Speter
45251875Speter/** Opaque global mutex structure. */
46251875Spetertypedef struct apr_global_mutex_t apr_global_mutex_t;
47251875Speter
48251875Speter/*   Function definitions */
49251875Speter
50251875Speter/**
51251875Speter * Create and initialize a mutex that can be used to synchronize both
52251875Speter * processes and threads. Note: There is considerable overhead in using
53251875Speter * this API if only cross-process or cross-thread mutual exclusion is
54251875Speter * required. See apr_proc_mutex.h and apr_thread_mutex.h for more
55251875Speter * specialized lock routines.
56251875Speter * @param mutex the memory address where the newly created mutex will be
57251875Speter *        stored.
58251875Speter * @param fname A file name to use if the lock mechanism requires one.  This
59251875Speter *        argument should always be provided.  The lock code itself will
60251875Speter *        determine if it should be used.
61251875Speter * @param mech The mechanism to use for the interprocess lock, if any; one of
62251875Speter * <PRE>
63251875Speter *            APR_LOCK_FCNTL
64251875Speter *            APR_LOCK_FLOCK
65251875Speter *            APR_LOCK_SYSVSEM
66251875Speter *            APR_LOCK_POSIXSEM
67251875Speter *            APR_LOCK_PROC_PTHREAD
68251875Speter *            APR_LOCK_DEFAULT     pick the default mechanism for the platform
69251875Speter * </PRE>
70251875Speter * @param pool the pool from which to allocate the mutex.
71251875Speter * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
72251875Speter *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
73251875Speter */
74251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
75251875Speter                                                  const char *fname,
76251875Speter                                                  apr_lockmech_e mech,
77251875Speter                                                  apr_pool_t *pool);
78251875Speter
79251875Speter/**
80251875Speter * Re-open a mutex in a child process.
81251875Speter * @param mutex The newly re-opened mutex structure.
82251875Speter * @param fname A file name to use if the mutex mechanism requires one.  This
83251875Speter *              argument should always be provided.  The mutex code itself will
84251875Speter *              determine if it should be used.  This filename should be the
85251875Speter *              same one that was passed to apr_global_mutex_create().
86251875Speter * @param pool The pool to operate on.
87251875Speter * @remark This function must be called to maintain portability, even
88251875Speter *         if the underlying lock mechanism does not require it.
89251875Speter */
90251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_child_init(
91251875Speter                              apr_global_mutex_t **mutex,
92251875Speter                              const char *fname,
93251875Speter                              apr_pool_t *pool);
94251875Speter
95251875Speter/**
96251875Speter * Acquire the lock for the given mutex. If the mutex is already locked,
97251875Speter * the current thread will be put to sleep until the lock becomes available.
98251875Speter * @param mutex the mutex on which to acquire the lock.
99251875Speter */
100251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex);
101251875Speter
102251875Speter/**
103251875Speter * Attempt to acquire the lock for the given mutex. If the mutex has already
104251875Speter * been acquired, the call returns immediately with APR_EBUSY. Note: it
105251875Speter * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
106251875Speter * if the return value was APR_EBUSY, for portability reasons.
107251875Speter * @param mutex the mutex on which to attempt the lock acquiring.
108251875Speter */
109251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex);
110251875Speter
111251875Speter/**
112251875Speter * Release the lock for the given mutex.
113251875Speter * @param mutex the mutex from which to release the lock.
114251875Speter */
115251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex);
116251875Speter
117251875Speter/**
118251875Speter * Destroy the mutex and free the memory associated with the lock.
119251875Speter * @param mutex the mutex to destroy.
120251875Speter */
121251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex);
122251875Speter
123251875Speter/**
124251875Speter * Return the name of the lockfile for the mutex, or NULL
125251875Speter * if the mutex doesn't use a lock file
126251875Speter */
127251875SpeterAPR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex);
128251875Speter
129251875Speter/**
130251875Speter * Display the name of the mutex, as it relates to the actual method used
131251875Speter * for the underlying apr_proc_mutex_t, if any.  NULL is returned if
132251875Speter * there is no underlying apr_proc_mutex_t.
133251875Speter * @param mutex the name of the mutex
134251875Speter */
135251875SpeterAPR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex);
136251875Speter
137251875Speter/**
138251875Speter * Get the pool used by this global_mutex.
139251875Speter * @return apr_pool_t the pool
140251875Speter */
141251875SpeterAPR_POOL_DECLARE_ACCESSOR(global_mutex);
142251875Speter
143251875Speter#else /* APR_PROC_MUTEX_IS_GLOBAL */
144251875Speter
145251875Speter/* Some platforms [e.g. Win32] have cross process locks that are truly
146251875Speter * global locks, since there isn't the concept of cross-process locks.
147251875Speter * Define these platforms in terms of an apr_proc_mutex_t.
148251875Speter */
149251875Speter
150251875Speter#define apr_global_mutex_t          apr_proc_mutex_t
151251875Speter#define apr_global_mutex_create     apr_proc_mutex_create
152251875Speter#define apr_global_mutex_child_init apr_proc_mutex_child_init
153251875Speter#define apr_global_mutex_lock       apr_proc_mutex_lock
154251875Speter#define apr_global_mutex_trylock    apr_proc_mutex_trylock
155251875Speter#define apr_global_mutex_unlock     apr_proc_mutex_unlock
156251875Speter#define apr_global_mutex_destroy    apr_proc_mutex_destroy
157251875Speter#define apr_global_mutex_lockfile   apr_proc_mutex_lockfile
158251875Speter#define apr_global_mutex_name       apr_proc_mutex_name
159251875Speter#define apr_global_mutex_pool_get   apr_proc_mutex_pool_get
160251875Speter
161251875Speter#endif
162251875Speter
163251875Speter/** @} */
164251875Speter
165251875Speter#ifdef __cplusplus
166251875Speter}
167251875Speter#endif
168251875Speter
169251875Speter#endif  /* ndef APR_GLOBAL_MUTEX_H */
170