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, write to the 19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 * Boston, MA 02111-1307, USA. 21 */ 22 23 /* 24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS 25 * file for a list of people on the GLib Team. See the ChangeLog 26 * files for a list of changes. These files are distributed with 27 * GLib at ftp://ftp.gtk.org/pub/gtk/. 28 */ 29 30 /* 31 * MT safe 32 */ 33 34 #ifndef _WIN32 35 /* for pipe2; need to define it first to avoid 36 * other headers pulling in unistd.h 37 */ 38 /* The meaning of_GNU_SOURCE that is intended here is present only on 39 * Linux; avoid the possibility that some misguided header in MinGW 40 * looks at it. Ideally we should define _GNU_SOURCE only on platforms 41 * where we know what it means and that is what we want here 42 * (i.e. Linux with glibc). After all, there might be some other POSIX 43 * platform even where _GNU_SOURCE is used for some unrelated change 44 * in semantics that isn't wanted. Sigh. 45 */ 46 #define _GNU_SOURCE 47 #endif 48 49 #include "config.h" 50 #include "glibconfig.h" 51 52 /* Uncomment the next line (and the corresponding line in gpoll.c) to 53 * enable debugging printouts if the environment variable 54 * G_MAIN_POLL_DEBUG is set to some value. 55 */ 56 /* #define G_MAIN_POLL_DEBUG */ 57 58 #ifdef _WIN32 59 /* Always enable debugging printout on Windows, as it is more often 60 * needed there... 61 */ 62 #define G_MAIN_POLL_DEBUG 63 #endif 64 65 #include <signal.h> 66 #include <sys/types.h> 67 #include <time.h> 68 #include <stdlib.h> 69 #ifdef HAVE_SYS_TIME_H 70 #include <sys/time.h> 71 #endif /* HAVE_SYS_TIME_H */ 72 #ifdef HAVE_UNISTD_H 73 #include <unistd.h> 74 #endif /* HAVE_UNISTD_H */ 75 #include <errno.h> 76 77 #ifdef G_OS_WIN32 78 #define STRICT 79 #include <windows.h> 80 #endif /* G_OS_WIN32 */ 81 82 #ifdef G_OS_BEOS 83 #include <sys/socket.h> 84 #include <sys/wait.h> 85 #endif /* G_OS_BEOS */ 86 87 #ifdef G_OS_UNIX 88 #include <fcntl.h> 89 #include <sys/wait.h> 90 #endif 91 92 #include "gmain.h" 93 94 #include "garray.h" 95 #include "giochannel.h" 96 #include "ghash.h" 97 #include "ghook.h" 98 #include "gqueue.h" 99 #include "gstrfuncs.h" 100 #include "gtestutils.h" 101 #include "gthreadprivate.h" 102 103 #ifdef G_OS_WIN32 104 #include "gwin32.h" 105 #endif 106 107 #ifdef G_MAIN_POLL_DEBUG 108 #include "gtimer.h" 109 #endif 110 111 /** 112 * SECTION:main 113 * @title: The Main Event Loop 114 * @short_description: manages all available sources of events 115 * 116 * The main event loop manages all the available sources of events for 117 * GLib and GTK+ applications. These events can come from any number of 118 * different types of sources such as file descriptors (plain files, 119 * pipes or sockets) and timeouts. New types of event sources can also 120 * be added using g_source_attach(). 121 * 122 * To allow multiple independent sets of sources to be handled in 123 * different threads, each source is associated with a #GMainContext. 124 * A GMainContext can only be running in a single thread, but 125 * sources can be added to it and removed from it from other threads. 126 * 127 * Each event source is assigned a priority. The default priority, 128 * #G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. 129 * Values greater than 0 denote lower priorities. Events from high priority 130 * sources are always processed before events from lower priority sources. 131 * 132 * Idle functions can also be added, and assigned a priority. These will 133 * be run whenever no events with a higher priority are ready to be processed. 134 * 135 * The #GMainLoop data type represents a main event loop. A GMainLoop is 136 * created with g_main_loop_new(). After adding the initial event sources, 137 * g_main_loop_run() is called. This continuously checks for new events from 138 * each of the event sources and dispatches them. Finally, the processing of 139 * an event from one of the sources leads to a call to g_main_loop_quit() to 140 * exit the main loop, and g_main_loop_run() returns. 141 * 142 * It is possible to create new instances of #GMainLoop recursively. 143 * This is often used in GTK+ applications when showing modal dialog 144 * boxes. Note that event sources are associated with a particular 145 * #GMainContext, and will be checked and dispatched for all main 146 * loops associated with that GMainContext. 147 * 148 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(), 149 * gtk_main_quit() and gtk_events_pending(). 150 * 151 * <refsect2><title>Creating new source types</title> 152 * <para>One of the unusual features of the #GMainLoop functionality 153 * is that new types of event source can be created and used in 154 * addition to the builtin type of event source. A new event source 155 * type is used for handling GDK events. A new source type is created 156 * by <firstterm>deriving</firstterm> from the #GSource structure. 157 * The derived type of source is represented by a structure that has 158 * the #GSource structure as a first element, and other elements specific 159 * to the new source type. To create an instance of the new source type, 160 * call g_source_new() passing in the size of the derived structure and 161 * a table of functions. These #GSourceFuncs determine the behavior of 162 * the new source type.</para> 163 * <para>New source types basically interact with the main context 164 * in two ways. Their prepare function in #GSourceFuncs can set a timeout 165 * to determine the maximum amount of time that the main loop will sleep 166 * before checking the source again. In addition, or as well, the source 167 * can add file descriptors to the set that the main context checks using 168 * g_source_add_poll().</para> 169 * </refsect2> 170 * <refsect2><title>Customizing the main loop iteration</title> 171 * <para>Single iterations of a #GMainContext can be run with 172 * g_main_context_iteration(). In some cases, more detailed control 173 * of exactly how the details of the main loop work is desired, for 174 * instance, when integrating the #GMainLoop with an external main loop. 175 * In such cases, you can call the component functions of 176 * g_main_context_iteration() directly. These functions are 177 * g_main_context_prepare(), g_main_context_query(), 178 * g_main_context_check() and g_main_context_dispatch().</para> 179 * <para>The operation of these functions can best be seen in terms 180 * of a state diagram, as shown in <xref linkend="mainloop-states"/>.</para> 181 * <figure id="mainloop-states"><title>States of a Main Context</title> 182 * <graphic fileref="mainloop-states.gif" format="GIF"></graphic> 183 * </figure> 184 * </refsect2> 185 */ 186 187 /* Types */ 188 189 typedef struct _GTimeoutSource GTimeoutSource; 190 typedef struct _GChildWatchSource GChildWatchSource; 191 typedef struct _GPollRec GPollRec; 192 typedef struct _GSourceCallback GSourceCallback; 193 194 typedef enum 195 { 196 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT, 197 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1) 198 } GSourceFlags; 199 200 #ifdef G_THREADS_ENABLED 201 typedef struct _GMainWaiter GMainWaiter; 202 203 struct _GMainWaiter 204 { 205 GCond *cond; 206 GMutex *mutex; 207 }; 208 #endif 209 210 typedef struct _GMainDispatch GMainDispatch; 211 212 struct _GMainDispatch 213 { 214 gint depth; 215 GSList *dispatching_sources; /* stack of current sources */ 216 }; 217 218 #ifdef G_MAIN_POLL_DEBUG 219 gboolean _g_main_poll_debug = FALSE; 220 #endif 221 222 struct _GMainContext 223 { 224 #ifdef G_THREADS_ENABLED 225 /* The following lock is used for both the list of sources 226 * and the list of poll records 227 */ 228 GStaticMutex mutex; 229 GCond *cond; 230 GThread *owner; 231 guint owner_count; 232 GSList *waiters; 233 #endif 234 235 gint ref_count; 236 237 GPtrArray *pending_dispatches; 238 gint timeout; /* Timeout for current iteration */ 239 240 guint next_id; 241 GSource *source_list; 242 gint in_check_or_prepare; 243 244 GPollRec *poll_records, *poll_records_tail; 245 guint n_poll_records; 246 GPollFD *cached_poll_array; 247 guint cached_poll_array_size; 248 249 #ifdef G_THREADS_ENABLED 250 #ifndef G_OS_WIN32 251 /* this pipe is used to wake up the main loop when a source is added. 252 */ 253 gint wake_up_pipe[2]; 254 #else /* G_OS_WIN32 */ 255 HANDLE wake_up_semaphore; 256 #endif /* G_OS_WIN32 */ 257 258 GPollFD wake_up_rec; 259 gboolean poll_waiting; 260 261 /* Flag indicating whether the set of fd's changed during a poll */ 262 gboolean poll_changed; 263 #endif /* G_THREADS_ENABLED */ 264 265 GPollFunc poll_func; 266 267 gint64 time; 268 gboolean time_is_fresh; 269 gint64 real_time; 270 gboolean real_time_is_fresh; 271 }; 272 273 struct _GSourceCallback 274 { 275 guint ref_count; 276 GSourceFunc func; 277 gpointer data; 278 GDestroyNotify notify; 279 }; 280 281 struct _GMainLoop 282 { 283 GMainContext *context; 284 gboolean is_running; 285 gint ref_count; 286 }; 287 288 struct _GTimeoutSource 289 { 290 GSource source; 291 gint64 expiration; 292 guint interval; 293 gboolean seconds; 294 }; 295 296 struct _GChildWatchSource 297 { 298 GSource source; 299 GPid pid; 300 gint child_status; 301 #ifdef G_OS_WIN32 302 GPollFD poll; 303 #else /* G_OS_WIN32 */ 304 gint count; 305 gboolean child_exited; 306 #endif /* G_OS_WIN32 */ 307 }; 308 309 struct _GPollRec 310 { 311 GPollFD *fd; 312 GPollRec *prev; 313 GPollRec *next; 314 gint priority; 315 }; 316 317 struct _GSourcePrivate 318 { 319 GSList *child_sources; 320 GSource *parent_source; 321 }; 322 323 #ifdef G_THREADS_ENABLED 324 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex) 325 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex) 326 #define G_THREAD_SELF g_thread_self () 327 #else 328 #define LOCK_CONTEXT(context) (void)0 329 #define UNLOCK_CONTEXT(context) (void)0 330 #define G_THREAD_SELF NULL 331 #endif 332 333 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0) 334 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \ 335 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0) 336 337 #define SOURCE_UNREF(source, context) \ 338 G_STMT_START { \ 339 if ((source)->ref_count > 1) \ 340 (source)->ref_count--; \ 341 else \ 342 g_source_unref_internal ((source), (context), TRUE); \ 343 } G_STMT_END 344 345 346 /* Forward declarations */ 347 348 static void g_source_unref_internal (GSource *source, 349 GMainContext *context, 350 gboolean have_lock); 351 static void g_source_destroy_internal (GSource *source, 352 GMainContext *context, 353 gboolean have_lock); 354 static void g_source_set_priority_unlocked (GSource *source, 355 GMainContext *context, 356 gint priority); 357 static void g_main_context_poll (GMainContext *context, 358 gint timeout, 359 gint priority, 360 GPollFD *fds, 361 gint n_fds); 362 static void g_main_context_add_poll_unlocked (GMainContext *context, 363 gint priority, 364 GPollFD *fd); 365 static void g_main_context_remove_poll_unlocked (GMainContext *context, 366 GPollFD *fd); 367 static void g_main_context_wakeup_unlocked (GMainContext *context); 368 369 static gboolean g_timeout_prepare (GSource *source, 370 gint *timeout); 371 static gboolean g_timeout_check (GSource *source); 372 static gboolean g_timeout_dispatch (GSource *source, 373 GSourceFunc callback, 374 gpointer user_data); 375 static gboolean g_child_watch_prepare (GSource *source, 376 gint *timeout); 377 static gboolean g_child_watch_check (GSource *source); 378 static gboolean g_child_watch_dispatch (GSource *source, 379 GSourceFunc callback, 380 gpointer user_data); 381 static gboolean g_idle_prepare (GSource *source, 382 gint *timeout); 383 static gboolean g_idle_check (GSource *source); 384 static gboolean g_idle_dispatch (GSource *source, 385 GSourceFunc callback, 386 gpointer user_data); 387 388 G_LOCK_DEFINE_STATIC (main_loop); 389 static GMainContext *default_main_context; 390 static GSList *main_contexts_without_pipe = NULL; 391 392 #ifndef G_OS_WIN32 393 /* Child status monitoring code */ 394 enum { 395 CHILD_WATCH_UNINITIALIZED, 396 CHILD_WATCH_INITIALIZED_SINGLE, 397 CHILD_WATCH_INITIALIZED_THREADED 398 }; 399 static gint child_watch_init_state = CHILD_WATCH_UNINITIALIZED; 400 static gint child_watch_count = 1; 401 static gint child_watch_wake_up_pipe[2] = {0, 0}; 402 #endif /* !G_OS_WIN32 */ 403 G_LOCK_DEFINE_STATIC (main_context_list); 404 static GSList *main_context_list = NULL; 405 406 GSourceFuncs g_timeout_funcs = 407 { 408 g_timeout_prepare, 409 g_timeout_check, 410 g_timeout_dispatch, 411 NULL 412 }; 413 414 GSourceFuncs g_child_watch_funcs = 415 { 416 g_child_watch_prepare, 417 g_child_watch_check, 418 g_child_watch_dispatch, 419 NULL 420 }; 421 422 GSourceFuncs g_idle_funcs = 423 { 424 g_idle_prepare, 425 g_idle_check, 426 g_idle_dispatch, 427 NULL 428 }; 429 430 /** 431 * g_main_context_ref: 432 * @context: a #GMainContext 433 * 434 * Increases the reference count on a #GMainContext object by one. 435 * 436 * Returns: the @context that was passed in (since 2.6) 437 **/ 438 GMainContext * 439 g_main_context_ref (GMainContext *context) 440 { 441 g_return_val_if_fail (context != NULL, NULL); 442 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL); 443 444 g_atomic_int_inc (&context->ref_count); 445 446 return context; 447 } 448 449 static inline void 450 poll_rec_list_free (GMainContext *context, 451 GPollRec *list) 452 { 453 g_slice_free_chain (GPollRec, list, next); 454 } 455 456 /** 457 * g_main_context_unref: 458 * @context: a #GMainContext 459 * 460 * Decreases the reference count on a #GMainContext object by one. If 461 * the result is zero, free the context and free all associated memory. 462 **/ 463 void 464 g_main_context_unref (GMainContext *context) 465 { 466 GSource *source; 467 g_return_if_fail (context != NULL); 468 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); 469 470 if (!g_atomic_int_dec_and_test (&context->ref_count)) 471 return; 472 473 G_LOCK (main_context_list); 474 main_context_list = g_slist_remove (main_context_list, context); 475 G_UNLOCK (main_context_list); 476 477 source = context->source_list; 478 while (source) 479 { 480 GSource *next = source->next; 481 g_source_destroy_internal (source, context, FALSE); 482 source = next; 483 } 484 485 #ifdef G_THREADS_ENABLED 486 g_static_mutex_free (&context->mutex); 487 #endif 488 489 g_ptr_array_free (context->pending_dispatches, TRUE); 490 g_free (context->cached_poll_array); 491 492 poll_rec_list_free (context, context->poll_records); 493 494 #ifdef G_THREADS_ENABLED 495 if (g_thread_supported()) 496 { 497 #ifndef G_OS_WIN32 498 close (context->wake_up_pipe[0]); 499 close (context->wake_up_pipe[1]); 500 #else 501 CloseHandle (context->wake_up_semaphore); 502 #endif 503 } 504 else 505 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe, 506 context); 507 508 if (context->cond != NULL) 509 g_cond_free (context->cond); 510 #endif 511 512 g_free (context); 513 } 514 515 #ifdef G_THREADS_ENABLED 516 static void 517 g_main_context_init_pipe (GMainContext *context) 518 { 519 # ifndef G_OS_WIN32 520 if (context->wake_up_pipe[0] != -1) 521 return; 522 523 #ifdef HAVE_PIPE2 524 /* if this fails, we fall through and try pipe */ 525 pipe2 (context->wake_up_pipe, O_CLOEXEC); 526 #endif 527 if (context->wake_up_pipe[0] == -1) 528 { 529 if (pipe (context->wake_up_pipe) < 0) 530 g_error ("Cannot create pipe main loop wake-up: %s\n", 531 g_strerror (errno)); 532 533 fcntl (context->wake_up_pipe[0], F_SETFD, FD_CLOEXEC); 534 fcntl (context->wake_up_pipe[1], F_SETFD, FD_CLOEXEC); 535 } 536 537 context->wake_up_rec.fd = context->wake_up_pipe[0]; 538 context->wake_up_rec.events = G_IO_IN; 539 # else 540 if (context->wake_up_semaphore != NULL) 541 return; 542 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL); 543 if (context->wake_up_semaphore == NULL) 544 g_error ("Cannot create wake-up semaphore: %s", 545 g_win32_error_message (GetLastError ())); 546 context->wake_up_rec.fd = (gintptr) context->wake_up_semaphore; 547 context->wake_up_rec.events = G_IO_IN; 548 549 if (_g_main_poll_debug) 550 g_print ("wake-up semaphore: %p\n", context->wake_up_semaphore); 551 # endif 552 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec); 553 } 554 555 void 556 _g_main_thread_init (void) 557 { 558 GSList *curr = main_contexts_without_pipe; 559 while (curr) 560 { 561 g_main_context_init_pipe ((GMainContext *)curr->data); 562 curr = curr->next; 563 } 564 g_slist_free (main_contexts_without_pipe); 565 main_contexts_without_pipe = NULL; 566 } 567 #endif /* G_THREADS_ENABLED */ 568 569 /** 570 * g_main_context_new: 571 * 572 * Creates a new #GMainContext structure. 573 * 574 * Return value: the new #GMainContext 575 **/ 576 GMainContext * 577 g_main_context_new (void) 578 { 579 GMainContext *context = g_new0 (GMainContext, 1); 580 581 #ifdef G_MAIN_POLL_DEBUG 582 { 583 static gboolean beenhere = FALSE; 584 585 if (!beenhere) 586 { 587 if (getenv ("G_MAIN_POLL_DEBUG") != NULL) 588 _g_main_poll_debug = TRUE; 589 beenhere = TRUE; 590 } 591 } 592 #endif 593 594 #ifdef G_THREADS_ENABLED 595 g_static_mutex_init (&context->mutex); 596 597 context->owner = NULL; 598 context->waiters = NULL; 599 600 # ifndef G_OS_WIN32 601 context->wake_up_pipe[0] = -1; 602 context->wake_up_pipe[1] = -1; 603 # else 604 context->wake_up_semaphore = NULL; 605 # endif 606 #endif 607 608 context->ref_count = 1; 609 610 context->next_id = 1; 611 612 context->source_list = NULL; 613 614 context->poll_func = g_poll; 615 616 context->cached_poll_array = NULL; 617 context->cached_poll_array_size = 0; 618 619 context->pending_dispatches = g_ptr_array_new (); 620 621 context->time_is_fresh = FALSE; 622 context->real_time_is_fresh = FALSE; 623 624 #ifdef G_THREADS_ENABLED 625 if (g_thread_supported ()) 626 g_main_context_init_pipe (context); 627 else 628 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe, 629 context); 630 #endif 631 632 G_LOCK (main_context_list); 633 main_context_list = g_slist_append (main_context_list, context); 634 635 #ifdef G_MAIN_POLL_DEBUG 636 if (_g_main_poll_debug) 637 g_print ("created context=%p\n", context); 638 #endif 639 640 G_UNLOCK (main_context_list); 641 642 return context; 643 } 644 645 /** 646 * g_main_context_default: 647 * 648 * Returns the global default main context. This is the main context 649 * used for main loop functions when a main loop is not explicitly 650 * specified, and corresponds to the "main" main loop. See also 651 * g_main_context_get_thread_default(). 652 * 653 * Return value: the global default main context. 654 **/ 655 GMainContext * 656 g_main_context_default (void) 657 { 658 /* Slow, but safe */ 659 660 G_LOCK (main_loop); 661 662 if (!default_main_context) 663 { 664 default_main_context = g_main_context_new (); 665 #ifdef G_MAIN_POLL_DEBUG 666 if (_g_main_poll_debug) 667 g_print ("default context=%p\n", default_main_context); 668 #endif 669 } 670 671 G_UNLOCK (main_loop); 672 673 return default_main_context; 674 } 675 676 static GStaticPrivate thread_context_stack = G_STATIC_PRIVATE_INIT; 677 678 static void 679 free_context_stack (gpointer data) 680 { 681 GQueue *stack = data; 682 GMainContext *context; 683 684 while (!g_queue_is_empty (stack)) 685 { 686 context = g_queue_pop_head (stack); 687 g_main_context_release (context); 688 if (context) 689 g_main_context_unref (context); 690 } 691 g_queue_free (stack); 692 } 693 694 /** 695 * g_main_context_push_thread_default: 696 * @context: a #GMainContext, or %NULL for the global default context 697 * 698 * Acquires @context and sets it as the thread-default context for the 699 * current thread. This will cause certain asynchronous operations 700 * (such as most <link linkend="gio">gio</link>-based I/O) which are 701 * started in this thread to run under @context and deliver their 702 * results to its main loop, rather than running under the global 703 * default context in the main thread. Note that calling this function 704 * changes the context returned by 705 * g_main_context_get_thread_default(), <emphasis>not</emphasis> the 706 * one returned by g_main_context_default(), so it does not affect the 707 * context used by functions like g_idle_add(). 708 * 709 * Normally you would call this function shortly after creating a new 710 * thread, passing it a #GMainContext which will be run by a 711 * #GMainLoop in that thread, to set a new default context for all 712 * async operations in that thread. (In this case, you don't need to 713 * ever call g_main_context_pop_thread_default().) In some cases 714 * however, you may want to schedule a single operation in a 715 * non-default context, or temporarily use a non-default context in 716 * the main thread. In that case, you can wrap the call to the 717 * asynchronous operation inside a 718 * g_main_context_push_thread_default() / 719 * g_main_context_pop_thread_default() pair, but it is up to you to 720 * ensure that no other asynchronous operations accidentally get 721 * started while the non-default context is active. 722 * 723 * Beware that libraries that predate this function may not correctly 724 * handle being used from a thread with a thread-default context. Eg, 725 * see g_file_supports_thread_contexts(). 726 * 727 * Since: 2.22 728 **/ 729 void 730 g_main_context_push_thread_default (GMainContext *context) 731 { 732 GQueue *stack; 733 gboolean acquired_context; 734 735 acquired_context = g_main_context_acquire (context); 736 g_return_if_fail (acquired_context); 737 738 if (context == g_main_context_default ()) 739 context = NULL; 740 else if (context) 741 g_main_context_ref (context); 742 743 stack = g_static_private_get (&thread_context_stack); 744 if (!stack) 745 { 746 stack = g_queue_new (); 747 g_static_private_set (&thread_context_stack, stack, 748 free_context_stack); 749 } 750 751 g_queue_push_head (stack, context); 752 } 753 754 /** 755 * g_main_context_pop_thread_default: 756 * @context: a #GMainContext object, or %NULL 757 * 758 * Pops @context off the thread-default context stack (verifying that 759 * it was on the top of the stack). 760 * 761 * Since: 2.22 762 **/ 763 void 764 g_main_context_pop_thread_default (GMainContext *context) 765 { 766 GQueue *stack; 767 768 if (context == g_main_context_default ()) 769 context = NULL; 770 771 stack = g_static_private_get (&thread_context_stack); 772 773 g_return_if_fail (stack != NULL); 774 g_return_if_fail (g_queue_peek_head (stack) == context); 775 776 g_queue_pop_head (stack); 777 778 g_main_context_release (context); 779 if (context) 780 g_main_context_unref (context); 781 } 782 783 /** 784 * g_main_context_get_thread_default: 785 * 786 * Gets the thread-default #GMainContext for this thread. Asynchronous 787 * operations that want to be able to be run in contexts other than 788 * the default one should call this method to get a #GMainContext to 789 * add their #GSource<!-- -->s to. (Note that even in single-threaded 790 * programs applications may sometimes want to temporarily push a 791 * non-default context, so it is not safe to assume that this will 792 * always return %NULL if threads are not initialized.) 793 * 794 * Returns: the thread-default #GMainContext, or %NULL if the 795 * thread-default context is the global default context. 796 * 797 * Since: 2.22 798 **/ 799 GMainContext * 800 g_main_context_get_thread_default (void) 801 { 802 GQueue *stack; 803 804 stack = g_static_private_get (&thread_context_stack); 805 if (stack) 806 return g_queue_peek_head (stack); 807 else 808 return NULL; 809 } 810 811 /* Hooks for adding to the main loop */ 812 813 /** 814 * g_source_new: 815 * @source_funcs: structure containing functions that implement 816 * the sources behavior. 817 * @struct_size: size of the #GSource structure to create. 818 * 819 * Creates a new #GSource structure. The size is specified to 820 * allow creating structures derived from #GSource that contain 821 * additional data. The size passed in must be at least 822 * <literal>sizeof (GSource)</literal>. 823 * 824 * The source will not initially be associated with any #GMainContext 825 * and must be added to one with g_source_attach() before it will be 826 * executed. 827 * 828 * Return value: the newly-created #GSource. 829 **/ 830 GSource * 831 g_source_new (GSourceFuncs *source_funcs, 832 guint struct_size) 833 { 834 GSource *source; 835 836 g_return_val_if_fail (source_funcs != NULL, NULL); 837 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL); 838 839 source = (GSource*) g_malloc0 (struct_size); 840 #ifdef GSTREAMER_LITE 841 if (source == NULL) 842 return NULL; 843 #endif // GSTREAMER_LITE 844 845 source->source_funcs = source_funcs; 846 source->ref_count = 1; 847 848 source->priority = G_PRIORITY_DEFAULT; 849 850 source->flags = G_HOOK_FLAG_ACTIVE; 851 852 /* NULL/0 initialization for all other fields */ 853 854 return source; 855 } 856 857 /* Holds context's lock 858 */ 859 static void 860 g_source_list_add (GSource *source, 861 GMainContext *context) 862 { 863 GSource *tmp_source, *last_source; 864 865 if (source->priv && source->priv->parent_source) 866 { 867 /* Put the source immediately before its parent */ 868 tmp_source = source->priv->parent_source; 869 last_source = source->priv->parent_source->prev; 870 } 871 else 872 { 873 last_source = NULL; 874 tmp_source = context->source_list; 875 while (tmp_source && tmp_source->priority <= source->priority) 876 { 877 last_source = tmp_source; 878 tmp_source = tmp_source->next; 879 } 880 } 881 882 source->next = tmp_source; 883 if (tmp_source) 884 tmp_source->prev = source; 885 886 source->prev = last_source; 887 if (last_source) 888 last_source->next = source; 889 else 890 context->source_list = source; 891 } 892 893 /* Holds context's lock 894 */ 895 static void 896 g_source_list_remove (GSource *source, 897 GMainContext *context) 898 { 899 if (source->prev) 900 source->prev->next = source->next; 901 else 902 context->source_list = source->next; 903 904 if (source->next) 905 source->next->prev = source->prev; 906 907 source->prev = NULL; 908 source->next = NULL; 909 } 910 911 static guint 912 g_source_attach_unlocked (GSource *source, 913 GMainContext *context) 914 { 915 guint result = 0; 916 GSList *tmp_list; 917 918 source->context = context; 919 result = source->source_id = context->next_id++; 920 921 source->ref_count++; 922 g_source_list_add (source, context); 923 924 tmp_list = source->poll_fds; 925 while (tmp_list) 926 { 927 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data); 928 tmp_list = tmp_list->next; 929 } 930 931 if (source->priv) 932 { 933 tmp_list = source->priv->child_sources; 934 while (tmp_list) 935 { 936 g_source_attach_unlocked (tmp_list->data, context); 937 tmp_list = tmp_list->next; 938 } 939 } 940 941 return result; 942 } 943 944 /** 945 * g_source_attach: 946 * @source: a #GSource 947 * @context: a #GMainContext (if %NULL, the default context will be used) 948 * 949 * Adds a #GSource to a @context so that it will be executed within 950 * that context. Remove it by calling g_source_destroy(). 951 * 952 * Return value: the ID (greater than 0) for the source within the 953 * #GMainContext. 954 **/ 955 guint 956 g_source_attach (GSource *source, 957 GMainContext *context) 958 { 959 guint result = 0; 960 961 #ifdef GSTREAMER_LITE 962 if (source == NULL) { 963 return 0; 964 } 965 #endif // GSTREAMER_LITE 966 967 g_return_val_if_fail (source->context == NULL, 0); 968 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0); 969 970 if (!context) 971 context = g_main_context_default (); 972 973 LOCK_CONTEXT (context); 974 975 result = g_source_attach_unlocked (source, context); 976 977 #ifdef G_THREADS_ENABLED 978 /* Now wake up the main loop if it is waiting in the poll() */ 979 g_main_context_wakeup_unlocked (context); 980 #endif 981 982 UNLOCK_CONTEXT (context); 983 984 return result; 985 } 986 987 static void 988 g_source_destroy_internal (GSource *source, 989 GMainContext *context, 990 gboolean have_lock) 991 { 992 if (!have_lock) 993 LOCK_CONTEXT (context); 994 995 if (!SOURCE_DESTROYED (source)) 996 { 997 GSList *tmp_list; 998 gpointer old_cb_data; 999 GSourceCallbackFuncs *old_cb_funcs; 1000 1001 source->flags &= ~G_HOOK_FLAG_ACTIVE; 1002 1003 old_cb_data = source->callback_data; 1004 old_cb_funcs = source->callback_funcs; 1005 1006 source->callback_data = NULL; 1007 source->callback_funcs = NULL; 1008 1009 if (old_cb_funcs) 1010 { 1011 UNLOCK_CONTEXT (context); 1012 old_cb_funcs->unref (old_cb_data); 1013 LOCK_CONTEXT (context); 1014 } 1015 1016 if (!SOURCE_BLOCKED (source)) 1017 { 1018 tmp_list = source->poll_fds; 1019 while (tmp_list) 1020 { 1021 g_main_context_remove_poll_unlocked (context, tmp_list->data); 1022 tmp_list = tmp_list->next; 1023 } 1024 } 1025 1026 if (source->priv && source->priv->child_sources) 1027 { 1028 /* This is safe because even if a child_source finalizer or 1029 * closure notify tried to modify source->priv->child_sources 1030 * from outside the lock, it would fail since 1031 * SOURCE_DESTROYED(source) is now TRUE. 1032 */ 1033 tmp_list = source->priv->child_sources; 1034 while (tmp_list) 1035 { 1036 g_source_destroy_internal (tmp_list->data, context, TRUE); 1037 g_source_unref_internal (tmp_list->data, context, TRUE); 1038 tmp_list = tmp_list->next; 1039 } 1040 g_slist_free (source->priv->child_sources); 1041 source->priv->child_sources = NULL; 1042 } 1043 1044 g_source_unref_internal (source, context, TRUE); 1045 } 1046 1047 if (!have_lock) 1048 UNLOCK_CONTEXT (context); 1049 } 1050 1051 /** 1052 * g_source_destroy: 1053 * @source: a #GSource 1054 * 1055 * Removes a source from its #GMainContext, if any, and mark it as 1056 * destroyed. The source cannot be subsequently added to another 1057 * context. 1058 **/ 1059 void 1060 g_source_destroy (GSource *source) 1061 { 1062 GMainContext *context; 1063 1064 g_return_if_fail (source != NULL); 1065 1066 context = source->context; 1067 1068 if (context) 1069 g_source_destroy_internal (source, context, FALSE); 1070 else 1071 source->flags &= ~G_HOOK_FLAG_ACTIVE; 1072 } 1073 1074 /** 1075 * g_source_get_id: 1076 * @source: a #GSource 1077 * 1078 * Returns the numeric ID for a particular source. The ID of a source 1079 * is a positive integer which is unique within a particular main loop 1080 * context. The reverse 1081 * mapping from ID to source is done by g_main_context_find_source_by_id(). 1082 * 1083 * Return value: the ID (greater than 0) for the source 1084 **/ 1085 guint 1086 g_source_get_id (GSource *source) 1087 { 1088 guint result; 1089 1090 g_return_val_if_fail (source != NULL, 0); 1091 g_return_val_if_fail (source->context != NULL, 0); 1092 1093 LOCK_CONTEXT (source->context); 1094 result = source->source_id; 1095 UNLOCK_CONTEXT (source->context); 1096 1097 return result; 1098 } 1099 1100 /** 1101 * g_source_get_context: 1102 * @source: a #GSource 1103 * 1104 * Gets the #GMainContext with which the source is associated. 1105 * Calling this function on a destroyed source is an error. 1106 * 1107 * Return value: the #GMainContext with which the source is associated, 1108 * or %NULL if the context has not yet been added 1109 * to a source. 1110 **/ 1111 GMainContext * 1112 g_source_get_context (GSource *source) 1113 { 1114 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL); 1115 1116 return source->context; 1117 } 1118 1119 /** 1120 * g_source_add_poll: 1121 * @source:a #GSource 1122 * @fd: a #GPollFD structure holding information about a file 1123 * descriptor to watch. 1124 * 1125 * Adds a file descriptor to the set of file descriptors polled for 1126 * this source. This is usually combined with g_source_new() to add an 1127 * event source. The event source's check function will typically test 1128 * the @revents field in the #GPollFD struct and return %TRUE if events need 1129 * to be processed. 1130 **/ 1131 void 1132 g_source_add_poll (GSource *source, 1133 GPollFD *fd) 1134 { 1135 GMainContext *context; 1136 1137 g_return_if_fail (source != NULL); 1138 g_return_if_fail (fd != NULL); 1139 g_return_if_fail (!SOURCE_DESTROYED (source)); 1140 1141 #ifdef GSTREAMER_LITE 1142 if (source == NULL) 1143 return; 1144 #endif // GSTREAMER_LITE 1145 1146 context = source->context; 1147 1148 if (context) 1149 LOCK_CONTEXT (context); 1150 1151 source->poll_fds = g_slist_prepend (source->poll_fds, fd); 1152 1153 if (context) 1154 { 1155 if (!SOURCE_BLOCKED (source)) 1156 g_main_context_add_poll_unlocked (context, source->priority, fd); 1157 UNLOCK_CONTEXT (context); 1158 } 1159 } 1160 1161 /** 1162 * g_source_remove_poll: 1163 * @source:a #GSource 1164 * @fd: a #GPollFD structure previously passed to g_source_add_poll(). 1165 * 1166 * Removes a file descriptor from the set of file descriptors polled for 1167 * this source. 1168 **/ 1169 void 1170 g_source_remove_poll (GSource *source, 1171 GPollFD *fd) 1172 { 1173 GMainContext *context; 1174 1175 g_return_if_fail (source != NULL); 1176 g_return_if_fail (fd != NULL); 1177 g_return_if_fail (!SOURCE_DESTROYED (source)); 1178 1179 #ifdef GSTREAMER_LITE 1180 if (source == NULL) 1181 return; 1182 #endif // GSTREAMER_LITE 1183 1184 context = source->context; 1185 1186 if (context) 1187 LOCK_CONTEXT (context); 1188 1189 source->poll_fds = g_slist_remove (source->poll_fds, fd); 1190 1191 if (context) 1192 { 1193 if (!SOURCE_BLOCKED (source)) 1194 g_main_context_remove_poll_unlocked (context, fd); 1195 UNLOCK_CONTEXT (context); 1196 } 1197 } 1198 1199 /** 1200 * g_source_add_child_source: 1201 * @source:a #GSource 1202 * @child_source: a second #GSource that @source should "poll" 1203 * 1204 * Adds @child_source to @source as a "polled" source; when @source is 1205 * added to a #GMainContext, @child_source will be automatically added 1206 * with the same priority, when @child_source is triggered, it will 1207 * cause @source to dispatch (in addition to calling its own 1208 * callback), and when @source is destroyed, it will destroy 1209 * @child_source as well. (@source will also still be dispatched if 1210 * its own prepare/check functions indicate that it is ready.) 1211 * 1212 * If you don't need @child_source to do anything on its own when it 1213 * triggers, you can call g_source_set_dummy_callback() on it to set a 1214 * callback that does nothing (except return %TRUE if appropriate). 1215 * 1216 * @source will hold a reference on @child_source while @child_source 1217 * is attached to it. 1218 * 1219 * Since: 2.28 1220 **/ 1221 void 1222 g_source_add_child_source (GSource *source, 1223 GSource *child_source) 1224 { 1225 GMainContext *context; 1226 1227 g_return_if_fail (source != NULL); 1228 g_return_if_fail (child_source != NULL); 1229 g_return_if_fail (!SOURCE_DESTROYED (source)); 1230 g_return_if_fail (!SOURCE_DESTROYED (child_source)); 1231 g_return_if_fail (child_source->context == NULL); 1232 g_return_if_fail (child_source->priv == NULL || child_source->priv->parent_source == NULL); 1233 1234 context = source->context; 1235 1236 if (context) 1237 LOCK_CONTEXT (context); 1238 1239 if (!source->priv) 1240 source->priv = g_slice_new0 (GSourcePrivate); 1241 if (!child_source->priv) 1242 child_source->priv = g_slice_new0 (GSourcePrivate); 1243 1244 source->priv->child_sources = g_slist_prepend (source->priv->child_sources, 1245 g_source_ref (child_source)); 1246 child_source->priv->parent_source = source; 1247 g_source_set_priority_unlocked (child_source, context, source->priority); 1248 1249 if (context) 1250 { 1251 UNLOCK_CONTEXT (context); 1252 g_source_attach (child_source, context); 1253 } 1254 } 1255 1256 /** 1257 * g_source_remove_child_source: 1258 * @source:a #GSource 1259 * @child_source: a #GSource previously passed to 1260 * g_source_add_child_source(). 1261 * 1262 * Detaches @child_source from @source and destroys it. 1263 * 1264 * Since: 2.28 1265 **/ 1266 void 1267 g_source_remove_child_source (GSource *source, 1268 GSource *child_source) 1269 { 1270 GMainContext *context; 1271 1272 g_return_if_fail (source != NULL); 1273 g_return_if_fail (child_source != NULL); 1274 g_return_if_fail (child_source->priv != NULL && child_source->priv->parent_source == source); 1275 g_return_if_fail (!SOURCE_DESTROYED (source)); 1276 g_return_if_fail (!SOURCE_DESTROYED (child_source)); 1277 1278 context = source->context; 1279 1280 if (context) 1281 LOCK_CONTEXT (context); 1282 1283 source->priv->child_sources = g_slist_remove (source->priv->child_sources, child_source); 1284 g_source_destroy_internal (child_source, context, TRUE); 1285 g_source_unref_internal (child_source, context, TRUE); 1286 1287 if (context) 1288 UNLOCK_CONTEXT (context); 1289 } 1290 1291 /** 1292 * g_source_set_callback_indirect: 1293 * @source: the source 1294 * @callback_data: pointer to callback data "object" 1295 * @callback_funcs: functions for reference counting @callback_data 1296 * and getting the callback and data 1297 * 1298 * Sets the callback function storing the data as a refcounted callback 1299 * "object". This is used internally. Note that calling 1300 * g_source_set_callback_indirect() assumes 1301 * an initial reference count on @callback_data, and thus 1302 * @callback_funcs->unref will eventually be called once more 1303 * than @callback_funcs->ref. 1304 **/ 1305 void 1306 g_source_set_callback_indirect (GSource *source, 1307 gpointer callback_data, 1308 GSourceCallbackFuncs *callback_funcs) 1309 { 1310 GMainContext *context; 1311 gpointer old_cb_data; 1312 GSourceCallbackFuncs *old_cb_funcs; 1313 1314 g_return_if_fail (source != NULL); 1315 g_return_if_fail (callback_funcs != NULL || callback_data == NULL); 1316 1317 context = source->context; 1318 1319 if (context) 1320 LOCK_CONTEXT (context); 1321 1322 old_cb_data = source->callback_data; 1323 old_cb_funcs = source->callback_funcs; 1324 1325 source->callback_data = callback_data; 1326 source->callback_funcs = callback_funcs; 1327 1328 if (context) 1329 UNLOCK_CONTEXT (context); 1330 1331 if (old_cb_funcs) 1332 old_cb_funcs->unref (old_cb_data); 1333 } 1334 1335 static void 1336 g_source_callback_ref (gpointer cb_data) 1337 { 1338 GSourceCallback *callback = cb_data; 1339 1340 callback->ref_count++; 1341 } 1342 1343 1344 static void 1345 g_source_callback_unref (gpointer cb_data) 1346 { 1347 GSourceCallback *callback = cb_data; 1348 1349 callback->ref_count--; 1350 if (callback->ref_count == 0) 1351 { 1352 if (callback->notify) 1353 callback->notify (callback->data); 1354 g_free (callback); 1355 } 1356 } 1357 1358 static void 1359 g_source_callback_get (gpointer cb_data, 1360 GSource *source, 1361 GSourceFunc *func, 1362 gpointer *data) 1363 { 1364 GSourceCallback *callback = cb_data; 1365 1366 *func = callback->func; 1367 *data = callback->data; 1368 } 1369 1370 static GSourceCallbackFuncs g_source_callback_funcs = { 1371 g_source_callback_ref, 1372 g_source_callback_unref, 1373 g_source_callback_get, 1374 }; 1375 1376 /** 1377 * g_source_set_callback: 1378 * @source: the source 1379 * @func: a callback function 1380 * @data: the data to pass to callback function 1381 * @notify: a function to call when @data is no longer in use, or %NULL. 1382 * 1383 * Sets the callback function for a source. The callback for a source is 1384 * called from the source's dispatch function. 1385 * 1386 * The exact type of @func depends on the type of source; ie. you 1387 * should not count on @func being called with @data as its first 1388 * parameter. 1389 * 1390 * Typically, you won't use this function. Instead use functions specific 1391 * to the type of source you are using. 1392 **/ 1393 void 1394 g_source_set_callback (GSource *source, 1395 GSourceFunc func, 1396 gpointer data, 1397 GDestroyNotify notify) 1398 { 1399 GSourceCallback *new_callback; 1400 1401 g_return_if_fail (source != NULL); 1402 #ifdef GSTREAMER_LITE 1403 if (source == NULL) 1404 return; 1405 #endif // GSTREAMER_LITE 1406 1407 new_callback = g_new (GSourceCallback, 1); 1408 1409 new_callback->ref_count = 1; 1410 new_callback->func = func; 1411 new_callback->data = data; 1412 new_callback->notify = notify; 1413 1414 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs); 1415 } 1416 1417 1418 /** 1419 * g_source_set_funcs: 1420 * @source: a #GSource 1421 * @funcs: the new #GSourceFuncs 1422 * 1423 * Sets the source functions (can be used to override 1424 * default implementations) of an unattached source. 1425 * 1426 * Since: 2.12 1427 */ 1428 void 1429 g_source_set_funcs (GSource *source, 1430 GSourceFuncs *funcs) 1431 { 1432 g_return_if_fail (source != NULL); 1433 g_return_if_fail (source->context == NULL); 1434 g_return_if_fail (source->ref_count > 0); 1435 g_return_if_fail (funcs != NULL); 1436 1437 source->source_funcs = funcs; 1438 } 1439 1440 static void 1441 g_source_set_priority_unlocked (GSource *source, 1442 GMainContext *context, 1443 gint priority) 1444 { 1445 GSList *tmp_list; 1446 1447 source->priority = priority; 1448 1449 if (context) 1450 { 1451 /* Remove the source from the context's source and then 1452 * add it back so it is sorted in the correct place 1453 */ 1454 g_source_list_remove (source, source->context); 1455 g_source_list_add (source, source->context); 1456 1457 if (!SOURCE_BLOCKED (source)) 1458 { 1459 tmp_list = source->poll_fds; 1460 while (tmp_list) 1461 { 1462 g_main_context_remove_poll_unlocked (context, tmp_list->data); 1463 g_main_context_add_poll_unlocked (context, priority, tmp_list->data); 1464 1465 tmp_list = tmp_list->next; 1466 } 1467 } 1468 } 1469 1470 if (source->priv && source->priv->child_sources) 1471 { 1472 tmp_list = source->priv->child_sources; 1473 while (tmp_list) 1474 { 1475 g_source_set_priority_unlocked (tmp_list->data, context, priority); 1476 tmp_list = tmp_list->next; 1477 } 1478 } 1479 } 1480 1481 /** 1482 * g_source_set_priority: 1483 * @source: a #GSource 1484 * @priority: the new priority. 1485 * 1486 * Sets the priority of a source. While the main loop is being run, a 1487 * source will be dispatched if it is ready to be dispatched and no 1488 * sources at a higher (numerically smaller) priority are ready to be 1489 * dispatched. 1490 **/ 1491 void 1492 g_source_set_priority (GSource *source, 1493 gint priority) 1494 { 1495 GMainContext *context; 1496 1497 #ifdef GSTREAMER_LITE 1498 if (source == NULL || source->priv == NULL || source->priv->parent_source == NULL) 1499 return; 1500 #endif // GSTREAMER_LITE 1501 1502 g_return_if_fail (source != NULL); 1503 1504 context = source->context; 1505 1506 if (context) 1507 LOCK_CONTEXT (context); 1508 g_source_set_priority_unlocked (source, context, priority); 1509 if (context) 1510 UNLOCK_CONTEXT (source->context); 1511 } 1512 1513 /** 1514 * g_source_get_priority: 1515 * @source: a #GSource 1516 * 1517 * Gets the priority of a source. 1518 * 1519 * Return value: the priority of the source 1520 **/ 1521 gint 1522 g_source_get_priority (GSource *source) 1523 { 1524 g_return_val_if_fail (source != NULL, 0); 1525 1526 return source->priority; 1527 } 1528 1529 /** 1530 * g_source_set_can_recurse: 1531 * @source: a #GSource 1532 * @can_recurse: whether recursion is allowed for this source 1533 * 1534 * Sets whether a source can be called recursively. If @can_recurse is 1535 * %TRUE, then while the source is being dispatched then this source 1536 * will be processed normally. Otherwise, all processing of this 1537 * source is blocked until the dispatch function returns. 1538 **/ 1539 void 1540 g_source_set_can_recurse (GSource *source, 1541 gboolean can_recurse) 1542 { 1543 GMainContext *context; 1544 1545 g_return_if_fail (source != NULL); 1546 1547 context = source->context; 1548 1549 if (context) 1550 LOCK_CONTEXT (context); 1551 1552 if (can_recurse) 1553 source->flags |= G_SOURCE_CAN_RECURSE; 1554 else 1555 source->flags &= ~G_SOURCE_CAN_RECURSE; 1556 1557 if (context) 1558 UNLOCK_CONTEXT (context); 1559 } 1560 1561 /** 1562 * g_source_get_can_recurse: 1563 * @source: a #GSource 1564 * 1565 * Checks whether a source is allowed to be called recursively. 1566 * see g_source_set_can_recurse(). 1567 * 1568 * Return value: whether recursion is allowed. 1569 **/ 1570 gboolean 1571 g_source_get_can_recurse (GSource *source) 1572 { 1573 g_return_val_if_fail (source != NULL, FALSE); 1574 1575 return (source->flags & G_SOURCE_CAN_RECURSE) != 0; 1576 } 1577 1578 1579 /** 1580 * g_source_set_name: 1581 * @source: a #GSource 1582 * @name: debug name for the source 1583 * 1584 * Sets a name for the source, used in debugging and profiling. 1585 * The name defaults to #NULL. 1586 * 1587 * The source name should describe in a human-readable way 1588 * what the source does. For example, "X11 event queue" 1589 * or "GTK+ repaint idle handler" or whatever it is. 1590 * 1591 * It is permitted to call this function multiple times, but is not 1592 * recommended due to the potential performance impact. For example, 1593 * one could change the name in the "check" function of a #GSourceFuncs 1594 * to include details like the event type in the source name. 1595 * 1596 * Since: 2.26 1597 **/ 1598 void 1599 g_source_set_name (GSource *source, 1600 const char *name) 1601 { 1602 g_return_if_fail (source != NULL); 1603 1604 /* setting back to NULL is allowed, just because it's 1605 * weird if get_name can return NULL but you can't 1606 * set that. 1607 */ 1608 1609 g_free (source->name); 1610 source->name = g_strdup (name); 1611 } 1612 1613 /** 1614 * g_source_get_name: 1615 * @source: a #GSource 1616 * 1617 * Gets a name for the source, used in debugging and profiling. 1618 * The name may be #NULL if it has never been set with 1619 * g_source_set_name(). 1620 * 1621 * Return value: the name of the source 1622 * Since: 2.26 1623 **/ 1624 G_CONST_RETURN char* 1625 g_source_get_name (GSource *source) 1626 { 1627 g_return_val_if_fail (source != NULL, NULL); 1628 1629 return source->name; 1630 } 1631 1632 /** 1633 * g_source_set_name_by_id: 1634 * @tag: a #GSource ID 1635 * @name: debug name for the source 1636 * 1637 * Sets the name of a source using its ID. 1638 * 1639 * This is a convenience utility to set source names from the return 1640 * value of g_idle_add(), g_timeout_add(), etc. 1641 * 1642 * Since: 2.26 1643 **/ 1644 void 1645 g_source_set_name_by_id (guint tag, 1646 const char *name) 1647 { 1648 GSource *source; 1649 1650 g_return_if_fail (tag > 0); 1651 1652 source = g_main_context_find_source_by_id (NULL, tag); 1653 if (source == NULL) 1654 return; 1655 1656 g_source_set_name (source, name); 1657 } 1658 1659 1660 /** 1661 * g_source_ref: 1662 * @source: a #GSource 1663 * 1664 * Increases the reference count on a source by one. 1665 * 1666 * Return value: @source 1667 **/ 1668 GSource * 1669 g_source_ref (GSource *source) 1670 { 1671 GMainContext *context; 1672 1673 g_return_val_if_fail (source != NULL, NULL); 1674 1675 context = source->context; 1676 1677 if (context) 1678 LOCK_CONTEXT (context); 1679 1680 source->ref_count++; 1681 1682 if (context) 1683 UNLOCK_CONTEXT (context); 1684 1685 return source; 1686 } 1687 1688 /* g_source_unref() but possible to call within context lock 1689 */ 1690 static void 1691 g_source_unref_internal (GSource *source, 1692 GMainContext *context, 1693 gboolean have_lock) 1694 { 1695 gpointer old_cb_data = NULL; 1696 GSourceCallbackFuncs *old_cb_funcs = NULL; 1697 1698 g_return_if_fail (source != NULL); 1699 1700 if (!have_lock && context) 1701 LOCK_CONTEXT (context); 1702 1703 source->ref_count--; 1704 if (source->ref_count == 0) 1705 { 1706 old_cb_data = source->callback_data; 1707 old_cb_funcs = source->callback_funcs; 1708 1709 source->callback_data = NULL; 1710 source->callback_funcs = NULL; 1711 1712 if (context) 1713 { 1714 if (!SOURCE_DESTROYED (source)) 1715 g_warning (G_STRLOC ": ref_count == 0, but source was still attached to a context!"); 1716 g_source_list_remove (source, context); 1717 } 1718 1719 if (source->source_funcs->finalize) 1720 { 1721 if (context) 1722 UNLOCK_CONTEXT (context); 1723 source->source_funcs->finalize (source); 1724 if (context) 1725 LOCK_CONTEXT (context); 1726 } 1727 1728 g_free (source->name); 1729 source->name = NULL; 1730 1731 g_slist_free (source->poll_fds); 1732 source->poll_fds = NULL; 1733 g_free (source); 1734 } 1735 1736 if (!have_lock && context) 1737 UNLOCK_CONTEXT (context); 1738 1739 if (old_cb_funcs) 1740 { 1741 if (have_lock) 1742 UNLOCK_CONTEXT (context); 1743 1744 old_cb_funcs->unref (old_cb_data); 1745 1746 if (have_lock) 1747 LOCK_CONTEXT (context); 1748 } 1749 } 1750 1751 /** 1752 * g_source_unref: 1753 * @source: a #GSource 1754 * 1755 * Decreases the reference count of a source by one. If the 1756 * resulting reference count is zero the source and associated 1757 * memory will be destroyed. 1758 **/ 1759 void 1760 g_source_unref (GSource *source) 1761 { 1762 g_return_if_fail (source != NULL); 1763 #ifdef GSTREAMER_LITE 1764 if (source == NULL) 1765 return; 1766 #endif // GSTREAMER_LITE 1767 1768 g_source_unref_internal (source, source->context, FALSE); 1769 } 1770 1771 /** 1772 * g_main_context_find_source_by_id: 1773 * @context: a #GMainContext (if %NULL, the default context will be used) 1774 * @source_id: the source ID, as returned by g_source_get_id(). 1775 * 1776 * Finds a #GSource given a pair of context and ID. 1777 * 1778 * Return value: the #GSource if found, otherwise, %NULL 1779 **/ 1780 GSource * 1781 g_main_context_find_source_by_id (GMainContext *context, 1782 guint source_id) 1783 { 1784 GSource *source; 1785 1786 g_return_val_if_fail (source_id > 0, NULL); 1787 1788 if (context == NULL) 1789 context = g_main_context_default (); 1790 1791 LOCK_CONTEXT (context); 1792 1793 source = context->source_list; 1794 while (source) 1795 { 1796 if (!SOURCE_DESTROYED (source) && 1797 source->source_id == source_id) 1798 break; 1799 source = source->next; 1800 } 1801 1802 UNLOCK_CONTEXT (context); 1803 1804 return source; 1805 } 1806 1807 /** 1808 * g_main_context_find_source_by_funcs_user_data: 1809 * @context: a #GMainContext (if %NULL, the default context will be used). 1810 * @funcs: the @source_funcs passed to g_source_new(). 1811 * @user_data: the user data from the callback. 1812 * 1813 * Finds a source with the given source functions and user data. If 1814 * multiple sources exist with the same source function and user data, 1815 * the first one found will be returned. 1816 * 1817 * Return value: the source, if one was found, otherwise %NULL 1818 **/ 1819 GSource * 1820 g_main_context_find_source_by_funcs_user_data (GMainContext *context, 1821 GSourceFuncs *funcs, 1822 gpointer user_data) 1823 { 1824 GSource *source; 1825 1826 g_return_val_if_fail (funcs != NULL, NULL); 1827 1828 if (context == NULL) 1829 context = g_main_context_default (); 1830 1831 LOCK_CONTEXT (context); 1832 1833 source = context->source_list; 1834 while (source) 1835 { 1836 if (!SOURCE_DESTROYED (source) && 1837 source->source_funcs == funcs && 1838 source->callback_funcs) 1839 { 1840 GSourceFunc callback; 1841 gpointer callback_data; 1842 1843 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data); 1844 1845 if (callback_data == user_data) 1846 break; 1847 } 1848 source = source->next; 1849 } 1850 1851 UNLOCK_CONTEXT (context); 1852 1853 return source; 1854 } 1855 1856 /** 1857 * g_main_context_find_source_by_user_data: 1858 * @context: a #GMainContext 1859 * @user_data: the user_data for the callback. 1860 * 1861 * Finds a source with the given user data for the callback. If 1862 * multiple sources exist with the same user data, the first 1863 * one found will be returned. 1864 * 1865 * Return value: the source, if one was found, otherwise %NULL 1866 **/ 1867 GSource * 1868 g_main_context_find_source_by_user_data (GMainContext *context, 1869 gpointer user_data) 1870 { 1871 GSource *source; 1872 1873 if (context == NULL) 1874 context = g_main_context_default (); 1875 1876 LOCK_CONTEXT (context); 1877 1878 source = context->source_list; 1879 while (source) 1880 { 1881 if (!SOURCE_DESTROYED (source) && 1882 source->callback_funcs) 1883 { 1884 GSourceFunc callback; 1885 gpointer callback_data = NULL; 1886 1887 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data); 1888 1889 if (callback_data == user_data) 1890 break; 1891 } 1892 source = source->next; 1893 } 1894 1895 UNLOCK_CONTEXT (context); 1896 1897 return source; 1898 } 1899 1900 /** 1901 * g_source_remove: 1902 * @tag: the ID of the source to remove. 1903 * 1904 * Removes the source with the given id from the default main context. 1905 * The id of 1906 * a #GSource is given by g_source_get_id(), or will be returned by the 1907 * functions g_source_attach(), g_idle_add(), g_idle_add_full(), 1908 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(), 1909 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full(). 1910 * 1911 * See also g_source_destroy(). You must use g_source_destroy() for sources 1912 * added to a non-default main context. 1913 * 1914 * Return value: %TRUE if the source was found and removed. 1915 **/ 1916 gboolean 1917 g_source_remove (guint tag) 1918 { 1919 GSource *source; 1920 1921 g_return_val_if_fail (tag > 0, FALSE); 1922 1923 source = g_main_context_find_source_by_id (NULL, tag); 1924 if (source) 1925 g_source_destroy (source); 1926 1927 return source != NULL; 1928 } 1929 1930 /** 1931 * g_source_remove_by_user_data: 1932 * @user_data: the user_data for the callback. 1933 * 1934 * Removes a source from the default main loop context given the user 1935 * data for the callback. If multiple sources exist with the same user 1936 * data, only one will be destroyed. 1937 * 1938 * Return value: %TRUE if a source was found and removed. 1939 **/ 1940 gboolean 1941 g_source_remove_by_user_data (gpointer user_data) 1942 { 1943 GSource *source; 1944 1945 source = g_main_context_find_source_by_user_data (NULL, user_data); 1946 if (source) 1947 { 1948 g_source_destroy (source); 1949 return TRUE; 1950 } 1951 else 1952 return FALSE; 1953 } 1954 1955 /** 1956 * g_source_remove_by_funcs_user_data: 1957 * @funcs: The @source_funcs passed to g_source_new() 1958 * @user_data: the user data for the callback 1959 * 1960 * Removes a source from the default main loop context given the 1961 * source functions and user data. If multiple sources exist with the 1962 * same source functions and user data, only one will be destroyed. 1963 * 1964 * Return value: %TRUE if a source was found and removed. 1965 **/ 1966 gboolean 1967 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs, 1968 gpointer user_data) 1969 { 1970 GSource *source; 1971 1972 g_return_val_if_fail (funcs != NULL, FALSE); 1973 1974 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data); 1975 if (source) 1976 { 1977 g_source_destroy (source); 1978 return TRUE; 1979 } 1980 else 1981 return FALSE; 1982 } 1983 1984 /** 1985 * g_get_current_time: 1986 * @result: #GTimeVal structure in which to store current time. 1987 * 1988 * Equivalent to the UNIX gettimeofday() function, but portable. 1989 * 1990 * You may find g_get_real_time() to be more convenient. 1991 **/ 1992 void 1993 g_get_current_time (GTimeVal *result) 1994 { 1995 #ifndef G_OS_WIN32 1996 struct timeval r; 1997 1998 g_return_if_fail (result != NULL); 1999 2000 /*this is required on alpha, there the timeval structs are int's 2001 not longs and a cast only would fail horribly*/ 2002 gettimeofday (&r, NULL); 2003 result->tv_sec = r.tv_sec; 2004 result->tv_usec = r.tv_usec; 2005 #else 2006 FILETIME ft; 2007 guint64 time64; 2008 2009 g_return_if_fail (result != NULL); 2010 2011 GetSystemTimeAsFileTime (&ft); 2012 memmove (&time64, &ft, sizeof (FILETIME)); 2013 2014 /* Convert from 100s of nanoseconds since 1601-01-01 2015 * to Unix epoch. Yes, this is Y2038 unsafe. 2016 */ 2017 time64 -= G_GINT64_CONSTANT (116444736000000000); 2018 time64 /= 10; 2019 2020 result->tv_sec = time64 / 1000000; 2021 result->tv_usec = time64 % 1000000; 2022 #endif 2023 } 2024 2025 /** 2026 * g_get_real_time: 2027 * 2028 * Queries the system wall-clock time. 2029 * 2030 * This call is functionally equivalent to g_get_current_time() except 2031 * that the return value is often more convenient than dealing with a 2032 * #GTimeVal. 2033 * 2034 * You should only use this call if you are actually interested in the real 2035 * wall-clock time. g_get_monotonic_time() is probably more useful for 2036 * measuring intervals. 2037 * 2038 * Returns: the number of microseconds since January 1, 1970 UTC. 2039 * 2040 * Since: 2.28 2041 **/ 2042 gint64 2043 g_get_real_time (void) 2044 { 2045 GTimeVal tv; 2046 2047 g_get_current_time (&tv); 2048 2049 return (((gint64) tv.tv_sec) * 1000000) + tv.tv_usec; 2050 } 2051 2052 /** 2053 * g_get_monotonic_time: 2054 * 2055 * Queries the system monotonic time, if available. 2056 * 2057 * On POSIX systems with clock_gettime() and %CLOCK_MONOTONIC this call 2058 * is a very shallow wrapper for that. Otherwise, we make a best effort 2059 * that probably involves returning the wall clock time (with at least 2060 * microsecond accuracy, subject to the limitations of the OS kernel). 2061 * 2062 * Note that, on Windows, "limitations of the OS kernel" is a rather 2063 * substantial statement. Depending on the configuration of the system, 2064 * the wall clock time is updated as infrequently as 64 times a second 2065 * (which is approximately every 16ms). 2066 * 2067 * Returns: the monotonic time, in microseconds 2068 * 2069 * Since: 2.28 2070 **/ 2071 gint64 2072 g_get_monotonic_time (void) 2073 { 2074 #ifdef HAVE_CLOCK_GETTIME 2075 /* librt clock_gettime() is our first choice */ 2076 { 2077 static int clockid = CLOCK_REALTIME; 2078 struct timespec ts; 2079 2080 #ifdef HAVE_MONOTONIC_CLOCK 2081 /* We have to check if we actually have monotonic clock support. 2082 * 2083 * There is no thread safety issue here since there is no harm if we 2084 * check twice. 2085 */ 2086 { 2087 static gboolean checked; 2088 2089 if G_UNLIKELY (!checked) 2090 { 2091 if (sysconf (_SC_MONOTONIC_CLOCK) >= 0) 2092 clockid = CLOCK_MONOTONIC; 2093 checked = TRUE; 2094 } 2095 } 2096 #endif 2097 2098 clock_gettime (clockid, &ts); 2099 2100 /* In theory monotonic time can have any epoch. 2101 * 2102 * glib presently assumes the following: 2103 * 2104 * 1) The epoch comes some time after the birth of Jesus of Nazareth, but 2105 * not more than 10000 years later. 2106 * 2107 * 2) The current time also falls sometime within this range. 2108 * 2109 * These two reasonable assumptions leave us with a maximum deviation from 2110 * the epoch of 10000 years, or 315569520000000000 seconds. 2111 * 2112 * If we restrict ourselves to this range then the number of microseconds 2113 * will always fit well inside the constraints of a int64 (by a factor of 2114 * about 29). 2115 * 2116 * If you actually hit the following assertion, probably you should file a 2117 * bug against your operating system for being excessively silly. 2118 **/ 2119 g_assert (G_GINT64_CONSTANT (-315569520000000000) < ts.tv_sec && 2120 ts.tv_sec < G_GINT64_CONSTANT (315569520000000000)); 2121 2122 return (((gint64) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); 2123 } 2124 #else 2125 /* It may look like we are discarding accuracy on Windows (since its 2126 * current time is expressed in 100s of nanoseconds) but according to 2127 * many sources, the time is only updated 64 times per second, so 2128 * microsecond accuracy is more than enough. 2129 */ 2130 { 2131 GTimeVal tv; 2132 2133 g_get_current_time (&tv); 2134 2135 return (((gint64) tv.tv_sec) * 1000000) + tv.tv_usec; 2136 } 2137 #endif 2138 } 2139 2140 static void 2141 g_main_dispatch_free (gpointer dispatch) 2142 { 2143 g_slice_free (GMainDispatch, dispatch); 2144 } 2145 2146 /* Running the main loop */ 2147 2148 static GMainDispatch * 2149 get_dispatch (void) 2150 { 2151 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT; 2152 GMainDispatch *dispatch = g_static_private_get (&depth_private); 2153 if (!dispatch) 2154 { 2155 dispatch = g_slice_new0 (GMainDispatch); 2156 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free); 2157 } 2158 2159 return dispatch; 2160 } 2161 2162 /** 2163 * g_main_depth: 2164 * 2165 * Returns the depth of the stack of calls to 2166 * g_main_context_dispatch() on any #GMainContext in the current thread. 2167 * That is, when called from the toplevel, it gives 0. When 2168 * called from within a callback from g_main_context_iteration() 2169 * (or g_main_loop_run(), etc.) it returns 1. When called from within 2170 * a callback to a recursive call to g_main_context_iteration(), 2171 * it returns 2. And so forth. 2172 * 2173 * This function is useful in a situation like the following: 2174 * Imagine an extremely simple "garbage collected" system. 2175 * 2176 * |[ 2177 * static GList *free_list; 2178 * 2179 * gpointer 2180 * allocate_memory (gsize size) 2181 * { 2182 * gpointer result = g_malloc (size); 2183 * free_list = g_list_prepend (free_list, result); 2184 * return result; 2185 * } 2186 * 2187 * void 2188 * free_allocated_memory (void) 2189 * { 2190 * GList *l; 2191 * for (l = free_list; l; l = l->next); 2192 * g_free (l->data); 2193 * g_list_free (free_list); 2194 * free_list = NULL; 2195 * } 2196 * 2197 * [...] 2198 * 2199 * while (TRUE); 2200 * { 2201 * g_main_context_iteration (NULL, TRUE); 2202 * free_allocated_memory(); 2203 * } 2204 * ]| 2205 * 2206 * This works from an application, however, if you want to do the same 2207 * thing from a library, it gets more difficult, since you no longer 2208 * control the main loop. You might think you can simply use an idle 2209 * function to make the call to free_allocated_memory(), but that 2210 * doesn't work, since the idle function could be called from a 2211 * recursive callback. This can be fixed by using g_main_depth() 2212 * 2213 * |[ 2214 * gpointer 2215 * allocate_memory (gsize size) 2216 * { 2217 * FreeListBlock *block = g_new (FreeListBlock, 1); 2218 * block->mem = g_malloc (size); 2219 * block->depth = g_main_depth (); 2220 * free_list = g_list_prepend (free_list, block); 2221 * return block->mem; 2222 * } 2223 * 2224 * void 2225 * free_allocated_memory (void) 2226 * { 2227 * GList *l; 2228 * 2229 * int depth = g_main_depth (); 2230 * for (l = free_list; l; ); 2231 * { 2232 * GList *next = l->next; 2233 * FreeListBlock *block = l->data; 2234 * if (block->depth > depth) 2235 * { 2236 * g_free (block->mem); 2237 * g_free (block); 2238 * free_list = g_list_delete_link (free_list, l); 2239 * } 2240 * 2241 * l = next; 2242 * } 2243 * } 2244 * ]| 2245 * 2246 * There is a temptation to use g_main_depth() to solve 2247 * problems with reentrancy. For instance, while waiting for data 2248 * to be received from the network in response to a menu item, 2249 * the menu item might be selected again. It might seem that 2250 * one could make the menu item's callback return immediately 2251 * and do nothing if g_main_depth() returns a value greater than 1. 2252 * However, this should be avoided since the user then sees selecting 2253 * the menu item do nothing. Furthermore, you'll find yourself adding 2254 * these checks all over your code, since there are doubtless many, 2255 * many things that the user could do. Instead, you can use the 2256 * following techniques: 2257 * 2258 * <orderedlist> 2259 * <listitem> 2260 * <para> 2261 * Use gtk_widget_set_sensitive() or modal dialogs to prevent 2262 * the user from interacting with elements while the main 2263 * loop is recursing. 2264 * </para> 2265 * </listitem> 2266 * <listitem> 2267 * <para> 2268 * Avoid main loop recursion in situations where you can't handle 2269 * arbitrary callbacks. Instead, structure your code so that you 2270 * simply return to the main loop and then get called again when 2271 * there is more work to do. 2272 * </para> 2273 * </listitem> 2274 * </orderedlist> 2275 * 2276 * Return value: The main loop recursion level in the current thread 2277 **/ 2278 int 2279 g_main_depth (void) 2280 { 2281 GMainDispatch *dispatch = get_dispatch (); 2282 return dispatch->depth; 2283 } 2284 2285 /** 2286 * g_main_current_source: 2287 * 2288 * Returns the currently firing source for this thread. 2289 * 2290 * Return value: The currently firing source or %NULL. 2291 * 2292 * Since: 2.12 2293 */ 2294 GSource * 2295 g_main_current_source (void) 2296 { 2297 GMainDispatch *dispatch = get_dispatch (); 2298 return dispatch->dispatching_sources ? dispatch->dispatching_sources->data : NULL; 2299 } 2300 2301 /** 2302 * g_source_is_destroyed: 2303 * @source: a #GSource 2304 * 2305 * Returns whether @source has been destroyed. 2306 * 2307 * This is important when you operate upon your objects 2308 * from within idle handlers, but may have freed the object 2309 * before the dispatch of your idle handler. 2310 * 2311 * |[ 2312 * static gboolean 2313 * idle_callback (gpointer data) 2314 * { 2315 * SomeWidget *self = data; 2316 * 2317 * GDK_THREADS_ENTER (<!-- -->); 2318 * /<!-- -->* do stuff with self *<!-- -->/ 2319 * self->idle_id = 0; 2320 * GDK_THREADS_LEAVE (<!-- -->); 2321 * 2322 * return FALSE; 2323 * } 2324 * 2325 * static void 2326 * some_widget_do_stuff_later (SomeWidget *self) 2327 * { 2328 * self->idle_id = g_idle_add (idle_callback, self); 2329 * } 2330 * 2331 * static void 2332 * some_widget_finalize (GObject *object) 2333 * { 2334 * SomeWidget *self = SOME_WIDGET (object); 2335 * 2336 * if (self->idle_id) 2337 * g_source_remove (self->idle_id); 2338 * 2339 * G_OBJECT_CLASS (parent_class)->finalize (object); 2340 * } 2341 * ]| 2342 * 2343 * This will fail in a multi-threaded application if the 2344 * widget is destroyed before the idle handler fires due 2345 * to the use after free in the callback. A solution, to 2346 * this particular problem, is to check to if the source 2347 * has already been destroy within the callback. 2348 * 2349 * |[ 2350 * static gboolean 2351 * idle_callback (gpointer data) 2352 * { 2353 * SomeWidget *self = data; 2354 * 2355 * GDK_THREADS_ENTER (); 2356 * if (!g_source_is_destroyed (g_main_current_source ())) 2357 * { 2358 * /<!-- -->* do stuff with self *<!-- -->/ 2359 * } 2360 * GDK_THREADS_LEAVE (); 2361 * 2362 * return FALSE; 2363 * } 2364 * ]| 2365 * 2366 * Return value: %TRUE if the source has been destroyed 2367 * 2368 * Since: 2.12 2369 */ 2370 gboolean 2371 g_source_is_destroyed (GSource *source) 2372 { 2373 return SOURCE_DESTROYED (source); 2374 } 2375 2376 /* Temporarily remove all this source's file descriptors from the 2377 * poll(), so that if data comes available for one of the file descriptors 2378 * we don't continually spin in the poll() 2379 */ 2380 /* HOLDS: source->context's lock */ 2381 static void 2382 block_source (GSource *source) 2383 { 2384 GSList *tmp_list; 2385 2386 g_return_if_fail (!SOURCE_BLOCKED (source)); 2387 2388 tmp_list = source->poll_fds; 2389 while (tmp_list) 2390 { 2391 g_main_context_remove_poll_unlocked (source->context, tmp_list->data); 2392 tmp_list = tmp_list->next; 2393 } 2394 } 2395 2396 /* HOLDS: source->context's lock */ 2397 static void 2398 unblock_source (GSource *source) 2399 { 2400 GSList *tmp_list; 2401 2402 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */ 2403 g_return_if_fail (!SOURCE_DESTROYED (source)); 2404 2405 tmp_list = source->poll_fds; 2406 while (tmp_list) 2407 { 2408 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data); 2409 tmp_list = tmp_list->next; 2410 } 2411 } 2412 2413 /* HOLDS: context's lock */ 2414 static void 2415 g_main_dispatch (GMainContext *context) 2416 { 2417 GMainDispatch *current = get_dispatch (); 2418 guint i; 2419 2420 for (i = 0; i < context->pending_dispatches->len; i++) 2421 { 2422 GSource *source = context->pending_dispatches->pdata[i]; 2423 2424 context->pending_dispatches->pdata[i] = NULL; 2425 g_assert (source); 2426 2427 source->flags &= ~G_SOURCE_READY; 2428 2429 if (!SOURCE_DESTROYED (source)) 2430 { 2431 gboolean was_in_call; 2432 gpointer user_data = NULL; 2433 GSourceFunc callback = NULL; 2434 GSourceCallbackFuncs *cb_funcs; 2435 gpointer cb_data; 2436 gboolean need_destroy; 2437 2438 gboolean (*dispatch) (GSource *, 2439 GSourceFunc, 2440 gpointer); 2441 GSList current_source_link; 2442 2443 dispatch = source->source_funcs->dispatch; 2444 cb_funcs = source->callback_funcs; 2445 cb_data = source->callback_data; 2446 2447 if (cb_funcs) 2448 cb_funcs->ref (cb_data); 2449 2450 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0) 2451 block_source (source); 2452 2453 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL; 2454 source->flags |= G_HOOK_FLAG_IN_CALL; 2455 2456 if (cb_funcs) 2457 cb_funcs->get (cb_data, source, &callback, &user_data); 2458 2459 UNLOCK_CONTEXT (context); 2460 2461 current->depth++; 2462 /* The on-stack allocation of the GSList is unconventional, but 2463 * we know that the lifetime of the link is bounded to this 2464 * function as the link is kept in a thread specific list and 2465 * not manipulated outside of this function and its descendants. 2466 * Avoiding the overhead of a g_slist_alloc() is useful as many 2467 * applications do little more than dispatch events. 2468 * 2469 * This is a performance hack - do not revert to g_slist_prepend()! 2470 */ 2471 current_source_link.data = source; 2472 current_source_link.next = current->dispatching_sources; 2473 current->dispatching_sources = ¤t_source_link; 2474 need_destroy = ! dispatch (source, 2475 callback, 2476 user_data); 2477 g_assert (current->dispatching_sources == ¤t_source_link); 2478 current->dispatching_sources = current_source_link.next; 2479 current->depth--; 2480 2481 if (cb_funcs) 2482 cb_funcs->unref (cb_data); 2483 2484 LOCK_CONTEXT (context); 2485 2486 if (!was_in_call) 2487 source->flags &= ~G_HOOK_FLAG_IN_CALL; 2488 2489 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 && 2490 !SOURCE_DESTROYED (source)) 2491 unblock_source (source); 2492 2493 /* Note: this depends on the fact that we can't switch 2494 * sources from one main context to another 2495 */ 2496 if (need_destroy && !SOURCE_DESTROYED (source)) 2497 { 2498 g_assert (source->context == context); 2499 g_source_destroy_internal (source, context, TRUE); 2500 } 2501 } 2502 2503 SOURCE_UNREF (source, context); 2504 } 2505 2506 g_ptr_array_set_size (context->pending_dispatches, 0); 2507 } 2508 2509 /* Holds context's lock */ 2510 static inline GSource * 2511 next_valid_source (GMainContext *context, 2512 GSource *source) 2513 { 2514 GSource *new_source = source ? source->next : context->source_list; 2515 2516 while (new_source) 2517 { 2518 if (!SOURCE_DESTROYED (new_source)) 2519 { 2520 new_source->ref_count++; 2521 break; 2522 } 2523 2524 new_source = new_source->next; 2525 } 2526 2527 if (source) 2528 SOURCE_UNREF (source, context); 2529 2530 return new_source; 2531 } 2532 2533 /** 2534 * g_main_context_acquire: 2535 * @context: a #GMainContext 2536 * 2537 * Tries to become the owner of the specified context. 2538 * If some other thread is the owner of the context, 2539 * returns %FALSE immediately. Ownership is properly 2540 * recursive: the owner can require ownership again 2541 * and will release ownership when g_main_context_release() 2542 * is called as many times as g_main_context_acquire(). 2543 * 2544 * You must be the owner of a context before you 2545 * can call g_main_context_prepare(), g_main_context_query(), 2546 * g_main_context_check(), g_main_context_dispatch(). 2547 * 2548 * Return value: %TRUE if the operation succeeded, and 2549 * this thread is now the owner of @context. 2550 **/ 2551 gboolean 2552 g_main_context_acquire (GMainContext *context) 2553 { 2554 #ifdef G_THREADS_ENABLED 2555 gboolean result = FALSE; 2556 GThread *self = G_THREAD_SELF; 2557 2558 if (context == NULL) 2559 context = g_main_context_default (); 2560 2561 LOCK_CONTEXT (context); 2562 2563 if (!context->owner) 2564 { 2565 context->owner = self; 2566 g_assert (context->owner_count == 0); 2567 } 2568 2569 if (context->owner == self) 2570 { 2571 context->owner_count++; 2572 result = TRUE; 2573 } 2574 2575 UNLOCK_CONTEXT (context); 2576 2577 return result; 2578 #else /* !G_THREADS_ENABLED */ 2579 return TRUE; 2580 #endif /* G_THREADS_ENABLED */ 2581 } 2582 2583 /** 2584 * g_main_context_release: 2585 * @context: a #GMainContext 2586 * 2587 * Releases ownership of a context previously acquired by this thread 2588 * with g_main_context_acquire(). If the context was acquired multiple 2589 * times, the ownership will be released only when g_main_context_release() 2590 * is called as many times as it was acquired. 2591 **/ 2592 void 2593 g_main_context_release (GMainContext *context) 2594 { 2595 #ifdef G_THREADS_ENABLED 2596 if (context == NULL) 2597 context = g_main_context_default (); 2598 2599 LOCK_CONTEXT (context); 2600 2601 context->owner_count--; 2602 if (context->owner_count == 0) 2603 { 2604 context->owner = NULL; 2605 2606 if (context->waiters) 2607 { 2608 GMainWaiter *waiter = context->waiters->data; 2609 gboolean loop_internal_waiter = 2610 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex)); 2611 context->waiters = g_slist_delete_link (context->waiters, 2612 context->waiters); 2613 if (!loop_internal_waiter) 2614 g_mutex_lock (waiter->mutex); 2615 2616 g_cond_signal (waiter->cond); 2617 2618 if (!loop_internal_waiter) 2619 g_mutex_unlock (waiter->mutex); 2620 } 2621 } 2622 2623 UNLOCK_CONTEXT (context); 2624 #endif /* G_THREADS_ENABLED */ 2625 } 2626 2627 /** 2628 * g_main_context_wait: 2629 * @context: a #GMainContext 2630 * @cond: a condition variable 2631 * @mutex: a mutex, currently held 2632 * 2633 * Tries to become the owner of the specified context, 2634 * as with g_main_context_acquire(). But if another thread 2635 * is the owner, atomically drop @mutex and wait on @cond until 2636 * that owner releases ownership or until @cond is signaled, then 2637 * try again (once) to become the owner. 2638 * 2639 * Return value: %TRUE if the operation succeeded, and 2640 * this thread is now the owner of @context. 2641 **/ 2642 gboolean 2643 g_main_context_wait (GMainContext *context, 2644 GCond *cond, 2645 GMutex *mutex) 2646 { 2647 #ifdef G_THREADS_ENABLED 2648 gboolean result = FALSE; 2649 GThread *self = G_THREAD_SELF; 2650 gboolean loop_internal_waiter; 2651 2652 if (context == NULL) 2653 context = g_main_context_default (); 2654 2655 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex)); 2656 2657 if (!loop_internal_waiter) 2658 LOCK_CONTEXT (context); 2659 2660 if (context->owner && context->owner != self) 2661 { 2662 GMainWaiter waiter; 2663 2664 waiter.cond = cond; 2665 waiter.mutex = mutex; 2666 2667 context->waiters = g_slist_append (context->waiters, &waiter); 2668 2669 if (!loop_internal_waiter) 2670 UNLOCK_CONTEXT (context); 2671 g_cond_wait (cond, mutex); 2672 if (!loop_internal_waiter) 2673 LOCK_CONTEXT (context); 2674 2675 context->waiters = g_slist_remove (context->waiters, &waiter); 2676 } 2677 2678 if (!context->owner) 2679 { 2680 context->owner = self; 2681 g_assert (context->owner_count == 0); 2682 } 2683 2684 if (context->owner == self) 2685 { 2686 context->owner_count++; 2687 result = TRUE; 2688 } 2689 2690 if (!loop_internal_waiter) 2691 UNLOCK_CONTEXT (context); 2692 2693 return result; 2694 #else /* !G_THREADS_ENABLED */ 2695 return TRUE; 2696 #endif /* G_THREADS_ENABLED */ 2697 } 2698 2699 /** 2700 * g_main_context_prepare: 2701 * @context: a #GMainContext 2702 * @priority: location to store priority of highest priority 2703 * source already ready. 2704 * 2705 * Prepares to poll sources within a main loop. The resulting information 2706 * for polling is determined by calling g_main_context_query (). 2707 * 2708 * Return value: %TRUE if some source is ready to be dispatched 2709 * prior to polling. 2710 **/ 2711 gboolean 2712 g_main_context_prepare (GMainContext *context, 2713 gint *priority) 2714 { 2715 gint i; 2716 gint n_ready = 0; 2717 gint current_priority = G_MAXINT; 2718 GSource *source; 2719 2720 if (context == NULL) 2721 context = g_main_context_default (); 2722 2723 LOCK_CONTEXT (context); 2724 2725 context->time_is_fresh = FALSE; 2726 context->real_time_is_fresh = FALSE; 2727 2728 if (context->in_check_or_prepare) 2729 { 2730 g_warning ("g_main_context_prepare() called recursively from within a source's check() or " 2731 "prepare() member."); 2732 UNLOCK_CONTEXT (context); 2733 return FALSE; 2734 } 2735 2736 #ifdef G_THREADS_ENABLED 2737 if (context->poll_waiting) 2738 { 2739 g_warning("g_main_context_prepare(): main loop already active in another thread"); 2740 UNLOCK_CONTEXT (context); 2741 return FALSE; 2742 } 2743 2744 context->poll_waiting = TRUE; 2745 #endif /* G_THREADS_ENABLED */ 2746 2747 #if 0 2748 /* If recursing, finish up current dispatch, before starting over */ 2749 if (context->pending_dispatches) 2750 { 2751 if (dispatch) 2752 g_main_dispatch (context, ¤t_time); 2753 2754 UNLOCK_CONTEXT (context); 2755 return TRUE; 2756 } 2757 #endif 2758 2759 /* If recursing, clear list of pending dispatches */ 2760 2761 for (i = 0; i < context->pending_dispatches->len; i++) 2762 { 2763 if (context->pending_dispatches->pdata[i]) 2764 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context); 2765 } 2766 g_ptr_array_set_size (context->pending_dispatches, 0); 2767 2768 /* Prepare all sources */ 2769 2770 context->timeout = -1; 2771 2772 source = next_valid_source (context, NULL); 2773 while (source) 2774 { 2775 gint source_timeout = -1; 2776 2777 if ((n_ready > 0) && (source->priority > current_priority)) 2778 { 2779 SOURCE_UNREF (source, context); 2780 break; 2781 } 2782 if (SOURCE_BLOCKED (source)) 2783 goto next; 2784 2785 if (!(source->flags & G_SOURCE_READY)) 2786 { 2787 gboolean result; 2788 gboolean (*prepare) (GSource *source, 2789 gint *timeout); 2790 2791 prepare = source->source_funcs->prepare; 2792 context->in_check_or_prepare++; 2793 UNLOCK_CONTEXT (context); 2794 2795 result = (*prepare) (source, &source_timeout); 2796 2797 LOCK_CONTEXT (context); 2798 context->in_check_or_prepare--; 2799 2800 if (result) 2801 { 2802 GSource *ready_source = source; 2803 2804 while (ready_source) 2805 { 2806 ready_source->flags |= G_SOURCE_READY; 2807 ready_source = ready_source->priv ? ready_source->priv->parent_source : NULL; 2808 } 2809 } 2810 } 2811 2812 if (source->flags & G_SOURCE_READY) 2813 { 2814 n_ready++; 2815 current_priority = source->priority; 2816 context->timeout = 0; 2817 } 2818 2819 if (source_timeout >= 0) 2820 { 2821 if (context->timeout < 0) 2822 context->timeout = source_timeout; 2823 else 2824 context->timeout = MIN (context->timeout, source_timeout); 2825 } 2826 2827 next: 2828 source = next_valid_source (context, source); 2829 } 2830 2831 UNLOCK_CONTEXT (context); 2832 2833 if (priority) 2834 *priority = current_priority; 2835 2836 return (n_ready > 0); 2837 } 2838 2839 /** 2840 * g_main_context_query: 2841 * @context: a #GMainContext 2842 * @max_priority: maximum priority source to check 2843 * @timeout_: location to store timeout to be used in polling 2844 * @fds: location to store #GPollFD records that need to be polled. 2845 * @n_fds: length of @fds. 2846 * 2847 * Determines information necessary to poll this main loop. 2848 * 2849 * Return value: the number of records actually stored in @fds, 2850 * or, if more than @n_fds records need to be stored, the number 2851 * of records that need to be stored. 2852 **/ 2853 gint 2854 g_main_context_query (GMainContext *context, 2855 gint max_priority, 2856 gint *timeout, 2857 GPollFD *fds, 2858 gint n_fds) 2859 { 2860 gint n_poll; 2861 GPollRec *pollrec; 2862 2863 LOCK_CONTEXT (context); 2864 2865 pollrec = context->poll_records; 2866 n_poll = 0; 2867 while (pollrec && max_priority >= pollrec->priority) 2868 { 2869 /* We need to include entries with fd->events == 0 in the array because 2870 * otherwise if the application changes fd->events behind our back and 2871 * makes it non-zero, we'll be out of sync when we check the fds[] array. 2872 * (Changing fd->events after adding an FD wasn't an anticipated use of 2873 * this API, but it occurs in practice.) */ 2874 if (n_poll < n_fds) 2875 { 2876 fds[n_poll].fd = pollrec->fd->fd; 2877 /* In direct contradiction to the Unix98 spec, IRIX runs into 2878 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL 2879 * flags in the events field of the pollfd while it should 2880 * just ignoring them. So we mask them out here. 2881 */ 2882 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL); 2883 fds[n_poll].revents = 0; 2884 } 2885 2886 pollrec = pollrec->next; 2887 n_poll++; 2888 } 2889 2890 #ifdef G_THREADS_ENABLED 2891 context->poll_changed = FALSE; 2892 #endif 2893 2894 if (timeout) 2895 { 2896 *timeout = context->timeout; 2897 if (*timeout != 0) 2898 { 2899 context->time_is_fresh = FALSE; 2900 context->real_time_is_fresh = FALSE; 2901 } 2902 } 2903 2904 UNLOCK_CONTEXT (context); 2905 2906 return n_poll; 2907 } 2908 2909 /** 2910 * g_main_context_check: 2911 * @context: a #GMainContext 2912 * @max_priority: the maximum numerical priority of sources to check 2913 * @fds: array of #GPollFD's that was passed to the last call to 2914 * g_main_context_query() 2915 * @n_fds: return value of g_main_context_query() 2916 * 2917 * Passes the results of polling back to the main loop. 2918 * 2919 * Return value: %TRUE if some sources are ready to be dispatched. 2920 **/ 2921 gboolean 2922 g_main_context_check (GMainContext *context, 2923 gint max_priority, 2924 GPollFD *fds, 2925 gint n_fds) 2926 { 2927 GSource *source; 2928 GPollRec *pollrec; 2929 gint n_ready = 0; 2930 gint i; 2931 2932 LOCK_CONTEXT (context); 2933 2934 if (context->in_check_or_prepare) 2935 { 2936 g_warning ("g_main_context_check() called recursively from within a source's check() or " 2937 "prepare() member."); 2938 UNLOCK_CONTEXT (context); 2939 return FALSE; 2940 } 2941 2942 #ifdef G_THREADS_ENABLED 2943 if (!context->poll_waiting) 2944 { 2945 #ifndef G_OS_WIN32 2946 gchar a; 2947 read (context->wake_up_pipe[0], &a, 1); 2948 #endif 2949 } 2950 else 2951 context->poll_waiting = FALSE; 2952 2953 /* If the set of poll file descriptors changed, bail out 2954 * and let the main loop rerun 2955 */ 2956 if (context->poll_changed) 2957 { 2958 UNLOCK_CONTEXT (context); 2959 return FALSE; 2960 } 2961 #endif /* G_THREADS_ENABLED */ 2962 2963 pollrec = context->poll_records; 2964 i = 0; 2965 while (i < n_fds) 2966 { 2967 if (pollrec->fd->events) 2968 pollrec->fd->revents = fds[i].revents; 2969 2970 pollrec = pollrec->next; 2971 i++; 2972 } 2973 2974 source = next_valid_source (context, NULL); 2975 while (source) 2976 { 2977 if ((n_ready > 0) && (source->priority > max_priority)) 2978 { 2979 SOURCE_UNREF (source, context); 2980 break; 2981 } 2982 if (SOURCE_BLOCKED (source)) 2983 goto next; 2984 2985 if (!(source->flags & G_SOURCE_READY)) 2986 { 2987 gboolean result; 2988 gboolean (*check) (GSource *source); 2989 2990 check = source->source_funcs->check; 2991 2992 context->in_check_or_prepare++; 2993 UNLOCK_CONTEXT (context); 2994 2995 result = (*check) (source); 2996 2997 LOCK_CONTEXT (context); 2998 context->in_check_or_prepare--; 2999 3000 if (result) 3001 { 3002 GSource *ready_source = source; 3003 3004 while (ready_source) 3005 { 3006 ready_source->flags |= G_SOURCE_READY; 3007 ready_source = ready_source->priv ? ready_source->priv->parent_source : NULL; 3008 } 3009 } 3010 } 3011 3012 if (source->flags & G_SOURCE_READY) 3013 { 3014 source->ref_count++; 3015 g_ptr_array_add (context->pending_dispatches, source); 3016 3017 n_ready++; 3018 3019 /* never dispatch sources with less priority than the first 3020 * one we choose to dispatch 3021 */ 3022 max_priority = source->priority; 3023 } 3024 3025 next: 3026 source = next_valid_source (context, source); 3027 } 3028 3029 UNLOCK_CONTEXT (context); 3030 3031 return n_ready > 0; 3032 } 3033 3034 /** 3035 * g_main_context_dispatch: 3036 * @context: a #GMainContext 3037 * 3038 * Dispatches all pending sources. 3039 **/ 3040 void 3041 g_main_context_dispatch (GMainContext *context) 3042 { 3043 LOCK_CONTEXT (context); 3044 3045 if (context->pending_dispatches->len > 0) 3046 { 3047 g_main_dispatch (context); 3048 } 3049 3050 UNLOCK_CONTEXT (context); 3051 } 3052 3053 /* HOLDS context lock */ 3054 static gboolean 3055 g_main_context_iterate (GMainContext *context, 3056 gboolean block, 3057 gboolean dispatch, 3058 GThread *self) 3059 { 3060 gint max_priority; 3061 gint timeout; 3062 gboolean some_ready; 3063 gint nfds, allocated_nfds; 3064 GPollFD *fds = NULL; 3065 3066 UNLOCK_CONTEXT (context); 3067 3068 #ifdef G_THREADS_ENABLED 3069 if (!g_main_context_acquire (context)) 3070 { 3071 gboolean got_ownership; 3072 3073 LOCK_CONTEXT (context); 3074 3075 g_return_val_if_fail (g_thread_supported (), FALSE); 3076 3077 if (!block) 3078 return FALSE; 3079 3080 if (!context->cond) 3081 context->cond = g_cond_new (); 3082 3083 got_ownership = g_main_context_wait (context, 3084 context->cond, 3085 g_static_mutex_get_mutex (&context->mutex)); 3086 3087 if (!got_ownership) 3088 return FALSE; 3089 } 3090 else 3091 LOCK_CONTEXT (context); 3092 #endif /* G_THREADS_ENABLED */ 3093 3094 if (!context->cached_poll_array) 3095 { 3096 context->cached_poll_array_size = context->n_poll_records; 3097 context->cached_poll_array = g_new (GPollFD, context->n_poll_records); 3098 } 3099 3100 allocated_nfds = context->cached_poll_array_size; 3101 fds = context->cached_poll_array; 3102 3103 UNLOCK_CONTEXT (context); 3104 3105 g_main_context_prepare (context, &max_priority); 3106 3107 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds, 3108 allocated_nfds)) > allocated_nfds) 3109 { 3110 LOCK_CONTEXT (context); 3111 g_free (fds); 3112 context->cached_poll_array_size = allocated_nfds = nfds; 3113 context->cached_poll_array = fds = g_new (GPollFD, nfds); 3114 UNLOCK_CONTEXT (context); 3115 } 3116 3117 if (!block) 3118 timeout = 0; 3119 3120 g_main_context_poll (context, timeout, max_priority, fds, nfds); 3121 3122 some_ready = g_main_context_check (context, max_priority, fds, nfds); 3123 3124 if (dispatch) 3125 g_main_context_dispatch (context); 3126 3127 #ifdef G_THREADS_ENABLED 3128 g_main_context_release (context); 3129 #endif /* G_THREADS_ENABLED */ 3130 3131 LOCK_CONTEXT (context); 3132 3133 return some_ready; 3134 } 3135 3136 /** 3137 * g_main_context_pending: 3138 * @context: a #GMainContext (if %NULL, the default context will be used) 3139 * 3140 * Checks if any sources have pending events for the given context. 3141 * 3142 * Return value: %TRUE if events are pending. 3143 **/ 3144 gboolean 3145 g_main_context_pending (GMainContext *context) 3146 { 3147 gboolean retval; 3148 3149 if (!context) 3150 context = g_main_context_default(); 3151 3152 LOCK_CONTEXT (context); 3153 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF); 3154 UNLOCK_CONTEXT (context); 3155 3156 return retval; 3157 } 3158 3159 /** 3160 * g_main_context_iteration: 3161 * @context: a #GMainContext (if %NULL, the default context will be used) 3162 * @may_block: whether the call may block. 3163 * 3164 * Runs a single iteration for the given main loop. This involves 3165 * checking to see if any event sources are ready to be processed, 3166 * then if no events sources are ready and @may_block is %TRUE, waiting 3167 * for a source to become ready, then dispatching the highest priority 3168 * events sources that are ready. Otherwise, if @may_block is %FALSE 3169 * sources are not waited to become ready, only those highest priority 3170 * events sources will be dispatched (if any), that are ready at this 3171 * given moment without further waiting. 3172 * 3173 * Note that even when @may_block is %TRUE, it is still possible for 3174 * g_main_context_iteration() to return %FALSE, since the the wait may 3175 * be interrupted for other reasons than an event source becoming ready. 3176 * 3177 * Return value: %TRUE if events were dispatched. 3178 **/ 3179 gboolean 3180 g_main_context_iteration (GMainContext *context, gboolean may_block) 3181 { 3182 gboolean retval; 3183 3184 if (!context) 3185 context = g_main_context_default(); 3186 3187 LOCK_CONTEXT (context); 3188 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF); 3189 UNLOCK_CONTEXT (context); 3190 3191 return retval; 3192 } 3193 3194 /** 3195 * g_main_loop_new: 3196 * @context: a #GMainContext (if %NULL, the default context will be used). 3197 * @is_running: set to %TRUE to indicate that the loop is running. This 3198 * is not very important since calling g_main_loop_run() will set this to 3199 * %TRUE anyway. 3200 * 3201 * Creates a new #GMainLoop structure. 3202 * 3203 * Return value: a new #GMainLoop. 3204 **/ 3205 GMainLoop * 3206 g_main_loop_new (GMainContext *context, 3207 gboolean is_running) 3208 { 3209 GMainLoop *loop; 3210 3211 if (!context) 3212 context = g_main_context_default(); 3213 3214 g_main_context_ref (context); 3215 3216 loop = g_new0 (GMainLoop, 1); 3217 loop->context = context; 3218 loop->is_running = is_running != FALSE; 3219 loop->ref_count = 1; 3220 3221 return loop; 3222 } 3223 3224 /** 3225 * g_main_loop_ref: 3226 * @loop: a #GMainLoop 3227 * 3228 * Increases the reference count on a #GMainLoop object by one. 3229 * 3230 * Return value: @loop 3231 **/ 3232 GMainLoop * 3233 g_main_loop_ref (GMainLoop *loop) 3234 { 3235 g_return_val_if_fail (loop != NULL, NULL); 3236 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL); 3237 3238 g_atomic_int_inc (&loop->ref_count); 3239 3240 return loop; 3241 } 3242 3243 /** 3244 * g_main_loop_unref: 3245 * @loop: a #GMainLoop 3246 * 3247 * Decreases the reference count on a #GMainLoop object by one. If 3248 * the result is zero, free the loop and free all associated memory. 3249 **/ 3250 void 3251 g_main_loop_unref (GMainLoop *loop) 3252 { 3253 g_return_if_fail (loop != NULL); 3254 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0); 3255 3256 if (!g_atomic_int_dec_and_test (&loop->ref_count)) 3257 return; 3258 3259 g_main_context_unref (loop->context); 3260 g_free (loop); 3261 } 3262 3263 /** 3264 * g_main_loop_run: 3265 * @loop: a #GMainLoop 3266 * 3267 * Runs a main loop until g_main_loop_quit() is called on the loop. 3268 * If this is called for the thread of the loop's #GMainContext, 3269 * it will process events from the loop, otherwise it will 3270 * simply wait. 3271 **/ 3272 void 3273 g_main_loop_run (GMainLoop *loop) 3274 { 3275 GThread *self = G_THREAD_SELF; 3276 3277 g_return_if_fail (loop != NULL); 3278 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0); 3279 3280 #ifdef G_THREADS_ENABLED 3281 if (!g_main_context_acquire (loop->context)) 3282 { 3283 gboolean got_ownership = FALSE; 3284 3285 /* Another thread owns this context */ 3286 if (!g_thread_supported ()) 3287 { 3288 g_warning ("g_main_loop_run() was called from second thread but " 3289 "g_thread_init() was never called."); 3290 return; 3291 } 3292 3293 LOCK_CONTEXT (loop->context); 3294 3295 g_atomic_int_inc (&loop->ref_count); 3296 3297 if (!loop->is_running) 3298 loop->is_running = TRUE; 3299 3300 if (!loop->context->cond) 3301 loop->context->cond = g_cond_new (); 3302 3303 while (loop->is_running && !got_ownership) 3304 got_ownership = g_main_context_wait (loop->context, 3305 loop->context->cond, 3306 g_static_mutex_get_mutex (&loop->context->mutex)); 3307 3308 if (!loop->is_running) 3309 { 3310 UNLOCK_CONTEXT (loop->context); 3311 if (got_ownership) 3312 g_main_context_release (loop->context); 3313 g_main_loop_unref (loop); 3314 return; 3315 } 3316 3317 g_assert (got_ownership); 3318 } 3319 else 3320 LOCK_CONTEXT (loop->context); 3321 #endif /* G_THREADS_ENABLED */ 3322 3323 if (loop->context->in_check_or_prepare) 3324 { 3325 g_warning ("g_main_loop_run(): called recursively from within a source's " 3326 "check() or prepare() member, iteration not possible."); 3327 return; 3328 } 3329 3330 g_atomic_int_inc (&loop->ref_count); 3331 loop->is_running = TRUE; 3332 while (loop->is_running) 3333 g_main_context_iterate (loop->context, TRUE, TRUE, self); 3334 3335 UNLOCK_CONTEXT (loop->context); 3336 3337 #ifdef G_THREADS_ENABLED 3338 g_main_context_release (loop->context); 3339 #endif /* G_THREADS_ENABLED */ 3340 3341 g_main_loop_unref (loop); 3342 } 3343 3344 /** 3345 * g_main_loop_quit: 3346 * @loop: a #GMainLoop 3347 * 3348 * Stops a #GMainLoop from running. Any calls to g_main_loop_run() 3349 * for the loop will return. 3350 * 3351 * Note that sources that have already been dispatched when 3352 * g_main_loop_quit() is called will still be executed. 3353 **/ 3354 void 3355 g_main_loop_quit (GMainLoop *loop) 3356 { 3357 g_return_if_fail (loop != NULL); 3358 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0); 3359 3360 LOCK_CONTEXT (loop->context); 3361 loop->is_running = FALSE; 3362 g_main_context_wakeup_unlocked (loop->context); 3363 3364 #ifdef G_THREADS_ENABLED 3365 if (loop->context->cond) 3366 g_cond_broadcast (loop->context->cond); 3367 #endif /* G_THREADS_ENABLED */ 3368 3369 UNLOCK_CONTEXT (loop->context); 3370 } 3371 3372 /** 3373 * g_main_loop_is_running: 3374 * @loop: a #GMainLoop. 3375 * 3376 * Checks to see if the main loop is currently being run via g_main_loop_run(). 3377 * 3378 * Return value: %TRUE if the mainloop is currently being run. 3379 **/ 3380 gboolean 3381 g_main_loop_is_running (GMainLoop *loop) 3382 { 3383 g_return_val_if_fail (loop != NULL, FALSE); 3384 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE); 3385 3386 return loop->is_running; 3387 } 3388 3389 /** 3390 * g_main_loop_get_context: 3391 * @loop: a #GMainLoop. 3392 * 3393 * Returns the #GMainContext of @loop. 3394 * 3395 * Return value: the #GMainContext of @loop 3396 **/ 3397 GMainContext * 3398 g_main_loop_get_context (GMainLoop *loop) 3399 { 3400 g_return_val_if_fail (loop != NULL, NULL); 3401 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL); 3402 3403 return loop->context; 3404 } 3405 3406 /* HOLDS: context's lock */ 3407 static void 3408 g_main_context_poll (GMainContext *context, 3409 gint timeout, 3410 gint priority, 3411 GPollFD *fds, 3412 gint n_fds) 3413 { 3414 #ifdef G_MAIN_POLL_DEBUG 3415 GTimer *poll_timer; 3416 GPollRec *pollrec; 3417 gint i; 3418 #endif 3419 3420 GPollFunc poll_func; 3421 3422 if (n_fds || timeout != 0) 3423 { 3424 #ifdef G_MAIN_POLL_DEBUG 3425 if (_g_main_poll_debug) 3426 { 3427 g_print ("polling context=%p n=%d timeout=%d\n", 3428 context, n_fds, timeout); 3429 poll_timer = g_timer_new (); 3430 } 3431 #endif 3432 3433 LOCK_CONTEXT (context); 3434 3435 poll_func = context->poll_func; 3436 3437 UNLOCK_CONTEXT (context); 3438 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR) 3439 { 3440 #ifndef G_OS_WIN32 3441 g_warning ("poll(2) failed due to: %s.", 3442 g_strerror (errno)); 3443 #else 3444 /* If g_poll () returns -1, it has already called g_warning() */ 3445 #endif 3446 } 3447 3448 #ifdef G_MAIN_POLL_DEBUG 3449 if (_g_main_poll_debug) 3450 { 3451 LOCK_CONTEXT (context); 3452 3453 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds", 3454 n_fds, 3455 timeout, 3456 g_timer_elapsed (poll_timer, NULL)); 3457 g_timer_destroy (poll_timer); 3458 pollrec = context->poll_records; 3459 3460 while (pollrec != NULL) 3461 { 3462 i = 0; 3463 while (i < n_fds) 3464 { 3465 if (fds[i].fd == pollrec->fd->fd && 3466 pollrec->fd->events && 3467 fds[i].revents) 3468 { 3469 g_print (" [" G_POLLFD_FORMAT " :", fds[i].fd); 3470 if (fds[i].revents & G_IO_IN) 3471 g_print ("i"); 3472 if (fds[i].revents & G_IO_OUT) 3473 g_print ("o"); 3474 if (fds[i].revents & G_IO_PRI) 3475 g_print ("p"); 3476 if (fds[i].revents & G_IO_ERR) 3477 g_print ("e"); 3478 if (fds[i].revents & G_IO_HUP) 3479 g_print ("h"); 3480 if (fds[i].revents & G_IO_NVAL) 3481 g_print ("n"); 3482 g_print ("]"); 3483 } 3484 i++; 3485 } 3486 pollrec = pollrec->next; 3487 } 3488 g_print ("\n"); 3489 3490 UNLOCK_CONTEXT (context); 3491 } 3492 #endif 3493 } /* if (n_fds || timeout != 0) */ 3494 } 3495 3496 /** 3497 * g_main_context_add_poll: 3498 * @context: a #GMainContext (or %NULL for the default context) 3499 * @fd: a #GPollFD structure holding information about a file 3500 * descriptor to watch. 3501 * @priority: the priority for this file descriptor which should be 3502 * the same as the priority used for g_source_attach() to ensure that the 3503 * file descriptor is polled whenever the results may be needed. 3504 * 3505 * Adds a file descriptor to the set of file descriptors polled for 3506 * this context. This will very seldomly be used directly. Instead 3507 * a typical event source will use g_source_add_poll() instead. 3508 **/ 3509 void 3510 g_main_context_add_poll (GMainContext *context, 3511 GPollFD *fd, 3512 gint priority) 3513 { 3514 if (!context) 3515 context = g_main_context_default (); 3516 3517 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); 3518 g_return_if_fail (fd); 3519 3520 LOCK_CONTEXT (context); 3521 g_main_context_add_poll_unlocked (context, priority, fd); 3522 UNLOCK_CONTEXT (context); 3523 } 3524 3525 /* HOLDS: main_loop_lock */ 3526 static void 3527 g_main_context_add_poll_unlocked (GMainContext *context, 3528 gint priority, 3529 GPollFD *fd) 3530 { 3531 GPollRec *prevrec, *nextrec; 3532 GPollRec *newrec = g_slice_new (GPollRec); 3533 #ifdef GSTREAMER_LITE 3534 if (newrec == NULL) { 3535 return; 3536 } 3537 #endif // GSTREAMER_LITE 3538 3539 /* This file descriptor may be checked before we ever poll */ 3540 fd->revents = 0; 3541 newrec->fd = fd; 3542 newrec->priority = priority; 3543 3544 prevrec = context->poll_records_tail; 3545 nextrec = NULL; 3546 while (prevrec && priority < prevrec->priority) 3547 { 3548 nextrec = prevrec; 3549 prevrec = prevrec->prev; 3550 } 3551 3552 if (prevrec) 3553 prevrec->next = newrec; 3554 else 3555 context->poll_records = newrec; 3556 3557 newrec->prev = prevrec; 3558 newrec->next = nextrec; 3559 3560 if (nextrec) 3561 nextrec->prev = newrec; 3562 else 3563 context->poll_records_tail = newrec; 3564 3565 context->n_poll_records++; 3566 3567 #ifdef G_THREADS_ENABLED 3568 context->poll_changed = TRUE; 3569 3570 /* Now wake up the main loop if it is waiting in the poll() */ 3571 g_main_context_wakeup_unlocked (context); 3572 #endif 3573 } 3574 3575 /** 3576 * g_main_context_remove_poll: 3577 * @context:a #GMainContext 3578 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll() 3579 * 3580 * Removes file descriptor from the set of file descriptors to be 3581 * polled for a particular context. 3582 **/ 3583 void 3584 g_main_context_remove_poll (GMainContext *context, 3585 GPollFD *fd) 3586 { 3587 if (!context) 3588 context = g_main_context_default (); 3589 3590 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); 3591 g_return_if_fail (fd); 3592 3593 LOCK_CONTEXT (context); 3594 g_main_context_remove_poll_unlocked (context, fd); 3595 UNLOCK_CONTEXT (context); 3596 } 3597 3598 static void 3599 g_main_context_remove_poll_unlocked (GMainContext *context, 3600 GPollFD *fd) 3601 { 3602 GPollRec *pollrec, *prevrec, *nextrec; 3603 3604 prevrec = NULL; 3605 pollrec = context->poll_records; 3606 3607 while (pollrec) 3608 { 3609 nextrec = pollrec->next; 3610 if (pollrec->fd == fd) 3611 { 3612 if (prevrec != NULL) 3613 prevrec->next = nextrec; 3614 else 3615 context->poll_records = nextrec; 3616 3617 if (nextrec != NULL) 3618 nextrec->prev = prevrec; 3619 else 3620 context->poll_records_tail = prevrec; 3621 3622 g_slice_free (GPollRec, pollrec); 3623 3624 context->n_poll_records--; 3625 break; 3626 } 3627 prevrec = pollrec; 3628 pollrec = nextrec; 3629 } 3630 3631 #ifdef G_THREADS_ENABLED 3632 context->poll_changed = TRUE; 3633 3634 /* Now wake up the main loop if it is waiting in the poll() */ 3635 g_main_context_wakeup_unlocked (context); 3636 #endif 3637 } 3638 3639 /** 3640 * g_source_get_current_time: 3641 * @source: a #GSource 3642 * @timeval: #GTimeVal structure in which to store current time. 3643 * 3644 * Gets the "current time" to be used when checking 3645 * this source. The advantage of calling this function over 3646 * calling g_get_current_time() directly is that when 3647 * checking multiple sources, GLib can cache a single value 3648 * instead of having to repeatedly get the system time. 3649 * 3650 * Deprecated: 2.28: use g_source_get_time() instead 3651 **/ 3652 void 3653 g_source_get_current_time (GSource *source, 3654 GTimeVal *timeval) 3655 { 3656 GMainContext *context; 3657 3658 g_return_if_fail (source->context != NULL); 3659 3660 context = source->context; 3661 3662 LOCK_CONTEXT (context); 3663 3664 if (!context->real_time_is_fresh) 3665 { 3666 context->real_time = g_get_real_time (); 3667 context->real_time_is_fresh = TRUE; 3668 } 3669 3670 timeval->tv_sec = context->real_time / 1000000; 3671 timeval->tv_usec = context->real_time % 1000000; 3672 3673 UNLOCK_CONTEXT (context); 3674 } 3675 3676 /** 3677 * g_source_get_time: 3678 * @source: a #GSource 3679 * 3680 * Gets the time to be used when checking this source. The advantage of 3681 * calling this function over calling g_get_monotonic_time() directly is 3682 * that when checking multiple sources, GLib can cache a single value 3683 * instead of having to repeatedly get the system monotonic time. 3684 * 3685 * The time here is the system monotonic time, if available, or some 3686 * other reasonable alternative otherwise. See g_get_monotonic_time(). 3687 * 3688 * Returns: the monotonic time in microseconds 3689 * 3690 * Since: 2.28 3691 **/ 3692 gint64 3693 g_source_get_time (GSource *source) 3694 { 3695 GMainContext *context; 3696 gint64 result; 3697 3698 g_return_val_if_fail (source->context != NULL, 0); 3699 3700 context = source->context; 3701 3702 LOCK_CONTEXT (context); 3703 3704 if (!context->time_is_fresh) 3705 { 3706 context->time = g_get_monotonic_time (); 3707 context->time_is_fresh = TRUE; 3708 } 3709 3710 result = context->time; 3711 3712 UNLOCK_CONTEXT (context); 3713 3714 return result; 3715 } 3716 3717 /** 3718 * g_main_context_set_poll_func: 3719 * @context: a #GMainContext 3720 * @func: the function to call to poll all file descriptors 3721 * 3722 * Sets the function to use to handle polling of file descriptors. It 3723 * will be used instead of the poll() system call 3724 * (or GLib's replacement function, which is used where 3725 * poll() isn't available). 3726 * 3727 * This function could possibly be used to integrate the GLib event 3728 * loop with an external event loop. 3729 **/ 3730 void 3731 g_main_context_set_poll_func (GMainContext *context, 3732 GPollFunc func) 3733 { 3734 if (!context) 3735 context = g_main_context_default (); 3736 3737 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); 3738 3739 LOCK_CONTEXT (context); 3740 3741 if (func) 3742 context->poll_func = func; 3743 else 3744 context->poll_func = g_poll; 3745 3746 UNLOCK_CONTEXT (context); 3747 } 3748 3749 /** 3750 * g_main_context_get_poll_func: 3751 * @context: a #GMainContext 3752 * 3753 * Gets the poll function set by g_main_context_set_poll_func(). 3754 * 3755 * Return value: the poll function 3756 **/ 3757 GPollFunc 3758 g_main_context_get_poll_func (GMainContext *context) 3759 { 3760 GPollFunc result; 3761 3762 if (!context) 3763 context = g_main_context_default (); 3764 3765 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL); 3766 3767 LOCK_CONTEXT (context); 3768 result = context->poll_func; 3769 UNLOCK_CONTEXT (context); 3770 3771 return result; 3772 } 3773 3774 /* HOLDS: context's lock */ 3775 /* Wake the main loop up from a poll() */ 3776 static void 3777 g_main_context_wakeup_unlocked (GMainContext *context) 3778 { 3779 #ifdef G_THREADS_ENABLED 3780 if (g_thread_supported() && context->poll_waiting) 3781 { 3782 context->poll_waiting = FALSE; 3783 #ifndef G_OS_WIN32 3784 write (context->wake_up_pipe[1], "A", 1); 3785 #else 3786 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL); 3787 #endif 3788 } 3789 #endif 3790 } 3791 3792 /** 3793 * g_main_context_wakeup: 3794 * @context: a #GMainContext 3795 * 3796 * If @context is currently waiting in a poll(), interrupt 3797 * the poll(), and continue the iteration process. 3798 **/ 3799 void 3800 g_main_context_wakeup (GMainContext *context) 3801 { 3802 if (!context) 3803 context = g_main_context_default (); 3804 3805 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); 3806 3807 LOCK_CONTEXT (context); 3808 g_main_context_wakeup_unlocked (context); 3809 UNLOCK_CONTEXT (context); 3810 } 3811 3812 /** 3813 * g_main_context_is_owner: 3814 * @context: a #GMainContext 3815 * 3816 * Determines whether this thread holds the (recursive) 3817 * ownership of this #GMaincontext. This is useful to 3818 * know before waiting on another thread that may be 3819 * blocking to get ownership of @context. 3820 * 3821 * Returns: %TRUE if current thread is owner of @context. 3822 * 3823 * Since: 2.10 3824 **/ 3825 gboolean 3826 g_main_context_is_owner (GMainContext *context) 3827 { 3828 gboolean is_owner; 3829 3830 if (!context) 3831 context = g_main_context_default (); 3832 3833 #ifdef G_THREADS_ENABLED 3834 LOCK_CONTEXT (context); 3835 is_owner = context->owner == G_THREAD_SELF; 3836 UNLOCK_CONTEXT (context); 3837 #else 3838 is_owner = TRUE; 3839 #endif 3840 3841 return is_owner; 3842 } 3843 3844 /* Timeouts */ 3845 3846 static void 3847 g_timeout_set_expiration (GTimeoutSource *timeout_source, 3848 gint64 current_time) 3849 { 3850 timeout_source->expiration = current_time + 3851 (guint64) timeout_source->interval * 1000; 3852 3853 if (timeout_source->seconds) 3854 { 3855 gint64 remainder; 3856 static gint timer_perturb = -1; 3857 3858 if (timer_perturb == -1) 3859 { 3860 /* 3861 * we want a per machine/session unique 'random' value; try the dbus 3862 * address first, that has a UUID in it. If there is no dbus, use the 3863 * hostname for hashing. 3864 */ 3865 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS"); 3866 if (!session_bus_address) 3867 session_bus_address = g_getenv ("HOSTNAME"); 3868 if (session_bus_address) 3869 timer_perturb = ABS ((gint) g_str_hash (session_bus_address)) % 1000000; 3870 else 3871 timer_perturb = 0; 3872 } 3873 3874 /* We want the microseconds part of the timeout to land on the 3875 * 'timer_perturb' mark, but we need to make sure we don't try to 3876 * set the timeout in the past. We do this by ensuring that we 3877 * always only *increase* the expiration time by adding a full 3878 * second in the case that the microsecond portion decreases. 3879 */ 3880 timeout_source->expiration -= timer_perturb; 3881 3882 remainder = timeout_source->expiration % 1000000; 3883 if (remainder >= 1000000/4) 3884 timeout_source->expiration += 1000000; 3885 3886 timeout_source->expiration -= remainder; 3887 timeout_source->expiration += timer_perturb; 3888 } 3889 } 3890 3891 static gboolean 3892 g_timeout_prepare (GSource *source, 3893 gint *timeout) 3894 { 3895 GTimeoutSource *timeout_source = (GTimeoutSource *) source; 3896 gint64 now = g_source_get_time (source); 3897 3898 if (now < timeout_source->expiration) 3899 { 3900 /* Round up to ensure that we don't try again too early */ 3901 *timeout = (timeout_source->expiration - now + 999) / 1000; 3902 return FALSE; 3903 } 3904 3905 *timeout = 0; 3906 return TRUE; 3907 } 3908 3909 static gboolean 3910 g_timeout_check (GSource *source) 3911 { 3912 GTimeoutSource *timeout_source = (GTimeoutSource *) source; 3913 gint64 now = g_source_get_time (source); 3914 3915 return timeout_source->expiration <= now; 3916 } 3917 3918 static gboolean 3919 g_timeout_dispatch (GSource *source, 3920 GSourceFunc callback, 3921 gpointer user_data) 3922 { 3923 GTimeoutSource *timeout_source = (GTimeoutSource *)source; 3924 gboolean again; 3925 3926 if (!callback) 3927 { 3928 g_warning ("Timeout source dispatched without callback\n" 3929 "You must call g_source_set_callback()."); 3930 return FALSE; 3931 } 3932 3933 again = callback (user_data); 3934 3935 if (again) 3936 g_timeout_set_expiration (timeout_source, g_source_get_time (source)); 3937 3938 return again; 3939 } 3940 3941 /** 3942 * g_timeout_source_new: 3943 * @interval: the timeout interval in milliseconds. 3944 * 3945 * Creates a new timeout source. 3946 * 3947 * The source will not initially be associated with any #GMainContext 3948 * and must be added to one with g_source_attach() before it will be 3949 * executed. 3950 * 3951 * Return value: the newly-created timeout source 3952 **/ 3953 GSource * 3954 g_timeout_source_new (guint interval) 3955 { 3956 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource)); 3957 #ifdef GSTREAMER_LITE 3958 if (source == NULL) 3959 return NULL; 3960 #endif // GSTREAMER_LITE 3961 GTimeoutSource *timeout_source = (GTimeoutSource *)source; 3962 3963 timeout_source->interval = interval; 3964 g_timeout_set_expiration (timeout_source, g_get_monotonic_time ()); 3965 3966 return source; 3967 } 3968 3969 #ifndef GSTREAMER_LITE 3970 /** 3971 * g_timeout_source_new_seconds: 3972 * @interval: the timeout interval in seconds 3973 * 3974 * Creates a new timeout source. 3975 * 3976 * The source will not initially be associated with any #GMainContext 3977 * and must be added to one with g_source_attach() before it will be 3978 * executed. 3979 * 3980 * The scheduling granularity/accuracy of this timeout source will be 3981 * in seconds. 3982 * 3983 * Return value: the newly-created timeout source 3984 * 3985 * Since: 2.14 3986 **/ 3987 GSource * 3988 g_timeout_source_new_seconds (guint interval) 3989 { 3990 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource)); 3991 GTimeoutSource *timeout_source = (GTimeoutSource *)source; 3992 3993 timeout_source->interval = 1000 * interval; 3994 timeout_source->seconds = TRUE; 3995 3996 g_timeout_set_expiration (timeout_source, g_get_monotonic_time ()); 3997 3998 return source; 3999 } 4000 #endif // GSTREAMER_LITE 4001 4002 /** 4003 * g_timeout_add_full: 4004 * @priority: the priority of the timeout source. Typically this will be in 4005 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH. 4006 * @interval: the time between calls to the function, in milliseconds 4007 * (1/1000ths of a second) 4008 * @function: function to call 4009 * @data: data to pass to @function 4010 * @notify: function to call when the timeout is removed, or %NULL 4011 * 4012 * Sets a function to be called at regular intervals, with the given 4013 * priority. The function is called repeatedly until it returns 4014 * %FALSE, at which point the timeout is automatically destroyed and 4015 * the function will not be called again. The @notify function is 4016 * called when the timeout is destroyed. The first call to the 4017 * function will be at the end of the first @interval. 4018 * 4019 * Note that timeout functions may be delayed, due to the processing of other 4020 * event sources. Thus they should not be relied on for precise timing. 4021 * After each call to the timeout function, the time of the next 4022 * timeout is recalculated based on the current time and the given interval 4023 * (it does not try to 'catch up' time lost in delays). 4024 * 4025 * This internally creates a main loop source using g_timeout_source_new() 4026 * and attaches it to the main loop context using g_source_attach(). You can 4027 * do these steps manually if you need greater control. 4028 * 4029 * Return value: the ID (greater than 0) of the event source. 4030 **/ 4031 guint 4032 g_timeout_add_full (gint priority, 4033 guint interval, 4034 GSourceFunc function, 4035 gpointer data, 4036 GDestroyNotify notify) 4037 { 4038 GSource *source; 4039 guint id; 4040 4041 g_return_val_if_fail (function != NULL, 0); 4042 4043 source = g_timeout_source_new (interval); 4044 #ifdef GSTREAMER_LITE 4045 if (source == NULL) 4046 return 0; 4047 #endif // GSTREAMER_LITE 4048 4049 if (priority != G_PRIORITY_DEFAULT) 4050 g_source_set_priority (source, priority); 4051 4052 g_source_set_callback (source, function, data, notify); 4053 id = g_source_attach (source, NULL); 4054 g_source_unref (source); 4055 4056 return id; 4057 } 4058 4059 /** 4060 * g_timeout_add: 4061 * @interval: the time between calls to the function, in milliseconds 4062 * (1/1000ths of a second) 4063 * @function: function to call 4064 * @data: data to pass to @function 4065 * 4066 * Sets a function to be called at regular intervals, with the default 4067 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly 4068 * until it returns %FALSE, at which point the timeout is automatically 4069 * destroyed and the function will not be called again. The first call 4070 * to the function will be at the end of the first @interval. 4071 * 4072 * Note that timeout functions may be delayed, due to the processing of other 4073 * event sources. Thus they should not be relied on for precise timing. 4074 * After each call to the timeout function, the time of the next 4075 * timeout is recalculated based on the current time and the given interval 4076 * (it does not try to 'catch up' time lost in delays). 4077 * 4078 * If you want to have a timer in the "seconds" range and do not care 4079 * about the exact time of the first call of the timer, use the 4080 * g_timeout_add_seconds() function; this function allows for more 4081 * optimizations and more efficient system power usage. 4082 * 4083 * This internally creates a main loop source using g_timeout_source_new() 4084 * and attaches it to the main loop context using g_source_attach(). You can 4085 * do these steps manually if you need greater control. 4086 * 4087 * Return value: the ID (greater than 0) of the event source. 4088 **/ 4089 guint 4090 g_timeout_add (guint32 interval, 4091 GSourceFunc function, 4092 gpointer data) 4093 { 4094 return g_timeout_add_full (G_PRIORITY_DEFAULT, 4095 interval, function, data, NULL); 4096 } 4097 4098 #ifndef GSTREAMER_LITE 4099 /** 4100 * g_timeout_add_seconds_full: 4101 * @priority: the priority of the timeout source. Typically this will be in 4102 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH. 4103 * @interval: the time between calls to the function, in seconds 4104 * @function: function to call 4105 * @data: data to pass to @function 4106 * @notify: function to call when the timeout is removed, or %NULL 4107 * 4108 * Sets a function to be called at regular intervals, with @priority. 4109 * The function is called repeatedly until it returns %FALSE, at which 4110 * point the timeout is automatically destroyed and the function will 4111 * not be called again. 4112 * 4113 * Unlike g_timeout_add(), this function operates at whole second granularity. 4114 * The initial starting point of the timer is determined by the implementation 4115 * and the implementation is expected to group multiple timers together so that 4116 * they fire all at the same time. 4117 * To allow this grouping, the @interval to the first timer is rounded 4118 * and can deviate up to one second from the specified interval. 4119 * Subsequent timer iterations will generally run at the specified interval. 4120 * 4121 * Note that timeout functions may be delayed, due to the processing of other 4122 * event sources. Thus they should not be relied on for precise timing. 4123 * After each call to the timeout function, the time of the next 4124 * timeout is recalculated based on the current time and the given @interval 4125 * 4126 * If you want timing more precise than whole seconds, use g_timeout_add() 4127 * instead. 4128 * 4129 * The grouping of timers to fire at the same time results in a more power 4130 * and CPU efficient behavior so if your timer is in multiples of seconds 4131 * and you don't require the first timer exactly one second from now, the 4132 * use of g_timeout_add_seconds() is preferred over g_timeout_add(). 4133 * 4134 * This internally creates a main loop source using 4135 * g_timeout_source_new_seconds() and attaches it to the main loop context 4136 * using g_source_attach(). You can do these steps manually if you need 4137 * greater control. 4138 * 4139 * Return value: the ID (greater than 0) of the event source. 4140 * 4141 * Since: 2.14 4142 **/ 4143 guint 4144 g_timeout_add_seconds_full (gint priority, 4145 guint32 interval, 4146 GSourceFunc function, 4147 gpointer data, 4148 GDestroyNotify notify) 4149 { 4150 GSource *source; 4151 guint id; 4152 4153 g_return_val_if_fail (function != NULL, 0); 4154 4155 source = g_timeout_source_new_seconds (interval); 4156 4157 if (priority != G_PRIORITY_DEFAULT) 4158 g_source_set_priority (source, priority); 4159 4160 g_source_set_callback (source, function, data, notify); 4161 id = g_source_attach (source, NULL); 4162 g_source_unref (source); 4163 4164 return id; 4165 } 4166 4167 /** 4168 * g_timeout_add_seconds: 4169 * @interval: the time between calls to the function, in seconds 4170 * @function: function to call 4171 * @data: data to pass to @function 4172 * 4173 * Sets a function to be called at regular intervals with the default 4174 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until 4175 * it returns %FALSE, at which point the timeout is automatically destroyed 4176 * and the function will not be called again. 4177 * 4178 * This internally creates a main loop source using 4179 * g_timeout_source_new_seconds() and attaches it to the main loop context 4180 * using g_source_attach(). You can do these steps manually if you need 4181 * greater control. Also see g_timout_add_seconds_full(). 4182 * 4183 * Note that the first call of the timer may not be precise for timeouts 4184 * of one second. If you need finer precision and have such a timeout, 4185 * you may want to use g_timeout_add() instead. 4186 * 4187 * Return value: the ID (greater than 0) of the event source. 4188 * 4189 * Since: 2.14 4190 **/ 4191 guint 4192 g_timeout_add_seconds (guint interval, 4193 GSourceFunc function, 4194 gpointer data) 4195 { 4196 g_return_val_if_fail (function != NULL, 0); 4197 4198 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL); 4199 } 4200 #endif // GSTREAMER_LITE 4201 4202 /* Child watch functions */ 4203 4204 #ifdef G_OS_WIN32 4205 4206 static gboolean 4207 g_child_watch_prepare (GSource *source, 4208 gint *timeout) 4209 { 4210 *timeout = -1; 4211 return FALSE; 4212 } 4213 4214 4215 static gboolean 4216 g_child_watch_check (GSource *source) 4217 { 4218 GChildWatchSource *child_watch_source; 4219 gboolean child_exited; 4220 4221 child_watch_source = (GChildWatchSource *) source; 4222 4223 child_exited = child_watch_source->poll.revents & G_IO_IN; 4224 4225 if (child_exited) 4226 { 4227 DWORD child_status; 4228 4229 /* 4230 * Note: We do _not_ check for the special value of STILL_ACTIVE 4231 * since we know that the process has exited and doing so runs into 4232 * problems if the child process "happens to return STILL_ACTIVE(259)" 4233 * as Microsoft's Platform SDK puts it. 4234 */ 4235 if (!GetExitCodeProcess (child_watch_source->pid, &child_status)) 4236 { 4237 gchar *emsg = g_win32_error_message (GetLastError ()); 4238 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg); 4239 g_free (emsg); 4240 4241 child_watch_source->child_status = -1; 4242 } 4243 else 4244 child_watch_source->child_status = child_status; 4245 } 4246 4247 return child_exited; 4248 } 4249 4250 #else /* G_OS_WIN32 */ 4251 4252 static gboolean 4253 check_for_child_exited (GSource *source) 4254 { 4255 GChildWatchSource *child_watch_source; 4256 gint count; 4257 4258 /* protect against another SIGCHLD in the middle of this call */ 4259 count = child_watch_count; 4260 4261 child_watch_source = (GChildWatchSource *) source; 4262 4263 if (child_watch_source->child_exited) 4264 return TRUE; 4265 4266 if (child_watch_source->count < count) 4267 { 4268 gint child_status; 4269 4270 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0) 4271 { 4272 child_watch_source->child_status = child_status; 4273 child_watch_source->child_exited = TRUE; 4274 } 4275 child_watch_source->count = count; 4276 } 4277 4278 return child_watch_source->child_exited; 4279 } 4280 4281 static gboolean 4282 g_child_watch_prepare (GSource *source, 4283 gint *timeout) 4284 { 4285 *timeout = -1; 4286 4287 return check_for_child_exited (source); 4288 } 4289 4290 4291 static gboolean 4292 g_child_watch_check (GSource *source) 4293 { 4294 return check_for_child_exited (source); 4295 } 4296 4297 #endif /* G_OS_WIN32 */ 4298 4299 static gboolean 4300 g_child_watch_dispatch (GSource *source, 4301 GSourceFunc callback, 4302 gpointer user_data) 4303 { 4304 GChildWatchSource *child_watch_source; 4305 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback; 4306 4307 child_watch_source = (GChildWatchSource *) source; 4308 4309 if (!callback) 4310 { 4311 g_warning ("Child watch source dispatched without callback\n" 4312 "You must call g_source_set_callback()."); 4313 return FALSE; 4314 } 4315 4316 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data); 4317 4318 /* We never keep a child watch source around as the child is gone */ 4319 return FALSE; 4320 } 4321 4322 #ifndef G_OS_WIN32 4323 4324 static void 4325 g_child_watch_signal_handler (int signum) 4326 { 4327 child_watch_count ++; 4328 4329 if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED) 4330 { 4331 write (child_watch_wake_up_pipe[1], "B", 1); 4332 } 4333 else 4334 { 4335 /* We count on the signal interrupting the poll in the same thread. 4336 */ 4337 } 4338 } 4339 4340 static void 4341 g_child_watch_source_init_single (void) 4342 { 4343 struct sigaction action; 4344 4345 g_assert (! g_thread_supported()); 4346 g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED); 4347 4348 child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE; 4349 4350 action.sa_handler = g_child_watch_signal_handler; 4351 sigemptyset (&action.sa_mask); 4352 action.sa_flags = SA_NOCLDSTOP; 4353 sigaction (SIGCHLD, &action, NULL); 4354 } 4355 4356 G_GNUC_NORETURN static gpointer 4357 child_watch_helper_thread (gpointer data) 4358 { 4359 while (1) 4360 { 4361 gchar b[20]; 4362 GSList *list; 4363 4364 read (child_watch_wake_up_pipe[0], b, 20); 4365 4366 /* We were woken up. Wake up all other contexts in all other threads */ 4367 G_LOCK (main_context_list); 4368 for (list = main_context_list; list; list = list->next) 4369 { 4370 GMainContext *context; 4371 4372 context = list->data; 4373 if (g_atomic_int_get (&context->ref_count) > 0) 4374 /* Due to racing conditions we can find ref_count == 0, in 4375 * that case, however, the context is still not destroyed 4376 * and no poll can be active, otherwise the ref_count 4377 * wouldn't be 0 */ 4378 g_main_context_wakeup (context); 4379 } 4380 G_UNLOCK (main_context_list); 4381 } 4382 } 4383 4384 static void 4385 g_child_watch_source_init_multi_threaded (void) 4386 { 4387 GError *error = NULL; 4388 struct sigaction action; 4389 4390 g_assert (g_thread_supported()); 4391 4392 if (pipe (child_watch_wake_up_pipe) < 0) 4393 g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno)); 4394 fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL)); 4395 4396 /* We create a helper thread that polls on the wakeup pipe indefinitely */ 4397 /* FIXME: Think this through for races */ 4398 if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL) 4399 g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message); 4400 child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED; 4401 4402 action.sa_handler = g_child_watch_signal_handler; 4403 sigemptyset (&action.sa_mask); 4404 action.sa_flags = SA_RESTART | SA_NOCLDSTOP; 4405 sigaction (SIGCHLD, &action, NULL); 4406 } 4407 4408 static void 4409 g_child_watch_source_init_promote_single_to_threaded (void) 4410 { 4411 g_child_watch_source_init_multi_threaded (); 4412 } 4413 4414 static void 4415 g_child_watch_source_init (void) 4416 { 4417 if (g_thread_supported()) 4418 { 4419 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED) 4420 g_child_watch_source_init_multi_threaded (); 4421 else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE) 4422 g_child_watch_source_init_promote_single_to_threaded (); 4423 } 4424 else 4425 { 4426 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED) 4427 g_child_watch_source_init_single (); 4428 } 4429 } 4430 4431 #endif /* !G_OS_WIN32 */ 4432 4433 /** 4434 * g_child_watch_source_new: 4435 * @pid: process to watch. On POSIX the pid of a child process. On 4436 * Windows a handle for a process (which doesn't have to be a child). 4437 * 4438 * Creates a new child_watch source. 4439 * 4440 * The source will not initially be associated with any #GMainContext 4441 * and must be added to one with g_source_attach() before it will be 4442 * executed. 4443 * 4444 * Note that child watch sources can only be used in conjunction with 4445 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD 4446 * flag is used. 4447 * 4448 * Note that on platforms where #GPid must be explicitly closed 4449 * (see g_spawn_close_pid()) @pid must not be closed while the 4450 * source is still active. Typically, you will want to call 4451 * g_spawn_close_pid() in the callback function for the source. 4452 * 4453 * Note further that using g_child_watch_source_new() is not 4454 * compatible with calling <literal>waitpid(-1)</literal> in 4455 * the application. Calling waitpid() for individual pids will 4456 * still work fine. 4457 * 4458 * Return value: the newly-created child watch source 4459 * 4460 * Since: 2.4 4461 **/ 4462 GSource * 4463 g_child_watch_source_new (GPid pid) 4464 { 4465 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource)); 4466 GChildWatchSource *child_watch_source = (GChildWatchSource *)source; 4467 4468 #ifdef GSTREAMER_LITE 4469 if (source == NULL) 4470 return NULL; 4471 #endif // GSTREAMER_LITE 4472 4473 #ifdef G_OS_WIN32 4474 child_watch_source->poll.fd = (gintptr) pid; 4475 child_watch_source->poll.events = G_IO_IN; 4476 4477 g_source_add_poll (source, &child_watch_source->poll); 4478 #else /* G_OS_WIN32 */ 4479 g_child_watch_source_init (); 4480 #endif /* G_OS_WIN32 */ 4481 4482 child_watch_source->pid = pid; 4483 4484 return source; 4485 } 4486 4487 /** 4488 * g_child_watch_add_full: 4489 * @priority: the priority of the idle source. Typically this will be in the 4490 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE. 4491 * @pid: process to watch. On POSIX the pid of a child process. On 4492 * Windows a handle for a process (which doesn't have to be a child). 4493 * @function: function to call 4494 * @data: data to pass to @function 4495 * @notify: function to call when the idle is removed, or %NULL 4496 * 4497 * Sets a function to be called when the child indicated by @pid 4498 * exits, at the priority @priority. 4499 * 4500 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes() 4501 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to 4502 * the spawn function for the child watching to work. 4503 * 4504 * Note that on platforms where #GPid must be explicitly closed 4505 * (see g_spawn_close_pid()) @pid must not be closed while the 4506 * source is still active. Typically, you will want to call 4507 * g_spawn_close_pid() in the callback function for the source. 4508 * 4509 * GLib supports only a single callback per process id. 4510 * 4511 * This internally creates a main loop source using 4512 * g_child_watch_source_new() and attaches it to the main loop context 4513 * using g_source_attach(). You can do these steps manually if you 4514 * need greater control. 4515 * 4516 * Return value: the ID (greater than 0) of the event source. 4517 * 4518 * Since: 2.4 4519 **/ 4520 guint 4521 g_child_watch_add_full (gint priority, 4522 GPid pid, 4523 GChildWatchFunc function, 4524 gpointer data, 4525 GDestroyNotify notify) 4526 { 4527 GSource *source; 4528 guint id; 4529 4530 g_return_val_if_fail (function != NULL, 0); 4531 4532 source = g_child_watch_source_new (pid); 4533 4534 if (priority != G_PRIORITY_DEFAULT) 4535 g_source_set_priority (source, priority); 4536 4537 g_source_set_callback (source, (GSourceFunc) function, data, notify); 4538 id = g_source_attach (source, NULL); 4539 g_source_unref (source); 4540 4541 return id; 4542 } 4543 4544 /** 4545 * g_child_watch_add: 4546 * @pid: process id to watch. On POSIX the pid of a child process. On 4547 * Windows a handle for a process (which doesn't have to be a child). 4548 * @function: function to call 4549 * @data: data to pass to @function 4550 * 4551 * Sets a function to be called when the child indicated by @pid 4552 * exits, at a default priority, #G_PRIORITY_DEFAULT. 4553 * 4554 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes() 4555 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to 4556 * the spawn function for the child watching to work. 4557 * 4558 * Note that on platforms where #GPid must be explicitly closed 4559 * (see g_spawn_close_pid()) @pid must not be closed while the 4560 * source is still active. Typically, you will want to call 4561 * g_spawn_close_pid() in the callback function for the source. 4562 * 4563 * GLib supports only a single callback per process id. 4564 * 4565 * This internally creates a main loop source using 4566 * g_child_watch_source_new() and attaches it to the main loop context 4567 * using g_source_attach(). You can do these steps manually if you 4568 * need greater control. 4569 * 4570 * Return value: the ID (greater than 0) of the event source. 4571 * 4572 * Since: 2.4 4573 **/ 4574 guint 4575 g_child_watch_add (GPid pid, 4576 GChildWatchFunc function, 4577 gpointer data) 4578 { 4579 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL); 4580 } 4581 4582 4583 /* Idle functions */ 4584 4585 static gboolean 4586 g_idle_prepare (GSource *source, 4587 gint *timeout) 4588 { 4589 *timeout = 0; 4590 4591 return TRUE; 4592 } 4593 4594 static gboolean 4595 g_idle_check (GSource *source) 4596 { 4597 return TRUE; 4598 } 4599 4600 static gboolean 4601 g_idle_dispatch (GSource *source, 4602 GSourceFunc callback, 4603 gpointer user_data) 4604 { 4605 if (!callback) 4606 { 4607 g_warning ("Idle source dispatched without callback\n" 4608 "You must call g_source_set_callback()."); 4609 return FALSE; 4610 } 4611 4612 return callback (user_data); 4613 } 4614 4615 /** 4616 * g_idle_source_new: 4617 * 4618 * Creates a new idle source. 4619 * 4620 * The source will not initially be associated with any #GMainContext 4621 * and must be added to one with g_source_attach() before it will be 4622 * executed. Note that the default priority for idle sources is 4623 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which 4624 * have a default priority of %G_PRIORITY_DEFAULT. 4625 * 4626 * Return value: the newly-created idle source 4627 **/ 4628 GSource * 4629 g_idle_source_new (void) 4630 { 4631 GSource *source; 4632 4633 source = g_source_new (&g_idle_funcs, sizeof (GSource)); 4634 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE); 4635 4636 return source; 4637 } 4638 4639 /** 4640 * g_idle_add_full: 4641 * @priority: the priority of the idle source. Typically this will be in the 4642 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE. 4643 * @function: function to call 4644 * @data: data to pass to @function 4645 * @notify: function to call when the idle is removed, or %NULL 4646 * 4647 * Adds a function to be called whenever there are no higher priority 4648 * events pending. If the function returns %FALSE it is automatically 4649 * removed from the list of event sources and will not be called again. 4650 * 4651 * This internally creates a main loop source using g_idle_source_new() 4652 * and attaches it to the main loop context using g_source_attach(). 4653 * You can do these steps manually if you need greater control. 4654 * 4655 * Return value: the ID (greater than 0) of the event source. 4656 **/ 4657 guint 4658 g_idle_add_full (gint priority, 4659 GSourceFunc function, 4660 gpointer data, 4661 GDestroyNotify notify) 4662 { 4663 GSource *source; 4664 guint id; 4665 4666 g_return_val_if_fail (function != NULL, 0); 4667 4668 source = g_idle_source_new (); 4669 4670 if (priority != G_PRIORITY_DEFAULT_IDLE) 4671 g_source_set_priority (source, priority); 4672 4673 g_source_set_callback (source, function, data, notify); 4674 id = g_source_attach (source, NULL); 4675 g_source_unref (source); 4676 4677 return id; 4678 } 4679 4680 /** 4681 * g_idle_add: 4682 * @function: function to call 4683 * @data: data to pass to @function. 4684 * 4685 * Adds a function to be called whenever there are no higher priority 4686 * events pending to the default main loop. The function is given the 4687 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function 4688 * returns %FALSE it is automatically removed from the list of event 4689 * sources and will not be called again. 4690 * 4691 * This internally creates a main loop source using g_idle_source_new() 4692 * and attaches it to the main loop context using g_source_attach(). 4693 * You can do these steps manually if you need greater control. 4694 * 4695 * Return value: the ID (greater than 0) of the event source. 4696 **/ 4697 guint 4698 g_idle_add (GSourceFunc function, 4699 gpointer data) 4700 { 4701 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL); 4702 } 4703 4704 /** 4705 * g_idle_remove_by_data: 4706 * @data: the data for the idle source's callback. 4707 * 4708 * Removes the idle function with the given data. 4709 * 4710 * Return value: %TRUE if an idle source was found and removed. 4711 **/ 4712 gboolean 4713 g_idle_remove_by_data (gpointer data) 4714 { 4715 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data); 4716 } 4717 4718 /** 4719 * g_main_context_invoke: 4720 * @context: a #GMainContext, or %NULL 4721 * @function: function to call 4722 * @data: data to pass to @function 4723 * 4724 * Invokes a function in such a way that @context is owned during the 4725 * invocation of @function. 4726 * 4727 * If @context is %NULL then the global default main context — as 4728 * returned by g_main_context_default() — is used. 4729 * 4730 * If @context is owned by the current thread, @function is called 4731 * directly. Otherwise, if @context is the thread-default main context 4732 * of the current thread and g_main_context_acquire() succeeds, then 4733 * @function is called and g_main_context_release() is called 4734 * afterwards. 4735 * 4736 * In any other case, an idle source is created to call @function and 4737 * that source is attached to @context (presumably to be run in another 4738 * thread). The idle source is attached with #G_PRIORITY_DEFAULT 4739 * priority. If you want a different priority, use 4740 * g_main_context_invoke_full(). 4741 * 4742 * Note that, as with normal idle functions, @function should probably 4743 * return %FALSE. If it returns %TRUE, it will be continuously run in a 4744 * loop (and may prevent this call from returning). 4745 * 4746 * Since: 2.28 4747 **/ 4748 void 4749 g_main_context_invoke (GMainContext *context, 4750 GSourceFunc function, 4751 gpointer data) 4752 { 4753 g_main_context_invoke_full (context, 4754 G_PRIORITY_DEFAULT, 4755 function, data, NULL); 4756 } 4757 4758 /** 4759 * g_main_context_invoke_full: 4760 * @context: a #GMainContext, or %NULL 4761 * @priority: the priority at which to run @function 4762 * @function: function to call 4763 * @data: data to pass to @function 4764 * @notify: a function to call when @data is no longer in use, or %NULL. 4765 * 4766 * Invokes a function in such a way that @context is owned during the 4767 * invocation of @function. 4768 * 4769 * This function is the same as g_main_context_invoke() except that it 4770 * lets you specify the priority incase @function ends up being 4771 * scheduled as an idle and also lets you give a #GDestroyNotify for @data. 4772 * 4773 * @notify should not assume that it is called from any particular 4774 * thread or with any particular context acquired. 4775 * 4776 * Since: 2.28 4777 **/ 4778 void 4779 g_main_context_invoke_full (GMainContext *context, 4780 gint priority, 4781 GSourceFunc function, 4782 gpointer data, 4783 GDestroyNotify notify) 4784 { 4785 g_return_if_fail (function != NULL); 4786 4787 if (!context) 4788 context = g_main_context_default (); 4789 4790 if (g_main_context_is_owner (context)) 4791 { 4792 while (function (data)); 4793 if (notify != NULL) 4794 notify (data); 4795 } 4796 4797 else 4798 { 4799 GMainContext *thread_default; 4800 4801 thread_default = g_main_context_get_thread_default (); 4802 4803 if (!thread_default) 4804 thread_default = g_main_context_default (); 4805 4806 if (thread_default == context && g_main_context_acquire (context)) 4807 { 4808 while (function (data)); 4809 4810 g_main_context_release (context); 4811 4812 if (notify != NULL) 4813 notify (data); 4814 } 4815 else 4816 { 4817 GSource *source; 4818 4819 source = g_idle_source_new (); 4820 g_source_set_priority (source, priority); 4821 g_source_set_callback (source, function, data, notify); 4822 g_source_attach (source, context); 4823 g_source_unref (source); 4824 } 4825 } 4826 }