Skip to content

Commit 1947c48

Browse files
authored
Merge pull request #1863 from c1728p9/thread_safe_cpp_api
Make core mbed API thread safe
2 parents 52e93ae + 3d8d441 commit 1947c48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1197
-142
lines changed

hal/api/AnalogIn.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
namespace mbed {
2626

2727
/** An analog input, used for reading the voltage on a pin
28+
*
29+
* @Note Synchronization level: Thread safe
2830
*
2931
* Example:
3032
* @code
@@ -53,15 +55,20 @@ class AnalogIn {
5355
* @param name (optional) A string to identify the object
5456
*/
5557
AnalogIn(PinName pin) {
58+
lock();
5659
analogin_init(&_adc, pin);
60+
unlock();
5761
}
5862

5963
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
6064
*
6165
* @returns A floating-point value representing the current input voltage, measured as a percentage
6266
*/
6367
float read() {
64-
return analogin_read(&_adc);
68+
lock();
69+
float ret = analogin_read(&_adc);
70+
unlock();
71+
return ret;
6572
}
6673

6774
/** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
@@ -70,7 +77,10 @@ class AnalogIn {
7077
* 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
7178
*/
7279
unsigned short read_u16() {
73-
return analogin_read_u16(&_adc);
80+
lock();
81+
unsigned short ret = analogin_read_u16(&_adc);
82+
unlock();
83+
return ret;
7484
}
7585

7686
#ifdef MBED_OPERATORS
@@ -88,12 +98,23 @@ class AnalogIn {
8898
* @endcode
8999
*/
90100
operator float() {
101+
// Underlying call is thread safe
91102
return read();
92103
}
93104
#endif
94105

95106
protected:
107+
108+
virtual void lock() {
109+
_mutex.lock();
110+
}
111+
112+
virtual void unlock() {
113+
_mutex.unlock();
114+
}
115+
96116
analogin_t _adc;
117+
static PlatformMutex _mutex;
97118
};
98119

99120
} // namespace mbed

hal/api/AnalogOut.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
namespace mbed {
2626

2727
/** An analog output, used for setting the voltage on a pin
28+
*
29+
* @Note Synchronization level: Thread safe
2830
*
2931
* Example:
3032
* @code
@@ -64,7 +66,9 @@ class AnalogOut {
6466
* Values outside this range will be saturated to 0.0f or 1.0f.
6567
*/
6668
void write(float value) {
69+
lock();
6770
analogout_write(&_dac, value);
71+
unlock();
6872
}
6973

7074
/** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
@@ -73,7 +77,9 @@ class AnalogOut {
7377
* normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
7478
*/
7579
void write_u16(unsigned short value) {
80+
lock();
7681
analogout_write_u16(&_dac, value);
82+
unlock();
7783
}
7884

7985
/** Return the current output voltage setting, measured as a percentage (float)
@@ -87,31 +93,47 @@ class AnalogOut {
8793
* This value may not match exactly the value set by a previous write().
8894
*/
8995
float read() {
90-
return analogout_read(&_dac);
96+
lock();
97+
float ret = analogout_read(&_dac);
98+
unlock();
99+
return ret;
91100
}
92101

93102
#ifdef MBED_OPERATORS
94103
/** An operator shorthand for write()
95104
*/
96105
AnalogOut& operator= (float percent) {
106+
// Underlying write call is thread safe
97107
write(percent);
98108
return *this;
99109
}
100110

101111
AnalogOut& operator= (AnalogOut& rhs) {
112+
// Underlying write call is thread safe
102113
write(rhs.read());
103114
return *this;
104115
}
105116

106117
/** An operator shorthand for read()
107118
*/
108119
operator float() {
120+
// Underlying read call is thread safe
109121
return read();
110122
}
111123
#endif
112124

113125
protected:
126+
127+
virtual void lock() {
128+
_mutex.lock();
129+
}
130+
131+
virtual void unlock() {
132+
_mutex.unlock();
133+
}
134+
114135
dac_t _dac;
136+
PlatformMutex _mutex;
115137
};
116138

117139
} // namespace mbed

hal/api/BusIn.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
namespace mbed {
2323

2424
/** A digital input bus, used for reading the state of a collection of pins
25+
*
26+
* @Note Synchronization level: Thread safe
2527
*/
2628
class BusIn {
2729

@@ -65,6 +67,7 @@ class BusIn {
6567
* Binary mask of connected pins
6668
*/
6769
int mask() {
70+
// No lock needed since _nc_mask is not modified outside the constructor
6871
return _nc_mask;
6972
}
7073

@@ -87,8 +90,12 @@ class BusIn {
8790
*/
8891
int _nc_mask;
8992

93+
PlatformMutex _mutex;
94+
9095
/* disallow copy constructor and assignment operators */
9196
private:
97+
virtual void lock();
98+
virtual void unlock();
9299
BusIn(const BusIn&);
93100
BusIn & operator = (const BusIn&);
94101
};

hal/api/BusInOut.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
namespace mbed {
2222

2323
/** A digital input output bus, used for setting the state of a collection of pins
24+
*
25+
* @Note Synchronization level: Thread safe
2426
*/
2527
class BusInOut {
2628

@@ -79,6 +81,7 @@ class BusInOut {
7981
* Binary mask of connected pins
8082
*/
8183
int mask() {
84+
// No lock needed since _nc_mask is not modified outside the constructor
8285
return _nc_mask;
8386
}
8487

@@ -98,6 +101,8 @@ class BusInOut {
98101
#endif
99102

100103
protected:
104+
virtual void lock();
105+
virtual void unlock();
101106
DigitalInOut* _pin[16];
102107

103108
/** Mask of bus's NC pins
@@ -106,6 +111,8 @@ class BusInOut {
106111
*/
107112
int _nc_mask;
108113

114+
PlatformMutex _mutex;
115+
109116
/* disallow copy constructor and assignment operators */
110117
private:
111118
BusInOut(const BusInOut&);

hal/api/BusOut.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class BusOut {
3030
*
3131
* @param p<n> DigitalOut pin to connect to bus bit <n> (p5-p30, NC)
3232
*
33+
* @Note Synchronization level: Thread safe
34+
*
3335
* @note
3436
* It is only required to specify as many pin variables as is required
3537
* for the bus; the rest will default to NC (not connected)
@@ -63,6 +65,7 @@ class BusOut {
6365
* Binary mask of connected pins
6466
*/
6567
int mask() {
68+
// No lock needed since _nc_mask is not modified outside the constructor
6669
return _nc_mask;
6770
}
6871

@@ -82,6 +85,8 @@ class BusOut {
8285
#endif
8386

8487
protected:
88+
virtual void lock();
89+
virtual void unlock();
8590
DigitalOut* _pin[16];
8691

8792
/** Mask of bus's NC pins
@@ -90,6 +95,8 @@ class BusOut {
9095
*/
9196
int _nc_mask;
9297

98+
PlatformMutex _mutex;
99+
93100
/* disallow copy constructor and assignment operators */
94101
private:
95102
BusOut(const BusOut&);

hal/api/CAN.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
namespace mbed {
2828

2929
/** CANMessage class
30+
*
31+
* @Note Synchronization level: Thread safe
3032
*/
3133
class CANMessage : public CAN_Message {
3234

@@ -220,6 +222,7 @@ class CAN {
220222
*/
221223
template<typename T>
222224
void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
225+
// Underlying call thread safe
223226
attach(Callback<void()>(obj, method), type);
224227
}
225228

@@ -232,14 +235,18 @@ class CAN {
232235
*/
233236
template<typename T>
234237
void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
238+
// Underlying call thread safe
235239
attach(Callback<void()>(obj, method), type);
236240
}
237241

238242
static void _irq_handler(uint32_t id, CanIrqType type);
239243

240244
protected:
245+
virtual void lock();
246+
virtual void unlock();
241247
can_t _can;
242248
Callback<void()> _irq[9];
249+
PlatformMutex _mutex;
243250
};
244251

245252
} // namespace mbed

hal/api/CThunk.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
/* IRQ/Exception compatible thunk entry function */
7272
typedef void (*CThunkEntry)(void);
7373

74+
/**
75+
* Class for created a pointer with data bound to it
76+
*
77+
* @Note Synchronization level: Not protected
78+
*/
7479
template<class T>
7580
class CThunk
7681
{

hal/api/CallChain.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace mbed {
2525
* sequence using CallChain::call(). Used mostly by the interrupt chaining code,
2626
* but can be used for other purposes.
2727
*
28+
* @Note Synchronization level: Not protected
29+
*
2830
* Example:
2931
* @code
3032
* #include "mbed.h"
@@ -58,6 +60,7 @@ namespace mbed {
5860
*/
5961

6062
typedef Callback<void()> *pFunctionPointer_t;
63+
class CallChainLink;
6164

6265
class CallChain {
6366
public:
@@ -160,17 +163,11 @@ class CallChain {
160163
}
161164
#endif
162165

163-
private:
164-
void _check_size();
165-
166-
pFunctionPointer_t* _chain;
167-
int _size;
168-
int _elements;
169-
170166
/* disallow copy constructor and assignment operators */
171167
private:
172168
CallChain(const CallChain&);
173169
CallChain & operator = (const CallChain&);
170+
CallChainLink *_chain;
174171
};
175172

176173
} // namespace mbed

hal/api/Callback.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace mbed {
2323

2424

2525
/** Callback class based on template specialization
26+
*
27+
* @Note Synchronization level: Not protected
2628
*/
2729
template <typename F>
2830
class Callback;

0 commit comments

Comments
 (0)