Class: Dramatis::Runtime::Task::Continuation::Future

Inherits:
Object
  • Object
show all
Includes:
Dramatis
Defined in:
lib/dramatis/runtime/task.rb

Instance Method Summary collapse

Methods included from Dramatis

future, interface, release

Constructor Details

#initialize(name, call_thread) ⇒ Future

Returns a new instance of Future.



282
283
284
285
286
287
288
289
290
# File 'lib/dramatis/runtime/task.rb', line 282

def initialize name, call_thread
  @state = :start
  @mutex = Mutex.new
  @wait = ConditionVariable.new
  @call_thread = call_thread
  # warn "contiunation to #{actor}"
  @actor = interface( Dramatis::Runtime::Scheduler.actor ) \
    .send :continuation, self, :call_thread => call_thread
end

Instance Method Details

#continuation_exception(exception) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/dramatis/runtime/task.rb', line 374

def continuation_exception exception
  # warn "except rpc"
  @mutex.synchronize do
    raise "hell" if @state != :start and @state != :waiting
    @type = :exception
    @value = exception
    if @state == :start
      @state = :signaled
    else
      @state = :done
      Dramatis::Runtime::Scheduler.current.wakeup_notification self
      @wait.signal
    end
  end
end

#continuation_result(result) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/dramatis/runtime/task.rb', line 359

def continuation_result result
  @mutex.synchronize do
    raise "hell" if @state != :start and @state != :waiting
    @type = :return
    @value = result
    if @state == :start
      @state = :signaled
    else
      @state = :done
      Dramatis::Runtime::Scheduler.current.wakeup_notification self
      @wait.signal
    end
  end
end

#exception(exception) ⇒ Object



352
353
354
355
356
357
# File 'lib/dramatis/runtime/task.rb', line 352

def exception exception
  # warn "4 exception " + exception.to_s
  # warn "4 exception " + exception.backtrace.join("\n")
  @actor.exception exception
  # warn "4 delivered ".to_s
end

#queuedObject



344
345
346
# File 'lib/dramatis/runtime/task.rb', line 344

def queued
  Dramatis::Future.new( self )
end

#ready?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/dramatis/runtime/task.rb', line 292

def ready?
  @mutex.synchronize { @state == :done  or @state == :signaled }
end

#result(result) ⇒ Object



348
349
350
# File 'lib/dramatis/runtime/task.rb', line 348

def result result
  @actor.result result
end

#valueObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/dramatis/runtime/task.rb', line 296

def value
  @mutex.synchronize do
    if @state == :start
      @state = :waiting
      begin
        tag = to_s
        call_thread = @call_thread
        @actor.instance_eval do
          @actor.instance_eval do
            @call_thread = call_thread
          end
          @actor.gate.only [ :continuation, tag ], :tag => tag
          @actor.schedule self
        end
        begin
          Dramatis::Runtime::Scheduler.current.suspend_notification self
          @wait.wait @mutex
          # this causes a deadlock if the waking thread, which may be
          # retiring, does so before this thead has awakend and notified
          # the scheduler
          # sleep 1
        ensure
          # Dramatis::Runtime::Scheduler.current.wakeup_notification self
        end
      ensure
        tag = to_s
        @actor.instance_eval do
          @actor.gate.default_by_tag tag
        end
      end
      raise "hell" if @state != :done
    end
  end

  raise "hell " + @type.inspect if ![ :return, :exception ].include? @type
  case @type
  when :return
    return @value
  when :exception
    begin
      # raise "hell for #{@value}"
    rescue Exception => e
      pp "#{e}", e.backtrace
    end
    raise @value
  end
end