1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef APR_GLOBAL_MUTEX_H 18 #define APR_GLOBAL_MUTEX_H 19 20 /** 21 * @file apr_global_mutex.h 22 * @brief APR Global Locking Routines 23 */ 24 25 #include "apr.h" 26 #include "apr_proc_mutex.h" /* only for apr_lockmech_e */ 27 #include "apr_pools.h" 28 #include "apr_errno.h" 29 #if APR_PROC_MUTEX_IS_GLOBAL 30 #include "apr_proc_mutex.h" 31 #endif 32 #include "apr_time.h" 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif /* __cplusplus */ 37 38 /** 39 * @defgroup APR_GlobalMutex Global Locking Routines 40 * @ingroup APR 41 * @{ 42 */ 43 44 #if !APR_PROC_MUTEX_IS_GLOBAL || defined(DOXYGEN) 45 46 /** Opaque global mutex structure. */ 47 typedef struct apr_global_mutex_t apr_global_mutex_t; 48 49 /* Function definitions */ 50 51 /** 52 * Create and initialize a mutex that can be used to synchronize both 53 * processes and threads. Note: There is considerable overhead in using 54 * this API if only cross-process or cross-thread mutual exclusion is 55 * required. See apr_proc_mutex.h and apr_thread_mutex.h for more 56 * specialized lock routines. 57 * @param mutex the memory address where the newly created mutex will be 58 * stored. 59 * @param fname A file name to use if the lock mechanism requires one. This 60 * argument should always be provided. The lock code itself will 61 * determine if it should be used. 62 * @param mech The mechanism to use for the interprocess lock, if any; one of 63 * <PRE> 64 * APR_LOCK_FCNTL 65 * APR_LOCK_FLOCK 66 * APR_LOCK_SYSVSEM 67 * APR_LOCK_POSIXSEM 68 * APR_LOCK_PROC_PTHREAD 69 * APR_LOCK_DEFAULT pick the default mechanism for the platform 70 * APR_LOCK_DEFAULT_TIMED pick the default timed mechanism 71 * </PRE> 72 * @param pool the pool from which to allocate the mutex. 73 * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports 74 * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable. 75 */ 76 APR_DECLARE(apr_status_t) apr_global_mutex_create(apr_global_mutex_t **mutex, 77 const char *fname, 78 apr_lockmech_e mech, 79 apr_pool_t *pool); 80 81 /** 82 * Re-open a mutex in a child process. 83 * @param mutex The newly re-opened mutex structure. 84 * @param fname A file name to use if the mutex mechanism requires one. This 85 * argument should always be provided. The mutex code itself will 86 * determine if it should be used. This filename should be the 87 * same one that was passed to apr_global_mutex_create(). 88 * @param pool The pool to operate on. 89 * @remark This function must be called to maintain portability, even 90 * if the underlying lock mechanism does not require it. 91 */ 92 APR_DECLARE(apr_status_t) apr_global_mutex_child_init( 93 apr_global_mutex_t **mutex, 94 const char *fname, 95 apr_pool_t *pool); 96 97 /** 98 * Acquire the lock for the given mutex. If the mutex is already locked, 99 * the current thread will be put to sleep until the lock becomes available. 100 * @param mutex the mutex on which to acquire the lock. 101 */ 102 APR_DECLARE(apr_status_t) apr_global_mutex_lock(apr_global_mutex_t *mutex); 103 104 /** 105 * Attempt to acquire the lock for the given mutex. If the mutex has already 106 * been acquired, the call returns immediately with APR_EBUSY. Note: it 107 * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine 108 * if the return value was APR_EBUSY, for portability reasons. 109 * @param mutex the mutex on which to attempt the lock acquiring. 110 */ 111 APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex); 112 113 /** 114 * Attempt to acquire the lock for the given mutex until timeout expires. 115 * If the acquisition time outs, the call returns with APR_TIMEUP. 116 * @param mutex the mutex on which to attempt the lock acquiring. 117 * @param timeout the relative timeout (microseconds). 118 * @note A negative or nul timeout means immediate attempt, returning 119 * APR_TIMEUP without blocking if it the lock is already acquired. 120 */ 121 APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex, 122 apr_interval_time_t timeout); 123 124 /** 125 * Release the lock for the given mutex. 126 * @param mutex the mutex from which to release the lock. 127 */ 128 APR_DECLARE(apr_status_t) apr_global_mutex_unlock(apr_global_mutex_t *mutex); 129 130 /** 131 * Destroy the mutex and free the memory associated with the lock. 132 * @param mutex the mutex to destroy. 133 */ 134 APR_DECLARE(apr_status_t) apr_global_mutex_destroy(apr_global_mutex_t *mutex); 135 136 /** 137 * Return the name of the lockfile for the mutex, or NULL 138 * if the mutex doesn't use a lock file 139 */ 140 APR_DECLARE(const char *) apr_global_mutex_lockfile(apr_global_mutex_t *mutex); 141 142 /** 143 * Get the mechanism of the mutex, as it relates to the actual method 144 * used for the underlying apr_proc_mutex_t. 145 * @param mutex the mutex to get the mechanism from. 146 */ 147 APR_DECLARE(apr_lockmech_e) apr_global_mutex_mech(apr_global_mutex_t *mutex); 148 149 /** 150 * Get the mechanism's name of the mutex, as it relates to the actual method 151 * used for the underlying apr_proc_mutex_t. 152 * @param mutex the mutex to get the mechanism's name from. 153 */ 154 APR_DECLARE(const char *) apr_global_mutex_name(apr_global_mutex_t *mutex); 155 156 /** 157 * Set mutex permissions. 158 */ 159 APR_PERMS_SET_IMPLEMENT(global_mutex); 160 161 /** 162 * Get the pool used by this global_mutex. 163 * @return apr_pool_t the pool 164 */ 165 APR_POOL_DECLARE_ACCESSOR(global_mutex); 166 167 #else /* APR_PROC_MUTEX_IS_GLOBAL */ 168 169 /* Some platforms [e.g. Win32] have cross process locks that are truly 170 * global locks, since there isn't the concept of cross-process locks. 171 * Define these platforms in terms of an apr_proc_mutex_t. 172 */ 173 174 #define apr_global_mutex_t apr_proc_mutex_t 175 #define apr_global_mutex_create apr_proc_mutex_create 176 #define apr_global_mutex_child_init apr_proc_mutex_child_init 177 #define apr_global_mutex_lock apr_proc_mutex_lock 178 #define apr_global_mutex_trylock apr_proc_mutex_trylock 179 #define apr_global_mutex_unlock apr_proc_mutex_unlock 180 #define apr_global_mutex_destroy apr_proc_mutex_destroy 181 #define apr_global_mutex_lockfile apr_proc_mutex_lockfile 182 #define apr_global_mutex_mech apr_proc_mutex_mech 183 #define apr_global_mutex_name apr_proc_mutex_name 184 #define apr_global_mutex_perms_set apr_proc_mutex_perms_set 185 #define apr_global_mutex_pool_get apr_proc_mutex_pool_get 186 187 #endif 188 189 /** @} */ 190 191 #ifdef __cplusplus 192 } 193 #endif 194 195 #endif /* ndef APR_GLOBAL_MUTEX_H */ 196