Skip to content

Commit

Permalink
fix MT problem regarding Process creation
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed Aug 16, 2022
1 parent 2dd0d6a commit 31b1d5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/ahk_x11.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions src/run/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 31b1d5c

Please sign in to comment.