Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login


class GLib::Cancellable

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

Class Methods

Gets the top cancellable from the stack.
  • Returns: a GLib::Cancellable from the top of the stack, or nil if the stack is empty

Creates a new GCancellable object.

Applications that want to start one or more operations that should be cancellable should create a GCancellable and pass it to the operations.

One GCancellable can be used in multiple consecutive operations, but not in multiple concurrent operations.

Instance Methods


Will set this GLib::Cancellable to cancelled, and will emit the GLib::Cancellable#cancelledself signal. (However, see the warning about race conditions in the documentation for that signal, if you are planning to connect to it.)

This function is thread-safe. In other words, you can safely call it from a thread other than the one running the operation that was passed the cancellable.

The convention within gio is that cancelling an asynchronous operation causes it to complete asynchronously. That is, if you cancel the operation from the same thread in which it is running, then the operation’s block will not be invoked until the application returns to the main loop.

  • Returns: self
Checks if a cancellable job has been cancelled.
connect{ }

Convenience function to connect to the GLib::Cancellable signal. Also handles the race condition that may happen if the cancellable is cancelled right before connecting.

The block is called at most once, either directly at the time of the connect if cancellable is already cancelled, or when cancellable is cancelled in some thread.

See GLib::Cancellable for details on how to use this.

Since 2.22

  • Returns: The id of the signal handler or 0 if cancellable has already been cancelled

Disconnects a handler from a cancellable instance similar to GLib::Instantiatable#signal_handler_disconnect. Additionally, in the event that a signal handler is currently running, this call will block until the handler has finished. Calling this function from a GLib::Cancellable signal handler will therefore result in a deadlock.

This avoids a race condition where a thread cancels at the same time as the cancellable operation is finished and the signal handler is removed. See GLib::Cancellable for details on how to use this.

This function does nothing if handler_id is 0.

Since 2.22

  • handler_id: Handler id of the handler to be disconnected

Gets the file descriptor for a cancellable job. This can be used to implement cancellable operations on Unix systems. The returned file descriptor will turn readable when cancellable is cancelled.

You are not supposed to read from the fd yourself, just check for readable status. Reading to unset the readable status is done with GLib::Cancellable#reset.

After a successful return from this function, you should use GLib::Cancellable#release_fd to free up resources allocated for the returned file descriptor.

See also GLib::Cancellable#make_pollfd.

  • Returns: A valid file descriptor, or -1 if the file descriptor is not supported, or on errors

Creates a GLib::PollFD corresponding to cancellable; this can be passed to Glib::poll and used to poll for cancellation. This is useful both for Unix systems without a native poll and for portability to Windows.

You should use GLib::Cancellable#release_fd to free up resources allocated for the GLib::PollFD.

If this function returns false, either no cancellable was given or resource limits prevent this function from allocating the necessary structures for polling. (On Linux, you will likely have reached the maximum number of file descriptors.) The suggested way to handle these cases is to ignore the cancellable.

You are not supposed to read from the fd yourself, just check for readable status. Reading to unset the readable status is done with GLib::Cancellable#reset.

Since 2.22

  • Returns: A Glib::PollFD, or nil if a GLib::PollFD couldn’t be created
Pops this GLib::Cancellable off the cancellable stack, if it’s at the top of the stack.
  • Returns: self

Pushes cancellable onto the cancellable stack. The current cancellable can then be recieved using GLib::Cancellable#get_current.

This is useful when implementing cancellable operations in code that does not allow you to pass down the cancellable object.

This is typically called automatically by, for example, GLib::File operations, so you rarely have to call this yourself.

  • Returns: self
Raise a GLib::IO::CancelledError, if this GLib::Cancellable has been cancelled.
  • Returns: self

Releases a resources previously allocated by GLib::Cancellable#fd or GLib::Cancellable#make_pollfd.

For compatibility reasons with older releases, calling this function is not strictly required, the resources will be automatically freed when the cancellable is finalized. However, the cancellable will block scarce file descriptors until it is finalized if this function is not called. This can cause the application to run out of file descriptors when many GLib::Cancellable objects are used at the same time.

Since 2.22

  • Returns: self
Resets this GLib::Cancellable to its uncancelled state.
  • Returns: self


cancelled: self

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 GLib::Instantiatable#signal_handler_disconnect 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: GLib::Cancellable#connect and GLib::Cancellable#disconnect that protect against problems like this.

An example of how to use this:

# Make sure we don't do any unnecessary work if already cancelled
return if cancellable and cancellable.cancelled?
id = 0
id = cancellable.connect{ … } if cancellable

# cancellable operation here…

cancellable.disconnect(id) if cancellable

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.