From 16da4afcf7e7d0306bdb5bbbd502fdd217e0307d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 2 Oct 2024 12:26:24 +0200 Subject: [PATCH] Use a global event ID counter on Windows. Avoids generating duplicate event IDs in different threads. Note that event IDs are defined to be thread-local, so this is not a semantic change, but it does help with debugging and with detecting illegal use of event IDs. --- source/eventcore/drivers/winapi/events.d | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/eventcore/drivers/winapi/events.d b/source/eventcore/drivers/winapi/events.d index 4f47d34..ae98058 100644 --- a/source/eventcore/drivers/winapi/events.d +++ b/source/eventcore/drivers/winapi/events.d @@ -30,7 +30,7 @@ final class WinAPIEventDriverEvents : EventDriverEvents { EventSlot[EventID] m_events; CRITICAL_SECTION m_mutex; ConsumableQueue!Trigger m_pending; - uint m_idCounter; + static shared uint m_idCounter; } this(WinAPIEventDriverCore core) @@ -70,8 +70,10 @@ final class WinAPIEventDriverEvents : EventDriverEvents { override EventID create() { - auto id = EventID(m_idCounter++, 0); - if (id == EventID.invalid) id = EventID(m_idCounter++, 0); + import core.atomic : atomicOp; + + auto id = EventID(atomicOp!"+="(m_idCounter, 1), 0); + if (id == EventID.invalid) id = EventID(atomicOp!"+="(m_idCounter, 1), 0); m_events[id] = EventSlot(1, new ConsumableQueue!EventCallback); // FIXME: avoid GC allocation debug (EventCoreLeakTrace) { import core.runtime : defaultTraceHandler;