Class: Libuv::Work

Inherits:
Q::DeferredPromise show all
Includes:
Listener, Resource
Defined in:
lib/libuv/work.rb

Constant Summary

Constants inherited from Q::Promise

Q::Promise::MAKE_PROMISE

Instance Attribute Summary collapse

Attributes inherited from Q::Promise

#trace

Instance Method Summary collapse

Methods included from Resource

#check_result, #check_result!, #resolve, #to_ptr

Methods inherited from Q::DeferredPromise

#resolved?, #then

Methods inherited from Q::Promise

#catch, #finally, #progress, #ruby_catch, #value

Constructor Details

#initialize(thread, work) ⇒ Work

Returns a new instance of Work.

Parameters:

  • thread (::Libuv::Reactor)

    thread this work request will be associated

  • work (Proc)

    callback to be called in the thread pool



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/libuv/work.rb', line 18

def initialize(thread, work)
    super(thread, thread.defer)

    @work = work
    @complete = false
    @pointer = ::Libuv::Ext.allocate_request_work
    @error = nil    # error in callback

    @instance_id = @pointer.address

    error = check_result ::Libuv::Ext.queue_work(@reactor, @pointer, callback(:on_work), callback(:on_complete))
    if error
        ::Libuv::Ext.free(@pointer)
        @complete = true
        @defer.reject(error)
    end
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/libuv/work.rb', line 8

def error
  @error
end

#resultObject (readonly)

Returns the value of attribute result.



9
10
11
# File 'lib/libuv/work.rb', line 9

def result
  @result
end

Instance Method Details

#canceltrue, false

Attempt to cancel the pending work. Returns true if the work has completed or was canceled.

Returns:

  • (true, false)


39
40
41
42
43
44
# File 'lib/libuv/work.rb', line 39

def cancel
    if not @complete
        @complete = ::Libuv::Ext.cancel(@pointer) >= 0
    end
    @complete
end

#completed?true, false

Indicates is the work has completed yet or not.

Returns:

  • (true, false)


49
50
51
# File 'lib/libuv/work.rb', line 49

def completed?
    return @complete
end