aboutsummaryrefslogtreecommitdiffstats
path: root/include/distortos/internal/synchronization/SignalInformationQueue.hpp
blob: 7a9d08a73274ffe4d9e479d143c6d704f3f10523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * \file
 * \brief SignalInformationQueue class header
 *
 * \author Copyright (C) 2015 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
 *
 * \par License
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
 * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_SIGNALINFORMATIONQUEUE_HPP_
#define INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_SIGNALINFORMATIONQUEUE_HPP_

#include "distortos/SignalInformation.hpp"

#include "estd/IntrusiveForwardList.hpp"

#include <memory>

namespace distortos
{

class SignalSet;

namespace internal
{

/// SignalInformationQueue class can be used for queuing of SignalInformation objects
class SignalInformationQueue
{
public:

	/// single node of internal forward list - estd::IntrusiveForwardListNode and SignalInformation
	struct QueueNode
	{
		/// node for intrusive forward list
		estd::IntrusiveForwardListNode node;

		/// queued SignalInformation
		SignalInformation signalInformation;
	};

	/// type of uninitialized storage for QueueNode
	using Storage = typename std::aligned_storage<sizeof(QueueNode), alignof(QueueNode)>::type;

	/// unique_ptr (with deleter) to Storage[]
	using StorageUniquePointer = std::unique_ptr<Storage[], void(&)(Storage*)>;

	/**
	 * \brief SignalInformationQueue's constructor
	 *
	 * \param [in] storageUniquePointer is a rvalue reference to StorageUniquePointer with storage for queue elements
	 * (sufficiently large for \a maxElements Storage objects) and appropriate deleter
	 * \param [in] maxElements is the number of elements in \a storage array
	 */

	SignalInformationQueue(StorageUniquePointer&& storageUniquePointer, size_t maxElements);

	/**
	 * \brief SignalInformationQueue's destructor
	 */

	~SignalInformationQueue();

	/**
	 * \brief Accepts (dequeues) one of signals that are queued.
	 *
	 * This should be called when the signal is "accepted".
	 *
	 * \param [in] signalNumber is the signal that will be accepted, [0; 31]
	 *
	 * \return pair with return code (0 on success, error code otherwise) and dequeued SignalInformation object;
	 * error codes:
	 * - EAGAIN - no SignalInformation object with signal number equal to \a signalNumber was queued;
	 */

	std::pair<int, SignalInformation> acceptQueuedSignal(uint8_t signalNumber);

	/**
	 * \return set of currently queued signals
	 */

	SignalSet getQueuedSignalSet() const;

	/**
	 * \brief Adds the signalNumber and signal value (sigval union) to list of queued SignalInformation objects.
	 *
	 * \param [in] signalNumber is the signal that will be queued, [0; 31]
	 * \param [in] value is the signal value
	 *
	 * \return 0 on success, error code otherwise:
	 * - EAGAIN - no resources are available to queue the signal, \a maxElements signals are already queued;
	 * - EINVAL - \a signalNumber value is invalid;
	 */

	int queueSignal(uint8_t signalNumber, sigval value);

	SignalInformationQueue(const SignalInformationQueue&) = delete;
	SignalInformationQueue(SignalInformationQueue&&) = default;
	const SignalInformationQueue& operator=(const SignalInformationQueue&) = delete;
	SignalInformationQueue& operator=(SignalInformationQueue&&) = delete;

private:

	/// type of container with SignalInformation objects
	using List = estd::IntrusiveForwardList<QueueNode, &QueueNode::node>;

	/// storage for queue elements
	StorageUniquePointer storageUniquePointer_;

	/// list of queued SignalInformation objects
	List signalInformationList_;

	/// list of "free" SignalInformation objects
	List freeSignalInformationList_;
};

}	// namespace internal

}	// namespace distortos

#endif	// INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_SIGNALINFORMATIONQUEUE_HPP_