OnixS C++ FIX Engine  4.12.0
API Documentation
ThreadSafePool.h
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable OnixS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 
20 #pragma once
21 
22 #include <OnixS/FIXEngine/ABI.h>
24 
25 namespace OnixS {
26 namespace Threading {
27 
28 /// The thread-safe pool of objects by pointers.
29 /// The pool is safe for many writers and one reader.
30 /// The common scenario : a producer of data creates the objects using the pool
31 /// and sends them to a consumer threads to process, then these threads returns
32 /// objects back to producer's pool and so on.
34 {
35 public:
36 
37  /// Initializes the instance.
38  ///
39  /// @param preferredSize The preferred size of the pool.
40  /// @param allocator The allocator to provide a strategy to create/destroy pool items.
41  ThreadSafePool(size_t preferredSize, PoolAllocatorBase & allocator);
42 
43  /// Removes pooled elements and cleans up internal resources.
44  ~ThreadSafePool();
45 
46  /// Gets an item from the pool.
47  /// Gives the ownership of the item to the smart pointer.
48  /// If the pool is empty then creates an item by the allocator strategy.
49  /// If the number of allocated elements becomes greater than the preferred pool size,
50  /// after a high load, then destroys a one element, when possible, by the allocator strategy.
51  ///
52  /// @param poolItem The smart pointer to store the pool item.
53  void get(ItemBaseUniquePtr & poolItem);
54 
55  /// Returns an item to the pool.
56  /// Takes the ownership of the item from the smart pointer.
57  ///
58  /// @throw The DomainException if the smart pointer is null.
59  ///
60  /// @param poolItem The smart pointer to the pool item for returning to the pool.
61  void put(ItemBaseUniquePtr & poolItem);
62 
63  /// Returns the preferred size of the pool.
64  size_t preferredSize();
65 
66 private:
67 
68  /// Implementation details.
69  struct Impl;
70  Impl * impl_;
71 
72  /// The copying and assignment are prohibited.
74  ThreadSafePool & operator = (const ThreadSafePool &);
75 };
76 
77 }
78 }
The thread-safe pool of objects by pointers.
PtrTraits< ItemBase >::UniquePtr ItemBaseUniquePtr
Definition: Definitions.h:89
#define ONIXS_FIXENGINE_API
Definition: ABI.h:45
The base class for a custom pool allocator to provide a strategy to create/destroy items for the thre...
Definition: Definitions.h:93