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_PROC_MUTEX_H 18251875Speter#define APR_PROC_MUTEX_H 19251875Speter 20251875Speter/** 21251875Speter * @file apr_proc_mutex.h 22251875Speter * @brief APR Process Locking Routines 23251875Speter */ 24251875Speter 25251875Speter#include "apr.h" 26251875Speter#include "apr_pools.h" 27251875Speter#include "apr_errno.h" 28251875Speter 29251875Speter#ifdef __cplusplus 30251875Speterextern "C" { 31251875Speter#endif /* __cplusplus */ 32251875Speter 33251875Speter/** 34251875Speter * @defgroup apr_proc_mutex Process Locking Routines 35251875Speter * @ingroup APR 36251875Speter * @{ 37251875Speter */ 38251875Speter 39251875Speter/** 40251875Speter * Enumerated potential types for APR process locking methods 41251875Speter * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports 42251875Speter * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable. 43251875Speter */ 44251875Spetertypedef enum { 45251875Speter APR_LOCK_FCNTL, /**< fcntl() */ 46251875Speter APR_LOCK_FLOCK, /**< flock() */ 47251875Speter APR_LOCK_SYSVSEM, /**< System V Semaphores */ 48251875Speter APR_LOCK_PROC_PTHREAD, /**< POSIX pthread process-based locking */ 49251875Speter APR_LOCK_POSIXSEM, /**< POSIX semaphore process-based locking */ 50251875Speter APR_LOCK_DEFAULT /**< Use the default process lock */ 51251875Speter} apr_lockmech_e; 52251875Speter 53251875Speter/** Opaque structure representing a process mutex. */ 54251875Spetertypedef struct apr_proc_mutex_t apr_proc_mutex_t; 55251875Speter 56251875Speter/* Function definitions */ 57251875Speter 58251875Speter/** 59251875Speter * Create and initialize a mutex that can be used to synchronize processes. 60251875Speter * @param mutex the memory address where the newly created mutex will be 61251875Speter * stored. 62251875Speter * @param fname A file name to use if the lock mechanism requires one. This 63251875Speter * argument should always be provided. The lock code itself will 64251875Speter * determine if it should be used. 65251875Speter * @param mech The mechanism to use for the interprocess lock, if any; one of 66251875Speter * <PRE> 67251875Speter * APR_LOCK_FCNTL 68251875Speter * APR_LOCK_FLOCK 69251875Speter * APR_LOCK_SYSVSEM 70251875Speter * APR_LOCK_POSIXSEM 71251875Speter * APR_LOCK_PROC_PTHREAD 72251875Speter * APR_LOCK_DEFAULT pick the default mechanism for the platform 73251875Speter * </PRE> 74251875Speter * @param pool the pool from which to allocate the mutex. 75251875Speter * @see apr_lockmech_e 76251875Speter * @warning Check APR_HAS_foo_SERIALIZE defines to see if the platform supports 77251875Speter * APR_LOCK_foo. Only APR_LOCK_DEFAULT is portable. 78251875Speter */ 79251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex, 80251875Speter const char *fname, 81251875Speter apr_lockmech_e mech, 82251875Speter apr_pool_t *pool); 83251875Speter 84251875Speter/** 85251875Speter * Re-open a mutex in a child process. 86251875Speter * @param mutex The newly re-opened mutex structure. 87251875Speter * @param fname A file name to use if the mutex mechanism requires one. This 88251875Speter * argument should always be provided. The mutex code itself will 89251875Speter * determine if it should be used. This filename should be the 90251875Speter * same one that was passed to apr_proc_mutex_create(). 91251875Speter * @param pool The pool to operate on. 92251875Speter * @remark This function must be called to maintain portability, even 93251875Speter * if the underlying lock mechanism does not require it. 94251875Speter */ 95251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex, 96251875Speter const char *fname, 97251875Speter apr_pool_t *pool); 98251875Speter 99251875Speter/** 100251875Speter * Acquire the lock for the given mutex. If the mutex is already locked, 101251875Speter * the current thread will be put to sleep until the lock becomes available. 102251875Speter * @param mutex the mutex on which to acquire the lock. 103251875Speter */ 104251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex); 105251875Speter 106251875Speter/** 107251875Speter * Attempt to acquire the lock for the given mutex. If the mutex has already 108251875Speter * been acquired, the call returns immediately with APR_EBUSY. Note: it 109251875Speter * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine 110251875Speter * if the return value was APR_EBUSY, for portability reasons. 111251875Speter * @param mutex the mutex on which to attempt the lock acquiring. 112251875Speter */ 113251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex); 114251875Speter 115251875Speter/** 116251875Speter * Release the lock for the given mutex. 117251875Speter * @param mutex the mutex from which to release the lock. 118251875Speter */ 119251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex); 120251875Speter 121251875Speter/** 122251875Speter * Destroy the mutex and free the memory associated with the lock. 123251875Speter * @param mutex the mutex to destroy. 124251875Speter */ 125251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex); 126251875Speter 127251875Speter/** 128251875Speter * Destroy the mutex and free the memory associated with the lock. 129251875Speter * @param mutex the mutex to destroy. 130251875Speter * @note This function is generally used to kill a cleanup on an already 131251875Speter * created mutex 132251875Speter */ 133251875SpeterAPR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex); 134251875Speter 135251875Speter/** 136251875Speter * Return the name of the lockfile for the mutex, or NULL 137251875Speter * if the mutex doesn't use a lock file 138251875Speter */ 139251875Speter 140251875SpeterAPR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex); 141251875Speter 142251875Speter/** 143251875Speter * Display the name of the mutex, as it relates to the actual method used. 144251875Speter * This matches the valid options for Apache's AcceptMutex directive 145251875Speter * @param mutex the name of the mutex 146251875Speter */ 147251875SpeterAPR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex); 148251875Speter 149251875Speter/** 150251875Speter * Display the name of the default mutex: APR_LOCK_DEFAULT 151251875Speter */ 152251875SpeterAPR_DECLARE(const char *) apr_proc_mutex_defname(void); 153251875Speter 154251875Speter/** 155251875Speter * Get the pool used by this proc_mutex. 156251875Speter * @return apr_pool_t the pool 157251875Speter */ 158251875SpeterAPR_POOL_DECLARE_ACCESSOR(proc_mutex); 159251875Speter 160251875Speter/** @} */ 161251875Speter 162251875Speter#ifdef __cplusplus 163251875Speter} 164251875Speter#endif 165251875Speter 166251875Speter#endif /* ! APR_PROC_MUTEX_H */ 167