diff --git a/lib/sync.rb b/lib/sync.rb index bae05a4..ec19889 100644 --- a/lib/sync.rb +++ b/lib/sync.rb @@ -140,16 +140,22 @@ module Sync_m while true @sync_mutex.synchronize do - if sync_try_lock_sub(m) - return self - else - if sync_sh_locker[Thread.current] - sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]] - sync_sh_locker.delete(Thread.current) + begin + if sync_try_lock_sub(m) + return self else - sync_waiting.push Thread.current + if sync_sh_locker[Thread.current] + sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]] + sync_sh_locker.delete(Thread.current) + else + unless sync_waiting.include?(Thread.current) || sync_upgrade_waiting.reverse_each.any?{|w| w.first == Thread.current } + sync_waiting.push Thread.current + end + end + @sync_mutex.sleep end - @sync_mutex.sleep + ensure + sync_waiting.delete(Thread.current) end end end