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