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