apr_anylock.h 5.1 KB

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