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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief APR-Util transparent any lock flavor wrapper
24 #include "apr_proc_mutex.h"
25 #include "apr_thread_mutex.h"
26 #include "apr_thread_rwlock.h"
28 /** Structure that may contain any APR lock type */
29 typedef struct apr_anylock_t {
30 /** Indicates what type of lock is in 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 */
38 /** Union of all possible APR locks */
39 union apr_anylock_u_t {
40 apr_proc_mutex_t *pm; /**< Process mutex */
42 apr_thread_mutex_t *tm; /**< Thread mutex */
43 apr_thread_rwlock_t *rw; /**< Read-write lock */
50 /** Lock an apr_anylock_t structure */
51 #define APR_ANYLOCK_LOCK(lck) \
52 (((lck)->type == apr_anylock_none) \
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) \
64 #else /* APR_HAS_THREADS */
66 #define APR_ANYLOCK_LOCK(lck) \
67 (((lck)->type == apr_anylock_none) \
69 : (((lck)->type == apr_anylock_procmutex) \
70 ? apr_proc_mutex_lock((lck)->lock.pm) \
73 #endif /* APR_HAS_THREADS */
77 /** Try to lock an apr_anylock_t structure */
78 #define APR_ANYLOCK_TRYLOCK(lck) \
79 (((lck)->type == apr_anylock_none) \
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) \
91 #else /* APR_HAS_THREADS */
93 #define APR_ANYLOCK_TRYLOCK(lck) \
94 (((lck)->type == apr_anylock_none) \
96 : (((lck)->type == apr_anylock_procmutex) \
97 ? apr_proc_mutex_trylock((lck)->lock.pm) \
100 #endif /* APR_HAS_THREADS */
104 /** Unlock an apr_anylock_t structure */
105 #define APR_ANYLOCK_UNLOCK(lck) \
106 (((lck)->type == apr_anylock_none) \
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) \
117 #else /* APR_HAS_THREADS */
119 #define APR_ANYLOCK_UNLOCK(lck) \
120 (((lck)->type == apr_anylock_none) \
122 : (((lck)->type == apr_anylock_procmutex) \
123 ? apr_proc_mutex_unlock((lck)->lock.pm) \
126 #endif /* APR_HAS_THREADS */
128 #endif /* !APR_ANYLOCK_H */