Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support to object construction with arguments. #259

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 47 additions & 63 deletions include/marl/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ class Pool {
MARL_NO_EXPORT inline T* get();

// construct() calls the constructor on the item's data.
MARL_NO_EXPORT inline void construct();
template <typename... Args>
MARL_NO_EXPORT inline void construct(Args&&... args) {
new (&data) T(std::forward<Args>(args)...);
}

// destruct() calls the destructor on the item's data.
MARL_NO_EXPORT inline void destruct();
Expand All @@ -110,11 +113,6 @@ T* Pool<T>::Item::get() {
return reinterpret_cast<T*>(&data);
}

template <typename T>
void Pool<T>::Item::construct() {
new (&data) T;
}

template <typename T>
void Pool<T>::Item::destruct() {
get()->~T();
Expand Down Expand Up @@ -214,32 +212,65 @@ class BoundedPool : public Pool<T> {

// borrow() borrows a single item from the pool, blocking until an item is
// returned if the pool is empty.
MARL_NO_EXPORT inline Loan borrow() const;
template <typename... Args>
MARL_NO_EXPORT inline Loan borrow(Args&&... args) const {
tinebp marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

@ben-clayton ben-clayton Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you've inlined these methods into the class? Marl tries to keep the implementations out of the class, so the public interface is easily human parsable. I'd prefer it if these were put back where they were.

static_assert((sizeof...(Args) == 0) || (POLICY != PoolPolicy::Preserve), "Arguments not supported with Preserve policy!");
marl::lock lock(storage->mutex);
storage->returned.wait(lock, [&] { return storage->free != nullptr; });
auto item = storage->free;
storage->free = storage->free->next;
if (POLICY == PoolPolicy::Reconstruct) {
item->construct(std::forward<Args>(args)...);
}
return Loan(item, storage);
}

// borrow() borrows count items from the pool, blocking until there are at
// borrowList() borrows count items from the pool, blocking until there are at
// least count items in the pool. The function f() is called with each
// borrowed item.
// F must be a function with the signature: void(T&&)
template <typename F>
MARL_NO_EXPORT inline void borrow(size_t count, const F& f) const;
template <typename F, typename... Args>
MARL_NO_EXPORT inline void borrowList(size_t count, const F& f, Args&&... args) const {
marl::lock lock(storage->mutex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add the static_assert here.

for (size_t i = 0; i < count; i++) {
storage->returned.wait(lock, [&] { return storage->free != nullptr; });
auto item = storage->free;
storage->free = storage->free->next;
if (POLICY == PoolPolicy::Reconstruct) {
item->construct(std::forward<Args>(args)...);
}
f(std::move(Loan(item, storage)));
}
}

// tryBorrow() attempts to borrow a single item from the pool without
// blocking.
// The boolean of the returned pair is true on success, or false if the pool
// is empty.
MARL_NO_EXPORT inline std::pair<Loan, bool> tryBorrow() const;
template <typename... Args>
MARL_NO_EXPORT inline std::pair<Loan, bool> tryBorrow(Args&&... args) const {
Item* item = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

{
marl::lock lock(storage->mutex);
if (storage->free == nullptr) {
return std::make_pair(Loan(), false);
}
item = storage->free;
storage->free = storage->free->next;
item->pool = this;
}
if (POLICY == PoolPolicy::Reconstruct) {
item->construct(std::forward<Args>(args)...);
}
return std::make_pair(Loan(item, storage), true);
}

private:
class Storage : public Pool<T>::Storage {
public:
MARL_NO_EXPORT inline Storage(Allocator* allocator);
MARL_NO_EXPORT inline ~Storage();
MARL_NO_EXPORT inline void return_(Item*) override;
// We cannot copy this as the Item pointers would be shared and
// deleted at a wrong point. We cannot move this because we return
// pointers into items[N].
MARL_NO_EXPORT inline Storage(const Storage&) = delete;
MARL_NO_EXPORT inline Storage& operator=(const Storage&) = delete;

Item items[N];
marl::mutex mutex;
Expand Down Expand Up @@ -275,48 +306,6 @@ BoundedPool<T, N, POLICY>::BoundedPool(
Allocator* allocator /* = Allocator::Default */)
: storage(allocator->make_shared<Storage>(allocator)) {}

template <typename T, int N, PoolPolicy POLICY>
typename BoundedPool<T, N, POLICY>::Loan BoundedPool<T, N, POLICY>::borrow()
const {
Loan out;
borrow(1, [&](Loan&& loan) { out = std::move(loan); });
return out;
}

template <typename T, int N, PoolPolicy POLICY>
template <typename F>
void BoundedPool<T, N, POLICY>::borrow(size_t n, const F& f) const {
marl::lock lock(storage->mutex);
for (size_t i = 0; i < n; i++) {
storage->returned.wait(lock, [&] { return storage->free != nullptr; });
auto item = storage->free;
storage->free = storage->free->next;
if (POLICY == PoolPolicy::Reconstruct) {
item->construct();
}
f(std::move(Loan(item, storage)));
}
}

template <typename T, int N, PoolPolicy POLICY>
std::pair<typename BoundedPool<T, N, POLICY>::Loan, bool>
BoundedPool<T, N, POLICY>::tryBorrow() const {
Item* item = nullptr;
{
marl::lock lock(storage->mutex);
if (storage->free == nullptr) {
return std::make_pair(Loan(), false);
}
item = storage->free;
storage->free = storage->free->next;
item->pool = this;
}
if (POLICY == PoolPolicy::Reconstruct) {
item->construct();
}
return std::make_pair(Loan(item, storage), true);
}

template <typename T, int N, PoolPolicy POLICY>
void BoundedPool<T, N, POLICY>::Storage::return_(Item* item) {
if (POLICY == PoolPolicy::Reconstruct) {
Expand Down Expand Up @@ -366,11 +355,6 @@ class UnboundedPool : public Pool<T> {
MARL_NO_EXPORT inline Storage(Allocator* allocator);
MARL_NO_EXPORT inline ~Storage();
MARL_NO_EXPORT inline void return_(Item*) override;
// We cannot copy this as the Item pointers would be shared and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I see you've moved this up to Storage, I think it would be generally safer if this remained here as well. Please revert.

// deleted at a wrong point. We could move this but would have to take
// extra care no Item pointers are left in the moved-out object.
MARL_NO_EXPORT inline Storage(const Storage&) = delete;
MARL_NO_EXPORT inline Storage& operator=(const Storage&) = delete;

Allocator* allocator;
marl::mutex mutex;
Expand Down