![]() |
TSDuck v3.46-4964
MPEG Transport Stream Toolkit
|
#include <tsMessageQueue.h>


Public Types | |
| using | MessagePtr = std::shared_ptr<MSG> |
| using | MessageType = MSG |
Public Member Functions | |
| MessageQueue (size_t max_messages=0) | |
| virtual | ~MessageQueue () override |
| void | addSubscription (SubscriptionHandlerInterface *handler) |
| void | cancelSubscription (SubscriptionHandlerInterface *handler) |
| void | clear () |
| size_t | currentQueueSize () |
| void | dequeue (MessagePtr &msg) |
| bool | dequeue (MessagePtr &msg, cn::milliseconds timeout) |
| bool | enqueue (MessagePtr &msg) |
| bool | enqueue (MessagePtr &msg, cn::milliseconds timeout) |
| bool | enqueue (MSG *msg) |
| bool | enqueue (MSG *msg, cn::milliseconds timeout) |
| bool | forceEnqueue (MessagePtr &msg, bool close_queue=false) |
| bool | forceEnqueue (MSG *msg, bool close_queue=false) |
| size_t | getMaxMessages () const |
| bool | isClosed () const |
| MessagePtr | peek () |
| void | setMaxMessages (size_t max_messages) |
Protected Types | |
| using | MessageList = std::list<MessagePtr> |
Protected Member Functions | |
| template<typename HANDLER, typename F> requires std::derived_from<HANDLER, SubscriptionHandlerInterface> | |
| void | callSubscribers (F &&func) |
| virtual MessageList::iterator | dequeuePlacement (MessageList &list) |
| virtual MessageList::iterator | enqueuePlacement (const MessagePtr &msg, MessageList &list) |
Template message queue for inter-thread communication.
This class implements a synchronized access to a shared queue of generic messages. Messages are manipulated using shared pointers (std::shared_ptr).
Subscription to enqueue and dequese is possible using MessageQueueHandlerInterface.
| MSG | The type of the messages to exchange. |
| using ts::MessageQueue< MSG >::MessageType = MSG |
Common renaming of the message type.
| using ts::MessageQueue< MSG >::MessagePtr = std::shared_ptr<MSG> |
Safe pointer to messages.
Since data are copied from the producer thread into the queue and later copied again from the queue into the consumer thread, the copied data is always a shared pointer to the actual message content.
|
protected |
Queues are implemented as list of smart pointers to messages.
| ts::MessageQueue< MSG >::MessageQueue | ( | size_t | max_messages = 0 | ) |
Constructor.
| [in] | max_messages | Maximum number of messages in the queue. |
|
overridevirtual |
Destructor
| size_t ts::MessageQueue< MSG >::getMaxMessages | ( | ) | const |
Get the maximum allowed messages in the queue.
| void ts::MessageQueue< MSG >::setMaxMessages | ( | size_t | max_messages | ) |
Change the maximum allowed messages in the queue.
| [in] | max_messages | Maximum number of messages in the queue. When a thread attempts to enqueue a message and the queue is full, the thread waits until at least one message is dequeued. If max_messages is 0, the queue is unlimited. In that case, the logic of the application must ensure that the queue is bounded somehow, otherwise the queue may exhaust all the process memory. |
| size_t ts::MessageQueue< MSG >::currentQueueSize | ( | ) |
Get the current queue size.
|
inline |
Check if the queue has been closed after a forceEnqueue().
| bool ts::MessageQueue< MSG >::enqueue | ( | MessagePtr & | msg | ) |
Insert a message in the queue. If the queue is full, the calling thread waits until some space becomes available in the queue.
| [in,out] | msg | The message to enqueue. The ownership of the pointed object is transfered to the message queue. Upon return, the msg safe pointer becomes a null pointer if the message was successfully enqueued. If the queue was previously closed, msg is unchanged. |
| bool ts::MessageQueue< MSG >::enqueue | ( | MessagePtr & | msg, |
| cn::milliseconds | timeout ) |
Insert a message in the queue. If the queue is full, the calling thread waits until some space becomes available in the queue or the timeout expires.
| [in,out] | msg | The message to enqueue. The ownership of the pointed object is transfered to the message queue. Upon return, the msg safe pointer becomes a null pointer if the message was successfully enqueued (no timeout, queue not closed). |
| [in] | timeout | Maximum time to wait in milliseconds. |
| bool ts::MessageQueue< MSG >::enqueue | ( | MSG * | msg | ) |
Insert a message in the queue.
| [in] | msg | A pointer to the message to enqueue. This pointer shall not be owned by a safe pointer. When the message is successfully enqueued, the pointer becomes owned by a safe pointer and will be deallocated when no longer used. If the queue is closed, the message is immediately deallocated. |
| bool ts::MessageQueue< MSG >::enqueue | ( | MSG * | msg, |
| cn::milliseconds | timeout ) |
Insert a message in the queue.
| [in] | msg | A pointer to the message to enqueue. This pointer shall not be owned by a safe pointer. When the message is successfully enqueued, the pointer becomes owned by a safe pointer and will be deallocated when no longer used. In case of timeout or closed queue, the message is not enqueued and immediately deallocated. |
| [in] | timeout | Maximum time to wait in milliseconds. |
| bool ts::MessageQueue< MSG >::forceEnqueue | ( | MessagePtr & | msg, |
| bool | close_queue = false ) |
Insert a message in the queue, even if the queue is full. This method immediately inserts the message, even if the queue is full. This can be used to allow exceptional overflow of the queue with unique messages, to enqueue a message to instruct the consumer thread to terminate for instance.
| [in,out] | msg | The message to enqueue. The ownership of the pointed object is transfered to the message queue. Upon return, the msg safe pointer becomes a null pointer. |
| [in] | close_queue | When true, the queue is considered closed and no more messages will be enqueued. All further enqueue operations will fail. |
| bool ts::MessageQueue< MSG >::forceEnqueue | ( | MSG * | msg, |
| bool | close_queue = false ) |
Insert a message in the queue, even if the queue is full. This method immediately inserts the message, even if the queue is full. This can be used to allow exceptional overflow of the queue with unique messages, to enqueue a message to instruct the consumer thread to terminate for instance.
| [in] | msg | A pointer to the message to enqueue. This pointer shall not be owned by a safe pointer. When the message is enqueued, the pointer becomes owned by a safe pointer and will be deallocated when no longer used. |
| [in] | close_queue | When true, the queue is considered closed and no more messages will be enqueued. All further enqueue operations will fail. |
| void ts::MessageQueue< MSG >::dequeue | ( | MessagePtr & | msg | ) |
Remove a message from the queue. Wait until a message is received.
| [out] | msg | Received message. |
| bool ts::MessageQueue< MSG >::dequeue | ( | MessagePtr & | msg, |
| cn::milliseconds | timeout ) |
Remove a message from the queue. Wait until a message is received or the timeout expires.
| [out] | msg | Received message. |
| [in] | timeout | Maximum time to wait in milliseconds. If timeout is zero and the queue is empty, return immediately. |
| ts::MessageQueue< MSG >::MessagePtr ts::MessageQueue< MSG >::peek | ( | ) |
Peek the next message from the queue, without dequeueing it. If several threads simultaneously read from the queue, the returned message may be deqeued in the meantime by another thread.
| void ts::MessageQueue< MSG >::clear | ( | ) |
Clear the content of the queue. If the close_queue flags was set, it is cleared and the queue is no longer closed.
|
protectedvirtual |
This virtual protected method performs placement in the message queue.
| [in] | msg | The message to enqueue later. The message is not enqueued. Its value is used to compute the place where it should be inserted. |
| [in] | list | The content of the queue. |
|
protectedvirtual |
This virtual protected method performs dequeue location in the message queue.
| [in] | list | The content of the queue. |
|
inherited |
Add a subscriber to open/close events.
| [in] | handler | The object to call on open() and close(). |
|
inherited |
Remove a subscriber to open/close events.
| [in] | handler | The object to no longer call on open() and close(). |
|
inlineprotectedinherited |
Call a handler on all subscribers, using a lambda expression.
| [in] | func | Function to call as lambda expression. |