gio/auto/
cancellable.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::ffi;
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// `GCancellable` allows operations to be cancelled.
10    ///
11    /// `GCancellable` is a thread-safe operation cancellation stack used
12    /// throughout GIO to allow for cancellation of synchronous and
13    /// asynchronous operations.
14    ///
15    /// ## Signals
16    ///
17    ///
18    /// #### `cancelled`
19    ///  Emitted when the operation has been cancelled.
20    ///
21    /// Can be used by implementations of cancellable operations. If the
22    /// operation is cancelled from another thread, the signal will be
23    /// emitted in the thread that cancelled the operation, not the
24    /// thread that is running the operation.
25    ///
26    /// Note that disconnecting from this signal (or any signal) in a
27    /// multi-threaded program is prone to race conditions. For instance
28    /// it is possible that a signal handler may be invoked even after
29    /// a call to g_signal_handler_disconnect() for that handler has
30    /// already returned.
31    ///
32    /// There is also a problem when cancellation happens right before
33    /// connecting to the signal. If this happens the signal will
34    /// unexpectedly not be emitted, and checking before connecting to
35    /// the signal leaves a race condition where this is still happening.
36    ///
37    /// In order to make it safe and easy to connect handlers there
38    /// are two helper functions: g_cancellable_connect() and
39    /// g_cancellable_disconnect() which protect against problems
40    /// like this.
41    ///
42    /// An example of how to us this:
43    ///
44    ///
45    /// **⚠️ The following code is in C ⚠️**
46    ///
47    /// ```C
48    ///     // Make sure we don't do unnecessary work if already cancelled
49    ///     if (g_cancellable_set_error_if_cancelled (cancellable, error))
50    ///       return;
51    ///
52    ///     // Set up all the data needed to be able to handle cancellation
53    ///     // of the operation
54    ///     my_data = my_data_new (...);
55    ///
56    ///     id = 0;
57    ///     if (cancellable)
58    ///       id = g_cancellable_connect (cancellable,
59    ///                       G_CALLBACK (cancelled_handler)
60    ///                       data, NULL);
61    ///
62    ///     // cancellable operation here...
63    ///
64    ///     g_cancellable_disconnect (cancellable, id);
65    ///
66    ///     // cancelled_handler is never called after this, it is now safe
67    ///     // to free the data
68    ///     my_data_free (my_data);
69    /// ```
70    ///
71    /// Note that the cancelled signal is emitted in the thread that
72    /// the user cancelled from, which may be the main thread. So, the
73    /// cancellable signal should not do something that can block.
74    ///
75    ///
76    ///
77    /// # Implements
78    ///
79    /// [`CancellableExt`][trait@crate::prelude::CancellableExt], [`trait@glib::ObjectExt`], [`CancellableExtManual`][trait@crate::prelude::CancellableExtManual]
80    #[doc(alias = "GCancellable")]
81    pub struct Cancellable(Object<ffi::GCancellable, ffi::GCancellableClass>);
82
83    match fn {
84        type_ => || ffi::g_cancellable_get_type(),
85    }
86}
87
88impl Cancellable {
89    pub const NONE: Option<&'static Cancellable> = None;
90
91    /// Creates a new #GCancellable object.
92    ///
93    /// Applications that want to start one or more operations
94    /// that should be cancellable should create a #GCancellable
95    /// and pass it to the operations.
96    ///
97    /// One #GCancellable can be used in multiple consecutive
98    /// operations or in multiple concurrent operations.
99    ///
100    /// # Returns
101    ///
102    /// a #GCancellable.
103    #[doc(alias = "g_cancellable_new")]
104    pub fn new() -> Cancellable {
105        unsafe { from_glib_full(ffi::g_cancellable_new()) }
106    }
107
108    /// Gets the top cancellable from the stack.
109    ///
110    /// # Returns
111    ///
112    /// a #GCancellable from the top
113    /// of the stack, or [`None`] if the stack is empty.
114    #[doc(alias = "g_cancellable_get_current")]
115    #[doc(alias = "get_current")]
116    pub fn current() -> Option<Cancellable> {
117        unsafe { from_glib_none(ffi::g_cancellable_get_current()) }
118    }
119}
120
121impl Default for Cancellable {
122    fn default() -> Self {
123        Self::new()
124    }
125}
126
127unsafe impl Send for Cancellable {}
128unsafe impl Sync for Cancellable {}
129
130/// Trait containing all [`struct@Cancellable`] methods.
131///
132/// # Implementors
133///
134/// [`Cancellable`][struct@crate::Cancellable]
135pub trait CancellableExt: IsA<Cancellable> + 'static {
136    /// Will set @self to cancelled, and will emit the
137    /// #GCancellable::cancelled signal. (However, see the warning about
138    /// race conditions in the documentation for that signal if you are
139    /// planning to connect to it.)
140    ///
141    /// This function is thread-safe. In other words, you can safely call
142    /// it from a thread other than the one running the operation that was
143    /// passed the @self.
144    ///
145    /// If @self is [`None`], this function returns immediately for convenience.
146    ///
147    /// The convention within GIO is that cancelling an asynchronous
148    /// operation causes it to complete asynchronously. That is, if you
149    /// cancel the operation from the same thread in which it is running,
150    /// then the operation's #GAsyncReadyCallback will not be invoked until
151    /// the application returns to the main loop.
152    #[doc(alias = "g_cancellable_cancel")]
153    fn cancel(&self) {
154        unsafe {
155            ffi::g_cancellable_cancel(self.as_ref().to_glib_none().0);
156        }
157    }
158
159    /// Gets the file descriptor for a cancellable job. This can be used to
160    /// implement cancellable operations on Unix systems. The returned fd will
161    /// turn readable when @self is cancelled.
162    ///
163    /// You are not supposed to read from the fd yourself, just check for
164    /// readable status. Reading to unset the readable status is done
165    /// with g_cancellable_reset().
166    ///
167    /// After a successful return from this function, you should use
168    /// g_cancellable_release_fd() to free up resources allocated for
169    /// the returned file descriptor.
170    ///
171    /// See also g_cancellable_make_pollfd().
172    ///
173    /// # Returns
174    ///
175    /// A valid file descriptor. `-1` if the file descriptor
176    /// is not supported, or on errors.
177    #[doc(alias = "g_cancellable_get_fd")]
178    #[doc(alias = "get_fd")]
179    fn fd(&self) -> i32 {
180        unsafe { ffi::g_cancellable_get_fd(self.as_ref().to_glib_none().0) }
181    }
182
183    /// Checks if a cancellable job has been cancelled.
184    ///
185    /// # Returns
186    ///
187    /// [`true`] if @self is cancelled,
188    /// FALSE if called with [`None`] or if item is not cancelled.
189    #[doc(alias = "g_cancellable_is_cancelled")]
190    fn is_cancelled(&self) -> bool {
191        unsafe {
192            from_glib(ffi::g_cancellable_is_cancelled(
193                self.as_ref().to_glib_none().0,
194            ))
195        }
196    }
197
198    //#[doc(alias = "g_cancellable_make_pollfd")]
199    //fn make_pollfd(&self, pollfd: /*Ignored*/&mut glib::PollFD) -> bool {
200    //    unsafe { TODO: call ffi:g_cancellable_make_pollfd() }
201    //}
202
203    /// Pops @self off the cancellable stack (verifying that @self
204    /// is on the top of the stack).
205    #[doc(alias = "g_cancellable_pop_current")]
206    fn pop_current(&self) {
207        unsafe {
208            ffi::g_cancellable_pop_current(self.as_ref().to_glib_none().0);
209        }
210    }
211
212    /// Pushes @self onto the cancellable stack. The current
213    /// cancellable can then be received using g_cancellable_get_current().
214    ///
215    /// This is useful when implementing cancellable operations in
216    /// code that does not allow you to pass down the cancellable object.
217    ///
218    /// This is typically called automatically by e.g. #GFile operations,
219    /// so you rarely have to call this yourself.
220    #[doc(alias = "g_cancellable_push_current")]
221    fn push_current(&self) {
222        unsafe {
223            ffi::g_cancellable_push_current(self.as_ref().to_glib_none().0);
224        }
225    }
226
227    /// Releases a resources previously allocated by g_cancellable_get_fd()
228    /// or g_cancellable_make_pollfd().
229    ///
230    /// For compatibility reasons with older releases, calling this function
231    /// is not strictly required, the resources will be automatically freed
232    /// when the @self is finalized. However, the @self will
233    /// block scarce file descriptors until it is finalized if this function
234    /// is not called. This can cause the application to run out of file
235    /// descriptors when many #GCancellables are used at the same time.
236    #[doc(alias = "g_cancellable_release_fd")]
237    fn release_fd(&self) {
238        unsafe {
239            ffi::g_cancellable_release_fd(self.as_ref().to_glib_none().0);
240        }
241    }
242}
243
244impl<O: IsA<Cancellable>> CancellableExt for O {}