glib/auto/
main_context.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
5#[cfg(feature = "v2_72")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v2_72")))]
7use crate::MainContextFlags;
8use crate::{ffi, translate::*};
9
10crate::wrapper! {
11    /// The `GMainContext` struct is an opaque data
12    /// type representing a set of sources to be handled in a main loop.
13    // rustdoc-stripper-ignore-next-stop
14    /// The `GMainContext` struct is an opaque data
15    /// type representing a set of sources to be handled in a main loop.
16    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
17    pub struct MainContext(Shared<ffi::GMainContext>);
18
19    match fn {
20        ref => |ptr| ffi::g_main_context_ref(ptr),
21        unref => |ptr| ffi::g_main_context_unref(ptr),
22        type_ => || ffi::g_main_context_get_type(),
23    }
24}
25
26impl MainContext {
27    /// Creates a new [`MainContext`][crate::MainContext] structure.
28    ///
29    /// # Returns
30    ///
31    /// the new main context
32    // rustdoc-stripper-ignore-next-stop
33    /// Creates a new [`MainContext`][crate::MainContext] structure.
34    ///
35    /// # Returns
36    ///
37    /// the new main context
38    #[doc(alias = "g_main_context_new")]
39    pub fn new() -> MainContext {
40        unsafe { from_glib_full(ffi::g_main_context_new()) }
41    }
42
43    /// Creates a new [`MainContext`][crate::MainContext] structure.
44    /// ## `flags`
45    /// a bitwise-OR combination of flags that can only be set at creation
46    ///   time
47    ///
48    /// # Returns
49    ///
50    /// the new main context
51    // rustdoc-stripper-ignore-next-stop
52    /// Creates a new [`MainContext`][crate::MainContext] structure.
53    /// ## `flags`
54    /// a bitwise-OR combination of flags that can only be set at creation
55    ///   time
56    ///
57    /// # Returns
58    ///
59    /// the new main context
60    #[cfg(feature = "v2_72")]
61    #[cfg_attr(docsrs, doc(cfg(feature = "v2_72")))]
62    #[doc(alias = "g_main_context_new_with_flags")]
63    #[doc(alias = "new_with_flags")]
64    pub fn with_flags(flags: MainContextFlags) -> MainContext {
65        unsafe { from_glib_full(ffi::g_main_context_new_with_flags(flags.into_glib())) }
66    }
67
68    //#[doc(alias = "g_main_context_add_poll")]
69    //pub fn add_poll(&self, fd: /*Ignored*/&mut PollFD, priority: i32) {
70    //    unsafe { TODO: call ffi:g_main_context_add_poll() }
71    //}
72
73    //#[doc(alias = "g_main_context_check")]
74    //pub fn check(&self, max_priority: i32, fds: /*Ignored*/&[PollFD]) -> bool {
75    //    unsafe { TODO: call ffi:g_main_context_check() }
76    //}
77
78    /// Dispatches all pending sources.
79    ///
80    /// You must have successfully acquired the context with
81    /// [`acquire()`][Self::acquire()] before you may call this function.
82    ///
83    /// Since 2.76 @self can be `NULL` to use the global-default
84    /// main context.
85    // rustdoc-stripper-ignore-next-stop
86    /// Dispatches all pending sources.
87    ///
88    /// You must have successfully acquired the context with
89    /// [`acquire()`][Self::acquire()] before you may call this function.
90    ///
91    /// Since 2.76 @self can be `NULL` to use the global-default
92    /// main context.
93    #[doc(alias = "g_main_context_dispatch")]
94    pub fn dispatch(&self) {
95        unsafe {
96            ffi::g_main_context_dispatch(self.to_glib_none().0);
97        }
98    }
99
100    //#[doc(alias = "g_main_context_find_source_by_funcs_user_data")]
101    //pub fn find_source_by_funcs_user_data(&self, funcs: /*Ignored*/&mut SourceFuncs, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> Option<Source> {
102    //    unsafe { TODO: call ffi:g_main_context_find_source_by_funcs_user_data() }
103    //}
104
105    //#[doc(alias = "g_main_context_find_source_by_user_data")]
106    //pub fn find_source_by_user_data(&self, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> Option<Source> {
107    //    unsafe { TODO: call ffi:g_main_context_find_source_by_user_data() }
108    //}
109
110    //#[doc(alias = "g_main_context_get_poll_func")]
111    //#[doc(alias = "get_poll_func")]
112    //pub fn poll_func(&self) -> /*Unimplemented*/Fn(/*Ignored*/PollFD, u32) -> i32 {
113    //    unsafe { TODO: call ffi:g_main_context_get_poll_func() }
114    //}
115
116    /// Determines whether this thread holds the (recursive)
117    /// ownership of this [`MainContext`][crate::MainContext].
118    ///
119    /// This is useful to
120    /// know before waiting on another thread that may be
121    /// blocking to get ownership of @self.
122    ///
123    /// # Returns
124    ///
125    /// true if current thread is owner of @self, false otherwise
126    // rustdoc-stripper-ignore-next-stop
127    /// Determines whether this thread holds the (recursive)
128    /// ownership of this [`MainContext`][crate::MainContext].
129    ///
130    /// This is useful to
131    /// know before waiting on another thread that may be
132    /// blocking to get ownership of @self.
133    ///
134    /// # Returns
135    ///
136    /// true if current thread is owner of @self, false otherwise
137    #[doc(alias = "g_main_context_is_owner")]
138    pub fn is_owner(&self) -> bool {
139        unsafe { from_glib(ffi::g_main_context_is_owner(self.to_glib_none().0)) }
140    }
141
142    /// Runs a single iteration for the given main loop.
143    ///
144    /// This involves
145    /// checking to see if any event sources are ready to be processed,
146    /// then if no events sources are ready and @may_block is true, waiting
147    /// for a source to become ready, then dispatching the highest priority
148    /// events sources that are ready. Otherwise, if @may_block is false,
149    /// this function does not wait for sources to become ready, and only the highest
150    /// priority sources which are already ready (if any) will be dispatched.
151    ///
152    /// Note that even when @may_block is true, it is still possible for
153    /// [`iteration()`][Self::iteration()] to return false, since the wait may
154    /// be interrupted for other reasons than an event source becoming ready.
155    /// ## `may_block`
156    /// whether the call may block
157    ///
158    /// # Returns
159    ///
160    /// true if events were dispatched, false otherwise
161    // rustdoc-stripper-ignore-next-stop
162    /// Runs a single iteration for the given main loop.
163    ///
164    /// This involves
165    /// checking to see if any event sources are ready to be processed,
166    /// then if no events sources are ready and @may_block is true, waiting
167    /// for a source to become ready, then dispatching the highest priority
168    /// events sources that are ready. Otherwise, if @may_block is false,
169    /// this function does not wait for sources to become ready, and only the highest
170    /// priority sources which are already ready (if any) will be dispatched.
171    ///
172    /// Note that even when @may_block is true, it is still possible for
173    /// [`iteration()`][Self::iteration()] to return false, since the wait may
174    /// be interrupted for other reasons than an event source becoming ready.
175    /// ## `may_block`
176    /// whether the call may block
177    ///
178    /// # Returns
179    ///
180    /// true if events were dispatched, false otherwise
181    #[doc(alias = "g_main_context_iteration")]
182    pub fn iteration(&self, may_block: bool) -> bool {
183        unsafe {
184            from_glib(ffi::g_main_context_iteration(
185                self.to_glib_none().0,
186                may_block.into_glib(),
187            ))
188        }
189    }
190
191    /// Checks if any sources have pending events for the given context.
192    ///
193    /// # Returns
194    ///
195    /// true if events are pending, false otherwise
196    // rustdoc-stripper-ignore-next-stop
197    /// Checks if any sources have pending events for the given context.
198    ///
199    /// # Returns
200    ///
201    /// true if events are pending, false otherwise
202    #[doc(alias = "g_main_context_pending")]
203    pub fn pending(&self) -> bool {
204        unsafe { from_glib(ffi::g_main_context_pending(self.to_glib_none().0)) }
205    }
206
207    //#[cfg(feature = "v2_64")]
208    //#[cfg_attr(docsrs, doc(cfg(feature = "v2_64")))]
209    //#[doc(alias = "g_main_context_pusher_new")]
210    //pub fn pusher_new(&self) -> /*Unknown conversion*//*Unimplemented*/MainContextPusher {
211    //    unsafe { TODO: call ffi:g_main_context_pusher_new() }
212    //}
213
214    //#[doc(alias = "g_main_context_query")]
215    //pub fn query(&self, max_priority: i32, fds: /*Ignored*/Vec<PollFD>) -> (i32, i32) {
216    //    unsafe { TODO: call ffi:g_main_context_query() }
217    //}
218
219    //#[doc(alias = "g_main_context_remove_poll")]
220    //pub fn remove_poll(&self, fd: /*Ignored*/&mut PollFD) {
221    //    unsafe { TODO: call ffi:g_main_context_remove_poll() }
222    //}
223
224    //#[doc(alias = "g_main_context_set_poll_func")]
225    //pub fn set_poll_func(&self, func: /*Unimplemented*/Fn(/*Ignored*/PollFD, u32) -> i32) {
226    //    unsafe { TODO: call ffi:g_main_context_set_poll_func() }
227    //}
228
229    //#[cfg_attr(feature = "v2_58", deprecated = "Since 2.58")]
230    //#[allow(deprecated)]
231    //#[doc(alias = "g_main_context_wait")]
232    //pub fn wait(&self, cond: /*Ignored*/&mut Cond, mutex: /*Ignored*/&mut Mutex) -> bool {
233    //    unsafe { TODO: call ffi:g_main_context_wait() }
234    //}
235
236    /// Wake up @self if it’s currently blocking in
237    /// [`iteration()`][Self::iteration()], causing it to stop blocking.
238    ///
239    /// The @self could be blocking waiting for a source to become ready.
240    /// Otherwise, if @self is not currently blocking, this function causes the
241    /// next invocation of [`iteration()`][Self::iteration()] to return without
242    /// blocking.
243    ///
244    /// This API is useful for low-level control over [`MainContext`][crate::MainContext]; for
245    /// example, integrating it with main loop implementations such as
246    /// [`MainLoop`][crate::MainLoop].
247    ///
248    /// Another related use for this function is when implementing a main
249    /// loop with a termination condition, computed from multiple threads:
250    ///
251    /// **⚠️ The following code is in c ⚠️**
252    ///
253    /// ```c
254    ///   #define NUM_TASKS 10
255    ///   static gint tasks_remaining = NUM_TASKS;  // (atomic)
256    ///   ...
257    ///
258    ///   while (g_atomic_int_get (&tasks_remaining) != 0)
259    ///     g_main_context_iteration (NULL, TRUE);
260    /// ```
261    ///
262    /// Then in a thread:
263    /// **⚠️ The following code is in c ⚠️**
264    ///
265    /// ```c
266    ///   perform_work ();
267    ///
268    ///   if (g_atomic_int_dec_and_test (&tasks_remaining))
269    ///     g_main_context_wakeup (NULL);
270    /// ```
271    // rustdoc-stripper-ignore-next-stop
272    /// Wake up @self if it’s currently blocking in
273    /// [`iteration()`][Self::iteration()], causing it to stop blocking.
274    ///
275    /// The @self could be blocking waiting for a source to become ready.
276    /// Otherwise, if @self is not currently blocking, this function causes the
277    /// next invocation of [`iteration()`][Self::iteration()] to return without
278    /// blocking.
279    ///
280    /// This API is useful for low-level control over [`MainContext`][crate::MainContext]; for
281    /// example, integrating it with main loop implementations such as
282    /// [`MainLoop`][crate::MainLoop].
283    ///
284    /// Another related use for this function is when implementing a main
285    /// loop with a termination condition, computed from multiple threads:
286    ///
287    /// **⚠️ The following code is in c ⚠️**
288    ///
289    /// ```c
290    ///   #define NUM_TASKS 10
291    ///   static gint tasks_remaining = NUM_TASKS;  // (atomic)
292    ///   ...
293    ///
294    ///   while (g_atomic_int_get (&tasks_remaining) != 0)
295    ///     g_main_context_iteration (NULL, TRUE);
296    /// ```
297    ///
298    /// Then in a thread:
299    /// **⚠️ The following code is in c ⚠️**
300    ///
301    /// ```c
302    ///   perform_work ();
303    ///
304    ///   if (g_atomic_int_dec_and_test (&tasks_remaining))
305    ///     g_main_context_wakeup (NULL);
306    /// ```
307    #[doc(alias = "g_main_context_wakeup")]
308    pub fn wakeup(&self) {
309        unsafe {
310            ffi::g_main_context_wakeup(self.to_glib_none().0);
311        }
312    }
313
314    /// Returns the global-default main context.
315    ///
316    /// This is the main context
317    /// used for main loop functions when a main loop is not explicitly
318    /// specified, and corresponds to the ‘main’ main loop. See also
319    /// [`thread_default()`][Self::thread_default()].
320    ///
321    /// # Returns
322    ///
323    /// the global-default main context.
324    // rustdoc-stripper-ignore-next-stop
325    /// Returns the global-default main context.
326    ///
327    /// This is the main context
328    /// used for main loop functions when a main loop is not explicitly
329    /// specified, and corresponds to the ‘main’ main loop. See also
330    /// [`thread_default()`][Self::thread_default()].
331    ///
332    /// # Returns
333    ///
334    /// the global-default main context.
335    #[doc(alias = "g_main_context_default")]
336    #[allow(clippy::should_implement_trait)]
337    pub fn default() -> MainContext {
338        unsafe { from_glib_none(ffi::g_main_context_default()) }
339    }
340
341    /// Gets the thread-default main context for this thread.
342    ///
343    /// Asynchronous operations that want to be able to be run in contexts other than
344    /// the default one should call this method or
345    /// [`ref_thread_default()`][Self::ref_thread_default()] to get a
346    /// [`MainContext`][crate::MainContext] to add their [`Source`][crate::Source]s to. (Note that
347    /// even in single-threaded programs applications may sometimes want to
348    /// temporarily push a non-default context, so it is not safe to assume that
349    /// this will always return `NULL` if you are running in the default thread.)
350    ///
351    /// If you need to hold a reference on the context, use
352    /// [`ref_thread_default()`][Self::ref_thread_default()] instead.
353    ///
354    /// # Returns
355    ///
356    /// the thread-default main context, or
357    ///   `NULL` if the thread-default context is the global-default main context
358    // rustdoc-stripper-ignore-next-stop
359    /// Gets the thread-default main context for this thread.
360    ///
361    /// Asynchronous operations that want to be able to be run in contexts other than
362    /// the default one should call this method or
363    /// [`ref_thread_default()`][Self::ref_thread_default()] to get a
364    /// [`MainContext`][crate::MainContext] to add their [`Source`][crate::Source]s to. (Note that
365    /// even in single-threaded programs applications may sometimes want to
366    /// temporarily push a non-default context, so it is not safe to assume that
367    /// this will always return `NULL` if you are running in the default thread.)
368    ///
369    /// If you need to hold a reference on the context, use
370    /// [`ref_thread_default()`][Self::ref_thread_default()] instead.
371    ///
372    /// # Returns
373    ///
374    /// the thread-default main context, or
375    ///   `NULL` if the thread-default context is the global-default main context
376    #[doc(alias = "g_main_context_get_thread_default")]
377    #[doc(alias = "get_thread_default")]
378    pub fn thread_default() -> Option<MainContext> {
379        unsafe { from_glib_none(ffi::g_main_context_get_thread_default()) }
380    }
381
382    //#[cfg(feature = "v2_64")]
383    //#[cfg_attr(docsrs, doc(cfg(feature = "v2_64")))]
384    //#[doc(alias = "g_main_context_pusher_free")]
385    //pub fn pusher_free(pusher: /*Unknown conversion*//*Unimplemented*/MainContextPusher) {
386    //    unsafe { TODO: call ffi:g_main_context_pusher_free() }
387    //}
388
389    /// Gets a reference to the thread-default [`MainContext`][crate::MainContext] for this
390    /// thread
391    ///
392    /// This is the same as [`thread_default()`][Self::thread_default()], but it also
393    /// adds a reference to the returned main context with `GLib::MainContext::ref()`.
394    /// In addition, unlike
395    /// [`thread_default()`][Self::thread_default()], if the thread-default context
396    /// is the global-default context, this will return that
397    /// [`MainContext`][crate::MainContext] (with a ref added to it) rather than returning
398    /// `NULL`.
399    ///
400    /// # Returns
401    ///
402    /// the thread-default main context
403    // rustdoc-stripper-ignore-next-stop
404    /// Gets a reference to the thread-default [`MainContext`][crate::MainContext] for this
405    /// thread
406    ///
407    /// This is the same as [`thread_default()`][Self::thread_default()], but it also
408    /// adds a reference to the returned main context with `GLib::MainContext::ref()`.
409    /// In addition, unlike
410    /// [`thread_default()`][Self::thread_default()], if the thread-default context
411    /// is the global-default context, this will return that
412    /// [`MainContext`][crate::MainContext] (with a ref added to it) rather than returning
413    /// `NULL`.
414    ///
415    /// # Returns
416    ///
417    /// the thread-default main context
418    #[doc(alias = "g_main_context_ref_thread_default")]
419    pub fn ref_thread_default() -> MainContext {
420        unsafe { from_glib_full(ffi::g_main_context_ref_thread_default()) }
421    }
422}
423
424impl Default for MainContext {
425    fn default() -> Self {
426        Self::new()
427    }
428}
429
430unsafe impl Send for MainContext {}
431unsafe impl Sync for MainContext {}