Apache Portable Runtime

apr_global_mutex.h

Go to the documentation of this file.
00001 /* Licensed to the Apache Software Foundation (ASF) under one or more
00002  * contributor license agreements.  See the NOTICE file distributed with
00003  * this work for additional information regarding copyright ownership.
00004  * The ASF licenses this file to You under the Apache License, Version 2.0
00005  * (the "License"); you may not use this file except in compliance with
00006  * the License.  You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef APR_GLOBAL_MUTEX_H
00018 #define APR_GLOBAL_MUTEX_H
00019 
00020 /**
00021  * @file apr_global_mutex.h
00022  * @brief APR Global Locking Routines
00023  */
00024 
00025 #include "apr.h"
00026 #include "apr_proc_mutex.h"    /* only for apr_lockmech_e */
00027 #include "apr_pools.h"
00028 #include "apr_errno.h"
00029 #if APR_PROC_MUTEX_IS_GLOBAL
00030 #include "apr_proc_mutex.h"
00031 #endif
00032 #include "apr_time.h"
00033 
00034 #ifdef __cplusplus
00035 extern "C" {
00036 #endif /* __cplusplus */
00037 
00038 /**
00039  * @defgroup APR_GlobalMutex Global Locking Routines
00040  * @ingroup APR 
00041  * @{
00042  */
00043 
00044 #if !APR_PROC_MUTEX_IS_GLOBAL || defined(DOXYGEN)
00045 
00046 /** Opaque global mutex structure. */
00047 typedef struct apr_global_mutex_t apr_global_mutex_t;
00048 
00049 /*   Function definitions */
00050 
00051 /**
00052  * Create and initialize a mutex that can be used to synchronize both
00053  * processes and threads. Note: There is considerable overhead in using
00054  * this API if only cross-process or cross-thread mutual exclusion is
00055  * required. See apr_proc_mutex.h and apr_thread_mutex.h for more
00056  * specialized lock routines.
00057  * @param mutex the memory address where the newly created mutex will be
00058  *        stored.
00059  * @param fname A file name to use if the lock mechanism requires one.  This
00060  *        argument should always be provided.  The lock code itself will
00061  *        determine if it should be used.
00062  * @param mech The mechanism to use for the interprocess lock, if any; one of
00063  * <PRE>
00064  *            APR_LOCK_FCNTL
00065  *            APR_LOCK_FLOCK
00066  *            APR_LOCK_SYSVSEM
00067  *            APR_LOCK_POSIXSEM
00068  *            APR_LOCK_PROC_PTHREAD
00069  *            APR_LOCK_DEFAULT     pick the default mechanism for the platform
00070  *            APR_LOCK_DEFAULT_TIMED pick the default timed mechanism
00071  * </PRE>
00072  * @param pool the pool from which to allocate the mutex.
00073  * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports
00074  *          APR_LOCK_foo.  Only APR_LOCK_DEFAULT is portable.
00075  */
00076 APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex,
00077                                                   const char *fname,
00078                                                   apr_lockmech_e mech,
00079                                                   apr_pool_t *pool);
00080 
00081 /**
00082  * Re-open a mutex in a child process.
00083  * @param mutex The newly re-opened mutex structure.
00084  * @param fname A file name to use if the mutex mechanism requires one.  This
00085  *              argument should always be provided.  The mutex code itself will
00086  *              determine if it should be used.  This filename should be the 
00087  *              same one that was passed to apr_global_mutex_create().
00088  * @param pool The pool to operate on.
00089  * @remark This function must be called to maintain portability, even
00090  *         if the underlying lock mechanism does not require it.
00091  */
00092 APR_DECLARE(apr_status_t) apr_global_mutex_child_init(
00093                               apr_global_mutex_t **mutex,
00094                               const char *fname,
00095                               apr_pool_t *pool);
00096 
00097 /**
00098  * Acquire the lock for the given mutex. If the mutex is already locked,
00099  * the current thread will be put to sleep until the lock becomes available.
00100  * @param mutex the mutex on which to acquire the lock.
00101  */
00102 APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex);
00103 
00104 /**
00105  * Attempt to acquire the lock for the given mutex. If the mutex has already
00106  * been acquired, the call returns immediately with APR_EBUSY. Note: it
00107  * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
00108  * if the return value was APR_EBUSY, for portability reasons.
00109  * @param mutex the mutex on which to attempt the lock acquiring.
00110  */
00111 APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex);
00112 
00113 /**
00114  * Attempt to acquire the lock for the given mutex until timeout expires.
00115  * If the acquisition time outs, the call returns with APR_TIMEUP.
00116  * @param mutex the mutex on which to attempt the lock acquiring.
00117  * @param timeout the relative timeout (microseconds).
00118  * @note A negative or nul timeout means immediate attempt, returning
00119  *       APR_TIMEUP without blocking if it the lock is already acquired.
00120  */
00121 APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex,
00122                                                  apr_interval_time_t timeout);
00123 
00124 /**
00125  * Release the lock for the given mutex.
00126  * @param mutex the mutex from which to release the lock.
00127  */
00128 APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex);
00129 
00130 /**
00131  * Destroy the mutex and free the memory associated with the lock.
00132  * @param mutex the mutex to destroy.
00133  */
00134 APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex);
00135 
00136 /**
00137  * Return the name of the lockfile for the mutex, or NULL
00138  * if the mutex doesn't use a lock file
00139  */
00140 APR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex);
00141 
00142 /**
00143  * Get the mechanism of the mutex, as it relates to the actual method
00144  * used for the underlying apr_proc_mutex_t.
00145  * @param mutex the mutex to get the mechanism from.
00146  */
00147 APR_DECLARE(apr_lockmech_e) apr_global_mutex_mech(apr_global_mutex_t *mutex);
00148 
00149 /**
00150  * Get the mechanism's name of the mutex, as it relates to the actual method
00151  * used for the underlying apr_proc_mutex_t.
00152  * @param mutex the mutex to get the mechanism's name from.
00153  */
00154 APR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex);
00155 
00156 /**
00157  * Set mutex permissions.
00158  */
00159 APR_PERMS_SET_IMPLEMENT(global_mutex);
00160 
00161 /**
00162  * Get the pool used by this global_mutex.
00163  * @return apr_pool_t the pool
00164  */
00165 APR_POOL_DECLARE_ACCESSOR(global_mutex);
00166 
00167 #else /* APR_PROC_MUTEX_IS_GLOBAL */
00168 
00169 /* Some platforms [e.g. Win32] have cross process locks that are truly
00170  * global locks, since there isn't the concept of cross-process locks.
00171  * Define these platforms in terms of an apr_proc_mutex_t.
00172  */
00173 
00174 #define apr_global_mutex_t          apr_proc_mutex_t
00175 #define apr_global_mutex_create     apr_proc_mutex_create
00176 #define apr_global_mutex_child_init apr_proc_mutex_child_init
00177 #define apr_global_mutex_lock       apr_proc_mutex_lock
00178 #define apr_global_mutex_trylock    apr_proc_mutex_trylock
00179 #define apr_global_mutex_unlock     apr_proc_mutex_unlock
00180 #define apr_global_mutex_destroy    apr_proc_mutex_destroy
00181 #define apr_global_mutex_lockfile   apr_proc_mutex_lockfile
00182 #define apr_global_mutex_mech       apr_proc_mutex_mech
00183 #define apr_global_mutex_name       apr_proc_mutex_name
00184 #define apr_global_mutex_perms_set  apr_proc_mutex_perms_set
00185 #define apr_global_mutex_pool_get   apr_proc_mutex_pool_get
00186 
00187 #endif
00188 
00189 /** @} */
00190 
00191 #ifdef __cplusplus
00192 }
00193 #endif
00194 
00195 #endif  /* ndef APR_GLOBAL_MUTEX_H */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines