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 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
14 pub struct MainContext(Shared<ffi::GMainContext>);
15
16 match fn {
17 ref => |ptr| ffi::g_main_context_ref(ptr),
18 unref => |ptr| ffi::g_main_context_unref(ptr),
19 type_ => || ffi::g_main_context_get_type(),
20 }
21}
22
23impl MainContext {
24 /// Creates a new [`MainContext`][crate::MainContext] structure.
25 ///
26 /// # Returns
27 ///
28 /// the new #GMainContext
29 #[doc(alias = "g_main_context_new")]
30 pub fn new() -> MainContext {
31 unsafe { from_glib_full(ffi::g_main_context_new()) }
32 }
33
34 /// Creates a new [`MainContext`][crate::MainContext] structure.
35 /// ## `flags`
36 /// a bitwise-OR combination of #GMainContextFlags flags that can only be
37 /// set at creation time.
38 ///
39 /// # Returns
40 ///
41 /// the new #GMainContext
42 #[cfg(feature = "v2_72")]
43 #[cfg_attr(docsrs, doc(cfg(feature = "v2_72")))]
44 #[doc(alias = "g_main_context_new_with_flags")]
45 #[doc(alias = "new_with_flags")]
46 pub fn with_flags(flags: MainContextFlags) -> MainContext {
47 unsafe { from_glib_full(ffi::g_main_context_new_with_flags(flags.into_glib())) }
48 }
49
50 //#[doc(alias = "g_main_context_add_poll")]
51 //pub fn add_poll(&self, fd: /*Ignored*/&mut PollFD, priority: i32) {
52 // unsafe { TODO: call ffi:g_main_context_add_poll() }
53 //}
54
55 //#[doc(alias = "g_main_context_check")]
56 //pub fn check(&self, max_priority: i32, fds: /*Ignored*/&[PollFD]) -> bool {
57 // unsafe { TODO: call ffi:g_main_context_check() }
58 //}
59
60 /// Dispatches all pending sources.
61 ///
62 /// You must have successfully acquired the context with
63 /// [`acquire()`][Self::acquire()] before you may call this function.
64 ///
65 /// Since 2.76 @self can be [`None`] to use the global-default
66 /// main context.
67 #[doc(alias = "g_main_context_dispatch")]
68 pub fn dispatch(&self) {
69 unsafe {
70 ffi::g_main_context_dispatch(self.to_glib_none().0);
71 }
72 }
73
74 //#[doc(alias = "g_main_context_find_source_by_funcs_user_data")]
75 //pub fn find_source_by_funcs_user_data(&self, funcs: /*Ignored*/&mut SourceFuncs, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> Source {
76 // unsafe { TODO: call ffi:g_main_context_find_source_by_funcs_user_data() }
77 //}
78
79 //#[doc(alias = "g_main_context_find_source_by_user_data")]
80 //pub fn find_source_by_user_data(&self, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> Source {
81 // unsafe { TODO: call ffi:g_main_context_find_source_by_user_data() }
82 //}
83
84 //#[doc(alias = "g_main_context_get_poll_func")]
85 //#[doc(alias = "get_poll_func")]
86 //pub fn poll_func(&self) -> /*Unimplemented*/Fn(/*Ignored*/PollFD, u32) -> i32 {
87 // unsafe { TODO: call ffi:g_main_context_get_poll_func() }
88 //}
89
90 /// Determines whether this thread holds the (recursive)
91 /// ownership of this [`MainContext`][crate::MainContext]. This is useful to
92 /// know before waiting on another thread that may be
93 /// blocking to get ownership of @self.
94 ///
95 /// # Returns
96 ///
97 /// [`true`] if current thread is owner of @self.
98 #[doc(alias = "g_main_context_is_owner")]
99 pub fn is_owner(&self) -> bool {
100 unsafe { from_glib(ffi::g_main_context_is_owner(self.to_glib_none().0)) }
101 }
102
103 /// Runs a single iteration for the given main loop. This involves
104 /// checking to see if any event sources are ready to be processed,
105 /// then if no events sources are ready and @may_block is [`true`], waiting
106 /// for a source to become ready, then dispatching the highest priority
107 /// events sources that are ready. Otherwise, if @may_block is [`false`]
108 /// sources are not waited to become ready, only those highest priority
109 /// events sources will be dispatched (if any), that are ready at this
110 /// given moment without further waiting.
111 ///
112 /// Note that even when @may_block is [`true`], it is still possible for
113 /// [`iteration()`][Self::iteration()] to return [`false`], since the wait may
114 /// be interrupted for other reasons than an event source becoming ready.
115 /// ## `may_block`
116 /// whether the call may block.
117 ///
118 /// # Returns
119 ///
120 /// [`true`] if events were dispatched.
121 #[doc(alias = "g_main_context_iteration")]
122 pub fn iteration(&self, may_block: bool) -> bool {
123 unsafe {
124 from_glib(ffi::g_main_context_iteration(
125 self.to_glib_none().0,
126 may_block.into_glib(),
127 ))
128 }
129 }
130
131 /// Checks if any sources have pending events for the given context.
132 ///
133 /// # Returns
134 ///
135 /// [`true`] if events are pending.
136 #[doc(alias = "g_main_context_pending")]
137 pub fn pending(&self) -> bool {
138 unsafe { from_glib(ffi::g_main_context_pending(self.to_glib_none().0)) }
139 }
140
141 //#[cfg(feature = "v2_64")]
142 //#[cfg_attr(docsrs, doc(cfg(feature = "v2_64")))]
143 //#[doc(alias = "g_main_context_pusher_new")]
144 //pub fn pusher_new(&self) -> /*Unknown conversion*//*Unimplemented*/MainContextPusher {
145 // unsafe { TODO: call ffi:g_main_context_pusher_new() }
146 //}
147
148 //#[doc(alias = "g_main_context_query")]
149 //pub fn query(&self, max_priority: i32, fds: /*Ignored*/Vec<PollFD>) -> (i32, i32) {
150 // unsafe { TODO: call ffi:g_main_context_query() }
151 //}
152
153 //#[doc(alias = "g_main_context_remove_poll")]
154 //pub fn remove_poll(&self, fd: /*Ignored*/&mut PollFD) {
155 // unsafe { TODO: call ffi:g_main_context_remove_poll() }
156 //}
157
158 //#[doc(alias = "g_main_context_set_poll_func")]
159 //pub fn set_poll_func(&self, func: /*Unimplemented*/Fn(/*Ignored*/PollFD, u32) -> i32) {
160 // unsafe { TODO: call ffi:g_main_context_set_poll_func() }
161 //}
162
163 //#[cfg_attr(feature = "v2_58", deprecated = "Since 2.58")]
164 //#[allow(deprecated)]
165 //#[doc(alias = "g_main_context_wait")]
166 //pub fn wait(&self, cond: /*Ignored*/&mut Cond, mutex: /*Ignored*/&mut Mutex) -> bool {
167 // unsafe { TODO: call ffi:g_main_context_wait() }
168 //}
169
170 /// If @self is currently blocking in [`iteration()`][Self::iteration()]
171 /// waiting for a source to become ready, cause it to stop blocking
172 /// and return. Otherwise, cause the next invocation of
173 /// [`iteration()`][Self::iteration()] to return without blocking.
174 ///
175 /// This API is useful for low-level control over [`MainContext`][crate::MainContext]; for
176 /// example, integrating it with main loop implementations such as
177 /// [`MainLoop`][crate::MainLoop].
178 ///
179 /// Another related use for this function is when implementing a main
180 /// loop with a termination condition, computed from multiple threads:
181 ///
182 ///
183 ///
184 /// **⚠️ The following code is in C ⚠️**
185 ///
186 /// ```C
187 /// #define NUM_TASKS 10
188 /// static gint tasks_remaining = NUM_TASKS; // (atomic)
189 /// ...
190 ///
191 /// while (g_atomic_int_get (&tasks_remaining) != 0)
192 /// g_main_context_iteration (NULL, TRUE);
193 /// ```
194 ///
195 /// Then in a thread:
196 ///
197 ///
198 /// **⚠️ The following code is in C ⚠️**
199 ///
200 /// ```C
201 /// perform_work();
202 ///
203 /// if (g_atomic_int_dec_and_test (&tasks_remaining))
204 /// g_main_context_wakeup (NULL);
205 /// ```
206 #[doc(alias = "g_main_context_wakeup")]
207 pub fn wakeup(&self) {
208 unsafe {
209 ffi::g_main_context_wakeup(self.to_glib_none().0);
210 }
211 }
212
213 /// Returns the global-default main context. This is the main context
214 /// used for main loop functions when a main loop is not explicitly
215 /// specified, and corresponds to the "main" main loop. See also
216 /// [`thread_default()`][Self::thread_default()].
217 ///
218 /// # Returns
219 ///
220 /// the global-default main context.
221 #[doc(alias = "g_main_context_default")]
222 #[allow(clippy::should_implement_trait)]
223 pub fn default() -> MainContext {
224 unsafe { from_glib_none(ffi::g_main_context_default()) }
225 }
226
227 /// Gets the thread-default #GMainContext for this thread. Asynchronous
228 /// operations that want to be able to be run in contexts other than
229 /// the default one should call this method or
230 /// [`ref_thread_default()`][Self::ref_thread_default()] to get a
231 /// [`MainContext`][crate::MainContext] to add their [`Source`][crate::Source]s to. (Note that
232 /// even in single-threaded programs applications may sometimes want to
233 /// temporarily push a non-default context, so it is not safe to assume that
234 /// this will always return [`None`] if you are running in the default thread.)
235 ///
236 /// If you need to hold a reference on the context, use
237 /// [`ref_thread_default()`][Self::ref_thread_default()] instead.
238 ///
239 /// # Returns
240 ///
241 /// the thread-default #GMainContext, or
242 /// [`None`] if the thread-default context is the global-default main context.
243 #[doc(alias = "g_main_context_get_thread_default")]
244 #[doc(alias = "get_thread_default")]
245 pub fn thread_default() -> Option<MainContext> {
246 unsafe { from_glib_none(ffi::g_main_context_get_thread_default()) }
247 }
248
249 //#[cfg(feature = "v2_64")]
250 //#[cfg_attr(docsrs, doc(cfg(feature = "v2_64")))]
251 //#[doc(alias = "g_main_context_pusher_free")]
252 //pub fn pusher_free(pusher: /*Unknown conversion*//*Unimplemented*/MainContextPusher) {
253 // unsafe { TODO: call ffi:g_main_context_pusher_free() }
254 //}
255
256 /// Gets the thread-default [`MainContext`][crate::MainContext] for this thread, as with
257 /// [`thread_default()`][Self::thread_default()], but also adds a reference to
258 /// it with `GLib::MainContext::ref()`. In addition, unlike
259 /// [`thread_default()`][Self::thread_default()], if the thread-default context
260 /// is the global-default context, this will return that
261 /// [`MainContext`][crate::MainContext] (with a ref added to it) rather than returning
262 /// [`None`].
263 ///
264 /// # Returns
265 ///
266 /// the thread-default #GMainContext. Unref
267 /// with `GLib::MainContext::unref()` when you are done with it.
268 #[doc(alias = "g_main_context_ref_thread_default")]
269 pub fn ref_thread_default() -> MainContext {
270 unsafe { from_glib_full(ffi::g_main_context_ref_thread_default()) }
271 }
272}
273
274impl Default for MainContext {
275 fn default() -> Self {
276 Self::new()
277 }
278}
279
280unsafe impl Send for MainContext {}
281unsafe impl Sync for MainContext {}