Module: Gtk

Defined in:
lib/gtk_thread_helper.rb

Constant Summary collapse

GTK_PENDING_BLOCKS =
[]
GTK_PENDING_BLOCKS_LOCK =
::Monitor.new
GTK_MAX_PENDING_BLOCKS_PER_ITERATION =
42

Class Method Summary collapse

Class Method Details

.main_with_queue(timeout = 100) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gtk_thread_helper.rb', line 59

def Gtk.main_with_queue timeout = 100
  Gtk.timeout_add timeout do
    GTK_PENDING_BLOCKS_LOCK.synchronize do
      length = GTK_PENDING_BLOCKS.length
      if length > GTK_MAX_PENDING_BLOCKS_PER_ITERATION
        GTK_PENDING_BLOCKS.shift(GTK_MAX_PENDING_BLOCKS_PER_ITERATION).each(&:call)
      elsif length > 0
        GTK_PENDING_BLOCKS.each(&:call).clear
      end
    end
    true
  end
  Gtk.main
end

.queue(&block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/gtk_thread_helper.rb', line 49

def Gtk.queue &block
  if Thread.current == Thread.main
    block.call
  else
    GTK_PENDING_BLOCKS_LOCK.synchronize do
      GTK_PENDING_BLOCKS << block
    end
  end
end