module TP2

def config(opts = nil, &app)

def config(opts = nil, &app)
  return @config if !opts && !app
  @config = opts || {}
  @config[:app] = app if app
end

def run(opts = nil, &app)

def run(opts = nil, &app)
  if @in_run
    @config = opts if opts
    @config[:app] = app if app
    return
  end
  opts ||= @config || {}
  begin
    @in_run = true
    machine = opts[:machine] || UM.new
    machine.puts(opts[:banner]) if opts[:banner]
    opts[:logger]&.info(message: "Running TP2 #{TP2::VERSION}, UringMachine #{UM::VERSION}, Ruby #{RUBY_VERSION}")
    server = Server.new(machine, opts, &app)
    setup_signal_handling(machine, Fiber.current)
    server.run
  ensure
    @in_run = false
  end
end

def setup_signal_handling(machine, fiber)

def setup_signal_handling(machine, fiber)
  queue = UM::Queue.new
  trap('SIGINT') { machine.push(queue, :SIGINT) }
  machine.spin { watch_for_int_signal(machine, queue, fiber) }
end

def watch_for_int_signal(machine, queue, fiber)

to be done
waits for signal from queue, then terminates given fiber
def watch_for_int_signal(machine, queue, fiber)
  machine.shift(queue)
  machine.schedule(fiber, UM::Terminate.new)
end