apr_reslist.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_RESLIST_H
  17. #define APR_RESLIST_H
  18. /**
  19. * @file apr_reslist.h
  20. * @brief APR-UTIL Resource List Routines
  21. */
  22. #include "apr.h"
  23. #include "apu.h"
  24. #include "apr_pools.h"
  25. #include "apr_errno.h"
  26. #include "apr_time.h"
  27. /**
  28. * @defgroup APR_Util_RL Resource List Routines
  29. * @ingroup APR_Util
  30. * @{
  31. */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif /* __cplusplus */
  35. /** Opaque resource list object */
  36. typedef struct apr_reslist_t apr_reslist_t;
  37. /* Generic constructor called by resource list when it needs to create a
  38. * resource.
  39. * @param resource opaque resource
  40. * @param params flags
  41. * @param pool Pool
  42. */
  43. typedef apr_status_t (*apr_reslist_constructor)(void **resource, void *params,
  44. apr_pool_t *pool);
  45. /* Generic destructor called by resource list when it needs to destroy a
  46. * resource.
  47. * @param resource opaque resource
  48. * @param params flags
  49. * @param pool Pool
  50. */
  51. typedef apr_status_t (*apr_reslist_destructor)(void *resource, void *params,
  52. apr_pool_t *pool);
  53. /* Cleanup order modes */
  54. #define APR_RESLIST_CLEANUP_DEFAULT 0 /**< default pool cleanup */
  55. #define APR_RESLIST_CLEANUP_FIRST 1 /**< use pool pre cleanup */
  56. /**
  57. * Create a new resource list with the following parameters:
  58. * @param reslist An address where the pointer to the new resource
  59. * list will be stored.
  60. * @param min Allowed minimum number of available resources. Zero
  61. * creates new resources only when needed.
  62. * @param smax Resources will be destroyed during reslist maintenance to
  63. * meet this maximum restriction as they expire (reach their ttl).
  64. * @param hmax Absolute maximum limit on the number of total resources.
  65. * @param ttl If non-zero, sets the maximum amount of time in microseconds an
  66. * unused resource is valid. Any resource which has exceeded this
  67. * time will be destroyed, either when encountered by
  68. * apr_reslist_acquire() or during reslist maintenance.
  69. * @param con Constructor routine that is called to create a new resource.
  70. * @param de Destructor routine that is called to destroy an expired resource.
  71. * @param params Passed to constructor and deconstructor
  72. * @param pool The pool from which to create this resource list. Also the
  73. * same pool that is passed to the constructor and destructor
  74. * routines.
  75. * @remark If APR has been compiled without thread support, hmax will be
  76. * automatically set to 1 and values of min and smax will be forced to
  77. * 1 for any non-zero value.
  78. */
  79. APU_DECLARE(apr_status_t) apr_reslist_create(apr_reslist_t **reslist,
  80. int min, int smax, int hmax,
  81. apr_interval_time_t ttl,
  82. apr_reslist_constructor con,
  83. apr_reslist_destructor de,
  84. void *params,
  85. apr_pool_t *pool);
  86. /**
  87. * Destroy the given resource list and all resources controlled by
  88. * this list.
  89. * FIXME: Should this block until all resources become available,
  90. * or maybe just destroy all the free ones, or maybe destroy
  91. * them even though they might be in use by something else?
  92. * Currently it will abort if there are resources that haven't
  93. * been released, so there is an assumption that all resources
  94. * have been released to the list before calling this function.
  95. * @param reslist The reslist to destroy
  96. */
  97. APU_DECLARE(apr_status_t) apr_reslist_destroy(apr_reslist_t *reslist);
  98. /**
  99. * Retrieve a resource from the list, creating a new one if necessary.
  100. * If we have met our maximum number of resources, we will block
  101. * until one becomes available.
  102. * @param reslist The resource list.
  103. * @param resource An address where the pointer to the resource
  104. * will be stored.
  105. */
  106. APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist,
  107. void **resource);
  108. /**
  109. * Return a resource back to the list of available resources.
  110. * @param reslist The resource list.
  111. * @param resource The resource to return to the list.
  112. */
  113. APU_DECLARE(apr_status_t) apr_reslist_release(apr_reslist_t *reslist,
  114. void *resource);
  115. /**
  116. * Set the timeout the acquire will wait for a free resource
  117. * when the maximum number of resources is exceeded.
  118. * @param reslist The resource list.
  119. * @param timeout Timeout to wait. The zero waits forever.
  120. */
  121. APU_DECLARE(void) apr_reslist_timeout_set(apr_reslist_t *reslist,
  122. apr_interval_time_t timeout);
  123. /**
  124. * Return the number of outstanding resources.
  125. * @param reslist The resource list.
  126. */
  127. APU_DECLARE(apr_uint32_t) apr_reslist_acquired_count(apr_reslist_t *reslist);
  128. /**
  129. * Invalidate a resource in the pool - e.g. a database connection
  130. * that returns a "lost connection" error and can't be restored.
  131. * Use this instead of apr_reslist_release if the resource is bad.
  132. * @param reslist The resource list.
  133. * @param resource The resource to invalidate.
  134. */
  135. APU_DECLARE(apr_status_t) apr_reslist_invalidate(apr_reslist_t *reslist,
  136. void *resource);
  137. /**
  138. * Perform routine maintenance on the resource list. This call
  139. * may instantiate new resources or expire old resources.
  140. * @param reslist The resource list.
  141. */
  142. APU_DECLARE(apr_status_t) apr_reslist_maintain(apr_reslist_t *reslist);
  143. /**
  144. * Set reslist cleanup order.
  145. * @param reslist The resource list.
  146. * @param mode Cleanup order mode
  147. * <PRE>
  148. * APR_RESLIST_CLEANUP_DEFAULT default pool cleanup order
  149. * APR_RESLIST_CLEANUP_FIRST use pool pre cleanup
  150. * </PRE>
  151. * @remark If APR_RESLIST_CLEANUP_FIRST is used the destructors will
  152. * be called before child pools of the pool used to create the reslist
  153. * are destroyed. This allows to explicitly destroy the child pools
  154. * inside reslist destructors.
  155. */
  156. APU_DECLARE(void) apr_reslist_cleanup_order_set(apr_reslist_t *reslist,
  157. apr_uint32_t mode);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. /** @} */
  162. #endif /* ! APR_RESLIST_H */