Apache Portable Runtime
apr_anylock.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 /**
00018  * @file apr_anylock.h
00019  * @brief APR-Util transparent any lock flavor wrapper
00020  */
00021 #ifndef APR_ANYLOCK_H
00022 #define APR_ANYLOCK_H
00023 
00024 #include "apr_proc_mutex.h"
00025 #include "apr_thread_mutex.h"
00026 #include "apr_thread_rwlock.h"
00027 
00028 /** Structure that may contain any APR lock type */
00029 typedef struct apr_anylock_t {
00030     /** Indicates what type of lock is in lock */
00031     enum tm_lock {
00032         apr_anylock_none,           /**< None */
00033         apr_anylock_procmutex,      /**< Process-based */
00034         apr_anylock_threadmutex,    /**< Thread-based */
00035         apr_anylock_readlock,       /**< Read lock */
00036         apr_anylock_writelock       /**< Write lock */
00037     } type;
00038     /** Union of all possible APR locks */
00039     union apr_anylock_u_t {
00040         apr_proc_mutex_t *pm;       /**< Process mutex */
00041 #if APR_HAS_THREADS
00042         apr_thread_mutex_t *tm;     /**< Thread mutex */
00043         apr_thread_rwlock_t *rw;    /**< Read-write lock */
00044 #endif
00045     } lock;
00046 } apr_anylock_t;
00047 
00048 #if APR_HAS_THREADS
00049 
00050 /** Lock an apr_anylock_t structure */
00051 #define APR_ANYLOCK_LOCK(lck)                \
00052     (((lck)->type == apr_anylock_none)         \
00053       ? APR_SUCCESS                              \
00054       : (((lck)->type == apr_anylock_threadmutex)  \
00055           ? apr_thread_mutex_lock((lck)->lock.tm)    \
00056           : (((lck)->type == apr_anylock_procmutex)    \
00057               ? apr_proc_mutex_lock((lck)->lock.pm)      \
00058               : (((lck)->type == apr_anylock_readlock)     \
00059                   ? apr_thread_rwlock_rdlock((lck)->lock.rw) \
00060                   : (((lck)->type == apr_anylock_writelock)    \
00061                       ? apr_thread_rwlock_wrlock((lck)->lock.rw) \
00062                       : APR_EINVAL)))))
00063 
00064 #else /* APR_HAS_THREADS */
00065 
00066 #define APR_ANYLOCK_LOCK(lck)                \
00067     (((lck)->type == apr_anylock_none)         \
00068       ? APR_SUCCESS                              \
00069           : (((lck)->type == apr_anylock_procmutex)    \
00070               ? apr_proc_mutex_lock((lck)->lock.pm)      \
00071                       : APR_EINVAL))
00072 
00073 #endif /* APR_HAS_THREADS */
00074 
00075 #if APR_HAS_THREADS
00076 
00077 /** Try to lock an apr_anylock_t structure */
00078 #define APR_ANYLOCK_TRYLOCK(lck)                \
00079     (((lck)->type == apr_anylock_none)            \
00080       ? APR_SUCCESS                                 \
00081       : (((lck)->type == apr_anylock_threadmutex)     \
00082           ? apr_thread_mutex_trylock((lck)->lock.tm)    \
00083           : (((lck)->type == apr_anylock_procmutex)       \
00084               ? apr_proc_mutex_trylock((lck)->lock.pm)      \
00085               : (((lck)->type == apr_anylock_readlock)        \
00086                   ? apr_thread_rwlock_tryrdlock((lck)->lock.rw) \
00087                   : (((lck)->type == apr_anylock_writelock)       \
00088                       ? apr_thread_rwlock_trywrlock((lck)->lock.rw) \
00089                           : APR_EINVAL)))))
00090 
00091 #else /* APR_HAS_THREADS */
00092 
00093 #define APR_ANYLOCK_TRYLOCK(lck)                \
00094     (((lck)->type == apr_anylock_none)            \
00095       ? APR_SUCCESS                                 \
00096           : (((lck)->type == apr_anylock_procmutex)       \
00097               ? apr_proc_mutex_trylock((lck)->lock.pm)      \
00098                           : APR_EINVAL))
00099 
00100 #endif /* APR_HAS_THREADS */
00101 
00102 #if APR_HAS_THREADS
00103 
00104 /** Unlock an apr_anylock_t structure */
00105 #define APR_ANYLOCK_UNLOCK(lck)              \
00106     (((lck)->type == apr_anylock_none)         \
00107       ? APR_SUCCESS                              \
00108       : (((lck)->type == apr_anylock_threadmutex)  \
00109           ? apr_thread_mutex_unlock((lck)->lock.tm)  \
00110           : (((lck)->type == apr_anylock_procmutex)    \
00111               ? apr_proc_mutex_unlock((lck)->lock.pm)    \
00112               : ((((lck)->type == apr_anylock_readlock) || \
00113                   ((lck)->type == apr_anylock_writelock))    \
00114                   ? apr_thread_rwlock_unlock((lck)->lock.rw)   \
00115                       : APR_EINVAL))))
00116 
00117 #else /* APR_HAS_THREADS */
00118 
00119 #define APR_ANYLOCK_UNLOCK(lck)              \
00120     (((lck)->type == apr_anylock_none)         \
00121       ? APR_SUCCESS                              \
00122           : (((lck)->type == apr_anylock_procmutex)    \
00123               ? apr_proc_mutex_unlock((lck)->lock.pm)    \
00124                       : APR_EINVAL))
00125 
00126 #endif /* APR_HAS_THREADS */
00127 
00128 #endif /* !APR_ANYLOCK_H */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines