Class: Semian::ThreadSafe::SlidingWindow

Inherits:
Simple::SlidingWindow show all
Defined in:
lib/semian/simple_sliding_window.rb

Instance Attribute Summary

Attributes inherited from Simple::SlidingWindow

#max_size

Instance Method Summary collapse

Methods inherited from Simple::SlidingWindow

#clear

Constructor Details

#initializeSlidingWindow

Returns a new instance of SlidingWindow.



49
50
51
52
# File 'lib/semian/simple_sliding_window.rb', line 49

def initialize(**)
  super
  @lock = Mutex.new
end

Instance Method Details

#pushObject



65
66
67
# File 'lib/semian/simple_sliding_window.rb', line 65

def push(*)
  @lock.synchronize { super }
end

#reject!Object

#size, #last, and #clear are not wrapped in a mutex. For the first two, the worst-case is a thread-switch at a timing where they’d receive an out-of-date value–which could happen with a mutex as well.

As for clear, it’s an all or nothing operation. Doesn’t matter if we have the lock or not.



61
62
63
# File 'lib/semian/simple_sliding_window.rb', line 61

def reject!(*)
  @lock.synchronize { super }
end