-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathShared.hpp
109 lines (89 loc) · 3.03 KB
/
Shared.hpp
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
/*
* This file is part of the Arduino_ThreadsafeIO library.
* Copyright (c) 2021 Arduino SA.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ARDUINO_THREADS_SHARED_HPP_
#define ARDUINO_THREADS_SHARED_HPP_
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <mbed.h>
/**************************************************************************************
* CONSTANT
**************************************************************************************/
static size_t constexpr SHARED_QUEUE_SIZE = 16;
/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/
template<class T, size_t QUEUE_SIZE = SHARED_QUEUE_SIZE>
class Shared
{
public:
T pop();
void push(T const & val);
inline T peek() const { return _val; }
operator T() [[deprecated("Use 'pop()' instead.")]];
void operator = (T const & val) [[deprecated("Use 'push()' instead.")]];
private:
T _val;
rtos::Mail<T, QUEUE_SIZE> _mailbox;
};
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
template<class T, size_t QUEUE_SIZE>
T Shared<T,QUEUE_SIZE>::pop()
{
T * val_ptr = _mailbox.try_get_for(rtos::Kernel::wait_for_u32_forever);
if (val_ptr)
{
T const tmp_val = *val_ptr;
_mailbox.free(val_ptr);
return tmp_val;
}
return _val;
}
template<class T, size_t QUEUE_SIZE>
void Shared<T,QUEUE_SIZE>::push(T const & val)
{
/* If the mailbox is full we are discarding the
* oldest element and then push the new one into
* the queue.
**/
if (_mailbox.full())
{
T * val_ptr = _mailbox.try_get_for(rtos::Kernel::wait_for_u32_forever);
_mailbox.free(val_ptr);
}
_val = val;
T * val_ptr = _mailbox.try_alloc();
if (val_ptr)
{
*val_ptr = val;
_mailbox.put(val_ptr);
}
}
template<class T, size_t QUEUE_SIZE>
Shared<T,QUEUE_SIZE>::operator T()
{
return pop();
}
template<class T, size_t QUEUE_SIZE>
void Shared<T,QUEUE_SIZE>::operator = (T const & val)
{
push(val);
}
#endif /* ARDUINO_THREADS_SHARED_HPP_ */