Create the event object in ThreadedFileEventDriver lazily.

Avoids creating event objects for every worker thread, even if never used.
This commit is contained in:
Sönke Ludwig 2018-03-14 21:39:18 +01:00
parent 0044946413
commit 4192d31716

View file

@ -108,7 +108,7 @@ final class ThreadedFileEventDriver(Events : EventDriverEvents) : EventDriverFil
ChoppedVector!FileInfo m_files; ChoppedVector!FileInfo m_files;
SmallIntegerSet!FileFD m_activeReads; SmallIntegerSet!FileFD m_activeReads;
SmallIntegerSet!FileFD m_activeWrites; SmallIntegerSet!FileFD m_activeWrites;
EventID m_readyEvent; EventID m_readyEvent = EventID.invalid;
bool m_waiting; bool m_waiting;
Events m_events; Events m_events;
} }
@ -118,18 +118,23 @@ final class ThreadedFileEventDriver(Events : EventDriverEvents) : EventDriverFil
this(Events events) this(Events events)
{ {
m_events = events; m_events = events;
m_readyEvent = events.create();
} }
void dispose() void dispose()
{ {
if (m_fileThreadPool) if (m_fileThreadPool) {
StaticTaskPool.releaseRef(); StaticTaskPool.releaseRef();
log("finishing file events"); m_fileThreadPool = null;
m_events.cancelWait(m_readyEvent, &onReady); }
onReady(m_readyEvent);
m_events.releaseRef(m_readyEvent); if (m_readyEvent != EventID.invalid) {
log("finished file events"); log("finishing file events");
m_events.cancelWait(m_readyEvent, &onReady);
onReady(m_readyEvent);
m_events.releaseRef(m_readyEvent);
m_readyEvent = EventID.invalid;
log("finished file events");
}
} }
final override FileFD open(string path, FileOpenMode mode) final override FileFD open(string path, FileOpenMode mode)
@ -191,8 +196,7 @@ final class ThreadedFileEventDriver(Events : EventDriverEvents) : EventDriverFil
assert(f.callback is null, "Concurrent file writes are not allowed."); assert(f.callback is null, "Concurrent file writes are not allowed.");
f.callback = on_write_finish; f.callback = on_write_finish;
m_activeWrites.insert(file); m_activeWrites.insert(file);
if (m_fileThreadPool is null) threadSetup();
m_fileThreadPool = StaticTaskPool.addRef();
log("start write task"); log("start write task");
try { try {
m_fileThreadPool.put(task!(taskFun!("write", const(ubyte)))(this, file, offset, buffer)); m_fileThreadPool.put(task!(taskFun!("write", const(ubyte)))(this, file, offset, buffer));
@ -223,8 +227,7 @@ final class ThreadedFileEventDriver(Events : EventDriverEvents) : EventDriverFil
assert(f.callback is null, "Concurrent file reads are not allowed."); assert(f.callback is null, "Concurrent file reads are not allowed.");
f.callback = on_read_finish; f.callback = on_read_finish;
m_activeReads.insert(file); m_activeReads.insert(file);
if (m_fileThreadPool is null) threadSetup();
m_fileThreadPool = StaticTaskPool.addRef();
log("start read task"); log("start read task");
try { try {
m_fileThreadPool.put(task!(taskFun!("read", ubyte))(this, file, offset, buffer)); m_fileThreadPool.put(task!(taskFun!("read", ubyte))(this, file, offset, buffer));
@ -330,6 +333,18 @@ log("ready event");
m_waiting = true; m_waiting = true;
} }
} }
private void threadSetup()
{
if (m_readyEvent == EventID.invalid) {
log("create file event");
m_readyEvent = m_events.create();
}
if (m_fileThreadPool is null) {
log("aquire thread pool");
m_fileThreadPool = StaticTaskPool.addRef();
}
}
} }
private auto safeAtomicLoad(T)(ref shared(T) v) @trusted { return atomicLoad(v); } private auto safeAtomicLoad(T)(ref shared(T) v) @trusted { return atomicLoad(v); }