dispenso 1.4.1
A library for task parallelism
Loading...
Searching...
No Matches
resource_pool.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
15#pragma once
16
17#include <dispenso/platform.h>
19#include <moodycamel/blockingconcurrentqueue.h>
20
21namespace dispenso {
22
23template <typename T>
24class ResourcePool;
25
30template <typename T>
31class Resource {
32 public:
34 Resource(Resource&& other) : resource_(other.resource_), pool_(other.pool_) {
35 other.resource_ = nullptr;
36 }
37
38 Resource& operator=(Resource&& other) {
39 if (&other != this) {
40 recycle();
41 resource_ = other.resource_;
42 pool_ = other.pool_;
43 other.resource_ = nullptr;
44 }
45 return *this;
46 }
47
53 T& get() {
54 return *resource_;
55 }
56
57 ~Resource() {
58 recycle();
59 }
60
61 private:
62 Resource(T* res, ResourcePool<T>* pool) : resource_(res), pool_(pool) {}
63
64 void recycle();
65
66 T* resource_;
67 ResourcePool<T>* pool_;
68
69 friend class ResourcePool<T>;
70};
71
76template <typename T>
78 public:
86 template <typename F>
87 ResourcePool(size_t size, const F& init)
88 : pool_(size),
89 backingResources_(
90 reinterpret_cast<char*>(
91 detail::alignedMalloc(size * detail::alignToCacheLine(sizeof(T))))),
92 size_(size) {
93 char* buf = backingResources_;
94
95 // There are three reasons we create our own buffer and use placement new:
96 // 1. We want to be able to handle non-movable non-copyable objects
97 // * Note that we could do this with std::deque
98 // 2. We want to minimize memory allocations, since that can be a common point of contention in
99 // multithreaded programs.
100 // 3. We can easily ensure that the objects are cache aligned to help avoid false sharing.
101
102 for (size_t i = 0; i < size; ++i) {
103 pool_.enqueue(new (buf) T(init()));
104 buf += detail::alignToCacheLine(sizeof(T));
105 }
106 }
107
114 T* t;
115 DISPENSO_TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
116 pool_.wait_dequeue(t);
117 DISPENSO_TSAN_ANNOTATE_IGNORE_WRITES_END();
118 return Resource<T>(t, this);
119 }
120
126 assert(pool_.size_approx() == size_);
127 for (size_t i = 0; i < size_; ++i) {
128 T* t;
129 DISPENSO_TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
130 pool_.wait_dequeue(t);
131 DISPENSO_TSAN_ANNOTATE_IGNORE_WRITES_END();
132 t->~T();
133 }
134 detail::alignedFree(backingResources_);
135 }
136
137 private:
138 void recycle(T* t) {
139 DISPENSO_TSAN_ANNOTATE_IGNORE_WRITES_BEGIN();
140 pool_.enqueue(t);
141 DISPENSO_TSAN_ANNOTATE_IGNORE_WRITES_END();
142 }
143
144 moodycamel::BlockingConcurrentQueue<T*> pool_;
145 char* backingResources_;
146 size_t size_;
147
148 friend class Resource<T>;
149};
150
151template <typename T>
152void Resource<T>::recycle() {
153 if (resource_) {
154 pool_->recycle(resource_);
155 }
156}
157
158} // namespace dispenso
Resource(Resource &&other)
Resource< T > acquire()
ResourcePool(size_t size, const F &init)