From b77adfcbd230c0f5ddf7048f90b458b27dd87960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 7 Oct 2019 11:18:58 +0200 Subject: [PATCH] Add proper thread synchronization to EventDriverDNS_GAI. --- source/eventcore/drivers/posix/dns.d | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/source/eventcore/drivers/posix/dns.d b/source/eventcore/drivers/posix/dns.d index d8a8dae..196455c 100644 --- a/source/eventcore/drivers/posix/dns.d +++ b/source/eventcore/drivers/posix/dns.d @@ -28,11 +28,12 @@ version (Posix) final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriverSignals) : EventDriverDNS { import std.parallelism : task, taskPool; import std.string : toStringz; + import core.atomic : atomicFence, atomicLoad, atomicStore; import core.thread : Thread; private { static struct Lookup { - bool done; + shared(bool) done; DNSLookupCallback callback; addrinfo* result; int retcode; @@ -104,7 +105,8 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver if (m_lookup.retcode == -1) version (CRuntime_Glibc) version (linux) __res_init(); - m_lookup.done = true; + atomicStore(m_lookup.done, true); + atomicFence(); // synchronize the other fields in m_lookup with the main thread m_events.trigger(m_event, true); debug (EventCoreLogDNS) print("lookup %s finished", m_lookup.name); } @@ -131,10 +133,13 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver { debug (EventCoreLogDNS) print("DNS event triggered"); m_events.wait(m_event, &onDNSSignal); + size_t lastmax; foreach (i, ref l; m_lookups) { if (i > m_maxHandle) break; - if (!l.done) continue; + if (!atomicLoad(l.done)) continue; + // synchronize the other fields in m_lookup with the lookup thread + atomicFence(); try { l.thread.join();