2
0

fspr_thread_rwlock.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef APR_THREAD_RWLOCK_H
  17. #define APR_THREAD_RWLOCK_H
  18. /**
  19. * @file fspr_thread_rwlock.h
  20. * @brief APR Reader/Writer Lock Routines
  21. */
  22. #include "fspr.h"
  23. #include "fspr_pools.h"
  24. #include "fspr_errno.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif /* __cplusplus */
  28. #if APR_HAS_THREADS
  29. /**
  30. * @defgroup fspr_thread_rwlock Reader/Writer Lock Routines
  31. * @ingroup APR
  32. * @{
  33. */
  34. /** Opaque read-write thread-safe lock. */
  35. typedef struct fspr_thread_rwlock_t fspr_thread_rwlock_t;
  36. /**
  37. * Note: The following operations have undefined results: unlocking a
  38. * read-write lock which is not locked in the calling thread; write
  39. * locking a read-write lock which is already locked by the calling
  40. * thread; destroying a read-write lock more than once; clearing or
  41. * destroying the pool from which a <b>locked</b> read-write lock is
  42. * allocated.
  43. */
  44. /**
  45. * Create and initialize a read-write lock that can be used to synchronize
  46. * threads.
  47. * @param rwlock the memory address where the newly created readwrite lock
  48. * will be stored.
  49. * @param pool the pool from which to allocate the mutex.
  50. */
  51. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_create(fspr_thread_rwlock_t **rwlock,
  52. fspr_pool_t *pool);
  53. /**
  54. * Acquire a shared-read lock on the given read-write lock. This will allow
  55. * multiple threads to enter the same critical section while they have acquired
  56. * the read lock.
  57. * @param rwlock the read-write lock on which to acquire the shared read.
  58. */
  59. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_rdlock(fspr_thread_rwlock_t *rwlock);
  60. /**
  61. * Attempt to acquire the shared-read lock on the given read-write lock. This
  62. * is the same as fspr_thread_rwlock_rdlock(), only that the function fails
  63. * if there is another thread holding the write lock, or if there are any
  64. * write threads blocking on the lock. If the function fails for this case,
  65. * APR_EBUSY will be returned. Note: it is important that the
  66. * APR_STATUS_IS_EBUSY(s) macro be used to determine if the return value was
  67. * APR_EBUSY, for portability reasons.
  68. * @param rwlock the rwlock on which to attempt the shared read.
  69. */
  70. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_tryrdlock(fspr_thread_rwlock_t *rwlock);
  71. /**
  72. * Acquire an exclusive-write lock on the given read-write lock. This will
  73. * allow only one single thread to enter the critical sections. If there
  74. * are any threads currently holding the read-lock, this thread is put to
  75. * sleep until it can have exclusive access to the lock.
  76. * @param rwlock the read-write lock on which to acquire the exclusive write.
  77. */
  78. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_wrlock(fspr_thread_rwlock_t *rwlock);
  79. /**
  80. * Attempt to acquire the exclusive-write lock on the given read-write lock.
  81. * This is the same as fspr_thread_rwlock_wrlock(), only that the function fails
  82. * if there is any other thread holding the lock (for reading or writing),
  83. * in which case the function will return APR_EBUSY. Note: it is important
  84. * that the APR_STATUS_IS_EBUSY(s) macro be used to determine if the return
  85. * value was APR_EBUSY, for portability reasons.
  86. * @param rwlock the rwlock on which to attempt the exclusive write.
  87. */
  88. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_trywrlock(fspr_thread_rwlock_t *rwlock);
  89. /**
  90. * Release either the read or write lock currently held by the calling thread
  91. * associated with the given read-write lock.
  92. * @param rwlock the read-write lock to be released (unlocked).
  93. */
  94. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_unlock(fspr_thread_rwlock_t *rwlock);
  95. /**
  96. * Destroy the read-write lock and free the associated memory.
  97. * @param rwlock the rwlock to destroy.
  98. */
  99. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_destroy(fspr_thread_rwlock_t *rwlock);
  100. /**
  101. * Get the pool used by this thread_rwlock.
  102. * @return fspr_pool_t the pool
  103. */
  104. APR_POOL_DECLARE_ACCESSOR(thread_rwlock);
  105. #endif /* APR_HAS_THREADS */
  106. /** @} */
  107. #ifdef __cplusplus
  108. }
  109. #endif
  110. #endif /* ! APR_THREAD_RWLOCK_H */