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 32362181Sdim#include "apr_time.h" 33251875Speter 34251875Speter#ifdef __cplusplus 35251875Speterextern "C" { 36251875Speter#endif /* __cplusplus */ 37251875Speter 38251875Speter/** 39251875Speter * @defgroup APR_GlobalMutex Global Locking Routines 40251875Speter * @ingroup APR 41251875Speter * @{ 42251875Speter */ 43251875Speter 44251875Speter#if !APR_PROC_MUTEX_IS_GLOBAL || defined(DOXYGEN) 45251875Speter 46251875Speter/** Opaque global mutex structure. */ 47251875Spetertypedef struct apr_global_mutex_t apr_global_mutex_t; 48251875Speter 49251875Speter/* Function definitions */ 50251875Speter 51251875Speter/** 52251875Speter * Create and initialize a mutex that can be used to synchronize both 53251875Speter * processes and threads. Note: There is considerable overhead in using 54251875Speter * this API if only cross-process or cross-thread mutual exclusion is 55251875Speter * required. See apr_proc_mutex.h and apr_thread_mutex.h for more 56251875Speter * specialized lock routines. 57251875Speter * @param mutex the memory address where the newly created mutex will be 58251875Speter * stored. 59251875Speter * @param fname A file name to use if the lock mechanism requires one. This 60251875Speter * argument should always be provided. The lock code itself will 61251875Speter * determine if it should be used. 62251875Speter * @param mech The mechanism to use for the interprocess lock, if any; one of 63251875Speter * <PRE> 64251875Speter * APR_LOCK_FCNTL 65251875Speter * APR_LOCK_FLOCK 66251875Speter * APR_LOCK_SYSVSEM 67251875Speter * APR_LOCK_POSIXSEM 68251875Speter * APR_LOCK_PROC_PTHREAD 69251875Speter * APR_LOCK_DEFAULT pick the default mechanism for the platform 70362181Sdim * APR_LOCK_DEFAULT_TIMED pick the default timed mechanism 71251875Speter * </PRE> 72251875Speter * @param pool the pool from which to allocate the mutex. 73251875Speter * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports 74251875Speter * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable. 75251875Speter */ 76251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex, 77251875Speter const char *fname, 78251875Speter apr_lockmech_e mech, 79251875Speter apr_pool_t *pool); 80251875Speter 81251875Speter/** 82251875Speter * Re-open a mutex in a child process. 83251875Speter * @param mutex The newly re-opened mutex structure. 84251875Speter * @param fname A file name to use if the mutex mechanism requires one. This 85251875Speter * argument should always be provided. The mutex code itself will 86251875Speter * determine if it should be used. This filename should be the 87251875Speter * same one that was passed to apr_global_mutex_create(). 88251875Speter * @param pool The pool to operate on. 89251875Speter * @remark This function must be called to maintain portability, even 90251875Speter * if the underlying lock mechanism does not require it. 91251875Speter */ 92251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_child_init( 93251875Speter apr_global_mutex_t **mutex, 94251875Speter const char *fname, 95251875Speter apr_pool_t *pool); 96251875Speter 97251875Speter/** 98251875Speter * Acquire the lock for the given mutex. If the mutex is already locked, 99251875Speter * the current thread will be put to sleep until the lock becomes available. 100251875Speter * @param mutex the mutex on which to acquire the lock. 101251875Speter */ 102251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex); 103251875Speter 104251875Speter/** 105251875Speter * Attempt to acquire the lock for the given mutex. If the mutex has already 106251875Speter * been acquired, the call returns immediately with APR_EBUSY. Note: it 107251875Speter * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine 108251875Speter * if the return value was APR_EBUSY, for portability reasons. 109251875Speter * @param mutex the mutex on which to attempt the lock acquiring. 110251875Speter */ 111251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex); 112251875Speter 113251875Speter/** 114362181Sdim * Attempt to acquire the lock for the given mutex until timeout expires. 115362181Sdim * If the acquisition time outs, the call returns with APR_TIMEUP. 116362181Sdim * @param mutex the mutex on which to attempt the lock acquiring. 117362181Sdim * @param timeout the relative timeout (microseconds). 118362181Sdim * @note A negative or nul timeout means immediate attempt, returning 119362181Sdim * APR_TIMEUP without blocking if it the lock is already acquired. 120362181Sdim */ 121362181SdimAPR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex, 122362181Sdim apr_interval_time_t timeout); 123362181Sdim 124362181Sdim/** 125251875Speter * Release the lock for the given mutex. 126251875Speter * @param mutex the mutex from which to release the lock. 127251875Speter */ 128251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex); 129251875Speter 130251875Speter/** 131251875Speter * Destroy the mutex and free the memory associated with the lock. 132251875Speter * @param mutex the mutex to destroy. 133251875Speter */ 134251875SpeterAPR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex); 135251875Speter 136251875Speter/** 137251875Speter * Return the name of the lockfile for the mutex, or NULL 138251875Speter * if the mutex doesn't use a lock file 139251875Speter */ 140251875SpeterAPR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex); 141251875Speter 142251875Speter/** 143362181Sdim * Get the mechanism of the mutex, as it relates to the actual method 144362181Sdim * used for the underlying apr_proc_mutex_t. 145362181Sdim * @param mutex the mutex to get the mechanism from. 146251875Speter */ 147362181SdimAPR_DECLARE(apr_lockmech_e) apr_global_mutex_mech(apr_global_mutex_t *mutex); 148362181Sdim 149362181Sdim/** 150362181Sdim * Get the mechanism's name of the mutex, as it relates to the actual method 151362181Sdim * used for the underlying apr_proc_mutex_t. 152362181Sdim * @param mutex the mutex to get the mechanism's name from. 153362181Sdim */ 154251875SpeterAPR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex); 155251875Speter 156251875Speter/** 157362181Sdim * Set mutex permissions. 158362181Sdim */ 159362181SdimAPR_PERMS_SET_IMPLEMENT(global_mutex); 160362181Sdim 161362181Sdim/** 162251875Speter * Get the pool used by this global_mutex. 163251875Speter * @return apr_pool_t the pool 164251875Speter */ 165251875SpeterAPR_POOL_DECLARE_ACCESSOR(global_mutex); 166251875Speter 167251875Speter#else /* APR_PROC_MUTEX_IS_GLOBAL */ 168251875Speter 169251875Speter/* Some platforms [e.g. Win32] have cross process locks that are truly 170251875Speter * global locks, since there isn't the concept of cross-process locks. 171251875Speter * Define these platforms in terms of an apr_proc_mutex_t. 172251875Speter */ 173251875Speter 174251875Speter#define apr_global_mutex_t apr_proc_mutex_t 175251875Speter#define apr_global_mutex_create apr_proc_mutex_create 176251875Speter#define apr_global_mutex_child_init apr_proc_mutex_child_init 177251875Speter#define apr_global_mutex_lock apr_proc_mutex_lock 178251875Speter#define apr_global_mutex_trylock apr_proc_mutex_trylock 179251875Speter#define apr_global_mutex_unlock apr_proc_mutex_unlock 180251875Speter#define apr_global_mutex_destroy apr_proc_mutex_destroy 181251875Speter#define apr_global_mutex_lockfile apr_proc_mutex_lockfile 182362181Sdim#define apr_global_mutex_mech apr_proc_mutex_mech 183251875Speter#define apr_global_mutex_name apr_proc_mutex_name 184362181Sdim#define apr_global_mutex_perms_set apr_proc_mutex_perms_set 185251875Speter#define apr_global_mutex_pool_get apr_proc_mutex_pool_get 186251875Speter 187251875Speter#endif 188251875Speter 189251875Speter/** @} */ 190251875Speter 191251875Speter#ifdef __cplusplus 192251875Speter} 193251875Speter#endif 194251875Speter 195251875Speter#endif /* ndef APR_GLOBAL_MUTEX_H */ 196