Skip to content

Add support for static mutexes #2051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hal/api/AnalogIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class AnalogIn {
}

analogin_t _adc;
static PlatformMutex _mutex;
static PlatformMutexStatic _mutex;
};

} // namespace mbed
Expand Down
2 changes: 1 addition & 1 deletion hal/api/FileBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class FileBase {
/* disallow copy constructor and assignment operators */
private:
static FileBase *_head;
static PlatformMutex _mutex;
static PlatformMutexStatic _mutex;

FileBase *_next;
const char * const _name;
Expand Down
2 changes: 1 addition & 1 deletion hal/api/I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class I2C {
i2c_t _i2c;
static I2C *_owner;
int _hz;
static PlatformMutex _mutex;
static PlatformMutexStatic _mutex;
};

} // namespace mbed
Expand Down
52 changes: 52 additions & 0 deletions hal/api/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "device.h"
#include "PinNames.h"
#include "PeripheralNames.h"
#include "critical.h"

#include <cstddef>
#include <cstdlib>
Expand Down Expand Up @@ -54,4 +55,55 @@ class PlatformMutex {

#endif

/** The static version of a PlatformMutex
*
* This class must only be used in a static context -
* this class must never be allocated or created on the
* stack.
*
* This class is lazily initialized on first use.
* This class is a POD type so if it is not used it will
* be garbage collected.
*/
struct PlatformMutexStatic {
PlatformMutex* _mutex;

void _init() {
PlatformMutex* current = _mutex;
PlatformMutex* new_mutex;

if (NULL == current) {
bool done = false;
new_mutex = new PlatformMutex;
while (!done) {
done = core_util_atomic_cas_ptr((void**)&_mutex, (void**)&current, (void*)new_mutex);
if (current != NULL) {
// Mutex was created on another thread first
// so delete ours
delete new_mutex;
break;
}
}
}
}

/** Wait until this Mutex becomes available.
*/
void lock() {
if (NULL == _mutex) {
_init();
}
_mutex->lock();
}

/** Unlock the mutex that has previously been locked by the same thread
*/
void unlock() {
if (NULL == _mutex) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, wouldn't it be an error to unlock a mutex that was never locked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Want me to assert this instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's up to you, not sure it matters that much

_init();
}
_mutex->unlock();
}
};

#endif
2 changes: 1 addition & 1 deletion hal/common/AnalogIn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace mbed {

PlatformMutex AnalogIn::_mutex;
PlatformMutexStatic AnalogIn::_mutex;

};

Expand Down
2 changes: 1 addition & 1 deletion hal/common/FileBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace mbed {

FileBase *FileBase::_head = NULL;
PlatformMutex FileBase::_mutex;
PlatformMutexStatic FileBase::_mutex;

FileBase::FileBase(const char *name, PathType t) : _next(NULL),
_name(name),
Expand Down
2 changes: 1 addition & 1 deletion hal/common/I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace mbed {

I2C *I2C::_owner = NULL;
PlatformMutex I2C::_mutex;
PlatformMutexStatic I2C::_mutex;

I2C::I2C(PinName sda, PinName scl) :
#if DEVICE_I2C_ASYNCH
Expand Down
2 changes: 1 addition & 1 deletion hal/common/retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extern const char __stderr_name[] = "/stderr";
* (or rather index+3, as filehandles 0-2 are stdin/out/err).
*/
static FileHandle *filehandles[OPEN_MAX];
static PlatformMutex filehandle_mutex;
static PlatformMutexStatic filehandle_mutex;

FileHandle::~FileHandle() {
filehandle_mutex.lock();
Expand Down