Skip to content

Commit

Permalink
Merge pull request #1863 from c1728p9/thread_safe_cpp_api
Browse files Browse the repository at this point in the history
Make core mbed API thread safe
  • Loading branch information
sg- authored Jun 11, 2016
2 parents 52e93ae + 3d8d441 commit 1947c48
Show file tree
Hide file tree
Showing 76 changed files with 1,197 additions and 142 deletions.
25 changes: 23 additions & 2 deletions hal/api/AnalogIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
namespace mbed {

/** An analog input, used for reading the voltage on a pin
*
* @Note Synchronization level: Thread safe
*
* Example:
* @code
Expand Down Expand Up @@ -53,15 +55,20 @@ class AnalogIn {
* @param name (optional) A string to identify the object
*/
AnalogIn(PinName pin) {
lock();
analogin_init(&_adc, pin);
unlock();
}

/** Read the input voltage, represented as a float in the range [0.0, 1.0]
*
* @returns A floating-point value representing the current input voltage, measured as a percentage
*/
float read() {
return analogin_read(&_adc);
lock();
float ret = analogin_read(&_adc);
unlock();
return ret;
}

/** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
Expand All @@ -70,7 +77,10 @@ class AnalogIn {
* 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
*/
unsigned short read_u16() {
return analogin_read_u16(&_adc);
lock();
unsigned short ret = analogin_read_u16(&_adc);
unlock();
return ret;
}

#ifdef MBED_OPERATORS
Expand All @@ -88,12 +98,23 @@ class AnalogIn {
* @endcode
*/
operator float() {
// Underlying call is thread safe
return read();
}
#endif

protected:

virtual void lock() {
_mutex.lock();
}

virtual void unlock() {
_mutex.unlock();
}

analogin_t _adc;
static PlatformMutex _mutex;
};

} // namespace mbed
Expand Down
24 changes: 23 additions & 1 deletion hal/api/AnalogOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
namespace mbed {

/** An analog output, used for setting the voltage on a pin
*
* @Note Synchronization level: Thread safe
*
* Example:
* @code
Expand Down Expand Up @@ -64,7 +66,9 @@ class AnalogOut {
* Values outside this range will be saturated to 0.0f or 1.0f.
*/
void write(float value) {
lock();
analogout_write(&_dac, value);
unlock();
}

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

/** Return the current output voltage setting, measured as a percentage (float)
Expand All @@ -87,31 +93,47 @@ class AnalogOut {
* This value may not match exactly the value set by a previous write().
*/
float read() {
return analogout_read(&_dac);
lock();
float ret = analogout_read(&_dac);
unlock();
return ret;
}

#ifdef MBED_OPERATORS
/** An operator shorthand for write()
*/
AnalogOut& operator= (float percent) {
// Underlying write call is thread safe
write(percent);
return *this;
}

AnalogOut& operator= (AnalogOut& rhs) {
// Underlying write call is thread safe
write(rhs.read());
return *this;
}

/** An operator shorthand for read()
*/
operator float() {
// Underlying read call is thread safe
return read();
}
#endif

protected:

virtual void lock() {
_mutex.lock();
}

virtual void unlock() {
_mutex.unlock();
}

dac_t _dac;
PlatformMutex _mutex;
};

} // namespace mbed
Expand Down
7 changes: 7 additions & 0 deletions hal/api/BusIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
namespace mbed {

/** A digital input bus, used for reading the state of a collection of pins
*
* @Note Synchronization level: Thread safe
*/
class BusIn {

Expand Down Expand Up @@ -65,6 +67,7 @@ class BusIn {
* Binary mask of connected pins
*/
int mask() {
// No lock needed since _nc_mask is not modified outside the constructor
return _nc_mask;
}

Expand All @@ -87,8 +90,12 @@ class BusIn {
*/
int _nc_mask;

PlatformMutex _mutex;

/* disallow copy constructor and assignment operators */
private:
virtual void lock();
virtual void unlock();
BusIn(const BusIn&);
BusIn & operator = (const BusIn&);
};
Expand Down
7 changes: 7 additions & 0 deletions hal/api/BusInOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
namespace mbed {

/** A digital input output bus, used for setting the state of a collection of pins
*
* @Note Synchronization level: Thread safe
*/
class BusInOut {

Expand Down Expand Up @@ -79,6 +81,7 @@ class BusInOut {
* Binary mask of connected pins
*/
int mask() {
// No lock needed since _nc_mask is not modified outside the constructor
return _nc_mask;
}

Expand All @@ -98,6 +101,8 @@ class BusInOut {
#endif

protected:
virtual void lock();
virtual void unlock();
DigitalInOut* _pin[16];

/** Mask of bus's NC pins
Expand All @@ -106,6 +111,8 @@ class BusInOut {
*/
int _nc_mask;

PlatformMutex _mutex;

/* disallow copy constructor and assignment operators */
private:
BusInOut(const BusInOut&);
Expand Down
7 changes: 7 additions & 0 deletions hal/api/BusOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class BusOut {
*
* @param p<n> DigitalOut pin to connect to bus bit <n> (p5-p30, NC)
*
* @Note Synchronization level: Thread safe
*
* @note
* It is only required to specify as many pin variables as is required
* for the bus; the rest will default to NC (not connected)
Expand Down Expand Up @@ -63,6 +65,7 @@ class BusOut {
* Binary mask of connected pins
*/
int mask() {
// No lock needed since _nc_mask is not modified outside the constructor
return _nc_mask;
}

Expand All @@ -82,6 +85,8 @@ class BusOut {
#endif

protected:
virtual void lock();
virtual void unlock();
DigitalOut* _pin[16];

/** Mask of bus's NC pins
Expand All @@ -90,6 +95,8 @@ class BusOut {
*/
int _nc_mask;

PlatformMutex _mutex;

/* disallow copy constructor and assignment operators */
private:
BusOut(const BusOut&);
Expand Down
7 changes: 7 additions & 0 deletions hal/api/CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
namespace mbed {

/** CANMessage class
*
* @Note Synchronization level: Thread safe
*/
class CANMessage : public CAN_Message {

Expand Down Expand Up @@ -220,6 +222,7 @@ class CAN {
*/
template<typename T>
void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
// Underlying call thread safe
attach(Callback<void()>(obj, method), type);
}

Expand All @@ -232,14 +235,18 @@ class CAN {
*/
template<typename T>
void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
// Underlying call thread safe
attach(Callback<void()>(obj, method), type);
}

static void _irq_handler(uint32_t id, CanIrqType type);

protected:
virtual void lock();
virtual void unlock();
can_t _can;
Callback<void()> _irq[9];
PlatformMutex _mutex;
};

} // namespace mbed
Expand Down
5 changes: 5 additions & 0 deletions hal/api/CThunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
/* IRQ/Exception compatible thunk entry function */
typedef void (*CThunkEntry)(void);

/**
* Class for created a pointer with data bound to it
*
* @Note Synchronization level: Not protected
*/
template<class T>
class CThunk
{
Expand Down
11 changes: 4 additions & 7 deletions hal/api/CallChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace mbed {
* sequence using CallChain::call(). Used mostly by the interrupt chaining code,
* but can be used for other purposes.
*
* @Note Synchronization level: Not protected
*
* Example:
* @code
* #include "mbed.h"
Expand Down Expand Up @@ -58,6 +60,7 @@ namespace mbed {
*/

typedef Callback<void()> *pFunctionPointer_t;
class CallChainLink;

class CallChain {
public:
Expand Down Expand Up @@ -160,17 +163,11 @@ class CallChain {
}
#endif

private:
void _check_size();

pFunctionPointer_t* _chain;
int _size;
int _elements;

/* disallow copy constructor and assignment operators */
private:
CallChain(const CallChain&);
CallChain & operator = (const CallChain&);
CallChainLink *_chain;
};

} // namespace mbed
Expand Down
2 changes: 2 additions & 0 deletions hal/api/Callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace mbed {


/** Callback class based on template specialization
*
* @Note Synchronization level: Not protected
*/
template <typename F>
class Callback;
Expand Down
Loading

0 comments on commit 1947c48

Please sign in to comment.