1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmain.c: Main loop abstraction, timeouts, and idle functions
5 * Copyright 1998 Owen Taylor
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.
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.
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.
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/.
35 #include "glibconfig.h"
37 /* Uncomment the next line (and the corresponding line in gpoll.c) to
38 * enable debugging printouts if the environment variable
39 * G_MAIN_POLL_DEBUG is set to some value.
41 /* #define G_MAIN_POLL_DEBUG */
44 /* Always enable debugging printout on Windows, as it is more often
47 #define G_MAIN_POLL_DEBUG
51 #include "glib-unix.h"
54 #include <sys/eventfd.h>
59 #include <sys/types.h>
62 #ifdef HAVE_SYS_TIME_H
64 #endif /* HAVE_SYS_TIME_H */
67 #endif /* HAVE_UNISTD_H */
74 #endif /* G_OS_WIN32 */
77 #include <sys/socket.h>
79 #endif /* G_OS_BEOS */
84 #include "giochannel.h"
88 #include "gstrfuncs.h"
89 #include "gtestutils.h"
95 #ifdef G_MAIN_POLL_DEBUG
100 #include "gmain-internal.h"
101 #include "glib-private.h"
105 * @title: The Main Event Loop
106 * @short_description: manages all available sources of events
108 * The main event loop manages all the available sources of events for
109 * GLib and GTK+ applications. These events can come from any number of
110 * different types of sources such as file descriptors (plain files,
111 * pipes or sockets) and timeouts. New types of event sources can also
112 * be added using g_source_attach().
114 * To allow multiple independent sets of sources to be handled in
115 * different threads, each source is associated with a #GMainContext.
116 * A GMainContext can only be running in a single thread, but
117 * sources can be added to it and removed from it from other threads.
119 * Each event source is assigned a priority. The default priority,
120 * #G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities.
121 * Values greater than 0 denote lower priorities. Events from high priority
122 * sources are always processed before events from lower priority sources.
124 * Idle functions can also be added, and assigned a priority. These will
125 * be run whenever no events with a higher priority are ready to be processed.
127 * The #GMainLoop data type represents a main event loop. A GMainLoop is
128 * created with g_main_loop_new(). After adding the initial event sources,
129 * g_main_loop_run() is called. This continuously checks for new events from
130 * each of the event sources and dispatches them. Finally, the processing of
131 * an event from one of the sources leads to a call to g_main_loop_quit() to
132 * exit the main loop, and g_main_loop_run() returns.
134 * It is possible to create new instances of #GMainLoop recursively.
135 * This is often used in GTK+ applications when showing modal dialog
136 * boxes. Note that event sources are associated with a particular
137 * #GMainContext, and will be checked and dispatched for all main
138 * loops associated with that GMainContext.
140 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
141 * gtk_main_quit() and gtk_events_pending().
143 * <refsect2><title>Creating new source types</title>
144 * <para>One of the unusual features of the #GMainLoop functionality
145 * is that new types of event source can be created and used in
146 * addition to the builtin type of event source. A new event source
147 * type is used for handling GDK events. A new source type is created
148 * by <firstterm>deriving</firstterm> from the #GSource structure.
149 * The derived type of source is represented by a structure that has
150 * the #GSource structure as a first element, and other elements specific
151 * to the new source type. To create an instance of the new source type,
152 * call g_source_new() passing in the size of the derived structure and
153 * a table of functions. These #GSourceFuncs determine the behavior of
154 * the new source type.</para>
155 * <para>New source types basically interact with the main context
156 * in two ways. Their prepare function in #GSourceFuncs can set a timeout
157 * to determine the maximum amount of time that the main loop will sleep
158 * before checking the source again. In addition, or as well, the source
159 * can add file descriptors to the set that the main context checks using
160 * g_source_add_poll().</para>
162 * <refsect2><title>Customizing the main loop iteration</title>
163 * <para>Single iterations of a #GMainContext can be run with
164 * g_main_context_iteration(). In some cases, more detailed control
165 * of exactly how the details of the main loop work is desired, for
166 * instance, when integrating the #GMainLoop with an external main loop.
167 * In such cases, you can call the component functions of
168 * g_main_context_iteration() directly. These functions are
169 * g_main_context_prepare(), g_main_context_query(),
170 * g_main_context_check() and g_main_context_dispatch().</para>
171 * <para>The operation of these functions can best be seen in terms
172 * of a state diagram, as shown in <xref linkend="mainloop-states"/>.</para>
173 * <figure id="mainloop-states"><title>States of a Main Context</title>
174 * <graphic fileref="mainloop-states.gif" format="GIF"></graphic>
178 * On Unix, the GLib mainloop is incompatible with fork(). Any program
179 * using the mainloop must either exec() or exit() from the child
180 * without returning to the mainloop.
185 typedef struct _GTimeoutSource GTimeoutSource
;
186 typedef struct _GChildWatchSource GChildWatchSource
;
187 typedef struct _GUnixSignalWatchSource GUnixSignalWatchSource
;
188 typedef struct _GPollRec GPollRec
;
189 typedef struct _GSourceCallback GSourceCallback
;
193 G_SOURCE_READY
= 1 << G_HOOK_FLAG_USER_SHIFT
,
194 G_SOURCE_CAN_RECURSE
= 1 << (G_HOOK_FLAG_USER_SHIFT
+ 1)
197 typedef struct _GMainWaiter GMainWaiter
;
205 typedef struct _GMainDispatch GMainDispatch
;
207 struct _GMainDispatch
210 GSList
*dispatching_sources
; /* stack of current sources */
213 #ifdef G_MAIN_POLL_DEBUG
214 gboolean _g_main_poll_debug
= FALSE
;
219 /* The following lock is used for both the list of sources
220 * and the list of poll records
230 GPtrArray
*pending_dispatches
;
231 gint timeout
; /* Timeout for current iteration */
234 GSource
*source_list
;
235 gint in_check_or_prepare
;
237 GPollRec
*poll_records
, *poll_records_tail
;
238 guint n_poll_records
;
239 GPollFD
*cached_poll_array
;
240 guint cached_poll_array_size
;
246 /* Flag indicating whether the set of fd's changed during a poll */
247 gboolean poll_changed
;
252 gboolean time_is_fresh
;
255 struct _GSourceCallback
260 GDestroyNotify notify
;
265 GMainContext
*context
;
270 struct _GTimeoutSource
278 struct _GChildWatchSource
285 #else /* G_OS_WIN32 */
286 gboolean child_exited
;
287 #endif /* G_OS_WIN32 */
290 struct _GUnixSignalWatchSource
305 struct _GSourcePrivate
307 GSList
*child_sources
;
308 GSource
*parent_source
;
311 #define LOCK_CONTEXT(context) g_mutex_lock (&context->mutex)
312 #define UNLOCK_CONTEXT(context) g_mutex_unlock (&context->mutex)
313 #define G_THREAD_SELF g_thread_self ()
315 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
316 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
317 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
319 #define SOURCE_UNREF(source, context) \
321 if ((source)->ref_count > 1) \
322 (source)->ref_count--; \
324 g_source_unref_internal ((source), (context), TRUE); \
328 /* Forward declarations */
330 static void g_source_unref_internal (GSource
*source
,
331 GMainContext
*context
,
333 static void g_source_destroy_internal (GSource
*source
,
334 GMainContext
*context
,
336 static void g_source_set_priority_unlocked (GSource
*source
,
337 GMainContext
*context
,
339 static void g_main_context_poll (GMainContext
*context
,
344 static void g_main_context_add_poll_unlocked (GMainContext
*context
,
347 static void g_main_context_remove_poll_unlocked (GMainContext
*context
,
350 static gboolean
g_timeout_prepare (GSource
*source
,
352 static gboolean
g_timeout_check (GSource
*source
);
353 static gboolean
g_timeout_dispatch (GSource
*source
,
354 GSourceFunc callback
,
356 static gboolean
g_child_watch_prepare (GSource
*source
,
358 static gboolean
g_child_watch_check (GSource
*source
);
359 static gboolean
g_child_watch_dispatch (GSource
*source
,
360 GSourceFunc callback
,
362 static void g_child_watch_finalize (GSource
*source
);
364 static void g_unix_signal_handler (int signum
);
365 static gboolean
g_unix_signal_watch_prepare (GSource
*source
,
367 static gboolean
g_unix_signal_watch_check (GSource
*source
);
368 static gboolean
g_unix_signal_watch_dispatch (GSource
*source
,
369 GSourceFunc callback
,
371 static void g_unix_signal_watch_finalize (GSource
*source
);
373 static gboolean
g_idle_prepare (GSource
*source
,
375 static gboolean
g_idle_check (GSource
*source
);
376 static gboolean
g_idle_dispatch (GSource
*source
,
377 GSourceFunc callback
,
380 static GMainContext
*glib_worker_context
;
382 G_LOCK_DEFINE_STATIC (main_loop
);
383 static GMainContext
*default_main_context
;
388 /* UNIX signals work by marking one of these variables then waking the
389 * worker context to check on them and dispatch accordingly.
391 static volatile gchar unix_signal_pending
[NSIG
];
392 static volatile gboolean any_unix_signal_pending
;
394 /* Guards all the data below */
395 G_LOCK_DEFINE_STATIC (unix_signal_lock
);
396 static GSList
*unix_signal_watches
;
397 static GSList
*unix_child_watches
;
399 static GSourceFuncs g_unix_signal_funcs
=
401 g_unix_signal_watch_prepare
,
402 g_unix_signal_watch_check
,
403 g_unix_signal_watch_dispatch
,
404 g_unix_signal_watch_finalize
406 #endif /* !G_OS_WIN32 */
407 G_LOCK_DEFINE_STATIC (main_context_list
);
408 static GSList
*main_context_list
= NULL
;
410 GSourceFuncs g_timeout_funcs
=
418 GSourceFuncs g_child_watch_funcs
=
420 g_child_watch_prepare
,
422 g_child_watch_dispatch
,
423 g_child_watch_finalize
426 GSourceFuncs g_idle_funcs
=
435 * g_main_context_ref:
436 * @context: a #GMainContext
438 * Increases the reference count on a #GMainContext object by one.
440 * Returns: the @context that was passed in (since 2.6)
443 g_main_context_ref (GMainContext
*context
)
445 g_return_val_if_fail (context
!= NULL
, NULL
);
446 g_return_val_if_fail (g_atomic_int_get (&context
->ref_count
) > 0, NULL
);
448 g_atomic_int_inc (&context
->ref_count
);
454 poll_rec_list_free (GMainContext
*context
,
457 g_slice_free_chain (GPollRec
, list
, next
);
461 * g_main_context_unref:
462 * @context: a #GMainContext
464 * Decreases the reference count on a #GMainContext object by one. If
465 * the result is zero, free the context and free all associated memory.
468 g_main_context_unref (GMainContext
*context
)
471 g_return_if_fail (context
!= NULL
);
472 g_return_if_fail (g_atomic_int_get (&context
->ref_count
) > 0);
474 if (!g_atomic_int_dec_and_test (&context
->ref_count
))
477 G_LOCK (main_context_list
);
478 main_context_list
= g_slist_remove (main_context_list
, context
);
479 G_UNLOCK (main_context_list
);
481 source
= context
->source_list
;
484 GSource
*next
= source
->next
;
485 g_source_destroy_internal (source
, context
, FALSE
);
489 g_mutex_clear (&context
->mutex
);
491 g_ptr_array_free (context
->pending_dispatches
, TRUE
);
492 g_free (context
->cached_poll_array
);
494 poll_rec_list_free (context
, context
->poll_records
);
496 g_wakeup_free (context
->wakeup
);
497 g_cond_clear (&context
->cond
);
503 * g_main_context_new:
505 * Creates a new #GMainContext structure.
507 * Return value: the new #GMainContext
510 g_main_context_new (void)
512 static gsize initialised
;
513 GMainContext
*context
;
515 if (g_once_init_enter (&initialised
))
517 #ifdef G_MAIN_POLL_DEBUG
518 if (getenv ("G_MAIN_POLL_DEBUG") != NULL
)
519 _g_main_poll_debug
= TRUE
;
522 g_once_init_leave (&initialised
, TRUE
);
525 context
= g_new0 (GMainContext
, 1);
527 g_mutex_init (&context
->mutex
);
528 g_cond_init (&context
->cond
);
530 context
->owner
= NULL
;
531 context
->waiters
= NULL
;
533 context
->ref_count
= 1;
535 context
->next_id
= 1;
537 context
->source_list
= NULL
;
539 context
->poll_func
= g_poll
;
541 context
->cached_poll_array
= NULL
;
542 context
->cached_poll_array_size
= 0;
544 context
->pending_dispatches
= g_ptr_array_new ();
546 context
->time_is_fresh
= FALSE
;
548 context
->wakeup
= g_wakeup_new ();
549 g_wakeup_get_pollfd (context
->wakeup
, &context
->wake_up_rec
);
550 g_main_context_add_poll_unlocked (context
, 0, &context
->wake_up_rec
);
552 G_LOCK (main_context_list
);
553 main_context_list
= g_slist_append (main_context_list
, context
);
555 #ifdef G_MAIN_POLL_DEBUG
556 if (_g_main_poll_debug
)
557 g_print ("created context=%p\n", context
);
560 G_UNLOCK (main_context_list
);
566 * g_main_context_default:
568 * Returns the global default main context. This is the main context
569 * used for main loop functions when a main loop is not explicitly
570 * specified, and corresponds to the "main" main loop. See also
571 * g_main_context_get_thread_default().
573 * Return value: (transfer none): the global default main context.
576 g_main_context_default (void)
582 if (!default_main_context
)
584 default_main_context
= g_main_context_new ();
585 #ifdef G_MAIN_POLL_DEBUG
586 if (_g_main_poll_debug
)
587 g_print ("default context=%p\n", default_main_context
);
591 G_UNLOCK (main_loop
);
593 return default_main_context
;
597 free_context (gpointer data
)
599 GMainContext
*context
= data
;
601 g_main_context_release (context
);
603 g_main_context_unref (context
);
607 free_context_stack (gpointer data
)
609 g_queue_free_full((GQueue
*) data
, (GDestroyNotify
) free_context
);
612 static GPrivate thread_context_stack
= G_PRIVATE_INIT (free_context_stack
);
615 * g_main_context_push_thread_default:
616 * @context: a #GMainContext, or %NULL for the global default context
618 * Acquires @context and sets it as the thread-default context for the
619 * current thread. This will cause certain asynchronous operations
620 * (such as most <link linkend="gio">gio</link>-based I/O) which are
621 * started in this thread to run under @context and deliver their
622 * results to its main loop, rather than running under the global
623 * default context in the main thread. Note that calling this function
624 * changes the context returned by
625 * g_main_context_get_thread_default(), <emphasis>not</emphasis> the
626 * one returned by g_main_context_default(), so it does not affect the
627 * context used by functions like g_idle_add().
629 * Normally you would call this function shortly after creating a new
630 * thread, passing it a #GMainContext which will be run by a
631 * #GMainLoop in that thread, to set a new default context for all
632 * async operations in that thread. (In this case, you don't need to
633 * ever call g_main_context_pop_thread_default().) In some cases
634 * however, you may want to schedule a single operation in a
635 * non-default context, or temporarily use a non-default context in
636 * the main thread. In that case, you can wrap the call to the
637 * asynchronous operation inside a
638 * g_main_context_push_thread_default() /
639 * g_main_context_pop_thread_default() pair, but it is up to you to
640 * ensure that no other asynchronous operations accidentally get
641 * started while the non-default context is active.
643 * Beware that libraries that predate this function may not correctly
644 * handle being used from a thread with a thread-default context. Eg,
645 * see g_file_supports_thread_contexts().
650 g_main_context_push_thread_default (GMainContext
*context
)
653 gboolean acquired_context
;
655 acquired_context
= g_main_context_acquire (context
);
656 g_return_if_fail (acquired_context
);
658 if (context
== g_main_context_default ())
661 g_main_context_ref (context
);
663 stack
= g_private_get (&thread_context_stack
);
666 stack
= g_queue_new ();
667 g_private_set (&thread_context_stack
, stack
);
670 g_queue_push_head (stack
, context
);
674 * g_main_context_pop_thread_default:
675 * @context: a #GMainContext object, or %NULL
677 * Pops @context off the thread-default context stack (verifying that
678 * it was on the top of the stack).
683 g_main_context_pop_thread_default (GMainContext
*context
)
687 if (context
== g_main_context_default ())
690 stack
= g_private_get (&thread_context_stack
);
692 g_return_if_fail (stack
!= NULL
);
693 g_return_if_fail (g_queue_peek_head (stack
) == context
);
695 g_queue_pop_head (stack
);
697 g_main_context_release (context
);
699 g_main_context_unref (context
);
703 * g_main_context_get_thread_default:
705 * Gets the thread-default #GMainContext for this thread. Asynchronous
706 * operations that want to be able to be run in contexts other than
707 * the default one should call this method or
708 * g_main_context_ref_thread_default() to get a #GMainContext to add
709 * their #GSource<!-- -->s to. (Note that even in single-threaded
710 * programs applications may sometimes want to temporarily push a
711 * non-default context, so it is not safe to assume that this will
712 * always return %NULL if you are running in the default thread.)
714 * If you need to hold a reference on the context, use
715 * g_main_context_ref_thread_default() instead.
717 * Returns: (transfer none): the thread-default #GMainContext, or
718 * %NULL if the thread-default context is the global default context.
723 g_main_context_get_thread_default (void)
727 stack
= g_private_get (&thread_context_stack
);
729 return g_queue_peek_head (stack
);
735 * g_main_context_ref_thread_default:
737 * Gets the thread-default #GMainContext for this thread, as with
738 * g_main_context_get_thread_default(), but also adds a reference to
739 * it with g_main_context_ref(). In addition, unlike
740 * g_main_context_get_thread_default(), if the thread-default context
741 * is the global default context, this will return that #GMainContext
742 * (with a ref added to it) rather than returning %NULL.
744 * Returns: (transfer full): the thread-default #GMainContext. Unref
745 * with g_main_context_unref() when you are done with it.
750 g_main_context_ref_thread_default (void)
752 GMainContext
*context
;
754 context
= g_main_context_get_thread_default ();
756 context
= g_main_context_default ();
757 return g_main_context_ref (context
);
760 /* Hooks for adding to the main loop */
764 * @source_funcs: structure containing functions that implement
765 * the sources behavior.
766 * @struct_size: size of the #GSource structure to create.
768 * Creates a new #GSource structure. The size is specified to
769 * allow creating structures derived from #GSource that contain
770 * additional data. The size passed in must be at least
771 * <literal>sizeof (GSource)</literal>.
773 * The source will not initially be associated with any #GMainContext
774 * and must be added to one with g_source_attach() before it will be
777 * Return value: the newly-created #GSource.
780 g_source_new (GSourceFuncs
*source_funcs
,
785 g_return_val_if_fail (source_funcs
!= NULL
, NULL
);
786 g_return_val_if_fail (struct_size
>= sizeof (GSource
), NULL
);
788 source
= (GSource
*) g_malloc0 (struct_size
);
790 source
->source_funcs
= source_funcs
;
791 source
->ref_count
= 1;
793 source
->priority
= G_PRIORITY_DEFAULT
;
795 source
->flags
= G_HOOK_FLAG_ACTIVE
;
797 /* NULL/0 initialization for all other fields */
802 /* Holds context's lock
805 g_source_list_add (GSource
*source
,
806 GMainContext
*context
)
808 GSource
*tmp_source
, *last_source
;
810 if (source
->priv
&& source
->priv
->parent_source
)
812 /* Put the source immediately before its parent */
813 tmp_source
= source
->priv
->parent_source
;
814 last_source
= source
->priv
->parent_source
->prev
;
819 tmp_source
= context
->source_list
;
820 while (tmp_source
&& tmp_source
->priority
<= source
->priority
)
822 last_source
= tmp_source
;
823 tmp_source
= tmp_source
->next
;
827 source
->next
= tmp_source
;
829 tmp_source
->prev
= source
;
831 source
->prev
= last_source
;
833 last_source
->next
= source
;
835 context
->source_list
= source
;
838 /* Holds context's lock
841 g_source_list_remove (GSource
*source
,
842 GMainContext
*context
)
845 source
->prev
->next
= source
->next
;
847 context
->source_list
= source
->next
;
850 source
->next
->prev
= source
->prev
;
857 g_source_attach_unlocked (GSource
*source
,
858 GMainContext
*context
)
863 source
->context
= context
;
864 result
= source
->source_id
= context
->next_id
++;
867 g_source_list_add (source
, context
);
869 tmp_list
= source
->poll_fds
;
872 g_main_context_add_poll_unlocked (context
, source
->priority
, tmp_list
->data
);
873 tmp_list
= tmp_list
->next
;
878 tmp_list
= source
->priv
->child_sources
;
881 g_source_attach_unlocked (tmp_list
->data
, context
);
882 tmp_list
= tmp_list
->next
;
891 * @source: a #GSource
892 * @context: (allow-none): a #GMainContext (if %NULL, the default context will be used)
894 * Adds a #GSource to a @context so that it will be executed within
895 * that context. Remove it by calling g_source_destroy().
897 * Return value: the ID (greater than 0) for the source within the
901 g_source_attach (GSource
*source
,
902 GMainContext
*context
)
906 g_return_val_if_fail (source
->context
== NULL
, 0);
907 g_return_val_if_fail (!SOURCE_DESTROYED (source
), 0);
910 context
= g_main_context_default ();
912 LOCK_CONTEXT (context
);
914 result
= g_source_attach_unlocked (source
, context
);
916 /* If another thread has acquired the context, wake it up since it
917 * might be in poll() right now.
919 if (context
->owner
&& context
->owner
!= G_THREAD_SELF
)
920 g_wakeup_signal (context
->wakeup
);
922 UNLOCK_CONTEXT (context
);
928 g_source_destroy_internal (GSource
*source
,
929 GMainContext
*context
,
933 LOCK_CONTEXT (context
);
935 if (!SOURCE_DESTROYED (source
))
938 gpointer old_cb_data
;
939 GSourceCallbackFuncs
*old_cb_funcs
;
941 source
->flags
&= ~G_HOOK_FLAG_ACTIVE
;
943 old_cb_data
= source
->callback_data
;
944 old_cb_funcs
= source
->callback_funcs
;
946 source
->callback_data
= NULL
;
947 source
->callback_funcs
= NULL
;
951 UNLOCK_CONTEXT (context
);
952 old_cb_funcs
->unref (old_cb_data
);
953 LOCK_CONTEXT (context
);
956 if (!SOURCE_BLOCKED (source
))
958 tmp_list
= source
->poll_fds
;
961 g_main_context_remove_poll_unlocked (context
, tmp_list
->data
);
962 tmp_list
= tmp_list
->next
;
966 if (source
->priv
&& source
->priv
->child_sources
)
968 /* This is safe because even if a child_source finalizer or
969 * closure notify tried to modify source->priv->child_sources
970 * from outside the lock, it would fail since
971 * SOURCE_DESTROYED(source) is now TRUE.
973 tmp_list
= source
->priv
->child_sources
;
976 g_source_destroy_internal (tmp_list
->data
, context
, TRUE
);
977 g_source_unref_internal (tmp_list
->data
, context
, TRUE
);
978 tmp_list
= tmp_list
->next
;
980 g_slist_free (source
->priv
->child_sources
);
981 source
->priv
->child_sources
= NULL
;
984 g_source_unref_internal (source
, context
, TRUE
);
988 UNLOCK_CONTEXT (context
);
993 * @source: a #GSource
995 * Removes a source from its #GMainContext, if any, and mark it as
996 * destroyed. The source cannot be subsequently added to another
1000 g_source_destroy (GSource
*source
)
1002 GMainContext
*context
;
1004 g_return_if_fail (source
!= NULL
);
1006 context
= source
->context
;
1009 g_source_destroy_internal (source
, context
, FALSE
);
1011 source
->flags
&= ~G_HOOK_FLAG_ACTIVE
;
1016 * @source: a #GSource
1018 * Returns the numeric ID for a particular source. The ID of a source
1019 * is a positive integer which is unique within a particular main loop
1020 * context. The reverse
1021 * mapping from ID to source is done by g_main_context_find_source_by_id().
1023 * Return value: the ID (greater than 0) for the source
1026 g_source_get_id (GSource
*source
)
1030 g_return_val_if_fail (source
!= NULL
, 0);
1031 g_return_val_if_fail (source
->context
!= NULL
, 0);
1033 LOCK_CONTEXT (source
->context
);
1034 result
= source
->source_id
;
1035 UNLOCK_CONTEXT (source
->context
);
1041 * g_source_get_context:
1042 * @source: a #GSource
1044 * Gets the #GMainContext with which the source is associated.
1045 * Calling this function on a destroyed source is an error.
1047 * Return value: (transfer none): the #GMainContext with which the
1048 * source is associated, or %NULL if the context has not
1049 * yet been added to a source.
1052 g_source_get_context (GSource
*source
)
1054 g_return_val_if_fail (!SOURCE_DESTROYED (source
), NULL
);
1056 return source
->context
;
1060 * g_source_add_poll:
1061 * @source:a #GSource
1062 * @fd: a #GPollFD structure holding information about a file
1063 * descriptor to watch.
1065 * Adds a file descriptor to the set of file descriptors polled for
1066 * this source. This is usually combined with g_source_new() to add an
1067 * event source. The event source's check function will typically test
1068 * the @revents field in the #GPollFD struct and return %TRUE if events need
1072 g_source_add_poll (GSource
*source
,
1075 GMainContext
*context
;
1077 g_return_if_fail (source
!= NULL
);
1078 g_return_if_fail (fd
!= NULL
);
1079 g_return_if_fail (!SOURCE_DESTROYED (source
));
1081 context
= source
->context
;
1084 LOCK_CONTEXT (context
);
1086 source
->poll_fds
= g_slist_prepend (source
->poll_fds
, fd
);
1090 if (!SOURCE_BLOCKED (source
))
1091 g_main_context_add_poll_unlocked (context
, source
->priority
, fd
);
1092 UNLOCK_CONTEXT (context
);
1097 * g_source_remove_poll:
1098 * @source:a #GSource
1099 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1101 * Removes a file descriptor from the set of file descriptors polled for
1105 g_source_remove_poll (GSource
*source
,
1108 GMainContext
*context
;
1110 g_return_if_fail (source
!= NULL
);
1111 g_return_if_fail (fd
!= NULL
);
1112 g_return_if_fail (!SOURCE_DESTROYED (source
));
1114 context
= source
->context
;
1117 LOCK_CONTEXT (context
);
1119 source
->poll_fds
= g_slist_remove (source
->poll_fds
, fd
);
1123 if (!SOURCE_BLOCKED (source
))
1124 g_main_context_remove_poll_unlocked (context
, fd
);
1125 UNLOCK_CONTEXT (context
);
1130 * g_source_add_child_source:
1131 * @source:a #GSource
1132 * @child_source: a second #GSource that @source should "poll"
1134 * Adds @child_source to @source as a "polled" source; when @source is
1135 * added to a #GMainContext, @child_source will be automatically added
1136 * with the same priority, when @child_source is triggered, it will
1137 * cause @source to dispatch (in addition to calling its own
1138 * callback), and when @source is destroyed, it will destroy
1139 * @child_source as well. (@source will also still be dispatched if
1140 * its own prepare/check functions indicate that it is ready.)
1142 * If you don't need @child_source to do anything on its own when it
1143 * triggers, you can call g_source_set_dummy_callback() on it to set a
1144 * callback that does nothing (except return %TRUE if appropriate).
1146 * @source will hold a reference on @child_source while @child_source
1147 * is attached to it.
1152 g_source_add_child_source (GSource
*source
,
1153 GSource
*child_source
)
1155 GMainContext
*context
;
1157 g_return_if_fail (source
!= NULL
);
1158 g_return_if_fail (child_source
!= NULL
);
1159 g_return_if_fail (!SOURCE_DESTROYED (source
));
1160 g_return_if_fail (!SOURCE_DESTROYED (child_source
));
1161 g_return_if_fail (child_source
->context
== NULL
);
1162 g_return_if_fail (child_source
->priv
== NULL
|| child_source
->priv
->parent_source
== NULL
);
1164 context
= source
->context
;
1167 LOCK_CONTEXT (context
);
1170 source
->priv
= g_slice_new0 (GSourcePrivate
);
1171 if (!child_source
->priv
)
1172 child_source
->priv
= g_slice_new0 (GSourcePrivate
);
1174 source
->priv
->child_sources
= g_slist_prepend (source
->priv
->child_sources
,
1175 g_source_ref (child_source
));
1176 child_source
->priv
->parent_source
= source
;
1177 g_source_set_priority_unlocked (child_source
, NULL
, source
->priority
);
1181 UNLOCK_CONTEXT (context
);
1182 g_source_attach (child_source
, context
);
1187 * g_source_remove_child_source:
1188 * @source:a #GSource
1189 * @child_source: a #GSource previously passed to
1190 * g_source_add_child_source().
1192 * Detaches @child_source from @source and destroys it.
1197 g_source_remove_child_source (GSource
*source
,
1198 GSource
*child_source
)
1200 GMainContext
*context
;
1202 g_return_if_fail (source
!= NULL
);
1203 g_return_if_fail (child_source
!= NULL
);
1204 g_return_if_fail (child_source
->priv
!= NULL
&& child_source
->priv
->parent_source
== source
);
1205 g_return_if_fail (!SOURCE_DESTROYED (source
));
1206 g_return_if_fail (!SOURCE_DESTROYED (child_source
));
1208 context
= source
->context
;
1211 LOCK_CONTEXT (context
);
1213 source
->priv
->child_sources
= g_slist_remove (source
->priv
->child_sources
, child_source
);
1214 g_source_destroy_internal (child_source
, context
, TRUE
);
1215 g_source_unref_internal (child_source
, context
, TRUE
);
1218 UNLOCK_CONTEXT (context
);
1222 * g_source_set_callback_indirect:
1223 * @source: the source
1224 * @callback_data: pointer to callback data "object"
1225 * @callback_funcs: functions for reference counting @callback_data
1226 * and getting the callback and data
1228 * Sets the callback function storing the data as a refcounted callback
1229 * "object". This is used internally. Note that calling
1230 * g_source_set_callback_indirect() assumes
1231 * an initial reference count on @callback_data, and thus
1232 * @callback_funcs->unref will eventually be called once more
1233 * than @callback_funcs->ref.
1236 g_source_set_callback_indirect (GSource
*source
,
1237 gpointer callback_data
,
1238 GSourceCallbackFuncs
*callback_funcs
)
1240 GMainContext
*context
;
1241 gpointer old_cb_data
;
1242 GSourceCallbackFuncs
*old_cb_funcs
;
1244 g_return_if_fail (source
!= NULL
);
1245 g_return_if_fail (callback_funcs
!= NULL
|| callback_data
== NULL
);
1247 context
= source
->context
;
1250 LOCK_CONTEXT (context
);
1252 old_cb_data
= source
->callback_data
;
1253 old_cb_funcs
= source
->callback_funcs
;
1255 source
->callback_data
= callback_data
;
1256 source
->callback_funcs
= callback_funcs
;
1259 UNLOCK_CONTEXT (context
);
1262 old_cb_funcs
->unref (old_cb_data
);
1266 g_source_callback_ref (gpointer cb_data
)
1268 GSourceCallback
*callback
= cb_data
;
1270 callback
->ref_count
++;
1275 g_source_callback_unref (gpointer cb_data
)
1277 GSourceCallback
*callback
= cb_data
;
1279 callback
->ref_count
--;
1280 if (callback
->ref_count
== 0)
1282 if (callback
->notify
)
1283 callback
->notify (callback
->data
);
1289 g_source_callback_get (gpointer cb_data
,
1294 GSourceCallback
*callback
= cb_data
;
1296 *func
= callback
->func
;
1297 *data
= callback
->data
;
1300 static GSourceCallbackFuncs g_source_callback_funcs
= {
1301 g_source_callback_ref
,
1302 g_source_callback_unref
,
1303 g_source_callback_get
,
1307 * g_source_set_callback:
1308 * @source: the source
1309 * @func: a callback function
1310 * @data: the data to pass to callback function
1311 * @notify: a function to call when @data is no longer in use, or %NULL.
1313 * Sets the callback function for a source. The callback for a source is
1314 * called from the source's dispatch function.
1316 * The exact type of @func depends on the type of source; ie. you
1317 * should not count on @func being called with @data as its first
1320 * Typically, you won't use this function. Instead use functions specific
1321 * to the type of source you are using.
1324 g_source_set_callback (GSource
*source
,
1327 GDestroyNotify notify
)
1329 GSourceCallback
*new_callback
;
1331 g_return_if_fail (source
!= NULL
);
1333 new_callback
= g_new (GSourceCallback
, 1);
1335 new_callback
->ref_count
= 1;
1336 new_callback
->func
= func
;
1337 new_callback
->data
= data
;
1338 new_callback
->notify
= notify
;
1340 g_source_set_callback_indirect (source
, new_callback
, &g_source_callback_funcs
);
1345 * g_source_set_funcs:
1346 * @source: a #GSource
1347 * @funcs: the new #GSourceFuncs
1349 * Sets the source functions (can be used to override
1350 * default implementations) of an unattached source.
1355 g_source_set_funcs (GSource
*source
,
1356 GSourceFuncs
*funcs
)
1358 g_return_if_fail (source
!= NULL
);
1359 g_return_if_fail (source
->context
== NULL
);
1360 g_return_if_fail (source
->ref_count
> 0);
1361 g_return_if_fail (funcs
!= NULL
);
1363 source
->source_funcs
= funcs
;
1367 g_source_set_priority_unlocked (GSource
*source
,
1368 GMainContext
*context
,
1373 source
->priority
= priority
;
1377 /* Remove the source from the context's source and then
1378 * add it back so it is sorted in the correct place
1380 g_source_list_remove (source
, source
->context
);
1381 g_source_list_add (source
, source
->context
);
1383 if (!SOURCE_BLOCKED (source
))
1385 tmp_list
= source
->poll_fds
;
1388 g_main_context_remove_poll_unlocked (context
, tmp_list
->data
);
1389 g_main_context_add_poll_unlocked (context
, priority
, tmp_list
->data
);
1391 tmp_list
= tmp_list
->next
;
1396 if (source
->priv
&& source
->priv
->child_sources
)
1398 tmp_list
= source
->priv
->child_sources
;
1401 g_source_set_priority_unlocked (tmp_list
->data
, context
, priority
);
1402 tmp_list
= tmp_list
->next
;
1408 * g_source_set_priority:
1409 * @source: a #GSource
1410 * @priority: the new priority.
1412 * Sets the priority of a source. While the main loop is being run, a
1413 * source will be dispatched if it is ready to be dispatched and no
1414 * sources at a higher (numerically smaller) priority are ready to be
1418 g_source_set_priority (GSource
*source
,
1421 GMainContext
*context
;
1423 g_return_if_fail (source
!= NULL
);
1425 context
= source
->context
;
1428 LOCK_CONTEXT (context
);
1429 g_source_set_priority_unlocked (source
, context
, priority
);
1431 UNLOCK_CONTEXT (source
->context
);
1435 * g_source_get_priority:
1436 * @source: a #GSource
1438 * Gets the priority of a source.
1440 * Return value: the priority of the source
1443 g_source_get_priority (GSource
*source
)
1445 g_return_val_if_fail (source
!= NULL
, 0);
1447 return source
->priority
;
1451 * g_source_set_can_recurse:
1452 * @source: a #GSource
1453 * @can_recurse: whether recursion is allowed for this source
1455 * Sets whether a source can be called recursively. If @can_recurse is
1456 * %TRUE, then while the source is being dispatched then this source
1457 * will be processed normally. Otherwise, all processing of this
1458 * source is blocked until the dispatch function returns.
1461 g_source_set_can_recurse (GSource
*source
,
1462 gboolean can_recurse
)
1464 GMainContext
*context
;
1466 g_return_if_fail (source
!= NULL
);
1468 context
= source
->context
;
1471 LOCK_CONTEXT (context
);
1474 source
->flags
|= G_SOURCE_CAN_RECURSE
;
1476 source
->flags
&= ~G_SOURCE_CAN_RECURSE
;
1479 UNLOCK_CONTEXT (context
);
1483 * g_source_get_can_recurse:
1484 * @source: a #GSource
1486 * Checks whether a source is allowed to be called recursively.
1487 * see g_source_set_can_recurse().
1489 * Return value: whether recursion is allowed.
1492 g_source_get_can_recurse (GSource
*source
)
1494 g_return_val_if_fail (source
!= NULL
, FALSE
);
1496 return (source
->flags
& G_SOURCE_CAN_RECURSE
) != 0;
1501 * g_source_set_name:
1502 * @source: a #GSource
1503 * @name: debug name for the source
1505 * Sets a name for the source, used in debugging and profiling.
1506 * The name defaults to #NULL.
1508 * The source name should describe in a human-readable way
1509 * what the source does. For example, "X11 event queue"
1510 * or "GTK+ repaint idle handler" or whatever it is.
1512 * It is permitted to call this function multiple times, but is not
1513 * recommended due to the potential performance impact. For example,
1514 * one could change the name in the "check" function of a #GSourceFuncs
1515 * to include details like the event type in the source name.
1520 g_source_set_name (GSource
*source
,
1523 g_return_if_fail (source
!= NULL
);
1525 /* setting back to NULL is allowed, just because it's
1526 * weird if get_name can return NULL but you can't
1530 g_free (source
->name
);
1531 source
->name
= g_strdup (name
);
1535 * g_source_get_name:
1536 * @source: a #GSource
1538 * Gets a name for the source, used in debugging and profiling.
1539 * The name may be #NULL if it has never been set with
1540 * g_source_set_name().
1542 * Return value: the name of the source
1546 g_source_get_name (GSource
*source
)
1548 g_return_val_if_fail (source
!= NULL
, NULL
);
1550 return source
->name
;
1554 * g_source_set_name_by_id:
1555 * @tag: a #GSource ID
1556 * @name: debug name for the source
1558 * Sets the name of a source using its ID.
1560 * This is a convenience utility to set source names from the return
1561 * value of g_idle_add(), g_timeout_add(), etc.
1566 g_source_set_name_by_id (guint tag
,
1571 g_return_if_fail (tag
> 0);
1573 source
= g_main_context_find_source_by_id (NULL
, tag
);
1577 g_source_set_name (source
, name
);
1583 * @source: a #GSource
1585 * Increases the reference count on a source by one.
1587 * Return value: @source
1590 g_source_ref (GSource
*source
)
1592 GMainContext
*context
;
1594 g_return_val_if_fail (source
!= NULL
, NULL
);
1596 context
= source
->context
;
1599 LOCK_CONTEXT (context
);
1601 source
->ref_count
++;
1604 UNLOCK_CONTEXT (context
);
1609 /* g_source_unref() but possible to call within context lock
1612 g_source_unref_internal (GSource
*source
,
1613 GMainContext
*context
,
1616 gpointer old_cb_data
= NULL
;
1617 GSourceCallbackFuncs
*old_cb_funcs
= NULL
;
1619 g_return_if_fail (source
!= NULL
);
1621 if (!have_lock
&& context
)
1622 LOCK_CONTEXT (context
);
1624 source
->ref_count
--;
1625 if (source
->ref_count
== 0)
1627 old_cb_data
= source
->callback_data
;
1628 old_cb_funcs
= source
->callback_funcs
;
1630 source
->callback_data
= NULL
;
1631 source
->callback_funcs
= NULL
;
1635 if (!SOURCE_DESTROYED (source
))
1636 g_warning (G_STRLOC
": ref_count == 0, but source was still attached to a context!");
1637 g_source_list_remove (source
, context
);
1640 if (source
->source_funcs
->finalize
)
1643 UNLOCK_CONTEXT (context
);
1644 source
->source_funcs
->finalize (source
);
1646 LOCK_CONTEXT (context
);
1649 g_free (source
->name
);
1650 source
->name
= NULL
;
1652 g_slist_free (source
->poll_fds
);
1653 source
->poll_fds
= NULL
;
1657 g_slice_free (GSourcePrivate
, source
->priv
);
1658 source
->priv
= NULL
;
1664 if (!have_lock
&& context
)
1665 UNLOCK_CONTEXT (context
);
1670 UNLOCK_CONTEXT (context
);
1672 old_cb_funcs
->unref (old_cb_data
);
1675 LOCK_CONTEXT (context
);
1681 * @source: a #GSource
1683 * Decreases the reference count of a source by one. If the
1684 * resulting reference count is zero the source and associated
1685 * memory will be destroyed.
1688 g_source_unref (GSource
*source
)
1690 g_return_if_fail (source
!= NULL
);
1692 g_source_unref_internal (source
, source
->context
, FALSE
);
1696 * g_main_context_find_source_by_id:
1697 * @context: a #GMainContext (if %NULL, the default context will be used)
1698 * @source_id: the source ID, as returned by g_source_get_id().
1700 * Finds a #GSource given a pair of context and ID.
1702 * Return value: (transfer none): the #GSource if found, otherwise, %NULL
1705 g_main_context_find_source_by_id (GMainContext
*context
,
1710 g_return_val_if_fail (source_id
> 0, NULL
);
1712 if (context
== NULL
)
1713 context
= g_main_context_default ();
1715 LOCK_CONTEXT (context
);
1717 source
= context
->source_list
;
1720 if (!SOURCE_DESTROYED (source
) &&
1721 source
->source_id
== source_id
)
1723 source
= source
->next
;
1726 UNLOCK_CONTEXT (context
);
1732 * g_main_context_find_source_by_funcs_user_data:
1733 * @context: a #GMainContext (if %NULL, the default context will be used).
1734 * @funcs: the @source_funcs passed to g_source_new().
1735 * @user_data: the user data from the callback.
1737 * Finds a source with the given source functions and user data. If
1738 * multiple sources exist with the same source function and user data,
1739 * the first one found will be returned.
1741 * Return value: (transfer none): the source, if one was found, otherwise %NULL
1744 g_main_context_find_source_by_funcs_user_data (GMainContext
*context
,
1745 GSourceFuncs
*funcs
,
1750 g_return_val_if_fail (funcs
!= NULL
, NULL
);
1752 if (context
== NULL
)
1753 context
= g_main_context_default ();
1755 LOCK_CONTEXT (context
);
1757 source
= context
->source_list
;
1760 if (!SOURCE_DESTROYED (source
) &&
1761 source
->source_funcs
== funcs
&&
1762 source
->callback_funcs
)
1764 GSourceFunc callback
;
1765 gpointer callback_data
;
1767 source
->callback_funcs
->get (source
->callback_data
, source
, &callback
, &callback_data
);
1769 if (callback_data
== user_data
)
1772 source
= source
->next
;
1775 UNLOCK_CONTEXT (context
);
1781 * g_main_context_find_source_by_user_data:
1782 * @context: a #GMainContext
1783 * @user_data: the user_data for the callback.
1785 * Finds a source with the given user data for the callback. If
1786 * multiple sources exist with the same user data, the first
1787 * one found will be returned.
1789 * Return value: (transfer none): the source, if one was found, otherwise %NULL
1792 g_main_context_find_source_by_user_data (GMainContext
*context
,
1797 if (context
== NULL
)
1798 context
= g_main_context_default ();
1800 LOCK_CONTEXT (context
);
1802 source
= context
->source_list
;
1805 if (!SOURCE_DESTROYED (source
) &&
1806 source
->callback_funcs
)
1808 GSourceFunc callback
;
1809 gpointer callback_data
= NULL
;
1811 source
->callback_funcs
->get (source
->callback_data
, source
, &callback
, &callback_data
);
1813 if (callback_data
== user_data
)
1816 source
= source
->next
;
1819 UNLOCK_CONTEXT (context
);
1826 * @tag: the ID of the source to remove.
1828 * Removes the source with the given id from the default main context.
1830 * a #GSource is given by g_source_get_id(), or will be returned by the
1831 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1832 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1833 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1835 * See also g_source_destroy(). You must use g_source_destroy() for sources
1836 * added to a non-default main context.
1838 * Return value: %TRUE if the source was found and removed.
1841 g_source_remove (guint tag
)
1845 g_return_val_if_fail (tag
> 0, FALSE
);
1847 source
= g_main_context_find_source_by_id (NULL
, tag
);
1849 g_source_destroy (source
);
1851 return source
!= NULL
;
1855 * g_source_remove_by_user_data:
1856 * @user_data: the user_data for the callback.
1858 * Removes a source from the default main loop context given the user
1859 * data for the callback. If multiple sources exist with the same user
1860 * data, only one will be destroyed.
1862 * Return value: %TRUE if a source was found and removed.
1865 g_source_remove_by_user_data (gpointer user_data
)
1869 source
= g_main_context_find_source_by_user_data (NULL
, user_data
);
1872 g_source_destroy (source
);
1880 * g_source_remove_by_funcs_user_data:
1881 * @funcs: The @source_funcs passed to g_source_new()
1882 * @user_data: the user data for the callback
1884 * Removes a source from the default main loop context given the
1885 * source functions and user data. If multiple sources exist with the
1886 * same source functions and user data, only one will be destroyed.
1888 * Return value: %TRUE if a source was found and removed.
1891 g_source_remove_by_funcs_user_data (GSourceFuncs
*funcs
,
1896 g_return_val_if_fail (funcs
!= NULL
, FALSE
);
1898 source
= g_main_context_find_source_by_funcs_user_data (NULL
, funcs
, user_data
);
1901 g_source_destroy (source
);
1909 * g_get_current_time:
1910 * @result: #GTimeVal structure in which to store current time.
1912 * Equivalent to the UNIX gettimeofday() function, but portable.
1914 * You may find g_get_real_time() to be more convenient.
1917 g_get_current_time (GTimeVal
*result
)
1922 g_return_if_fail (result
!= NULL
);
1924 /*this is required on alpha, there the timeval structs are int's
1925 not longs and a cast only would fail horribly*/
1926 gettimeofday (&r
, NULL
);
1927 result
->tv_sec
= r
.tv_sec
;
1928 result
->tv_usec
= r
.tv_usec
;
1933 g_return_if_fail (result
!= NULL
);
1935 GetSystemTimeAsFileTime (&ft
);
1936 memmove (&time64
, &ft
, sizeof (FILETIME
));
1938 /* Convert from 100s of nanoseconds since 1601-01-01
1939 * to Unix epoch. Yes, this is Y2038 unsafe.
1941 time64
-= G_GINT64_CONSTANT (116444736000000000);
1944 result
->tv_sec
= time64
/ 1000000;
1945 result
->tv_usec
= time64
% 1000000;
1952 * Queries the system wall-clock time.
1954 * This call is functionally equivalent to g_get_current_time() except
1955 * that the return value is often more convenient than dealing with a
1958 * You should only use this call if you are actually interested in the real
1959 * wall-clock time. g_get_monotonic_time() is probably more useful for
1960 * measuring intervals.
1962 * Returns: the number of microseconds since January 1, 1970 UTC.
1967 g_get_real_time (void)
1971 g_get_current_time (&tv
);
1973 return (((gint64
) tv
.tv_sec
) * 1000000) + tv
.tv_usec
;
1977 static ULONGLONG (*g_GetTickCount64
) (void) = NULL
;
1978 static guint32 g_win32_tick_epoch
= 0;
1980 G_GNUC_INTERNAL
void
1981 g_clock_win32_init (void)
1985 g_GetTickCount64
= NULL
;
1986 kernel32
= GetModuleHandle ("KERNEL32.DLL");
1987 if (kernel32
!= NULL
)
1988 g_GetTickCount64
= (void *) GetProcAddress (kernel32
, "GetTickCount64");
1989 g_win32_tick_epoch
= ((guint32
)GetTickCount()) >> 31;
1994 * g_get_monotonic_time:
1996 * Queries the system monotonic time, if available.
1998 * On POSIX systems with clock_gettime() and <literal>CLOCK_MONOTONIC</literal> this call
1999 * is a very shallow wrapper for that. Otherwise, we make a best effort
2000 * that probably involves returning the wall clock time (with at least
2001 * microsecond accuracy, subject to the limitations of the OS kernel).
2003 * It's important to note that POSIX <literal>CLOCK_MONOTONIC</literal> does
2004 * not count time spent while the machine is suspended.
2006 * On Windows, "limitations of the OS kernel" is a rather substantial
2007 * statement. Depending on the configuration of the system, the wall
2008 * clock time is updated as infrequently as 64 times a second (which
2009 * is approximately every 16ms). Also, on XP (but not on Vista or later)
2010 * the monotonic clock is locally monotonic, but may differ in exact
2011 * value between processes due to timer wrap handling.
2013 * Returns: the monotonic time, in microseconds
2018 g_get_monotonic_time (void)
2020 #ifdef HAVE_CLOCK_GETTIME
2021 /* librt clock_gettime() is our first choice */
2024 #ifdef CLOCK_MONOTONIC
2025 clock_gettime (CLOCK_MONOTONIC
, &ts
);
2027 clock_gettime (CLOCK_REALTIME
, &ts
);
2030 /* In theory monotonic time can have any epoch.
2032 * glib presently assumes the following:
2034 * 1) The epoch comes some time after the birth of Jesus of Nazareth, but
2035 * not more than 10000 years later.
2037 * 2) The current time also falls sometime within this range.
2039 * These two reasonable assumptions leave us with a maximum deviation from
2040 * the epoch of 10000 years, or 315569520000000000 seconds.
2042 * If we restrict ourselves to this range then the number of microseconds
2043 * will always fit well inside the constraints of a int64 (by a factor of
2046 * If you actually hit the following assertion, probably you should file a
2047 * bug against your operating system for being excessively silly.
2049 g_assert (G_GINT64_CONSTANT (-315569520000000000) < ts
.tv_sec
&&
2050 ts
.tv_sec
< G_GINT64_CONSTANT (315569520000000000));
2052 return (((gint64
) ts
.tv_sec
) * 1000000) + (ts
.tv_nsec
/ 1000);
2054 #elif defined (G_OS_WIN32)
2058 /* There are four sources for the monotonic time on Windows:
2060 * Three are based on a (1 msec accuracy, but only read periodically) clock chip:
2061 * - GetTickCount (GTC)
2062 * 32bit msec counter, updated each ~15msec, wraps in ~50 days
2063 * - GetTickCount64 (GTC64)
2064 * Same as GetTickCount, but extended to 64bit, so no wrap
2065 * Only availible in Vista or later
2066 * - timeGetTime (TGT)
2067 * similar to GetTickCount by default: 15msec, 50 day wrap.
2068 * available in winmm.dll (thus known as the multimedia timers)
2069 * However apps can raise the system timer clock frequency using timeBeginPeriod()
2070 * increasing the accuracy up to 1 msec, at a cost in general system performance
2073 * One is based on high precision clocks:
2074 * - QueryPrecisionCounter (QPC)
2075 * This has much higher accuracy, but is not guaranteed monotonic, and
2076 * has lots of complications like clock jumps and different times on different
2077 * CPUs. It also has lower long term accuracy (i.e. it will drift compared to
2078 * the low precision clocks.
2080 * Additionally, the precision available in the timer-based wakeup such as
2081 * MsgWaitForMultipleObjectsEx (which is what the mainloop is based on) is based
2082 * on the TGT resolution, so by default it is ~15msec, but can be increased by apps.
2084 * The QPC timer has too many issues to be used as is. The only way it could be used
2085 * is to use it to interpolate the lower precision clocks. Firefox does something like
2087 * https://bugzilla.mozilla.org/show_bug.cgi?id=363258
2089 * However this seems quite complicated, so we're not doing this right now.
2091 * The approach we take instead is to use the TGT timer, extending it to 64bit
2092 * either by using the GTC64 value, or if that is not availible, a process local
2093 * time epoch that we increment when we detect a timer wrap (assumes that we read
2094 * the time at least once every 50 days).
2097 * - We have a globally consistent monotonic clock on Vista and later
2098 * - We have a locally monotonic clock on XP
2099 * - Apps that need higher precision in timeouts and clock reads can call
2100 * timeBeginPeriod() to increase it as much as they want
2103 if (g_GetTickCount64
!= NULL
)
2105 guint32 ticks_as_32bit
;
2107 ticks
= g_GetTickCount64 ();
2108 ticks32
= timeGetTime();
2110 /* GTC64 and TGT are sampled at different times, however they
2111 * have the same base and source (msecs since system boot).
2112 * They can differ by as much as -16 to +16 msecs.
2113 * We can't just inject the low bits into the 64bit counter
2114 * as one of the counters can have wrapped in 32bit space and
2115 * the other not. Instead we calculate the signed difference
2116 * in 32bit space and apply that difference to the 64bit counter.
2118 ticks_as_32bit
= (guint32
)ticks
;
2120 /* We could do some 2's complement hack, but we play it safe */
2121 if (ticks32
- ticks_as_32bit
<= G_MAXINT32
)
2122 ticks
+= ticks32
- ticks_as_32bit
;
2124 ticks
-= ticks_as_32bit
- ticks32
;
2130 epoch
= g_atomic_int_get (&g_win32_tick_epoch
);
2132 /* Must read ticks after the epoch. Then we're guaranteed
2133 * that the ticks value we read is higher or equal to any
2134 * previous ones that lead to the writing of the epoch.
2136 ticks32
= timeGetTime();
2138 /* We store the MSB of the current time as the LSB
2139 * of the epoch. Comparing these bits lets us detect when
2140 * the 32bit counter has wrapped so we can increase the
2143 * This will work as long as this function is called at
2144 * least once every ~24 days, which is half the wrap time
2145 * of a 32bit msec counter. I think this is pretty likely.
2147 * Note that g_win32_tick_epoch is a process local state,
2148 * so the monotonic clock will not be the same between
2151 if ((ticks32
>> 31) != (epoch
& 1))
2154 g_atomic_int_set (&g_win32_tick_epoch
, epoch
);
2158 ticks
= (guint64
)ticks32
| ((guint64
)epoch
) << 31;
2161 return ticks
* 1000;
2163 #else /* !HAVE_CLOCK_GETTIME && ! G_OS_WIN32*/
2167 g_get_current_time (&tv
);
2169 return (((gint64
) tv
.tv_sec
) * 1000000) + tv
.tv_usec
;
2174 g_main_dispatch_free (gpointer dispatch
)
2176 g_slice_free (GMainDispatch
, dispatch
);
2179 /* Running the main loop */
2181 static GMainDispatch
*
2184 static GPrivate depth_private
= G_PRIVATE_INIT (g_main_dispatch_free
);
2185 GMainDispatch
*dispatch
;
2187 dispatch
= g_private_get (&depth_private
);
2191 dispatch
= g_slice_new0 (GMainDispatch
);
2192 g_private_set (&depth_private
, dispatch
);
2201 * Returns the depth of the stack of calls to
2202 * g_main_context_dispatch() on any #GMainContext in the current thread.
2203 * That is, when called from the toplevel, it gives 0. When
2204 * called from within a callback from g_main_context_iteration()
2205 * (or g_main_loop_run(), etc.) it returns 1. When called from within
2206 * a callback to a recursive call to g_main_context_iteration(),
2207 * it returns 2. And so forth.
2209 * This function is useful in a situation like the following:
2210 * Imagine an extremely simple "garbage collected" system.
2213 * static GList *free_list;
2216 * allocate_memory (gsize size)
2218 * gpointer result = g_malloc (size);
2219 * free_list = g_list_prepend (free_list, result);
2224 * free_allocated_memory (void)
2227 * for (l = free_list; l; l = l->next);
2229 * g_list_free (free_list);
2237 * g_main_context_iteration (NULL, TRUE);
2238 * free_allocated_memory();
2242 * This works from an application, however, if you want to do the same
2243 * thing from a library, it gets more difficult, since you no longer
2244 * control the main loop. You might think you can simply use an idle
2245 * function to make the call to free_allocated_memory(), but that
2246 * doesn't work, since the idle function could be called from a
2247 * recursive callback. This can be fixed by using g_main_depth()
2251 * allocate_memory (gsize size)
2253 * FreeListBlock *block = g_new (FreeListBlock, 1);
2254 * block->mem = g_malloc (size);
2255 * block->depth = g_main_depth ();
2256 * free_list = g_list_prepend (free_list, block);
2257 * return block->mem;
2261 * free_allocated_memory (void)
2265 * int depth = g_main_depth ();
2266 * for (l = free_list; l; );
2268 * GList *next = l->next;
2269 * FreeListBlock *block = l->data;
2270 * if (block->depth > depth)
2272 * g_free (block->mem);
2274 * free_list = g_list_delete_link (free_list, l);
2282 * There is a temptation to use g_main_depth() to solve
2283 * problems with reentrancy. For instance, while waiting for data
2284 * to be received from the network in response to a menu item,
2285 * the menu item might be selected again. It might seem that
2286 * one could make the menu item's callback return immediately
2287 * and do nothing if g_main_depth() returns a value greater than 1.
2288 * However, this should be avoided since the user then sees selecting
2289 * the menu item do nothing. Furthermore, you'll find yourself adding
2290 * these checks all over your code, since there are doubtless many,
2291 * many things that the user could do. Instead, you can use the
2292 * following techniques:
2297 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
2298 * the user from interacting with elements while the main
2299 * loop is recursing.
2304 * Avoid main loop recursion in situations where you can't handle
2305 * arbitrary callbacks. Instead, structure your code so that you
2306 * simply return to the main loop and then get called again when
2307 * there is more work to do.
2312 * Return value: The main loop recursion level in the current thread
2317 GMainDispatch
*dispatch
= get_dispatch ();
2318 return dispatch
->depth
;
2322 * g_main_current_source:
2324 * Returns the currently firing source for this thread.
2326 * Return value: (transfer none): The currently firing source or %NULL.
2331 g_main_current_source (void)
2333 GMainDispatch
*dispatch
= get_dispatch ();
2334 return dispatch
->dispatching_sources
? dispatch
->dispatching_sources
->data
: NULL
;
2338 * g_source_is_destroyed:
2339 * @source: a #GSource
2341 * Returns whether @source has been destroyed.
2343 * This is important when you operate upon your objects
2344 * from within idle handlers, but may have freed the object
2345 * before the dispatch of your idle handler.
2349 * idle_callback (gpointer data)
2351 * SomeWidget *self = data;
2353 * GDK_THREADS_ENTER (<!-- -->);
2354 * /<!-- -->* do stuff with self *<!-- -->/
2355 * self->idle_id = 0;
2356 * GDK_THREADS_LEAVE (<!-- -->);
2358 * return G_SOURCE_REMOVE;
2362 * some_widget_do_stuff_later (SomeWidget *self)
2364 * self->idle_id = g_idle_add (idle_callback, self);
2368 * some_widget_finalize (GObject *object)
2370 * SomeWidget *self = SOME_WIDGET (object);
2372 * if (self->idle_id)
2373 * g_source_remove (self->idle_id);
2375 * G_OBJECT_CLASS (parent_class)->finalize (object);
2379 * This will fail in a multi-threaded application if the
2380 * widget is destroyed before the idle handler fires due
2381 * to the use after free in the callback. A solution, to
2382 * this particular problem, is to check to if the source
2383 * has already been destroy within the callback.
2387 * idle_callback (gpointer data)
2389 * SomeWidget *self = data;
2391 * GDK_THREADS_ENTER ();
2392 * if (!g_source_is_destroyed (g_main_current_source ()))
2394 * /<!-- -->* do stuff with self *<!-- -->/
2396 * GDK_THREADS_LEAVE ();
2402 * Return value: %TRUE if the source has been destroyed
2407 g_source_is_destroyed (GSource
*source
)
2409 return SOURCE_DESTROYED (source
);
2412 /* Temporarily remove all this source's file descriptors from the
2413 * poll(), so that if data comes available for one of the file descriptors
2414 * we don't continually spin in the poll()
2416 /* HOLDS: source->context's lock */
2418 block_source (GSource
*source
)
2422 g_return_if_fail (!SOURCE_BLOCKED (source
));
2424 tmp_list
= source
->poll_fds
;
2427 g_main_context_remove_poll_unlocked (source
->context
, tmp_list
->data
);
2428 tmp_list
= tmp_list
->next
;
2432 /* HOLDS: source->context's lock */
2434 unblock_source (GSource
*source
)
2438 g_return_if_fail (!SOURCE_BLOCKED (source
)); /* Source already unblocked */
2439 g_return_if_fail (!SOURCE_DESTROYED (source
));
2441 tmp_list
= source
->poll_fds
;
2444 g_main_context_add_poll_unlocked (source
->context
, source
->priority
, tmp_list
->data
);
2445 tmp_list
= tmp_list
->next
;
2449 /* HOLDS: context's lock */
2451 g_main_dispatch (GMainContext
*context
)
2453 GMainDispatch
*current
= get_dispatch ();
2456 for (i
= 0; i
< context
->pending_dispatches
->len
; i
++)
2458 GSource
*source
= context
->pending_dispatches
->pdata
[i
];
2460 context
->pending_dispatches
->pdata
[i
] = NULL
;
2463 source
->flags
&= ~G_SOURCE_READY
;
2465 if (!SOURCE_DESTROYED (source
))
2467 gboolean was_in_call
;
2468 gpointer user_data
= NULL
;
2469 GSourceFunc callback
= NULL
;
2470 GSourceCallbackFuncs
*cb_funcs
;
2472 gboolean need_destroy
;
2474 gboolean (*dispatch
) (GSource
*,
2477 GSList current_source_link
;
2479 dispatch
= source
->source_funcs
->dispatch
;
2480 cb_funcs
= source
->callback_funcs
;
2481 cb_data
= source
->callback_data
;
2484 cb_funcs
->ref (cb_data
);
2486 if ((source
->flags
& G_SOURCE_CAN_RECURSE
) == 0)
2487 block_source (source
);
2489 was_in_call
= source
->flags
& G_HOOK_FLAG_IN_CALL
;
2490 source
->flags
|= G_HOOK_FLAG_IN_CALL
;
2493 cb_funcs
->get (cb_data
, source
, &callback
, &user_data
);
2495 UNLOCK_CONTEXT (context
);
2498 /* The on-stack allocation of the GSList is unconventional, but
2499 * we know that the lifetime of the link is bounded to this
2500 * function as the link is kept in a thread specific list and
2501 * not manipulated outside of this function and its descendants.
2502 * Avoiding the overhead of a g_slist_alloc() is useful as many
2503 * applications do little more than dispatch events.
2505 * This is a performance hack - do not revert to g_slist_prepend()!
2507 current_source_link
.data
= source
;
2508 current_source_link
.next
= current
->dispatching_sources
;
2509 current
->dispatching_sources
= ¤t_source_link
;
2510 need_destroy
= ! dispatch (source
,
2513 g_assert (current
->dispatching_sources
== ¤t_source_link
);
2514 current
->dispatching_sources
= current_source_link
.next
;
2518 cb_funcs
->unref (cb_data
);
2520 LOCK_CONTEXT (context
);
2523 source
->flags
&= ~G_HOOK_FLAG_IN_CALL
;
2525 if ((source
->flags
& G_SOURCE_CAN_RECURSE
) == 0 &&
2526 !SOURCE_DESTROYED (source
))
2527 unblock_source (source
);
2529 /* Note: this depends on the fact that we can't switch
2530 * sources from one main context to another
2532 if (need_destroy
&& !SOURCE_DESTROYED (source
))
2534 g_assert (source
->context
== context
);
2535 g_source_destroy_internal (source
, context
, TRUE
);
2539 SOURCE_UNREF (source
, context
);
2542 g_ptr_array_set_size (context
->pending_dispatches
, 0);
2545 /* Holds context's lock */
2546 static inline GSource
*
2547 next_valid_source (GMainContext
*context
,
2550 GSource
*new_source
= source
? source
->next
: context
->source_list
;
2554 if (!SOURCE_DESTROYED (new_source
))
2556 new_source
->ref_count
++;
2560 new_source
= new_source
->next
;
2564 SOURCE_UNREF (source
, context
);
2570 * g_main_context_acquire:
2571 * @context: a #GMainContext
2573 * Tries to become the owner of the specified context.
2574 * If some other thread is the owner of the context,
2575 * returns %FALSE immediately. Ownership is properly
2576 * recursive: the owner can require ownership again
2577 * and will release ownership when g_main_context_release()
2578 * is called as many times as g_main_context_acquire().
2580 * You must be the owner of a context before you
2581 * can call g_main_context_prepare(), g_main_context_query(),
2582 * g_main_context_check(), g_main_context_dispatch().
2584 * Return value: %TRUE if the operation succeeded, and
2585 * this thread is now the owner of @context.
2588 g_main_context_acquire (GMainContext
*context
)
2590 gboolean result
= FALSE
;
2591 GThread
*self
= G_THREAD_SELF
;
2593 if (context
== NULL
)
2594 context
= g_main_context_default ();
2596 LOCK_CONTEXT (context
);
2598 if (!context
->owner
)
2600 context
->owner
= self
;
2601 g_assert (context
->owner_count
== 0);
2604 if (context
->owner
== self
)
2606 context
->owner_count
++;
2610 UNLOCK_CONTEXT (context
);
2616 * g_main_context_release:
2617 * @context: a #GMainContext
2619 * Releases ownership of a context previously acquired by this thread
2620 * with g_main_context_acquire(). If the context was acquired multiple
2621 * times, the ownership will be released only when g_main_context_release()
2622 * is called as many times as it was acquired.
2625 g_main_context_release (GMainContext
*context
)
2627 if (context
== NULL
)
2628 context
= g_main_context_default ();
2630 LOCK_CONTEXT (context
);
2632 context
->owner_count
--;
2633 if (context
->owner_count
== 0)
2635 context
->owner
= NULL
;
2637 if (context
->waiters
)
2639 GMainWaiter
*waiter
= context
->waiters
->data
;
2640 gboolean loop_internal_waiter
= (waiter
->mutex
== &context
->mutex
);
2641 context
->waiters
= g_slist_delete_link (context
->waiters
,
2643 if (!loop_internal_waiter
)
2644 g_mutex_lock (waiter
->mutex
);
2646 g_cond_signal (waiter
->cond
);
2648 if (!loop_internal_waiter
)
2649 g_mutex_unlock (waiter
->mutex
);
2653 UNLOCK_CONTEXT (context
);
2657 * g_main_context_wait:
2658 * @context: a #GMainContext
2659 * @cond: a condition variable
2660 * @mutex: a mutex, currently held
2662 * Tries to become the owner of the specified context,
2663 * as with g_main_context_acquire(). But if another thread
2664 * is the owner, atomically drop @mutex and wait on @cond until
2665 * that owner releases ownership or until @cond is signaled, then
2666 * try again (once) to become the owner.
2668 * Return value: %TRUE if the operation succeeded, and
2669 * this thread is now the owner of @context.
2672 g_main_context_wait (GMainContext
*context
,
2676 gboolean result
= FALSE
;
2677 GThread
*self
= G_THREAD_SELF
;
2678 gboolean loop_internal_waiter
;
2680 if (context
== NULL
)
2681 context
= g_main_context_default ();
2683 loop_internal_waiter
= (mutex
== &context
->mutex
);
2685 if (!loop_internal_waiter
)
2686 LOCK_CONTEXT (context
);
2688 if (context
->owner
&& context
->owner
!= self
)
2693 waiter
.mutex
= mutex
;
2695 context
->waiters
= g_slist_append (context
->waiters
, &waiter
);
2697 if (!loop_internal_waiter
)
2698 UNLOCK_CONTEXT (context
);
2699 g_cond_wait (cond
, mutex
);
2700 if (!loop_internal_waiter
)
2701 LOCK_CONTEXT (context
);
2703 context
->waiters
= g_slist_remove (context
->waiters
, &waiter
);
2706 if (!context
->owner
)
2708 context
->owner
= self
;
2709 g_assert (context
->owner_count
== 0);
2712 if (context
->owner
== self
)
2714 context
->owner_count
++;
2718 if (!loop_internal_waiter
)
2719 UNLOCK_CONTEXT (context
);
2725 * g_main_context_prepare:
2726 * @context: a #GMainContext
2727 * @priority: location to store priority of highest priority
2728 * source already ready.
2730 * Prepares to poll sources within a main loop. The resulting information
2731 * for polling is determined by calling g_main_context_query ().
2733 * Return value: %TRUE if some source is ready to be dispatched
2737 g_main_context_prepare (GMainContext
*context
,
2742 gint current_priority
= G_MAXINT
;
2745 if (context
== NULL
)
2746 context
= g_main_context_default ();
2748 LOCK_CONTEXT (context
);
2750 context
->time_is_fresh
= FALSE
;
2752 if (context
->in_check_or_prepare
)
2754 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2755 "prepare() member.");
2756 UNLOCK_CONTEXT (context
);
2761 /* If recursing, finish up current dispatch, before starting over */
2762 if (context
->pending_dispatches
)
2765 g_main_dispatch (context
, ¤t_time
);
2767 UNLOCK_CONTEXT (context
);
2772 /* If recursing, clear list of pending dispatches */
2774 for (i
= 0; i
< context
->pending_dispatches
->len
; i
++)
2776 if (context
->pending_dispatches
->pdata
[i
])
2777 SOURCE_UNREF ((GSource
*)context
->pending_dispatches
->pdata
[i
], context
);
2779 g_ptr_array_set_size (context
->pending_dispatches
, 0);
2781 /* Prepare all sources */
2783 context
->timeout
= -1;
2785 source
= next_valid_source (context
, NULL
);
2788 gint source_timeout
= -1;
2790 if ((n_ready
> 0) && (source
->priority
> current_priority
))
2792 SOURCE_UNREF (source
, context
);
2795 if (SOURCE_BLOCKED (source
))
2798 if (!(source
->flags
& G_SOURCE_READY
))
2801 gboolean (*prepare
) (GSource
*source
,
2804 prepare
= source
->source_funcs
->prepare
;
2805 context
->in_check_or_prepare
++;
2806 UNLOCK_CONTEXT (context
);
2808 result
= (*prepare
) (source
, &source_timeout
);
2810 LOCK_CONTEXT (context
);
2811 context
->in_check_or_prepare
--;
2815 GSource
*ready_source
= source
;
2817 while (ready_source
)
2819 ready_source
->flags
|= G_SOURCE_READY
;
2820 ready_source
= ready_source
->priv
? ready_source
->priv
->parent_source
: NULL
;
2825 if (source
->flags
& G_SOURCE_READY
)
2828 current_priority
= source
->priority
;
2829 context
->timeout
= 0;
2832 if (source_timeout
>= 0)
2834 if (context
->timeout
< 0)
2835 context
->timeout
= source_timeout
;
2837 context
->timeout
= MIN (context
->timeout
, source_timeout
);
2841 source
= next_valid_source (context
, source
);
2844 UNLOCK_CONTEXT (context
);
2847 *priority
= current_priority
;
2849 return (n_ready
> 0);
2853 * g_main_context_query:
2854 * @context: a #GMainContext
2855 * @max_priority: maximum priority source to check
2856 * @timeout_: (out): location to store timeout to be used in polling
2857 * @fds: (out caller-allocates) (array length=n_fds): location to
2858 * store #GPollFD records that need to be polled.
2859 * @n_fds: length of @fds.
2861 * Determines information necessary to poll this main loop.
2863 * Return value: the number of records actually stored in @fds,
2864 * or, if more than @n_fds records need to be stored, the number
2865 * of records that need to be stored.
2868 g_main_context_query (GMainContext
*context
,
2877 LOCK_CONTEXT (context
);
2879 pollrec
= context
->poll_records
;
2881 while (pollrec
&& max_priority
>= pollrec
->priority
)
2883 /* We need to include entries with fd->events == 0 in the array because
2884 * otherwise if the application changes fd->events behind our back and
2885 * makes it non-zero, we'll be out of sync when we check the fds[] array.
2886 * (Changing fd->events after adding an FD wasn't an anticipated use of
2887 * this API, but it occurs in practice.) */
2890 fds
[n_poll
].fd
= pollrec
->fd
->fd
;
2891 /* In direct contradiction to the Unix98 spec, IRIX runs into
2892 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2893 * flags in the events field of the pollfd while it should
2894 * just ignoring them. So we mask them out here.
2896 fds
[n_poll
].events
= pollrec
->fd
->events
& ~(G_IO_ERR
|G_IO_HUP
|G_IO_NVAL
);
2897 fds
[n_poll
].revents
= 0;
2900 pollrec
= pollrec
->next
;
2904 context
->poll_changed
= FALSE
;
2908 *timeout
= context
->timeout
;
2910 context
->time_is_fresh
= FALSE
;
2913 UNLOCK_CONTEXT (context
);
2919 * g_main_context_check:
2920 * @context: a #GMainContext
2921 * @max_priority: the maximum numerical priority of sources to check
2922 * @fds: (array length=n_fds): array of #GPollFD's that was passed to
2923 * the last call to g_main_context_query()
2924 * @n_fds: return value of g_main_context_query()
2926 * Passes the results of polling back to the main loop.
2928 * Return value: %TRUE if some sources are ready to be dispatched.
2931 g_main_context_check (GMainContext
*context
,
2941 LOCK_CONTEXT (context
);
2943 if (context
->in_check_or_prepare
)
2945 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2946 "prepare() member.");
2947 UNLOCK_CONTEXT (context
);
2951 if (context
->wake_up_rec
.events
)
2952 g_wakeup_acknowledge (context
->wakeup
);
2954 /* If the set of poll file descriptors changed, bail out
2955 * and let the main loop rerun
2957 if (context
->poll_changed
)
2959 UNLOCK_CONTEXT (context
);
2963 pollrec
= context
->poll_records
;
2967 if (pollrec
->fd
->events
)
2968 pollrec
->fd
->revents
= fds
[i
].revents
;
2970 pollrec
= pollrec
->next
;
2974 source
= next_valid_source (context
, NULL
);
2977 if ((n_ready
> 0) && (source
->priority
> max_priority
))
2979 SOURCE_UNREF (source
, context
);
2982 if (SOURCE_BLOCKED (source
))
2985 if (!(source
->flags
& G_SOURCE_READY
))
2988 gboolean (*check
) (GSource
*source
);
2990 check
= source
->source_funcs
->check
;
2992 context
->in_check_or_prepare
++;
2993 UNLOCK_CONTEXT (context
);
2995 result
= (*check
) (source
);
2997 LOCK_CONTEXT (context
);
2998 context
->in_check_or_prepare
--;
3002 GSource
*ready_source
= source
;
3004 while (ready_source
)
3006 ready_source
->flags
|= G_SOURCE_READY
;
3007 ready_source
= ready_source
->priv
? ready_source
->priv
->parent_source
: NULL
;
3012 if (source
->flags
& G_SOURCE_READY
)
3014 source
->ref_count
++;
3015 g_ptr_array_add (context
->pending_dispatches
, source
);
3019 /* never dispatch sources with less priority than the first
3020 * one we choose to dispatch
3022 max_priority
= source
->priority
;
3026 source
= next_valid_source (context
, source
);
3029 UNLOCK_CONTEXT (context
);
3035 * g_main_context_dispatch:
3036 * @context: a #GMainContext
3038 * Dispatches all pending sources.
3041 g_main_context_dispatch (GMainContext
*context
)
3043 LOCK_CONTEXT (context
);
3045 if (context
->pending_dispatches
->len
> 0)
3047 g_main_dispatch (context
);
3050 UNLOCK_CONTEXT (context
);
3053 /* HOLDS context lock */
3055 g_main_context_iterate (GMainContext
*context
,
3062 gboolean some_ready
;
3063 gint nfds
, allocated_nfds
;
3064 GPollFD
*fds
= NULL
;
3066 UNLOCK_CONTEXT (context
);
3068 if (!g_main_context_acquire (context
))
3070 gboolean got_ownership
;
3072 LOCK_CONTEXT (context
);
3077 got_ownership
= g_main_context_wait (context
,
3085 LOCK_CONTEXT (context
);
3087 if (!context
->cached_poll_array
)
3089 context
->cached_poll_array_size
= context
->n_poll_records
;
3090 context
->cached_poll_array
= g_new (GPollFD
, context
->n_poll_records
);
3093 allocated_nfds
= context
->cached_poll_array_size
;
3094 fds
= context
->cached_poll_array
;
3096 UNLOCK_CONTEXT (context
);
3098 g_main_context_prepare (context
, &max_priority
);
3100 while ((nfds
= g_main_context_query (context
, max_priority
, &timeout
, fds
,
3101 allocated_nfds
)) > allocated_nfds
)
3103 LOCK_CONTEXT (context
);
3105 context
->cached_poll_array_size
= allocated_nfds
= nfds
;
3106 context
->cached_poll_array
= fds
= g_new (GPollFD
, nfds
);
3107 UNLOCK_CONTEXT (context
);
3113 g_main_context_poll (context
, timeout
, max_priority
, fds
, nfds
);
3115 some_ready
= g_main_context_check (context
, max_priority
, fds
, nfds
);
3118 g_main_context_dispatch (context
);
3120 g_main_context_release (context
);
3122 LOCK_CONTEXT (context
);
3128 * g_main_context_pending:
3129 * @context: a #GMainContext (if %NULL, the default context will be used)
3131 * Checks if any sources have pending events for the given context.
3133 * Return value: %TRUE if events are pending.
3136 g_main_context_pending (GMainContext
*context
)
3141 context
= g_main_context_default();
3143 LOCK_CONTEXT (context
);
3144 retval
= g_main_context_iterate (context
, FALSE
, FALSE
, G_THREAD_SELF
);
3145 UNLOCK_CONTEXT (context
);
3151 * g_main_context_iteration:
3152 * @context: a #GMainContext (if %NULL, the default context will be used)
3153 * @may_block: whether the call may block.
3155 * Runs a single iteration for the given main loop. This involves
3156 * checking to see if any event sources are ready to be processed,
3157 * then if no events sources are ready and @may_block is %TRUE, waiting
3158 * for a source to become ready, then dispatching the highest priority
3159 * events sources that are ready. Otherwise, if @may_block is %FALSE
3160 * sources are not waited to become ready, only those highest priority
3161 * events sources will be dispatched (if any), that are ready at this
3162 * given moment without further waiting.
3164 * Note that even when @may_block is %TRUE, it is still possible for
3165 * g_main_context_iteration() to return %FALSE, since the the wait may
3166 * be interrupted for other reasons than an event source becoming ready.
3168 * Return value: %TRUE if events were dispatched.
3171 g_main_context_iteration (GMainContext
*context
, gboolean may_block
)
3176 context
= g_main_context_default();
3178 LOCK_CONTEXT (context
);
3179 retval
= g_main_context_iterate (context
, may_block
, TRUE
, G_THREAD_SELF
);
3180 UNLOCK_CONTEXT (context
);
3187 * @context: (allow-none): a #GMainContext (if %NULL, the default context will be used).
3188 * @is_running: set to %TRUE to indicate that the loop is running. This
3189 * is not very important since calling g_main_loop_run() will set this to
3192 * Creates a new #GMainLoop structure.
3194 * Return value: a new #GMainLoop.
3197 g_main_loop_new (GMainContext
*context
,
3198 gboolean is_running
)
3203 context
= g_main_context_default();
3205 g_main_context_ref (context
);
3207 loop
= g_new0 (GMainLoop
, 1);
3208 loop
->context
= context
;
3209 loop
->is_running
= is_running
!= FALSE
;
3210 loop
->ref_count
= 1;
3217 * @loop: a #GMainLoop
3219 * Increases the reference count on a #GMainLoop object by one.
3221 * Return value: @loop
3224 g_main_loop_ref (GMainLoop
*loop
)
3226 g_return_val_if_fail (loop
!= NULL
, NULL
);
3227 g_return_val_if_fail (g_atomic_int_get (&loop
->ref_count
) > 0, NULL
);
3229 g_atomic_int_inc (&loop
->ref_count
);
3235 * g_main_loop_unref:
3236 * @loop: a #GMainLoop
3238 * Decreases the reference count on a #GMainLoop object by one. If
3239 * the result is zero, free the loop and free all associated memory.
3242 g_main_loop_unref (GMainLoop
*loop
)
3244 g_return_if_fail (loop
!= NULL
);
3245 g_return_if_fail (g_atomic_int_get (&loop
->ref_count
) > 0);
3247 if (!g_atomic_int_dec_and_test (&loop
->ref_count
))
3250 g_main_context_unref (loop
->context
);
3256 * @loop: a #GMainLoop
3258 * Runs a main loop until g_main_loop_quit() is called on the loop.
3259 * If this is called for the thread of the loop's #GMainContext,
3260 * it will process events from the loop, otherwise it will
3264 g_main_loop_run (GMainLoop
*loop
)
3266 GThread
*self
= G_THREAD_SELF
;
3268 g_return_if_fail (loop
!= NULL
);
3269 g_return_if_fail (g_atomic_int_get (&loop
->ref_count
) > 0);
3271 if (!g_main_context_acquire (loop
->context
))
3273 gboolean got_ownership
= FALSE
;
3275 /* Another thread owns this context */
3276 LOCK_CONTEXT (loop
->context
);
3278 g_atomic_int_inc (&loop
->ref_count
);
3280 if (!loop
->is_running
)
3281 loop
->is_running
= TRUE
;
3283 while (loop
->is_running
&& !got_ownership
)
3284 got_ownership
= g_main_context_wait (loop
->context
,
3285 &loop
->context
->cond
,
3286 &loop
->context
->mutex
);
3288 if (!loop
->is_running
)
3290 UNLOCK_CONTEXT (loop
->context
);
3292 g_main_context_release (loop
->context
);
3293 g_main_loop_unref (loop
);
3297 g_assert (got_ownership
);
3300 LOCK_CONTEXT (loop
->context
);
3302 if (loop
->context
->in_check_or_prepare
)
3304 g_warning ("g_main_loop_run(): called recursively from within a source's "
3305 "check() or prepare() member, iteration not possible.");
3309 g_atomic_int_inc (&loop
->ref_count
);
3310 loop
->is_running
= TRUE
;
3311 while (loop
->is_running
)
3312 g_main_context_iterate (loop
->context
, TRUE
, TRUE
, self
);
3314 UNLOCK_CONTEXT (loop
->context
);
3316 g_main_context_release (loop
->context
);
3318 g_main_loop_unref (loop
);
3323 * @loop: a #GMainLoop
3325 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
3326 * for the loop will return.
3328 * Note that sources that have already been dispatched when
3329 * g_main_loop_quit() is called will still be executed.
3332 g_main_loop_quit (GMainLoop
*loop
)
3334 g_return_if_fail (loop
!= NULL
);
3335 g_return_if_fail (g_atomic_int_get (&loop
->ref_count
) > 0);
3337 LOCK_CONTEXT (loop
->context
);
3338 loop
->is_running
= FALSE
;
3339 g_wakeup_signal (loop
->context
->wakeup
);
3341 g_cond_broadcast (&loop
->context
->cond
);
3343 UNLOCK_CONTEXT (loop
->context
);
3347 * g_main_loop_is_running:
3348 * @loop: a #GMainLoop.
3350 * Checks to see if the main loop is currently being run via g_main_loop_run().
3352 * Return value: %TRUE if the mainloop is currently being run.
3355 g_main_loop_is_running (GMainLoop
*loop
)
3357 g_return_val_if_fail (loop
!= NULL
, FALSE
);
3358 g_return_val_if_fail (g_atomic_int_get (&loop
->ref_count
) > 0, FALSE
);
3360 return loop
->is_running
;
3364 * g_main_loop_get_context:
3365 * @loop: a #GMainLoop.
3367 * Returns the #GMainContext of @loop.
3369 * Return value: (transfer none): the #GMainContext of @loop
3372 g_main_loop_get_context (GMainLoop
*loop
)
3374 g_return_val_if_fail (loop
!= NULL
, NULL
);
3375 g_return_val_if_fail (g_atomic_int_get (&loop
->ref_count
) > 0, NULL
);
3377 return loop
->context
;
3380 /* HOLDS: context's lock */
3382 g_main_context_poll (GMainContext
*context
,
3388 #ifdef G_MAIN_POLL_DEBUG
3394 GPollFunc poll_func
;
3396 if (n_fds
|| timeout
!= 0)
3398 #ifdef G_MAIN_POLL_DEBUG
3399 if (_g_main_poll_debug
)
3401 g_print ("polling context=%p n=%d timeout=%d\n",
3402 context
, n_fds
, timeout
);
3403 poll_timer
= g_timer_new ();
3407 LOCK_CONTEXT (context
);
3409 poll_func
= context
->poll_func
;
3411 UNLOCK_CONTEXT (context
);
3412 if ((*poll_func
) (fds
, n_fds
, timeout
) < 0 && errno
!= EINTR
)
3415 g_warning ("poll(2) failed due to: %s.",
3416 g_strerror (errno
));
3418 /* If g_poll () returns -1, it has already called g_warning() */
3422 #ifdef G_MAIN_POLL_DEBUG
3423 if (_g_main_poll_debug
)
3425 LOCK_CONTEXT (context
);
3427 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
3430 g_timer_elapsed (poll_timer
, NULL
));
3431 g_timer_destroy (poll_timer
);
3432 pollrec
= context
->poll_records
;
3434 while (pollrec
!= NULL
)
3439 if (fds
[i
].fd
== pollrec
->fd
->fd
&&
3440 pollrec
->fd
->events
&&
3443 g_print (" [" G_POLLFD_FORMAT
" :", fds
[i
].fd
);
3444 if (fds
[i
].revents
& G_IO_IN
)
3446 if (fds
[i
].revents
& G_IO_OUT
)
3448 if (fds
[i
].revents
& G_IO_PRI
)
3450 if (fds
[i
].revents
& G_IO_ERR
)
3452 if (fds
[i
].revents
& G_IO_HUP
)
3454 if (fds
[i
].revents
& G_IO_NVAL
)
3460 pollrec
= pollrec
->next
;
3464 UNLOCK_CONTEXT (context
);
3467 } /* if (n_fds || timeout != 0) */
3471 * g_main_context_add_poll:
3472 * @context: a #GMainContext (or %NULL for the default context)
3473 * @fd: a #GPollFD structure holding information about a file
3474 * descriptor to watch.
3475 * @priority: the priority for this file descriptor which should be
3476 * the same as the priority used for g_source_attach() to ensure that the
3477 * file descriptor is polled whenever the results may be needed.
3479 * Adds a file descriptor to the set of file descriptors polled for
3480 * this context. This will very seldom be used directly. Instead
3481 * a typical event source will use g_source_add_poll() instead.
3484 g_main_context_add_poll (GMainContext
*context
,
3489 context
= g_main_context_default ();
3491 g_return_if_fail (g_atomic_int_get (&context
->ref_count
) > 0);
3492 g_return_if_fail (fd
);
3494 LOCK_CONTEXT (context
);
3495 g_main_context_add_poll_unlocked (context
, priority
, fd
);
3496 UNLOCK_CONTEXT (context
);
3499 /* HOLDS: main_loop_lock */
3501 g_main_context_add_poll_unlocked (GMainContext
*context
,
3505 GPollRec
*prevrec
, *nextrec
;
3506 GPollRec
*newrec
= g_slice_new (GPollRec
);
3508 /* This file descriptor may be checked before we ever poll */
3511 newrec
->priority
= priority
;
3513 prevrec
= context
->poll_records_tail
;
3515 while (prevrec
&& priority
< prevrec
->priority
)
3518 prevrec
= prevrec
->prev
;
3522 prevrec
->next
= newrec
;
3524 context
->poll_records
= newrec
;
3526 newrec
->prev
= prevrec
;
3527 newrec
->next
= nextrec
;
3530 nextrec
->prev
= newrec
;
3532 context
->poll_records_tail
= newrec
;
3534 context
->n_poll_records
++;
3536 context
->poll_changed
= TRUE
;
3538 /* Now wake up the main loop if it is waiting in the poll() */
3539 g_wakeup_signal (context
->wakeup
);
3543 * g_main_context_remove_poll:
3544 * @context:a #GMainContext
3545 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3547 * Removes file descriptor from the set of file descriptors to be
3548 * polled for a particular context.
3551 g_main_context_remove_poll (GMainContext
*context
,
3555 context
= g_main_context_default ();
3557 g_return_if_fail (g_atomic_int_get (&context
->ref_count
) > 0);
3558 g_return_if_fail (fd
);
3560 LOCK_CONTEXT (context
);
3561 g_main_context_remove_poll_unlocked (context
, fd
);
3562 UNLOCK_CONTEXT (context
);
3566 g_main_context_remove_poll_unlocked (GMainContext
*context
,
3569 GPollRec
*pollrec
, *prevrec
, *nextrec
;
3572 pollrec
= context
->poll_records
;
3576 nextrec
= pollrec
->next
;
3577 if (pollrec
->fd
== fd
)
3579 if (prevrec
!= NULL
)
3580 prevrec
->next
= nextrec
;
3582 context
->poll_records
= nextrec
;
3584 if (nextrec
!= NULL
)
3585 nextrec
->prev
= prevrec
;
3587 context
->poll_records_tail
= prevrec
;
3589 g_slice_free (GPollRec
, pollrec
);
3591 context
->n_poll_records
--;
3598 context
->poll_changed
= TRUE
;
3600 /* Now wake up the main loop if it is waiting in the poll() */
3601 g_wakeup_signal (context
->wakeup
);
3605 * g_source_get_current_time:
3606 * @source: a #GSource
3607 * @timeval: #GTimeVal structure in which to store current time.
3609 * This function ignores @source and is otherwise the same as
3610 * g_get_current_time().
3612 * Deprecated: 2.28: use g_source_get_time() instead
3615 g_source_get_current_time (GSource
*source
,
3618 g_get_current_time (timeval
);
3622 * g_source_get_time:
3623 * @source: a #GSource
3625 * Gets the time to be used when checking this source. The advantage of
3626 * calling this function over calling g_get_monotonic_time() directly is
3627 * that when checking multiple sources, GLib can cache a single value
3628 * instead of having to repeatedly get the system monotonic time.
3630 * The time here is the system monotonic time, if available, or some
3631 * other reasonable alternative otherwise. See g_get_monotonic_time().
3633 * Returns: the monotonic time in microseconds
3638 g_source_get_time (GSource
*source
)
3640 GMainContext
*context
;
3643 g_return_val_if_fail (source
->context
!= NULL
, 0);
3645 context
= source
->context
;
3647 LOCK_CONTEXT (context
);
3649 if (!context
->time_is_fresh
)
3651 context
->time
= g_get_monotonic_time ();
3652 context
->time_is_fresh
= TRUE
;
3655 result
= context
->time
;
3657 UNLOCK_CONTEXT (context
);
3663 * g_main_context_set_poll_func:
3664 * @context: a #GMainContext
3665 * @func: the function to call to poll all file descriptors
3667 * Sets the function to use to handle polling of file descriptors. It
3668 * will be used instead of the poll() system call
3669 * (or GLib's replacement function, which is used where
3670 * poll() isn't available).
3672 * This function could possibly be used to integrate the GLib event
3673 * loop with an external event loop.
3676 g_main_context_set_poll_func (GMainContext
*context
,
3680 context
= g_main_context_default ();
3682 g_return_if_fail (g_atomic_int_get (&context
->ref_count
) > 0);
3684 LOCK_CONTEXT (context
);
3687 context
->poll_func
= func
;
3689 context
->poll_func
= g_poll
;
3691 UNLOCK_CONTEXT (context
);
3695 * g_main_context_get_poll_func:
3696 * @context: a #GMainContext
3698 * Gets the poll function set by g_main_context_set_poll_func().
3700 * Return value: the poll function
3703 g_main_context_get_poll_func (GMainContext
*context
)
3708 context
= g_main_context_default ();
3710 g_return_val_if_fail (g_atomic_int_get (&context
->ref_count
) > 0, NULL
);
3712 LOCK_CONTEXT (context
);
3713 result
= context
->poll_func
;
3714 UNLOCK_CONTEXT (context
);
3720 * g_main_context_wakeup:
3721 * @context: a #GMainContext
3723 * If @context is currently waiting in a poll(), interrupt
3724 * the poll(), and continue the iteration process.
3727 g_main_context_wakeup (GMainContext
*context
)
3730 context
= g_main_context_default ();
3732 g_return_if_fail (g_atomic_int_get (&context
->ref_count
) > 0);
3734 g_wakeup_signal (context
->wakeup
);
3738 * g_main_context_is_owner:
3739 * @context: a #GMainContext
3741 * Determines whether this thread holds the (recursive)
3742 * ownership of this #GMainContext. This is useful to
3743 * know before waiting on another thread that may be
3744 * blocking to get ownership of @context.
3746 * Returns: %TRUE if current thread is owner of @context.
3751 g_main_context_is_owner (GMainContext
*context
)
3756 context
= g_main_context_default ();
3758 LOCK_CONTEXT (context
);
3759 is_owner
= context
->owner
== G_THREAD_SELF
;
3760 UNLOCK_CONTEXT (context
);
3768 g_timeout_set_expiration (GTimeoutSource
*timeout_source
,
3769 gint64 current_time
)
3771 timeout_source
->expiration
= current_time
+
3772 (guint64
) timeout_source
->interval
* 1000;
3774 if (timeout_source
->seconds
)
3777 static gint timer_perturb
= -1;
3779 if (timer_perturb
== -1)
3782 * we want a per machine/session unique 'random' value; try the dbus
3783 * address first, that has a UUID in it. If there is no dbus, use the
3784 * hostname for hashing.
3786 const char *session_bus_address
= g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3787 if (!session_bus_address
)
3788 session_bus_address
= g_getenv ("HOSTNAME");
3789 if (session_bus_address
)
3790 timer_perturb
= ABS ((gint
) g_str_hash (session_bus_address
)) % 1000000;
3795 /* We want the microseconds part of the timeout to land on the
3796 * 'timer_perturb' mark, but we need to make sure we don't try to
3797 * set the timeout in the past. We do this by ensuring that we
3798 * always only *increase* the expiration time by adding a full
3799 * second in the case that the microsecond portion decreases.
3801 timeout_source
->expiration
-= timer_perturb
;
3803 remainder
= timeout_source
->expiration
% 1000000;
3804 if (remainder
>= 1000000/4)
3805 timeout_source
->expiration
+= 1000000;
3807 timeout_source
->expiration
-= remainder
;
3808 timeout_source
->expiration
+= timer_perturb
;
3813 g_timeout_prepare (GSource
*source
,
3816 GTimeoutSource
*timeout_source
= (GTimeoutSource
*) source
;
3817 gint64 now
= g_source_get_time (source
);
3819 if (now
< timeout_source
->expiration
)
3821 /* Round up to ensure that we don't try again too early */
3822 *timeout
= (timeout_source
->expiration
- now
+ 999) / 1000;
3831 g_timeout_check (GSource
*source
)
3833 GTimeoutSource
*timeout_source
= (GTimeoutSource
*) source
;
3834 gint64 now
= g_source_get_time (source
);
3836 return timeout_source
->expiration
<= now
;
3840 g_timeout_dispatch (GSource
*source
,
3841 GSourceFunc callback
,
3844 GTimeoutSource
*timeout_source
= (GTimeoutSource
*)source
;
3849 g_warning ("Timeout source dispatched without callback\n"
3850 "You must call g_source_set_callback().");
3854 again
= callback (user_data
);
3857 g_timeout_set_expiration (timeout_source
, g_source_get_time (source
));
3863 * g_timeout_source_new:
3864 * @interval: the timeout interval in milliseconds.
3866 * Creates a new timeout source.
3868 * The source will not initially be associated with any #GMainContext
3869 * and must be added to one with g_source_attach() before it will be
3872 * The interval given is in terms of monotonic time, not wall clock
3873 * time. See g_get_monotonic_time().
3875 * Return value: the newly-created timeout source
3878 g_timeout_source_new (guint interval
)
3880 GSource
*source
= g_source_new (&g_timeout_funcs
, sizeof (GTimeoutSource
));
3881 GTimeoutSource
*timeout_source
= (GTimeoutSource
*)source
;
3883 timeout_source
->interval
= interval
;
3884 g_timeout_set_expiration (timeout_source
, g_get_monotonic_time ());
3890 * g_timeout_source_new_seconds:
3891 * @interval: the timeout interval in seconds
3893 * Creates a new timeout source.
3895 * The source will not initially be associated with any #GMainContext
3896 * and must be added to one with g_source_attach() before it will be
3899 * The scheduling granularity/accuracy of this timeout source will be
3902 * The interval given in terms of monotonic time, not wall clock time.
3903 * See g_get_monotonic_time().
3905 * Return value: the newly-created timeout source
3910 g_timeout_source_new_seconds (guint interval
)
3912 GSource
*source
= g_source_new (&g_timeout_funcs
, sizeof (GTimeoutSource
));
3913 GTimeoutSource
*timeout_source
= (GTimeoutSource
*)source
;
3915 timeout_source
->interval
= 1000 * interval
;
3916 timeout_source
->seconds
= TRUE
;
3918 g_timeout_set_expiration (timeout_source
, g_get_monotonic_time ());
3925 * g_timeout_add_full:
3926 * @priority: the priority of the timeout source. Typically this will be in
3927 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3928 * @interval: the time between calls to the function, in milliseconds
3929 * (1/1000ths of a second)
3930 * @function: function to call
3931 * @data: data to pass to @function
3932 * @notify: function to call when the timeout is removed, or %NULL
3934 * Sets a function to be called at regular intervals, with the given
3935 * priority. The function is called repeatedly until it returns
3936 * %FALSE, at which point the timeout is automatically destroyed and
3937 * the function will not be called again. The @notify function is
3938 * called when the timeout is destroyed. The first call to the
3939 * function will be at the end of the first @interval.
3941 * Note that timeout functions may be delayed, due to the processing of other
3942 * event sources. Thus they should not be relied on for precise timing.
3943 * After each call to the timeout function, the time of the next
3944 * timeout is recalculated based on the current time and the given interval
3945 * (it does not try to 'catch up' time lost in delays).
3947 * This internally creates a main loop source using g_timeout_source_new()
3948 * and attaches it to the main loop context using g_source_attach(). You can
3949 * do these steps manually if you need greater control.
3951 * The interval given in terms of monotonic time, not wall clock time.
3952 * See g_get_monotonic_time().
3954 * Return value: the ID (greater than 0) of the event source.
3955 * Rename to: g_timeout_add
3958 g_timeout_add_full (gint priority
,
3960 GSourceFunc function
,
3962 GDestroyNotify notify
)
3967 g_return_val_if_fail (function
!= NULL
, 0);
3969 source
= g_timeout_source_new (interval
);
3971 if (priority
!= G_PRIORITY_DEFAULT
)
3972 g_source_set_priority (source
, priority
);
3974 g_source_set_callback (source
, function
, data
, notify
);
3975 id
= g_source_attach (source
, NULL
);
3976 g_source_unref (source
);
3983 * @interval: the time between calls to the function, in milliseconds
3984 * (1/1000ths of a second)
3985 * @function: function to call
3986 * @data: data to pass to @function
3988 * Sets a function to be called at regular intervals, with the default
3989 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3990 * until it returns %FALSE, at which point the timeout is automatically
3991 * destroyed and the function will not be called again. The first call
3992 * to the function will be at the end of the first @interval.
3994 * Note that timeout functions may be delayed, due to the processing of other
3995 * event sources. Thus they should not be relied on for precise timing.
3996 * After each call to the timeout function, the time of the next
3997 * timeout is recalculated based on the current time and the given interval
3998 * (it does not try to 'catch up' time lost in delays).
4000 * If you want to have a timer in the "seconds" range and do not care
4001 * about the exact time of the first call of the timer, use the
4002 * g_timeout_add_seconds() function; this function allows for more
4003 * optimizations and more efficient system power usage.
4005 * This internally creates a main loop source using g_timeout_source_new()
4006 * and attaches it to the main loop context using g_source_attach(). You can
4007 * do these steps manually if you need greater control.
4009 * The interval given is in terms of monotonic time, not wall clock
4010 * time. See g_get_monotonic_time().
4012 * Return value: the ID (greater than 0) of the event source.
4015 g_timeout_add (guint32 interval
,
4016 GSourceFunc function
,
4019 return g_timeout_add_full (G_PRIORITY_DEFAULT
,
4020 interval
, function
, data
, NULL
);
4024 * g_timeout_add_seconds_full:
4025 * @priority: the priority of the timeout source. Typically this will be in
4026 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
4027 * @interval: the time between calls to the function, in seconds
4028 * @function: function to call
4029 * @data: data to pass to @function
4030 * @notify: function to call when the timeout is removed, or %NULL
4032 * Sets a function to be called at regular intervals, with @priority.
4033 * The function is called repeatedly until it returns %FALSE, at which
4034 * point the timeout is automatically destroyed and the function will
4035 * not be called again.
4037 * Unlike g_timeout_add(), this function operates at whole second granularity.
4038 * The initial starting point of the timer is determined by the implementation
4039 * and the implementation is expected to group multiple timers together so that
4040 * they fire all at the same time.
4041 * To allow this grouping, the @interval to the first timer is rounded
4042 * and can deviate up to one second from the specified interval.
4043 * Subsequent timer iterations will generally run at the specified interval.
4045 * Note that timeout functions may be delayed, due to the processing of other
4046 * event sources. Thus they should not be relied on for precise timing.
4047 * After each call to the timeout function, the time of the next
4048 * timeout is recalculated based on the current time and the given @interval
4050 * If you want timing more precise than whole seconds, use g_timeout_add()
4053 * The grouping of timers to fire at the same time results in a more power
4054 * and CPU efficient behavior so if your timer is in multiples of seconds
4055 * and you don't require the first timer exactly one second from now, the
4056 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
4058 * This internally creates a main loop source using
4059 * g_timeout_source_new_seconds() and attaches it to the main loop context
4060 * using g_source_attach(). You can do these steps manually if you need
4063 * The interval given is in terms of monotonic time, not wall clock
4064 * time. See g_get_monotonic_time().
4066 * Return value: the ID (greater than 0) of the event source.
4068 * Rename to: g_timeout_add_seconds
4072 g_timeout_add_seconds_full (gint priority
,
4074 GSourceFunc function
,
4076 GDestroyNotify notify
)
4081 g_return_val_if_fail (function
!= NULL
, 0);
4083 source
= g_timeout_source_new_seconds (interval
);
4085 if (priority
!= G_PRIORITY_DEFAULT
)
4086 g_source_set_priority (source
, priority
);
4088 g_source_set_callback (source
, function
, data
, notify
);
4089 id
= g_source_attach (source
, NULL
);
4090 g_source_unref (source
);
4096 * g_timeout_add_seconds:
4097 * @interval: the time between calls to the function, in seconds
4098 * @function: function to call
4099 * @data: data to pass to @function
4101 * Sets a function to be called at regular intervals with the default
4102 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
4103 * it returns %FALSE, at which point the timeout is automatically destroyed
4104 * and the function will not be called again.
4106 * This internally creates a main loop source using
4107 * g_timeout_source_new_seconds() and attaches it to the main loop context
4108 * using g_source_attach(). You can do these steps manually if you need
4109 * greater control. Also see g_timeout_add_seconds_full().
4111 * Note that the first call of the timer may not be precise for timeouts
4112 * of one second. If you need finer precision and have such a timeout,
4113 * you may want to use g_timeout_add() instead.
4115 * The interval given is in terms of monotonic time, not wall clock
4116 * time. See g_get_monotonic_time().
4118 * Return value: the ID (greater than 0) of the event source.
4123 g_timeout_add_seconds (guint interval
,
4124 GSourceFunc function
,
4127 g_return_val_if_fail (function
!= NULL
, 0);
4129 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT
, interval
, function
, data
, NULL
);
4132 /* Child watch functions */
4137 g_child_watch_prepare (GSource
*source
,
4145 g_child_watch_check (GSource
*source
)
4147 GChildWatchSource
*child_watch_source
;
4148 gboolean child_exited
;
4150 child_watch_source
= (GChildWatchSource
*) source
;
4152 child_exited
= child_watch_source
->poll
.revents
& G_IO_IN
;
4159 * Note: We do _not_ check for the special value of STILL_ACTIVE
4160 * since we know that the process has exited and doing so runs into
4161 * problems if the child process "happens to return STILL_ACTIVE(259)"
4162 * as Microsoft's Platform SDK puts it.
4164 if (!GetExitCodeProcess (child_watch_source
->pid
, &child_status
))
4166 gchar
*emsg
= g_win32_error_message (GetLastError ());
4167 g_warning (G_STRLOC
": GetExitCodeProcess() failed: %s", emsg
);
4170 child_watch_source
->child_status
= -1;
4173 child_watch_source
->child_status
= child_status
;
4176 return child_exited
;
4180 g_child_watch_finalize (GSource
*source
)
4184 #else /* G_OS_WIN32 */
4187 wake_source (GSource
*source
)
4189 GMainContext
*context
;
4191 /* This should be thread-safe:
4193 * - if the source is currently being added to a context, that
4194 * context will be woken up anyway
4196 * - if the source is currently being destroyed, we simply need not
4199 * - the memory for the source will remain valid until after the
4200 * source finalize function was called (which would remove the
4201 * source from the global list which we are currently holding the
4204 * - the GMainContext will either be NULL or point to a live
4207 * - the GMainContext will remain valid since we hold the
4208 * main_context_list lock
4210 * Since we are holding a lot of locks here, don't try to enter any
4211 * more GMainContext functions for fear of dealock -- just hit the
4212 * GWakeup and run. Even if that's safe now, it could easily become
4213 * unsafe with some very minor changes in the future, and signal
4214 * handling is not the most well-tested codepath.
4216 G_LOCK(main_context_list
);
4217 context
= source
->context
;
4219 g_wakeup_signal (context
->wakeup
);
4220 G_UNLOCK(main_context_list
);
4224 dispatch_unix_signals (void)
4228 /* clear this first incase another one arrives while we're processing */
4229 any_unix_signal_pending
= FALSE
;
4231 G_LOCK(unix_signal_lock
);
4233 /* handle GChildWatchSource instances */
4234 if (unix_signal_pending
[SIGCHLD
])
4236 unix_signal_pending
[SIGCHLD
] = FALSE
;
4238 /* The only way we can do this is to scan all of the children.
4240 * The docs promise that we will not reap children that we are not
4241 * explicitly watching, so that ties our hands from calling
4242 * waitpid(-1). We also can't use siginfo's si_pid field since if
4243 * multiple SIGCHLD arrive at the same time, one of them can be
4244 * dropped (since a given UNIX signal can only be pending once).
4246 for (node
= unix_child_watches
; node
; node
= node
->next
)
4248 GChildWatchSource
*source
= node
->data
;
4250 if (!source
->child_exited
)
4252 if (waitpid (source
->pid
, &source
->child_status
, WNOHANG
) > 0)
4254 source
->child_exited
= TRUE
;
4256 wake_source ((GSource
*) source
);
4262 /* handle GUnixSignalWatchSource instances */
4263 for (node
= unix_signal_watches
; node
; node
= node
->next
)
4265 GUnixSignalWatchSource
*source
= node
->data
;
4267 if (!source
->pending
)
4269 if (unix_signal_pending
[source
->signum
])
4271 unix_signal_pending
[source
->signum
] = FALSE
;
4272 source
->pending
= TRUE
;
4274 wake_source ((GSource
*) source
);
4279 G_UNLOCK(unix_signal_lock
);
4283 g_child_watch_prepare (GSource
*source
,
4286 GChildWatchSource
*child_watch_source
;
4288 child_watch_source
= (GChildWatchSource
*) source
;
4290 return child_watch_source
->child_exited
;
4294 g_child_watch_check (GSource
*source
)
4296 GChildWatchSource
*child_watch_source
;
4298 child_watch_source
= (GChildWatchSource
*) source
;
4300 return child_watch_source
->child_exited
;
4304 g_unix_signal_watch_prepare (GSource
*source
,
4307 GUnixSignalWatchSource
*unix_signal_source
;
4309 unix_signal_source
= (GUnixSignalWatchSource
*) source
;
4311 return unix_signal_source
->pending
;
4315 g_unix_signal_watch_check (GSource
*source
)
4317 GUnixSignalWatchSource
*unix_signal_source
;
4319 unix_signal_source
= (GUnixSignalWatchSource
*) source
;
4321 return unix_signal_source
->pending
;
4325 g_unix_signal_watch_dispatch (GSource
*source
,
4326 GSourceFunc callback
,
4329 GUnixSignalWatchSource
*unix_signal_source
;
4331 unix_signal_source
= (GUnixSignalWatchSource
*) source
;
4335 g_warning ("Unix signal source dispatched without callback\n"
4336 "You must call g_source_set_callback().");
4340 (callback
) (user_data
);
4342 unix_signal_source
->pending
= FALSE
;
4348 ensure_unix_signal_handler_installed_unlocked (int signum
)
4350 static sigset_t installed_signal_mask
;
4351 static gboolean initialized
;
4352 struct sigaction action
;
4356 sigemptyset (&installed_signal_mask
);
4357 g_get_worker_context ();
4361 if (sigismember (&installed_signal_mask
, signum
))
4364 sigaddset (&installed_signal_mask
, signum
);
4366 action
.sa_handler
= g_unix_signal_handler
;
4367 sigemptyset (&action
.sa_mask
);
4368 action
.sa_flags
= SA_RESTART
| SA_NOCLDSTOP
;
4369 sigaction (signum
, &action
, NULL
);
4373 _g_main_create_unix_signal_watch (int signum
)
4376 GUnixSignalWatchSource
*unix_signal_source
;
4378 source
= g_source_new (&g_unix_signal_funcs
, sizeof (GUnixSignalWatchSource
));
4379 unix_signal_source
= (GUnixSignalWatchSource
*) source
;
4381 unix_signal_source
->signum
= signum
;
4382 unix_signal_source
->pending
= FALSE
;
4384 G_LOCK (unix_signal_lock
);
4385 ensure_unix_signal_handler_installed_unlocked (signum
);
4386 unix_signal_watches
= g_slist_prepend (unix_signal_watches
, unix_signal_source
);
4387 if (unix_signal_pending
[signum
])
4388 unix_signal_source
->pending
= TRUE
;
4389 unix_signal_pending
[signum
] = FALSE
;
4390 G_UNLOCK (unix_signal_lock
);
4396 g_unix_signal_watch_finalize (GSource
*source
)
4398 G_LOCK (unix_signal_lock
);
4399 unix_signal_watches
= g_slist_remove (unix_signal_watches
, source
);
4400 G_UNLOCK (unix_signal_lock
);
4404 g_child_watch_finalize (GSource
*source
)
4406 G_LOCK (unix_signal_lock
);
4407 unix_child_watches
= g_slist_remove (unix_child_watches
, source
);
4408 G_UNLOCK (unix_signal_lock
);
4411 #endif /* G_OS_WIN32 */
4414 g_child_watch_dispatch (GSource
*source
,
4415 GSourceFunc callback
,
4418 GChildWatchSource
*child_watch_source
;
4419 GChildWatchFunc child_watch_callback
= (GChildWatchFunc
) callback
;
4421 child_watch_source
= (GChildWatchSource
*) source
;
4425 g_warning ("Child watch source dispatched without callback\n"
4426 "You must call g_source_set_callback().");
4430 (child_watch_callback
) (child_watch_source
->pid
, child_watch_source
->child_status
, user_data
);
4432 /* We never keep a child watch source around as the child is gone */
4439 g_unix_signal_handler (int signum
)
4441 unix_signal_pending
[signum
] = TRUE
;
4442 any_unix_signal_pending
= TRUE
;
4444 g_wakeup_signal (glib_worker_context
->wakeup
);
4447 #endif /* !G_OS_WIN32 */
4450 * g_child_watch_source_new:
4451 * @pid: process to watch. On POSIX the pid of a child process. On
4452 * Windows a handle for a process (which doesn't have to be a child).
4454 * Creates a new child_watch source.
4456 * The source will not initially be associated with any #GMainContext
4457 * and must be added to one with g_source_attach() before it will be
4460 * Note that child watch sources can only be used in conjunction with
4461 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
4464 * Note that on platforms where #GPid must be explicitly closed
4465 * (see g_spawn_close_pid()) @pid must not be closed while the
4466 * source is still active. Typically, you will want to call
4467 * g_spawn_close_pid() in the callback function for the source.
4469 * Note further that using g_child_watch_source_new() is not
4470 * compatible with calling <literal>waitpid(-1)</literal> in
4471 * the application. Calling waitpid() for individual pids will
4474 * Return value: the newly-created child watch source
4479 g_child_watch_source_new (GPid pid
)
4481 GSource
*source
= g_source_new (&g_child_watch_funcs
, sizeof (GChildWatchSource
));
4482 GChildWatchSource
*child_watch_source
= (GChildWatchSource
*)source
;
4484 child_watch_source
->pid
= pid
;
4487 child_watch_source
->poll
.fd
= (gintptr
) pid
;
4488 child_watch_source
->poll
.events
= G_IO_IN
;
4490 g_source_add_poll (source
, &child_watch_source
->poll
);
4491 #else /* G_OS_WIN32 */
4492 G_LOCK (unix_signal_lock
);
4493 ensure_unix_signal_handler_installed_unlocked (SIGCHLD
);
4494 unix_child_watches
= g_slist_prepend (unix_child_watches
, child_watch_source
);
4495 if (waitpid (pid
, &child_watch_source
->child_status
, WNOHANG
) > 0)
4496 child_watch_source
->child_exited
= TRUE
;
4497 G_UNLOCK (unix_signal_lock
);
4498 #endif /* G_OS_WIN32 */
4504 * g_child_watch_add_full:
4505 * @priority: the priority of the idle source. Typically this will be in the
4506 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4507 * @pid: process to watch. On POSIX the pid of a child process. On
4508 * Windows a handle for a process (which doesn't have to be a child).
4509 * @function: function to call
4510 * @data: data to pass to @function
4511 * @notify: function to call when the idle is removed, or %NULL
4513 * Sets a function to be called when the child indicated by @pid
4514 * exits, at the priority @priority.
4516 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4517 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4518 * the spawn function for the child watching to work.
4520 * Note that on platforms where #GPid must be explicitly closed
4521 * (see g_spawn_close_pid()) @pid must not be closed while the
4522 * source is still active. Typically, you will want to call
4523 * g_spawn_close_pid() in the callback function for the source.
4525 * GLib supports only a single callback per process id.
4527 * This internally creates a main loop source using
4528 * g_child_watch_source_new() and attaches it to the main loop context
4529 * using g_source_attach(). You can do these steps manually if you
4530 * need greater control.
4532 * Return value: the ID (greater than 0) of the event source.
4534 * Rename to: g_child_watch_add
4538 g_child_watch_add_full (gint priority
,
4540 GChildWatchFunc function
,
4542 GDestroyNotify notify
)
4547 g_return_val_if_fail (function
!= NULL
, 0);
4549 source
= g_child_watch_source_new (pid
);
4551 if (priority
!= G_PRIORITY_DEFAULT
)
4552 g_source_set_priority (source
, priority
);
4554 g_source_set_callback (source
, (GSourceFunc
) function
, data
, notify
);
4555 id
= g_source_attach (source
, NULL
);
4556 g_source_unref (source
);
4562 * g_child_watch_add:
4563 * @pid: process id to watch. On POSIX the pid of a child process. On
4564 * Windows a handle for a process (which doesn't have to be a child).
4565 * @function: function to call
4566 * @data: data to pass to @function
4568 * Sets a function to be called when the child indicated by @pid
4569 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4571 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4572 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4573 * the spawn function for the child watching to work.
4575 * Note that on platforms where #GPid must be explicitly closed
4576 * (see g_spawn_close_pid()) @pid must not be closed while the
4577 * source is still active. Typically, you will want to call
4578 * g_spawn_close_pid() in the callback function for the source.
4580 * GLib supports only a single callback per process id.
4582 * This internally creates a main loop source using
4583 * g_child_watch_source_new() and attaches it to the main loop context
4584 * using g_source_attach(). You can do these steps manually if you
4585 * need greater control.
4587 * Return value: the ID (greater than 0) of the event source.
4592 g_child_watch_add (GPid pid
,
4593 GChildWatchFunc function
,
4596 return g_child_watch_add_full (G_PRIORITY_DEFAULT
, pid
, function
, data
, NULL
);
4600 /* Idle functions */
4603 g_idle_prepare (GSource
*source
,
4612 g_idle_check (GSource
*source
)
4618 g_idle_dispatch (GSource
*source
,
4619 GSourceFunc callback
,
4624 g_warning ("Idle source dispatched without callback\n"
4625 "You must call g_source_set_callback().");
4629 return callback (user_data
);
4633 * g_idle_source_new:
4635 * Creates a new idle source.
4637 * The source will not initially be associated with any #GMainContext
4638 * and must be added to one with g_source_attach() before it will be
4639 * executed. Note that the default priority for idle sources is
4640 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4641 * have a default priority of %G_PRIORITY_DEFAULT.
4643 * Return value: the newly-created idle source
4646 g_idle_source_new (void)
4650 source
= g_source_new (&g_idle_funcs
, sizeof (GSource
));
4651 g_source_set_priority (source
, G_PRIORITY_DEFAULT_IDLE
);
4658 * @priority: the priority of the idle source. Typically this will be in the
4659 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4660 * @function: function to call
4661 * @data: data to pass to @function
4662 * @notify: function to call when the idle is removed, or %NULL
4664 * Adds a function to be called whenever there are no higher priority
4665 * events pending. If the function returns %FALSE it is automatically
4666 * removed from the list of event sources and will not be called again.
4668 * This internally creates a main loop source using g_idle_source_new()
4669 * and attaches it to the main loop context using g_source_attach().
4670 * You can do these steps manually if you need greater control.
4672 * Return value: the ID (greater than 0) of the event source.
4673 * Rename to: g_idle_add
4676 g_idle_add_full (gint priority
,
4677 GSourceFunc function
,
4679 GDestroyNotify notify
)
4684 g_return_val_if_fail (function
!= NULL
, 0);
4686 source
= g_idle_source_new ();
4688 if (priority
!= G_PRIORITY_DEFAULT_IDLE
)
4689 g_source_set_priority (source
, priority
);
4691 g_source_set_callback (source
, function
, data
, notify
);
4692 id
= g_source_attach (source
, NULL
);
4693 g_source_unref (source
);
4700 * @function: function to call
4701 * @data: data to pass to @function.
4703 * Adds a function to be called whenever there are no higher priority
4704 * events pending to the default main loop. The function is given the
4705 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4706 * returns %FALSE it is automatically removed from the list of event
4707 * sources and will not be called again.
4709 * This internally creates a main loop source using g_idle_source_new()
4710 * and attaches it to the main loop context using g_source_attach().
4711 * You can do these steps manually if you need greater control.
4713 * Return value: the ID (greater than 0) of the event source.
4716 g_idle_add (GSourceFunc function
,
4719 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE
, function
, data
, NULL
);
4723 * g_idle_remove_by_data:
4724 * @data: the data for the idle source's callback.
4726 * Removes the idle function with the given data.
4728 * Return value: %TRUE if an idle source was found and removed.
4731 g_idle_remove_by_data (gpointer data
)
4733 return g_source_remove_by_funcs_user_data (&g_idle_funcs
, data
);
4737 * g_main_context_invoke:
4738 * @context: (allow-none): a #GMainContext, or %NULL
4739 * @function: function to call
4740 * @data: data to pass to @function
4742 * Invokes a function in such a way that @context is owned during the
4743 * invocation of @function.
4745 * If @context is %NULL then the global default main context — as
4746 * returned by g_main_context_default() — is used.
4748 * If @context is owned by the current thread, @function is called
4749 * directly. Otherwise, if @context is the thread-default main context
4750 * of the current thread and g_main_context_acquire() succeeds, then
4751 * @function is called and g_main_context_release() is called
4754 * In any other case, an idle source is created to call @function and
4755 * that source is attached to @context (presumably to be run in another
4756 * thread). The idle source is attached with #G_PRIORITY_DEFAULT
4757 * priority. If you want a different priority, use
4758 * g_main_context_invoke_full().
4760 * Note that, as with normal idle functions, @function should probably
4761 * return %FALSE. If it returns %TRUE, it will be continuously run in a
4762 * loop (and may prevent this call from returning).
4767 g_main_context_invoke (GMainContext
*context
,
4768 GSourceFunc function
,
4771 g_main_context_invoke_full (context
,
4773 function
, data
, NULL
);
4777 * g_main_context_invoke_full:
4778 * @context: (allow-none): a #GMainContext, or %NULL
4779 * @priority: the priority at which to run @function
4780 * @function: function to call
4781 * @data: data to pass to @function
4782 * @notify: a function to call when @data is no longer in use, or %NULL.
4784 * Invokes a function in such a way that @context is owned during the
4785 * invocation of @function.
4787 * This function is the same as g_main_context_invoke() except that it
4788 * lets you specify the priority incase @function ends up being
4789 * scheduled as an idle and also lets you give a #GDestroyNotify for @data.
4791 * @notify should not assume that it is called from any particular
4792 * thread or with any particular context acquired.
4797 g_main_context_invoke_full (GMainContext
*context
,
4799 GSourceFunc function
,
4801 GDestroyNotify notify
)
4803 g_return_if_fail (function
!= NULL
);
4806 context
= g_main_context_default ();
4808 if (g_main_context_is_owner (context
))
4810 while (function (data
));
4817 GMainContext
*thread_default
;
4819 thread_default
= g_main_context_get_thread_default ();
4821 if (!thread_default
)
4822 thread_default
= g_main_context_default ();
4824 if (thread_default
== context
&& g_main_context_acquire (context
))
4826 while (function (data
));
4828 g_main_context_release (context
);
4837 source
= g_idle_source_new ();
4838 g_source_set_priority (source
, priority
);
4839 g_source_set_callback (source
, function
, data
, notify
);
4840 g_source_attach (source
, context
);
4841 g_source_unref (source
);
4847 glib_worker_main (gpointer data
)
4851 g_main_context_iteration (glib_worker_context
, TRUE
);
4854 if (any_unix_signal_pending
)
4855 dispatch_unix_signals ();
4859 return NULL
; /* worst GCC warning message ever... */
4863 g_get_worker_context (void)
4865 static gsize initialised
;
4867 if (g_once_init_enter (&initialised
))
4869 /* mask all signals in the worker thread */
4875 pthread_sigmask (SIG_SETMASK
, &all
, &prev_mask
);
4877 glib_worker_context
= g_main_context_new ();
4878 g_thread_new ("gmain", glib_worker_main
, NULL
);
4880 pthread_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
4882 g_once_init_leave (&initialised
, TRUE
);
4885 return glib_worker_context
;