From 31b1d5c4f656d1a3c59edaf56ae3a0301bdaea87 Mon Sep 17 00:00:00 2001 From: phil294 Date: Tue, 16 Aug 2022 18:18:57 +0200 Subject: [PATCH] fix MT problem regarding Process creation --- src/ahk_x11.cr | 10 ++++++++-- src/run/runner.cr | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ahk_x11.cr b/src/ahk_x11.cr index 359329f..1b3f658 100644 --- a/src/ahk_x11.cr +++ b/src/ahk_x11.cr @@ -3,9 +3,15 @@ require "./build/builder" require "./run/runner" fun main(argc : Int32, argv : UInt8**) : Int32 + # It's also possible to run everything *without* `preview_mt` and spawn threads manually instead. + # This is mostly complete in the `gui-without-preview_mt` branch but because Channels don't work + # in MT, this needs suboptimal workarounds. Also, in that branch, gui glabel actions aren't + # working because the gui code is expected to modify the threads array from the main thread... + # It's all possible but rather ugly, so I went with MT for now. + # # Enforce 4 threads because less than that break the program. For now, this is the - # only way to enforce it. (1 = main, 2 = x11, 3 = gui, 4 = ? probably timer, 5 = actually the amount is undefined because stdlib may use `spawn`s several times. TODO:) - LibC.setenv("CRYSTAL_WORKERS", "8", 1) + # only way to enforce it. (1 = main, 2 = x11, 3 = gui, 4 = ? probably timer) + LibC.setenv("CRYSTAL_WORKERS", "4", 1) Crystal.main(argc, argv) end diff --git a/src/run/runner.cr b/src/run/runner.cr index 6fdd63a..d91f13e 100644 --- a/src/run/runner.cr +++ b/src/run/runner.cr @@ -54,8 +54,13 @@ module Run def run(*, hotkeys, hotstrings, auto_execute_section : Cmd::Base) hotkeys.each { |h| add_hotkey h } hotstrings.each { |h| add_hotstring h } - spawn @x11.run self, @settings.hotstring_end_chars # separate worker thread because event loop is blocking - spawn @gui.run # separate worker thread because gtk loop is blocking + # Cannot use normal mt `spawn` because https://github.com/crystal-lang/crystal/issues/12392 + ::Thread.new do + @x11.run self, @settings.hotstring_end_chars # separate worker thread because event loop is blocking + end + ::Thread.new do + @gui.run # separate worker thread because gtk loop is blocking + end spawn same_thread: true { clock } @auto_execute_thread = add_thread auto_execute_section, 0 end