Skip to content

Commit 153ca8d

Browse files
committed
Add virtual lock for thread safe classes
Add a virtual lock for the classes which are thread safe. This allows the use of a mutex to be overridden.
1 parent 0af0153 commit 153ca8d

File tree

14 files changed

+129
-59
lines changed

14 files changed

+129
-59
lines changed

hal/api/AnalogIn.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ class AnalogIn {
5555
* @param name (optional) A string to identify the object
5656
*/
5757
AnalogIn(PinName pin) {
58-
_mutex.lock();
58+
lock();
5959
analogin_init(&_adc, pin);
60-
_mutex.unlock();
60+
unlock();
6161
}
6262

6363
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
6464
*
6565
* @returns A floating-point value representing the current input voltage, measured as a percentage
6666
*/
6767
float read() {
68-
_mutex.lock();
68+
lock();
6969
float ret = analogin_read(&_adc);
70-
_mutex.unlock();
70+
unlock();
7171
return ret;
7272
}
7373

@@ -77,9 +77,9 @@ class AnalogIn {
7777
* 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
7878
*/
7979
unsigned short read_u16() {
80-
_mutex.lock();
80+
lock();
8181
unsigned short ret = analogin_read_u16(&_adc);
82-
_mutex.unlock();
82+
unlock();
8383
return ret;
8484
}
8585

@@ -104,6 +104,15 @@ class AnalogIn {
104104
#endif
105105

106106
protected:
107+
108+
virtual void lock() {
109+
_mutex.lock();
110+
}
111+
112+
virtual void unlock() {
113+
_mutex.unlock();
114+
}
115+
107116
analogin_t _adc;
108117
static rtos::Mutex _mutex;
109118
};

hal/api/AnalogOut.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class AnalogOut {
6666
* Values outside this range will be saturated to 0.0f or 1.0f.
6767
*/
6868
void write(float value) {
69-
_mutex.lock();
69+
lock();
7070
analogout_write(&_dac, value);
71-
_mutex.unlock();
71+
unlock();
7272
}
7373

7474
/** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
@@ -77,9 +77,9 @@ class AnalogOut {
7777
* normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
7878
*/
7979
void write_u16(unsigned short value) {
80-
_mutex.lock();
80+
lock();
8181
analogout_write_u16(&_dac, value);
82-
_mutex.unlock();
82+
unlock();
8383
}
8484

8585
/** Return the current output voltage setting, measured as a percentage (float)
@@ -93,9 +93,9 @@ class AnalogOut {
9393
* This value may not match exactly the value set by a previous write().
9494
*/
9595
float read() {
96-
_mutex.lock();
96+
lock();
9797
float ret = analogout_read(&_dac);
98-
_mutex.unlock();
98+
unlock();
9999
return ret;
100100
}
101101

@@ -123,6 +123,15 @@ class AnalogOut {
123123
#endif
124124

125125
protected:
126+
127+
virtual void lock() {
128+
_mutex.lock();
129+
}
130+
131+
virtual void unlock() {
132+
_mutex.unlock();
133+
}
134+
126135
dac_t _dac;
127136
rtos::Mutex _mutex;
128137
};

hal/api/BusIn.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class BusIn {
9494

9595
/* disallow copy constructor and assignment operators */
9696
private:
97+
virtual void lock();
98+
virtual void unlock();
9799
BusIn(const BusIn&);
98100
BusIn & operator = (const BusIn&);
99101
};

hal/api/BusInOut.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class BusInOut {
101101
#endif
102102

103103
protected:
104+
virtual void lock();
105+
virtual void unlock();
104106
DigitalInOut* _pin[16];
105107

106108
/** Mask of bus's NC pins

hal/api/BusOut.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class BusOut {
8585
#endif
8686

8787
protected:
88+
virtual void lock();
89+
virtual void unlock();
8890
DigitalOut* _pin[16];
8991

9092
/** Mask of bus's NC pins

hal/api/CAN.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ class CAN {
242242
static void _irq_handler(uint32_t id, CanIrqType type);
243243

244244
protected:
245+
virtual void lock();
246+
virtual void unlock();
245247
can_t _can;
246248
Callback<void()> _irq[9];
247249
rtos::Mutex _mutex;

hal/api/I2C.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ class I2C {
139139

140140
/** Acquire exclusive access to this I2C bus
141141
*/
142-
void lock(void);
142+
virtual void lock(void);
143143

144144
/** Release exclusive access to this I2C bus
145145
*/
146-
void unlock(void);
146+
virtual void unlock(void);
147147

148148
#if DEVICE_I2C_ASYNCH
149149

hal/api/InterruptManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ class InterruptManager {
113113
*/
114114
bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
115115

116+
protected:
117+
virtual void lock();
118+
virtual void unlock();
119+
116120
private:
117121
InterruptManager();
118122
~InterruptManager();

hal/api/SPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ class SPI {
116116

117117
/** Acquire exclusive access to this SPI bus
118118
*/
119-
void lock(void);
119+
virtual void lock(void);
120120

121121
/** Release exclusive access to this SPI bus
122122
*/
123-
void unlock(void);
123+
virtual void unlock(void);
124124

125125
#if DEVICE_SPI_ASYNCH
126126

hal/common/BusIn.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,31 @@ BusIn::~BusIn() {
5353

5454
int BusIn::read() {
5555
int v = 0;
56-
_mutex.lock();
56+
lock();
5757
for (int i=0; i<16; i++) {
5858
if (_pin[i] != 0) {
5959
v |= _pin[i]->read() << i;
6060
}
6161
}
62-
_mutex.unlock();
62+
unlock();
6363
return v;
6464
}
6565

6666
void BusIn::mode(PinMode pull) {
67-
_mutex.lock();
67+
lock();
6868
for (int i=0; i<16; i++) {
6969
if (_pin[i] != 0) {
7070
_pin[i]->mode(pull);
7171
}
7272
}
73+
unlock();
74+
}
75+
76+
void BusIn::lock() {
77+
_mutex.lock();
78+
}
79+
80+
void BusIn::unlock() {
7381
_mutex.unlock();
7482
}
7583

hal/common/BusInOut.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,55 +52,55 @@ BusInOut::~BusInOut() {
5252
}
5353

5454
void BusInOut::write(int value) {
55-
_mutex.lock();
55+
lock();
5656
for (int i=0; i<16; i++) {
5757
if (_pin[i] != 0) {
5858
_pin[i]->write((value >> i) & 1);
5959
}
6060
}
61-
_mutex.unlock();
61+
unlock();
6262
}
6363

6464
int BusInOut::read() {
65-
_mutex.lock();
65+
lock();
6666
int v = 0;
6767
for (int i=0; i<16; i++) {
6868
if (_pin[i] != 0) {
6969
v |= _pin[i]->read() << i;
7070
}
7171
}
72-
_mutex.unlock();
72+
unlock();
7373
return v;
7474
}
7575

7676
void BusInOut::output() {
77-
_mutex.lock();
77+
lock();
7878
for (int i=0; i<16; i++) {
7979
if (_pin[i] != 0) {
8080
_pin[i]->output();
8181
}
8282
}
83-
_mutex.unlock();
83+
unlock();
8484
}
8585

8686
void BusInOut::input() {
87-
_mutex.lock();
87+
lock();
8888
for (int i=0; i<16; i++) {
8989
if (_pin[i] != 0) {
9090
_pin[i]->input();
9191
}
9292
}
93-
_mutex.unlock();
93+
unlock();
9494
}
9595

9696
void BusInOut::mode(PinMode pull) {
97-
_mutex.lock();
97+
lock();
9898
for (int i=0; i<16; i++) {
9999
if (_pin[i] != 0) {
100100
_pin[i]->mode(pull);
101101
}
102102
}
103-
_mutex.unlock();
103+
unlock();
104104
}
105105

106106
#ifdef MBED_OPERATORS
@@ -129,4 +129,12 @@ BusInOut::operator int() {
129129
}
130130
#endif
131131

132+
void BusInOut::lock() {
133+
_mutex.lock();
134+
}
135+
136+
void BusInOut::unlock() {
137+
_mutex.unlock();
138+
}
139+
132140
} // namespace mbed

hal/common/BusOut.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,24 @@ BusOut::~BusOut() {
5252
}
5353

5454
void BusOut::write(int value) {
55-
_mutex.lock();
55+
lock();
5656
for (int i=0; i<16; i++) {
5757
if (_pin[i] != 0) {
5858
_pin[i]->write((value >> i) & 1);
5959
}
6060
}
61-
_mutex.unlock();
61+
unlock();
6262
}
6363

6464
int BusOut::read() {
65-
_mutex.lock();
65+
lock();
6666
int v = 0;
6767
for (int i=0; i<16; i++) {
6868
if (_pin[i] != 0) {
6969
v |= _pin[i]->read() << i;
7070
}
7171
}
72-
_mutex.unlock();
72+
unlock();
7373
return v;
7474
}
7575

@@ -99,4 +99,12 @@ BusOut::operator int() {
9999
}
100100
#endif
101101

102+
void BusOut::lock() {
103+
_mutex.lock();
104+
}
105+
106+
void BusOut::unlock() {
107+
_mutex.unlock();
108+
}
109+
102110
} // namespace mbed

0 commit comments

Comments
 (0)