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_PROC_MUTEX_H 00018 #define APR_PROC_MUTEX_H 00019 00020 /** 00021 * @file apr_proc_mutex.h 00022 * @brief APR Process Locking Routines 00023 */ 00024 00025 #include "apr.h" 00026 #include "apr_pools.h" 00027 #include "apr_errno.h" 00028 #include "apr_perms_set.h" 00029 #include "apr_time.h" 00030 00031 #ifdef __cplusplus 00032 extern "C" { 00033 #endif /* __cplusplus */ 00034 00035 /** 00036 * @defgroup apr_proc_mutex Process Locking Routines 00037 * @ingroup APR 00038 * @{ 00039 */ 00040 00041 /** 00042 * Enumerated potential types for APR process locking methods 00043 * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports 00044 * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable. 00045 */ 00046 typedef enum { 00047 APR_LOCK_FCNTL, /**< fcntl() */ 00048 APR_LOCK_FLOCK, /**< flock() */ 00049 APR_LOCK_SYSVSEM, /**< System V Semaphores */ 00050 APR_LOCK_PROC_PTHREAD, /**< POSIX pthread process-based locking */ 00051 APR_LOCK_POSIXSEM, /**< POSIX semaphore process-based locking */ 00052 APR_LOCK_DEFAULT, /**< Use the default process lock */ 00053 APR_LOCK_DEFAULT_TIMED /**< Use the default process timed lock */ 00054 } apr_lockmech_e; 00055 00056 /** Opaque structure representing a process mutex. */ 00057 typedef struct apr_proc_mutex_t apr_proc_mutex_t; 00058 00059 /* Function definitions */ 00060 00061 /** 00062 * Create and initialize a mutex that can be used to synchronize processes. 00063 * @param mutex the memory address where the newly created mutex will be 00064 * stored. 00065 * @param fname A file name to use if the lock mechanism requires one. This 00066 * argument should always be provided. The lock code itself will 00067 * determine if it should be used. 00068 * @param mech The mechanism to use for the interprocess lock, if any; one of 00069 * <PRE> 00070 * APR_LOCK_FCNTL 00071 * APR_LOCK_FLOCK 00072 * APR_LOCK_SYSVSEM 00073 * APR_LOCK_POSIXSEM 00074 * APR_LOCK_PROC_PTHREAD 00075 * APR_LOCK_DEFAULT pick the default mechanism for the platform 00076 * </PRE> 00077 * @param pool the pool from which to allocate the mutex. 00078 * @see apr_lockmech_e 00079 * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports 00080 * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable. 00081 */ 00082 APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex, 00083 const char *fname, 00084 apr_lockmech_e mech, 00085 apr_pool_t *pool); 00086 00087 /** 00088 * Re-open a mutex in a child process. 00089 * @param mutex The newly re-opened mutex structure. 00090 * @param fname A file name to use if the mutex mechanism requires one. This 00091 * argument should always be provided. The mutex code itself will 00092 * determine if it should be used. This filename should be the 00093 * same one that was passed to apr_proc_mutex_create(). 00094 * @param pool The pool to operate on. 00095 * @remark This function must be called to maintain portability, even 00096 * if the underlying lock mechanism does not require it. 00097 */ 00098 APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex, 00099 const char *fname, 00100 apr_pool_t *pool); 00101 00102 /** 00103 * Acquire the lock for the given mutex. If the mutex is already locked, 00104 * the current thread will be put to sleep until the lock becomes available. 00105 * @param mutex the mutex on which to acquire the lock. 00106 */ 00107 APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex); 00108 00109 /** 00110 * Attempt to acquire the lock for the given mutex. If the mutex has already 00111 * been acquired, the call returns immediately with APR_EBUSY. Note: it 00112 * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine 00113 * if the return value was APR_EBUSY, for portability reasons. 00114 * @param mutex the mutex on which to attempt the lock acquiring. 00115 */ 00116 APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex); 00117 00118 /** 00119 * Attempt to acquire the lock for the given mutex until timeout expires. 00120 * If the acquisition time outs, the call returns with APR_TIMEUP. 00121 * @param mutex the mutex on which to attempt the lock acquiring. 00122 * @param timeout the relative timeout (microseconds). 00123 * @note A negative or nul timeout means immediate attempt, returning 00124 * APR_TIMEUP without blocking if it the lock is already acquired. 00125 */ 00126 APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, 00127 apr_interval_time_t timeout); 00128 00129 /** 00130 * Release the lock for the given mutex. 00131 * @param mutex the mutex from which to release the lock. 00132 */ 00133 APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex); 00134 00135 /** 00136 * Destroy the mutex and free the memory associated with the lock. 00137 * @param mutex the mutex to destroy. 00138 */ 00139 APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex); 00140 00141 /** 00142 * Destroy the mutex and free the memory associated with the lock. 00143 * @param mutex the mutex to destroy. 00144 * @note This function is generally used to kill a cleanup on an already 00145 * created mutex 00146 */ 00147 APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex); 00148 00149 /** 00150 * Return the name of the lockfile for the mutex, or NULL 00151 * if the mutex doesn't use a lock file 00152 */ 00153 00154 APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex); 00155 00156 /** 00157 * Get the mechanism of the mutex, as it relates to the actual method 00158 * used for the underlying apr_proc_mutex_t. 00159 * @param mutex the mutex to get the mechanism from. 00160 */ 00161 APR_DECLARE(apr_lockmech_e) apr_proc_mutex_mech(apr_proc_mutex_t *mutex); 00162 00163 /** 00164 * Get the mechanism's name of the mutex, as it relates to the actual method 00165 * used for the underlying apr_proc_mutex_t. 00166 * @param mutex the mutex to get the mechanism's name from. 00167 */ 00168 APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex); 00169 00170 /** 00171 * Display the name of the default mutex: APR_LOCK_DEFAULT 00172 */ 00173 APR_DECLARE(const char *) apr_proc_mutex_defname(void); 00174 00175 /** 00176 * Set mutex permissions. 00177 */ 00178 APR_PERMS_SET_IMPLEMENT(proc_mutex); 00179 00180 /** 00181 * Get the pool used by this proc_mutex. 00182 * @return apr_pool_t the pool 00183 */ 00184 APR_POOL_DECLARE_ACCESSOR(proc_mutex); 00185 00186 /** @} */ 00187 00188 #ifdef __cplusplus 00189 } 00190 #endif 00191 00192 #endif /* ! APR_PROC_MUTEX_H */
1.5.6