Skip to content

Commit 8cab209

Browse files
facchinmcmaglie
authored andcommitted
[PUSB] Fix static initialization order fiasco
For details see: https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
1 parent 66d3eab commit 8cab209

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

cores/arduino/PluggableUSB.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
extern uint8_t _initEndpoints[];
2727

28-
PluggableUSB_ PluggableUSB;
29-
3028
int PluggableUSB_::getInterface(uint8_t* interfaceNum)
3129
{
3230
int sent = 0;
@@ -90,6 +88,12 @@ bool PluggableUSB_::plug(PUSBListNode *node)
9088
// restart USB layer???
9189
}
9290

91+
PluggableUSB_& PluggableUSB()
92+
{
93+
static PluggableUSB_ obj;
94+
return obj;
95+
}
96+
9397
PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT),
9498
lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT),
9599
rootNode(NULL)

cores/arduino/PluggableUSB.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ class PluggableUSB_ {
6666
PUSBListNode* rootNode;
6767
};
6868

69-
extern PluggableUSB_ PluggableUSB;
69+
// Replacement for global singleton.
70+
// This function prevents static-initialization-order-fiasco
71+
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
72+
PluggableUSB_& PluggableUSB();
7073

7174
#endif
7275

cores/arduino/USBCore.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ bool ClassInterfaceRequest(USBSetup& setup)
362362
return CDC_Setup(setup);
363363

364364
#ifdef PLUGGABLE_USB_ENABLED
365-
return PluggableUSB.setup(setup, i);
365+
return PluggableUSB().setup(setup, i);
366366
#endif
367367
return false;
368368
}
@@ -440,7 +440,7 @@ static u8 SendInterfaces()
440440
CDC_GetInterface(&interfaces);
441441

442442
#ifdef PLUGGABLE_USB_ENABLED
443-
PluggableUSB.getInterface(&interfaces);
443+
PluggableUSB().getInterface(&interfaces);
444444
#endif
445445

446446
return interfaces;
@@ -476,7 +476,7 @@ bool SendDescriptor(USBSetup& setup)
476476

477477
InitControl(setup.wLength);
478478
#ifdef PLUGGABLE_USB_ENABLED
479-
ret = PluggableUSB.getDescriptor(t);
479+
ret = PluggableUSB().getDescriptor(t);
480480
if (ret != 0) {
481481
return (ret > 0 ? true : false);
482482
}

libraries/HID/HID.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
#if defined(USBCON)
2323

24-
HID_ HID;
24+
HID_& HID()
25+
{
26+
static HID_ obj;
27+
return obj;
28+
}
2529

2630
int HID_::getInterface(uint8_t* interfaceNum)
2731
{
@@ -113,7 +117,7 @@ HID_::HID_(void) : PUSBListNode(1, 1, epType),
113117
protocol(1), idle(1)
114118
{
115119
epType[0] = EP_TYPE_INTERRUPT_IN;
116-
PluggableUSB.plug(this);
120+
PluggableUSB().plug(this);
117121
}
118122

119123
int HID_::begin(void)

libraries/HID/HID.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ class HID_ : public PUSBListNode
9393
uint8_t idle;
9494
};
9595

96+
// Replacement for global singleton.
97+
// This function prevents static-initialization-order-fiasco
98+
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
99+
HID_& HID();
100+
96101
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) }
97102

98103
#endif // USBCON

0 commit comments

Comments
 (0)