Class: FDB::Future
- Inherits:
-
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.
308
309
310
311
|
# File 'lib/fdbimpl.rb', line 308
def initialize(fpointer)
@fpointer = fpointer
ObjectSpace.define_finalizer(self, FDB::Future.finalize(@fpointer))
end
|
Class Method Details
.finalize(ptr) ⇒ Object
301
302
303
304
305
306
|
# File 'lib/fdbimpl.rb', line 301
def self.finalize(ptr)
proc do
FDBC.fdb_future_destroy(ptr)
end
end
|
.wait_for_any(*futures) ⇒ Object
357
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
|
# File 'lib/fdbimpl.rb', line 357
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_ready ⇒ Object
321
322
323
|
# File 'lib/fdbimpl.rb', line 321
def block_until_ready
FDBC.check_error FDBC.fdb_future_block_until_ready(@fpointer)
end
|
#callback_wrapper(f, &block) ⇒ Object
326
327
328
329
330
331
332
333
334
335
336
|
# File 'lib/fdbimpl.rb', line 326
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
|
#cancel ⇒ Object
313
314
315
|
# File 'lib/fdbimpl.rb', line 313
def cancel
FDBC.fdb_future_cancel(@fpointer)
end
|
#on_ready(&block) ⇒ Object
325
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
|
# File 'lib/fdbimpl.rb', line 325
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
317
318
319
|
# File 'lib/fdbimpl.rb', line 317
def ready?
return !FDBC.fdb_future_is_ready(@fpointer).zero?
end
|