Skip to content

Commit

Permalink
tools: avoid nested lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
dbartolini committed May 18, 2024
1 parent dc9ddb2 commit a2062b8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
1 change: 0 additions & 1 deletion scripts/uncrustify/format-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ format_tools_vala () {
| grep -v 'data_compiler.vala' \
| grep -v 'deploy_dialog.vala' \
| grep -v 'font_import_dialog.vala' \
| grep -v 'launcher.vala' \
| grep -v 'level.vala' \
| grep -v 'level_editor.vala' \
| grep -v 'level_tree_view.vala' \
Expand Down
89 changes: 46 additions & 43 deletions tools/level_editor/launcher.vala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,51 @@ public static void wait_async_ready_callback(Object? source_object, AsyncResult
}
}

public static void child_watch_function(GLib.Pid pid, int wait_status)
{
// When the monitored child terminates, terminate all the
// processes it spawned.
try {
if (GLib.Process.check_exit_status(wait_status)) {
int exit_status;
#if CROWN_PLATFORM_WINDOWS
exit_status = wait_status;
#else
exit_status = Process.exit_status(wait_status);
#endif
logi("Child PID %s terminated with status %d".printf(pid.to_string(), exit_status));
}
} catch (GLib.Error e) {
loge(e.message);
}
GLib.Process.close_pid(pid);

uint wait_timer_id = 0;
GLib.Cancellable cancellable = new GLib.Cancellable();
if (subprocesses.size > 0) {
// Spawn a watchdog to cancel all wait_async() after a while.
uint interval_ms = 500;
wait_timer_id = GLib.Timeout.add(interval_ms, () => {
cancellable.cancel();
return GLib.Source.REMOVE;
});

logi("Waiting %ums for %d subprocesses to terminate...".printf(interval_ms, subprocesses.size));
}

if (wait_timer_id > 0) {
// Wait asynchronusly for children to terminate.
foreach (var subproc in subprocesses)
subproc.wait_async.begin(cancellable, wait_async_ready_callback);
} else {
// Force children to exit.
foreach (var subproc in subprocesses)
subproc.force_exit();

loop.quit();
}
}

public static int launcher_main(string[] args)
{
loop = new GLib.MainLoop (null, false);
Expand Down Expand Up @@ -184,49 +229,7 @@ public static int launcher_main(string[] args)

// Monitor child for termination.
// This requires one of GLib.Process.spawn*().
uint event_source = GLib.ChildWatch.add(child_pid, (pid, wait_status) => {
// When the monitored child terminates, terminate all the
// processes it spawned.
try {
if (GLib.Process.check_exit_status(wait_status)) {
int exit_status;
#if CROWN_PLATFORM_WINDOWS
exit_status = wait_status;
#else
exit_status = Process.exit_status(wait_status);
#endif
logi("Child PID %s terminated with status %d".printf(pid.to_string(), exit_status));
}
} catch (GLib.Error e) {
loge(e.message);
}
GLib.Process.close_pid(pid);

uint wait_timer_id = 0;
GLib.Cancellable cancellable = new GLib.Cancellable();
if (subprocesses.size > 0) {
// Spawn a watchdog to cancel all wait_async() after a while.
uint interval_ms = 500;
wait_timer_id = GLib.Timeout.add(interval_ms, () => {
cancellable.cancel();
return GLib.Source.REMOVE;
});

logi("Waiting %ums for %d subprocesses to terminate...".printf(interval_ms, subprocesses.size));
}

if (wait_timer_id > 0) {
// Wait asynchronusly for children to terminate.
foreach (var subproc in subprocesses)
subproc.wait_async.begin(cancellable, wait_async_ready_callback);
} else {
// Force children to exit.
foreach (var subproc in subprocesses)
subproc.force_exit();

loop.quit();
}
});
uint event_source = GLib.ChildWatch.add(child_pid, child_watch_function);

if (event_source <= 0) {
loge("Failed to create child watch");
Expand Down

0 comments on commit a2062b8

Please sign in to comment.