Skip to content

Instantly share code, notes, and snippets.

@darkhelmet
Created August 29, 2009 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkhelmet/177751 to your computer and use it in GitHub Desktop.
Save darkhelmet/177751 to your computer and use it in GitHub Desktop.
require "thread"
module Gtk
PENDING_CALLS_MUTEX = Mutex.new
PENDING_CALLS = []
def self.thread_protect(&proc)
if Thread.current == Thread.main
proc.call
else
PENDING_CALLS_MUTEX.synchronize do
PENDING_CALLS << proc
end
end
end
def self.thread_flush
if PENDING_CALLS_MUTEX.try_lock
PENDING_CALLS.each { |closure| closure.call }
PENDING_CALLS.clear
PENDING_CALLS_MUTEX.unlock
end
end
def self.init_thread_protect
Gtk.timeout_add(100) do
PENDING_CALLS_MUTEX.synchronize do
PENDING_CALLS.each { |closure| closure.call }
PENDING_CALLS.clear
end
true
end
end
end
require "thread"
module Gtk
# Thread-safety stuff.
# Loosely based on booh, by Guillaume Cottenceau.
PENDING_CALLS_MUTEX = Mutex.new
PENDING_CALLS = []
def self.thread_protect(&proc)
if Thread.current == Thread.main
proc.call
else
PENDING_CALLS_MUTEX.synchronize do
PENDING_CALLS << proc
end
end
end
def self.thread_flush
if PENDING_CALLS_MUTEX.try_lock
for closure in PENDING_CALLS
closure.call
end
PENDING_CALLS.clear
PENDING_CALLS_MUTEX.unlock
end
end
def self.init_thread_protect
Gtk.timeout_add(100) do
PENDING_CALLS_MUTEX.synchronize do
for closure in PENDING_CALLS
closure.call
end
PENDING_CALLS.clear
end
true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment