Skip to content

Commit 8db5b87

Browse files
committed
retarget - stdio serial new placement for Serial
Use placement new for Serial with locking. Compare to scott mayer's singleton, as it was implemented, saves guard variable + code around guards (for IAR it's 4 bytes RAM and 96 bytes ROM - high optimization enabled).
1 parent efea142 commit 8db5b87

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

hal/common/retarget.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,23 @@ static char stdio_out_prev;
102102

103103
namespace mbed {
104104

105+
static SingletonPtr<PlatformMutex> mutex_stdio_serial;
106+
105107
Serial& get_stdio_serial()
106108
{
107-
static bool stdio_uart_inited = false;
108-
static Serial stdio_serial(STDIO_UART_TX, STDIO_UART_RX);
109-
if (!stdio_uart_inited) {
109+
static bool stdio_inited = false;
110+
static unsigned stdio_uart[(sizeof(Serial) + sizeof(unsigned) - 1) / sizeof(unsigned)];
111+
112+
if (!stdio_inited) {
113+
mutex_stdio_serial->lock();
114+
new (stdio_uart) Serial(STDIO_UART_TX, STDIO_UART_RX);
115+
mutex_stdio_serial->unlock();
110116
#if MBED_CONF_CORE_STDIO_BAUD_RATE
111-
stdio_serial.baud(MBED_CONF_CORE_STDIO_BAUD_RATE);
117+
reinterpret_cast<Serial*>(stdio_uart)->baud(MBED_CONF_CORE_STDIO_BAUD_RATE);
112118
#endif
113-
stdio_uart_inited = true;
114-
}
115-
return stdio_serial;
119+
stdio_inited = true;
120+
}
121+
return reinterpret_cast<Serial&>(stdio_uart);
116122
}
117123

118124
} // namespace mbed

0 commit comments

Comments
 (0)