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