apr_anylock.h revision 256281
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/** 18 * @file apr_anylock.h 19 * @brief APR-Util transparent any lock flavor wrapper 20 */ 21#ifndef APR_ANYLOCK_H 22#define APR_ANYLOCK_H 23 24#include "apr_proc_mutex.h" 25#include "apr_thread_mutex.h" 26#include "apr_thread_rwlock.h" 27 28/** Structure that may contain any APR lock type */ 29typedef struct apr_anylock_t { 30 /** Indicates what type of lock is in lock */ 31 enum tm_lock { 32 apr_anylock_none, /**< None */ 33 apr_anylock_procmutex, /**< Process-based */ 34 apr_anylock_threadmutex, /**< Thread-based */ 35 apr_anylock_readlock, /**< Read lock */ 36 apr_anylock_writelock /**< Write lock */ 37 } type; 38 /** Union of all possible APR locks */ 39 union apr_anylock_u_t { 40 apr_proc_mutex_t *pm; /**< Process mutex */ 41#if APR_HAS_THREADS 42 apr_thread_mutex_t *tm; /**< Thread mutex */ 43 apr_thread_rwlock_t *rw; /**< Read-write lock */ 44#endif 45 } lock; 46} apr_anylock_t; 47 48#if APR_HAS_THREADS 49 50/** Lock an apr_anylock_t structure */ 51#define APR_ANYLOCK_LOCK(lck) \ 52 (((lck)->type == apr_anylock_none) \ 53 ? APR_SUCCESS \ 54 : (((lck)->type == apr_anylock_threadmutex) \ 55 ? apr_thread_mutex_lock((lck)->lock.tm) \ 56 : (((lck)->type == apr_anylock_procmutex) \ 57 ? apr_proc_mutex_lock((lck)->lock.pm) \ 58 : (((lck)->type == apr_anylock_readlock) \ 59 ? apr_thread_rwlock_rdlock((lck)->lock.rw) \ 60 : (((lck)->type == apr_anylock_writelock) \ 61 ? apr_thread_rwlock_wrlock((lck)->lock.rw) \ 62 : APR_EINVAL))))) 63 64#else /* APR_HAS_THREADS */ 65 66#define APR_ANYLOCK_LOCK(lck) \ 67 (((lck)->type == apr_anylock_none) \ 68 ? APR_SUCCESS \ 69 : (((lck)->type == apr_anylock_procmutex) \ 70 ? apr_proc_mutex_lock((lck)->lock.pm) \ 71 : APR_EINVAL)) 72 73#endif /* APR_HAS_THREADS */ 74 75#if APR_HAS_THREADS 76 77/** Try to lock an apr_anylock_t structure */ 78#define APR_ANYLOCK_TRYLOCK(lck) \ 79 (((lck)->type == apr_anylock_none) \ 80 ? APR_SUCCESS \ 81 : (((lck)->type == apr_anylock_threadmutex) \ 82 ? apr_thread_mutex_trylock((lck)->lock.tm) \ 83 : (((lck)->type == apr_anylock_procmutex) \ 84 ? apr_proc_mutex_trylock((lck)->lock.pm) \ 85 : (((lck)->type == apr_anylock_readlock) \ 86 ? apr_thread_rwlock_tryrdlock((lck)->lock.rw) \ 87 : (((lck)->type == apr_anylock_writelock) \ 88 ? apr_thread_rwlock_trywrlock((lck)->lock.rw) \ 89 : APR_EINVAL))))) 90 91#else /* APR_HAS_THREADS */ 92 93#define APR_ANYLOCK_TRYLOCK(lck) \ 94 (((lck)->type == apr_anylock_none) \ 95 ? APR_SUCCESS \ 96 : (((lck)->type == apr_anylock_procmutex) \ 97 ? apr_proc_mutex_trylock((lck)->lock.pm) \ 98 : APR_EINVAL)) 99 100#endif /* APR_HAS_THREADS */ 101 102#if APR_HAS_THREADS 103 104/** Unlock an apr_anylock_t structure */ 105#define APR_ANYLOCK_UNLOCK(lck) \ 106 (((lck)->type == apr_anylock_none) \ 107 ? APR_SUCCESS \ 108 : (((lck)->type == apr_anylock_threadmutex) \ 109 ? apr_thread_mutex_unlock((lck)->lock.tm) \ 110 : (((lck)->type == apr_anylock_procmutex) \ 111 ? apr_proc_mutex_unlock((lck)->lock.pm) \ 112 : ((((lck)->type == apr_anylock_readlock) || \ 113 ((lck)->type == apr_anylock_writelock)) \ 114 ? apr_thread_rwlock_unlock((lck)->lock.rw) \ 115 : APR_EINVAL)))) 116 117#else /* APR_HAS_THREADS */ 118 119#define APR_ANYLOCK_UNLOCK(lck) \ 120 (((lck)->type == apr_anylock_none) \ 121 ? APR_SUCCESS \ 122 : (((lck)->type == apr_anylock_procmutex) \ 123 ? apr_proc_mutex_unlock((lck)->lock.pm) \ 124 : APR_EINVAL)) 125 126#endif /* APR_HAS_THREADS */ 127 128#endif /* !APR_ANYLOCK_H */ 129