Sha256: 0440ea9f521602a96c383db4d0d50703b5d679366f374333bcd817799929dbbf

Contents?: true

Size: 908 Bytes

Versions: 11

Compression:

Stored size: 908 Bytes

Contents

#--
#           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#                   Version 2, December 2004
#
#           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
#  0. You just DO WHAT THE FUCK YOU WANT TO.
#++

require 'thread'

# A recursive mutex lets you lock in various threads recursively, allowing
# you to do multiple locks one inside another.
#
# You really shouldn't use this, but in some cases it makes your life easier.
class RecursiveMutex < Mutex
	def initialize
		@threads_lock = Mutex.new
		@threads = Hash.new { |h, k| h[k] = 0 }

		super
	end

	# Lock the mutex.
	def lock
		super if @threads_lock.synchronize{ (@threads[Thread.current] += 1) == 1 }
	end

	# Unlock the mutex.
	def unlock
		if @threads_lock.synchronize{ (@threads[Thread.current] -= 1) == 0 }
			@threads.delete(Thread.current)

			super
		end
	end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
thread-0.2.2 lib/thread/recursive_mutex.rb
thread-0.2.1 lib/thread/recursive_mutex.rb
seekingalpha_thread-1.0.1 lib/thread/recursive_mutex.rb
thread-0.2.0 lib/thread/recursive_mutex.rb
thread-0.1.7 lib/thread/recursive_mutex.rb
thread-0.1.6 lib/thread/recursive_mutex.rb
thread-0.1.5 lib/thread/recursive_mutex.rb
thread-0.1.4 lib/thread/recursive_mutex.rb
thread-0.1.3 lib/thread/recursive_mutex.rb
thread-0.1.2 lib/thread/recursive_mutex.rb
vinted-thread-0.1.1 lib/thread/recursive_mutex.rb