Files
2024-09-23 12:00:04 -04:00

79 lines
2.9 KiB
C++

/*
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
*
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
* and international copyright treaties.
*
* Access to and use of the software is governed by the terms of the applicable OnixS Software
* Services Agreement (the Agreement) and Customer end user license agreements granting
* a non-assignable, non-transferable and non-exclusive license to use the software
* for it's own data processing purposes under the terms defined in the Agreement.
*
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
* of this source code or associated reference material to any other location for further reproduction
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
*
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
* the terms of the Agreement is a violation of copyright law.
*/
#pragma once
#include <OnixS/FIXEngine/ABI.h>
#include <OnixS/FIXEngine/Threading/Definitions.h>
namespace OnixS {
namespace Threading {
/// The thread-safe pool of objects by pointers.
/// The pool is safe for many writers and one reader.
/// The common scenario : a producer of data creates the objects using the pool
/// and sends them to a consumer threads to process, then these threads returns
/// objects back to producer's pool and so on.
class ONIXS_FIXENGINE_API ThreadSafePool
{
public:
/// Initializes the instance.
///
/// @param preferredSize The preferred size of the pool.
/// @param allocator The allocator to provide a strategy to create/destroy pool items.
ThreadSafePool(size_t preferredSize, PoolAllocatorBase & allocator);
/// Removes pooled elements and cleans up internal resources.
~ThreadSafePool();
/// Gets an item from the pool.
/// Gives the ownership of the item to the smart pointer.
/// If the pool is empty then creates an item by the allocator strategy.
/// If the number of allocated elements becomes greater than the preferred pool size,
/// after a high load, then destroys a one element, when possible, by the allocator strategy.
///
/// @param poolItem The smart pointer to store the pool item.
void get(ItemBaseUniquePtr & poolItem);
/// Returns an item to the pool.
/// Takes the ownership of the item from the smart pointer.
///
/// @throw The DomainException if the smart pointer is null.
///
/// @param poolItem The smart pointer to the pool item for returning to the pool.
void put(ItemBaseUniquePtr & poolItem);
/// Returns the preferred size of the pool.
size_t preferredSize();
private:
/// Implementation details.
struct Impl;
Impl * impl_;
/// The copying and assignment are prohibited.
ThreadSafePool(const ThreadSafePool &);
ThreadSafePool & operator = (const ThreadSafePool &);
};
}
}