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

94 lines
3.1 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 {
struct ThreadSafeQueueResult
{
enum Enum
{
/// No data during the timeout.
Timeout,
/// We get data immediately or after waiting during the timeout.
Data,
// We don't get data but was signaled to stop wait.
Signaled
};
};
/// The thread-safe queue to send data from some thread(s) to another single thread.
/// The thread-safe queue owns enqueued elements and deallocates element's memory on the destruction.
class ONIXS_FIXENGINE_API ThreadSafeQueue
{
public:
/// Initializes the instance.
ThreadSafeQueue();
/// Cleans up enqueued elements and internal resources.
~ThreadSafeQueue();
/// Adds an item to the queue.
/// Takes the ownership of the item from the smart pointer.
///
/// @throw The DomainException if the smart pointer is null.
///
/// @param enqueueItem The smart pointer to the allocated item to enqueue.
void enqueue(ItemBaseUniquePtr & enqueueItem);
/// Tries to get an item from the queue during the timeout.
/// Gives the ownership of the item to the smart pointer.
/// Returns the `ThreadSafeQueueResult` value which reflects the result status ('Timeout', 'Data', 'Signaled').
///
/// @param dequeueItem The smart pointer to store the dequeued item.
/// @param waitInMs The waiting timeout.
ThreadSafeQueueResult::Enum dequeue(ItemBaseUniquePtr & dequeueItem, const int waitInMs = InfiniteTimeout);
/// Removes all elements from the queue.
void clear();
/// Returns 'true' if the queue is empty, otherwise 'false'.
bool empty() const;
/// Wakes up the queue if it was in the waiting mode.
/// The `dequeue` method will return the 'Signaled' status if it was in the waiting mode.
void signal();
private:
/// Implementation details.
struct Impl;
Impl * impl_;
/// The copying and assignment are prohibited.
ThreadSafeQueue(const ThreadSafeQueue &);
ThreadSafeQueue & operator = (const ThreadSafeQueue &);
};
}
}