class FDB::Future

def self.wait_for_any(*futures)

def self.wait_for_any(*futures)
  if futures.empty?
    raise ArgumentError, "wait_for_any requires at least one future"
  end
  mx = Mutex.new
  cv = ConditionVariable.new
  ready_idx = -1
  futures.each_with_index do |f, i|
    f.on_ready do |f|
      mx.synchronize {
        if ready_idx < 0
          ready_idx = i
          cv.signal
        end
      }
    end
  end
  mx.synchronize {
    if ready_idx < 0
      cv.wait mx
    end
  }
  ready_idx
end