File tree Expand file tree Collapse file tree 8 files changed +59
-7
lines changed Expand file tree Collapse file tree 8 files changed +59
-7
lines changed Original file line number Diff line number Diff line change @@ -118,7 +118,7 @@ class AnalogIn {
118
118
}
119
119
120
120
analogin_t _adc;
121
- static PlatformMutex _mutex;
121
+ static PlatformMutexStatic _mutex;
122
122
};
123
123
124
124
} // namespace mbed
Original file line number Diff line number Diff line change @@ -65,7 +65,7 @@ class FileBase {
65
65
/* disallow copy constructor and assignment operators */
66
66
private:
67
67
static FileBase *_head;
68
- static PlatformMutex _mutex;
68
+ static PlatformMutexStatic _mutex;
69
69
70
70
FileBase *_next;
71
71
const char * const _name;
Original file line number Diff line number Diff line change @@ -181,7 +181,7 @@ class I2C {
181
181
i2c_t _i2c;
182
182
static I2C *_owner;
183
183
int _hz;
184
- static PlatformMutex _mutex;
184
+ static PlatformMutexStatic _mutex;
185
185
};
186
186
187
187
} // namespace mbed
Original file line number Diff line number Diff line change 21
21
#include " device.h"
22
22
#include " PinNames.h"
23
23
#include " PeripheralNames.h"
24
+ #include " critical.h"
24
25
25
26
#include < cstddef>
26
27
#include < cstdlib>
@@ -54,4 +55,55 @@ class PlatformMutex {
54
55
55
56
#endif
56
57
58
+ /* * The static version of a PlatformMutex
59
+ *
60
+ * This class must only be used in a static context -
61
+ * this class must never be allocated or created on the
62
+ * stack.
63
+ *
64
+ * This class is lazily initialized on first use.
65
+ * This class is a POD type so if it is not used it will
66
+ * be garbage collected.
67
+ */
68
+ struct PlatformMutexStatic {
69
+ PlatformMutex* _mutex;
70
+
71
+ void _init () {
72
+ PlatformMutex* current = _mutex;
73
+ PlatformMutex* new_mutex;
74
+
75
+ if (NULL == current) {
76
+ bool done = false ;
77
+ new_mutex = new PlatformMutex;
78
+ while (!done) {
79
+ done = core_util_atomic_cas_ptr ((void **)&_mutex, (void **)¤t, (void *)new_mutex);
80
+ if (current != NULL ) {
81
+ // Mutex was created on another thread first
82
+ // so delete ours
83
+ delete new_mutex;
84
+ break ;
85
+ }
86
+ }
87
+ }
88
+ }
89
+
90
+ /* * Wait until this Mutex becomes available.
91
+ */
92
+ void lock () {
93
+ if (NULL == _mutex) {
94
+ _init ();
95
+ }
96
+ _mutex->lock ();
97
+ }
98
+
99
+ /* * Unlock the mutex that has previously been locked by the same thread
100
+ */
101
+ void unlock () {
102
+ if (NULL == _mutex) {
103
+ _init ();
104
+ }
105
+ _mutex->unlock ();
106
+ }
107
+ };
108
+
57
109
#endif
Original file line number Diff line number Diff line change 22
22
23
23
namespace mbed {
24
24
25
- PlatformMutex AnalogIn::_mutex;
25
+ PlatformMutexStatic AnalogIn::_mutex;
26
26
27
27
};
28
28
Original file line number Diff line number Diff line change 18
18
namespace mbed {
19
19
20
20
FileBase *FileBase::_head = NULL ;
21
- PlatformMutex FileBase::_mutex;
21
+ PlatformMutexStatic FileBase::_mutex;
22
22
23
23
FileBase::FileBase (const char *name, PathType t) : _next(NULL ),
24
24
_name (name),
Original file line number Diff line number Diff line change 20
20
namespace mbed {
21
21
22
22
I2C *I2C::_owner = NULL ;
23
- PlatformMutex I2C::_mutex;
23
+ PlatformMutexStatic I2C::_mutex;
24
24
25
25
I2C::I2C (PinName sda, PinName scl) :
26
26
#if DEVICE_I2C_ASYNCH
Original file line number Diff line number Diff line change @@ -72,7 +72,7 @@ extern const char __stderr_name[] = "/stderr";
72
72
* (or rather index+3, as filehandles 0-2 are stdin/out/err).
73
73
*/
74
74
static FileHandle *filehandles[OPEN_MAX];
75
- static PlatformMutex filehandle_mutex;
75
+ static PlatformMutexStatic filehandle_mutex;
76
76
77
77
FileHandle::~FileHandle () {
78
78
filehandle_mutex.lock ();
You can’t perform that action at this time.
0 commit comments