Class: FDB::Future

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

Direct Known Subclasses

FutureKeyValueArray, FutureNil, LazyFuture

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fpointer) ⇒ Future

Returns a new instance of Future.



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

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

Class Method Details

.finalize(ptr) ⇒ Object



302
303
304
305
306
307
# File 'lib/fdbimpl.rb', line 302

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

.wait_for_any(*futures) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/fdbimpl.rb', line 358

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



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

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

#callback_wrapper(f, &block) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
# File 'lib/fdbimpl.rb', line 327

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



314
315
316
# File 'lib/fdbimpl.rb', line 314

def cancel
  FDBC.fdb_future_cancel(@fpointer)
end

#on_ready(&block) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/fdbimpl.rb', line 326

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)


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

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