vendor/reentrant_mutex.rb in cli-ui-2.2.1 vs vendor/reentrant_mutex.rb in cli-ui-2.2.2

- old
+ new

@@ -20,56 +20,59 @@ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # Sourced from https://github.com/dotboris/reentrant_mutex +module CLI + module UI + class ReentrantMutex < Mutex + def initialize + @count_mutex = Mutex.new + @counts = Hash.new(0) -class ReentrantMutex < Mutex - def initialize - @count_mutex = Mutex.new - @counts = Hash.new(0) + super + end - super - end + def synchronize + raise ThreadError, 'Must be called with a block' unless block_given? - def synchronize - raise ThreadError, 'Must be called with a block' unless block_given? + begin + lock + yield + ensure + unlock + end + end - begin - lock - yield - ensure - unlock - end - end + def lock + c = increase_count Thread.current + super if c <= 1 + end - def lock - c = increase_count Thread.current - super if c <= 1 - end + def unlock + c = decrease_count Thread.current + if c <= 0 + super + delete_count Thread.current + end + end - def unlock - c = decrease_count Thread.current - if c <= 0 - super - delete_count Thread.current - end - end + def count + @count_mutex.synchronize { @counts[Thread.current] } + end - def count - @count_mutex.synchronize { @counts[Thread.current] } - end + private - private + def increase_count(thread) + @count_mutex.synchronize { @counts[thread] += 1 } + end - def increase_count(thread) - @count_mutex.synchronize { @counts[thread] += 1 } - end + def decrease_count(thread) + @count_mutex.synchronize { @counts[thread] -= 1 } + end - def decrease_count(thread) - @count_mutex.synchronize { @counts[thread] -= 1 } - end - - def delete_count(thread) - @count_mutex.synchronize { @counts.delete(thread) } + def delete_count(thread) + @count_mutex.synchronize { @counts.delete(thread) } + end + end end end