GCancellable: GCancellable

Description Methods and Functions Hierarchy Detailed Description Structures Convenient Construction Signals Author(s) References

Description

Thread-safe Operation Cancellation Stack

Methods and Functions

gCancellableNew()
gCancellableIsCancelled(object)
gCancellableSetErrorIfCancelled(object, .errwarn = TRUE)
gCancellableGetFd(object)
gCancellableReleaseFd(object)
gCancellableGetCurrent()
gCancellablePopCurrent(object)
gCancellablePushCurrent(object)
gCancellableReset(object)
gCancellableDisconnect(object, handler.id)
gCancellableCancel(object)
gCancellable()

Hierarchy

1
2

Detailed Description

GCancellable is a thread-safe operation cancellation stack used throughout GIO to allow for cancellation of synchronous and asynchronous operations.

Structures

GCancellable

Allows actions to be cancelled.

Convenient Construction

gCancellable is the equivalent of gCancellableNew.

Signals

cancelled(cancellable, user.data)

Emitted when the operation has been cancelled.

Can be used by implementations of cancellable operations. If the operation is cancelled from another thread, the signal will be emitted in the thread that cancelled the operation, not the thread that is running the operation.

Note that disconnecting from this signal (or any signal) in a multi-threaded program is prone to race conditions. For instance it is possible that a signal handler may be invoked even after a call to gSignalHandlerDisconnect for that handler has already returned.

There is also a problem when cancellation happen right before connecting to the signal. If this happens the signal will unexpectedly not be emitted, and checking before connecting to the signal leaves a race condition where this is still happening.

In order to make it safe and easy to connect handlers there are two helper functions: gCancellableConnect() and gCancellableDisconnect which protect against problems like this.

An example of how to us this:

## Make sure we don't do any unnecessary work if already cancelled
if (cancellable$setErrorIfCancelled())
  return()
## Set up all the data needed to be able to
## handle cancellation of the operation
my_data <- myData(...)

id <- 0
if (!is.null(cancellable))
  id <- cancellable$connect(cancelled_handler, data, NULL)

## cancellable operation here...

cancellable$disconnect(id)

Note that the cancelled signal is emitted in the thread that the user cancelled from, which may be the main thread. So, the cancellable signal should not do something that can block.

cancellable

a GCancellable.

user.data

user data set when the signal handler was connected.

Author(s)

Derived by RGtkGen from GTK+ documentation

References

https://developer.gnome.org/gio/stable/GCancellable.html


RGtk2 documentation built on Oct. 14, 2021, 5:08 p.m.

Related to GCancellable in RGtk2...