1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-17 11:54:33 +01:00

[PUSB] Fix static initialization order fiasco

For details see:
https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
This commit is contained in:
Martino Facchin 2015-10-01 17:35:26 +02:00 committed by Cristian Maglie
parent d13aadc7d5
commit 65b8430fec
9 changed files with 28 additions and 14 deletions

View File

@ -25,8 +25,6 @@
extern uint8_t _initEndpoints[];
PluggableUSB_ PluggableUSB;
int PluggableUSB_::getInterface(uint8_t* interfaceNum)
{
int sent = 0;
@ -90,6 +88,12 @@ bool PluggableUSB_::plug(PUSBListNode *node)
// restart USB layer???
}
PluggableUSB_& PluggableUSB()
{
static PluggableUSB_ obj;
return obj;
}
PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT),
lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT),
rootNode(NULL)

View File

@ -66,7 +66,10 @@ private:
PUSBListNode* rootNode;
};
extern PluggableUSB_ PluggableUSB;
// Replacement for global singleton.
// This function prevents static-initialization-order-fiasco
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
PluggableUSB_& PluggableUSB();
#endif

View File

@ -362,7 +362,7 @@ bool ClassInterfaceRequest(USBSetup& setup)
return CDC_Setup(setup);
#ifdef PLUGGABLE_USB_ENABLED
return PluggableUSB.setup(setup, i);
return PluggableUSB().setup(setup, i);
#endif
return false;
}
@ -440,7 +440,7 @@ static u8 SendInterfaces()
CDC_GetInterface(&interfaces);
#ifdef PLUGGABLE_USB_ENABLED
PluggableUSB.getInterface(&interfaces);
PluggableUSB().getInterface(&interfaces);
#endif
return interfaces;
@ -476,7 +476,7 @@ bool SendDescriptor(USBSetup& setup)
InitControl(setup.wLength);
#ifdef PLUGGABLE_USB_ENABLED
ret = PluggableUSB.getDescriptor(t);
ret = PluggableUSB().getDescriptor(t);
if (ret != 0) {
return (ret > 0 ? true : false);
}

View File

@ -21,7 +21,11 @@
#if defined(USBCON)
HID_ HID;
HID_& HID()
{
static HID_ obj;
return obj;
}
int HID_::getInterface(uint8_t* interfaceNum)
{
@ -113,7 +117,7 @@ HID_::HID_(void) : PUSBListNode(1, 1, epType),
protocol(1), idle(1)
{
epType[0] = EP_TYPE_INTERRUPT_IN;
PluggableUSB.plug(this);
PluggableUSB().plug(this);
}
int HID_::begin(void)

View File

@ -93,6 +93,11 @@ private:
uint8_t idle;
};
// Replacement for global singleton.
// This function prevents static-initialization-order-fiasco
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
HID_& HID();
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) }
#endif // USBCON

View File

@ -63,7 +63,7 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
Keyboard_::Keyboard_(void)
{
static HIDDescriptorListNode node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
HID.AppendDescriptor(&node);
HID().AppendDescriptor(&node);
}
void Keyboard_::begin(void)
@ -76,7 +76,7 @@ void Keyboard_::end(void)
void Keyboard_::sendReport(KeyReport* keys)
{
HID.SendReport(2,keys,sizeof(KeyReport));
HID().SendReport(2,keys,sizeof(KeyReport));
}
extern

View File

@ -94,7 +94,6 @@ public:
void releaseAll(void);
};
extern Keyboard_ Keyboard;
extern HID_ HID;
#endif
#endif

View File

@ -63,7 +63,7 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
Mouse_::Mouse_(void) : _buttons(0)
{
static HIDDescriptorListNode node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
HID.AppendDescriptor(&node);
HID().AppendDescriptor(&node);
}
void Mouse_::begin(void)
@ -89,7 +89,7 @@ void Mouse_::move(signed char x, signed char y, signed char wheel)
m[1] = x;
m[2] = y;
m[3] = wheel;
HID.SendReport(1,m,4);
HID().SendReport(1,m,4);
}
void Mouse_::buttons(uint8_t b)

View File

@ -55,7 +55,6 @@ public:
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
};
extern Mouse_ Mouse;
extern HID_ HID;
#endif
#endif