Fix indentation and remove unused imports/variables.

This commit is contained in:
Sönke Ludwig 2019-08-23 19:54:55 +02:00
parent 5c3afcc175
commit 20373d10db

View file

@ -16,10 +16,8 @@ private enum SIGCHLD = 17;
final class PosixEventDriverProcesses(Loop : PosixEventLoop) : EventDriverProcesses { final class PosixEventDriverProcesses(Loop : PosixEventLoop) : EventDriverProcesses {
@safe: /*@nogc:*/ nothrow: @safe: /*@nogc:*/ nothrow:
import core.stdc.errno : errno, EAGAIN, EINPROGRESS;
import core.sync.mutex : Mutex; import core.sync.mutex : Mutex;
import core.sys.linux.sys.signalfd; import core.sys.posix.unistd : dup;
import core.sys.posix.unistd : close, read, write, dup;
import core.thread : Thread; import core.thread : Thread;
private { private {
@ -34,8 +32,6 @@ final class PosixEventDriverProcesses(Loop : PosixEventLoop) : EventDriverProces
this(Loop loop, EventDriver driver) this(Loop loop, EventDriver driver)
{ {
import core.sys.posix.signal;
m_loop = loop; m_loop = loop;
m_driver = driver; m_driver = driver;
} }
@ -302,85 +298,83 @@ final class PosixEventDriverProcesses(Loop : PosixEventLoop) : EventDriverProces
fn(info); fn(info);
} }
private static void add(ProcessID pid, ProcessInfo info) @trusted { private static void add(ProcessID pid, ProcessInfo info) @trusted {
s_mutex.lock_nothrow(); s_mutex.lock_nothrow();
scope (exit) s_mutex.unlock_nothrow(); scope (exit) s_mutex.unlock_nothrow();
if (!s_waitThread) { if (!s_waitThread) {
s_waitThread = new Thread(&waitForProcesses); s_waitThread = new Thread(&waitForProcesses);
s_waitThread.start(); s_waitThread.start();
}
assert(pid !in s_processes, "Process adopted twice");
s_processes[pid] = info;
} }
private static void waitForProcesses() assert(pid !in s_processes, "Process adopted twice");
@system { s_processes[pid] = info;
import core.stdc.errno : ECHILD, errno; }
import core.sys.posix.sys.wait : idtype_t, WNOHANG, WNOWAIT, WEXITED, WEXITSTATUS, WIFEXITED, WTERMSIG, waitid, waitpid;
import core.sys.posix.signal : siginfo_t;
while (true) { private static void waitForProcesses()
siginfo_t dummy; @system {
auto ret = waitid(idtype_t.P_ALL, -1, &dummy, WEXITED|WNOWAIT); import core.sys.posix.sys.wait : idtype_t, WNOHANG, WNOWAIT, WEXITED, WEXITSTATUS, WIFEXITED, WTERMSIG, waitid, waitpid;
if (ret == -1) { import core.sys.posix.signal : siginfo_t;
{
s_mutex.lock_nothrow();
scope (exit) s_mutex.unlock_nothrow();
s_waitThread = null;
}
break;
}
ProcessID[] allprocs;
while (true) {
siginfo_t dummy;
auto ret = waitid(idtype_t.P_ALL, -1, &dummy, WEXITED|WNOWAIT);
if (ret == -1) {
{ {
s_mutex.lock_nothrow(); s_mutex.lock_nothrow();
scope (exit) s_mutex.unlock_nothrow(); scope (exit) s_mutex.unlock_nothrow();
s_waitThread = null;
() @trusted {
foreach (ref entry; s_processes.byKeyValue) {
if (!entry.value.exited)
allprocs ~= entry.key;
}
} ();
} }
break;
}
foreach (pid; allprocs) { ProcessID[] allprocs;
int status;
ret = () @trusted { return waitpid(cast(int)pid, &status, WNOHANG); } (); {
if (ret == cast(int)pid) { s_mutex.lock_nothrow();
int exitstatus = WIFEXITED(status) ? WEXITSTATUS(status) : -WTERMSIG(status); scope (exit) s_mutex.unlock_nothrow();
onProcessExitStatic(ret, exitstatus);
() @trusted {
foreach (ref entry; s_processes.byKeyValue) {
if (!entry.value.exited)
allprocs ~= entry.key;
} }
} ();
}
foreach (pid; allprocs) {
int status;
ret = () @trusted { return waitpid(cast(int)pid, &status, WNOHANG); } ();
if (ret == cast(int)pid) {
int exitstatus = WIFEXITED(status) ? WEXITSTATUS(status) : -WTERMSIG(status);
onProcessExitStatic(ret, exitstatus);
} }
} }
} }
}
private static void onProcessExitStatic(int system_pid, int exit_status) private static void onProcessExitStatic(int system_pid, int exit_status)
{ {
auto pid = cast(ProcessID)system_pid; auto pid = cast(ProcessID)system_pid;
ProcessWaitCallback[] callbacks; PosixEventDriverProcesses driver;
PosixEventDriverProcesses driver; lockedProcessInfo(pid, (ProcessInfo* info) @safe {
lockedProcessInfo(pid, (ProcessInfo* info) @safe { // We get notified of any child exiting, so ignore the ones we're
// We get notified of any child exiting, so ignore the ones we're // not aware of
// not aware of if (info is null) return;
if (info is null) return;
// Increment the ref count to make sure it doesn't get removed // Increment the ref count to make sure it doesn't get removed
info.refCount++; info.refCount++;
info.exited = true; info.exited = true;
info.exitCode = exit_status; info.exitCode = exit_status;
driver = info.driver; driver = info.driver;
}); });
if (driver) if (driver)
() @trusted { return cast(shared)driver; } ().onProcessExit(cast(int)pid); () @trusted { return cast(shared)driver; } ().onProcessExit(cast(int)pid);
} }
private static struct ProcessInfo { private static struct ProcessInfo {
bool exited = true; bool exited = true;