Class: FDB::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/fdbimpl.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fpointer) ⇒ Future

Returns a new instance of Future.



316
317
318
319
# File 'lib/fdbimpl.rb', line 316

def initialize(fpointer)
  @fpointer = fpointer
  ObjectSpace.define_finalizer(self, FDB::Future.finalize(@fpointer))
end

Class Method Details

.finalize(ptr) ⇒ Object



309
310
311
312
313
314
# File 'lib/fdbimpl.rb', line 309

def self.finalize(ptr)
  proc do
    #puts "Destroying future #{ptr}"
    FDBC.fdb_future_destroy(ptr)
  end
end

.wait_for_any(*futures) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/fdbimpl.rb', line 365

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

Instance Method Details

#block_until_readyObject



329
330
331
# File 'lib/fdbimpl.rb', line 329

def block_until_ready
  FDBC.check_error FDBC.fdb_future_block_until_ready(@fpointer)
end

#callback_wrapper(f, &block) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
# File 'lib/fdbimpl.rb', line 334

def callback_wrapper(f, &block)
  begin
    yield f
  rescue Exception
    begin
      $stderr.puts "Discarding uncaught exception from user callback:"
      $stderr.puts "#{$@.first}: #{$!.message} (#{$!.class})", $@.drop(1).map{|s| "\t#{s}"}
    rescue Exception
    end
  end
end

#cancelObject



321
322
323
# File 'lib/fdbimpl.rb', line 321

def cancel
  FDBC.fdb_future_cancel(@fpointer)
end

#on_ready(&block) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/fdbimpl.rb', line 333

def on_ready(&block)
  def callback_wrapper(f, &block)
    begin
      yield f
    rescue Exception
      begin
        $stderr.puts "Discarding uncaught exception from user callback:"
        $stderr.puts "#{$@.first}: #{$!.message} (#{$!.class})", $@.drop(1).map{|s| "\t#{s}"}
      rescue Exception
      end
    end
  end

  entry = CallbackEntry.new

  FDB.cb_mutex.synchronize {
    pos = FDB.ffi_callbacks.length
    entry.index = pos
    FDB.ffi_callbacks << entry
  }

  entry.callback = FFI::Function.new(:void, [:pointer, :pointer]) do |ign1, ign2|
    FDB.cb_mutex.synchronize {
      FDB.ffi_callbacks[-1].index = entry.index
      FDB.ffi_callbacks[entry.index] = FDB.ffi_callbacks[-1]
      FDB.ffi_callbacks.pop
    }
    callback_wrapper(self, &block)
  end
  FDBC.fdb_future_set_callback(@fpointer, entry.callback, nil)
end

#ready?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/fdbimpl.rb', line 325

def ready?
  return !FDBC.fdb_future_is_ready(@fpointer).zero?
end