Class: Distem::Lib::Synchronization::SlidingWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/distem/distemlib/synchronization.rb

Instance Method Summary (collapse)

Constructor Details

- (SlidingWindow) initialize(size)

Returns a new instance of SlidingWindow



44
45
46
47
48
49
50
51
# File 'lib/distem/distemlib/synchronization.rb', line 44

def initialize(size)
  @queue = []
  @lock = Mutex.new
  @finished = false
  @size = size
  @tids = []
  @res = {}
end

Instance Method Details

- (Object) add(task, tag = nil)



53
54
55
# File 'lib/distem/distemlib/synchronization.rb', line 53

def add(task, tag = nil)
  @queue << [task, tag]
end

- (Object) kill



91
92
93
# File 'lib/distem/distemlib/synchronization.rb', line 91

def kill
  @tids.each { |tid| tid.kill }
end

- (Object) results



87
88
89
# File 'lib/distem/distemlib/synchronization.rb', line 87

def results
  @res
end

- (Object) run



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/distem/distemlib/synchronization.rb', line 57

def run
  @lock.synchronize {
    @queue = @queue.reverse
  }
  (1..@size).each {
    @tids << Thread.new {
      while !@finished do
        task = nil
        tag = nil
        @lock.synchronize {
          if @queue.size > 0
            task, tag = @queue.pop
          else
            @finished = true
          end
        }
        if task
          if task.is_a?(Proc)
            task.call
          else
            res = Shell.run_without_logging(task)
            @res[tag] = res if tag
          end
        end
      end
    }
  }
  @tids.each { |tid| tid.join }
end