ResourcePool.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2016-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. */
  9. #pragma once
  10. #include <cassert>
  11. #include <functional>
  12. #include <memory>
  13. #include <mutex>
  14. #include <vector>
  15. namespace pzstd {
  16. /**
  17. * An unbounded pool of resources.
  18. * A `ResourcePool<T>` requires a factory function that takes allocates `T*` and
  19. * a free function that frees a `T*`.
  20. * Calling `ResourcePool::get()` will give you a new `ResourcePool::UniquePtr`
  21. * to a `T`, and when it goes out of scope the resource will be returned to the
  22. * pool.
  23. * The `ResourcePool<T>` *must* survive longer than any resources it hands out.
  24. * Remember that `ResourcePool<T>` hands out mutable `T`s, so make sure to clean
  25. * up the resource before or after every use.
  26. */
  27. template <typename T>
  28. class ResourcePool {
  29. public:
  30. class Deleter;
  31. using Factory = std::function<T*()>;
  32. using Free = std::function<void(T*)>;
  33. using UniquePtr = std::unique_ptr<T, Deleter>;
  34. private:
  35. std::mutex mutex_;
  36. Factory factory_;
  37. Free free_;
  38. std::vector<T*> resources_;
  39. unsigned inUse_;
  40. public:
  41. /**
  42. * Creates a `ResourcePool`.
  43. *
  44. * @param factory The function to use to create new resources.
  45. * @param free The function to use to free resources created by `factory`.
  46. */
  47. ResourcePool(Factory factory, Free free)
  48. : factory_(std::move(factory)), free_(std::move(free)), inUse_(0) {}
  49. /**
  50. * @returns A unique pointer to a resource. The resource is null iff
  51. * there are no available resources and `factory()` returns null.
  52. */
  53. UniquePtr get() {
  54. std::lock_guard<std::mutex> lock(mutex_);
  55. if (!resources_.empty()) {
  56. UniquePtr resource{resources_.back(), Deleter{*this}};
  57. resources_.pop_back();
  58. ++inUse_;
  59. return resource;
  60. }
  61. UniquePtr resource{factory_(), Deleter{*this}};
  62. ++inUse_;
  63. return resource;
  64. }
  65. ~ResourcePool() noexcept {
  66. assert(inUse_ == 0);
  67. for (const auto resource : resources_) {
  68. free_(resource);
  69. }
  70. }
  71. class Deleter {
  72. ResourcePool *pool_;
  73. public:
  74. explicit Deleter(ResourcePool &pool) : pool_(&pool) {}
  75. void operator() (T *resource) {
  76. std::lock_guard<std::mutex> lock(pool_->mutex_);
  77. // Make sure we don't put null resources into the pool
  78. if (resource) {
  79. pool_->resources_.push_back(resource);
  80. }
  81. assert(pool_->inUse_ > 0);
  82. --pool_->inUse_;
  83. }
  84. };
  85. };
  86. }