2
0

apr_thread_mutex.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_MUTEX_H
  17. #define APR_THREAD_MUTEX_H
  18. /**
  19. * @file apr_thread_mutex.h
  20. * @brief APR Thread Mutex Routines
  21. */
  22. #include "apr.h"
  23. #include "apr_errno.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif /* __cplusplus */
  27. #if APR_HAS_THREADS || defined(DOXYGEN)
  28. /**
  29. * @defgroup apr_thread_mutex Thread Mutex Routines
  30. * @ingroup APR
  31. * @{
  32. */
  33. /** Opaque thread-local mutex structure */
  34. typedef struct apr_thread_mutex_t apr_thread_mutex_t;
  35. #define APR_THREAD_MUTEX_DEFAULT 0x0 /**< platform-optimal lock behavior */
  36. #define APR_THREAD_MUTEX_NESTED 0x1 /**< enable nested (recursive) locks */
  37. #define APR_THREAD_MUTEX_UNNESTED 0x2 /**< disable nested locks */
  38. /* Delayed the include to avoid a circular reference */
  39. #include "apr_pools.h"
  40. /**
  41. * Create and initialize a mutex that can be used to synchronize threads.
  42. * @param mutex the memory address where the newly created mutex will be
  43. * stored.
  44. * @param flags Or'ed value of:
  45. * <PRE>
  46. * APR_THREAD_MUTEX_DEFAULT platform-optimal lock behavior.
  47. * APR_THREAD_MUTEX_NESTED enable nested (recursive) locks.
  48. * APR_THREAD_MUTEX_UNNESTED disable nested locks (non-recursive).
  49. * </PRE>
  50. * @param pool the pool from which to allocate the mutex.
  51. * @warning Be cautious in using APR_THREAD_MUTEX_DEFAULT. While this is the
  52. * most optimial mutex based on a given platform's performance charateristics,
  53. * it will behave as either a nested or an unnested lock.
  54. */
  55. APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
  56. unsigned int flags,
  57. apr_pool_t *pool);
  58. /**
  59. * Acquire the lock for the given mutex. If the mutex is already locked,
  60. * the current thread will be put to sleep until the lock becomes available.
  61. * @param mutex the mutex on which to acquire the lock.
  62. */
  63. APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex);
  64. /**
  65. * Attempt to acquire the lock for the given mutex. If the mutex has already
  66. * been acquired, the call returns immediately with APR_EBUSY. Note: it
  67. * is important that the APR_STATUS_IS_EBUSY(s) macro be used to determine
  68. * if the return value was APR_EBUSY, for portability reasons.
  69. * @param mutex the mutex on which to attempt the lock acquiring.
  70. */
  71. APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex);
  72. /**
  73. * Release the lock for the given mutex.
  74. * @param mutex the mutex from which to release the lock.
  75. */
  76. APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex);
  77. /**
  78. * Destroy the mutex and free the memory associated with the lock.
  79. * @param mutex the mutex to destroy.
  80. */
  81. APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex);
  82. /**
  83. * Get the pool used by this thread_mutex.
  84. * @return apr_pool_t the pool
  85. */
  86. APR_POOL_DECLARE_ACCESSOR(thread_mutex);
  87. #endif /* APR_HAS_THREADS */
  88. /** @} */
  89. #ifdef __cplusplus
  90. }
  91. #endif
  92. #endif /* ! APR_THREAD_MUTEX_H */