/src/irssi/subprojects/glib-2.74.7/glib/gmain.c
Line | Count | Source |
1 | | /* GLIB - Library of useful routines for C programming |
2 | | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
3 | | * |
4 | | * gmain.c: Main loop abstraction, timeouts, and idle functions |
5 | | * Copyright 1998 Owen Taylor |
6 | | * |
7 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
8 | | * |
9 | | * This library is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public |
11 | | * License as published by the Free Software Foundation; either |
12 | | * version 2.1 of the License, or (at your option) any later version. |
13 | | * |
14 | | * This library is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public |
20 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | /* |
24 | | * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
25 | | * file for a list of people on the GLib Team. See the ChangeLog |
26 | | * files for a list of changes. These files are distributed with |
27 | | * GLib at ftp://ftp.gtk.org/pub/gtk/. |
28 | | */ |
29 | | |
30 | | /* |
31 | | * MT safe |
32 | | */ |
33 | | |
34 | | #include "config.h" |
35 | | #include "glibconfig.h" |
36 | | #include "glib_trace.h" |
37 | | |
38 | | /* Uncomment the next line (and the corresponding line in gpoll.c) to |
39 | | * enable debugging printouts if the environment variable |
40 | | * G_MAIN_POLL_DEBUG is set to some value. |
41 | | */ |
42 | | /* #define G_MAIN_POLL_DEBUG */ |
43 | | |
44 | | #ifdef _WIN32 |
45 | | /* Always enable debugging printout on Windows, as it is more often |
46 | | * needed there... |
47 | | */ |
48 | | #define G_MAIN_POLL_DEBUG |
49 | | #endif |
50 | | |
51 | | #ifdef G_OS_UNIX |
52 | | #include "glib-unix.h" |
53 | | #include <pthread.h> |
54 | | #ifdef HAVE_EVENTFD |
55 | | #include <sys/eventfd.h> |
56 | | #endif |
57 | | #endif |
58 | | |
59 | | #include <signal.h> |
60 | | #include <sys/types.h> |
61 | | #include <time.h> |
62 | | #include <stdlib.h> |
63 | | #ifdef HAVE_SYS_TIME_H |
64 | | #include <sys/time.h> |
65 | | #endif /* HAVE_SYS_TIME_H */ |
66 | | #ifdef G_OS_UNIX |
67 | | #include <unistd.h> |
68 | | #endif /* G_OS_UNIX */ |
69 | | #include <errno.h> |
70 | | #include <string.h> |
71 | | |
72 | | #ifdef HAVE_PIDFD |
73 | | #include <sys/syscall.h> |
74 | | #include <sys/wait.h> |
75 | | #include <linux/wait.h> /* P_PIDFD */ |
76 | | #ifndef W_EXITCODE |
77 | | #define W_EXITCODE(ret, sig) ((ret) << 8 | (sig)) |
78 | | #endif |
79 | | #ifndef W_STOPCODE |
80 | | #define W_STOPCODE(sig) ((sig) << 8 | 0x7f) |
81 | | #endif |
82 | | #ifndef WCOREFLAG |
83 | | /* musl doesn’t define WCOREFLAG while glibc does. Unfortunately, there’s no way |
84 | | * to detect we’re building against musl, so just define it and hope. |
85 | | * See https://git.musl-libc.org/cgit/musl/tree/include/sys/wait.h#n51 */ |
86 | | #define WCOREFLAG 0x80 |
87 | | #endif |
88 | | #ifndef __W_CONTINUED |
89 | | /* Same as above, for musl */ |
90 | | #define __W_CONTINUED 0xffff |
91 | | #endif |
92 | | #endif /* HAVE_PIDFD */ |
93 | | |
94 | | #ifdef G_OS_WIN32 |
95 | | #define STRICT |
96 | | #include <windows.h> |
97 | | #endif /* G_OS_WIN32 */ |
98 | | |
99 | | #ifdef HAVE_MACH_MACH_TIME_H |
100 | | #include <mach/mach_time.h> |
101 | | #endif |
102 | | |
103 | | #include "glib_trace.h" |
104 | | |
105 | | #include "gmain.h" |
106 | | |
107 | | #include "garray.h" |
108 | | #include "giochannel.h" |
109 | | #include "ghash.h" |
110 | | #include "ghook.h" |
111 | | #include "gqueue.h" |
112 | | #include "gstrfuncs.h" |
113 | | #include "gtestutils.h" |
114 | | #include "gthreadprivate.h" |
115 | | #include "gtrace-private.h" |
116 | | |
117 | | #ifdef G_OS_WIN32 |
118 | | #include "gwin32.h" |
119 | | #endif |
120 | | |
121 | | #ifdef G_MAIN_POLL_DEBUG |
122 | | #include "gtimer.h" |
123 | | #endif |
124 | | |
125 | | #include "gwakeup.h" |
126 | | #include "gmain-internal.h" |
127 | | #include "glib-init.h" |
128 | | #include "glib-private.h" |
129 | | |
130 | | /** |
131 | | * SECTION:main |
132 | | * @title: The Main Event Loop |
133 | | * @short_description: manages all available sources of events |
134 | | * |
135 | | * The main event loop manages all the available sources of events for |
136 | | * GLib and GTK+ applications. These events can come from any number of |
137 | | * different types of sources such as file descriptors (plain files, |
138 | | * pipes or sockets) and timeouts. New types of event sources can also |
139 | | * be added using g_source_attach(). |
140 | | * |
141 | | * To allow multiple independent sets of sources to be handled in |
142 | | * different threads, each source is associated with a #GMainContext. |
143 | | * A #GMainContext can only be running in a single thread, but |
144 | | * sources can be added to it and removed from it from other threads. All |
145 | | * functions which operate on a #GMainContext or a built-in #GSource are |
146 | | * thread-safe. |
147 | | * |
148 | | * Each event source is assigned a priority. The default priority, |
149 | | * %G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. |
150 | | * Values greater than 0 denote lower priorities. Events from high priority |
151 | | * sources are always processed before events from lower priority sources: if |
152 | | * several sources are ready to dispatch, the ones with equal-highest priority |
153 | | * will be dispatched on the current #GMainContext iteration, and the rest wait |
154 | | * until a subsequent #GMainContext iteration when they have the highest |
155 | | * priority of the sources which are ready for dispatch. |
156 | | * |
157 | | * Idle functions can also be added, and assigned a priority. These will |
158 | | * be run whenever no events with a higher priority are ready to be dispatched. |
159 | | * |
160 | | * The #GMainLoop data type represents a main event loop. A GMainLoop is |
161 | | * created with g_main_loop_new(). After adding the initial event sources, |
162 | | * g_main_loop_run() is called. This continuously checks for new events from |
163 | | * each of the event sources and dispatches them. Finally, the processing of |
164 | | * an event from one of the sources leads to a call to g_main_loop_quit() to |
165 | | * exit the main loop, and g_main_loop_run() returns. |
166 | | * |
167 | | * It is possible to create new instances of #GMainLoop recursively. |
168 | | * This is often used in GTK+ applications when showing modal dialog |
169 | | * boxes. Note that event sources are associated with a particular |
170 | | * #GMainContext, and will be checked and dispatched for all main |
171 | | * loops associated with that GMainContext. |
172 | | * |
173 | | * GTK+ contains wrappers of some of these functions, e.g. gtk_main(), |
174 | | * gtk_main_quit() and gtk_events_pending(). |
175 | | * |
176 | | * ## Creating new source types |
177 | | * |
178 | | * One of the unusual features of the #GMainLoop functionality |
179 | | * is that new types of event source can be created and used in |
180 | | * addition to the builtin type of event source. A new event source |
181 | | * type is used for handling GDK events. A new source type is created |
182 | | * by "deriving" from the #GSource structure. The derived type of |
183 | | * source is represented by a structure that has the #GSource structure |
184 | | * as a first element, and other elements specific to the new source |
185 | | * type. To create an instance of the new source type, call |
186 | | * g_source_new() passing in the size of the derived structure and |
187 | | * a table of functions. These #GSourceFuncs determine the behavior of |
188 | | * the new source type. |
189 | | * |
190 | | * New source types basically interact with the main context |
191 | | * in two ways. Their prepare function in #GSourceFuncs can set a timeout |
192 | | * to determine the maximum amount of time that the main loop will sleep |
193 | | * before checking the source again. In addition, or as well, the source |
194 | | * can add file descriptors to the set that the main context checks using |
195 | | * g_source_add_poll(). |
196 | | * |
197 | | * ## Customizing the main loop iteration |
198 | | * |
199 | | * Single iterations of a #GMainContext can be run with |
200 | | * g_main_context_iteration(). In some cases, more detailed control |
201 | | * of exactly how the details of the main loop work is desired, for |
202 | | * instance, when integrating the #GMainLoop with an external main loop. |
203 | | * In such cases, you can call the component functions of |
204 | | * g_main_context_iteration() directly. These functions are |
205 | | * g_main_context_prepare(), g_main_context_query(), |
206 | | * g_main_context_check() and g_main_context_dispatch(). |
207 | | * |
208 | | * If the event loop thread releases #GMainContext ownership until the results |
209 | | * required by g_main_context_check() are ready you must create a context with |
210 | | * the flag %G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING or else you'll lose |
211 | | * g_source_attach() notifications. This happens for instance when you integrate |
212 | | * the GLib event loop into implementations that follow the proactor pattern |
213 | | * (i.e. in these contexts the `poll()` implementation will reclaim the thread for |
214 | | * other tasks until the results are ready). One example of the proactor pattern |
215 | | * is the Boost.Asio library. |
216 | | * |
217 | | * ## State of a Main Context # {#mainloop-states} |
218 | | * |
219 | | * The operation of these functions can best be seen in terms |
220 | | * of a state diagram, as shown in this image. |
221 | | * |
222 | | *  |
223 | | * |
224 | | * On UNIX, the GLib mainloop is incompatible with fork(). Any program |
225 | | * using the mainloop must either exec() or exit() from the child |
226 | | * without returning to the mainloop. |
227 | | * |
228 | | * ## Memory management of sources # {#mainloop-memory-management} |
229 | | * |
230 | | * There are two options for memory management of the user data passed to a |
231 | | * #GSource to be passed to its callback on invocation. This data is provided |
232 | | * in calls to g_timeout_add(), g_timeout_add_full(), g_idle_add(), etc. and |
233 | | * more generally, using g_source_set_callback(). This data is typically an |
234 | | * object which ‘owns’ the timeout or idle callback, such as a widget or a |
235 | | * network protocol implementation. In many cases, it is an error for the |
236 | | * callback to be invoked after this owning object has been destroyed, as that |
237 | | * results in use of freed memory. |
238 | | * |
239 | | * The first, and preferred, option is to store the source ID returned by |
240 | | * functions such as g_timeout_add() or g_source_attach(), and explicitly |
241 | | * remove that source from the main context using g_source_remove() when the |
242 | | * owning object is finalized. This ensures that the callback can only be |
243 | | * invoked while the object is still alive. |
244 | | * |
245 | | * The second option is to hold a strong reference to the object in the |
246 | | * callback, and to release it in the callback’s #GDestroyNotify. This ensures |
247 | | * that the object is kept alive until after the source is finalized, which is |
248 | | * guaranteed to be after it is invoked for the final time. The #GDestroyNotify |
249 | | * is another callback passed to the ‘full’ variants of #GSource functions (for |
250 | | * example, g_timeout_add_full()). It is called when the source is finalized, |
251 | | * and is designed for releasing references like this. |
252 | | * |
253 | | * One important caveat of this second approach is that it will keep the object |
254 | | * alive indefinitely if the main loop is stopped before the #GSource is |
255 | | * invoked, which may be undesirable. |
256 | | */ |
257 | | |
258 | | /* Types */ |
259 | | |
260 | | typedef struct _GIdleSource GIdleSource; |
261 | | typedef struct _GTimeoutSource GTimeoutSource; |
262 | | typedef struct _GChildWatchSource GChildWatchSource; |
263 | | typedef struct _GUnixSignalWatchSource GUnixSignalWatchSource; |
264 | | typedef struct _GPollRec GPollRec; |
265 | | typedef struct _GSourceCallback GSourceCallback; |
266 | | |
267 | | typedef enum |
268 | | { |
269 | | G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT, |
270 | | G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1), |
271 | | G_SOURCE_BLOCKED = 1 << (G_HOOK_FLAG_USER_SHIFT + 2) |
272 | | } GSourceFlags; |
273 | | |
274 | | typedef struct _GSourceList GSourceList; |
275 | | |
276 | | struct _GSourceList |
277 | | { |
278 | | GSource *head, *tail; |
279 | | gint priority; |
280 | | }; |
281 | | |
282 | | typedef struct _GMainWaiter GMainWaiter; |
283 | | |
284 | | struct _GMainWaiter |
285 | | { |
286 | | GCond *cond; |
287 | | GMutex *mutex; |
288 | | }; |
289 | | |
290 | | typedef struct _GMainDispatch GMainDispatch; |
291 | | |
292 | | struct _GMainDispatch |
293 | | { |
294 | | gint depth; |
295 | | GSource *source; |
296 | | }; |
297 | | |
298 | | #ifdef G_MAIN_POLL_DEBUG |
299 | | gboolean _g_main_poll_debug = FALSE; |
300 | | #endif |
301 | | |
302 | | struct _GMainContext |
303 | | { |
304 | | /* The following lock is used for both the list of sources |
305 | | * and the list of poll records |
306 | | */ |
307 | | GMutex mutex; |
308 | | GCond cond; |
309 | | GThread *owner; |
310 | | guint owner_count; |
311 | | GMainContextFlags flags; |
312 | | GSList *waiters; |
313 | | |
314 | | gint ref_count; /* (atomic) */ |
315 | | |
316 | | GHashTable *sources; /* guint -> GSource */ |
317 | | |
318 | | GPtrArray *pending_dispatches; |
319 | | gint timeout; /* Timeout for current iteration */ |
320 | | |
321 | | guint next_id; |
322 | | GList *source_lists; |
323 | | gint in_check_or_prepare; |
324 | | |
325 | | GPollRec *poll_records; |
326 | | guint n_poll_records; |
327 | | GPollFD *cached_poll_array; |
328 | | guint cached_poll_array_size; |
329 | | |
330 | | GWakeup *wakeup; |
331 | | |
332 | | GPollFD wake_up_rec; |
333 | | |
334 | | /* Flag indicating whether the set of fd's changed during a poll */ |
335 | | gboolean poll_changed; |
336 | | |
337 | | GPollFunc poll_func; |
338 | | |
339 | | gint64 time; |
340 | | gboolean time_is_fresh; |
341 | | }; |
342 | | |
343 | | struct _GSourceCallback |
344 | | { |
345 | | gint ref_count; /* (atomic) */ |
346 | | GSourceFunc func; |
347 | | gpointer data; |
348 | | GDestroyNotify notify; |
349 | | }; |
350 | | |
351 | | struct _GMainLoop |
352 | | { |
353 | | GMainContext *context; |
354 | | gboolean is_running; /* (atomic) */ |
355 | | gint ref_count; /* (atomic) */ |
356 | | }; |
357 | | |
358 | | struct _GIdleSource |
359 | | { |
360 | | GSource source; |
361 | | gboolean one_shot; |
362 | | }; |
363 | | |
364 | | struct _GTimeoutSource |
365 | | { |
366 | | GSource source; |
367 | | /* Measured in seconds if 'seconds' is TRUE, or milliseconds otherwise. */ |
368 | | guint interval; |
369 | | gboolean seconds; |
370 | | gboolean one_shot; |
371 | | }; |
372 | | |
373 | | struct _GChildWatchSource |
374 | | { |
375 | | GSource source; |
376 | | GPid pid; |
377 | | /* On Unix this is a wait status, which is the thing you pass to WEXITSTATUS() |
378 | | * to get the status returned from the process’ main() or passed to exit(): */ |
379 | | gint child_status; |
380 | | /* @poll is always used on Windows, and used on Unix iff @using_pidfd is set: */ |
381 | | GPollFD poll; |
382 | | #ifndef G_OS_WIN32 |
383 | | gboolean child_exited; /* (atomic); not used iff @using_pidfd is set */ |
384 | | gboolean using_pidfd; |
385 | | #endif /* G_OS_WIN32 */ |
386 | | }; |
387 | | |
388 | | struct _GUnixSignalWatchSource |
389 | | { |
390 | | GSource source; |
391 | | int signum; |
392 | | gboolean pending; /* (atomic) */ |
393 | | }; |
394 | | |
395 | | struct _GPollRec |
396 | | { |
397 | | GPollFD *fd; |
398 | | GPollRec *prev; |
399 | | GPollRec *next; |
400 | | gint priority; |
401 | | }; |
402 | | |
403 | | struct _GSourcePrivate |
404 | | { |
405 | | GSList *child_sources; |
406 | | GSource *parent_source; |
407 | | |
408 | | gint64 ready_time; |
409 | | |
410 | | /* This is currently only used on UNIX, but we always declare it (and |
411 | | * let it remain empty on Windows) to avoid #ifdef all over the place. |
412 | | */ |
413 | | GSList *fds; |
414 | | |
415 | | GSourceDisposeFunc dispose; |
416 | | |
417 | | gboolean static_name; |
418 | | }; |
419 | | |
420 | | typedef struct _GSourceIter |
421 | | { |
422 | | GMainContext *context; |
423 | | gboolean may_modify; |
424 | | GList *current_list; |
425 | | GSource *source; |
426 | | } GSourceIter; |
427 | | |
428 | 15.7k | #define LOCK_CONTEXT(context) g_mutex_lock (&context->mutex) |
429 | 15.7k | #define UNLOCK_CONTEXT(context) g_mutex_unlock (&context->mutex) |
430 | 0 | #define G_THREAD_SELF g_thread_self () |
431 | | |
432 | 8.88k | #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0) |
433 | 6.38k | #define SOURCE_BLOCKED(source) (((source)->flags & G_SOURCE_BLOCKED) != 0) |
434 | | |
435 | | /* Forward declarations */ |
436 | | |
437 | | static void g_source_unref_internal (GSource *source, |
438 | | GMainContext *context, |
439 | | gboolean have_lock); |
440 | | static void g_source_destroy_internal (GSource *source, |
441 | | GMainContext *context, |
442 | | gboolean have_lock); |
443 | | static void g_source_set_priority_unlocked (GSource *source, |
444 | | GMainContext *context, |
445 | | gint priority); |
446 | | static void g_child_source_remove_internal (GSource *child_source, |
447 | | GMainContext *context); |
448 | | |
449 | | static void g_main_context_poll (GMainContext *context, |
450 | | gint timeout, |
451 | | gint priority, |
452 | | GPollFD *fds, |
453 | | gint n_fds); |
454 | | static void g_main_context_add_poll_unlocked (GMainContext *context, |
455 | | gint priority, |
456 | | GPollFD *fd); |
457 | | static void g_main_context_remove_poll_unlocked (GMainContext *context, |
458 | | GPollFD *fd); |
459 | | |
460 | | static void g_source_iter_init (GSourceIter *iter, |
461 | | GMainContext *context, |
462 | | gboolean may_modify); |
463 | | static gboolean g_source_iter_next (GSourceIter *iter, |
464 | | GSource **source); |
465 | | static void g_source_iter_clear (GSourceIter *iter); |
466 | | |
467 | | static gboolean g_timeout_dispatch (GSource *source, |
468 | | GSourceFunc callback, |
469 | | gpointer user_data); |
470 | | static gboolean g_child_watch_prepare (GSource *source, |
471 | | gint *timeout); |
472 | | static gboolean g_child_watch_check (GSource *source); |
473 | | static gboolean g_child_watch_dispatch (GSource *source, |
474 | | GSourceFunc callback, |
475 | | gpointer user_data); |
476 | | static void g_child_watch_finalize (GSource *source); |
477 | | #ifdef G_OS_UNIX |
478 | | static void g_unix_signal_handler (int signum); |
479 | | static gboolean g_unix_signal_watch_prepare (GSource *source, |
480 | | gint *timeout); |
481 | | static gboolean g_unix_signal_watch_check (GSource *source); |
482 | | static gboolean g_unix_signal_watch_dispatch (GSource *source, |
483 | | GSourceFunc callback, |
484 | | gpointer user_data); |
485 | | static void g_unix_signal_watch_finalize (GSource *source); |
486 | | #endif |
487 | | static gboolean g_idle_prepare (GSource *source, |
488 | | gint *timeout); |
489 | | static gboolean g_idle_check (GSource *source); |
490 | | static gboolean g_idle_dispatch (GSource *source, |
491 | | GSourceFunc callback, |
492 | | gpointer user_data); |
493 | | |
494 | | static void block_source (GSource *source); |
495 | | |
496 | | static GMainContext *glib_worker_context; |
497 | | |
498 | | #ifndef G_OS_WIN32 |
499 | | |
500 | | |
501 | | /* UNIX signals work by marking one of these variables then waking the |
502 | | * worker context to check on them and dispatch accordingly. |
503 | | * |
504 | | * Both variables must be accessed using atomic primitives, unless those atomic |
505 | | * primitives are implemented using fallback mutexes (as those aren’t safe in |
506 | | * an interrupt context). |
507 | | * |
508 | | * If using atomic primitives, the variables must be of type `int` (so they’re |
509 | | * the right size for the atomic primitives). Otherwise, use `sig_atomic_t` if |
510 | | * it’s available, which is guaranteed to be async-signal-safe (but it’s *not* |
511 | | * guaranteed to be thread-safe, which is why we use atomic primitives if |
512 | | * possible). |
513 | | * |
514 | | * Typically, `sig_atomic_t` is a typedef to `int`, but that’s not the case on |
515 | | * FreeBSD, so we can’t use it unconditionally if it’s defined. |
516 | | */ |
517 | | #if (defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) || !defined(HAVE_SIG_ATOMIC_T) |
518 | | static volatile int unix_signal_pending[NSIG]; |
519 | | static volatile int any_unix_signal_pending; |
520 | | #else |
521 | | static volatile sig_atomic_t unix_signal_pending[NSIG]; |
522 | | static volatile sig_atomic_t any_unix_signal_pending; |
523 | | #endif |
524 | | |
525 | | /* Guards all the data below */ |
526 | | G_LOCK_DEFINE_STATIC (unix_signal_lock); |
527 | | static guint unix_signal_refcount[NSIG]; |
528 | | static GSList *unix_signal_watches; |
529 | | static GSList *unix_child_watches; |
530 | | |
531 | | GSourceFuncs g_unix_signal_funcs = |
532 | | { |
533 | | g_unix_signal_watch_prepare, |
534 | | g_unix_signal_watch_check, |
535 | | g_unix_signal_watch_dispatch, |
536 | | g_unix_signal_watch_finalize, |
537 | | NULL, NULL |
538 | | }; |
539 | | #endif /* !G_OS_WIN32 */ |
540 | | G_LOCK_DEFINE_STATIC (main_context_list); |
541 | | static GSList *main_context_list = NULL; |
542 | | |
543 | | GSourceFuncs g_timeout_funcs = |
544 | | { |
545 | | NULL, /* prepare */ |
546 | | NULL, /* check */ |
547 | | g_timeout_dispatch, |
548 | | NULL, NULL, NULL |
549 | | }; |
550 | | |
551 | | GSourceFuncs g_child_watch_funcs = |
552 | | { |
553 | | g_child_watch_prepare, |
554 | | g_child_watch_check, |
555 | | g_child_watch_dispatch, |
556 | | g_child_watch_finalize, |
557 | | NULL, NULL |
558 | | }; |
559 | | |
560 | | GSourceFuncs g_idle_funcs = |
561 | | { |
562 | | g_idle_prepare, |
563 | | g_idle_check, |
564 | | g_idle_dispatch, |
565 | | NULL, NULL, NULL |
566 | | }; |
567 | | |
568 | | /** |
569 | | * g_main_context_ref: |
570 | | * @context: a #GMainContext |
571 | | * |
572 | | * Increases the reference count on a #GMainContext object by one. |
573 | | * |
574 | | * Returns: the @context that was passed in (since 2.6) |
575 | | **/ |
576 | | GMainContext * |
577 | | g_main_context_ref (GMainContext *context) |
578 | 0 | { |
579 | 0 | int old_ref_count; |
580 | |
|
581 | 0 | g_return_val_if_fail (context != NULL, NULL); |
582 | | |
583 | 0 | old_ref_count = g_atomic_int_add (&context->ref_count, 1); |
584 | 0 | g_return_val_if_fail (old_ref_count > 0, NULL); |
585 | | |
586 | 0 | return context; |
587 | 0 | } |
588 | | |
589 | | static inline void |
590 | | poll_rec_list_free (GMainContext *context, |
591 | | GPollRec *list) |
592 | 0 | { |
593 | 0 | g_slice_free_chain (GPollRec, list, next); |
594 | 0 | } |
595 | | |
596 | | /** |
597 | | * g_main_context_unref: |
598 | | * @context: a #GMainContext |
599 | | * |
600 | | * Decreases the reference count on a #GMainContext object by one. If |
601 | | * the result is zero, free the context and free all associated memory. |
602 | | **/ |
603 | | void |
604 | | g_main_context_unref (GMainContext *context) |
605 | 0 | { |
606 | 0 | GSourceIter iter; |
607 | 0 | GSource *source; |
608 | 0 | GList *sl_iter; |
609 | 0 | GSList *s_iter, *remaining_sources = NULL; |
610 | 0 | GSourceList *list; |
611 | 0 | guint i; |
612 | |
|
613 | 0 | g_return_if_fail (context != NULL); |
614 | 0 | g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); |
615 | | |
616 | 0 | if (!g_atomic_int_dec_and_test (&context->ref_count)) |
617 | 0 | return; |
618 | | |
619 | 0 | G_LOCK (main_context_list); |
620 | 0 | main_context_list = g_slist_remove (main_context_list, context); |
621 | 0 | G_UNLOCK (main_context_list); |
622 | | |
623 | | /* Free pending dispatches */ |
624 | 0 | for (i = 0; i < context->pending_dispatches->len; i++) |
625 | 0 | g_source_unref_internal (context->pending_dispatches->pdata[i], context, FALSE); |
626 | | |
627 | | /* g_source_iter_next() assumes the context is locked. */ |
628 | 0 | LOCK_CONTEXT (context); |
629 | | |
630 | | /* First collect all remaining sources from the sources lists and store a |
631 | | * new reference in a separate list. Also set the context of the sources |
632 | | * to NULL so that they can't access a partially destroyed context anymore. |
633 | | * |
634 | | * We have to do this first so that we have a strong reference to all |
635 | | * sources and destroying them below does not also free them, and so that |
636 | | * none of the sources can access the context from their finalize/dispose |
637 | | * functions. */ |
638 | 0 | g_source_iter_init (&iter, context, FALSE); |
639 | 0 | while (g_source_iter_next (&iter, &source)) |
640 | 0 | { |
641 | 0 | source->context = NULL; |
642 | 0 | remaining_sources = g_slist_prepend (remaining_sources, g_source_ref (source)); |
643 | 0 | } |
644 | 0 | g_source_iter_clear (&iter); |
645 | | |
646 | | /* Next destroy all sources. As we still hold a reference to all of them, |
647 | | * this won't cause any of them to be freed yet and especially prevents any |
648 | | * source that unrefs another source from its finalize function to be freed. |
649 | | */ |
650 | 0 | for (s_iter = remaining_sources; s_iter; s_iter = s_iter->next) |
651 | 0 | { |
652 | 0 | source = s_iter->data; |
653 | 0 | g_source_destroy_internal (source, context, TRUE); |
654 | 0 | } |
655 | |
|
656 | 0 | for (sl_iter = context->source_lists; sl_iter; sl_iter = sl_iter->next) |
657 | 0 | { |
658 | 0 | list = sl_iter->data; |
659 | 0 | g_slice_free (GSourceList, list); |
660 | 0 | } |
661 | 0 | g_list_free (context->source_lists); |
662 | |
|
663 | 0 | g_hash_table_destroy (context->sources); |
664 | |
|
665 | 0 | UNLOCK_CONTEXT (context); |
666 | 0 | g_mutex_clear (&context->mutex); |
667 | |
|
668 | 0 | g_ptr_array_free (context->pending_dispatches, TRUE); |
669 | 0 | g_free (context->cached_poll_array); |
670 | |
|
671 | 0 | poll_rec_list_free (context, context->poll_records); |
672 | |
|
673 | 0 | g_wakeup_free (context->wakeup); |
674 | 0 | g_cond_clear (&context->cond); |
675 | |
|
676 | 0 | g_free (context); |
677 | | |
678 | | /* And now finally get rid of our references to the sources. This will cause |
679 | | * them to be freed unless something else still has a reference to them. Due |
680 | | * to setting the context pointers in the sources to NULL above, this won't |
681 | | * ever access the context or the internal linked list inside the GSource. |
682 | | * We already removed the sources completely from the context above. */ |
683 | 0 | for (s_iter = remaining_sources; s_iter; s_iter = s_iter->next) |
684 | 0 | { |
685 | 0 | source = s_iter->data; |
686 | 0 | g_source_unref_internal (source, NULL, FALSE); |
687 | 0 | } |
688 | 0 | g_slist_free (remaining_sources); |
689 | 0 | } |
690 | | |
691 | | /* Helper function used by mainloop/overflow test. |
692 | | */ |
693 | | GMainContext * |
694 | | g_main_context_new_with_next_id (guint next_id) |
695 | 0 | { |
696 | 0 | GMainContext *ret = g_main_context_new (); |
697 | | |
698 | 0 | ret->next_id = next_id; |
699 | | |
700 | 0 | return ret; |
701 | 0 | } |
702 | | |
703 | | /** |
704 | | * g_main_context_new: |
705 | | * |
706 | | * Creates a new #GMainContext structure. |
707 | | * |
708 | | * Returns: the new #GMainContext |
709 | | **/ |
710 | | GMainContext * |
711 | | g_main_context_new (void) |
712 | 8 | { |
713 | 8 | return g_main_context_new_with_flags (G_MAIN_CONTEXT_FLAGS_NONE); |
714 | 8 | } |
715 | | |
716 | | /** |
717 | | * g_main_context_new_with_flags: |
718 | | * @flags: a bitwise-OR combination of #GMainContextFlags flags that can only be |
719 | | * set at creation time. |
720 | | * |
721 | | * Creates a new #GMainContext structure. |
722 | | * |
723 | | * Returns: (transfer full): the new #GMainContext |
724 | | * |
725 | | * Since: 2.72 |
726 | | */ |
727 | | GMainContext * |
728 | | g_main_context_new_with_flags (GMainContextFlags flags) |
729 | 8 | { |
730 | 8 | static gsize initialised; |
731 | 8 | GMainContext *context; |
732 | | |
733 | 8 | if (g_once_init_enter (&initialised)) |
734 | 8 | { |
735 | | #ifdef G_MAIN_POLL_DEBUG |
736 | | if (g_getenv ("G_MAIN_POLL_DEBUG") != NULL) |
737 | | _g_main_poll_debug = TRUE; |
738 | | #endif |
739 | | |
740 | 8 | g_once_init_leave (&initialised, TRUE); |
741 | 8 | } |
742 | | |
743 | 8 | context = g_new0 (GMainContext, 1); |
744 | | |
745 | 8 | TRACE (GLIB_MAIN_CONTEXT_NEW (context)); |
746 | | |
747 | 8 | g_mutex_init (&context->mutex); |
748 | 8 | g_cond_init (&context->cond); |
749 | | |
750 | 8 | context->sources = g_hash_table_new (NULL, NULL); |
751 | 8 | context->owner = NULL; |
752 | 8 | context->flags = flags; |
753 | 8 | context->waiters = NULL; |
754 | | |
755 | 8 | context->ref_count = 1; |
756 | | |
757 | 8 | context->next_id = 1; |
758 | | |
759 | 8 | context->source_lists = NULL; |
760 | | |
761 | 8 | context->poll_func = g_poll; |
762 | | |
763 | 8 | context->cached_poll_array = NULL; |
764 | 8 | context->cached_poll_array_size = 0; |
765 | | |
766 | 8 | context->pending_dispatches = g_ptr_array_new (); |
767 | | |
768 | 8 | context->time_is_fresh = FALSE; |
769 | | |
770 | 8 | context->wakeup = g_wakeup_new (); |
771 | 8 | g_wakeup_get_pollfd (context->wakeup, &context->wake_up_rec); |
772 | 8 | g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec); |
773 | | |
774 | 8 | G_LOCK (main_context_list); |
775 | 8 | main_context_list = g_slist_append (main_context_list, context); |
776 | | |
777 | | #ifdef G_MAIN_POLL_DEBUG |
778 | | if (_g_main_poll_debug) |
779 | | g_print ("created context=%p\n", context); |
780 | | #endif |
781 | | |
782 | 8 | G_UNLOCK (main_context_list); |
783 | | |
784 | 8 | return context; |
785 | 8 | } |
786 | | |
787 | | /** |
788 | | * g_main_context_default: |
789 | | * |
790 | | * Returns the global default main context. This is the main context |
791 | | * used for main loop functions when a main loop is not explicitly |
792 | | * specified, and corresponds to the "main" main loop. See also |
793 | | * g_main_context_get_thread_default(). |
794 | | * |
795 | | * Returns: (transfer none): the global default main context. |
796 | | **/ |
797 | | GMainContext * |
798 | | g_main_context_default (void) |
799 | 6.38k | { |
800 | 6.38k | static GMainContext *default_main_context = NULL; |
801 | | |
802 | 6.38k | if (g_once_init_enter (&default_main_context)) |
803 | 8 | { |
804 | 8 | GMainContext *context; |
805 | | |
806 | 8 | context = g_main_context_new (); |
807 | | |
808 | 8 | TRACE (GLIB_MAIN_CONTEXT_DEFAULT (context)); |
809 | | |
810 | | #ifdef G_MAIN_POLL_DEBUG |
811 | | if (_g_main_poll_debug) |
812 | | g_print ("default context=%p\n", context); |
813 | | #endif |
814 | | |
815 | 8 | g_once_init_leave (&default_main_context, context); |
816 | 8 | } |
817 | | |
818 | 6.38k | return default_main_context; |
819 | 6.38k | } |
820 | | |
821 | | static void |
822 | | free_context (gpointer data) |
823 | 0 | { |
824 | 0 | GMainContext *context = data; |
825 | |
|
826 | 0 | TRACE (GLIB_MAIN_CONTEXT_FREE (context)); |
827 | |
|
828 | 0 | g_main_context_release (context); |
829 | 0 | if (context) |
830 | 0 | g_main_context_unref (context); |
831 | 0 | } |
832 | | |
833 | | static void |
834 | | free_context_stack (gpointer data) |
835 | 0 | { |
836 | 0 | g_queue_free_full((GQueue *) data, (GDestroyNotify) free_context); |
837 | 0 | } |
838 | | |
839 | | static GPrivate thread_context_stack = G_PRIVATE_INIT (free_context_stack); |
840 | | |
841 | | /** |
842 | | * g_main_context_push_thread_default: |
843 | | * @context: (nullable): a #GMainContext, or %NULL for the global default context |
844 | | * |
845 | | * Acquires @context and sets it as the thread-default context for the |
846 | | * current thread. This will cause certain asynchronous operations |
847 | | * (such as most [gio][gio]-based I/O) which are |
848 | | * started in this thread to run under @context and deliver their |
849 | | * results to its main loop, rather than running under the global |
850 | | * default context in the main thread. Note that calling this function |
851 | | * changes the context returned by g_main_context_get_thread_default(), |
852 | | * not the one returned by g_main_context_default(), so it does not affect |
853 | | * the context used by functions like g_idle_add(). |
854 | | * |
855 | | * Normally you would call this function shortly after creating a new |
856 | | * thread, passing it a #GMainContext which will be run by a |
857 | | * #GMainLoop in that thread, to set a new default context for all |
858 | | * async operations in that thread. In this case you may not need to |
859 | | * ever call g_main_context_pop_thread_default(), assuming you want the |
860 | | * new #GMainContext to be the default for the whole lifecycle of the |
861 | | * thread. |
862 | | * |
863 | | * If you don't have control over how the new thread was created (e.g. |
864 | | * in the new thread isn't newly created, or if the thread life |
865 | | * cycle is managed by a #GThreadPool), it is always suggested to wrap |
866 | | * the logic that needs to use the new #GMainContext inside a |
867 | | * g_main_context_push_thread_default() / g_main_context_pop_thread_default() |
868 | | * pair, otherwise threads that are re-used will end up never explicitly |
869 | | * releasing the #GMainContext reference they hold. |
870 | | * |
871 | | * In some cases you may want to schedule a single operation in a |
872 | | * non-default context, or temporarily use a non-default context in |
873 | | * the main thread. In that case, you can wrap the call to the |
874 | | * asynchronous operation inside a |
875 | | * g_main_context_push_thread_default() / |
876 | | * g_main_context_pop_thread_default() pair, but it is up to you to |
877 | | * ensure that no other asynchronous operations accidentally get |
878 | | * started while the non-default context is active. |
879 | | * |
880 | | * Beware that libraries that predate this function may not correctly |
881 | | * handle being used from a thread with a thread-default context. Eg, |
882 | | * see g_file_supports_thread_contexts(). |
883 | | * |
884 | | * Since: 2.22 |
885 | | **/ |
886 | | void |
887 | | g_main_context_push_thread_default (GMainContext *context) |
888 | 0 | { |
889 | 0 | GQueue *stack; |
890 | 0 | gboolean acquired_context; |
891 | |
|
892 | 0 | acquired_context = g_main_context_acquire (context); |
893 | 0 | g_return_if_fail (acquired_context); |
894 | | |
895 | 0 | if (context == g_main_context_default ()) |
896 | 0 | context = NULL; |
897 | 0 | else if (context) |
898 | 0 | g_main_context_ref (context); |
899 | |
|
900 | 0 | stack = g_private_get (&thread_context_stack); |
901 | 0 | if (!stack) |
902 | 0 | { |
903 | 0 | stack = g_queue_new (); |
904 | 0 | g_private_set (&thread_context_stack, stack); |
905 | 0 | } |
906 | |
|
907 | 0 | g_queue_push_head (stack, context); |
908 | |
|
909 | 0 | TRACE (GLIB_MAIN_CONTEXT_PUSH_THREAD_DEFAULT (context)); |
910 | 0 | } |
911 | | |
912 | | /** |
913 | | * g_main_context_pop_thread_default: |
914 | | * @context: (nullable): a #GMainContext object, or %NULL |
915 | | * |
916 | | * Pops @context off the thread-default context stack (verifying that |
917 | | * it was on the top of the stack). |
918 | | * |
919 | | * Since: 2.22 |
920 | | **/ |
921 | | void |
922 | | g_main_context_pop_thread_default (GMainContext *context) |
923 | 0 | { |
924 | 0 | GQueue *stack; |
925 | |
|
926 | 0 | if (context == g_main_context_default ()) |
927 | 0 | context = NULL; |
928 | |
|
929 | 0 | stack = g_private_get (&thread_context_stack); |
930 | |
|
931 | 0 | g_return_if_fail (stack != NULL); |
932 | 0 | g_return_if_fail (g_queue_peek_head (stack) == context); |
933 | | |
934 | 0 | TRACE (GLIB_MAIN_CONTEXT_POP_THREAD_DEFAULT (context)); |
935 | |
|
936 | 0 | g_queue_pop_head (stack); |
937 | |
|
938 | 0 | g_main_context_release (context); |
939 | 0 | if (context) |
940 | 0 | g_main_context_unref (context); |
941 | 0 | } |
942 | | |
943 | | /** |
944 | | * g_main_context_get_thread_default: |
945 | | * |
946 | | * Gets the thread-default #GMainContext for this thread. Asynchronous |
947 | | * operations that want to be able to be run in contexts other than |
948 | | * the default one should call this method or |
949 | | * g_main_context_ref_thread_default() to get a #GMainContext to add |
950 | | * their #GSources to. (Note that even in single-threaded |
951 | | * programs applications may sometimes want to temporarily push a |
952 | | * non-default context, so it is not safe to assume that this will |
953 | | * always return %NULL if you are running in the default thread.) |
954 | | * |
955 | | * If you need to hold a reference on the context, use |
956 | | * g_main_context_ref_thread_default() instead. |
957 | | * |
958 | | * Returns: (transfer none) (nullable): the thread-default #GMainContext, or |
959 | | * %NULL if the thread-default context is the global default context. |
960 | | * |
961 | | * Since: 2.22 |
962 | | **/ |
963 | | GMainContext * |
964 | | g_main_context_get_thread_default (void) |
965 | 0 | { |
966 | 0 | GQueue *stack; |
967 | |
|
968 | 0 | stack = g_private_get (&thread_context_stack); |
969 | 0 | if (stack) |
970 | 0 | return g_queue_peek_head (stack); |
971 | 0 | else |
972 | 0 | return NULL; |
973 | 0 | } |
974 | | |
975 | | /** |
976 | | * g_main_context_ref_thread_default: |
977 | | * |
978 | | * Gets the thread-default #GMainContext for this thread, as with |
979 | | * g_main_context_get_thread_default(), but also adds a reference to |
980 | | * it with g_main_context_ref(). In addition, unlike |
981 | | * g_main_context_get_thread_default(), if the thread-default context |
982 | | * is the global default context, this will return that #GMainContext |
983 | | * (with a ref added to it) rather than returning %NULL. |
984 | | * |
985 | | * Returns: (transfer full): the thread-default #GMainContext. Unref |
986 | | * with g_main_context_unref() when you are done with it. |
987 | | * |
988 | | * Since: 2.32 |
989 | | */ |
990 | | GMainContext * |
991 | | g_main_context_ref_thread_default (void) |
992 | 0 | { |
993 | 0 | GMainContext *context; |
994 | |
|
995 | 0 | context = g_main_context_get_thread_default (); |
996 | 0 | if (!context) |
997 | 0 | context = g_main_context_default (); |
998 | 0 | return g_main_context_ref (context); |
999 | 0 | } |
1000 | | |
1001 | | /* Hooks for adding to the main loop */ |
1002 | | |
1003 | | /** |
1004 | | * g_source_new: |
1005 | | * @source_funcs: structure containing functions that implement |
1006 | | * the sources behavior. |
1007 | | * @struct_size: size of the #GSource structure to create. |
1008 | | * |
1009 | | * Creates a new #GSource structure. The size is specified to |
1010 | | * allow creating structures derived from #GSource that contain |
1011 | | * additional data. The size passed in must be at least |
1012 | | * `sizeof (GSource)`. |
1013 | | * |
1014 | | * The source will not initially be associated with any #GMainContext |
1015 | | * and must be added to one with g_source_attach() before it will be |
1016 | | * executed. |
1017 | | * |
1018 | | * Returns: the newly-created #GSource. |
1019 | | **/ |
1020 | | GSource * |
1021 | | g_source_new (GSourceFuncs *source_funcs, |
1022 | | guint struct_size) |
1023 | 3.42k | { |
1024 | 3.42k | GSource *source; |
1025 | | |
1026 | 3.42k | g_return_val_if_fail (source_funcs != NULL, NULL); |
1027 | 3.42k | g_return_val_if_fail (struct_size >= sizeof (GSource), NULL); |
1028 | | |
1029 | 3.42k | source = (GSource*) g_malloc0 (struct_size); |
1030 | 3.42k | source->priv = g_slice_new0 (GSourcePrivate); |
1031 | 3.42k | source->source_funcs = source_funcs; |
1032 | 3.42k | source->ref_count = 1; |
1033 | | |
1034 | 3.42k | source->priority = G_PRIORITY_DEFAULT; |
1035 | | |
1036 | 3.42k | source->flags = G_HOOK_FLAG_ACTIVE; |
1037 | | |
1038 | 3.42k | source->priv->ready_time = -1; |
1039 | | |
1040 | | /* NULL/0 initialization for all other fields */ |
1041 | | |
1042 | 3.42k | TRACE (GLIB_SOURCE_NEW (source, source_funcs->prepare, source_funcs->check, |
1043 | 3.42k | source_funcs->dispatch, source_funcs->finalize, |
1044 | 3.42k | struct_size)); |
1045 | | |
1046 | 3.42k | return source; |
1047 | 3.42k | } |
1048 | | |
1049 | | /** |
1050 | | * g_source_set_dispose_function: |
1051 | | * @source: A #GSource to set the dispose function on |
1052 | | * @dispose: #GSourceDisposeFunc to set on the source |
1053 | | * |
1054 | | * Set @dispose as dispose function on @source. @dispose will be called once |
1055 | | * the reference count of @source reaches 0 but before any of the state of the |
1056 | | * source is freed, especially before the finalize function is called. |
1057 | | * |
1058 | | * This means that at this point @source is still a valid #GSource and it is |
1059 | | * allow for the reference count to increase again until @dispose returns. |
1060 | | * |
1061 | | * The dispose function can be used to clear any "weak" references to the |
1062 | | * @source in other data structures in a thread-safe way where it is possible |
1063 | | * for another thread to increase the reference count of @source again while |
1064 | | * it is being freed. |
1065 | | * |
1066 | | * The finalize function can not be used for this purpose as at that point |
1067 | | * @source is already partially freed and not valid anymore. |
1068 | | * |
1069 | | * This should only ever be called from #GSource implementations. |
1070 | | * |
1071 | | * Since: 2.64 |
1072 | | **/ |
1073 | | void |
1074 | | g_source_set_dispose_function (GSource *source, |
1075 | | GSourceDisposeFunc dispose) |
1076 | 0 | { |
1077 | 0 | g_return_if_fail (source != NULL); |
1078 | 0 | g_return_if_fail (source->priv->dispose == NULL); |
1079 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1080 | 0 | source->priv->dispose = dispose; |
1081 | 0 | } |
1082 | | |
1083 | | /* Holds context's lock */ |
1084 | | static void |
1085 | | g_source_iter_init (GSourceIter *iter, |
1086 | | GMainContext *context, |
1087 | | gboolean may_modify) |
1088 | 0 | { |
1089 | 0 | iter->context = context; |
1090 | 0 | iter->current_list = NULL; |
1091 | 0 | iter->source = NULL; |
1092 | 0 | iter->may_modify = may_modify; |
1093 | 0 | } |
1094 | | |
1095 | | /* Holds context's lock */ |
1096 | | static gboolean |
1097 | | g_source_iter_next (GSourceIter *iter, GSource **source) |
1098 | 0 | { |
1099 | 0 | GSource *next_source; |
1100 | |
|
1101 | 0 | if (iter->source) |
1102 | 0 | next_source = iter->source->next; |
1103 | 0 | else |
1104 | 0 | next_source = NULL; |
1105 | |
|
1106 | 0 | if (!next_source) |
1107 | 0 | { |
1108 | 0 | if (iter->current_list) |
1109 | 0 | iter->current_list = iter->current_list->next; |
1110 | 0 | else |
1111 | 0 | iter->current_list = iter->context->source_lists; |
1112 | |
|
1113 | 0 | if (iter->current_list) |
1114 | 0 | { |
1115 | 0 | GSourceList *source_list = iter->current_list->data; |
1116 | |
|
1117 | 0 | next_source = source_list->head; |
1118 | 0 | } |
1119 | 0 | } |
1120 | | |
1121 | | /* Note: unreffing iter->source could potentially cause its |
1122 | | * GSourceList to be removed from source_lists (if iter->source is |
1123 | | * the only source in its list, and it is destroyed), so we have to |
1124 | | * keep it reffed until after we advance iter->current_list, above. |
1125 | | * |
1126 | | * Also we first have to ref the next source before unreffing the |
1127 | | * previous one as unreffing the previous source can potentially |
1128 | | * free the next one. |
1129 | | */ |
1130 | 0 | if (next_source && iter->may_modify) |
1131 | 0 | g_source_ref (next_source); |
1132 | |
|
1133 | 0 | if (iter->source && iter->may_modify) |
1134 | 0 | g_source_unref_internal (iter->source, iter->context, TRUE); |
1135 | 0 | iter->source = next_source; |
1136 | |
|
1137 | 0 | *source = iter->source; |
1138 | 0 | return *source != NULL; |
1139 | 0 | } |
1140 | | |
1141 | | /* Holds context's lock. Only necessary to call if you broke out of |
1142 | | * the g_source_iter_next() loop early. |
1143 | | */ |
1144 | | static void |
1145 | | g_source_iter_clear (GSourceIter *iter) |
1146 | 0 | { |
1147 | 0 | if (iter->source && iter->may_modify) |
1148 | 0 | { |
1149 | 0 | g_source_unref_internal (iter->source, iter->context, TRUE); |
1150 | 0 | iter->source = NULL; |
1151 | 0 | } |
1152 | 0 | } |
1153 | | |
1154 | | /* Holds context's lock |
1155 | | */ |
1156 | | static GSourceList * |
1157 | | find_source_list_for_priority (GMainContext *context, |
1158 | | gint priority, |
1159 | | gboolean create) |
1160 | 6.38k | { |
1161 | 6.38k | GList *iter, *last; |
1162 | 6.38k | GSourceList *source_list; |
1163 | | |
1164 | 6.38k | last = NULL; |
1165 | 6.38k | for (iter = context->source_lists; iter != NULL; last = iter, iter = iter->next) |
1166 | 6.37k | { |
1167 | 6.37k | source_list = iter->data; |
1168 | | |
1169 | 6.37k | if (source_list->priority == priority) |
1170 | 6.37k | return source_list; |
1171 | | |
1172 | 0 | if (source_list->priority > priority) |
1173 | 0 | { |
1174 | 0 | if (!create) |
1175 | 0 | return NULL; |
1176 | | |
1177 | 0 | source_list = g_slice_new0 (GSourceList); |
1178 | 0 | source_list->priority = priority; |
1179 | 0 | context->source_lists = g_list_insert_before (context->source_lists, |
1180 | 0 | iter, |
1181 | 0 | source_list); |
1182 | 0 | return source_list; |
1183 | 0 | } |
1184 | 0 | } |
1185 | | |
1186 | 8 | if (!create) |
1187 | 0 | return NULL; |
1188 | | |
1189 | 8 | source_list = g_slice_new0 (GSourceList); |
1190 | 8 | source_list->priority = priority; |
1191 | | |
1192 | 8 | if (!last) |
1193 | 8 | context->source_lists = g_list_append (NULL, source_list); |
1194 | 0 | else |
1195 | 0 | { |
1196 | | /* This just appends source_list to the end of |
1197 | | * context->source_lists without having to walk the list again. |
1198 | | */ |
1199 | 0 | last = g_list_append (last, source_list); |
1200 | 0 | (void) last; |
1201 | 0 | } |
1202 | 8 | return source_list; |
1203 | 8 | } |
1204 | | |
1205 | | /* Holds context's lock |
1206 | | */ |
1207 | | static void |
1208 | | source_add_to_context (GSource *source, |
1209 | | GMainContext *context) |
1210 | 3.42k | { |
1211 | 3.42k | GSourceList *source_list; |
1212 | 3.42k | GSource *prev, *next; |
1213 | | |
1214 | 3.42k | source_list = find_source_list_for_priority (context, source->priority, TRUE); |
1215 | | |
1216 | 3.42k | if (source->priv->parent_source) |
1217 | 0 | { |
1218 | 0 | g_assert (source_list->head != NULL); |
1219 | | |
1220 | | /* Put the source immediately before its parent */ |
1221 | 0 | prev = source->priv->parent_source->prev; |
1222 | 0 | next = source->priv->parent_source; |
1223 | 0 | } |
1224 | 3.42k | else |
1225 | 3.42k | { |
1226 | 3.42k | prev = source_list->tail; |
1227 | 3.42k | next = NULL; |
1228 | 3.42k | } |
1229 | | |
1230 | 3.42k | source->next = next; |
1231 | 3.42k | if (next) |
1232 | 0 | next->prev = source; |
1233 | 3.42k | else |
1234 | 3.42k | source_list->tail = source; |
1235 | | |
1236 | 3.42k | source->prev = prev; |
1237 | 3.42k | if (prev) |
1238 | 3.41k | prev->next = source; |
1239 | 8 | else |
1240 | 8 | source_list->head = source; |
1241 | 3.42k | } |
1242 | | |
1243 | | /* Holds context's lock |
1244 | | */ |
1245 | | static void |
1246 | | source_remove_from_context (GSource *source, |
1247 | | GMainContext *context) |
1248 | 2.96k | { |
1249 | 2.96k | GSourceList *source_list; |
1250 | | |
1251 | 2.96k | source_list = find_source_list_for_priority (context, source->priority, FALSE); |
1252 | 2.96k | g_return_if_fail (source_list != NULL); |
1253 | | |
1254 | 2.96k | if (source->prev) |
1255 | 2.96k | source->prev->next = source->next; |
1256 | 0 | else |
1257 | 0 | source_list->head = source->next; |
1258 | | |
1259 | 2.96k | if (source->next) |
1260 | 0 | source->next->prev = source->prev; |
1261 | 2.96k | else |
1262 | 2.96k | source_list->tail = source->prev; |
1263 | | |
1264 | 2.96k | source->prev = NULL; |
1265 | 2.96k | source->next = NULL; |
1266 | | |
1267 | 2.96k | if (source_list->head == NULL) |
1268 | 0 | { |
1269 | 0 | context->source_lists = g_list_remove (context->source_lists, source_list); |
1270 | 0 | g_slice_free (GSourceList, source_list); |
1271 | 0 | } |
1272 | 2.96k | } |
1273 | | |
1274 | | static guint |
1275 | | g_source_attach_unlocked (GSource *source, |
1276 | | GMainContext *context, |
1277 | | gboolean do_wakeup) |
1278 | 3.42k | { |
1279 | 3.42k | GSList *tmp_list; |
1280 | 3.42k | guint id; |
1281 | | |
1282 | | /* The counter may have wrapped, so we must ensure that we do not |
1283 | | * reuse the source id of an existing source. |
1284 | | */ |
1285 | 3.42k | do |
1286 | 3.42k | id = context->next_id++; |
1287 | 3.42k | while (id == 0 || g_hash_table_contains (context->sources, GUINT_TO_POINTER (id))); |
1288 | | |
1289 | 3.42k | source->context = context; |
1290 | 3.42k | source->source_id = id; |
1291 | 3.42k | g_source_ref (source); |
1292 | | |
1293 | 3.42k | g_hash_table_insert (context->sources, GUINT_TO_POINTER (id), source); |
1294 | | |
1295 | 3.42k | source_add_to_context (source, context); |
1296 | | |
1297 | 3.42k | if (!SOURCE_BLOCKED (source)) |
1298 | 3.42k | { |
1299 | 3.42k | tmp_list = source->poll_fds; |
1300 | 3.80k | while (tmp_list) |
1301 | 388 | { |
1302 | 388 | g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data); |
1303 | 388 | tmp_list = tmp_list->next; |
1304 | 388 | } |
1305 | | |
1306 | 3.42k | for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next) |
1307 | 0 | g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data); |
1308 | 3.42k | } |
1309 | | |
1310 | 3.42k | tmp_list = source->priv->child_sources; |
1311 | 3.42k | while (tmp_list) |
1312 | 0 | { |
1313 | 0 | g_source_attach_unlocked (tmp_list->data, context, FALSE); |
1314 | 0 | tmp_list = tmp_list->next; |
1315 | 0 | } |
1316 | | |
1317 | | /* If another thread has acquired the context, wake it up since it |
1318 | | * might be in poll() right now. |
1319 | | */ |
1320 | 3.42k | if (do_wakeup && |
1321 | 3.42k | (context->flags & G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING || |
1322 | 3.42k | (context->owner && context->owner != G_THREAD_SELF))) |
1323 | 0 | { |
1324 | 0 | g_wakeup_signal (context->wakeup); |
1325 | 0 | } |
1326 | | |
1327 | 3.42k | g_trace_mark (G_TRACE_CURRENT_TIME, 0, |
1328 | 3.42k | "GLib", "g_source_attach", |
1329 | 3.42k | "%s to context %p", |
1330 | 3.42k | (g_source_get_name (source) != NULL) ? g_source_get_name (source) : "(unnamed)", |
1331 | 3.42k | context); |
1332 | | |
1333 | 3.42k | return source->source_id; |
1334 | 3.42k | } |
1335 | | |
1336 | | /** |
1337 | | * g_source_attach: |
1338 | | * @source: a #GSource |
1339 | | * @context: (nullable): a #GMainContext (if %NULL, the default context will be used) |
1340 | | * |
1341 | | * Adds a #GSource to a @context so that it will be executed within |
1342 | | * that context. Remove it by calling g_source_destroy(). |
1343 | | * |
1344 | | * This function is safe to call from any thread, regardless of which thread |
1345 | | * the @context is running in. |
1346 | | * |
1347 | | * Returns: the ID (greater than 0) for the source within the |
1348 | | * #GMainContext. |
1349 | | **/ |
1350 | | guint |
1351 | | g_source_attach (GSource *source, |
1352 | | GMainContext *context) |
1353 | 3.42k | { |
1354 | 3.42k | guint result = 0; |
1355 | | |
1356 | 3.42k | g_return_val_if_fail (source != NULL, 0); |
1357 | 3.42k | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0); |
1358 | 3.42k | g_return_val_if_fail (source->context == NULL, 0); |
1359 | 3.42k | g_return_val_if_fail (!SOURCE_DESTROYED (source), 0); |
1360 | | |
1361 | 3.42k | if (!context) |
1362 | 3.42k | context = g_main_context_default (); |
1363 | | |
1364 | 3.42k | LOCK_CONTEXT (context); |
1365 | | |
1366 | 3.42k | result = g_source_attach_unlocked (source, context, TRUE); |
1367 | | |
1368 | 3.42k | TRACE (GLIB_MAIN_SOURCE_ATTACH (g_source_get_name (source), source, context, |
1369 | 3.42k | result)); |
1370 | | |
1371 | 3.42k | UNLOCK_CONTEXT (context); |
1372 | | |
1373 | 3.42k | return result; |
1374 | 3.42k | } |
1375 | | |
1376 | | static void |
1377 | | g_source_destroy_internal (GSource *source, |
1378 | | GMainContext *context, |
1379 | | gboolean have_lock) |
1380 | 2.96k | { |
1381 | 2.96k | TRACE (GLIB_MAIN_SOURCE_DESTROY (g_source_get_name (source), source, |
1382 | 2.96k | context)); |
1383 | | |
1384 | 2.96k | if (!have_lock) |
1385 | 2.96k | LOCK_CONTEXT (context); |
1386 | | |
1387 | 2.96k | if (!SOURCE_DESTROYED (source)) |
1388 | 2.96k | { |
1389 | 2.96k | GSList *tmp_list; |
1390 | 2.96k | gpointer old_cb_data; |
1391 | 2.96k | GSourceCallbackFuncs *old_cb_funcs; |
1392 | | |
1393 | 2.96k | source->flags &= ~G_HOOK_FLAG_ACTIVE; |
1394 | | |
1395 | 2.96k | old_cb_data = source->callback_data; |
1396 | 2.96k | old_cb_funcs = source->callback_funcs; |
1397 | | |
1398 | 2.96k | source->callback_data = NULL; |
1399 | 2.96k | source->callback_funcs = NULL; |
1400 | | |
1401 | 2.96k | if (old_cb_funcs) |
1402 | 2.96k | { |
1403 | 2.96k | UNLOCK_CONTEXT (context); |
1404 | 2.96k | old_cb_funcs->unref (old_cb_data); |
1405 | 2.96k | LOCK_CONTEXT (context); |
1406 | 2.96k | } |
1407 | | |
1408 | 2.96k | if (!SOURCE_BLOCKED (source)) |
1409 | 2.96k | { |
1410 | 2.96k | tmp_list = source->poll_fds; |
1411 | 2.96k | while (tmp_list) |
1412 | 0 | { |
1413 | 0 | g_main_context_remove_poll_unlocked (context, tmp_list->data); |
1414 | 0 | tmp_list = tmp_list->next; |
1415 | 0 | } |
1416 | | |
1417 | 2.96k | for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next) |
1418 | 0 | g_main_context_remove_poll_unlocked (context, tmp_list->data); |
1419 | 2.96k | } |
1420 | | |
1421 | 2.96k | while (source->priv->child_sources) |
1422 | 0 | g_child_source_remove_internal (source->priv->child_sources->data, context); |
1423 | | |
1424 | 2.96k | if (source->priv->parent_source) |
1425 | 0 | g_child_source_remove_internal (source, context); |
1426 | | |
1427 | 2.96k | g_source_unref_internal (source, context, TRUE); |
1428 | 2.96k | } |
1429 | | |
1430 | 2.96k | if (!have_lock) |
1431 | 2.96k | UNLOCK_CONTEXT (context); |
1432 | 2.96k | } |
1433 | | |
1434 | | /** |
1435 | | * g_source_destroy: |
1436 | | * @source: a #GSource |
1437 | | * |
1438 | | * Removes a source from its #GMainContext, if any, and mark it as |
1439 | | * destroyed. The source cannot be subsequently added to another |
1440 | | * context. It is safe to call this on sources which have already been |
1441 | | * removed from their context. |
1442 | | * |
1443 | | * This does not unref the #GSource: if you still hold a reference, use |
1444 | | * g_source_unref() to drop it. |
1445 | | * |
1446 | | * This function is safe to call from any thread, regardless of which thread |
1447 | | * the #GMainContext is running in. |
1448 | | * |
1449 | | * If the source is currently attached to a #GMainContext, destroying it |
1450 | | * will effectively unset the callback similar to calling g_source_set_callback(). |
1451 | | * This can mean, that the data's #GDestroyNotify gets called right away. |
1452 | | */ |
1453 | | void |
1454 | | g_source_destroy (GSource *source) |
1455 | 2.96k | { |
1456 | 2.96k | GMainContext *context; |
1457 | | |
1458 | 2.96k | g_return_if_fail (source != NULL); |
1459 | 2.96k | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1460 | | |
1461 | 2.96k | context = source->context; |
1462 | | |
1463 | 2.96k | if (context) |
1464 | 2.96k | g_source_destroy_internal (source, context, FALSE); |
1465 | 0 | else |
1466 | 0 | source->flags &= ~G_HOOK_FLAG_ACTIVE; |
1467 | 2.96k | } |
1468 | | |
1469 | | /** |
1470 | | * g_source_get_id: |
1471 | | * @source: a #GSource |
1472 | | * |
1473 | | * Returns the numeric ID for a particular source. The ID of a source |
1474 | | * is a positive integer which is unique within a particular main loop |
1475 | | * context. The reverse |
1476 | | * mapping from ID to source is done by g_main_context_find_source_by_id(). |
1477 | | * |
1478 | | * You can only call this function while the source is associated to a |
1479 | | * #GMainContext instance; calling this function before g_source_attach() |
1480 | | * or after g_source_destroy() yields undefined behavior. The ID returned |
1481 | | * is unique within the #GMainContext instance passed to g_source_attach(). |
1482 | | * |
1483 | | * Returns: the ID (greater than 0) for the source |
1484 | | **/ |
1485 | | guint |
1486 | | g_source_get_id (GSource *source) |
1487 | 0 | { |
1488 | 0 | guint result; |
1489 | | |
1490 | 0 | g_return_val_if_fail (source != NULL, 0); |
1491 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0); |
1492 | 0 | g_return_val_if_fail (source->context != NULL, 0); |
1493 | | |
1494 | 0 | LOCK_CONTEXT (source->context); |
1495 | 0 | result = source->source_id; |
1496 | 0 | UNLOCK_CONTEXT (source->context); |
1497 | | |
1498 | 0 | return result; |
1499 | 0 | } |
1500 | | |
1501 | | /** |
1502 | | * g_source_get_context: |
1503 | | * @source: a #GSource |
1504 | | * |
1505 | | * Gets the #GMainContext with which the source is associated. |
1506 | | * |
1507 | | * You can call this on a source that has been destroyed, provided |
1508 | | * that the #GMainContext it was attached to still exists (in which |
1509 | | * case it will return that #GMainContext). In particular, you can |
1510 | | * always call this function on the source returned from |
1511 | | * g_main_current_source(). But calling this function on a source |
1512 | | * whose #GMainContext has been destroyed is an error. |
1513 | | * |
1514 | | * Returns: (transfer none) (nullable): the #GMainContext with which the |
1515 | | * source is associated, or %NULL if the context has not |
1516 | | * yet been added to a source. |
1517 | | **/ |
1518 | | GMainContext * |
1519 | | g_source_get_context (GSource *source) |
1520 | 0 | { |
1521 | 0 | g_return_val_if_fail (source != NULL, NULL); |
1522 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, NULL); |
1523 | 0 | g_return_val_if_fail (source->context != NULL || !SOURCE_DESTROYED (source), NULL); |
1524 | | |
1525 | 0 | return source->context; |
1526 | 0 | } |
1527 | | |
1528 | | /** |
1529 | | * g_source_add_poll: |
1530 | | * @source:a #GSource |
1531 | | * @fd: a #GPollFD structure holding information about a file |
1532 | | * descriptor to watch. |
1533 | | * |
1534 | | * Adds a file descriptor to the set of file descriptors polled for |
1535 | | * this source. This is usually combined with g_source_new() to add an |
1536 | | * event source. The event source's check function will typically test |
1537 | | * the @revents field in the #GPollFD struct and return %TRUE if events need |
1538 | | * to be processed. |
1539 | | * |
1540 | | * This API is only intended to be used by implementations of #GSource. |
1541 | | * Do not call this API on a #GSource that you did not create. |
1542 | | * |
1543 | | * Using this API forces the linear scanning of event sources on each |
1544 | | * main loop iteration. Newly-written event sources should try to use |
1545 | | * g_source_add_unix_fd() instead of this API. |
1546 | | **/ |
1547 | | void |
1548 | | g_source_add_poll (GSource *source, |
1549 | | GPollFD *fd) |
1550 | 388 | { |
1551 | 388 | GMainContext *context; |
1552 | | |
1553 | 388 | g_return_if_fail (source != NULL); |
1554 | 388 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1555 | 388 | g_return_if_fail (fd != NULL); |
1556 | 388 | g_return_if_fail (!SOURCE_DESTROYED (source)); |
1557 | | |
1558 | 388 | context = source->context; |
1559 | | |
1560 | 388 | if (context) |
1561 | 0 | LOCK_CONTEXT (context); |
1562 | | |
1563 | 388 | source->poll_fds = g_slist_prepend (source->poll_fds, fd); |
1564 | | |
1565 | 388 | if (context) |
1566 | 0 | { |
1567 | 0 | if (!SOURCE_BLOCKED (source)) |
1568 | 0 | g_main_context_add_poll_unlocked (context, source->priority, fd); |
1569 | 0 | UNLOCK_CONTEXT (context); |
1570 | 0 | } |
1571 | 388 | } |
1572 | | |
1573 | | /** |
1574 | | * g_source_remove_poll: |
1575 | | * @source:a #GSource |
1576 | | * @fd: a #GPollFD structure previously passed to g_source_add_poll(). |
1577 | | * |
1578 | | * Removes a file descriptor from the set of file descriptors polled for |
1579 | | * this source. |
1580 | | * |
1581 | | * This API is only intended to be used by implementations of #GSource. |
1582 | | * Do not call this API on a #GSource that you did not create. |
1583 | | **/ |
1584 | | void |
1585 | | g_source_remove_poll (GSource *source, |
1586 | | GPollFD *fd) |
1587 | 0 | { |
1588 | 0 | GMainContext *context; |
1589 | | |
1590 | 0 | g_return_if_fail (source != NULL); |
1591 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1592 | 0 | g_return_if_fail (fd != NULL); |
1593 | 0 | g_return_if_fail (!SOURCE_DESTROYED (source)); |
1594 | | |
1595 | 0 | context = source->context; |
1596 | |
|
1597 | 0 | if (context) |
1598 | 0 | LOCK_CONTEXT (context); |
1599 | | |
1600 | 0 | source->poll_fds = g_slist_remove (source->poll_fds, fd); |
1601 | |
|
1602 | 0 | if (context) |
1603 | 0 | { |
1604 | 0 | if (!SOURCE_BLOCKED (source)) |
1605 | 0 | g_main_context_remove_poll_unlocked (context, fd); |
1606 | 0 | UNLOCK_CONTEXT (context); |
1607 | 0 | } |
1608 | 0 | } |
1609 | | |
1610 | | /** |
1611 | | * g_source_add_child_source: |
1612 | | * @source:a #GSource |
1613 | | * @child_source: a second #GSource that @source should "poll" |
1614 | | * |
1615 | | * Adds @child_source to @source as a "polled" source; when @source is |
1616 | | * added to a #GMainContext, @child_source will be automatically added |
1617 | | * with the same priority, when @child_source is triggered, it will |
1618 | | * cause @source to dispatch (in addition to calling its own |
1619 | | * callback), and when @source is destroyed, it will destroy |
1620 | | * @child_source as well. (@source will also still be dispatched if |
1621 | | * its own prepare/check functions indicate that it is ready.) |
1622 | | * |
1623 | | * If you don't need @child_source to do anything on its own when it |
1624 | | * triggers, you can call g_source_set_dummy_callback() on it to set a |
1625 | | * callback that does nothing (except return %TRUE if appropriate). |
1626 | | * |
1627 | | * @source will hold a reference on @child_source while @child_source |
1628 | | * is attached to it. |
1629 | | * |
1630 | | * This API is only intended to be used by implementations of #GSource. |
1631 | | * Do not call this API on a #GSource that you did not create. |
1632 | | * |
1633 | | * Since: 2.28 |
1634 | | **/ |
1635 | | void |
1636 | | g_source_add_child_source (GSource *source, |
1637 | | GSource *child_source) |
1638 | 0 | { |
1639 | 0 | GMainContext *context; |
1640 | |
|
1641 | 0 | g_return_if_fail (source != NULL); |
1642 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1643 | 0 | g_return_if_fail (child_source != NULL); |
1644 | 0 | g_return_if_fail (g_atomic_int_get (&child_source->ref_count) > 0); |
1645 | 0 | g_return_if_fail (!SOURCE_DESTROYED (source)); |
1646 | 0 | g_return_if_fail (!SOURCE_DESTROYED (child_source)); |
1647 | 0 | g_return_if_fail (child_source->context == NULL); |
1648 | 0 | g_return_if_fail (child_source->priv->parent_source == NULL); |
1649 | | |
1650 | 0 | context = source->context; |
1651 | |
|
1652 | 0 | if (context) |
1653 | 0 | LOCK_CONTEXT (context); |
1654 | |
|
1655 | 0 | TRACE (GLIB_SOURCE_ADD_CHILD_SOURCE (source, child_source)); |
1656 | |
|
1657 | 0 | source->priv->child_sources = g_slist_prepend (source->priv->child_sources, |
1658 | 0 | g_source_ref (child_source)); |
1659 | 0 | child_source->priv->parent_source = source; |
1660 | 0 | g_source_set_priority_unlocked (child_source, NULL, source->priority); |
1661 | 0 | if (SOURCE_BLOCKED (source)) |
1662 | 0 | block_source (child_source); |
1663 | |
|
1664 | 0 | if (context) |
1665 | 0 | { |
1666 | 0 | g_source_attach_unlocked (child_source, context, TRUE); |
1667 | 0 | UNLOCK_CONTEXT (context); |
1668 | 0 | } |
1669 | 0 | } |
1670 | | |
1671 | | static void |
1672 | | g_child_source_remove_internal (GSource *child_source, |
1673 | | GMainContext *context) |
1674 | 0 | { |
1675 | 0 | GSource *parent_source = child_source->priv->parent_source; |
1676 | |
|
1677 | 0 | parent_source->priv->child_sources = |
1678 | 0 | g_slist_remove (parent_source->priv->child_sources, child_source); |
1679 | 0 | child_source->priv->parent_source = NULL; |
1680 | |
|
1681 | 0 | g_source_destroy_internal (child_source, context, TRUE); |
1682 | 0 | g_source_unref_internal (child_source, context, TRUE); |
1683 | 0 | } |
1684 | | |
1685 | | /** |
1686 | | * g_source_remove_child_source: |
1687 | | * @source:a #GSource |
1688 | | * @child_source: a #GSource previously passed to |
1689 | | * g_source_add_child_source(). |
1690 | | * |
1691 | | * Detaches @child_source from @source and destroys it. |
1692 | | * |
1693 | | * This API is only intended to be used by implementations of #GSource. |
1694 | | * Do not call this API on a #GSource that you did not create. |
1695 | | * |
1696 | | * Since: 2.28 |
1697 | | **/ |
1698 | | void |
1699 | | g_source_remove_child_source (GSource *source, |
1700 | | GSource *child_source) |
1701 | 0 | { |
1702 | 0 | GMainContext *context; |
1703 | |
|
1704 | 0 | g_return_if_fail (source != NULL); |
1705 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1706 | 0 | g_return_if_fail (child_source != NULL); |
1707 | 0 | g_return_if_fail (g_atomic_int_get (&child_source->ref_count) > 0); |
1708 | 0 | g_return_if_fail (child_source->priv->parent_source == source); |
1709 | 0 | g_return_if_fail (!SOURCE_DESTROYED (source)); |
1710 | 0 | g_return_if_fail (!SOURCE_DESTROYED (child_source)); |
1711 | | |
1712 | 0 | context = source->context; |
1713 | |
|
1714 | 0 | if (context) |
1715 | 0 | LOCK_CONTEXT (context); |
1716 | |
|
1717 | 0 | g_child_source_remove_internal (child_source, context); |
1718 | |
|
1719 | 0 | if (context) |
1720 | 0 | UNLOCK_CONTEXT (context); |
1721 | 0 | } |
1722 | | |
1723 | | static void |
1724 | | g_source_callback_ref (gpointer cb_data) |
1725 | 0 | { |
1726 | 0 | GSourceCallback *callback = cb_data; |
1727 | |
|
1728 | 0 | g_atomic_int_inc (&callback->ref_count); |
1729 | 0 | } |
1730 | | |
1731 | | static void |
1732 | | g_source_callback_unref (gpointer cb_data) |
1733 | 2.96k | { |
1734 | 2.96k | GSourceCallback *callback = cb_data; |
1735 | | |
1736 | 2.96k | if (g_atomic_int_dec_and_test (&callback->ref_count)) |
1737 | 2.96k | { |
1738 | 2.96k | if (callback->notify) |
1739 | 0 | callback->notify (callback->data); |
1740 | 2.96k | g_free (callback); |
1741 | 2.96k | } |
1742 | 2.96k | } |
1743 | | |
1744 | | static void |
1745 | | g_source_callback_get (gpointer cb_data, |
1746 | | GSource *source, |
1747 | | GSourceFunc *func, |
1748 | | gpointer *data) |
1749 | 0 | { |
1750 | 0 | GSourceCallback *callback = cb_data; |
1751 | |
|
1752 | 0 | *func = callback->func; |
1753 | 0 | *data = callback->data; |
1754 | 0 | } |
1755 | | |
1756 | | static GSourceCallbackFuncs g_source_callback_funcs = { |
1757 | | g_source_callback_ref, |
1758 | | g_source_callback_unref, |
1759 | | g_source_callback_get, |
1760 | | }; |
1761 | | |
1762 | | /** |
1763 | | * g_source_set_callback_indirect: |
1764 | | * @source: the source |
1765 | | * @callback_data: pointer to callback data "object" |
1766 | | * @callback_funcs: functions for reference counting @callback_data |
1767 | | * and getting the callback and data |
1768 | | * |
1769 | | * Sets the callback function storing the data as a refcounted callback |
1770 | | * "object". This is used internally. Note that calling |
1771 | | * g_source_set_callback_indirect() assumes |
1772 | | * an initial reference count on @callback_data, and thus |
1773 | | * @callback_funcs->unref will eventually be called once more |
1774 | | * than @callback_funcs->ref. |
1775 | | * |
1776 | | * It is safe to call this function multiple times on a source which has already |
1777 | | * been attached to a context. The changes will take effect for the next time |
1778 | | * the source is dispatched after this call returns. |
1779 | | **/ |
1780 | | void |
1781 | | g_source_set_callback_indirect (GSource *source, |
1782 | | gpointer callback_data, |
1783 | | GSourceCallbackFuncs *callback_funcs) |
1784 | 3.42k | { |
1785 | 3.42k | GMainContext *context; |
1786 | 3.42k | gpointer old_cb_data; |
1787 | 3.42k | GSourceCallbackFuncs *old_cb_funcs; |
1788 | | |
1789 | 3.42k | g_return_if_fail (source != NULL); |
1790 | 3.42k | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1791 | 3.42k | g_return_if_fail (callback_funcs != NULL || callback_data == NULL); |
1792 | | |
1793 | 3.42k | context = source->context; |
1794 | | |
1795 | 3.42k | if (context) |
1796 | 0 | LOCK_CONTEXT (context); |
1797 | | |
1798 | 3.42k | if (callback_funcs != &g_source_callback_funcs) |
1799 | 0 | { |
1800 | 0 | TRACE (GLIB_SOURCE_SET_CALLBACK_INDIRECT (source, callback_data, |
1801 | 0 | callback_funcs->ref, |
1802 | 0 | callback_funcs->unref, |
1803 | 0 | callback_funcs->get)); |
1804 | 0 | } |
1805 | | |
1806 | 3.42k | old_cb_data = source->callback_data; |
1807 | 3.42k | old_cb_funcs = source->callback_funcs; |
1808 | | |
1809 | 3.42k | source->callback_data = callback_data; |
1810 | 3.42k | source->callback_funcs = callback_funcs; |
1811 | | |
1812 | 3.42k | if (context) |
1813 | 0 | UNLOCK_CONTEXT (context); |
1814 | | |
1815 | 3.42k | if (old_cb_funcs) |
1816 | 0 | old_cb_funcs->unref (old_cb_data); |
1817 | 3.42k | } |
1818 | | |
1819 | | /** |
1820 | | * g_source_set_callback: |
1821 | | * @source: the source |
1822 | | * @func: a callback function |
1823 | | * @data: the data to pass to callback function |
1824 | | * @notify: (nullable): a function to call when @data is no longer in use, or %NULL. |
1825 | | * |
1826 | | * Sets the callback function for a source. The callback for a source is |
1827 | | * called from the source's dispatch function. |
1828 | | * |
1829 | | * The exact type of @func depends on the type of source; ie. you |
1830 | | * should not count on @func being called with @data as its first |
1831 | | * parameter. Cast @func with G_SOURCE_FUNC() to avoid warnings about |
1832 | | * incompatible function types. |
1833 | | * |
1834 | | * See [memory management of sources][mainloop-memory-management] for details |
1835 | | * on how to handle memory management of @data. |
1836 | | * |
1837 | | * Typically, you won't use this function. Instead use functions specific |
1838 | | * to the type of source you are using, such as g_idle_add() or g_timeout_add(). |
1839 | | * |
1840 | | * It is safe to call this function multiple times on a source which has already |
1841 | | * been attached to a context. The changes will take effect for the next time |
1842 | | * the source is dispatched after this call returns. |
1843 | | * |
1844 | | * Note that g_source_destroy() for a currently attached source has the effect |
1845 | | * of also unsetting the callback. |
1846 | | **/ |
1847 | | void |
1848 | | g_source_set_callback (GSource *source, |
1849 | | GSourceFunc func, |
1850 | | gpointer data, |
1851 | | GDestroyNotify notify) |
1852 | 3.42k | { |
1853 | 3.42k | GSourceCallback *new_callback; |
1854 | | |
1855 | 3.42k | g_return_if_fail (source != NULL); |
1856 | 3.42k | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1857 | | |
1858 | 3.42k | TRACE (GLIB_SOURCE_SET_CALLBACK (source, func, data, notify)); |
1859 | | |
1860 | 3.42k | new_callback = g_new (GSourceCallback, 1); |
1861 | | |
1862 | 3.42k | new_callback->ref_count = 1; |
1863 | 3.42k | new_callback->func = func; |
1864 | 3.42k | new_callback->data = data; |
1865 | 3.42k | new_callback->notify = notify; |
1866 | | |
1867 | 3.42k | g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs); |
1868 | 3.42k | } |
1869 | | |
1870 | | |
1871 | | /** |
1872 | | * g_source_set_funcs: |
1873 | | * @source: a #GSource |
1874 | | * @funcs: the new #GSourceFuncs |
1875 | | * |
1876 | | * Sets the source functions (can be used to override |
1877 | | * default implementations) of an unattached source. |
1878 | | * |
1879 | | * Since: 2.12 |
1880 | | */ |
1881 | | void |
1882 | | g_source_set_funcs (GSource *source, |
1883 | | GSourceFuncs *funcs) |
1884 | 0 | { |
1885 | 0 | g_return_if_fail (source != NULL); |
1886 | 0 | g_return_if_fail (source->context == NULL); |
1887 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1888 | 0 | g_return_if_fail (funcs != NULL); |
1889 | | |
1890 | 0 | source->source_funcs = funcs; |
1891 | 0 | } |
1892 | | |
1893 | | static void |
1894 | | g_source_set_priority_unlocked (GSource *source, |
1895 | | GMainContext *context, |
1896 | | gint priority) |
1897 | 0 | { |
1898 | 0 | GSList *tmp_list; |
1899 | | |
1900 | 0 | g_return_if_fail (source->priv->parent_source == NULL || |
1901 | 0 | source->priv->parent_source->priority == priority); |
1902 | | |
1903 | 0 | TRACE (GLIB_SOURCE_SET_PRIORITY (source, context, priority)); |
1904 | |
|
1905 | 0 | if (context) |
1906 | 0 | { |
1907 | | /* Remove the source from the context's source and then |
1908 | | * add it back after so it is sorted in the correct place |
1909 | | */ |
1910 | 0 | source_remove_from_context (source, source->context); |
1911 | 0 | } |
1912 | |
|
1913 | 0 | source->priority = priority; |
1914 | |
|
1915 | 0 | if (context) |
1916 | 0 | { |
1917 | 0 | source_add_to_context (source, source->context); |
1918 | |
|
1919 | 0 | if (!SOURCE_BLOCKED (source)) |
1920 | 0 | { |
1921 | 0 | tmp_list = source->poll_fds; |
1922 | 0 | while (tmp_list) |
1923 | 0 | { |
1924 | 0 | g_main_context_remove_poll_unlocked (context, tmp_list->data); |
1925 | 0 | g_main_context_add_poll_unlocked (context, priority, tmp_list->data); |
1926 | | |
1927 | 0 | tmp_list = tmp_list->next; |
1928 | 0 | } |
1929 | |
|
1930 | 0 | for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next) |
1931 | 0 | { |
1932 | 0 | g_main_context_remove_poll_unlocked (context, tmp_list->data); |
1933 | 0 | g_main_context_add_poll_unlocked (context, priority, tmp_list->data); |
1934 | 0 | } |
1935 | 0 | } |
1936 | 0 | } |
1937 | |
|
1938 | 0 | if (source->priv->child_sources) |
1939 | 0 | { |
1940 | 0 | tmp_list = source->priv->child_sources; |
1941 | 0 | while (tmp_list) |
1942 | 0 | { |
1943 | 0 | g_source_set_priority_unlocked (tmp_list->data, context, priority); |
1944 | 0 | tmp_list = tmp_list->next; |
1945 | 0 | } |
1946 | 0 | } |
1947 | 0 | } |
1948 | | |
1949 | | /** |
1950 | | * g_source_set_priority: |
1951 | | * @source: a #GSource |
1952 | | * @priority: the new priority. |
1953 | | * |
1954 | | * Sets the priority of a source. While the main loop is being run, a |
1955 | | * source will be dispatched if it is ready to be dispatched and no |
1956 | | * sources at a higher (numerically smaller) priority are ready to be |
1957 | | * dispatched. |
1958 | | * |
1959 | | * A child source always has the same priority as its parent. It is not |
1960 | | * permitted to change the priority of a source once it has been added |
1961 | | * as a child of another source. |
1962 | | **/ |
1963 | | void |
1964 | | g_source_set_priority (GSource *source, |
1965 | | gint priority) |
1966 | 0 | { |
1967 | 0 | GMainContext *context; |
1968 | |
|
1969 | 0 | g_return_if_fail (source != NULL); |
1970 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
1971 | 0 | g_return_if_fail (source->priv->parent_source == NULL); |
1972 | | |
1973 | 0 | context = source->context; |
1974 | |
|
1975 | 0 | if (context) |
1976 | 0 | LOCK_CONTEXT (context); |
1977 | 0 | g_source_set_priority_unlocked (source, context, priority); |
1978 | 0 | if (context) |
1979 | 0 | UNLOCK_CONTEXT (context); |
1980 | 0 | } |
1981 | | |
1982 | | /** |
1983 | | * g_source_get_priority: |
1984 | | * @source: a #GSource |
1985 | | * |
1986 | | * Gets the priority of a source. |
1987 | | * |
1988 | | * Returns: the priority of the source |
1989 | | **/ |
1990 | | gint |
1991 | | g_source_get_priority (GSource *source) |
1992 | 0 | { |
1993 | 0 | g_return_val_if_fail (source != NULL, 0); |
1994 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0); |
1995 | | |
1996 | 0 | return source->priority; |
1997 | 0 | } |
1998 | | |
1999 | | /** |
2000 | | * g_source_set_ready_time: |
2001 | | * @source: a #GSource |
2002 | | * @ready_time: the monotonic time at which the source will be ready, |
2003 | | * 0 for "immediately", -1 for "never" |
2004 | | * |
2005 | | * Sets a #GSource to be dispatched when the given monotonic time is |
2006 | | * reached (or passed). If the monotonic time is in the past (as it |
2007 | | * always will be if @ready_time is 0) then the source will be |
2008 | | * dispatched immediately. |
2009 | | * |
2010 | | * If @ready_time is -1 then the source is never woken up on the basis |
2011 | | * of the passage of time. |
2012 | | * |
2013 | | * Dispatching the source does not reset the ready time. You should do |
2014 | | * so yourself, from the source dispatch function. |
2015 | | * |
2016 | | * Note that if you have a pair of sources where the ready time of one |
2017 | | * suggests that it will be delivered first but the priority for the |
2018 | | * other suggests that it would be delivered first, and the ready time |
2019 | | * for both sources is reached during the same main context iteration, |
2020 | | * then the order of dispatch is undefined. |
2021 | | * |
2022 | | * It is a no-op to call this function on a #GSource which has already been |
2023 | | * destroyed with g_source_destroy(). |
2024 | | * |
2025 | | * This API is only intended to be used by implementations of #GSource. |
2026 | | * Do not call this API on a #GSource that you did not create. |
2027 | | * |
2028 | | * Since: 2.36 |
2029 | | **/ |
2030 | | void |
2031 | | g_source_set_ready_time (GSource *source, |
2032 | | gint64 ready_time) |
2033 | 3.03k | { |
2034 | 3.03k | GMainContext *context; |
2035 | | |
2036 | 3.03k | g_return_if_fail (source != NULL); |
2037 | 3.03k | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
2038 | | |
2039 | 3.03k | context = source->context; |
2040 | | |
2041 | 3.03k | if (context) |
2042 | 0 | LOCK_CONTEXT (context); |
2043 | | |
2044 | 3.03k | if (source->priv->ready_time == ready_time) |
2045 | 0 | { |
2046 | 0 | if (context) |
2047 | 0 | UNLOCK_CONTEXT (context); |
2048 | |
|
2049 | 0 | return; |
2050 | 0 | } |
2051 | | |
2052 | 3.03k | source->priv->ready_time = ready_time; |
2053 | | |
2054 | 3.03k | TRACE (GLIB_SOURCE_SET_READY_TIME (source, ready_time)); |
2055 | | |
2056 | 3.03k | if (context) |
2057 | 0 | { |
2058 | | /* Quite likely that we need to change the timeout on the poll */ |
2059 | 0 | if (!SOURCE_BLOCKED (source)) |
2060 | 0 | g_wakeup_signal (context->wakeup); |
2061 | 0 | UNLOCK_CONTEXT (context); |
2062 | 0 | } |
2063 | 3.03k | } |
2064 | | |
2065 | | /** |
2066 | | * g_source_get_ready_time: |
2067 | | * @source: a #GSource |
2068 | | * |
2069 | | * Gets the "ready time" of @source, as set by |
2070 | | * g_source_set_ready_time(). |
2071 | | * |
2072 | | * Any time before the current monotonic time (including 0) is an |
2073 | | * indication that the source will fire immediately. |
2074 | | * |
2075 | | * Returns: the monotonic ready time, -1 for "never" |
2076 | | **/ |
2077 | | gint64 |
2078 | | g_source_get_ready_time (GSource *source) |
2079 | 0 | { |
2080 | 0 | g_return_val_if_fail (source != NULL, -1); |
2081 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, -1); |
2082 | | |
2083 | 0 | return source->priv->ready_time; |
2084 | 0 | } |
2085 | | |
2086 | | /** |
2087 | | * g_source_set_can_recurse: |
2088 | | * @source: a #GSource |
2089 | | * @can_recurse: whether recursion is allowed for this source |
2090 | | * |
2091 | | * Sets whether a source can be called recursively. If @can_recurse is |
2092 | | * %TRUE, then while the source is being dispatched then this source |
2093 | | * will be processed normally. Otherwise, all processing of this |
2094 | | * source is blocked until the dispatch function returns. |
2095 | | **/ |
2096 | | void |
2097 | | g_source_set_can_recurse (GSource *source, |
2098 | | gboolean can_recurse) |
2099 | 0 | { |
2100 | 0 | GMainContext *context; |
2101 | | |
2102 | 0 | g_return_if_fail (source != NULL); |
2103 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
2104 | | |
2105 | 0 | context = source->context; |
2106 | |
|
2107 | 0 | if (context) |
2108 | 0 | LOCK_CONTEXT (context); |
2109 | | |
2110 | 0 | if (can_recurse) |
2111 | 0 | source->flags |= G_SOURCE_CAN_RECURSE; |
2112 | 0 | else |
2113 | 0 | source->flags &= ~G_SOURCE_CAN_RECURSE; |
2114 | |
|
2115 | 0 | if (context) |
2116 | 0 | UNLOCK_CONTEXT (context); |
2117 | 0 | } |
2118 | | |
2119 | | /** |
2120 | | * g_source_get_can_recurse: |
2121 | | * @source: a #GSource |
2122 | | * |
2123 | | * Checks whether a source is allowed to be called recursively. |
2124 | | * see g_source_set_can_recurse(). |
2125 | | * |
2126 | | * Returns: whether recursion is allowed. |
2127 | | **/ |
2128 | | gboolean |
2129 | | g_source_get_can_recurse (GSource *source) |
2130 | 0 | { |
2131 | 0 | g_return_val_if_fail (source != NULL, FALSE); |
2132 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, FALSE); |
2133 | | |
2134 | 0 | return (source->flags & G_SOURCE_CAN_RECURSE) != 0; |
2135 | 0 | } |
2136 | | |
2137 | | static void |
2138 | | g_source_set_name_full (GSource *source, |
2139 | | const char *name, |
2140 | | gboolean is_static) |
2141 | 388 | { |
2142 | 388 | GMainContext *context; |
2143 | | |
2144 | 388 | g_return_if_fail (source != NULL); |
2145 | 388 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
2146 | | |
2147 | 388 | context = source->context; |
2148 | | |
2149 | 388 | if (context) |
2150 | 0 | LOCK_CONTEXT (context); |
2151 | | |
2152 | 388 | TRACE (GLIB_SOURCE_SET_NAME (source, name)); |
2153 | | |
2154 | | /* setting back to NULL is allowed, just because it's |
2155 | | * weird if get_name can return NULL but you can't |
2156 | | * set that. |
2157 | | */ |
2158 | | |
2159 | 388 | if (!source->priv->static_name) |
2160 | 388 | g_free (source->name); |
2161 | | |
2162 | 388 | if (is_static) |
2163 | 388 | source->name = (char *)name; |
2164 | 0 | else |
2165 | 0 | source->name = g_strdup (name); |
2166 | | |
2167 | 388 | source->priv->static_name = is_static; |
2168 | | |
2169 | 388 | if (context) |
2170 | 0 | UNLOCK_CONTEXT (context); |
2171 | 388 | } |
2172 | | |
2173 | | /** |
2174 | | * g_source_set_name: |
2175 | | * @source: a #GSource |
2176 | | * @name: debug name for the source |
2177 | | * |
2178 | | * Sets a name for the source, used in debugging and profiling. |
2179 | | * The name defaults to #NULL. |
2180 | | * |
2181 | | * The source name should describe in a human-readable way |
2182 | | * what the source does. For example, "X11 event queue" |
2183 | | * or "GTK+ repaint idle handler" or whatever it is. |
2184 | | * |
2185 | | * It is permitted to call this function multiple times, but is not |
2186 | | * recommended due to the potential performance impact. For example, |
2187 | | * one could change the name in the "check" function of a #GSourceFuncs |
2188 | | * to include details like the event type in the source name. |
2189 | | * |
2190 | | * Use caution if changing the name while another thread may be |
2191 | | * accessing it with g_source_get_name(); that function does not copy |
2192 | | * the value, and changing the value will free it while the other thread |
2193 | | * may be attempting to use it. |
2194 | | * |
2195 | | * Also see g_source_set_static_name(). |
2196 | | * |
2197 | | * Since: 2.26 |
2198 | | **/ |
2199 | | void |
2200 | | g_source_set_name (GSource *source, |
2201 | | const char *name) |
2202 | 0 | { |
2203 | 0 | g_source_set_name_full (source, name, FALSE); |
2204 | 0 | } |
2205 | | |
2206 | | /** |
2207 | | * g_source_set_static_name: |
2208 | | * @source: a #GSource |
2209 | | * @name: debug name for the source |
2210 | | * |
2211 | | * A variant of g_source_set_name() that does not |
2212 | | * duplicate the @name, and can only be used with |
2213 | | * string literals. |
2214 | | * |
2215 | | * Since: 2.70 |
2216 | | */ |
2217 | | void |
2218 | | g_source_set_static_name (GSource *source, |
2219 | | const char *name) |
2220 | 388 | { |
2221 | 388 | g_source_set_name_full (source, name, TRUE); |
2222 | 388 | } |
2223 | | |
2224 | | /** |
2225 | | * g_source_get_name: |
2226 | | * @source: a #GSource |
2227 | | * |
2228 | | * Gets a name for the source, used in debugging and profiling. The |
2229 | | * name may be #NULL if it has never been set with g_source_set_name(). |
2230 | | * |
2231 | | * Returns: (nullable): the name of the source |
2232 | | * |
2233 | | * Since: 2.26 |
2234 | | **/ |
2235 | | const char * |
2236 | | g_source_get_name (GSource *source) |
2237 | 0 | { |
2238 | 0 | g_return_val_if_fail (source != NULL, NULL); |
2239 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, NULL); |
2240 | | |
2241 | 0 | return source->name; |
2242 | 0 | } |
2243 | | |
2244 | | /** |
2245 | | * g_source_set_name_by_id: |
2246 | | * @tag: a #GSource ID |
2247 | | * @name: debug name for the source |
2248 | | * |
2249 | | * Sets the name of a source using its ID. |
2250 | | * |
2251 | | * This is a convenience utility to set source names from the return |
2252 | | * value of g_idle_add(), g_timeout_add(), etc. |
2253 | | * |
2254 | | * It is a programmer error to attempt to set the name of a non-existent |
2255 | | * source. |
2256 | | * |
2257 | | * More specifically: source IDs can be reissued after a source has been |
2258 | | * destroyed and therefore it is never valid to use this function with a |
2259 | | * source ID which may have already been removed. An example is when |
2260 | | * scheduling an idle to run in another thread with g_idle_add(): the |
2261 | | * idle may already have run and been removed by the time this function |
2262 | | * is called on its (now invalid) source ID. This source ID may have |
2263 | | * been reissued, leading to the operation being performed against the |
2264 | | * wrong source. |
2265 | | * |
2266 | | * Since: 2.26 |
2267 | | **/ |
2268 | | void |
2269 | | g_source_set_name_by_id (guint tag, |
2270 | | const char *name) |
2271 | 0 | { |
2272 | 0 | GSource *source; |
2273 | |
|
2274 | 0 | g_return_if_fail (tag > 0); |
2275 | | |
2276 | 0 | source = g_main_context_find_source_by_id (NULL, tag); |
2277 | 0 | if (source == NULL) |
2278 | 0 | return; |
2279 | | |
2280 | 0 | g_source_set_name (source, name); |
2281 | 0 | } |
2282 | | |
2283 | | |
2284 | | /** |
2285 | | * g_source_ref: |
2286 | | * @source: a #GSource |
2287 | | * |
2288 | | * Increases the reference count on a source by one. |
2289 | | * |
2290 | | * Returns: @source |
2291 | | **/ |
2292 | | GSource * |
2293 | | g_source_ref (GSource *source) |
2294 | 3.42k | { |
2295 | 3.42k | g_return_val_if_fail (source != NULL, NULL); |
2296 | | /* We allow ref_count == 0 here to allow the dispose function to resurrect |
2297 | | * the GSource if needed */ |
2298 | 3.42k | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) >= 0, NULL); |
2299 | | |
2300 | 3.42k | g_atomic_int_inc (&source->ref_count); |
2301 | | |
2302 | 3.42k | return source; |
2303 | 3.42k | } |
2304 | | |
2305 | | /* g_source_unref() but possible to call within context lock |
2306 | | */ |
2307 | | static void |
2308 | | g_source_unref_internal (GSource *source, |
2309 | | GMainContext *context, |
2310 | | gboolean have_lock) |
2311 | 6.38k | { |
2312 | 6.38k | gpointer old_cb_data = NULL; |
2313 | 6.38k | GSourceCallbackFuncs *old_cb_funcs = NULL; |
2314 | | |
2315 | 6.38k | g_return_if_fail (source != NULL); |
2316 | | |
2317 | 6.38k | if (!have_lock && context) |
2318 | 3.42k | LOCK_CONTEXT (context); |
2319 | | |
2320 | 6.38k | if (g_atomic_int_dec_and_test (&source->ref_count)) |
2321 | 2.96k | { |
2322 | | /* If there's a dispose function, call this first */ |
2323 | 2.96k | if (source->priv->dispose) |
2324 | 0 | { |
2325 | | /* Temporarily increase the ref count again so that GSource methods |
2326 | | * can be called from dispose(). */ |
2327 | 0 | g_atomic_int_inc (&source->ref_count); |
2328 | 0 | if (context) |
2329 | 0 | UNLOCK_CONTEXT (context); |
2330 | 0 | source->priv->dispose (source); |
2331 | 0 | if (context) |
2332 | 0 | LOCK_CONTEXT (context); |
2333 | | |
2334 | | /* Now the reference count might be bigger than 0 again, in which |
2335 | | * case we simply return from here before freeing the source */ |
2336 | 0 | if (!g_atomic_int_dec_and_test (&source->ref_count)) |
2337 | 0 | { |
2338 | 0 | if (!have_lock && context) |
2339 | 0 | UNLOCK_CONTEXT (context); |
2340 | 0 | return; |
2341 | 0 | } |
2342 | 0 | } |
2343 | | |
2344 | 2.96k | TRACE (GLIB_SOURCE_BEFORE_FREE (source, context, |
2345 | 2.96k | source->source_funcs->finalize)); |
2346 | | |
2347 | 2.96k | old_cb_data = source->callback_data; |
2348 | 2.96k | old_cb_funcs = source->callback_funcs; |
2349 | | |
2350 | 2.96k | source->callback_data = NULL; |
2351 | 2.96k | source->callback_funcs = NULL; |
2352 | | |
2353 | 2.96k | if (context) |
2354 | 2.96k | { |
2355 | 2.96k | if (!SOURCE_DESTROYED (source)) |
2356 | 0 | g_warning (G_STRLOC ": ref_count == 0, but source was still attached to a context!"); |
2357 | 2.96k | source_remove_from_context (source, context); |
2358 | | |
2359 | 2.96k | g_hash_table_remove (context->sources, GUINT_TO_POINTER (source->source_id)); |
2360 | 2.96k | } |
2361 | | |
2362 | 2.96k | if (source->source_funcs->finalize) |
2363 | 0 | { |
2364 | 0 | gint old_ref_count; |
2365 | | |
2366 | | /* Temporarily increase the ref count again so that GSource methods |
2367 | | * can be called from finalize(). */ |
2368 | 0 | g_atomic_int_inc (&source->ref_count); |
2369 | 0 | if (context) |
2370 | 0 | UNLOCK_CONTEXT (context); |
2371 | 0 | source->source_funcs->finalize (source); |
2372 | 0 | if (context) |
2373 | 0 | LOCK_CONTEXT (context); |
2374 | 0 | old_ref_count = g_atomic_int_add (&source->ref_count, -1); |
2375 | 0 | g_warn_if_fail (old_ref_count == 1); |
2376 | 0 | } |
2377 | | |
2378 | 2.96k | if (old_cb_funcs) |
2379 | 0 | { |
2380 | 0 | gint old_ref_count; |
2381 | | |
2382 | | /* Temporarily increase the ref count again so that GSource methods |
2383 | | * can be called from callback_funcs.unref(). */ |
2384 | 0 | g_atomic_int_inc (&source->ref_count); |
2385 | 0 | if (context) |
2386 | 0 | UNLOCK_CONTEXT (context); |
2387 | |
|
2388 | 0 | old_cb_funcs->unref (old_cb_data); |
2389 | |
|
2390 | 0 | if (context) |
2391 | 0 | LOCK_CONTEXT (context); |
2392 | 0 | old_ref_count = g_atomic_int_add (&source->ref_count, -1); |
2393 | 0 | g_warn_if_fail (old_ref_count == 1); |
2394 | 0 | } |
2395 | | |
2396 | 2.96k | if (!source->priv->static_name) |
2397 | 2.96k | g_free (source->name); |
2398 | 2.96k | source->name = NULL; |
2399 | | |
2400 | 2.96k | g_slist_free (source->poll_fds); |
2401 | 2.96k | source->poll_fds = NULL; |
2402 | | |
2403 | 2.96k | g_slist_free_full (source->priv->fds, g_free); |
2404 | | |
2405 | 2.96k | while (source->priv->child_sources) |
2406 | 0 | { |
2407 | 0 | GSource *child_source = source->priv->child_sources->data; |
2408 | |
|
2409 | 0 | source->priv->child_sources = |
2410 | 0 | g_slist_remove (source->priv->child_sources, child_source); |
2411 | 0 | child_source->priv->parent_source = NULL; |
2412 | |
|
2413 | 0 | g_source_unref_internal (child_source, context, TRUE); |
2414 | 0 | } |
2415 | | |
2416 | 2.96k | g_slice_free (GSourcePrivate, source->priv); |
2417 | 2.96k | source->priv = NULL; |
2418 | | |
2419 | 2.96k | g_free (source); |
2420 | 2.96k | } |
2421 | | |
2422 | 6.38k | if (!have_lock && context) |
2423 | 3.42k | UNLOCK_CONTEXT (context); |
2424 | 6.38k | } |
2425 | | |
2426 | | /** |
2427 | | * g_source_unref: |
2428 | | * @source: a #GSource |
2429 | | * |
2430 | | * Decreases the reference count of a source by one. If the |
2431 | | * resulting reference count is zero the source and associated |
2432 | | * memory will be destroyed. |
2433 | | **/ |
2434 | | void |
2435 | | g_source_unref (GSource *source) |
2436 | 3.42k | { |
2437 | 3.42k | g_return_if_fail (source != NULL); |
2438 | 3.42k | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
2439 | | |
2440 | 3.42k | g_source_unref_internal (source, source->context, FALSE); |
2441 | 3.42k | } |
2442 | | |
2443 | | /** |
2444 | | * g_main_context_find_source_by_id: |
2445 | | * @context: (nullable): a #GMainContext (if %NULL, the default context will be used) |
2446 | | * @source_id: the source ID, as returned by g_source_get_id(). |
2447 | | * |
2448 | | * Finds a #GSource given a pair of context and ID. |
2449 | | * |
2450 | | * It is a programmer error to attempt to look up a non-existent source. |
2451 | | * |
2452 | | * More specifically: source IDs can be reissued after a source has been |
2453 | | * destroyed and therefore it is never valid to use this function with a |
2454 | | * source ID which may have already been removed. An example is when |
2455 | | * scheduling an idle to run in another thread with g_idle_add(): the |
2456 | | * idle may already have run and been removed by the time this function |
2457 | | * is called on its (now invalid) source ID. This source ID may have |
2458 | | * been reissued, leading to the operation being performed against the |
2459 | | * wrong source. |
2460 | | * |
2461 | | * Returns: (transfer none): the #GSource |
2462 | | **/ |
2463 | | GSource * |
2464 | | g_main_context_find_source_by_id (GMainContext *context, |
2465 | | guint source_id) |
2466 | 2.96k | { |
2467 | 2.96k | GSource *source; |
2468 | | |
2469 | 2.96k | g_return_val_if_fail (source_id > 0, NULL); |
2470 | | |
2471 | 2.96k | if (context == NULL) |
2472 | 2.96k | context = g_main_context_default (); |
2473 | | |
2474 | 2.96k | LOCK_CONTEXT (context); |
2475 | 2.96k | source = g_hash_table_lookup (context->sources, GUINT_TO_POINTER (source_id)); |
2476 | 2.96k | UNLOCK_CONTEXT (context); |
2477 | | |
2478 | 2.96k | if (source && SOURCE_DESTROYED (source)) |
2479 | 0 | source = NULL; |
2480 | | |
2481 | 2.96k | return source; |
2482 | 2.96k | } |
2483 | | |
2484 | | /** |
2485 | | * g_main_context_find_source_by_funcs_user_data: |
2486 | | * @context: (nullable): a #GMainContext (if %NULL, the default context will be used). |
2487 | | * @funcs: the @source_funcs passed to g_source_new(). |
2488 | | * @user_data: the user data from the callback. |
2489 | | * |
2490 | | * Finds a source with the given source functions and user data. If |
2491 | | * multiple sources exist with the same source function and user data, |
2492 | | * the first one found will be returned. |
2493 | | * |
2494 | | * Returns: (transfer none): the source, if one was found, otherwise %NULL |
2495 | | **/ |
2496 | | GSource * |
2497 | | g_main_context_find_source_by_funcs_user_data (GMainContext *context, |
2498 | | GSourceFuncs *funcs, |
2499 | | gpointer user_data) |
2500 | 0 | { |
2501 | 0 | GSourceIter iter; |
2502 | 0 | GSource *source; |
2503 | | |
2504 | 0 | g_return_val_if_fail (funcs != NULL, NULL); |
2505 | | |
2506 | 0 | if (context == NULL) |
2507 | 0 | context = g_main_context_default (); |
2508 | | |
2509 | 0 | LOCK_CONTEXT (context); |
2510 | |
|
2511 | 0 | g_source_iter_init (&iter, context, FALSE); |
2512 | 0 | while (g_source_iter_next (&iter, &source)) |
2513 | 0 | { |
2514 | 0 | if (!SOURCE_DESTROYED (source) && |
2515 | 0 | source->source_funcs == funcs && |
2516 | 0 | source->callback_funcs) |
2517 | 0 | { |
2518 | 0 | GSourceFunc callback; |
2519 | 0 | gpointer callback_data; |
2520 | |
|
2521 | 0 | source->callback_funcs->get (source->callback_data, source, &callback, &callback_data); |
2522 | | |
2523 | 0 | if (callback_data == user_data) |
2524 | 0 | break; |
2525 | 0 | } |
2526 | 0 | } |
2527 | 0 | g_source_iter_clear (&iter); |
2528 | |
|
2529 | 0 | UNLOCK_CONTEXT (context); |
2530 | |
|
2531 | 0 | return source; |
2532 | 0 | } |
2533 | | |
2534 | | /** |
2535 | | * g_main_context_find_source_by_user_data: |
2536 | | * @context: a #GMainContext |
2537 | | * @user_data: the user_data for the callback. |
2538 | | * |
2539 | | * Finds a source with the given user data for the callback. If |
2540 | | * multiple sources exist with the same user data, the first |
2541 | | * one found will be returned. |
2542 | | * |
2543 | | * Returns: (transfer none): the source, if one was found, otherwise %NULL |
2544 | | **/ |
2545 | | GSource * |
2546 | | g_main_context_find_source_by_user_data (GMainContext *context, |
2547 | | gpointer user_data) |
2548 | 0 | { |
2549 | 0 | GSourceIter iter; |
2550 | 0 | GSource *source; |
2551 | | |
2552 | 0 | if (context == NULL) |
2553 | 0 | context = g_main_context_default (); |
2554 | | |
2555 | 0 | LOCK_CONTEXT (context); |
2556 | |
|
2557 | 0 | g_source_iter_init (&iter, context, FALSE); |
2558 | 0 | while (g_source_iter_next (&iter, &source)) |
2559 | 0 | { |
2560 | 0 | if (!SOURCE_DESTROYED (source) && |
2561 | 0 | source->callback_funcs) |
2562 | 0 | { |
2563 | 0 | GSourceFunc callback; |
2564 | 0 | gpointer callback_data = NULL; |
2565 | |
|
2566 | 0 | source->callback_funcs->get (source->callback_data, source, &callback, &callback_data); |
2567 | |
|
2568 | 0 | if (callback_data == user_data) |
2569 | 0 | break; |
2570 | 0 | } |
2571 | 0 | } |
2572 | 0 | g_source_iter_clear (&iter); |
2573 | |
|
2574 | 0 | UNLOCK_CONTEXT (context); |
2575 | |
|
2576 | 0 | return source; |
2577 | 0 | } |
2578 | | |
2579 | | /** |
2580 | | * g_source_remove: |
2581 | | * @tag: the ID of the source to remove. |
2582 | | * |
2583 | | * Removes the source with the given ID from the default main context. You must |
2584 | | * use g_source_destroy() for sources added to a non-default main context. |
2585 | | * |
2586 | | * The ID of a #GSource is given by g_source_get_id(), or will be |
2587 | | * returned by the functions g_source_attach(), g_idle_add(), |
2588 | | * g_idle_add_full(), g_timeout_add(), g_timeout_add_full(), |
2589 | | * g_child_watch_add(), g_child_watch_add_full(), g_io_add_watch(), and |
2590 | | * g_io_add_watch_full(). |
2591 | | * |
2592 | | * It is a programmer error to attempt to remove a non-existent source. |
2593 | | * |
2594 | | * More specifically: source IDs can be reissued after a source has been |
2595 | | * destroyed and therefore it is never valid to use this function with a |
2596 | | * source ID which may have already been removed. An example is when |
2597 | | * scheduling an idle to run in another thread with g_idle_add(): the |
2598 | | * idle may already have run and been removed by the time this function |
2599 | | * is called on its (now invalid) source ID. This source ID may have |
2600 | | * been reissued, leading to the operation being performed against the |
2601 | | * wrong source. |
2602 | | * |
2603 | | * Returns: %TRUE if the source was found and removed. |
2604 | | **/ |
2605 | | gboolean |
2606 | | g_source_remove (guint tag) |
2607 | 33.3k | { |
2608 | 33.3k | GSource *source; |
2609 | | |
2610 | 33.3k | g_return_val_if_fail (tag > 0, FALSE); |
2611 | | |
2612 | 2.96k | source = g_main_context_find_source_by_id (NULL, tag); |
2613 | 2.96k | if (source) |
2614 | 2.96k | g_source_destroy (source); |
2615 | 0 | else |
2616 | 0 | g_critical ("Source ID %u was not found when attempting to remove it", tag); |
2617 | | |
2618 | 2.96k | return source != NULL; |
2619 | 33.3k | } |
2620 | | |
2621 | | /** |
2622 | | * g_source_remove_by_user_data: |
2623 | | * @user_data: the user_data for the callback. |
2624 | | * |
2625 | | * Removes a source from the default main loop context given the user |
2626 | | * data for the callback. If multiple sources exist with the same user |
2627 | | * data, only one will be destroyed. |
2628 | | * |
2629 | | * Returns: %TRUE if a source was found and removed. |
2630 | | **/ |
2631 | | gboolean |
2632 | | g_source_remove_by_user_data (gpointer user_data) |
2633 | 0 | { |
2634 | 0 | GSource *source; |
2635 | | |
2636 | 0 | source = g_main_context_find_source_by_user_data (NULL, user_data); |
2637 | 0 | if (source) |
2638 | 0 | { |
2639 | 0 | g_source_destroy (source); |
2640 | 0 | return TRUE; |
2641 | 0 | } |
2642 | 0 | else |
2643 | 0 | return FALSE; |
2644 | 0 | } |
2645 | | |
2646 | | /** |
2647 | | * g_source_remove_by_funcs_user_data: |
2648 | | * @funcs: The @source_funcs passed to g_source_new() |
2649 | | * @user_data: the user data for the callback |
2650 | | * |
2651 | | * Removes a source from the default main loop context given the |
2652 | | * source functions and user data. If multiple sources exist with the |
2653 | | * same source functions and user data, only one will be destroyed. |
2654 | | * |
2655 | | * Returns: %TRUE if a source was found and removed. |
2656 | | **/ |
2657 | | gboolean |
2658 | | g_source_remove_by_funcs_user_data (GSourceFuncs *funcs, |
2659 | | gpointer user_data) |
2660 | 0 | { |
2661 | 0 | GSource *source; |
2662 | |
|
2663 | 0 | g_return_val_if_fail (funcs != NULL, FALSE); |
2664 | | |
2665 | 0 | source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data); |
2666 | 0 | if (source) |
2667 | 0 | { |
2668 | 0 | g_source_destroy (source); |
2669 | 0 | return TRUE; |
2670 | 0 | } |
2671 | 0 | else |
2672 | 0 | return FALSE; |
2673 | 0 | } |
2674 | | |
2675 | | /** |
2676 | | * g_clear_handle_id: (skip) |
2677 | | * @tag_ptr: (not nullable): a pointer to the handler ID |
2678 | | * @clear_func: (not nullable): the function to call to clear the handler |
2679 | | * |
2680 | | * Clears a numeric handler, such as a #GSource ID. |
2681 | | * |
2682 | | * @tag_ptr must be a valid pointer to the variable holding the handler. |
2683 | | * |
2684 | | * If the ID is zero then this function does nothing. |
2685 | | * Otherwise, clear_func() is called with the ID as a parameter, and the tag is |
2686 | | * set to zero. |
2687 | | * |
2688 | | * A macro is also included that allows this function to be used without |
2689 | | * pointer casts. |
2690 | | * |
2691 | | * Since: 2.56 |
2692 | | */ |
2693 | | #undef g_clear_handle_id |
2694 | | void |
2695 | | g_clear_handle_id (guint *tag_ptr, |
2696 | | GClearHandleFunc clear_func) |
2697 | 0 | { |
2698 | 0 | guint _handle_id; |
2699 | |
|
2700 | 0 | _handle_id = *tag_ptr; |
2701 | 0 | if (_handle_id > 0) |
2702 | 0 | { |
2703 | 0 | *tag_ptr = 0; |
2704 | 0 | clear_func (_handle_id); |
2705 | 0 | } |
2706 | 0 | } |
2707 | | |
2708 | | #ifdef G_OS_UNIX |
2709 | | /** |
2710 | | * g_source_add_unix_fd: |
2711 | | * @source: a #GSource |
2712 | | * @fd: the fd to monitor |
2713 | | * @events: an event mask |
2714 | | * |
2715 | | * Monitors @fd for the IO events in @events. |
2716 | | * |
2717 | | * The tag returned by this function can be used to remove or modify the |
2718 | | * monitoring of the fd using g_source_remove_unix_fd() or |
2719 | | * g_source_modify_unix_fd(). |
2720 | | * |
2721 | | * It is not necessary to remove the fd before destroying the source; it |
2722 | | * will be cleaned up automatically. |
2723 | | * |
2724 | | * This API is only intended to be used by implementations of #GSource. |
2725 | | * Do not call this API on a #GSource that you did not create. |
2726 | | * |
2727 | | * As the name suggests, this function is not available on Windows. |
2728 | | * |
2729 | | * Returns: (not nullable): an opaque tag |
2730 | | * |
2731 | | * Since: 2.36 |
2732 | | **/ |
2733 | | gpointer |
2734 | | g_source_add_unix_fd (GSource *source, |
2735 | | gint fd, |
2736 | | GIOCondition events) |
2737 | 0 | { |
2738 | 0 | GMainContext *context; |
2739 | 0 | GPollFD *poll_fd; |
2740 | |
|
2741 | 0 | g_return_val_if_fail (source != NULL, NULL); |
2742 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, NULL); |
2743 | 0 | g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL); |
2744 | | |
2745 | 0 | poll_fd = g_new (GPollFD, 1); |
2746 | 0 | poll_fd->fd = fd; |
2747 | 0 | poll_fd->events = events; |
2748 | 0 | poll_fd->revents = 0; |
2749 | |
|
2750 | 0 | context = source->context; |
2751 | |
|
2752 | 0 | if (context) |
2753 | 0 | LOCK_CONTEXT (context); |
2754 | |
|
2755 | 0 | source->priv->fds = g_slist_prepend (source->priv->fds, poll_fd); |
2756 | |
|
2757 | 0 | if (context) |
2758 | 0 | { |
2759 | 0 | if (!SOURCE_BLOCKED (source)) |
2760 | 0 | g_main_context_add_poll_unlocked (context, source->priority, poll_fd); |
2761 | 0 | UNLOCK_CONTEXT (context); |
2762 | 0 | } |
2763 | |
|
2764 | 0 | return poll_fd; |
2765 | 0 | } |
2766 | | |
2767 | | /** |
2768 | | * g_source_modify_unix_fd: |
2769 | | * @source: a #GSource |
2770 | | * @tag: (not nullable): the tag from g_source_add_unix_fd() |
2771 | | * @new_events: the new event mask to watch |
2772 | | * |
2773 | | * Updates the event mask to watch for the fd identified by @tag. |
2774 | | * |
2775 | | * @tag is the tag returned from g_source_add_unix_fd(). |
2776 | | * |
2777 | | * If you want to remove a fd, don't set its event mask to zero. |
2778 | | * Instead, call g_source_remove_unix_fd(). |
2779 | | * |
2780 | | * This API is only intended to be used by implementations of #GSource. |
2781 | | * Do not call this API on a #GSource that you did not create. |
2782 | | * |
2783 | | * As the name suggests, this function is not available on Windows. |
2784 | | * |
2785 | | * Since: 2.36 |
2786 | | **/ |
2787 | | void |
2788 | | g_source_modify_unix_fd (GSource *source, |
2789 | | gpointer tag, |
2790 | | GIOCondition new_events) |
2791 | 0 | { |
2792 | 0 | GMainContext *context; |
2793 | 0 | GPollFD *poll_fd; |
2794 | |
|
2795 | 0 | g_return_if_fail (source != NULL); |
2796 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
2797 | 0 | g_return_if_fail (g_slist_find (source->priv->fds, tag)); |
2798 | | |
2799 | 0 | context = source->context; |
2800 | 0 | poll_fd = tag; |
2801 | |
|
2802 | 0 | poll_fd->events = new_events; |
2803 | |
|
2804 | 0 | if (context) |
2805 | 0 | g_main_context_wakeup (context); |
2806 | 0 | } |
2807 | | |
2808 | | /** |
2809 | | * g_source_remove_unix_fd: |
2810 | | * @source: a #GSource |
2811 | | * @tag: (not nullable): the tag from g_source_add_unix_fd() |
2812 | | * |
2813 | | * Reverses the effect of a previous call to g_source_add_unix_fd(). |
2814 | | * |
2815 | | * You only need to call this if you want to remove an fd from being |
2816 | | * watched while keeping the same source around. In the normal case you |
2817 | | * will just want to destroy the source. |
2818 | | * |
2819 | | * This API is only intended to be used by implementations of #GSource. |
2820 | | * Do not call this API on a #GSource that you did not create. |
2821 | | * |
2822 | | * As the name suggests, this function is not available on Windows. |
2823 | | * |
2824 | | * Since: 2.36 |
2825 | | **/ |
2826 | | void |
2827 | | g_source_remove_unix_fd (GSource *source, |
2828 | | gpointer tag) |
2829 | 0 | { |
2830 | 0 | GMainContext *context; |
2831 | 0 | GPollFD *poll_fd; |
2832 | |
|
2833 | 0 | g_return_if_fail (source != NULL); |
2834 | 0 | g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0); |
2835 | 0 | g_return_if_fail (g_slist_find (source->priv->fds, tag)); |
2836 | | |
2837 | 0 | context = source->context; |
2838 | 0 | poll_fd = tag; |
2839 | |
|
2840 | 0 | if (context) |
2841 | 0 | LOCK_CONTEXT (context); |
2842 | |
|
2843 | 0 | source->priv->fds = g_slist_remove (source->priv->fds, poll_fd); |
2844 | |
|
2845 | 0 | if (context) |
2846 | 0 | { |
2847 | 0 | if (!SOURCE_BLOCKED (source)) |
2848 | 0 | g_main_context_remove_poll_unlocked (context, poll_fd); |
2849 | |
|
2850 | 0 | UNLOCK_CONTEXT (context); |
2851 | 0 | } |
2852 | |
|
2853 | 0 | g_free (poll_fd); |
2854 | 0 | } |
2855 | | |
2856 | | /** |
2857 | | * g_source_query_unix_fd: |
2858 | | * @source: a #GSource |
2859 | | * @tag: (not nullable): the tag from g_source_add_unix_fd() |
2860 | | * |
2861 | | * Queries the events reported for the fd corresponding to @tag on |
2862 | | * @source during the last poll. |
2863 | | * |
2864 | | * The return value of this function is only defined when the function |
2865 | | * is called from the check or dispatch functions for @source. |
2866 | | * |
2867 | | * This API is only intended to be used by implementations of #GSource. |
2868 | | * Do not call this API on a #GSource that you did not create. |
2869 | | * |
2870 | | * As the name suggests, this function is not available on Windows. |
2871 | | * |
2872 | | * Returns: the conditions reported on the fd |
2873 | | * |
2874 | | * Since: 2.36 |
2875 | | **/ |
2876 | | GIOCondition |
2877 | | g_source_query_unix_fd (GSource *source, |
2878 | | gpointer tag) |
2879 | 0 | { |
2880 | 0 | GPollFD *poll_fd; |
2881 | |
|
2882 | 0 | g_return_val_if_fail (source != NULL, 0); |
2883 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0); |
2884 | 0 | g_return_val_if_fail (g_slist_find (source->priv->fds, tag), 0); |
2885 | | |
2886 | 0 | poll_fd = tag; |
2887 | |
|
2888 | 0 | return poll_fd->revents; |
2889 | 0 | } |
2890 | | #endif /* G_OS_UNIX */ |
2891 | | |
2892 | | /** |
2893 | | * g_get_current_time: |
2894 | | * @result: #GTimeVal structure in which to store current time. |
2895 | | * |
2896 | | * Equivalent to the UNIX gettimeofday() function, but portable. |
2897 | | * |
2898 | | * You may find g_get_real_time() to be more convenient. |
2899 | | * |
2900 | | * Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use g_get_real_time() |
2901 | | * instead. |
2902 | | **/ |
2903 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
2904 | | void |
2905 | | g_get_current_time (GTimeVal *result) |
2906 | 9.24k | { |
2907 | 9.24k | gint64 tv; |
2908 | | |
2909 | 9.24k | g_return_if_fail (result != NULL); |
2910 | | |
2911 | 9.24k | tv = g_get_real_time (); |
2912 | | |
2913 | 9.24k | result->tv_sec = tv / 1000000; |
2914 | 9.24k | result->tv_usec = tv % 1000000; |
2915 | 9.24k | } |
2916 | | G_GNUC_END_IGNORE_DEPRECATIONS |
2917 | | |
2918 | | /** |
2919 | | * g_get_real_time: |
2920 | | * |
2921 | | * Queries the system wall-clock time. |
2922 | | * |
2923 | | * This call is functionally equivalent to g_get_current_time() except |
2924 | | * that the return value is often more convenient than dealing with a |
2925 | | * #GTimeVal. |
2926 | | * |
2927 | | * You should only use this call if you are actually interested in the real |
2928 | | * wall-clock time. g_get_monotonic_time() is probably more useful for |
2929 | | * measuring intervals. |
2930 | | * |
2931 | | * Returns: the number of microseconds since January 1, 1970 UTC. |
2932 | | * |
2933 | | * Since: 2.28 |
2934 | | **/ |
2935 | | gint64 |
2936 | | g_get_real_time (void) |
2937 | 1.86M | { |
2938 | 1.86M | #ifndef G_OS_WIN32 |
2939 | 1.86M | struct timeval r; |
2940 | | |
2941 | | /* this is required on alpha, there the timeval structs are ints |
2942 | | * not longs and a cast only would fail horribly */ |
2943 | 1.86M | gettimeofday (&r, NULL); |
2944 | | |
2945 | 1.86M | return (((gint64) r.tv_sec) * 1000000) + r.tv_usec; |
2946 | | #else |
2947 | | FILETIME ft; |
2948 | | guint64 time64; |
2949 | | |
2950 | | GetSystemTimeAsFileTime (&ft); |
2951 | | memmove (&time64, &ft, sizeof (FILETIME)); |
2952 | | |
2953 | | /* Convert from 100s of nanoseconds since 1601-01-01 |
2954 | | * to Unix epoch. This is Y2038 safe. |
2955 | | */ |
2956 | | time64 -= G_GINT64_CONSTANT (116444736000000000); |
2957 | | time64 /= 10; |
2958 | | |
2959 | | return time64; |
2960 | | #endif |
2961 | 1.86M | } |
2962 | | |
2963 | | /** |
2964 | | * g_get_monotonic_time: |
2965 | | * |
2966 | | * Queries the system monotonic time. |
2967 | | * |
2968 | | * The monotonic clock will always increase and doesn't suffer |
2969 | | * discontinuities when the user (or NTP) changes the system time. It |
2970 | | * may or may not continue to tick during times where the machine is |
2971 | | * suspended. |
2972 | | * |
2973 | | * We try to use the clock that corresponds as closely as possible to |
2974 | | * the passage of time as measured by system calls such as poll() but it |
2975 | | * may not always be possible to do this. |
2976 | | * |
2977 | | * Returns: the monotonic time, in microseconds |
2978 | | * |
2979 | | * Since: 2.28 |
2980 | | **/ |
2981 | | #if defined (G_OS_WIN32) |
2982 | | /* NOTE: |
2983 | | * time_usec = ticks_since_boot * usec_per_sec / ticks_per_sec |
2984 | | * |
2985 | | * Doing (ticks_since_boot * usec_per_sec) before the division can overflow 64 bits |
2986 | | * (ticks_since_boot / ticks_per_sec) and then multiply would not be accurate enough. |
2987 | | * So for now we calculate (usec_per_sec / ticks_per_sec) and use floating point |
2988 | | */ |
2989 | | static gdouble g_monotonic_usec_per_tick = 0; |
2990 | | |
2991 | | void |
2992 | | g_clock_win32_init (void) |
2993 | | { |
2994 | | LARGE_INTEGER freq; |
2995 | | |
2996 | | if (!QueryPerformanceFrequency (&freq) || freq.QuadPart == 0) |
2997 | | { |
2998 | | /* The documentation says that this should never happen */ |
2999 | | g_assert_not_reached (); |
3000 | | return; |
3001 | | } |
3002 | | |
3003 | | g_monotonic_usec_per_tick = (gdouble)G_USEC_PER_SEC / freq.QuadPart; |
3004 | | } |
3005 | | |
3006 | | gint64 |
3007 | | g_get_monotonic_time (void) |
3008 | | { |
3009 | | if (G_LIKELY (g_monotonic_usec_per_tick != 0)) |
3010 | | { |
3011 | | LARGE_INTEGER ticks; |
3012 | | |
3013 | | if (QueryPerformanceCounter (&ticks)) |
3014 | | return (gint64)(ticks.QuadPart * g_monotonic_usec_per_tick); |
3015 | | |
3016 | | g_warning ("QueryPerformanceCounter Failed (%lu)", GetLastError ()); |
3017 | | g_monotonic_usec_per_tick = 0; |
3018 | | } |
3019 | | |
3020 | | return 0; |
3021 | | } |
3022 | | #elif defined(HAVE_MACH_MACH_TIME_H) /* Mac OS */ |
3023 | | gint64 |
3024 | | g_get_monotonic_time (void) |
3025 | | { |
3026 | | mach_timebase_info_data_t timebase_info; |
3027 | | guint64 val; |
3028 | | |
3029 | | /* we get nanoseconds from mach_absolute_time() using timebase_info */ |
3030 | | mach_timebase_info (&timebase_info); |
3031 | | val = mach_absolute_time (); |
3032 | | |
3033 | | if (timebase_info.numer != timebase_info.denom) |
3034 | | { |
3035 | | #ifdef HAVE_UINT128_T |
3036 | | val = ((__uint128_t) val * (__uint128_t) timebase_info.numer) / timebase_info.denom / 1000; |
3037 | | #else |
3038 | | guint64 t_high, t_low; |
3039 | | guint64 result_high, result_low; |
3040 | | |
3041 | | /* 64 bit x 32 bit / 32 bit with 96-bit intermediate |
3042 | | * algorithm lifted from qemu */ |
3043 | | t_low = (val & 0xffffffffLL) * (guint64) timebase_info.numer; |
3044 | | t_high = (val >> 32) * (guint64) timebase_info.numer; |
3045 | | t_high += (t_low >> 32); |
3046 | | result_high = t_high / (guint64) timebase_info.denom; |
3047 | | result_low = (((t_high % (guint64) timebase_info.denom) << 32) + |
3048 | | (t_low & 0xffffffff)) / |
3049 | | (guint64) timebase_info.denom; |
3050 | | val = ((result_high << 32) | result_low) / 1000; |
3051 | | #endif |
3052 | | } |
3053 | | else |
3054 | | { |
3055 | | /* nanoseconds to microseconds */ |
3056 | | val = val / 1000; |
3057 | | } |
3058 | | |
3059 | | return val; |
3060 | | } |
3061 | | #else |
3062 | | gint64 |
3063 | | g_get_monotonic_time (void) |
3064 | 3.03k | { |
3065 | 3.03k | struct timespec ts; |
3066 | 3.03k | gint result; |
3067 | | |
3068 | 3.03k | result = clock_gettime (CLOCK_MONOTONIC, &ts); |
3069 | | |
3070 | 3.03k | if G_UNLIKELY (result != 0) |
3071 | 3.03k | g_error ("GLib requires working CLOCK_MONOTONIC"); |
3072 | | |
3073 | 3.03k | return (((gint64) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); |
3074 | 3.03k | } |
3075 | | #endif |
3076 | | |
3077 | | static void |
3078 | | g_main_dispatch_free (gpointer dispatch) |
3079 | 0 | { |
3080 | 0 | g_free (dispatch); |
3081 | 0 | } |
3082 | | |
3083 | | /* Running the main loop */ |
3084 | | |
3085 | | static GMainDispatch * |
3086 | | get_dispatch (void) |
3087 | 0 | { |
3088 | 0 | static GPrivate depth_private = G_PRIVATE_INIT (g_main_dispatch_free); |
3089 | 0 | GMainDispatch *dispatch; |
3090 | |
|
3091 | 0 | dispatch = g_private_get (&depth_private); |
3092 | |
|
3093 | 0 | if (!dispatch) |
3094 | 0 | dispatch = g_private_set_alloc0 (&depth_private, sizeof (GMainDispatch)); |
3095 | |
|
3096 | 0 | return dispatch; |
3097 | 0 | } |
3098 | | |
3099 | | /** |
3100 | | * g_main_depth: |
3101 | | * |
3102 | | * Returns the depth of the stack of calls to |
3103 | | * g_main_context_dispatch() on any #GMainContext in the current thread. |
3104 | | * That is, when called from the toplevel, it gives 0. When |
3105 | | * called from within a callback from g_main_context_iteration() |
3106 | | * (or g_main_loop_run(), etc.) it returns 1. When called from within |
3107 | | * a callback to a recursive call to g_main_context_iteration(), |
3108 | | * it returns 2. And so forth. |
3109 | | * |
3110 | | * This function is useful in a situation like the following: |
3111 | | * Imagine an extremely simple "garbage collected" system. |
3112 | | * |
3113 | | * |[<!-- language="C" --> |
3114 | | * static GList *free_list; |
3115 | | * |
3116 | | * gpointer |
3117 | | * allocate_memory (gsize size) |
3118 | | * { |
3119 | | * gpointer result = g_malloc (size); |
3120 | | * free_list = g_list_prepend (free_list, result); |
3121 | | * return result; |
3122 | | * } |
3123 | | * |
3124 | | * void |
3125 | | * free_allocated_memory (void) |
3126 | | * { |
3127 | | * GList *l; |
3128 | | * for (l = free_list; l; l = l->next); |
3129 | | * g_free (l->data); |
3130 | | * g_list_free (free_list); |
3131 | | * free_list = NULL; |
3132 | | * } |
3133 | | * |
3134 | | * [...] |
3135 | | * |
3136 | | * while (TRUE); |
3137 | | * { |
3138 | | * g_main_context_iteration (NULL, TRUE); |
3139 | | * free_allocated_memory(); |
3140 | | * } |
3141 | | * ]| |
3142 | | * |
3143 | | * This works from an application, however, if you want to do the same |
3144 | | * thing from a library, it gets more difficult, since you no longer |
3145 | | * control the main loop. You might think you can simply use an idle |
3146 | | * function to make the call to free_allocated_memory(), but that |
3147 | | * doesn't work, since the idle function could be called from a |
3148 | | * recursive callback. This can be fixed by using g_main_depth() |
3149 | | * |
3150 | | * |[<!-- language="C" --> |
3151 | | * gpointer |
3152 | | * allocate_memory (gsize size) |
3153 | | * { |
3154 | | * FreeListBlock *block = g_new (FreeListBlock, 1); |
3155 | | * block->mem = g_malloc (size); |
3156 | | * block->depth = g_main_depth (); |
3157 | | * free_list = g_list_prepend (free_list, block); |
3158 | | * return block->mem; |
3159 | | * } |
3160 | | * |
3161 | | * void |
3162 | | * free_allocated_memory (void) |
3163 | | * { |
3164 | | * GList *l; |
3165 | | * |
3166 | | * int depth = g_main_depth (); |
3167 | | * for (l = free_list; l; ); |
3168 | | * { |
3169 | | * GList *next = l->next; |
3170 | | * FreeListBlock *block = l->data; |
3171 | | * if (block->depth > depth) |
3172 | | * { |
3173 | | * g_free (block->mem); |
3174 | | * g_free (block); |
3175 | | * free_list = g_list_delete_link (free_list, l); |
3176 | | * } |
3177 | | * |
3178 | | * l = next; |
3179 | | * } |
3180 | | * } |
3181 | | * ]| |
3182 | | * |
3183 | | * There is a temptation to use g_main_depth() to solve |
3184 | | * problems with reentrancy. For instance, while waiting for data |
3185 | | * to be received from the network in response to a menu item, |
3186 | | * the menu item might be selected again. It might seem that |
3187 | | * one could make the menu item's callback return immediately |
3188 | | * and do nothing if g_main_depth() returns a value greater than 1. |
3189 | | * However, this should be avoided since the user then sees selecting |
3190 | | * the menu item do nothing. Furthermore, you'll find yourself adding |
3191 | | * these checks all over your code, since there are doubtless many, |
3192 | | * many things that the user could do. Instead, you can use the |
3193 | | * following techniques: |
3194 | | * |
3195 | | * 1. Use gtk_widget_set_sensitive() or modal dialogs to prevent |
3196 | | * the user from interacting with elements while the main |
3197 | | * loop is recursing. |
3198 | | * |
3199 | | * 2. Avoid main loop recursion in situations where you can't handle |
3200 | | * arbitrary callbacks. Instead, structure your code so that you |
3201 | | * simply return to the main loop and then get called again when |
3202 | | * there is more work to do. |
3203 | | * |
3204 | | * Returns: The main loop recursion level in the current thread |
3205 | | */ |
3206 | | int |
3207 | | g_main_depth (void) |
3208 | 0 | { |
3209 | 0 | GMainDispatch *dispatch = get_dispatch (); |
3210 | 0 | return dispatch->depth; |
3211 | 0 | } |
3212 | | |
3213 | | /** |
3214 | | * g_main_current_source: |
3215 | | * |
3216 | | * Returns the currently firing source for this thread. |
3217 | | * |
3218 | | * Returns: (transfer none) (nullable): The currently firing source or %NULL. |
3219 | | * |
3220 | | * Since: 2.12 |
3221 | | */ |
3222 | | GSource * |
3223 | | g_main_current_source (void) |
3224 | 0 | { |
3225 | 0 | GMainDispatch *dispatch = get_dispatch (); |
3226 | 0 | return dispatch->source; |
3227 | 0 | } |
3228 | | |
3229 | | /** |
3230 | | * g_source_is_destroyed: |
3231 | | * @source: a #GSource |
3232 | | * |
3233 | | * Returns whether @source has been destroyed. |
3234 | | * |
3235 | | * This is important when you operate upon your objects |
3236 | | * from within idle handlers, but may have freed the object |
3237 | | * before the dispatch of your idle handler. |
3238 | | * |
3239 | | * |[<!-- language="C" --> |
3240 | | * static gboolean |
3241 | | * idle_callback (gpointer data) |
3242 | | * { |
3243 | | * SomeWidget *self = data; |
3244 | | * |
3245 | | * g_mutex_lock (&self->idle_id_mutex); |
3246 | | * // do stuff with self |
3247 | | * self->idle_id = 0; |
3248 | | * g_mutex_unlock (&self->idle_id_mutex); |
3249 | | * |
3250 | | * return G_SOURCE_REMOVE; |
3251 | | * } |
3252 | | * |
3253 | | * static void |
3254 | | * some_widget_do_stuff_later (SomeWidget *self) |
3255 | | * { |
3256 | | * g_mutex_lock (&self->idle_id_mutex); |
3257 | | * self->idle_id = g_idle_add (idle_callback, self); |
3258 | | * g_mutex_unlock (&self->idle_id_mutex); |
3259 | | * } |
3260 | | * |
3261 | | * static void |
3262 | | * some_widget_init (SomeWidget *self) |
3263 | | * { |
3264 | | * g_mutex_init (&self->idle_id_mutex); |
3265 | | * |
3266 | | * // ... |
3267 | | * } |
3268 | | * |
3269 | | * static void |
3270 | | * some_widget_finalize (GObject *object) |
3271 | | * { |
3272 | | * SomeWidget *self = SOME_WIDGET (object); |
3273 | | * |
3274 | | * if (self->idle_id) |
3275 | | * g_source_remove (self->idle_id); |
3276 | | * |
3277 | | * g_mutex_clear (&self->idle_id_mutex); |
3278 | | * |
3279 | | * G_OBJECT_CLASS (parent_class)->finalize (object); |
3280 | | * } |
3281 | | * ]| |
3282 | | * |
3283 | | * This will fail in a multi-threaded application if the |
3284 | | * widget is destroyed before the idle handler fires due |
3285 | | * to the use after free in the callback. A solution, to |
3286 | | * this particular problem, is to check to if the source |
3287 | | * has already been destroy within the callback. |
3288 | | * |
3289 | | * |[<!-- language="C" --> |
3290 | | * static gboolean |
3291 | | * idle_callback (gpointer data) |
3292 | | * { |
3293 | | * SomeWidget *self = data; |
3294 | | * |
3295 | | * g_mutex_lock (&self->idle_id_mutex); |
3296 | | * if (!g_source_is_destroyed (g_main_current_source ())) |
3297 | | * { |
3298 | | * // do stuff with self |
3299 | | * } |
3300 | | * g_mutex_unlock (&self->idle_id_mutex); |
3301 | | * |
3302 | | * return FALSE; |
3303 | | * } |
3304 | | * ]| |
3305 | | * |
3306 | | * Calls to this function from a thread other than the one acquired by the |
3307 | | * #GMainContext the #GSource is attached to are typically redundant, as the |
3308 | | * source could be destroyed immediately after this function returns. However, |
3309 | | * once a source is destroyed it cannot be un-destroyed, so this function can be |
3310 | | * used for opportunistic checks from any thread. |
3311 | | * |
3312 | | * Returns: %TRUE if the source has been destroyed |
3313 | | * |
3314 | | * Since: 2.12 |
3315 | | */ |
3316 | | gboolean |
3317 | | g_source_is_destroyed (GSource *source) |
3318 | 0 | { |
3319 | 0 | g_return_val_if_fail (source != NULL, TRUE); |
3320 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, TRUE); |
3321 | 0 | return SOURCE_DESTROYED (source); |
3322 | 0 | } |
3323 | | |
3324 | | /* Temporarily remove all this source's file descriptors from the |
3325 | | * poll(), so that if data comes available for one of the file descriptors |
3326 | | * we don't continually spin in the poll() |
3327 | | */ |
3328 | | /* HOLDS: source->context's lock */ |
3329 | | static void |
3330 | | block_source (GSource *source) |
3331 | 0 | { |
3332 | 0 | GSList *tmp_list; |
3333 | |
|
3334 | 0 | g_return_if_fail (!SOURCE_BLOCKED (source)); |
3335 | | |
3336 | 0 | source->flags |= G_SOURCE_BLOCKED; |
3337 | |
|
3338 | 0 | if (source->context) |
3339 | 0 | { |
3340 | 0 | tmp_list = source->poll_fds; |
3341 | 0 | while (tmp_list) |
3342 | 0 | { |
3343 | 0 | g_main_context_remove_poll_unlocked (source->context, tmp_list->data); |
3344 | 0 | tmp_list = tmp_list->next; |
3345 | 0 | } |
3346 | |
|
3347 | 0 | for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next) |
3348 | 0 | g_main_context_remove_poll_unlocked (source->context, tmp_list->data); |
3349 | 0 | } |
3350 | |
|
3351 | 0 | if (source->priv && source->priv->child_sources) |
3352 | 0 | { |
3353 | 0 | tmp_list = source->priv->child_sources; |
3354 | 0 | while (tmp_list) |
3355 | 0 | { |
3356 | 0 | block_source (tmp_list->data); |
3357 | 0 | tmp_list = tmp_list->next; |
3358 | 0 | } |
3359 | 0 | } |
3360 | 0 | } |
3361 | | |
3362 | | /* HOLDS: source->context's lock */ |
3363 | | static void |
3364 | | unblock_source (GSource *source) |
3365 | 0 | { |
3366 | 0 | GSList *tmp_list; |
3367 | |
|
3368 | 0 | g_return_if_fail (SOURCE_BLOCKED (source)); /* Source already unblocked */ |
3369 | 0 | g_return_if_fail (!SOURCE_DESTROYED (source)); |
3370 | | |
3371 | 0 | source->flags &= ~G_SOURCE_BLOCKED; |
3372 | |
|
3373 | 0 | tmp_list = source->poll_fds; |
3374 | 0 | while (tmp_list) |
3375 | 0 | { |
3376 | 0 | g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data); |
3377 | 0 | tmp_list = tmp_list->next; |
3378 | 0 | } |
3379 | |
|
3380 | 0 | for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next) |
3381 | 0 | g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data); |
3382 | |
|
3383 | 0 | if (source->priv && source->priv->child_sources) |
3384 | 0 | { |
3385 | 0 | tmp_list = source->priv->child_sources; |
3386 | 0 | while (tmp_list) |
3387 | 0 | { |
3388 | 0 | unblock_source (tmp_list->data); |
3389 | 0 | tmp_list = tmp_list->next; |
3390 | 0 | } |
3391 | 0 | } |
3392 | 0 | } |
3393 | | |
3394 | | /* HOLDS: context's lock */ |
3395 | | static void |
3396 | | g_main_dispatch (GMainContext *context) |
3397 | 0 | { |
3398 | 0 | GMainDispatch *current = get_dispatch (); |
3399 | 0 | guint i; |
3400 | |
|
3401 | 0 | for (i = 0; i < context->pending_dispatches->len; i++) |
3402 | 0 | { |
3403 | 0 | GSource *source = context->pending_dispatches->pdata[i]; |
3404 | |
|
3405 | 0 | context->pending_dispatches->pdata[i] = NULL; |
3406 | 0 | g_assert (source); |
3407 | | |
3408 | 0 | source->flags &= ~G_SOURCE_READY; |
3409 | |
|
3410 | 0 | if (!SOURCE_DESTROYED (source)) |
3411 | 0 | { |
3412 | 0 | gboolean was_in_call; |
3413 | 0 | gpointer user_data = NULL; |
3414 | 0 | GSourceFunc callback = NULL; |
3415 | 0 | GSourceCallbackFuncs *cb_funcs; |
3416 | 0 | gpointer cb_data; |
3417 | 0 | gboolean need_destroy; |
3418 | |
|
3419 | 0 | gboolean (*dispatch) (GSource *, |
3420 | 0 | GSourceFunc, |
3421 | 0 | gpointer); |
3422 | 0 | GSource *prev_source; |
3423 | 0 | gint64 begin_time_nsec G_GNUC_UNUSED; |
3424 | |
|
3425 | 0 | dispatch = source->source_funcs->dispatch; |
3426 | 0 | cb_funcs = source->callback_funcs; |
3427 | 0 | cb_data = source->callback_data; |
3428 | |
|
3429 | 0 | if (cb_funcs) |
3430 | 0 | cb_funcs->ref (cb_data); |
3431 | | |
3432 | 0 | if ((source->flags & G_SOURCE_CAN_RECURSE) == 0) |
3433 | 0 | block_source (source); |
3434 | | |
3435 | 0 | was_in_call = source->flags & G_HOOK_FLAG_IN_CALL; |
3436 | 0 | source->flags |= G_HOOK_FLAG_IN_CALL; |
3437 | |
|
3438 | 0 | if (cb_funcs) |
3439 | 0 | cb_funcs->get (cb_data, source, &callback, &user_data); |
3440 | |
|
3441 | 0 | UNLOCK_CONTEXT (context); |
3442 | | |
3443 | | /* These operations are safe because 'current' is thread-local |
3444 | | * and not modified from anywhere but this function. |
3445 | | */ |
3446 | 0 | prev_source = current->source; |
3447 | 0 | current->source = source; |
3448 | 0 | current->depth++; |
3449 | |
|
3450 | 0 | begin_time_nsec = G_TRACE_CURRENT_TIME; |
3451 | |
|
3452 | 0 | TRACE (GLIB_MAIN_BEFORE_DISPATCH (g_source_get_name (source), source, |
3453 | 0 | dispatch, callback, user_data)); |
3454 | 0 | need_destroy = !(* dispatch) (source, callback, user_data); |
3455 | 0 | TRACE (GLIB_MAIN_AFTER_DISPATCH (g_source_get_name (source), source, |
3456 | 0 | dispatch, need_destroy)); |
3457 | |
|
3458 | 0 | g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec, |
3459 | 0 | "GLib", "GSource.dispatch", |
3460 | 0 | "%s ⇒ %s", |
3461 | 0 | (g_source_get_name (source) != NULL) ? g_source_get_name (source) : "(unnamed)", |
3462 | 0 | need_destroy ? "destroy" : "keep"); |
3463 | |
|
3464 | 0 | current->source = prev_source; |
3465 | 0 | current->depth--; |
3466 | |
|
3467 | 0 | if (cb_funcs) |
3468 | 0 | cb_funcs->unref (cb_data); |
3469 | |
|
3470 | 0 | LOCK_CONTEXT (context); |
3471 | | |
3472 | 0 | if (!was_in_call) |
3473 | 0 | source->flags &= ~G_HOOK_FLAG_IN_CALL; |
3474 | |
|
3475 | 0 | if (SOURCE_BLOCKED (source) && !SOURCE_DESTROYED (source)) |
3476 | 0 | unblock_source (source); |
3477 | | |
3478 | | /* Note: this depends on the fact that we can't switch |
3479 | | * sources from one main context to another |
3480 | | */ |
3481 | 0 | if (need_destroy && !SOURCE_DESTROYED (source)) |
3482 | 0 | { |
3483 | 0 | g_assert (source->context == context); |
3484 | 0 | g_source_destroy_internal (source, context, TRUE); |
3485 | 0 | } |
3486 | 0 | } |
3487 | | |
3488 | 0 | g_source_unref_internal (source, context, TRUE); |
3489 | 0 | } |
3490 | | |
3491 | 0 | g_ptr_array_set_size (context->pending_dispatches, 0); |
3492 | 0 | } |
3493 | | |
3494 | | /** |
3495 | | * g_main_context_acquire: |
3496 | | * @context: a #GMainContext |
3497 | | * |
3498 | | * Tries to become the owner of the specified context. |
3499 | | * If some other thread is the owner of the context, |
3500 | | * returns %FALSE immediately. Ownership is properly |
3501 | | * recursive: the owner can require ownership again |
3502 | | * and will release ownership when g_main_context_release() |
3503 | | * is called as many times as g_main_context_acquire(). |
3504 | | * |
3505 | | * You must be the owner of a context before you |
3506 | | * can call g_main_context_prepare(), g_main_context_query(), |
3507 | | * g_main_context_check(), g_main_context_dispatch(). |
3508 | | * |
3509 | | * Returns: %TRUE if the operation succeeded, and |
3510 | | * this thread is now the owner of @context. |
3511 | | **/ |
3512 | | gboolean |
3513 | | g_main_context_acquire (GMainContext *context) |
3514 | 0 | { |
3515 | 0 | gboolean result = FALSE; |
3516 | 0 | GThread *self = G_THREAD_SELF; |
3517 | |
|
3518 | 0 | if (context == NULL) |
3519 | 0 | context = g_main_context_default (); |
3520 | | |
3521 | 0 | LOCK_CONTEXT (context); |
3522 | |
|
3523 | 0 | if (!context->owner) |
3524 | 0 | { |
3525 | 0 | context->owner = self; |
3526 | 0 | g_assert (context->owner_count == 0); |
3527 | 0 | TRACE (GLIB_MAIN_CONTEXT_ACQUIRE (context, TRUE /* success */)); |
3528 | 0 | } |
3529 | | |
3530 | 0 | if (context->owner == self) |
3531 | 0 | { |
3532 | 0 | context->owner_count++; |
3533 | 0 | result = TRUE; |
3534 | 0 | } |
3535 | 0 | else |
3536 | 0 | { |
3537 | 0 | TRACE (GLIB_MAIN_CONTEXT_ACQUIRE (context, FALSE /* failure */)); |
3538 | 0 | } |
3539 | |
|
3540 | 0 | UNLOCK_CONTEXT (context); |
3541 | |
|
3542 | 0 | return result; |
3543 | 0 | } |
3544 | | |
3545 | | /** |
3546 | | * g_main_context_release: |
3547 | | * @context: a #GMainContext |
3548 | | * |
3549 | | * Releases ownership of a context previously acquired by this thread |
3550 | | * with g_main_context_acquire(). If the context was acquired multiple |
3551 | | * times, the ownership will be released only when g_main_context_release() |
3552 | | * is called as many times as it was acquired. |
3553 | | **/ |
3554 | | void |
3555 | | g_main_context_release (GMainContext *context) |
3556 | 0 | { |
3557 | 0 | if (context == NULL) |
3558 | 0 | context = g_main_context_default (); |
3559 | | |
3560 | 0 | LOCK_CONTEXT (context); |
3561 | |
|
3562 | 0 | context->owner_count--; |
3563 | 0 | if (context->owner_count == 0) |
3564 | 0 | { |
3565 | 0 | TRACE (GLIB_MAIN_CONTEXT_RELEASE (context)); |
3566 | |
|
3567 | 0 | context->owner = NULL; |
3568 | |
|
3569 | 0 | if (context->waiters) |
3570 | 0 | { |
3571 | 0 | GMainWaiter *waiter = context->waiters->data; |
3572 | 0 | gboolean loop_internal_waiter = (waiter->mutex == &context->mutex); |
3573 | 0 | context->waiters = g_slist_delete_link (context->waiters, |
3574 | 0 | context->waiters); |
3575 | 0 | if (!loop_internal_waiter) |
3576 | 0 | g_mutex_lock (waiter->mutex); |
3577 | | |
3578 | 0 | g_cond_signal (waiter->cond); |
3579 | | |
3580 | 0 | if (!loop_internal_waiter) |
3581 | 0 | g_mutex_unlock (waiter->mutex); |
3582 | 0 | } |
3583 | 0 | } |
3584 | |
|
3585 | 0 | UNLOCK_CONTEXT (context); |
3586 | 0 | } |
3587 | | |
3588 | | static gboolean |
3589 | | g_main_context_wait_internal (GMainContext *context, |
3590 | | GCond *cond, |
3591 | | GMutex *mutex) |
3592 | 0 | { |
3593 | 0 | gboolean result = FALSE; |
3594 | 0 | GThread *self = G_THREAD_SELF; |
3595 | 0 | gboolean loop_internal_waiter; |
3596 | | |
3597 | 0 | if (context == NULL) |
3598 | 0 | context = g_main_context_default (); |
3599 | |
|
3600 | 0 | loop_internal_waiter = (mutex == &context->mutex); |
3601 | | |
3602 | 0 | if (!loop_internal_waiter) |
3603 | 0 | LOCK_CONTEXT (context); |
3604 | |
|
3605 | 0 | if (context->owner && context->owner != self) |
3606 | 0 | { |
3607 | 0 | GMainWaiter waiter; |
3608 | |
|
3609 | 0 | waiter.cond = cond; |
3610 | 0 | waiter.mutex = mutex; |
3611 | |
|
3612 | 0 | context->waiters = g_slist_append (context->waiters, &waiter); |
3613 | | |
3614 | 0 | if (!loop_internal_waiter) |
3615 | 0 | UNLOCK_CONTEXT (context); |
3616 | 0 | g_cond_wait (cond, mutex); |
3617 | 0 | if (!loop_internal_waiter) |
3618 | 0 | LOCK_CONTEXT (context); |
3619 | |
|
3620 | 0 | context->waiters = g_slist_remove (context->waiters, &waiter); |
3621 | 0 | } |
3622 | |
|
3623 | 0 | if (!context->owner) |
3624 | 0 | { |
3625 | 0 | context->owner = self; |
3626 | 0 | g_assert (context->owner_count == 0); |
3627 | 0 | } |
3628 | | |
3629 | 0 | if (context->owner == self) |
3630 | 0 | { |
3631 | 0 | context->owner_count++; |
3632 | 0 | result = TRUE; |
3633 | 0 | } |
3634 | |
|
3635 | 0 | if (!loop_internal_waiter) |
3636 | 0 | UNLOCK_CONTEXT (context); |
3637 | | |
3638 | 0 | return result; |
3639 | 0 | } |
3640 | | |
3641 | | /** |
3642 | | * g_main_context_wait: |
3643 | | * @context: a #GMainContext |
3644 | | * @cond: a condition variable |
3645 | | * @mutex: a mutex, currently held |
3646 | | * |
3647 | | * Tries to become the owner of the specified context, |
3648 | | * as with g_main_context_acquire(). But if another thread |
3649 | | * is the owner, atomically drop @mutex and wait on @cond until |
3650 | | * that owner releases ownership or until @cond is signaled, then |
3651 | | * try again (once) to become the owner. |
3652 | | * |
3653 | | * Returns: %TRUE if the operation succeeded, and |
3654 | | * this thread is now the owner of @context. |
3655 | | * Deprecated: 2.58: Use g_main_context_is_owner() and separate locking instead. |
3656 | | */ |
3657 | | gboolean |
3658 | | g_main_context_wait (GMainContext *context, |
3659 | | GCond *cond, |
3660 | | GMutex *mutex) |
3661 | 0 | { |
3662 | 0 | if (context == NULL) |
3663 | 0 | context = g_main_context_default (); |
3664 | |
|
3665 | 0 | if (G_UNLIKELY (cond != &context->cond || mutex != &context->mutex)) |
3666 | 0 | { |
3667 | 0 | static gboolean warned; |
3668 | |
|
3669 | 0 | if (!warned) |
3670 | 0 | { |
3671 | 0 | g_critical ("WARNING!! g_main_context_wait() will be removed in a future release. " |
3672 | 0 | "If you see this message, please file a bug immediately."); |
3673 | 0 | warned = TRUE; |
3674 | 0 | } |
3675 | 0 | } |
3676 | |
|
3677 | 0 | return g_main_context_wait_internal (context, cond, mutex); |
3678 | 0 | } |
3679 | | |
3680 | | /** |
3681 | | * g_main_context_prepare: |
3682 | | * @context: a #GMainContext |
3683 | | * @priority: (out) (optional): location to store priority of highest priority |
3684 | | * source already ready. |
3685 | | * |
3686 | | * Prepares to poll sources within a main loop. The resulting information |
3687 | | * for polling is determined by calling g_main_context_query (). |
3688 | | * |
3689 | | * You must have successfully acquired the context with |
3690 | | * g_main_context_acquire() before you may call this function. |
3691 | | * |
3692 | | * Returns: %TRUE if some source is ready to be dispatched |
3693 | | * prior to polling. |
3694 | | **/ |
3695 | | gboolean |
3696 | | g_main_context_prepare (GMainContext *context, |
3697 | | gint *priority) |
3698 | 0 | { |
3699 | 0 | guint i; |
3700 | 0 | gint n_ready = 0; |
3701 | 0 | gint current_priority = G_MAXINT; |
3702 | 0 | GSource *source; |
3703 | 0 | GSourceIter iter; |
3704 | |
|
3705 | 0 | if (context == NULL) |
3706 | 0 | context = g_main_context_default (); |
3707 | | |
3708 | 0 | LOCK_CONTEXT (context); |
3709 | |
|
3710 | 0 | context->time_is_fresh = FALSE; |
3711 | |
|
3712 | 0 | if (context->in_check_or_prepare) |
3713 | 0 | { |
3714 | 0 | g_warning ("g_main_context_prepare() called recursively from within a source's check() or " |
3715 | 0 | "prepare() member."); |
3716 | 0 | UNLOCK_CONTEXT (context); |
3717 | 0 | return FALSE; |
3718 | 0 | } |
3719 | | |
3720 | 0 | TRACE (GLIB_MAIN_CONTEXT_BEFORE_PREPARE (context)); |
3721 | |
|
3722 | | #if 0 |
3723 | | /* If recursing, finish up current dispatch, before starting over */ |
3724 | | if (context->pending_dispatches) |
3725 | | { |
3726 | | if (dispatch) |
3727 | | g_main_dispatch (context, ¤t_time); |
3728 | | |
3729 | | UNLOCK_CONTEXT (context); |
3730 | | return TRUE; |
3731 | | } |
3732 | | #endif |
3733 | | |
3734 | | /* If recursing, clear list of pending dispatches */ |
3735 | |
|
3736 | 0 | for (i = 0; i < context->pending_dispatches->len; i++) |
3737 | 0 | { |
3738 | 0 | if (context->pending_dispatches->pdata[i]) |
3739 | 0 | g_source_unref_internal ((GSource *)context->pending_dispatches->pdata[i], context, TRUE); |
3740 | 0 | } |
3741 | 0 | g_ptr_array_set_size (context->pending_dispatches, 0); |
3742 | | |
3743 | | /* Prepare all sources */ |
3744 | |
|
3745 | 0 | context->timeout = -1; |
3746 | | |
3747 | 0 | g_source_iter_init (&iter, context, TRUE); |
3748 | 0 | while (g_source_iter_next (&iter, &source)) |
3749 | 0 | { |
3750 | 0 | gint source_timeout = -1; |
3751 | |
|
3752 | 0 | if (SOURCE_DESTROYED (source) || SOURCE_BLOCKED (source)) |
3753 | 0 | continue; |
3754 | 0 | if ((n_ready > 0) && (source->priority > current_priority)) |
3755 | 0 | break; |
3756 | | |
3757 | 0 | if (!(source->flags & G_SOURCE_READY)) |
3758 | 0 | { |
3759 | 0 | gboolean result; |
3760 | 0 | gboolean (* prepare) (GSource *source, |
3761 | 0 | gint *timeout); |
3762 | |
|
3763 | 0 | prepare = source->source_funcs->prepare; |
3764 | |
|
3765 | 0 | if (prepare) |
3766 | 0 | { |
3767 | 0 | gint64 begin_time_nsec G_GNUC_UNUSED; |
3768 | |
|
3769 | 0 | context->in_check_or_prepare++; |
3770 | 0 | UNLOCK_CONTEXT (context); |
3771 | |
|
3772 | 0 | begin_time_nsec = G_TRACE_CURRENT_TIME; |
3773 | |
|
3774 | 0 | result = (* prepare) (source, &source_timeout); |
3775 | 0 | TRACE (GLIB_MAIN_AFTER_PREPARE (source, prepare, source_timeout)); |
3776 | |
|
3777 | 0 | g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec, |
3778 | 0 | "GLib", "GSource.prepare", |
3779 | 0 | "%s ⇒ %s", |
3780 | 0 | (g_source_get_name (source) != NULL) ? g_source_get_name (source) : "(unnamed)", |
3781 | 0 | result ? "ready" : "unready"); |
3782 | |
|
3783 | 0 | LOCK_CONTEXT (context); |
3784 | 0 | context->in_check_or_prepare--; |
3785 | 0 | } |
3786 | 0 | else |
3787 | 0 | { |
3788 | 0 | source_timeout = -1; |
3789 | 0 | result = FALSE; |
3790 | 0 | } |
3791 | |
|
3792 | 0 | if (result == FALSE && source->priv->ready_time != -1) |
3793 | 0 | { |
3794 | 0 | if (!context->time_is_fresh) |
3795 | 0 | { |
3796 | 0 | context->time = g_get_monotonic_time (); |
3797 | 0 | context->time_is_fresh = TRUE; |
3798 | 0 | } |
3799 | |
|
3800 | 0 | if (source->priv->ready_time <= context->time) |
3801 | 0 | { |
3802 | 0 | source_timeout = 0; |
3803 | 0 | result = TRUE; |
3804 | 0 | } |
3805 | 0 | else |
3806 | 0 | { |
3807 | 0 | gint64 timeout; |
3808 | | |
3809 | | /* rounding down will lead to spinning, so always round up */ |
3810 | 0 | timeout = (source->priv->ready_time - context->time + 999) / 1000; |
3811 | |
|
3812 | 0 | if (source_timeout < 0 || timeout < source_timeout) |
3813 | 0 | source_timeout = MIN (timeout, G_MAXINT); |
3814 | 0 | } |
3815 | 0 | } |
3816 | |
|
3817 | 0 | if (result) |
3818 | 0 | { |
3819 | 0 | GSource *ready_source = source; |
3820 | |
|
3821 | 0 | while (ready_source) |
3822 | 0 | { |
3823 | 0 | ready_source->flags |= G_SOURCE_READY; |
3824 | 0 | ready_source = ready_source->priv->parent_source; |
3825 | 0 | } |
3826 | 0 | } |
3827 | 0 | } |
3828 | |
|
3829 | 0 | if (source->flags & G_SOURCE_READY) |
3830 | 0 | { |
3831 | 0 | n_ready++; |
3832 | 0 | current_priority = source->priority; |
3833 | 0 | context->timeout = 0; |
3834 | 0 | } |
3835 | | |
3836 | 0 | if (source_timeout >= 0) |
3837 | 0 | { |
3838 | 0 | if (context->timeout < 0) |
3839 | 0 | context->timeout = source_timeout; |
3840 | 0 | else |
3841 | 0 | context->timeout = MIN (context->timeout, source_timeout); |
3842 | 0 | } |
3843 | 0 | } |
3844 | 0 | g_source_iter_clear (&iter); |
3845 | |
|
3846 | 0 | TRACE (GLIB_MAIN_CONTEXT_AFTER_PREPARE (context, current_priority, n_ready)); |
3847 | |
|
3848 | 0 | UNLOCK_CONTEXT (context); |
3849 | | |
3850 | 0 | if (priority) |
3851 | 0 | *priority = current_priority; |
3852 | | |
3853 | 0 | return (n_ready > 0); |
3854 | 0 | } |
3855 | | |
3856 | | /** |
3857 | | * g_main_context_query: |
3858 | | * @context: a #GMainContext |
3859 | | * @max_priority: maximum priority source to check |
3860 | | * @timeout_: (out): location to store timeout to be used in polling |
3861 | | * @fds: (out caller-allocates) (array length=n_fds): location to |
3862 | | * store #GPollFD records that need to be polled. |
3863 | | * @n_fds: (in): length of @fds. |
3864 | | * |
3865 | | * Determines information necessary to poll this main loop. You should |
3866 | | * be careful to pass the resulting @fds array and its length @n_fds |
3867 | | * as is when calling g_main_context_check(), as this function relies |
3868 | | * on assumptions made when the array is filled. |
3869 | | * |
3870 | | * You must have successfully acquired the context with |
3871 | | * g_main_context_acquire() before you may call this function. |
3872 | | * |
3873 | | * Returns: the number of records actually stored in @fds, |
3874 | | * or, if more than @n_fds records need to be stored, the number |
3875 | | * of records that need to be stored. |
3876 | | **/ |
3877 | | gint |
3878 | | g_main_context_query (GMainContext *context, |
3879 | | gint max_priority, |
3880 | | gint *timeout, |
3881 | | GPollFD *fds, |
3882 | | gint n_fds) |
3883 | 0 | { |
3884 | 0 | gint n_poll; |
3885 | 0 | GPollRec *pollrec, *lastpollrec; |
3886 | 0 | gushort events; |
3887 | | |
3888 | 0 | LOCK_CONTEXT (context); |
3889 | |
|
3890 | 0 | TRACE (GLIB_MAIN_CONTEXT_BEFORE_QUERY (context, max_priority)); |
3891 | | |
3892 | | /* fds is filled sequentially from poll_records. Since poll_records |
3893 | | * are incrementally sorted by file descriptor identifier, fds will |
3894 | | * also be incrementally sorted. |
3895 | | */ |
3896 | 0 | n_poll = 0; |
3897 | 0 | lastpollrec = NULL; |
3898 | 0 | for (pollrec = context->poll_records; pollrec; pollrec = pollrec->next) |
3899 | 0 | { |
3900 | 0 | if (pollrec->priority > max_priority) |
3901 | 0 | continue; |
3902 | | |
3903 | | /* In direct contradiction to the Unix98 spec, IRIX runs into |
3904 | | * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL |
3905 | | * flags in the events field of the pollfd while it should |
3906 | | * just ignoring them. So we mask them out here. |
3907 | | */ |
3908 | 0 | events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL); |
3909 | | |
3910 | | /* This optimization --using the same GPollFD to poll for more |
3911 | | * than one poll record-- relies on the poll records being |
3912 | | * incrementally sorted. |
3913 | | */ |
3914 | 0 | if (lastpollrec && pollrec->fd->fd == lastpollrec->fd->fd) |
3915 | 0 | { |
3916 | 0 | if (n_poll - 1 < n_fds) |
3917 | 0 | fds[n_poll - 1].events |= events; |
3918 | 0 | } |
3919 | 0 | else |
3920 | 0 | { |
3921 | 0 | if (n_poll < n_fds) |
3922 | 0 | { |
3923 | 0 | fds[n_poll].fd = pollrec->fd->fd; |
3924 | 0 | fds[n_poll].events = events; |
3925 | 0 | fds[n_poll].revents = 0; |
3926 | 0 | } |
3927 | |
|
3928 | 0 | n_poll++; |
3929 | 0 | } |
3930 | |
|
3931 | 0 | lastpollrec = pollrec; |
3932 | 0 | } |
3933 | |
|
3934 | 0 | context->poll_changed = FALSE; |
3935 | | |
3936 | 0 | if (timeout) |
3937 | 0 | { |
3938 | 0 | *timeout = context->timeout; |
3939 | 0 | if (*timeout != 0) |
3940 | 0 | context->time_is_fresh = FALSE; |
3941 | 0 | } |
3942 | |
|
3943 | 0 | TRACE (GLIB_MAIN_CONTEXT_AFTER_QUERY (context, context->timeout, |
3944 | 0 | fds, n_poll)); |
3945 | |
|
3946 | 0 | UNLOCK_CONTEXT (context); |
3947 | |
|
3948 | 0 | return n_poll; |
3949 | 0 | } |
3950 | | |
3951 | | /** |
3952 | | * g_main_context_check: |
3953 | | * @context: a #GMainContext |
3954 | | * @max_priority: the maximum numerical priority of sources to check |
3955 | | * @fds: (array length=n_fds): array of #GPollFD's that was passed to |
3956 | | * the last call to g_main_context_query() |
3957 | | * @n_fds: return value of g_main_context_query() |
3958 | | * |
3959 | | * Passes the results of polling back to the main loop. You should be |
3960 | | * careful to pass @fds and its length @n_fds as received from |
3961 | | * g_main_context_query(), as this functions relies on assumptions |
3962 | | * on how @fds is filled. |
3963 | | * |
3964 | | * You must have successfully acquired the context with |
3965 | | * g_main_context_acquire() before you may call this function. |
3966 | | * |
3967 | | * Returns: %TRUE if some sources are ready to be dispatched. |
3968 | | **/ |
3969 | | gboolean |
3970 | | g_main_context_check (GMainContext *context, |
3971 | | gint max_priority, |
3972 | | GPollFD *fds, |
3973 | | gint n_fds) |
3974 | 0 | { |
3975 | 0 | GSource *source; |
3976 | 0 | GSourceIter iter; |
3977 | 0 | GPollRec *pollrec; |
3978 | 0 | gint n_ready = 0; |
3979 | 0 | gint i; |
3980 | | |
3981 | 0 | LOCK_CONTEXT (context); |
3982 | |
|
3983 | 0 | if (context->in_check_or_prepare) |
3984 | 0 | { |
3985 | 0 | g_warning ("g_main_context_check() called recursively from within a source's check() or " |
3986 | 0 | "prepare() member."); |
3987 | 0 | UNLOCK_CONTEXT (context); |
3988 | 0 | return FALSE; |
3989 | 0 | } |
3990 | | |
3991 | 0 | TRACE (GLIB_MAIN_CONTEXT_BEFORE_CHECK (context, max_priority, fds, n_fds)); |
3992 | |
|
3993 | 0 | for (i = 0; i < n_fds; i++) |
3994 | 0 | { |
3995 | 0 | if (fds[i].fd == context->wake_up_rec.fd) |
3996 | 0 | { |
3997 | 0 | if (fds[i].revents) |
3998 | 0 | { |
3999 | 0 | TRACE (GLIB_MAIN_CONTEXT_WAKEUP_ACKNOWLEDGE (context)); |
4000 | 0 | g_wakeup_acknowledge (context->wakeup); |
4001 | 0 | } |
4002 | 0 | break; |
4003 | 0 | } |
4004 | 0 | } |
4005 | | |
4006 | | /* If the set of poll file descriptors changed, bail out |
4007 | | * and let the main loop rerun |
4008 | | */ |
4009 | 0 | if (context->poll_changed) |
4010 | 0 | { |
4011 | 0 | TRACE (GLIB_MAIN_CONTEXT_AFTER_CHECK (context, 0)); |
4012 | |
|
4013 | 0 | UNLOCK_CONTEXT (context); |
4014 | 0 | return FALSE; |
4015 | 0 | } |
4016 | | |
4017 | | /* The linear iteration below relies on the assumption that both |
4018 | | * poll records and the fds array are incrementally sorted by file |
4019 | | * descriptor identifier. |
4020 | | */ |
4021 | 0 | pollrec = context->poll_records; |
4022 | 0 | i = 0; |
4023 | 0 | while (pollrec && i < n_fds) |
4024 | 0 | { |
4025 | | /* Make sure that fds is sorted by file descriptor identifier. */ |
4026 | 0 | g_assert (i <= 0 || fds[i - 1].fd < fds[i].fd); |
4027 | | |
4028 | | /* Skip until finding the first GPollRec matching the current GPollFD. */ |
4029 | 0 | while (pollrec && pollrec->fd->fd != fds[i].fd) |
4030 | 0 | pollrec = pollrec->next; |
4031 | | |
4032 | | /* Update all consecutive GPollRecs that match. */ |
4033 | 0 | while (pollrec && pollrec->fd->fd == fds[i].fd) |
4034 | 0 | { |
4035 | 0 | if (pollrec->priority <= max_priority) |
4036 | 0 | { |
4037 | 0 | pollrec->fd->revents = |
4038 | 0 | fds[i].revents & (pollrec->fd->events | G_IO_ERR | G_IO_HUP | G_IO_NVAL); |
4039 | 0 | } |
4040 | 0 | pollrec = pollrec->next; |
4041 | 0 | } |
4042 | | |
4043 | | /* Iterate to next GPollFD. */ |
4044 | 0 | i++; |
4045 | 0 | } |
4046 | | |
4047 | 0 | g_source_iter_init (&iter, context, TRUE); |
4048 | 0 | while (g_source_iter_next (&iter, &source)) |
4049 | 0 | { |
4050 | 0 | if (SOURCE_DESTROYED (source) || SOURCE_BLOCKED (source)) |
4051 | 0 | continue; |
4052 | 0 | if ((n_ready > 0) && (source->priority > max_priority)) |
4053 | 0 | break; |
4054 | | |
4055 | 0 | if (!(source->flags & G_SOURCE_READY)) |
4056 | 0 | { |
4057 | 0 | gboolean result; |
4058 | 0 | gboolean (* check) (GSource *source); |
4059 | |
|
4060 | 0 | check = source->source_funcs->check; |
4061 | |
|
4062 | 0 | if (check) |
4063 | 0 | { |
4064 | 0 | gint64 begin_time_nsec G_GNUC_UNUSED; |
4065 | | |
4066 | | /* If the check function is set, call it. */ |
4067 | 0 | context->in_check_or_prepare++; |
4068 | 0 | UNLOCK_CONTEXT (context); |
4069 | |
|
4070 | 0 | begin_time_nsec = G_TRACE_CURRENT_TIME; |
4071 | |
|
4072 | 0 | result = (* check) (source); |
4073 | |
|
4074 | 0 | TRACE (GLIB_MAIN_AFTER_CHECK (source, check, result)); |
4075 | |
|
4076 | 0 | g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec, |
4077 | 0 | "GLib", "GSource.check", |
4078 | 0 | "%s ⇒ %s", |
4079 | 0 | (g_source_get_name (source) != NULL) ? g_source_get_name (source) : "(unnamed)", |
4080 | 0 | result ? "dispatch" : "ignore"); |
4081 | |
|
4082 | 0 | LOCK_CONTEXT (context); |
4083 | 0 | context->in_check_or_prepare--; |
4084 | 0 | } |
4085 | 0 | else |
4086 | 0 | result = FALSE; |
4087 | |
|
4088 | 0 | if (result == FALSE) |
4089 | 0 | { |
4090 | 0 | GSList *tmp_list; |
4091 | | |
4092 | | /* If not already explicitly flagged ready by ->check() |
4093 | | * (or if we have no check) then we can still be ready if |
4094 | | * any of our fds poll as ready. |
4095 | | */ |
4096 | 0 | for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next) |
4097 | 0 | { |
4098 | 0 | GPollFD *pollfd = tmp_list->data; |
4099 | |
|
4100 | 0 | if (pollfd->revents) |
4101 | 0 | { |
4102 | 0 | result = TRUE; |
4103 | 0 | break; |
4104 | 0 | } |
4105 | 0 | } |
4106 | 0 | } |
4107 | |
|
4108 | 0 | if (result == FALSE && source->priv->ready_time != -1) |
4109 | 0 | { |
4110 | 0 | if (!context->time_is_fresh) |
4111 | 0 | { |
4112 | 0 | context->time = g_get_monotonic_time (); |
4113 | 0 | context->time_is_fresh = TRUE; |
4114 | 0 | } |
4115 | |
|
4116 | 0 | if (source->priv->ready_time <= context->time) |
4117 | 0 | result = TRUE; |
4118 | 0 | } |
4119 | |
|
4120 | 0 | if (result) |
4121 | 0 | { |
4122 | 0 | GSource *ready_source = source; |
4123 | |
|
4124 | 0 | while (ready_source) |
4125 | 0 | { |
4126 | 0 | ready_source->flags |= G_SOURCE_READY; |
4127 | 0 | ready_source = ready_source->priv->parent_source; |
4128 | 0 | } |
4129 | 0 | } |
4130 | 0 | } |
4131 | |
|
4132 | 0 | if (source->flags & G_SOURCE_READY) |
4133 | 0 | { |
4134 | 0 | g_source_ref (source); |
4135 | 0 | g_ptr_array_add (context->pending_dispatches, source); |
4136 | |
|
4137 | 0 | n_ready++; |
4138 | | |
4139 | | /* never dispatch sources with less priority than the first |
4140 | | * one we choose to dispatch |
4141 | | */ |
4142 | 0 | max_priority = source->priority; |
4143 | 0 | } |
4144 | 0 | } |
4145 | 0 | g_source_iter_clear (&iter); |
4146 | |
|
4147 | 0 | TRACE (GLIB_MAIN_CONTEXT_AFTER_CHECK (context, n_ready)); |
4148 | |
|
4149 | 0 | UNLOCK_CONTEXT (context); |
4150 | |
|
4151 | 0 | return n_ready > 0; |
4152 | 0 | } |
4153 | | |
4154 | | /** |
4155 | | * g_main_context_dispatch: |
4156 | | * @context: a #GMainContext |
4157 | | * |
4158 | | * Dispatches all pending sources. |
4159 | | * |
4160 | | * You must have successfully acquired the context with |
4161 | | * g_main_context_acquire() before you may call this function. |
4162 | | **/ |
4163 | | void |
4164 | | g_main_context_dispatch (GMainContext *context) |
4165 | 0 | { |
4166 | 0 | LOCK_CONTEXT (context); |
4167 | |
|
4168 | 0 | TRACE (GLIB_MAIN_CONTEXT_BEFORE_DISPATCH (context)); |
4169 | |
|
4170 | 0 | if (context->pending_dispatches->len > 0) |
4171 | 0 | { |
4172 | 0 | g_main_dispatch (context); |
4173 | 0 | } |
4174 | |
|
4175 | 0 | TRACE (GLIB_MAIN_CONTEXT_AFTER_DISPATCH (context)); |
4176 | |
|
4177 | 0 | UNLOCK_CONTEXT (context); |
4178 | 0 | } |
4179 | | |
4180 | | /* HOLDS context lock */ |
4181 | | static gboolean |
4182 | | g_main_context_iterate (GMainContext *context, |
4183 | | gboolean block, |
4184 | | gboolean dispatch, |
4185 | | GThread *self) |
4186 | 0 | { |
4187 | 0 | gint max_priority = 0; |
4188 | 0 | gint timeout; |
4189 | 0 | gboolean some_ready; |
4190 | 0 | gint nfds, allocated_nfds; |
4191 | 0 | GPollFD *fds = NULL; |
4192 | 0 | gint64 begin_time_nsec G_GNUC_UNUSED; |
4193 | |
|
4194 | 0 | UNLOCK_CONTEXT (context); |
4195 | |
|
4196 | 0 | begin_time_nsec = G_TRACE_CURRENT_TIME; |
4197 | |
|
4198 | 0 | if (!g_main_context_acquire (context)) |
4199 | 0 | { |
4200 | 0 | gboolean got_ownership; |
4201 | |
|
4202 | 0 | LOCK_CONTEXT (context); |
4203 | |
|
4204 | 0 | if (!block) |
4205 | 0 | return FALSE; |
4206 | | |
4207 | 0 | got_ownership = g_main_context_wait_internal (context, |
4208 | 0 | &context->cond, |
4209 | 0 | &context->mutex); |
4210 | |
|
4211 | 0 | if (!got_ownership) |
4212 | 0 | return FALSE; |
4213 | 0 | } |
4214 | 0 | else |
4215 | 0 | LOCK_CONTEXT (context); |
4216 | | |
4217 | 0 | if (!context->cached_poll_array) |
4218 | 0 | { |
4219 | 0 | context->cached_poll_array_size = context->n_poll_records; |
4220 | 0 | context->cached_poll_array = g_new (GPollFD, context->n_poll_records); |
4221 | 0 | } |
4222 | |
|
4223 | 0 | allocated_nfds = context->cached_poll_array_size; |
4224 | 0 | fds = context->cached_poll_array; |
4225 | | |
4226 | 0 | UNLOCK_CONTEXT (context); |
4227 | |
|
4228 | 0 | g_main_context_prepare (context, &max_priority); |
4229 | | |
4230 | 0 | while ((nfds = g_main_context_query (context, max_priority, &timeout, fds, |
4231 | 0 | allocated_nfds)) > allocated_nfds) |
4232 | 0 | { |
4233 | 0 | LOCK_CONTEXT (context); |
4234 | 0 | g_free (fds); |
4235 | 0 | context->cached_poll_array_size = allocated_nfds = nfds; |
4236 | 0 | context->cached_poll_array = fds = g_new (GPollFD, nfds); |
4237 | 0 | UNLOCK_CONTEXT (context); |
4238 | 0 | } |
4239 | |
|
4240 | 0 | if (!block) |
4241 | 0 | timeout = 0; |
4242 | | |
4243 | 0 | g_main_context_poll (context, timeout, max_priority, fds, nfds); |
4244 | | |
4245 | 0 | some_ready = g_main_context_check (context, max_priority, fds, nfds); |
4246 | | |
4247 | 0 | if (dispatch) |
4248 | 0 | g_main_context_dispatch (context); |
4249 | | |
4250 | 0 | g_main_context_release (context); |
4251 | |
|
4252 | 0 | g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec, |
4253 | 0 | "GLib", "g_main_context_iterate", |
4254 | 0 | "Context %p, %s ⇒ %s", context, block ? "blocking" : "non-blocking", some_ready ? "dispatched" : "nothing"); |
4255 | |
|
4256 | 0 | LOCK_CONTEXT (context); |
4257 | |
|
4258 | 0 | return some_ready; |
4259 | 0 | } |
4260 | | |
4261 | | /** |
4262 | | * g_main_context_pending: |
4263 | | * @context: (nullable): a #GMainContext (if %NULL, the default context will be used) |
4264 | | * |
4265 | | * Checks if any sources have pending events for the given context. |
4266 | | * |
4267 | | * Returns: %TRUE if events are pending. |
4268 | | **/ |
4269 | | gboolean |
4270 | | g_main_context_pending (GMainContext *context) |
4271 | 0 | { |
4272 | 0 | gboolean retval; |
4273 | |
|
4274 | 0 | if (!context) |
4275 | 0 | context = g_main_context_default(); |
4276 | |
|
4277 | 0 | LOCK_CONTEXT (context); |
4278 | 0 | retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF); |
4279 | 0 | UNLOCK_CONTEXT (context); |
4280 | | |
4281 | 0 | return retval; |
4282 | 0 | } |
4283 | | |
4284 | | /** |
4285 | | * g_main_context_iteration: |
4286 | | * @context: (nullable): a #GMainContext (if %NULL, the default context will be used) |
4287 | | * @may_block: whether the call may block. |
4288 | | * |
4289 | | * Runs a single iteration for the given main loop. This involves |
4290 | | * checking to see if any event sources are ready to be processed, |
4291 | | * then if no events sources are ready and @may_block is %TRUE, waiting |
4292 | | * for a source to become ready, then dispatching the highest priority |
4293 | | * events sources that are ready. Otherwise, if @may_block is %FALSE |
4294 | | * sources are not waited to become ready, only those highest priority |
4295 | | * events sources will be dispatched (if any), that are ready at this |
4296 | | * given moment without further waiting. |
4297 | | * |
4298 | | * Note that even when @may_block is %TRUE, it is still possible for |
4299 | | * g_main_context_iteration() to return %FALSE, since the wait may |
4300 | | * be interrupted for other reasons than an event source becoming ready. |
4301 | | * |
4302 | | * Returns: %TRUE if events were dispatched. |
4303 | | **/ |
4304 | | gboolean |
4305 | | g_main_context_iteration (GMainContext *context, gboolean may_block) |
4306 | 0 | { |
4307 | 0 | gboolean retval; |
4308 | |
|
4309 | 0 | if (!context) |
4310 | 0 | context = g_main_context_default(); |
4311 | | |
4312 | 0 | LOCK_CONTEXT (context); |
4313 | 0 | retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF); |
4314 | 0 | UNLOCK_CONTEXT (context); |
4315 | | |
4316 | 0 | return retval; |
4317 | 0 | } |
4318 | | |
4319 | | /** |
4320 | | * g_main_loop_new: |
4321 | | * @context: (nullable): a #GMainContext (if %NULL, the default context will be used). |
4322 | | * @is_running: set to %TRUE to indicate that the loop is running. This |
4323 | | * is not very important since calling g_main_loop_run() will set this to |
4324 | | * %TRUE anyway. |
4325 | | * |
4326 | | * Creates a new #GMainLoop structure. |
4327 | | * |
4328 | | * Returns: a new #GMainLoop. |
4329 | | **/ |
4330 | | GMainLoop * |
4331 | | g_main_loop_new (GMainContext *context, |
4332 | | gboolean is_running) |
4333 | 0 | { |
4334 | 0 | GMainLoop *loop; |
4335 | |
|
4336 | 0 | if (!context) |
4337 | 0 | context = g_main_context_default(); |
4338 | | |
4339 | 0 | g_main_context_ref (context); |
4340 | |
|
4341 | 0 | loop = g_new0 (GMainLoop, 1); |
4342 | 0 | loop->context = context; |
4343 | 0 | loop->is_running = is_running != FALSE; |
4344 | 0 | loop->ref_count = 1; |
4345 | |
|
4346 | 0 | TRACE (GLIB_MAIN_LOOP_NEW (loop, context)); |
4347 | |
|
4348 | 0 | return loop; |
4349 | 0 | } |
4350 | | |
4351 | | /** |
4352 | | * g_main_loop_ref: |
4353 | | * @loop: a #GMainLoop |
4354 | | * |
4355 | | * Increases the reference count on a #GMainLoop object by one. |
4356 | | * |
4357 | | * Returns: @loop |
4358 | | **/ |
4359 | | GMainLoop * |
4360 | | g_main_loop_ref (GMainLoop *loop) |
4361 | 0 | { |
4362 | 0 | g_return_val_if_fail (loop != NULL, NULL); |
4363 | 0 | g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL); |
4364 | | |
4365 | 0 | g_atomic_int_inc (&loop->ref_count); |
4366 | |
|
4367 | 0 | return loop; |
4368 | 0 | } |
4369 | | |
4370 | | /** |
4371 | | * g_main_loop_unref: |
4372 | | * @loop: a #GMainLoop |
4373 | | * |
4374 | | * Decreases the reference count on a #GMainLoop object by one. If |
4375 | | * the result is zero, free the loop and free all associated memory. |
4376 | | **/ |
4377 | | void |
4378 | | g_main_loop_unref (GMainLoop *loop) |
4379 | 0 | { |
4380 | 0 | g_return_if_fail (loop != NULL); |
4381 | 0 | g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0); |
4382 | | |
4383 | 0 | if (!g_atomic_int_dec_and_test (&loop->ref_count)) |
4384 | 0 | return; |
4385 | | |
4386 | 0 | g_main_context_unref (loop->context); |
4387 | 0 | g_free (loop); |
4388 | 0 | } |
4389 | | |
4390 | | /** |
4391 | | * g_main_loop_run: |
4392 | | * @loop: a #GMainLoop |
4393 | | * |
4394 | | * Runs a main loop until g_main_loop_quit() is called on the loop. |
4395 | | * If this is called for the thread of the loop's #GMainContext, |
4396 | | * it will process events from the loop, otherwise it will |
4397 | | * simply wait. |
4398 | | **/ |
4399 | | void |
4400 | | g_main_loop_run (GMainLoop *loop) |
4401 | 0 | { |
4402 | 0 | GThread *self = G_THREAD_SELF; |
4403 | |
|
4404 | 0 | g_return_if_fail (loop != NULL); |
4405 | 0 | g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0); |
4406 | | |
4407 | | /* Hold a reference in case the loop is unreffed from a callback function */ |
4408 | 0 | g_atomic_int_inc (&loop->ref_count); |
4409 | |
|
4410 | 0 | if (!g_main_context_acquire (loop->context)) |
4411 | 0 | { |
4412 | 0 | gboolean got_ownership = FALSE; |
4413 | | |
4414 | | /* Another thread owns this context */ |
4415 | 0 | LOCK_CONTEXT (loop->context); |
4416 | |
|
4417 | 0 | g_atomic_int_set (&loop->is_running, TRUE); |
4418 | |
|
4419 | 0 | while (g_atomic_int_get (&loop->is_running) && !got_ownership) |
4420 | 0 | got_ownership = g_main_context_wait_internal (loop->context, |
4421 | 0 | &loop->context->cond, |
4422 | 0 | &loop->context->mutex); |
4423 | | |
4424 | 0 | if (!g_atomic_int_get (&loop->is_running)) |
4425 | 0 | { |
4426 | 0 | UNLOCK_CONTEXT (loop->context); |
4427 | 0 | if (got_ownership) |
4428 | 0 | g_main_context_release (loop->context); |
4429 | 0 | g_main_loop_unref (loop); |
4430 | 0 | return; |
4431 | 0 | } |
4432 | | |
4433 | 0 | g_assert (got_ownership); |
4434 | 0 | } |
4435 | 0 | else |
4436 | 0 | LOCK_CONTEXT (loop->context); |
4437 | | |
4438 | 0 | if (loop->context->in_check_or_prepare) |
4439 | 0 | { |
4440 | 0 | g_warning ("g_main_loop_run(): called recursively from within a source's " |
4441 | 0 | "check() or prepare() member, iteration not possible."); |
4442 | 0 | g_main_loop_unref (loop); |
4443 | 0 | return; |
4444 | 0 | } |
4445 | | |
4446 | 0 | g_atomic_int_set (&loop->is_running, TRUE); |
4447 | 0 | while (g_atomic_int_get (&loop->is_running)) |
4448 | 0 | g_main_context_iterate (loop->context, TRUE, TRUE, self); |
4449 | |
|
4450 | 0 | UNLOCK_CONTEXT (loop->context); |
4451 | | |
4452 | 0 | g_main_context_release (loop->context); |
4453 | | |
4454 | 0 | g_main_loop_unref (loop); |
4455 | 0 | } |
4456 | | |
4457 | | /** |
4458 | | * g_main_loop_quit: |
4459 | | * @loop: a #GMainLoop |
4460 | | * |
4461 | | * Stops a #GMainLoop from running. Any calls to g_main_loop_run() |
4462 | | * for the loop will return. |
4463 | | * |
4464 | | * Note that sources that have already been dispatched when |
4465 | | * g_main_loop_quit() is called will still be executed. |
4466 | | **/ |
4467 | | void |
4468 | | g_main_loop_quit (GMainLoop *loop) |
4469 | 0 | { |
4470 | 0 | g_return_if_fail (loop != NULL); |
4471 | 0 | g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0); |
4472 | | |
4473 | 0 | LOCK_CONTEXT (loop->context); |
4474 | 0 | g_atomic_int_set (&loop->is_running, FALSE); |
4475 | 0 | g_wakeup_signal (loop->context->wakeup); |
4476 | |
|
4477 | 0 | g_cond_broadcast (&loop->context->cond); |
4478 | |
|
4479 | 0 | UNLOCK_CONTEXT (loop->context); |
4480 | |
|
4481 | 0 | TRACE (GLIB_MAIN_LOOP_QUIT (loop)); |
4482 | 0 | } |
4483 | | |
4484 | | /** |
4485 | | * g_main_loop_is_running: |
4486 | | * @loop: a #GMainLoop. |
4487 | | * |
4488 | | * Checks to see if the main loop is currently being run via g_main_loop_run(). |
4489 | | * |
4490 | | * Returns: %TRUE if the mainloop is currently being run. |
4491 | | **/ |
4492 | | gboolean |
4493 | | g_main_loop_is_running (GMainLoop *loop) |
4494 | 0 | { |
4495 | 0 | g_return_val_if_fail (loop != NULL, FALSE); |
4496 | 0 | g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE); |
4497 | | |
4498 | 0 | return g_atomic_int_get (&loop->is_running); |
4499 | 0 | } |
4500 | | |
4501 | | /** |
4502 | | * g_main_loop_get_context: |
4503 | | * @loop: a #GMainLoop. |
4504 | | * |
4505 | | * Returns the #GMainContext of @loop. |
4506 | | * |
4507 | | * Returns: (transfer none): the #GMainContext of @loop |
4508 | | **/ |
4509 | | GMainContext * |
4510 | | g_main_loop_get_context (GMainLoop *loop) |
4511 | 0 | { |
4512 | 0 | g_return_val_if_fail (loop != NULL, NULL); |
4513 | 0 | g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL); |
4514 | | |
4515 | 0 | return loop->context; |
4516 | 0 | } |
4517 | | |
4518 | | /* HOLDS: context's lock */ |
4519 | | static void |
4520 | | g_main_context_poll (GMainContext *context, |
4521 | | gint timeout, |
4522 | | gint priority, |
4523 | | GPollFD *fds, |
4524 | | gint n_fds) |
4525 | 0 | { |
4526 | | #ifdef G_MAIN_POLL_DEBUG |
4527 | | GTimer *poll_timer; |
4528 | | GPollRec *pollrec; |
4529 | | gint i; |
4530 | | #endif |
4531 | |
|
4532 | 0 | GPollFunc poll_func; |
4533 | |
|
4534 | 0 | if (n_fds || timeout != 0) |
4535 | 0 | { |
4536 | 0 | int ret, errsv; |
4537 | |
|
4538 | | #ifdef G_MAIN_POLL_DEBUG |
4539 | | poll_timer = NULL; |
4540 | | if (_g_main_poll_debug) |
4541 | | { |
4542 | | g_print ("polling context=%p n=%d timeout=%d\n", |
4543 | | context, n_fds, timeout); |
4544 | | poll_timer = g_timer_new (); |
4545 | | } |
4546 | | #endif |
4547 | |
|
4548 | 0 | LOCK_CONTEXT (context); |
4549 | |
|
4550 | 0 | poll_func = context->poll_func; |
4551 | |
|
4552 | 0 | UNLOCK_CONTEXT (context); |
4553 | 0 | ret = (*poll_func) (fds, n_fds, timeout); |
4554 | 0 | errsv = errno; |
4555 | 0 | if (ret < 0 && errsv != EINTR) |
4556 | 0 | { |
4557 | 0 | #ifndef G_OS_WIN32 |
4558 | 0 | g_warning ("poll(2) failed due to: %s.", |
4559 | 0 | g_strerror (errsv)); |
4560 | | #else |
4561 | | /* If g_poll () returns -1, it has already called g_warning() */ |
4562 | | #endif |
4563 | 0 | } |
4564 | | |
4565 | | #ifdef G_MAIN_POLL_DEBUG |
4566 | | if (_g_main_poll_debug) |
4567 | | { |
4568 | | LOCK_CONTEXT (context); |
4569 | | |
4570 | | g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds", |
4571 | | n_fds, |
4572 | | timeout, |
4573 | | g_timer_elapsed (poll_timer, NULL)); |
4574 | | g_timer_destroy (poll_timer); |
4575 | | pollrec = context->poll_records; |
4576 | | |
4577 | | while (pollrec != NULL) |
4578 | | { |
4579 | | i = 0; |
4580 | | while (i < n_fds) |
4581 | | { |
4582 | | if (fds[i].fd == pollrec->fd->fd && |
4583 | | pollrec->fd->events && |
4584 | | fds[i].revents) |
4585 | | { |
4586 | | g_print (" [" G_POLLFD_FORMAT " :", fds[i].fd); |
4587 | | if (fds[i].revents & G_IO_IN) |
4588 | | g_print ("i"); |
4589 | | if (fds[i].revents & G_IO_OUT) |
4590 | | g_print ("o"); |
4591 | | if (fds[i].revents & G_IO_PRI) |
4592 | | g_print ("p"); |
4593 | | if (fds[i].revents & G_IO_ERR) |
4594 | | g_print ("e"); |
4595 | | if (fds[i].revents & G_IO_HUP) |
4596 | | g_print ("h"); |
4597 | | if (fds[i].revents & G_IO_NVAL) |
4598 | | g_print ("n"); |
4599 | | g_print ("]"); |
4600 | | } |
4601 | | i++; |
4602 | | } |
4603 | | pollrec = pollrec->next; |
4604 | | } |
4605 | | g_print ("\n"); |
4606 | | |
4607 | | UNLOCK_CONTEXT (context); |
4608 | | } |
4609 | | #endif |
4610 | 0 | } /* if (n_fds || timeout != 0) */ |
4611 | 0 | } |
4612 | | |
4613 | | /** |
4614 | | * g_main_context_add_poll: |
4615 | | * @context: (nullable): a #GMainContext (or %NULL for the default context) |
4616 | | * @fd: a #GPollFD structure holding information about a file |
4617 | | * descriptor to watch. |
4618 | | * @priority: the priority for this file descriptor which should be |
4619 | | * the same as the priority used for g_source_attach() to ensure that the |
4620 | | * file descriptor is polled whenever the results may be needed. |
4621 | | * |
4622 | | * Adds a file descriptor to the set of file descriptors polled for |
4623 | | * this context. This will very seldom be used directly. Instead |
4624 | | * a typical event source will use g_source_add_unix_fd() instead. |
4625 | | **/ |
4626 | | void |
4627 | | g_main_context_add_poll (GMainContext *context, |
4628 | | GPollFD *fd, |
4629 | | gint priority) |
4630 | 0 | { |
4631 | 0 | if (!context) |
4632 | 0 | context = g_main_context_default (); |
4633 | | |
4634 | 0 | g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); |
4635 | 0 | g_return_if_fail (fd); |
4636 | | |
4637 | 0 | LOCK_CONTEXT (context); |
4638 | 0 | g_main_context_add_poll_unlocked (context, priority, fd); |
4639 | 0 | UNLOCK_CONTEXT (context); |
4640 | 0 | } |
4641 | | |
4642 | | /* HOLDS: main_loop_lock */ |
4643 | | static void |
4644 | | g_main_context_add_poll_unlocked (GMainContext *context, |
4645 | | gint priority, |
4646 | | GPollFD *fd) |
4647 | 396 | { |
4648 | 396 | GPollRec *prevrec, *nextrec; |
4649 | 396 | GPollRec *newrec = g_slice_new (GPollRec); |
4650 | | |
4651 | | /* This file descriptor may be checked before we ever poll */ |
4652 | 396 | fd->revents = 0; |
4653 | 396 | newrec->fd = fd; |
4654 | 396 | newrec->priority = priority; |
4655 | | |
4656 | | /* Poll records are incrementally sorted by file descriptor identifier. */ |
4657 | 396 | prevrec = NULL; |
4658 | 396 | nextrec = context->poll_records; |
4659 | 75.6k | while (nextrec) |
4660 | 75.3k | { |
4661 | 75.3k | if (nextrec->fd->fd > fd->fd) |
4662 | 42 | break; |
4663 | 75.2k | prevrec = nextrec; |
4664 | 75.2k | nextrec = nextrec->next; |
4665 | 75.2k | } |
4666 | | |
4667 | 396 | if (prevrec) |
4668 | 388 | prevrec->next = newrec; |
4669 | 8 | else |
4670 | 8 | context->poll_records = newrec; |
4671 | | |
4672 | 396 | newrec->prev = prevrec; |
4673 | 396 | newrec->next = nextrec; |
4674 | | |
4675 | 396 | if (nextrec) |
4676 | 42 | nextrec->prev = newrec; |
4677 | | |
4678 | 396 | context->n_poll_records++; |
4679 | | |
4680 | 396 | context->poll_changed = TRUE; |
4681 | | |
4682 | | /* Now wake up the main loop if it is waiting in the poll() */ |
4683 | 396 | if (fd != &context->wake_up_rec) |
4684 | 388 | g_wakeup_signal (context->wakeup); |
4685 | 396 | } |
4686 | | |
4687 | | /** |
4688 | | * g_main_context_remove_poll: |
4689 | | * @context:a #GMainContext |
4690 | | * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll() |
4691 | | * |
4692 | | * Removes file descriptor from the set of file descriptors to be |
4693 | | * polled for a particular context. |
4694 | | **/ |
4695 | | void |
4696 | | g_main_context_remove_poll (GMainContext *context, |
4697 | | GPollFD *fd) |
4698 | 0 | { |
4699 | 0 | if (!context) |
4700 | 0 | context = g_main_context_default (); |
4701 | | |
4702 | 0 | g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); |
4703 | 0 | g_return_if_fail (fd); |
4704 | | |
4705 | 0 | LOCK_CONTEXT (context); |
4706 | 0 | g_main_context_remove_poll_unlocked (context, fd); |
4707 | 0 | UNLOCK_CONTEXT (context); |
4708 | 0 | } |
4709 | | |
4710 | | static void |
4711 | | g_main_context_remove_poll_unlocked (GMainContext *context, |
4712 | | GPollFD *fd) |
4713 | 0 | { |
4714 | 0 | GPollRec *pollrec, *prevrec, *nextrec; |
4715 | |
|
4716 | 0 | prevrec = NULL; |
4717 | 0 | pollrec = context->poll_records; |
4718 | |
|
4719 | 0 | while (pollrec) |
4720 | 0 | { |
4721 | 0 | nextrec = pollrec->next; |
4722 | 0 | if (pollrec->fd == fd) |
4723 | 0 | { |
4724 | 0 | if (prevrec != NULL) |
4725 | 0 | prevrec->next = nextrec; |
4726 | 0 | else |
4727 | 0 | context->poll_records = nextrec; |
4728 | |
|
4729 | 0 | if (nextrec != NULL) |
4730 | 0 | nextrec->prev = prevrec; |
4731 | |
|
4732 | 0 | g_slice_free (GPollRec, pollrec); |
4733 | |
|
4734 | 0 | context->n_poll_records--; |
4735 | 0 | break; |
4736 | 0 | } |
4737 | 0 | prevrec = pollrec; |
4738 | 0 | pollrec = nextrec; |
4739 | 0 | } |
4740 | |
|
4741 | 0 | context->poll_changed = TRUE; |
4742 | | |
4743 | | /* Now wake up the main loop if it is waiting in the poll() */ |
4744 | 0 | g_wakeup_signal (context->wakeup); |
4745 | 0 | } |
4746 | | |
4747 | | /** |
4748 | | * g_source_get_current_time: |
4749 | | * @source: a #GSource |
4750 | | * @timeval: #GTimeVal structure in which to store current time. |
4751 | | * |
4752 | | * This function ignores @source and is otherwise the same as |
4753 | | * g_get_current_time(). |
4754 | | * |
4755 | | * Deprecated: 2.28: use g_source_get_time() instead |
4756 | | **/ |
4757 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
4758 | | void |
4759 | | g_source_get_current_time (GSource *source, |
4760 | | GTimeVal *timeval) |
4761 | 0 | { |
4762 | 0 | g_get_current_time (timeval); |
4763 | 0 | } |
4764 | | G_GNUC_END_IGNORE_DEPRECATIONS |
4765 | | |
4766 | | /** |
4767 | | * g_source_get_time: |
4768 | | * @source: a #GSource |
4769 | | * |
4770 | | * Gets the time to be used when checking this source. The advantage of |
4771 | | * calling this function over calling g_get_monotonic_time() directly is |
4772 | | * that when checking multiple sources, GLib can cache a single value |
4773 | | * instead of having to repeatedly get the system monotonic time. |
4774 | | * |
4775 | | * The time here is the system monotonic time, if available, or some |
4776 | | * other reasonable alternative otherwise. See g_get_monotonic_time(). |
4777 | | * |
4778 | | * Returns: the monotonic time in microseconds |
4779 | | * |
4780 | | * Since: 2.28 |
4781 | | **/ |
4782 | | gint64 |
4783 | | g_source_get_time (GSource *source) |
4784 | 0 | { |
4785 | 0 | GMainContext *context; |
4786 | 0 | gint64 result; |
4787 | |
|
4788 | 0 | g_return_val_if_fail (source != NULL, 0); |
4789 | 0 | g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0); |
4790 | 0 | g_return_val_if_fail (source->context != NULL, 0); |
4791 | | |
4792 | 0 | context = source->context; |
4793 | |
|
4794 | 0 | LOCK_CONTEXT (context); |
4795 | |
|
4796 | 0 | if (!context->time_is_fresh) |
4797 | 0 | { |
4798 | 0 | context->time = g_get_monotonic_time (); |
4799 | 0 | context->time_is_fresh = TRUE; |
4800 | 0 | } |
4801 | |
|
4802 | 0 | result = context->time; |
4803 | |
|
4804 | 0 | UNLOCK_CONTEXT (context); |
4805 | |
|
4806 | 0 | return result; |
4807 | 0 | } |
4808 | | |
4809 | | /** |
4810 | | * g_main_context_set_poll_func: |
4811 | | * @context: a #GMainContext |
4812 | | * @func: the function to call to poll all file descriptors |
4813 | | * |
4814 | | * Sets the function to use to handle polling of file descriptors. It |
4815 | | * will be used instead of the poll() system call |
4816 | | * (or GLib's replacement function, which is used where |
4817 | | * poll() isn't available). |
4818 | | * |
4819 | | * This function could possibly be used to integrate the GLib event |
4820 | | * loop with an external event loop. |
4821 | | **/ |
4822 | | void |
4823 | | g_main_context_set_poll_func (GMainContext *context, |
4824 | | GPollFunc func) |
4825 | 0 | { |
4826 | 0 | if (!context) |
4827 | 0 | context = g_main_context_default (); |
4828 | | |
4829 | 0 | g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); |
4830 | | |
4831 | 0 | LOCK_CONTEXT (context); |
4832 | | |
4833 | 0 | if (func) |
4834 | 0 | context->poll_func = func; |
4835 | 0 | else |
4836 | 0 | context->poll_func = g_poll; |
4837 | |
|
4838 | 0 | UNLOCK_CONTEXT (context); |
4839 | 0 | } |
4840 | | |
4841 | | /** |
4842 | | * g_main_context_get_poll_func: |
4843 | | * @context: a #GMainContext |
4844 | | * |
4845 | | * Gets the poll function set by g_main_context_set_poll_func(). |
4846 | | * |
4847 | | * Returns: the poll function |
4848 | | **/ |
4849 | | GPollFunc |
4850 | | g_main_context_get_poll_func (GMainContext *context) |
4851 | 0 | { |
4852 | 0 | GPollFunc result; |
4853 | | |
4854 | 0 | if (!context) |
4855 | 0 | context = g_main_context_default (); |
4856 | | |
4857 | 0 | g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL); |
4858 | | |
4859 | 0 | LOCK_CONTEXT (context); |
4860 | 0 | result = context->poll_func; |
4861 | 0 | UNLOCK_CONTEXT (context); |
4862 | |
|
4863 | 0 | return result; |
4864 | 0 | } |
4865 | | |
4866 | | /** |
4867 | | * g_main_context_wakeup: |
4868 | | * @context: a #GMainContext |
4869 | | * |
4870 | | * If @context is currently blocking in g_main_context_iteration() |
4871 | | * waiting for a source to become ready, cause it to stop blocking |
4872 | | * and return. Otherwise, cause the next invocation of |
4873 | | * g_main_context_iteration() to return without blocking. |
4874 | | * |
4875 | | * This API is useful for low-level control over #GMainContext; for |
4876 | | * example, integrating it with main loop implementations such as |
4877 | | * #GMainLoop. |
4878 | | * |
4879 | | * Another related use for this function is when implementing a main |
4880 | | * loop with a termination condition, computed from multiple threads: |
4881 | | * |
4882 | | * |[<!-- language="C" --> |
4883 | | * #define NUM_TASKS 10 |
4884 | | * static gint tasks_remaining = NUM_TASKS; // (atomic) |
4885 | | * ... |
4886 | | * |
4887 | | * while (g_atomic_int_get (&tasks_remaining) != 0) |
4888 | | * g_main_context_iteration (NULL, TRUE); |
4889 | | * ]| |
4890 | | * |
4891 | | * Then in a thread: |
4892 | | * |[<!-- language="C" --> |
4893 | | * perform_work(); |
4894 | | * |
4895 | | * if (g_atomic_int_dec_and_test (&tasks_remaining)) |
4896 | | * g_main_context_wakeup (NULL); |
4897 | | * ]| |
4898 | | **/ |
4899 | | void |
4900 | | g_main_context_wakeup (GMainContext *context) |
4901 | 0 | { |
4902 | 0 | if (!context) |
4903 | 0 | context = g_main_context_default (); |
4904 | |
|
4905 | 0 | g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); |
4906 | | |
4907 | 0 | TRACE (GLIB_MAIN_CONTEXT_WAKEUP (context)); |
4908 | |
|
4909 | 0 | g_wakeup_signal (context->wakeup); |
4910 | 0 | } |
4911 | | |
4912 | | /** |
4913 | | * g_main_context_is_owner: |
4914 | | * @context: a #GMainContext |
4915 | | * |
4916 | | * Determines whether this thread holds the (recursive) |
4917 | | * ownership of this #GMainContext. This is useful to |
4918 | | * know before waiting on another thread that may be |
4919 | | * blocking to get ownership of @context. |
4920 | | * |
4921 | | * Returns: %TRUE if current thread is owner of @context. |
4922 | | * |
4923 | | * Since: 2.10 |
4924 | | **/ |
4925 | | gboolean |
4926 | | g_main_context_is_owner (GMainContext *context) |
4927 | 0 | { |
4928 | 0 | gboolean is_owner; |
4929 | |
|
4930 | 0 | if (!context) |
4931 | 0 | context = g_main_context_default (); |
4932 | |
|
4933 | 0 | LOCK_CONTEXT (context); |
4934 | 0 | is_owner = context->owner == G_THREAD_SELF; |
4935 | 0 | UNLOCK_CONTEXT (context); |
4936 | |
|
4937 | 0 | return is_owner; |
4938 | 0 | } |
4939 | | |
4940 | | /* Timeouts */ |
4941 | | |
4942 | | static void |
4943 | | g_timeout_set_expiration (GTimeoutSource *timeout_source, |
4944 | | gint64 current_time) |
4945 | 3.03k | { |
4946 | 3.03k | gint64 expiration; |
4947 | | |
4948 | 3.03k | if (timeout_source->seconds) |
4949 | 0 | { |
4950 | 0 | gint64 remainder; |
4951 | 0 | static gint timer_perturb = -1; |
4952 | |
|
4953 | 0 | if (timer_perturb == -1) |
4954 | 0 | { |
4955 | | /* |
4956 | | * we want a per machine/session unique 'random' value; try the dbus |
4957 | | * address first, that has a UUID in it. If there is no dbus, use the |
4958 | | * hostname for hashing. |
4959 | | */ |
4960 | 0 | const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS"); |
4961 | 0 | if (!session_bus_address) |
4962 | 0 | session_bus_address = g_getenv ("HOSTNAME"); |
4963 | 0 | if (session_bus_address) |
4964 | 0 | timer_perturb = ABS ((gint) g_str_hash (session_bus_address)) % 1000000; |
4965 | 0 | else |
4966 | 0 | timer_perturb = 0; |
4967 | 0 | } |
4968 | |
|
4969 | 0 | expiration = current_time + (guint64) timeout_source->interval * 1000 * 1000; |
4970 | | |
4971 | | /* We want the microseconds part of the timeout to land on the |
4972 | | * 'timer_perturb' mark, but we need to make sure we don't try to |
4973 | | * set the timeout in the past. We do this by ensuring that we |
4974 | | * always only *increase* the expiration time by adding a full |
4975 | | * second in the case that the microsecond portion decreases. |
4976 | | */ |
4977 | 0 | expiration -= timer_perturb; |
4978 | |
|
4979 | 0 | remainder = expiration % 1000000; |
4980 | 0 | if (remainder >= 1000000/4) |
4981 | 0 | expiration += 1000000; |
4982 | |
|
4983 | 0 | expiration -= remainder; |
4984 | 0 | expiration += timer_perturb; |
4985 | 0 | } |
4986 | 3.03k | else |
4987 | 3.03k | { |
4988 | 3.03k | expiration = current_time + (guint64) timeout_source->interval * 1000; |
4989 | 3.03k | } |
4990 | | |
4991 | 3.03k | g_source_set_ready_time ((GSource *) timeout_source, expiration); |
4992 | 3.03k | } |
4993 | | |
4994 | | static gboolean |
4995 | | g_timeout_dispatch (GSource *source, |
4996 | | GSourceFunc callback, |
4997 | | gpointer user_data) |
4998 | 0 | { |
4999 | 0 | GTimeoutSource *timeout_source = (GTimeoutSource *)source; |
5000 | 0 | gboolean again; |
5001 | |
|
5002 | 0 | if (!callback) |
5003 | 0 | { |
5004 | 0 | g_warning ("Timeout source dispatched without callback. " |
5005 | 0 | "You must call g_source_set_callback()."); |
5006 | 0 | return FALSE; |
5007 | 0 | } |
5008 | | |
5009 | 0 | if (timeout_source->one_shot) |
5010 | 0 | { |
5011 | 0 | GSourceOnceFunc once_callback = (GSourceOnceFunc) callback; |
5012 | 0 | once_callback (user_data); |
5013 | 0 | again = G_SOURCE_REMOVE; |
5014 | 0 | } |
5015 | 0 | else |
5016 | 0 | { |
5017 | 0 | again = callback (user_data); |
5018 | 0 | } |
5019 | |
|
5020 | 0 | TRACE (GLIB_TIMEOUT_DISPATCH (source, source->context, callback, user_data, again)); |
5021 | |
|
5022 | 0 | if (again) |
5023 | 0 | g_timeout_set_expiration (timeout_source, g_source_get_time (source)); |
5024 | |
|
5025 | 0 | return again; |
5026 | 0 | } |
5027 | | |
5028 | | static GSource * |
5029 | | timeout_source_new (guint interval, |
5030 | | gboolean seconds, |
5031 | | gboolean one_shot) |
5032 | 3.03k | { |
5033 | 3.03k | GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource)); |
5034 | 3.03k | GTimeoutSource *timeout_source = (GTimeoutSource *)source; |
5035 | | |
5036 | 3.03k | timeout_source->interval = interval; |
5037 | 3.03k | timeout_source->seconds = seconds; |
5038 | 3.03k | timeout_source->one_shot = one_shot; |
5039 | | |
5040 | 3.03k | g_timeout_set_expiration (timeout_source, g_get_monotonic_time ()); |
5041 | | |
5042 | 3.03k | return source; |
5043 | 3.03k | } |
5044 | | |
5045 | | /** |
5046 | | * g_timeout_source_new: |
5047 | | * @interval: the timeout interval in milliseconds. |
5048 | | * |
5049 | | * Creates a new timeout source. |
5050 | | * |
5051 | | * The source will not initially be associated with any #GMainContext |
5052 | | * and must be added to one with g_source_attach() before it will be |
5053 | | * executed. |
5054 | | * |
5055 | | * The interval given is in terms of monotonic time, not wall clock |
5056 | | * time. See g_get_monotonic_time(). |
5057 | | * |
5058 | | * Returns: the newly-created timeout source |
5059 | | **/ |
5060 | | GSource * |
5061 | | g_timeout_source_new (guint interval) |
5062 | 0 | { |
5063 | 0 | return timeout_source_new (interval, FALSE, FALSE); |
5064 | 0 | } |
5065 | | |
5066 | | /** |
5067 | | * g_timeout_source_new_seconds: |
5068 | | * @interval: the timeout interval in seconds |
5069 | | * |
5070 | | * Creates a new timeout source. |
5071 | | * |
5072 | | * The source will not initially be associated with any #GMainContext |
5073 | | * and must be added to one with g_source_attach() before it will be |
5074 | | * executed. |
5075 | | * |
5076 | | * The scheduling granularity/accuracy of this timeout source will be |
5077 | | * in seconds. |
5078 | | * |
5079 | | * The interval given is in terms of monotonic time, not wall clock time. |
5080 | | * See g_get_monotonic_time(). |
5081 | | * |
5082 | | * Returns: the newly-created timeout source |
5083 | | * |
5084 | | * Since: 2.14 |
5085 | | **/ |
5086 | | GSource * |
5087 | | g_timeout_source_new_seconds (guint interval) |
5088 | 0 | { |
5089 | 0 | return timeout_source_new (interval, TRUE, FALSE); |
5090 | 0 | } |
5091 | | |
5092 | | static guint |
5093 | | timeout_add_full (gint priority, |
5094 | | guint interval, |
5095 | | gboolean seconds, |
5096 | | gboolean one_shot, |
5097 | | GSourceFunc function, |
5098 | | gpointer data, |
5099 | | GDestroyNotify notify) |
5100 | 3.03k | { |
5101 | 3.03k | GSource *source; |
5102 | 3.03k | guint id; |
5103 | | |
5104 | 3.03k | g_return_val_if_fail (function != NULL, 0); |
5105 | | |
5106 | 3.03k | source = timeout_source_new (interval, seconds, one_shot); |
5107 | | |
5108 | 3.03k | if (priority != G_PRIORITY_DEFAULT) |
5109 | 0 | g_source_set_priority (source, priority); |
5110 | | |
5111 | 3.03k | g_source_set_callback (source, function, data, notify); |
5112 | 3.03k | id = g_source_attach (source, NULL); |
5113 | | |
5114 | 3.03k | TRACE (GLIB_TIMEOUT_ADD (source, g_main_context_default (), id, priority, interval, function, data)); |
5115 | | |
5116 | 3.03k | g_source_unref (source); |
5117 | | |
5118 | 3.03k | return id; |
5119 | 3.03k | } |
5120 | | |
5121 | | /** |
5122 | | * g_timeout_add_full: (rename-to g_timeout_add) |
5123 | | * @priority: the priority of the timeout source. Typically this will be in |
5124 | | * the range between %G_PRIORITY_DEFAULT and %G_PRIORITY_HIGH. |
5125 | | * @interval: the time between calls to the function, in milliseconds |
5126 | | * (1/1000ths of a second) |
5127 | | * @function: function to call |
5128 | | * @data: data to pass to @function |
5129 | | * @notify: (nullable): function to call when the timeout is removed, or %NULL |
5130 | | * |
5131 | | * Sets a function to be called at regular intervals, with the given |
5132 | | * priority. The function is called repeatedly until it returns |
5133 | | * %FALSE, at which point the timeout is automatically destroyed and |
5134 | | * the function will not be called again. The @notify function is |
5135 | | * called when the timeout is destroyed. The first call to the |
5136 | | * function will be at the end of the first @interval. |
5137 | | * |
5138 | | * Note that timeout functions may be delayed, due to the processing of other |
5139 | | * event sources. Thus they should not be relied on for precise timing. |
5140 | | * After each call to the timeout function, the time of the next |
5141 | | * timeout is recalculated based on the current time and the given interval |
5142 | | * (it does not try to 'catch up' time lost in delays). |
5143 | | * |
5144 | | * See [memory management of sources][mainloop-memory-management] for details |
5145 | | * on how to handle the return value and memory management of @data. |
5146 | | * |
5147 | | * This internally creates a main loop source using g_timeout_source_new() |
5148 | | * and attaches it to the global #GMainContext using g_source_attach(), so |
5149 | | * the callback will be invoked in whichever thread is running that main |
5150 | | * context. You can do these steps manually if you need greater control or to |
5151 | | * use a custom main context. |
5152 | | * |
5153 | | * The interval given is in terms of monotonic time, not wall clock time. |
5154 | | * See g_get_monotonic_time(). |
5155 | | * |
5156 | | * Returns: the ID (greater than 0) of the event source. |
5157 | | **/ |
5158 | | guint |
5159 | | g_timeout_add_full (gint priority, |
5160 | | guint interval, |
5161 | | GSourceFunc function, |
5162 | | gpointer data, |
5163 | | GDestroyNotify notify) |
5164 | 3.03k | { |
5165 | 3.03k | return timeout_add_full (priority, interval, FALSE, FALSE, function, data, notify); |
5166 | 3.03k | } |
5167 | | |
5168 | | /** |
5169 | | * g_timeout_add: |
5170 | | * @interval: the time between calls to the function, in milliseconds |
5171 | | * (1/1000ths of a second) |
5172 | | * @function: function to call |
5173 | | * @data: data to pass to @function |
5174 | | * |
5175 | | * Sets a function to be called at regular intervals, with the default |
5176 | | * priority, %G_PRIORITY_DEFAULT. |
5177 | | * |
5178 | | * The given @function is called repeatedly until it returns %G_SOURCE_REMOVE |
5179 | | * or %FALSE, at which point the timeout is automatically destroyed and the |
5180 | | * function will not be called again. The first call to the function will be |
5181 | | * at the end of the first @interval. |
5182 | | * |
5183 | | * Note that timeout functions may be delayed, due to the processing of other |
5184 | | * event sources. Thus they should not be relied on for precise timing. |
5185 | | * After each call to the timeout function, the time of the next |
5186 | | * timeout is recalculated based on the current time and the given interval |
5187 | | * (it does not try to 'catch up' time lost in delays). |
5188 | | * |
5189 | | * See [memory management of sources][mainloop-memory-management] for details |
5190 | | * on how to handle the return value and memory management of @data. |
5191 | | * |
5192 | | * If you want to have a timer in the "seconds" range and do not care |
5193 | | * about the exact time of the first call of the timer, use the |
5194 | | * g_timeout_add_seconds() function; this function allows for more |
5195 | | * optimizations and more efficient system power usage. |
5196 | | * |
5197 | | * This internally creates a main loop source using g_timeout_source_new() |
5198 | | * and attaches it to the global #GMainContext using g_source_attach(), so |
5199 | | * the callback will be invoked in whichever thread is running that main |
5200 | | * context. You can do these steps manually if you need greater control or to |
5201 | | * use a custom main context. |
5202 | | * |
5203 | | * It is safe to call this function from any thread. |
5204 | | * |
5205 | | * The interval given is in terms of monotonic time, not wall clock |
5206 | | * time. See g_get_monotonic_time(). |
5207 | | * |
5208 | | * Returns: the ID (greater than 0) of the event source. |
5209 | | **/ |
5210 | | guint |
5211 | | g_timeout_add (guint32 interval, |
5212 | | GSourceFunc function, |
5213 | | gpointer data) |
5214 | 3.03k | { |
5215 | 3.03k | return g_timeout_add_full (G_PRIORITY_DEFAULT, |
5216 | 3.03k | interval, function, data, NULL); |
5217 | 3.03k | } |
5218 | | |
5219 | | /** |
5220 | | * g_timeout_add_once: |
5221 | | * @interval: the time after which the function will be called, in |
5222 | | * milliseconds (1/1000ths of a second) |
5223 | | * @function: function to call |
5224 | | * @data: data to pass to @function |
5225 | | * |
5226 | | * Sets a function to be called after @interval milliseconds have elapsed, |
5227 | | * with the default priority, %G_PRIORITY_DEFAULT. |
5228 | | * |
5229 | | * The given @function is called once and then the source will be automatically |
5230 | | * removed from the main context. |
5231 | | * |
5232 | | * This function otherwise behaves like g_timeout_add(). |
5233 | | * |
5234 | | * Returns: the ID (greater than 0) of the event source |
5235 | | * |
5236 | | * Since: 2.74 |
5237 | | */ |
5238 | | guint |
5239 | | g_timeout_add_once (guint32 interval, |
5240 | | GSourceOnceFunc function, |
5241 | | gpointer data) |
5242 | 0 | { |
5243 | 0 | return timeout_add_full (G_PRIORITY_DEFAULT, interval, FALSE, TRUE, (GSourceFunc) function, data, NULL); |
5244 | 0 | } |
5245 | | |
5246 | | /** |
5247 | | * g_timeout_add_seconds_full: (rename-to g_timeout_add_seconds) |
5248 | | * @priority: the priority of the timeout source. Typically this will be in |
5249 | | * the range between %G_PRIORITY_DEFAULT and %G_PRIORITY_HIGH. |
5250 | | * @interval: the time between calls to the function, in seconds |
5251 | | * @function: function to call |
5252 | | * @data: data to pass to @function |
5253 | | * @notify: (nullable): function to call when the timeout is removed, or %NULL |
5254 | | * |
5255 | | * Sets a function to be called at regular intervals, with @priority. |
5256 | | * |
5257 | | * The function is called repeatedly until it returns %G_SOURCE_REMOVE |
5258 | | * or %FALSE, at which point the timeout is automatically destroyed and |
5259 | | * the function will not be called again. |
5260 | | * |
5261 | | * Unlike g_timeout_add(), this function operates at whole second granularity. |
5262 | | * The initial starting point of the timer is determined by the implementation |
5263 | | * and the implementation is expected to group multiple timers together so that |
5264 | | * they fire all at the same time. To allow this grouping, the @interval to the |
5265 | | * first timer is rounded and can deviate up to one second from the specified |
5266 | | * interval. Subsequent timer iterations will generally run at the specified |
5267 | | * interval. |
5268 | | * |
5269 | | * Note that timeout functions may be delayed, due to the processing of other |
5270 | | * event sources. Thus they should not be relied on for precise timing. |
5271 | | * After each call to the timeout function, the time of the next |
5272 | | * timeout is recalculated based on the current time and the given @interval |
5273 | | * |
5274 | | * See [memory management of sources][mainloop-memory-management] for details |
5275 | | * on how to handle the return value and memory management of @data. |
5276 | | * |
5277 | | * If you want timing more precise than whole seconds, use g_timeout_add() |
5278 | | * instead. |
5279 | | * |
5280 | | * The grouping of timers to fire at the same time results in a more power |
5281 | | * and CPU efficient behavior so if your timer is in multiples of seconds |
5282 | | * and you don't require the first timer exactly one second from now, the |
5283 | | * use of g_timeout_add_seconds() is preferred over g_timeout_add(). |
5284 | | * |
5285 | | * This internally creates a main loop source using |
5286 | | * g_timeout_source_new_seconds() and attaches it to the main loop context |
5287 | | * using g_source_attach(). You can do these steps manually if you need |
5288 | | * greater control. |
5289 | | * |
5290 | | * It is safe to call this function from any thread. |
5291 | | * |
5292 | | * The interval given is in terms of monotonic time, not wall clock |
5293 | | * time. See g_get_monotonic_time(). |
5294 | | * |
5295 | | * Returns: the ID (greater than 0) of the event source. |
5296 | | * |
5297 | | * Since: 2.14 |
5298 | | **/ |
5299 | | guint |
5300 | | g_timeout_add_seconds_full (gint priority, |
5301 | | guint32 interval, |
5302 | | GSourceFunc function, |
5303 | | gpointer data, |
5304 | | GDestroyNotify notify) |
5305 | 0 | { |
5306 | 0 | GSource *source; |
5307 | 0 | guint id; |
5308 | |
|
5309 | 0 | g_return_val_if_fail (function != NULL, 0); |
5310 | | |
5311 | 0 | source = g_timeout_source_new_seconds (interval); |
5312 | |
|
5313 | 0 | if (priority != G_PRIORITY_DEFAULT) |
5314 | 0 | g_source_set_priority (source, priority); |
5315 | |
|
5316 | 0 | g_source_set_callback (source, function, data, notify); |
5317 | 0 | id = g_source_attach (source, NULL); |
5318 | 0 | g_source_unref (source); |
5319 | |
|
5320 | 0 | return id; |
5321 | 0 | } |
5322 | | |
5323 | | /** |
5324 | | * g_timeout_add_seconds: |
5325 | | * @interval: the time between calls to the function, in seconds |
5326 | | * @function: function to call |
5327 | | * @data: data to pass to @function |
5328 | | * |
5329 | | * Sets a function to be called at regular intervals with the default |
5330 | | * priority, %G_PRIORITY_DEFAULT. |
5331 | | * |
5332 | | * The function is called repeatedly until it returns %G_SOURCE_REMOVE |
5333 | | * or %FALSE, at which point the timeout is automatically destroyed |
5334 | | * and the function will not be called again. |
5335 | | * |
5336 | | * This internally creates a main loop source using |
5337 | | * g_timeout_source_new_seconds() and attaches it to the main loop context |
5338 | | * using g_source_attach(). You can do these steps manually if you need |
5339 | | * greater control. Also see g_timeout_add_seconds_full(). |
5340 | | * |
5341 | | * It is safe to call this function from any thread. |
5342 | | * |
5343 | | * Note that the first call of the timer may not be precise for timeouts |
5344 | | * of one second. If you need finer precision and have such a timeout, |
5345 | | * you may want to use g_timeout_add() instead. |
5346 | | * |
5347 | | * See [memory management of sources][mainloop-memory-management] for details |
5348 | | * on how to handle the return value and memory management of @data. |
5349 | | * |
5350 | | * The interval given is in terms of monotonic time, not wall clock |
5351 | | * time. See g_get_monotonic_time(). |
5352 | | * |
5353 | | * Returns: the ID (greater than 0) of the event source. |
5354 | | * |
5355 | | * Since: 2.14 |
5356 | | **/ |
5357 | | guint |
5358 | | g_timeout_add_seconds (guint interval, |
5359 | | GSourceFunc function, |
5360 | | gpointer data) |
5361 | 0 | { |
5362 | 0 | g_return_val_if_fail (function != NULL, 0); |
5363 | | |
5364 | 0 | return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL); |
5365 | 0 | } |
5366 | | |
5367 | | /* Child watch functions */ |
5368 | | |
5369 | | #ifdef G_OS_WIN32 |
5370 | | |
5371 | | static gboolean |
5372 | | g_child_watch_prepare (GSource *source, |
5373 | | gint *timeout) |
5374 | | { |
5375 | | *timeout = -1; |
5376 | | return FALSE; |
5377 | | } |
5378 | | |
5379 | | static gboolean |
5380 | | g_child_watch_check (GSource *source) |
5381 | | { |
5382 | | GChildWatchSource *child_watch_source; |
5383 | | gboolean child_exited; |
5384 | | |
5385 | | child_watch_source = (GChildWatchSource *) source; |
5386 | | |
5387 | | child_exited = child_watch_source->poll.revents & G_IO_IN; |
5388 | | |
5389 | | if (child_exited) |
5390 | | { |
5391 | | DWORD child_status; |
5392 | | |
5393 | | /* |
5394 | | * Note: We do _not_ check for the special value of STILL_ACTIVE |
5395 | | * since we know that the process has exited and doing so runs into |
5396 | | * problems if the child process "happens to return STILL_ACTIVE(259)" |
5397 | | * as Microsoft's Platform SDK puts it. |
5398 | | */ |
5399 | | if (!GetExitCodeProcess (child_watch_source->pid, &child_status)) |
5400 | | { |
5401 | | gchar *emsg = g_win32_error_message (GetLastError ()); |
5402 | | g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg); |
5403 | | g_free (emsg); |
5404 | | |
5405 | | child_watch_source->child_status = -1; |
5406 | | } |
5407 | | else |
5408 | | child_watch_source->child_status = child_status; |
5409 | | } |
5410 | | |
5411 | | return child_exited; |
5412 | | } |
5413 | | |
5414 | | static void |
5415 | | g_child_watch_finalize (GSource *source) |
5416 | | { |
5417 | | } |
5418 | | |
5419 | | #else /* G_OS_WIN32 */ |
5420 | | |
5421 | | static void |
5422 | | wake_source (GSource *source) |
5423 | 0 | { |
5424 | 0 | GMainContext *context; |
5425 | | |
5426 | | /* This should be thread-safe: |
5427 | | * |
5428 | | * - if the source is currently being added to a context, that |
5429 | | * context will be woken up anyway |
5430 | | * |
5431 | | * - if the source is currently being destroyed, we simply need not |
5432 | | * to crash: |
5433 | | * |
5434 | | * - the memory for the source will remain valid until after the |
5435 | | * source finalize function was called (which would remove the |
5436 | | * source from the global list which we are currently holding the |
5437 | | * lock for) |
5438 | | * |
5439 | | * - the GMainContext will either be NULL or point to a live |
5440 | | * GMainContext |
5441 | | * |
5442 | | * - the GMainContext will remain valid since we hold the |
5443 | | * main_context_list lock |
5444 | | * |
5445 | | * Since we are holding a lot of locks here, don't try to enter any |
5446 | | * more GMainContext functions for fear of dealock -- just hit the |
5447 | | * GWakeup and run. Even if that's safe now, it could easily become |
5448 | | * unsafe with some very minor changes in the future, and signal |
5449 | | * handling is not the most well-tested codepath. |
5450 | | */ |
5451 | 0 | G_LOCK(main_context_list); |
5452 | 0 | context = source->context; |
5453 | 0 | if (context) |
5454 | 0 | g_wakeup_signal (context->wakeup); |
5455 | 0 | G_UNLOCK(main_context_list); |
5456 | 0 | } |
5457 | | |
5458 | | static void |
5459 | | dispatch_unix_signals_unlocked (void) |
5460 | 0 | { |
5461 | 0 | gboolean pending[NSIG]; |
5462 | 0 | GSList *node; |
5463 | 0 | gint i; |
5464 | | |
5465 | | /* clear this first in case another one arrives while we're processing */ |
5466 | 0 | g_atomic_int_set (&any_unix_signal_pending, 0); |
5467 | | |
5468 | | /* We atomically test/clear the bit from the global array in case |
5469 | | * other signals arrive while we are dispatching. |
5470 | | * |
5471 | | * We then can safely use our own array below without worrying about |
5472 | | * races. |
5473 | | */ |
5474 | 0 | for (i = 0; i < NSIG; i++) |
5475 | 0 | { |
5476 | | /* Be very careful with (the volatile) unix_signal_pending. |
5477 | | * |
5478 | | * We must ensure that it's not possible that we clear it without |
5479 | | * handling the signal. We therefore must ensure that our pending |
5480 | | * array has a field set (ie: we will do something about the |
5481 | | * signal) before we clear the item in unix_signal_pending. |
5482 | | * |
5483 | | * Note specifically: we must check _our_ array. |
5484 | | */ |
5485 | 0 | pending[i] = g_atomic_int_compare_and_exchange (&unix_signal_pending[i], 1, 0); |
5486 | 0 | } |
5487 | | |
5488 | | /* handle GChildWatchSource instances */ |
5489 | 0 | if (pending[SIGCHLD]) |
5490 | 0 | { |
5491 | | /* The only way we can do this is to scan all of the children. |
5492 | | * |
5493 | | * The docs promise that we will not reap children that we are not |
5494 | | * explicitly watching, so that ties our hands from calling |
5495 | | * waitpid(-1). We also can't use siginfo's si_pid field since if |
5496 | | * multiple SIGCHLD arrive at the same time, one of them can be |
5497 | | * dropped (since a given UNIX signal can only be pending once). |
5498 | | */ |
5499 | 0 | for (node = unix_child_watches; node; node = node->next) |
5500 | 0 | { |
5501 | 0 | GChildWatchSource *source = node->data; |
5502 | |
|
5503 | 0 | if (!source->using_pidfd && |
5504 | 0 | !g_atomic_int_get (&source->child_exited)) |
5505 | 0 | { |
5506 | 0 | pid_t pid; |
5507 | 0 | do |
5508 | 0 | { |
5509 | 0 | g_assert (source->pid > 0); |
5510 | | |
5511 | 0 | pid = waitpid (source->pid, &source->child_status, WNOHANG); |
5512 | 0 | if (pid > 0) |
5513 | 0 | { |
5514 | 0 | g_atomic_int_set (&source->child_exited, TRUE); |
5515 | 0 | wake_source ((GSource *) source); |
5516 | 0 | } |
5517 | 0 | else if (pid == -1 && errno == ECHILD) |
5518 | 0 | { |
5519 | 0 | g_warning ("GChildWatchSource: Exit status of a child process was requested but ECHILD was received by waitpid(). See the documentation of g_child_watch_source_new() for possible causes."); |
5520 | 0 | source->child_status = 0; |
5521 | 0 | g_atomic_int_set (&source->child_exited, TRUE); |
5522 | 0 | wake_source ((GSource *) source); |
5523 | 0 | } |
5524 | 0 | } |
5525 | 0 | while (pid == -1 && errno == EINTR); |
5526 | 0 | } |
5527 | 0 | } |
5528 | 0 | } |
5529 | | |
5530 | | /* handle GUnixSignalWatchSource instances */ |
5531 | 0 | for (node = unix_signal_watches; node; node = node->next) |
5532 | 0 | { |
5533 | 0 | GUnixSignalWatchSource *source = node->data; |
5534 | |
|
5535 | 0 | if (pending[source->signum] && |
5536 | 0 | g_atomic_int_compare_and_exchange (&source->pending, FALSE, TRUE)) |
5537 | 0 | { |
5538 | 0 | wake_source ((GSource *) source); |
5539 | 0 | } |
5540 | 0 | } |
5541 | |
|
5542 | 0 | } |
5543 | | |
5544 | | static void |
5545 | | dispatch_unix_signals (void) |
5546 | 0 | { |
5547 | 0 | G_LOCK(unix_signal_lock); |
5548 | 0 | dispatch_unix_signals_unlocked (); |
5549 | 0 | G_UNLOCK(unix_signal_lock); |
5550 | 0 | } |
5551 | | |
5552 | | static gboolean |
5553 | | g_child_watch_prepare (GSource *source, |
5554 | | gint *timeout) |
5555 | 0 | { |
5556 | 0 | GChildWatchSource *child_watch_source; |
5557 | |
|
5558 | 0 | child_watch_source = (GChildWatchSource *) source; |
5559 | |
|
5560 | 0 | return g_atomic_int_get (&child_watch_source->child_exited); |
5561 | 0 | } |
5562 | | |
5563 | | #ifdef HAVE_PIDFD |
5564 | | static int |
5565 | | siginfo_t_to_wait_status (const siginfo_t *info) |
5566 | 0 | { |
5567 | | /* Each of these returns is essentially the inverse of WIFEXITED(), |
5568 | | * WIFSIGNALED(), etc. */ |
5569 | 0 | switch (info->si_code) |
5570 | 0 | { |
5571 | 0 | case CLD_EXITED: |
5572 | 0 | return W_EXITCODE (info->si_status, 0); |
5573 | 0 | case CLD_KILLED: |
5574 | 0 | return W_EXITCODE (0, info->si_status); |
5575 | 0 | case CLD_DUMPED: |
5576 | 0 | return W_EXITCODE (0, info->si_status | WCOREFLAG); |
5577 | 0 | case CLD_CONTINUED: |
5578 | 0 | return __W_CONTINUED; |
5579 | 0 | case CLD_STOPPED: |
5580 | 0 | case CLD_TRAPPED: |
5581 | 0 | default: |
5582 | 0 | return W_STOPCODE (info->si_status); |
5583 | 0 | } |
5584 | 0 | } |
5585 | | #endif /* HAVE_PIDFD */ |
5586 | | |
5587 | | static gboolean |
5588 | | g_child_watch_check (GSource *source) |
5589 | 0 | { |
5590 | 0 | GChildWatchSource *child_watch_source; |
5591 | |
|
5592 | 0 | child_watch_source = (GChildWatchSource *) source; |
5593 | |
|
5594 | 0 | #ifdef HAVE_PIDFD |
5595 | 0 | if (child_watch_source->using_pidfd) |
5596 | 0 | { |
5597 | 0 | gboolean child_exited = child_watch_source->poll.revents & G_IO_IN; |
5598 | |
|
5599 | 0 | if (child_exited) |
5600 | 0 | { |
5601 | 0 | siginfo_t child_info = { 0, }; |
5602 | | |
5603 | | /* Get the exit status */ |
5604 | 0 | if (waitid (P_PIDFD, child_watch_source->poll.fd, &child_info, WEXITED | WNOHANG) >= 0 && |
5605 | 0 | child_info.si_pid != 0) |
5606 | 0 | { |
5607 | | /* waitid() helpfully provides the wait status in a decomposed |
5608 | | * form which is quite useful. Unfortunately we have to report it |
5609 | | * to the #GChildWatchFunc as a waitpid()-style platform-specific |
5610 | | * wait status, so that the user code in #GChildWatchFunc can then |
5611 | | * call WIFEXITED() (etc.) on it. That means re-composing the |
5612 | | * status information. */ |
5613 | 0 | child_watch_source->child_status = siginfo_t_to_wait_status (&child_info); |
5614 | 0 | child_watch_source->child_exited = TRUE; |
5615 | 0 | } |
5616 | 0 | } |
5617 | |
|
5618 | 0 | return child_exited; |
5619 | 0 | } |
5620 | 0 | #endif /* HAVE_PIDFD */ |
5621 | | |
5622 | 0 | return g_atomic_int_get (&child_watch_source->child_exited); |
5623 | 0 | } |
5624 | | |
5625 | | static gboolean |
5626 | | g_unix_signal_watch_prepare (GSource *source, |
5627 | | gint *timeout) |
5628 | 0 | { |
5629 | 0 | GUnixSignalWatchSource *unix_signal_source; |
5630 | |
|
5631 | 0 | unix_signal_source = (GUnixSignalWatchSource *) source; |
5632 | |
|
5633 | 0 | return g_atomic_int_get (&unix_signal_source->pending); |
5634 | 0 | } |
5635 | | |
5636 | | static gboolean |
5637 | | g_unix_signal_watch_check (GSource *source) |
5638 | 0 | { |
5639 | 0 | GUnixSignalWatchSource *unix_signal_source; |
5640 | |
|
5641 | 0 | unix_signal_source = (GUnixSignalWatchSource *) source; |
5642 | |
|
5643 | 0 | return g_atomic_int_get (&unix_signal_source->pending); |
5644 | 0 | } |
5645 | | |
5646 | | static gboolean |
5647 | | g_unix_signal_watch_dispatch (GSource *source, |
5648 | | GSourceFunc callback, |
5649 | | gpointer user_data) |
5650 | 0 | { |
5651 | 0 | GUnixSignalWatchSource *unix_signal_source; |
5652 | 0 | gboolean again; |
5653 | |
|
5654 | 0 | unix_signal_source = (GUnixSignalWatchSource *) source; |
5655 | |
|
5656 | 0 | if (!callback) |
5657 | 0 | { |
5658 | 0 | g_warning ("Unix signal source dispatched without callback. " |
5659 | 0 | "You must call g_source_set_callback()."); |
5660 | 0 | return FALSE; |
5661 | 0 | } |
5662 | | |
5663 | 0 | g_atomic_int_set (&unix_signal_source->pending, FALSE); |
5664 | |
|
5665 | 0 | again = (callback) (user_data); |
5666 | |
|
5667 | 0 | return again; |
5668 | 0 | } |
5669 | | |
5670 | | static void |
5671 | | ref_unix_signal_handler_unlocked (int signum) |
5672 | 0 | { |
5673 | | /* Ensure we have the worker context */ |
5674 | 0 | g_get_worker_context (); |
5675 | 0 | unix_signal_refcount[signum]++; |
5676 | 0 | if (unix_signal_refcount[signum] == 1) |
5677 | 0 | { |
5678 | 0 | struct sigaction action; |
5679 | 0 | action.sa_handler = g_unix_signal_handler; |
5680 | 0 | sigemptyset (&action.sa_mask); |
5681 | 0 | #ifdef SA_RESTART |
5682 | 0 | action.sa_flags = SA_RESTART | SA_NOCLDSTOP; |
5683 | | #else |
5684 | | action.sa_flags = SA_NOCLDSTOP; |
5685 | | #endif |
5686 | 0 | sigaction (signum, &action, NULL); |
5687 | 0 | } |
5688 | 0 | } |
5689 | | |
5690 | | static void |
5691 | | unref_unix_signal_handler_unlocked (int signum) |
5692 | 0 | { |
5693 | 0 | unix_signal_refcount[signum]--; |
5694 | 0 | if (unix_signal_refcount[signum] == 0) |
5695 | 0 | { |
5696 | 0 | struct sigaction action; |
5697 | 0 | memset (&action, 0, sizeof (action)); |
5698 | 0 | action.sa_handler = SIG_DFL; |
5699 | 0 | sigemptyset (&action.sa_mask); |
5700 | 0 | sigaction (signum, &action, NULL); |
5701 | 0 | } |
5702 | 0 | } |
5703 | | |
5704 | | /* Return a const string to avoid allocations. We lose precision in the case the |
5705 | | * @signum is unrecognised, but that’ll do. */ |
5706 | | static const gchar * |
5707 | | signum_to_string (int signum) |
5708 | 0 | { |
5709 | | /* See `man 0P signal.h` */ |
5710 | 0 | #define SIGNAL(s) \ |
5711 | 0 | case (s): \ |
5712 | 0 | return ("GUnixSignalSource: " #s); |
5713 | 0 | switch (signum) |
5714 | 0 | { |
5715 | | /* These signals are guaranteed to exist by POSIX. */ |
5716 | 0 | SIGNAL (SIGABRT) |
5717 | 0 | SIGNAL (SIGFPE) |
5718 | 0 | SIGNAL (SIGILL) |
5719 | 0 | SIGNAL (SIGINT) |
5720 | 0 | SIGNAL (SIGSEGV) |
5721 | 0 | SIGNAL (SIGTERM) |
5722 | | /* Frustratingly, these are not, and hence for brevity the list is |
5723 | | * incomplete. */ |
5724 | 0 | #ifdef SIGALRM |
5725 | 0 | SIGNAL (SIGALRM) |
5726 | 0 | #endif |
5727 | 0 | #ifdef SIGCHLD |
5728 | 0 | SIGNAL (SIGCHLD) |
5729 | 0 | #endif |
5730 | 0 | #ifdef SIGHUP |
5731 | 0 | SIGNAL (SIGHUP) |
5732 | 0 | #endif |
5733 | 0 | #ifdef SIGKILL |
5734 | 0 | SIGNAL (SIGKILL) |
5735 | 0 | #endif |
5736 | 0 | #ifdef SIGPIPE |
5737 | 0 | SIGNAL (SIGPIPE) |
5738 | 0 | #endif |
5739 | 0 | #ifdef SIGQUIT |
5740 | 0 | SIGNAL (SIGQUIT) |
5741 | 0 | #endif |
5742 | 0 | #ifdef SIGSTOP |
5743 | 0 | SIGNAL (SIGSTOP) |
5744 | 0 | #endif |
5745 | 0 | #ifdef SIGUSR1 |
5746 | 0 | SIGNAL (SIGUSR1) |
5747 | 0 | #endif |
5748 | 0 | #ifdef SIGUSR2 |
5749 | 0 | SIGNAL (SIGUSR2) |
5750 | 0 | #endif |
5751 | 0 | #ifdef SIGPOLL |
5752 | 0 | SIGNAL (SIGPOLL) |
5753 | 0 | #endif |
5754 | 0 | #ifdef SIGPROF |
5755 | 0 | SIGNAL (SIGPROF) |
5756 | 0 | #endif |
5757 | 0 | #ifdef SIGTRAP |
5758 | 0 | SIGNAL (SIGTRAP) |
5759 | 0 | #endif |
5760 | 0 | default: |
5761 | 0 | return "GUnixSignalSource: Unrecognized signal"; |
5762 | 0 | } |
5763 | 0 | #undef SIGNAL |
5764 | 0 | } |
5765 | | |
5766 | | GSource * |
5767 | | _g_main_create_unix_signal_watch (int signum) |
5768 | 0 | { |
5769 | 0 | GSource *source; |
5770 | 0 | GUnixSignalWatchSource *unix_signal_source; |
5771 | |
|
5772 | 0 | source = g_source_new (&g_unix_signal_funcs, sizeof (GUnixSignalWatchSource)); |
5773 | 0 | unix_signal_source = (GUnixSignalWatchSource *) source; |
5774 | |
|
5775 | 0 | unix_signal_source->signum = signum; |
5776 | 0 | unix_signal_source->pending = FALSE; |
5777 | | |
5778 | | /* Set a default name on the source, just in case the caller does not. */ |
5779 | 0 | g_source_set_static_name (source, signum_to_string (signum)); |
5780 | |
|
5781 | 0 | G_LOCK (unix_signal_lock); |
5782 | 0 | ref_unix_signal_handler_unlocked (signum); |
5783 | 0 | unix_signal_watches = g_slist_prepend (unix_signal_watches, unix_signal_source); |
5784 | 0 | dispatch_unix_signals_unlocked (); |
5785 | 0 | G_UNLOCK (unix_signal_lock); |
5786 | |
|
5787 | 0 | return source; |
5788 | 0 | } |
5789 | | |
5790 | | static void |
5791 | | g_unix_signal_watch_finalize (GSource *source) |
5792 | 0 | { |
5793 | 0 | GUnixSignalWatchSource *unix_signal_source; |
5794 | |
|
5795 | 0 | unix_signal_source = (GUnixSignalWatchSource *) source; |
5796 | |
|
5797 | 0 | G_LOCK (unix_signal_lock); |
5798 | 0 | unref_unix_signal_handler_unlocked (unix_signal_source->signum); |
5799 | 0 | unix_signal_watches = g_slist_remove (unix_signal_watches, source); |
5800 | 0 | G_UNLOCK (unix_signal_lock); |
5801 | 0 | } |
5802 | | |
5803 | | static void |
5804 | | g_child_watch_finalize (GSource *source) |
5805 | 0 | { |
5806 | 0 | GChildWatchSource *child_watch_source = (GChildWatchSource *) source; |
5807 | |
|
5808 | 0 | if (child_watch_source->using_pidfd) |
5809 | 0 | { |
5810 | 0 | if (child_watch_source->poll.fd >= 0) |
5811 | 0 | close (child_watch_source->poll.fd); |
5812 | 0 | return; |
5813 | 0 | } |
5814 | | |
5815 | 0 | G_LOCK (unix_signal_lock); |
5816 | 0 | unix_child_watches = g_slist_remove (unix_child_watches, source); |
5817 | 0 | unref_unix_signal_handler_unlocked (SIGCHLD); |
5818 | 0 | G_UNLOCK (unix_signal_lock); |
5819 | 0 | } |
5820 | | |
5821 | | #endif /* G_OS_WIN32 */ |
5822 | | |
5823 | | static gboolean |
5824 | | g_child_watch_dispatch (GSource *source, |
5825 | | GSourceFunc callback, |
5826 | | gpointer user_data) |
5827 | 0 | { |
5828 | 0 | GChildWatchSource *child_watch_source; |
5829 | 0 | GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback; |
5830 | |
|
5831 | 0 | child_watch_source = (GChildWatchSource *) source; |
5832 | |
|
5833 | 0 | if (!callback) |
5834 | 0 | { |
5835 | 0 | g_warning ("Child watch source dispatched without callback. " |
5836 | 0 | "You must call g_source_set_callback()."); |
5837 | 0 | return FALSE; |
5838 | 0 | } |
5839 | | |
5840 | 0 | (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data); |
5841 | | |
5842 | | /* We never keep a child watch source around as the child is gone */ |
5843 | 0 | return FALSE; |
5844 | 0 | } |
5845 | | |
5846 | | #ifndef G_OS_WIN32 |
5847 | | |
5848 | | static void |
5849 | | g_unix_signal_handler (int signum) |
5850 | 0 | { |
5851 | 0 | gint saved_errno = errno; |
5852 | |
|
5853 | 0 | #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) |
5854 | 0 | g_atomic_int_set (&unix_signal_pending[signum], 1); |
5855 | 0 | g_atomic_int_set (&any_unix_signal_pending, 1); |
5856 | | #else |
5857 | | #warning "Can't use atomics in g_unix_signal_handler(): Unix signal handling will be racy" |
5858 | | unix_signal_pending[signum] = 1; |
5859 | | any_unix_signal_pending = 1; |
5860 | | #endif |
5861 | |
|
5862 | 0 | g_wakeup_signal (glib_worker_context->wakeup); |
5863 | |
|
5864 | 0 | errno = saved_errno; |
5865 | 0 | } |
5866 | | |
5867 | | #endif /* !G_OS_WIN32 */ |
5868 | | |
5869 | | /** |
5870 | | * g_child_watch_source_new: |
5871 | | * @pid: process to watch. On POSIX the positive pid of a child process. On |
5872 | | * Windows a handle for a process (which doesn't have to be a child). |
5873 | | * |
5874 | | * Creates a new child_watch source. |
5875 | | * |
5876 | | * The source will not initially be associated with any #GMainContext |
5877 | | * and must be added to one with g_source_attach() before it will be |
5878 | | * executed. |
5879 | | * |
5880 | | * Note that child watch sources can only be used in conjunction with |
5881 | | * `g_spawn...` when the %G_SPAWN_DO_NOT_REAP_CHILD flag is used. |
5882 | | * |
5883 | | * Note that on platforms where #GPid must be explicitly closed |
5884 | | * (see g_spawn_close_pid()) @pid must not be closed while the |
5885 | | * source is still active. Typically, you will want to call |
5886 | | * g_spawn_close_pid() in the callback function for the source. |
5887 | | * |
5888 | | * On POSIX platforms, the following restrictions apply to this API |
5889 | | * due to limitations in POSIX process interfaces: |
5890 | | * |
5891 | | * * @pid must be a child of this process |
5892 | | * * @pid must be positive |
5893 | | * * the application must not call `waitpid` with a non-positive |
5894 | | * first argument, for instance in another thread |
5895 | | * * the application must not wait for @pid to exit by any other |
5896 | | * mechanism, including `waitpid(pid, ...)` or a second child-watch |
5897 | | * source for the same @pid |
5898 | | * * the application must not ignore `SIGCHLD` |
5899 | | * |
5900 | | * If any of those conditions are not met, this and related APIs will |
5901 | | * not work correctly. This can often be diagnosed via a GLib warning |
5902 | | * stating that `ECHILD` was received by `waitpid`. |
5903 | | * |
5904 | | * Calling `waitpid` for specific processes other than @pid remains a |
5905 | | * valid thing to do. |
5906 | | * |
5907 | | * Returns: the newly-created child watch source |
5908 | | * |
5909 | | * Since: 2.4 |
5910 | | **/ |
5911 | | GSource * |
5912 | | g_child_watch_source_new (GPid pid) |
5913 | 0 | { |
5914 | 0 | GSource *source; |
5915 | 0 | GChildWatchSource *child_watch_source; |
5916 | 0 | #ifdef HAVE_PIDFD |
5917 | 0 | int errsv; |
5918 | 0 | #endif |
5919 | |
|
5920 | 0 | #ifndef G_OS_WIN32 |
5921 | 0 | g_return_val_if_fail (pid > 0, NULL); |
5922 | 0 | #endif |
5923 | | |
5924 | 0 | source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource)); |
5925 | 0 | child_watch_source = (GChildWatchSource *)source; |
5926 | | |
5927 | | /* Set a default name on the source, just in case the caller does not. */ |
5928 | 0 | g_source_set_static_name (source, "GChildWatchSource"); |
5929 | |
|
5930 | 0 | child_watch_source->pid = pid; |
5931 | |
|
5932 | | #ifdef G_OS_WIN32 |
5933 | | child_watch_source->poll.fd = (gintptr) pid; |
5934 | | child_watch_source->poll.events = G_IO_IN; |
5935 | | |
5936 | | g_source_add_poll (source, &child_watch_source->poll); |
5937 | | #else /* !G_OS_WIN32 */ |
5938 | |
|
5939 | 0 | #ifdef HAVE_PIDFD |
5940 | | /* Use a pidfd, if possible, to avoid having to install a global SIGCHLD |
5941 | | * handler and potentially competing with any other library/code which wants |
5942 | | * to install one. |
5943 | | * |
5944 | | * Unfortunately this use of pidfd isn’t race-free (the PID could be recycled |
5945 | | * between the caller calling g_child_watch_source_new() and here), but it’s |
5946 | | * better than SIGCHLD. |
5947 | | */ |
5948 | 0 | child_watch_source->poll.fd = (int) syscall (SYS_pidfd_open, pid, 0); |
5949 | 0 | errsv = errno; |
5950 | |
|
5951 | 0 | if (child_watch_source->poll.fd >= 0) |
5952 | 0 | { |
5953 | 0 | child_watch_source->using_pidfd = TRUE; |
5954 | 0 | child_watch_source->poll.events = G_IO_IN; |
5955 | 0 | g_source_add_poll (source, &child_watch_source->poll); |
5956 | |
|
5957 | 0 | return source; |
5958 | 0 | } |
5959 | 0 | else |
5960 | 0 | { |
5961 | 0 | g_debug ("pidfd_open(%" G_PID_FORMAT ") failed with error: %s", |
5962 | 0 | pid, g_strerror (errsv)); |
5963 | | /* Fall through; likely the kernel isn’t new enough to support pidfd_open() */ |
5964 | 0 | } |
5965 | 0 | #endif /* HAVE_PIDFD */ |
5966 | | |
5967 | 0 | G_LOCK (unix_signal_lock); |
5968 | 0 | ref_unix_signal_handler_unlocked (SIGCHLD); |
5969 | 0 | unix_child_watches = g_slist_prepend (unix_child_watches, child_watch_source); |
5970 | 0 | if (waitpid (pid, &child_watch_source->child_status, WNOHANG) > 0) |
5971 | 0 | child_watch_source->child_exited = TRUE; |
5972 | 0 | G_UNLOCK (unix_signal_lock); |
5973 | 0 | #endif /* !G_OS_WIN32 */ |
5974 | |
|
5975 | 0 | return source; |
5976 | 0 | } |
5977 | | |
5978 | | /** |
5979 | | * g_child_watch_add_full: (rename-to g_child_watch_add) |
5980 | | * @priority: the priority of the idle source. Typically this will be in the |
5981 | | * range between %G_PRIORITY_DEFAULT_IDLE and %G_PRIORITY_HIGH_IDLE. |
5982 | | * @pid: process to watch. On POSIX the positive pid of a child process. On |
5983 | | * Windows a handle for a process (which doesn't have to be a child). |
5984 | | * @function: function to call |
5985 | | * @data: data to pass to @function |
5986 | | * @notify: (nullable): function to call when the idle is removed, or %NULL |
5987 | | * |
5988 | | * Sets a function to be called when the child indicated by @pid |
5989 | | * exits, at the priority @priority. |
5990 | | * |
5991 | | * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes() |
5992 | | * you will need to pass %G_SPAWN_DO_NOT_REAP_CHILD as flag to |
5993 | | * the spawn function for the child watching to work. |
5994 | | * |
5995 | | * In many programs, you will want to call g_spawn_check_wait_status() |
5996 | | * in the callback to determine whether or not the child exited |
5997 | | * successfully. |
5998 | | * |
5999 | | * Also, note that on platforms where #GPid must be explicitly closed |
6000 | | * (see g_spawn_close_pid()) @pid must not be closed while the source |
6001 | | * is still active. Typically, you should invoke g_spawn_close_pid() |
6002 | | * in the callback function for the source. |
6003 | | * |
6004 | | * GLib supports only a single callback per process id. |
6005 | | * On POSIX platforms, the same restrictions mentioned for |
6006 | | * g_child_watch_source_new() apply to this function. |
6007 | | * |
6008 | | * This internally creates a main loop source using |
6009 | | * g_child_watch_source_new() and attaches it to the main loop context |
6010 | | * using g_source_attach(). You can do these steps manually if you |
6011 | | * need greater control. |
6012 | | * |
6013 | | * Returns: the ID (greater than 0) of the event source. |
6014 | | * |
6015 | | * Since: 2.4 |
6016 | | **/ |
6017 | | guint |
6018 | | g_child_watch_add_full (gint priority, |
6019 | | GPid pid, |
6020 | | GChildWatchFunc function, |
6021 | | gpointer data, |
6022 | | GDestroyNotify notify) |
6023 | 0 | { |
6024 | 0 | GSource *source; |
6025 | 0 | guint id; |
6026 | | |
6027 | 0 | g_return_val_if_fail (function != NULL, 0); |
6028 | 0 | #ifndef G_OS_WIN32 |
6029 | 0 | g_return_val_if_fail (pid > 0, 0); |
6030 | 0 | #endif |
6031 | | |
6032 | 0 | source = g_child_watch_source_new (pid); |
6033 | |
|
6034 | 0 | if (priority != G_PRIORITY_DEFAULT) |
6035 | 0 | g_source_set_priority (source, priority); |
6036 | |
|
6037 | 0 | g_source_set_callback (source, (GSourceFunc) function, data, notify); |
6038 | 0 | id = g_source_attach (source, NULL); |
6039 | 0 | g_source_unref (source); |
6040 | |
|
6041 | 0 | return id; |
6042 | 0 | } |
6043 | | |
6044 | | /** |
6045 | | * g_child_watch_add: |
6046 | | * @pid: process id to watch. On POSIX the positive pid of a child |
6047 | | * process. On Windows a handle for a process (which doesn't have |
6048 | | * to be a child). |
6049 | | * @function: function to call |
6050 | | * @data: data to pass to @function |
6051 | | * |
6052 | | * Sets a function to be called when the child indicated by @pid |
6053 | | * exits, at a default priority, %G_PRIORITY_DEFAULT. |
6054 | | * |
6055 | | * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes() |
6056 | | * you will need to pass %G_SPAWN_DO_NOT_REAP_CHILD as flag to |
6057 | | * the spawn function for the child watching to work. |
6058 | | * |
6059 | | * Note that on platforms where #GPid must be explicitly closed |
6060 | | * (see g_spawn_close_pid()) @pid must not be closed while the |
6061 | | * source is still active. Typically, you will want to call |
6062 | | * g_spawn_close_pid() in the callback function for the source. |
6063 | | * |
6064 | | * GLib supports only a single callback per process id. |
6065 | | * On POSIX platforms, the same restrictions mentioned for |
6066 | | * g_child_watch_source_new() apply to this function. |
6067 | | * |
6068 | | * This internally creates a main loop source using |
6069 | | * g_child_watch_source_new() and attaches it to the main loop context |
6070 | | * using g_source_attach(). You can do these steps manually if you |
6071 | | * need greater control. |
6072 | | * |
6073 | | * Returns: the ID (greater than 0) of the event source. |
6074 | | * |
6075 | | * Since: 2.4 |
6076 | | **/ |
6077 | | guint |
6078 | | g_child_watch_add (GPid pid, |
6079 | | GChildWatchFunc function, |
6080 | | gpointer data) |
6081 | 0 | { |
6082 | 0 | return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL); |
6083 | 0 | } |
6084 | | |
6085 | | |
6086 | | /* Idle functions */ |
6087 | | |
6088 | | static gboolean |
6089 | | g_idle_prepare (GSource *source, |
6090 | | gint *timeout) |
6091 | 0 | { |
6092 | 0 | *timeout = 0; |
6093 | |
|
6094 | 0 | return TRUE; |
6095 | 0 | } |
6096 | | |
6097 | | static gboolean |
6098 | | g_idle_check (GSource *source) |
6099 | 0 | { |
6100 | 0 | return TRUE; |
6101 | 0 | } |
6102 | | |
6103 | | static gboolean |
6104 | | g_idle_dispatch (GSource *source, |
6105 | | GSourceFunc callback, |
6106 | | gpointer user_data) |
6107 | 0 | { |
6108 | 0 | GIdleSource *idle_source = (GIdleSource *)source; |
6109 | 0 | gboolean again; |
6110 | |
|
6111 | 0 | if (!callback) |
6112 | 0 | { |
6113 | 0 | g_warning ("Idle source dispatched without callback. " |
6114 | 0 | "You must call g_source_set_callback()."); |
6115 | 0 | return FALSE; |
6116 | 0 | } |
6117 | | |
6118 | 0 | if (idle_source->one_shot) |
6119 | 0 | { |
6120 | 0 | GSourceOnceFunc once_callback = (GSourceOnceFunc) callback; |
6121 | 0 | once_callback (user_data); |
6122 | 0 | again = G_SOURCE_REMOVE; |
6123 | 0 | } |
6124 | 0 | else |
6125 | 0 | { |
6126 | 0 | again = callback (user_data); |
6127 | 0 | } |
6128 | |
|
6129 | 0 | TRACE (GLIB_IDLE_DISPATCH (source, source->context, callback, user_data, again)); |
6130 | |
|
6131 | 0 | return again; |
6132 | 0 | } |
6133 | | |
6134 | | static GSource * |
6135 | | idle_source_new (gboolean one_shot) |
6136 | 0 | { |
6137 | 0 | GSource *source; |
6138 | 0 | GIdleSource *idle_source; |
6139 | |
|
6140 | 0 | source = g_source_new (&g_idle_funcs, sizeof (GIdleSource)); |
6141 | 0 | idle_source = (GIdleSource *) source; |
6142 | |
|
6143 | 0 | idle_source->one_shot = one_shot; |
6144 | |
|
6145 | 0 | g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE); |
6146 | | |
6147 | | /* Set a default name on the source, just in case the caller does not. */ |
6148 | 0 | g_source_set_static_name (source, "GIdleSource"); |
6149 | |
|
6150 | 0 | return source; |
6151 | 0 | } |
6152 | | |
6153 | | /** |
6154 | | * g_idle_source_new: |
6155 | | * |
6156 | | * Creates a new idle source. |
6157 | | * |
6158 | | * The source will not initially be associated with any #GMainContext |
6159 | | * and must be added to one with g_source_attach() before it will be |
6160 | | * executed. Note that the default priority for idle sources is |
6161 | | * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which |
6162 | | * have a default priority of %G_PRIORITY_DEFAULT. |
6163 | | * |
6164 | | * Returns: the newly-created idle source |
6165 | | **/ |
6166 | | GSource * |
6167 | | g_idle_source_new (void) |
6168 | 0 | { |
6169 | 0 | return idle_source_new (FALSE); |
6170 | 0 | } |
6171 | | |
6172 | | static guint |
6173 | | idle_add_full (gint priority, |
6174 | | gboolean one_shot, |
6175 | | GSourceFunc function, |
6176 | | gpointer data, |
6177 | | GDestroyNotify notify) |
6178 | 0 | { |
6179 | 0 | GSource *source; |
6180 | 0 | guint id; |
6181 | |
|
6182 | 0 | g_return_val_if_fail (function != NULL, 0); |
6183 | | |
6184 | 0 | source = idle_source_new (one_shot); |
6185 | |
|
6186 | 0 | if (priority != G_PRIORITY_DEFAULT_IDLE) |
6187 | 0 | g_source_set_priority (source, priority); |
6188 | |
|
6189 | 0 | g_source_set_callback (source, function, data, notify); |
6190 | 0 | id = g_source_attach (source, NULL); |
6191 | |
|
6192 | 0 | TRACE (GLIB_IDLE_ADD (source, g_main_context_default (), id, priority, function, data)); |
6193 | |
|
6194 | 0 | g_source_unref (source); |
6195 | |
|
6196 | 0 | return id; |
6197 | 0 | } |
6198 | | |
6199 | | /** |
6200 | | * g_idle_add_full: (rename-to g_idle_add) |
6201 | | * @priority: the priority of the idle source. Typically this will be in the |
6202 | | * range between %G_PRIORITY_DEFAULT_IDLE and %G_PRIORITY_HIGH_IDLE. |
6203 | | * @function: function to call |
6204 | | * @data: data to pass to @function |
6205 | | * @notify: (nullable): function to call when the idle is removed, or %NULL |
6206 | | * |
6207 | | * Adds a function to be called whenever there are no higher priority |
6208 | | * events pending. |
6209 | | * |
6210 | | * If the function returns %G_SOURCE_REMOVE or %FALSE it is automatically |
6211 | | * removed from the list of event sources and will not be called again. |
6212 | | * |
6213 | | * See [memory management of sources][mainloop-memory-management] for details |
6214 | | * on how to handle the return value and memory management of @data. |
6215 | | * |
6216 | | * This internally creates a main loop source using g_idle_source_new() |
6217 | | * and attaches it to the global #GMainContext using g_source_attach(), so |
6218 | | * the callback will be invoked in whichever thread is running that main |
6219 | | * context. You can do these steps manually if you need greater control or to |
6220 | | * use a custom main context. |
6221 | | * |
6222 | | * Returns: the ID (greater than 0) of the event source. |
6223 | | **/ |
6224 | | guint |
6225 | | g_idle_add_full (gint priority, |
6226 | | GSourceFunc function, |
6227 | | gpointer data, |
6228 | | GDestroyNotify notify) |
6229 | 0 | { |
6230 | 0 | return idle_add_full (priority, FALSE, function, data, notify); |
6231 | 0 | } |
6232 | | |
6233 | | /** |
6234 | | * g_idle_add: |
6235 | | * @function: function to call |
6236 | | * @data: data to pass to @function. |
6237 | | * |
6238 | | * Adds a function to be called whenever there are no higher priority |
6239 | | * events pending to the default main loop. The function is given the |
6240 | | * default idle priority, %G_PRIORITY_DEFAULT_IDLE. If the function |
6241 | | * returns %FALSE it is automatically removed from the list of event |
6242 | | * sources and will not be called again. |
6243 | | * |
6244 | | * See [memory management of sources][mainloop-memory-management] for details |
6245 | | * on how to handle the return value and memory management of @data. |
6246 | | * |
6247 | | * This internally creates a main loop source using g_idle_source_new() |
6248 | | * and attaches it to the global #GMainContext using g_source_attach(), so |
6249 | | * the callback will be invoked in whichever thread is running that main |
6250 | | * context. You can do these steps manually if you need greater control or to |
6251 | | * use a custom main context. |
6252 | | * |
6253 | | * Returns: the ID (greater than 0) of the event source. |
6254 | | **/ |
6255 | | guint |
6256 | | g_idle_add (GSourceFunc function, |
6257 | | gpointer data) |
6258 | 0 | { |
6259 | 0 | return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL); |
6260 | 0 | } |
6261 | | |
6262 | | /** |
6263 | | * g_idle_add_once: |
6264 | | * @function: function to call |
6265 | | * @data: data to pass to @function |
6266 | | * |
6267 | | * Adds a function to be called whenever there are no higher priority |
6268 | | * events pending to the default main loop. The function is given the |
6269 | | * default idle priority, %G_PRIORITY_DEFAULT_IDLE. |
6270 | | * |
6271 | | * The function will only be called once and then the source will be |
6272 | | * automatically removed from the main context. |
6273 | | * |
6274 | | * This function otherwise behaves like g_idle_add(). |
6275 | | * |
6276 | | * Returns: the ID (greater than 0) of the event source |
6277 | | * |
6278 | | * Since: 2.74 |
6279 | | */ |
6280 | | guint |
6281 | | g_idle_add_once (GSourceOnceFunc function, |
6282 | | gpointer data) |
6283 | 0 | { |
6284 | 0 | return idle_add_full (G_PRIORITY_DEFAULT_IDLE, TRUE, (GSourceFunc) function, data, NULL); |
6285 | 0 | } |
6286 | | |
6287 | | /** |
6288 | | * g_idle_remove_by_data: |
6289 | | * @data: the data for the idle source's callback. |
6290 | | * |
6291 | | * Removes the idle function with the given data. |
6292 | | * |
6293 | | * Returns: %TRUE if an idle source was found and removed. |
6294 | | **/ |
6295 | | gboolean |
6296 | | g_idle_remove_by_data (gpointer data) |
6297 | 0 | { |
6298 | 0 | return g_source_remove_by_funcs_user_data (&g_idle_funcs, data); |
6299 | 0 | } |
6300 | | |
6301 | | /** |
6302 | | * g_main_context_invoke: |
6303 | | * @context: (nullable): a #GMainContext, or %NULL |
6304 | | * @function: function to call |
6305 | | * @data: data to pass to @function |
6306 | | * |
6307 | | * Invokes a function in such a way that @context is owned during the |
6308 | | * invocation of @function. |
6309 | | * |
6310 | | * If @context is %NULL then the global default main context — as |
6311 | | * returned by g_main_context_default() — is used. |
6312 | | * |
6313 | | * If @context is owned by the current thread, @function is called |
6314 | | * directly. Otherwise, if @context is the thread-default main context |
6315 | | * of the current thread and g_main_context_acquire() succeeds, then |
6316 | | * @function is called and g_main_context_release() is called |
6317 | | * afterwards. |
6318 | | * |
6319 | | * In any other case, an idle source is created to call @function and |
6320 | | * that source is attached to @context (presumably to be run in another |
6321 | | * thread). The idle source is attached with %G_PRIORITY_DEFAULT |
6322 | | * priority. If you want a different priority, use |
6323 | | * g_main_context_invoke_full(). |
6324 | | * |
6325 | | * Note that, as with normal idle functions, @function should probably |
6326 | | * return %FALSE. If it returns %TRUE, it will be continuously run in a |
6327 | | * loop (and may prevent this call from returning). |
6328 | | * |
6329 | | * Since: 2.28 |
6330 | | **/ |
6331 | | void |
6332 | | g_main_context_invoke (GMainContext *context, |
6333 | | GSourceFunc function, |
6334 | | gpointer data) |
6335 | 0 | { |
6336 | 0 | g_main_context_invoke_full (context, |
6337 | 0 | G_PRIORITY_DEFAULT, |
6338 | 0 | function, data, NULL); |
6339 | 0 | } |
6340 | | |
6341 | | /** |
6342 | | * g_main_context_invoke_full: |
6343 | | * @context: (nullable): a #GMainContext, or %NULL |
6344 | | * @priority: the priority at which to run @function |
6345 | | * @function: function to call |
6346 | | * @data: data to pass to @function |
6347 | | * @notify: (nullable): a function to call when @data is no longer in use, or %NULL. |
6348 | | * |
6349 | | * Invokes a function in such a way that @context is owned during the |
6350 | | * invocation of @function. |
6351 | | * |
6352 | | * This function is the same as g_main_context_invoke() except that it |
6353 | | * lets you specify the priority in case @function ends up being |
6354 | | * scheduled as an idle and also lets you give a #GDestroyNotify for @data. |
6355 | | * |
6356 | | * @notify should not assume that it is called from any particular |
6357 | | * thread or with any particular context acquired. |
6358 | | * |
6359 | | * Since: 2.28 |
6360 | | **/ |
6361 | | void |
6362 | | g_main_context_invoke_full (GMainContext *context, |
6363 | | gint priority, |
6364 | | GSourceFunc function, |
6365 | | gpointer data, |
6366 | | GDestroyNotify notify) |
6367 | 0 | { |
6368 | 0 | g_return_if_fail (function != NULL); |
6369 | | |
6370 | 0 | if (!context) |
6371 | 0 | context = g_main_context_default (); |
6372 | |
|
6373 | 0 | if (g_main_context_is_owner (context)) |
6374 | 0 | { |
6375 | 0 | while (function (data)); |
6376 | 0 | if (notify != NULL) |
6377 | 0 | notify (data); |
6378 | 0 | } |
6379 | | |
6380 | 0 | else |
6381 | 0 | { |
6382 | 0 | GMainContext *thread_default; |
6383 | |
|
6384 | 0 | thread_default = g_main_context_get_thread_default (); |
6385 | |
|
6386 | 0 | if (!thread_default) |
6387 | 0 | thread_default = g_main_context_default (); |
6388 | |
|
6389 | 0 | if (thread_default == context && g_main_context_acquire (context)) |
6390 | 0 | { |
6391 | 0 | while (function (data)); |
6392 | |
|
6393 | 0 | g_main_context_release (context); |
6394 | |
|
6395 | 0 | if (notify != NULL) |
6396 | 0 | notify (data); |
6397 | 0 | } |
6398 | 0 | else |
6399 | 0 | { |
6400 | 0 | GSource *source; |
6401 | |
|
6402 | 0 | source = g_idle_source_new (); |
6403 | 0 | g_source_set_priority (source, priority); |
6404 | 0 | g_source_set_callback (source, function, data, notify); |
6405 | 0 | g_source_attach (source, context); |
6406 | 0 | g_source_unref (source); |
6407 | 0 | } |
6408 | 0 | } |
6409 | 0 | } |
6410 | | |
6411 | | static gpointer |
6412 | | glib_worker_main (gpointer data) |
6413 | 0 | { |
6414 | 0 | while (TRUE) |
6415 | 0 | { |
6416 | 0 | g_main_context_iteration (glib_worker_context, TRUE); |
6417 | |
|
6418 | 0 | #ifdef G_OS_UNIX |
6419 | 0 | if (g_atomic_int_get (&any_unix_signal_pending)) |
6420 | 0 | dispatch_unix_signals (); |
6421 | 0 | #endif |
6422 | 0 | } |
6423 | |
|
6424 | 0 | return NULL; /* worst GCC warning message ever... */ |
6425 | 0 | } |
6426 | | |
6427 | | GMainContext * |
6428 | | g_get_worker_context (void) |
6429 | 0 | { |
6430 | 0 | static gsize initialised; |
6431 | |
|
6432 | 0 | if (g_once_init_enter (&initialised)) |
6433 | 0 | { |
6434 | | /* mask all signals in the worker thread */ |
6435 | 0 | #ifdef G_OS_UNIX |
6436 | 0 | sigset_t prev_mask; |
6437 | 0 | sigset_t all; |
6438 | |
|
6439 | 0 | sigfillset (&all); |
6440 | 0 | pthread_sigmask (SIG_SETMASK, &all, &prev_mask); |
6441 | 0 | #endif |
6442 | 0 | glib_worker_context = g_main_context_new (); |
6443 | 0 | g_thread_new ("gmain", glib_worker_main, NULL); |
6444 | 0 | #ifdef G_OS_UNIX |
6445 | 0 | pthread_sigmask (SIG_SETMASK, &prev_mask, NULL); |
6446 | 0 | #endif |
6447 | 0 | g_once_init_leave (&initialised, TRUE); |
6448 | 0 | } |
6449 | |
|
6450 | 0 | return glib_worker_context; |
6451 | 0 | } |
6452 | | |
6453 | | /** |
6454 | | * g_steal_fd: |
6455 | | * @fd_ptr: (not optional) (inout): A pointer to a file descriptor |
6456 | | * |
6457 | | * Sets @fd_ptr to `-1`, returning the value that was there before. |
6458 | | * |
6459 | | * Conceptually, this transfers the ownership of the file descriptor |
6460 | | * from the referenced variable to the caller of the function (i.e. |
6461 | | * ‘steals’ the reference). This is very similar to g_steal_pointer(), |
6462 | | * but for file descriptors. |
6463 | | * |
6464 | | * On POSIX platforms, this function is async-signal safe |
6465 | | * (see [`signal(7)`](man:signal(7)) and |
6466 | | * [`signal-safety(7)`](man:signal-safety(7))), making it safe to call from a |
6467 | | * signal handler or a #GSpawnChildSetupFunc. |
6468 | | * |
6469 | | * Returns: the value that @fd_ptr previously had |
6470 | | * Since: 2.70 |
6471 | | */ |