Skip to content

Instantly share code, notes, and snippets.

@2called-chaos
Last active August 29, 2015 14:27
Show Gist options
  • Save 2called-chaos/16aaab2aa6533fb2360b to your computer and use it in GitHub Desktop.
Save 2called-chaos/16aaab2aa6533fb2360b to your computer and use it in GitHub Desktop.
$deferred_importer = Mutex.new
$importing = []
$importing.extend(MonitorMixin)
def aquire_deferred_lock
t = Thread.new{
loop do
Thread.current[:lock] = $deferred_importer if $deferred_importer.try_lock
break if Thread.current[:lock]
sleep 1
end
Thread.current[:lock].lock rescue `say moe` # it raises here so IT LOCKED
}
end
def wait_for_workers
t = Thread.new{
loop do
l = $importing.synchronize { $importing.length }
return if l == 0
sleep 1
end
}
end
# ==================================
# = Wait for other workers to exit =
# ==================================
@status = ["waiting for other workers...", :yellow]
wchannel = var.wait_for_workers
second_progress(wchannel, "waiting for other workers (:seconds)...").join
may_interrupt
# =================
# = Aquiring lock =
# =================
@status = ["waiting for deferred import lock...", :yellow]
lchannel = var.aquire_deferred_lock
second_progress(lchannel, "waiting for deferred import lock (:seconds)...").join
may_interrupt
# Do some importing here (only one thread should be able to do this at once)
# My problem is that this doesn't seem to work, they get executed simultaneously
lchannel[:lock].unlock # they can't unlock though, they just keep hanging here as opposed to when aquiring it...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment