Blame include/apr_anylock.h

Packit 383869
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 383869
 * contributor license agreements.  See the NOTICE file distributed with
Packit 383869
 * this work for additional information regarding copyright ownership.
Packit 383869
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 383869
 * (the "License"); you may not use this file except in compliance with
Packit 383869
 * the License.  You may obtain a copy of the License at
Packit 383869
 *
Packit 383869
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 383869
 *
Packit 383869
 * Unless required by applicable law or agreed to in writing, software
Packit 383869
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 383869
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 383869
 * See the License for the specific language governing permissions and
Packit 383869
 * limitations under the License.
Packit 383869
 */
Packit 383869
Packit 383869
/**
Packit 383869
 * @file apr_anylock.h
Packit 383869
 * @brief APR-Util transparent any lock flavor wrapper
Packit 383869
 */
Packit 383869
#ifndef APR_ANYLOCK_H
Packit 383869
#define APR_ANYLOCK_H
Packit 383869
Packit 383869
#include "apr_proc_mutex.h"
Packit 383869
#include "apr_thread_mutex.h"
Packit 383869
#include "apr_thread_rwlock.h"
Packit 383869
Packit 383869
/** Structure that may contain any APR lock type */
Packit 383869
typedef struct apr_anylock_t {
Packit 383869
    /** Indicates what type of lock is in lock */
Packit 383869
    enum tm_lock {
Packit 383869
        apr_anylock_none,           /**< None */
Packit 383869
        apr_anylock_procmutex,      /**< Process-based */
Packit 383869
        apr_anylock_threadmutex,    /**< Thread-based */
Packit 383869
        apr_anylock_readlock,       /**< Read lock */
Packit 383869
        apr_anylock_writelock       /**< Write lock */
Packit 383869
    } type;
Packit 383869
    /** Union of all possible APR locks */
Packit 383869
    union apr_anylock_u_t {
Packit 383869
        apr_proc_mutex_t *pm;       /**< Process mutex */
Packit 383869
#if APR_HAS_THREADS
Packit 383869
        apr_thread_mutex_t *tm;     /**< Thread mutex */
Packit 383869
        apr_thread_rwlock_t *rw;    /**< Read-write lock */
Packit 383869
#endif
Packit 383869
    } lock;
Packit 383869
} apr_anylock_t;
Packit 383869
Packit 383869
#if APR_HAS_THREADS
Packit 383869
Packit 383869
/** Lock an apr_anylock_t structure */
Packit 383869
#define APR_ANYLOCK_LOCK(lck)                \
Packit 383869
    (((lck)->type == apr_anylock_none)         \
Packit 383869
      ? APR_SUCCESS                              \
Packit 383869
      : (((lck)->type == apr_anylock_threadmutex)  \
Packit 383869
          ? apr_thread_mutex_lock((lck)->lock.tm)    \
Packit 383869
          : (((lck)->type == apr_anylock_procmutex)    \
Packit 383869
              ? apr_proc_mutex_lock((lck)->lock.pm)      \
Packit 383869
              : (((lck)->type == apr_anylock_readlock)     \
Packit 383869
                  ? apr_thread_rwlock_rdlock((lck)->lock.rw) \
Packit 383869
                  : (((lck)->type == apr_anylock_writelock)    \
Packit 383869
                      ? apr_thread_rwlock_wrlock((lck)->lock.rw) \
Packit 383869
                      : APR_EINVAL)))))
Packit 383869
Packit 383869
#else /* APR_HAS_THREADS */
Packit 383869
Packit 383869
#define APR_ANYLOCK_LOCK(lck)                \
Packit 383869
    (((lck)->type == apr_anylock_none)         \
Packit 383869
      ? APR_SUCCESS                              \
Packit 383869
          : (((lck)->type == apr_anylock_procmutex)    \
Packit 383869
              ? apr_proc_mutex_lock((lck)->lock.pm)      \
Packit 383869
                      : APR_EINVAL))
Packit 383869
Packit 383869
#endif /* APR_HAS_THREADS */
Packit 383869
Packit 383869
#if APR_HAS_THREADS
Packit 383869
Packit 383869
/** Try to lock an apr_anylock_t structure */
Packit 383869
#define APR_ANYLOCK_TRYLOCK(lck)                \
Packit 383869
    (((lck)->type == apr_anylock_none)            \
Packit 383869
      ? APR_SUCCESS                                 \
Packit 383869
      : (((lck)->type == apr_anylock_threadmutex)     \
Packit 383869
          ? apr_thread_mutex_trylock((lck)->lock.tm)    \
Packit 383869
          : (((lck)->type == apr_anylock_procmutex)       \
Packit 383869
              ? apr_proc_mutex_trylock((lck)->lock.pm)      \
Packit 383869
              : (((lck)->type == apr_anylock_readlock)        \
Packit 383869
                  ? apr_thread_rwlock_tryrdlock((lck)->lock.rw) \
Packit 383869
                  : (((lck)->type == apr_anylock_writelock)       \
Packit 383869
                      ? apr_thread_rwlock_trywrlock((lck)->lock.rw) \
Packit 383869
                          : APR_EINVAL)))))
Packit 383869
Packit 383869
#else /* APR_HAS_THREADS */
Packit 383869
Packit 383869
#define APR_ANYLOCK_TRYLOCK(lck)                \
Packit 383869
    (((lck)->type == apr_anylock_none)            \
Packit 383869
      ? APR_SUCCESS                                 \
Packit 383869
          : (((lck)->type == apr_anylock_procmutex)       \
Packit 383869
              ? apr_proc_mutex_trylock((lck)->lock.pm)      \
Packit 383869
                          : APR_EINVAL))
Packit 383869
Packit 383869
#endif /* APR_HAS_THREADS */
Packit 383869
Packit 383869
#if APR_HAS_THREADS
Packit 383869
Packit 383869
/** Unlock an apr_anylock_t structure */
Packit 383869
#define APR_ANYLOCK_UNLOCK(lck)              \
Packit 383869
    (((lck)->type == apr_anylock_none)         \
Packit 383869
      ? APR_SUCCESS                              \
Packit 383869
      : (((lck)->type == apr_anylock_threadmutex)  \
Packit 383869
          ? apr_thread_mutex_unlock((lck)->lock.tm)  \
Packit 383869
          : (((lck)->type == apr_anylock_procmutex)    \
Packit 383869
              ? apr_proc_mutex_unlock((lck)->lock.pm)    \
Packit 383869
              : ((((lck)->type == apr_anylock_readlock) || \
Packit 383869
                  ((lck)->type == apr_anylock_writelock))    \
Packit 383869
                  ? apr_thread_rwlock_unlock((lck)->lock.rw)   \
Packit 383869
                      : APR_EINVAL))))
Packit 383869
Packit 383869
#else /* APR_HAS_THREADS */
Packit 383869
Packit 383869
#define APR_ANYLOCK_UNLOCK(lck)              \
Packit 383869
    (((lck)->type == apr_anylock_none)         \
Packit 383869
      ? APR_SUCCESS                              \
Packit 383869
          : (((lck)->type == apr_anylock_procmutex)    \
Packit 383869
              ? apr_proc_mutex_unlock((lck)->lock.pm)    \
Packit 383869
                      : APR_EINVAL))
Packit 383869
Packit 383869
#endif /* APR_HAS_THREADS */
Packit 383869
Packit 383869
#endif /* !APR_ANYLOCK_H */