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/.
36 /* uncomment the next line to get poll() debugging info */
37 /* #define G_MAIN_POLL_DEBUG */
40 #include <sys/types.h>
42 #ifdef HAVE_SYS_TIME_H
44 #endif /* HAVE_SYS_TIME_H */
45 #ifdef GLIB_HAVE_SYS_POLL_H
46 # include <sys/poll.h>
47 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
48 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
49 #endif /* GLIB_HAVE_SYS_POLL_H */
52 #endif /* HAVE_UNISTD_H */
58 #endif /* G_OS_WIN32 */
61 #include <net/socket.h>
62 #endif /* G_OS_BEOS */
66 typedef struct _GTimeoutSource GTimeoutSource;
67 typedef struct _GPollRec GPollRec;
68 typedef struct _GSourceCallback GSourceCallback;
72 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
73 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
76 #ifdef G_THREADS_ENABLED
77 typedef struct _GMainWaiter GMainWaiter;
88 #ifdef G_THREADS_ENABLED
89 /* The following lock is used for both the list of sources
90 * and the list of poll records
101 GPtrArray *pending_dispatches;
102 gint timeout; /* Timeout for current iteration */
105 GSource *source_list;
106 gint in_check_or_prepare;
108 GPollRec *poll_records;
109 GPollRec *poll_free_list;
110 GMemChunk *poll_chunk;
111 guint n_poll_records;
112 GPollFD *cached_poll_array;
113 guint cached_poll_array_size;
115 #ifdef G_THREADS_ENABLED
117 /* this pipe is used to wake up the main loop when a source is added.
119 gint wake_up_pipe[2];
120 #else /* G_OS_WIN32 */
121 HANDLE wake_up_semaphore;
122 #endif /* G_OS_WIN32 */
125 gboolean poll_waiting;
127 /* Flag indicating whether the set of fd's changed during a poll */
128 gboolean poll_changed;
129 #endif /* G_THREADS_ENABLED */
133 GTimeVal current_time;
134 gboolean time_is_current;
137 struct _GSourceCallback
142 GDestroyNotify notify;
147 GMainContext *context;
152 struct _GTimeoutSource
166 #ifdef G_THREADS_ENABLED
167 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
168 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
169 #define G_THREAD_SELF g_thread_self ()
171 #define LOCK_CONTEXT(context) (void)0
172 #define UNLOCK_CONTEXT(context) (void)0
173 #define G_THREAD_SELF NULL
176 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
178 #define SOURCE_UNREF(source, context) \
180 if ((source)->ref_count > 1) \
181 (source)->ref_count--; \
183 g_source_unref_internal ((source), (context), TRUE); \
187 /* Forward declarations */
189 static void g_source_unref_internal (GSource *source,
190 GMainContext *context,
192 static void g_source_destroy_internal (GSource *source,
193 GMainContext *context,
195 static void g_main_context_poll (GMainContext *context,
200 static void g_main_context_add_poll_unlocked (GMainContext *context,
203 static void g_main_context_remove_poll_unlocked (GMainContext *context,
205 static void g_main_context_wakeup_unlocked (GMainContext *context);
207 static gboolean g_timeout_prepare (GSource *source,
209 static gboolean g_timeout_check (GSource *source);
210 static gboolean g_timeout_dispatch (GSource *source,
211 GSourceFunc callback,
213 static gboolean g_idle_prepare (GSource *source,
215 static gboolean g_idle_check (GSource *source);
216 static gboolean g_idle_dispatch (GSource *source,
217 GSourceFunc callback,
220 G_LOCK_DEFINE_STATIC (main_loop);
221 static GMainContext *default_main_context;
222 static GSList *main_contexts_without_pipe = NULL;
224 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
225 __declspec(dllexport)
227 GSourceFuncs g_timeout_funcs =
235 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
236 __declspec(dllexport)
238 GSourceFuncs g_idle_funcs =
247 /* SunOS has poll, but doesn't provide a prototype. */
248 # if defined (sun) && !defined (__SVR4)
249 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
251 #else /* !HAVE_POLL */
256 g_poll (GPollFD *fds,
260 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
261 gboolean poll_msgs = FALSE;
268 for (f = fds; f < &fds[nfds]; ++f)
271 if (f->fd == G_WIN32_MSG_HANDLE)
275 #ifdef G_MAIN_POLL_DEBUG
276 g_print ("g_poll: waiting for %#x\n", f->fd);
278 handles[nhandles++] = (HANDLE) f->fd;
287 /* Waiting for messages, and maybe events
288 * -> First PeekMessage
290 #ifdef G_MAIN_POLL_DEBUG
291 g_print ("PeekMessage\n");
293 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
294 ready = WAIT_OBJECT_0 + nhandles;
299 /* Waiting just for messages */
300 if (timeout == INFINITE)
305 #ifdef G_MAIN_POLL_DEBUG
306 g_print ("WaitMessage\n");
309 g_warning (G_STRLOC ": WaitMessage() failed");
310 ready = WAIT_OBJECT_0 + nhandles;
312 else if (timeout == 0)
314 /* Waiting just for messages, zero timeout.
315 * If we got here, there was no message
317 ready = WAIT_TIMEOUT;
321 /* Waiting just for messages, some timeout
322 * -> Set a timer, wait for message,
323 * kill timer, use PeekMessage
325 timer = SetTimer (NULL, 0, timeout, NULL);
328 g_warning (G_STRLOC ": SetTimer() failed");
329 ready = WAIT_TIMEOUT;
333 #ifdef G_MAIN_POLL_DEBUG
334 g_print ("WaitMessage\n");
337 KillTimer (NULL, timer);
338 #ifdef G_MAIN_POLL_DEBUG
339 g_print ("PeekMessage\n");
341 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)
342 && msg.message != WM_TIMER)
343 ready = WAIT_OBJECT_0;
345 ready = WAIT_TIMEOUT;
351 /* Wait for either message or event
352 * -> Use MsgWaitForMultipleObjects
354 #ifdef G_MAIN_POLL_DEBUG
355 g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
357 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
358 timeout, QS_ALLINPUT);
360 if (ready == WAIT_FAILED)
361 g_warning (G_STRLOC ": MsgWaitForMultipleObjects() failed");
365 else if (nhandles == 0)
367 /* Wait for nothing (huh?) */
372 /* Wait for just events
373 * -> Use WaitForMultipleObjects
375 #ifdef G_MAIN_POLL_DEBUG
376 g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
378 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
379 if (ready == WAIT_FAILED)
380 g_warning (G_STRLOC ": WaitForMultipleObjects() failed");
383 #ifdef G_MAIN_POLL_DEBUG
384 g_print ("wait returns %ld%s\n",
386 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
387 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
388 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
390 for (f = fds; f < &fds[nfds]; ++f)
393 if (ready == WAIT_FAILED)
395 else if (ready == WAIT_TIMEOUT)
397 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
399 for (f = fds; f < &fds[nfds]; ++f)
402 if (f->events & G_IO_IN)
403 if (f->fd == G_WIN32_MSG_HANDLE)
404 f->revents |= G_IO_IN;
407 #if 1 /* TEST_WITHOUT_THIS */
408 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
409 for (f = fds; f < &fds[nfds]; ++f)
411 if ((f->events & (G_IO_IN | G_IO_OUT))
412 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
414 if (f->events & G_IO_IN)
415 f->revents |= G_IO_IN;
417 f->revents |= G_IO_OUT;
418 #ifdef G_MAIN_POLL_DEBUG
419 g_print ("g_poll: got event %#x\n", f->fd);
422 ResetEvent ((HANDLE) f->fd);
431 #else /* !G_OS_WIN32 */
433 /* The following implementation of poll() comes from the GNU C Library.
434 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
437 #include <string.h> /* for bzero on BSD systems */
439 #ifdef HAVE_SYS_SELECT_H
440 #include <sys/select.h>
441 #endif /* HAVE_SYS_SELECT_H */
445 #endif /* G_OS_BEOS */
448 # define SELECT_MASK fd_set
449 #else /* !NO_FD_SET */
451 typedef long fd_mask;
454 # define SELECT_MASK void
456 # define SELECT_MASK int
457 # endif /* !_IBMR2 */
458 #endif /* !NO_FD_SET */
461 g_poll (GPollFD *fds,
466 SELECT_MASK rset, wset, xset;
475 for (f = fds; f < &fds[nfds]; ++f)
478 if (f->events & G_IO_IN)
479 FD_SET (f->fd, &rset);
480 if (f->events & G_IO_OUT)
481 FD_SET (f->fd, &wset);
482 if (f->events & G_IO_PRI)
483 FD_SET (f->fd, &xset);
484 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
488 tv.tv_sec = timeout / 1000;
489 tv.tv_usec = (timeout % 1000) * 1000;
491 ready = select (maxfd + 1, &rset, &wset, &xset,
492 timeout == -1 ? NULL : &tv);
494 for (f = fds; f < &fds[nfds]; ++f)
499 if (FD_ISSET (f->fd, &rset))
500 f->revents |= G_IO_IN;
501 if (FD_ISSET (f->fd, &wset))
502 f->revents |= G_IO_OUT;
503 if (FD_ISSET (f->fd, &xset))
504 f->revents |= G_IO_PRI;
511 #endif /* !G_OS_WIN32 */
513 #endif /* !HAVE_POLL */
516 * g_main_context_ref:
517 * @context: a #GMainContext
519 * Increases the reference count on a #GMainContext object by one.
522 g_main_context_ref (GMainContext *context)
524 g_return_if_fail (context != NULL);
525 g_return_if_fail (context->ref_count > 0);
527 LOCK_CONTEXT (context);
529 context->ref_count++;
531 UNLOCK_CONTEXT (context);
535 g_main_context_unref_and_unlock (GMainContext *context)
539 context->ref_count--;
541 if (context->ref_count != 0)
543 UNLOCK_CONTEXT (context);
547 source = context->source_list;
550 GSource *next = source->next;
551 g_source_destroy_internal (source, context, TRUE);
554 UNLOCK_CONTEXT (context);
556 #ifdef G_THREADS_ENABLED
557 g_static_mutex_free (&context->mutex);
560 g_ptr_array_free (context->pending_dispatches, TRUE);
561 g_free (context->cached_poll_array);
563 g_mem_chunk_destroy (context->poll_chunk);
565 #ifdef G_THREADS_ENABLED
566 if (g_thread_supported())
569 close (context->wake_up_pipe[0]);
570 close (context->wake_up_pipe[1]);
572 CloseHandle (context->wake_up_semaphore);
576 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
584 * g_main_context_unref:
585 * @context: a #GMainContext
587 * Decreases the reference count on a #GMainContext object by one. If
588 * the result is zero, free the context and free all associated memory.
591 g_main_context_unref (GMainContext *context)
593 g_return_if_fail (context != NULL);
594 g_return_if_fail (context->ref_count > 0);
596 LOCK_CONTEXT (context);
597 g_main_context_unref_and_unlock (context);
600 #ifdef G_THREADS_ENABLED
602 g_main_context_init_pipe (GMainContext *context)
605 if (pipe (context->wake_up_pipe) < 0)
606 g_error ("Cannot create pipe main loop wake-up: %s\n",
609 context->wake_up_rec.fd = context->wake_up_pipe[0];
610 context->wake_up_rec.events = G_IO_IN;
612 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
613 if (context->wake_up_semaphore == NULL)
614 g_error ("Cannot create wake-up semaphore: %s",
615 g_win32_error_message (GetLastError ()));
616 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
617 context->wake_up_rec.events = G_IO_IN;
618 # ifdef G_MAIN_POLL_DEBUG
619 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
622 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
626 g_main_thread_init ()
628 GSList *curr = main_contexts_without_pipe;
631 g_main_context_init_pipe ((GMainContext *)curr->data);
634 g_slist_free (main_contexts_without_pipe);
635 main_contexts_without_pipe = NULL;
637 #endif /* G_THREADS_ENABLED */
640 * g_main_context_new:
642 * Creates a new #GMainContext strcuture
644 * Return value: the new #GMainContext
647 g_main_context_new ()
649 GMainContext *context = g_new0 (GMainContext, 1);
651 #ifdef G_THREADS_ENABLED
652 g_static_mutex_init (&context->mutex);
654 context->owner = NULL;
655 context->waiters = NULL;
658 context->ref_count = 1;
660 context->next_id = 1;
662 context->source_list = NULL;
665 context->poll_func = (GPollFunc)poll;
667 context->poll_func = g_poll;
670 context->cached_poll_array = NULL;
671 context->cached_poll_array_size = 0;
673 context->pending_dispatches = g_ptr_array_new ();
675 context->time_is_current = FALSE;
677 #ifdef G_THREADS_ENABLED
678 if (g_thread_supported ())
679 g_main_context_init_pipe (context);
681 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
689 * g_main_context_default:
691 * Returns the default main context. This is the main context used
692 * for main loop functions when a main loop is not explicitly
695 * Return value: the default main context.
698 g_main_context_default (void)
704 if (!default_main_context)
705 default_main_context = g_main_context_new ();
707 G_UNLOCK (main_loop);
709 return default_main_context;
712 /* Hooks for adding to the main loop */
716 * @source_funcs: structure containing functions that implement
717 * the sources behavior.
718 * @struct_size: size of the #GSource structure to create.
720 * Creates a new #GSource structure. The size is specified to
721 * allow creating structures derived from #GSource that contain
722 * additional data. The size passed in must be at least
723 * <literal>sizeof (GSource)</literal>.
725 * The source will not initially be associated with any #GMainContext
726 * and must be added to one with g_source_attach() before it will be
729 * Return value: the newly-created #GSource.
732 g_source_new (GSourceFuncs *source_funcs,
737 g_return_val_if_fail (source_funcs != NULL, NULL);
738 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
740 source = (GSource*) g_malloc0 (struct_size);
742 source->source_funcs = source_funcs;
743 source->ref_count = 1;
745 source->priority = G_PRIORITY_DEFAULT;
747 source->flags = G_HOOK_FLAG_ACTIVE;
749 /* NULL/0 initialization for all other fields */
754 /* Holds context's lock
757 g_source_list_add (GSource *source,
758 GMainContext *context)
760 GSource *tmp_source, *last_source;
763 tmp_source = context->source_list;
764 while (tmp_source && tmp_source->priority <= source->priority)
766 last_source = tmp_source;
767 tmp_source = tmp_source->next;
770 source->next = tmp_source;
772 tmp_source->prev = source;
774 source->prev = last_source;
776 last_source->next = source;
778 context->source_list = source;
781 /* Holds context's lock
784 g_source_list_remove (GSource *source,
785 GMainContext *context)
788 source->prev->next = source->next;
790 context->source_list = source->next;
793 source->next->prev = source->prev;
801 * @source: a #GSource
802 * @context: a #GMainContext (if %NULL, the default context will be used)
804 * Adds a #GSource to a @context so that it will be executed within
807 * Return value: the ID for the source within the #GMainContext
810 g_source_attach (GSource *source,
811 GMainContext *context)
816 g_return_val_if_fail (source->context == NULL, 0);
817 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
820 context = g_main_context_default ();
822 LOCK_CONTEXT (context);
824 source->context = context;
825 result = source->source_id = context->next_id++;
828 g_source_list_add (source, context);
830 tmp_list = source->poll_fds;
833 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
834 tmp_list = tmp_list->next;
837 #ifdef G_THREADS_ENABLED
838 /* Now wake up the main loop if it is waiting in the poll() */
839 g_main_context_wakeup_unlocked (context);
842 UNLOCK_CONTEXT (context);
848 g_source_destroy_internal (GSource *source,
849 GMainContext *context,
853 LOCK_CONTEXT (context);
855 if (!SOURCE_DESTROYED (source))
858 gpointer old_cb_data;
859 GSourceCallbackFuncs *old_cb_funcs;
861 source->flags &= ~G_HOOK_FLAG_ACTIVE;
863 old_cb_data = source->callback_data;
864 old_cb_funcs = source->callback_funcs;
866 source->callback_data = NULL;
867 source->callback_funcs = NULL;
871 UNLOCK_CONTEXT (context);
872 old_cb_funcs->unref (old_cb_data);
873 LOCK_CONTEXT (context);
876 tmp_list = source->poll_fds;
879 g_main_context_remove_poll_unlocked (context, tmp_list->data);
880 tmp_list = tmp_list->next;
883 g_source_unref_internal (source, context, TRUE);
887 UNLOCK_CONTEXT (context);
892 * @source: a #GSource
894 * Removes a source from its #GMainContext, if any, and mark it as
895 * destroyed. The source cannot be subsequently added to another
899 g_source_destroy (GSource *source)
901 GMainContext *context;
903 g_return_if_fail (source != NULL);
905 context = source->context;
908 g_source_destroy_internal (source, context, FALSE);
910 source->flags &= ~G_HOOK_FLAG_ACTIVE;
915 * @source: a #GSource
917 * Returns the numeric ID for a particular source. The ID of a source
918 * is unique within a particular main loop context. The reverse
919 * mapping from ID to source is done by g_main_context_find_source_by_id().
921 * Return value: the ID for the source
924 g_source_get_id (GSource *source)
928 g_return_val_if_fail (source != NULL, 0);
929 g_return_val_if_fail (source->context != NULL, 0);
931 LOCK_CONTEXT (source->context);
932 result = source->source_id;
933 UNLOCK_CONTEXT (source->context);
939 * g_source_get_context:
940 * @source: a #GSource
942 * Gets the #GMainContext with which the source is associated.
943 * Calling this function on a destroyed source is an error.
945 * Return value: the #GMainContext with which the source is associated,
946 * or %NULL if the context has not yet been added
950 g_source_get_context (GSource *source)
952 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
954 return source->context;
960 * @fd: a #GPollFD structure holding information about a file
961 * descriptor to watch.
963 * Adds a file descriptor to the set of file descriptors polled for
964 * this source. This is usually combined with g_source_new() to add an
965 * event source. The event source's check function will typically test
966 * the @revents field in the #GPollFD struct and return %TRUE if events need
970 g_source_add_poll (GSource *source,
973 GMainContext *context;
975 g_return_if_fail (source != NULL);
976 g_return_if_fail (fd != NULL);
977 g_return_if_fail (!SOURCE_DESTROYED (source));
979 context = source->context;
982 LOCK_CONTEXT (context);
984 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
988 g_main_context_add_poll_unlocked (context, source->priority, fd);
989 UNLOCK_CONTEXT (context);
994 * g_source_remove_poll:
996 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
998 * Removes a file descriptor from the set of file descriptors polled for
1002 g_source_remove_poll (GSource *source,
1005 GMainContext *context;
1007 g_return_if_fail (source != NULL);
1008 g_return_if_fail (fd != NULL);
1009 g_return_if_fail (!SOURCE_DESTROYED (source));
1011 context = source->context;
1014 LOCK_CONTEXT (context);
1016 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1020 g_main_context_remove_poll_unlocked (context, fd);
1021 UNLOCK_CONTEXT (context);
1026 * g_source_set_callback_indirect:
1027 * @source: the source
1028 * @callback_data: pointer to callback data "object"
1029 * @callback_funcs: functions for reference counting @callback_data
1030 * and getting the callback and data
1032 * Sets the callback function storing the data as a refcounted callback
1033 * "object". This is used internally. Note that calling
1034 * g_source_set_callback_indirect() assumes
1035 * an initial reference count on @callback_data, and thus
1036 * @callback_funcs->unref will eventually be called once more
1037 * than @callback_funcs->ref.
1040 g_source_set_callback_indirect (GSource *source,
1041 gpointer callback_data,
1042 GSourceCallbackFuncs *callback_funcs)
1044 GMainContext *context;
1045 gpointer old_cb_data;
1046 GSourceCallbackFuncs *old_cb_funcs;
1048 g_return_if_fail (source != NULL);
1049 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1051 context = source->context;
1054 LOCK_CONTEXT (context);
1056 old_cb_data = source->callback_data;
1057 old_cb_funcs = source->callback_funcs;
1059 source->callback_data = callback_data;
1060 source->callback_funcs = callback_funcs;
1063 UNLOCK_CONTEXT (context);
1066 old_cb_funcs->unref (old_cb_data);
1070 g_source_callback_ref (gpointer cb_data)
1072 GSourceCallback *callback = cb_data;
1074 callback->ref_count++;
1079 g_source_callback_unref (gpointer cb_data)
1081 GSourceCallback *callback = cb_data;
1083 callback->ref_count--;
1084 if (callback->ref_count == 0)
1086 if (callback->notify)
1087 callback->notify (callback->data);
1093 g_source_callback_get (gpointer cb_data,
1098 GSourceCallback *callback = cb_data;
1100 *func = callback->func;
1101 *data = callback->data;
1104 static GSourceCallbackFuncs g_source_callback_funcs = {
1105 g_source_callback_ref,
1106 g_source_callback_unref,
1107 g_source_callback_get,
1111 * g_source_set_callback:
1112 * @source: the source
1113 * @func: a callback function
1114 * @data: the data to pass to callback function
1115 * @notify: a function to call when @data is no longer in use, or %NULL.
1117 * Sets the callback function for a source. The callback for a source is
1118 * called from the source's dispatch function.
1120 * The exact type of @func depends on the type of source; ie. you
1121 * should not count on @func being called with @data as its first
1124 * Typically, you won't use this function. Instead use functions specific
1125 * to the type of source you are using.
1128 g_source_set_callback (GSource *source,
1131 GDestroyNotify notify)
1133 GSourceCallback *new_callback;
1135 g_return_if_fail (source != NULL);
1137 new_callback = g_new (GSourceCallback, 1);
1139 new_callback->ref_count = 1;
1140 new_callback->func = func;
1141 new_callback->data = data;
1142 new_callback->notify = notify;
1144 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1148 * g_source_set_priority:
1149 * @source: a #GSource
1150 * @priority: the new priority.
1152 * Sets the priority of a source. While the main loop is being
1153 * run, a source will be dispatched if it is ready to be dispatched and no sources
1154 * at a higher (numerically smaller) priority are ready to be dispatched.
1157 g_source_set_priority (GSource *source,
1161 GMainContext *context;
1163 g_return_if_fail (source != NULL);
1165 context = source->context;
1168 LOCK_CONTEXT (context);
1170 source->priority = priority;
1174 source->next = NULL;
1175 source->prev = NULL;
1177 tmp_list = source->poll_fds;
1180 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1181 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1183 tmp_list = tmp_list->next;
1186 UNLOCK_CONTEXT (source->context);
1191 * g_source_get_priority:
1192 * @source: a #GSource
1194 * Gets the priority of a source.
1196 * Return value: the priority of the source
1199 g_source_get_priority (GSource *source)
1201 g_return_val_if_fail (source != NULL, 0);
1203 return source->priority;
1207 * g_source_set_can_recurse:
1208 * @source: a #GSource
1209 * @can_recurse: whether recursion is allowed for this source
1211 * Sets whether a source can be called recursively. If @can_recurse is
1212 * %TRUE, then while the source is being dispatched then this source
1213 * will be processed normally. Otherwise, all processing of this
1214 * source is blocked until the dispatch function returns.
1217 g_source_set_can_recurse (GSource *source,
1218 gboolean can_recurse)
1220 GMainContext *context;
1222 g_return_if_fail (source != NULL);
1224 context = source->context;
1227 LOCK_CONTEXT (context);
1230 source->flags |= G_SOURCE_CAN_RECURSE;
1232 source->flags &= ~G_SOURCE_CAN_RECURSE;
1235 UNLOCK_CONTEXT (context);
1239 * g_source_get_can_recurse:
1240 * @source: a #GSource
1242 * Checks whether a source is allowed to be called recursively.
1243 * see g_source_set_can_recurse().
1245 * Return value: whether recursion is allowed.
1248 g_source_get_can_recurse (GSource *source)
1250 g_return_val_if_fail (source != NULL, FALSE);
1252 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1257 * @source: a #GSource
1259 * Increases the reference count on a source by one.
1261 * Return value: @source
1264 g_source_ref (GSource *source)
1266 GMainContext *context;
1268 g_return_val_if_fail (source != NULL, NULL);
1270 context = source->context;
1273 LOCK_CONTEXT (context);
1275 source->ref_count++;
1278 UNLOCK_CONTEXT (context);
1283 /* g_source_unref() but possible to call within context lock
1286 g_source_unref_internal (GSource *source,
1287 GMainContext *context,
1290 gpointer old_cb_data = NULL;
1291 GSourceCallbackFuncs *old_cb_funcs = NULL;
1293 g_return_if_fail (source != NULL);
1295 if (!have_lock && context)
1296 LOCK_CONTEXT (context);
1298 source->ref_count--;
1299 if (source->ref_count == 0)
1301 old_cb_data = source->callback_data;
1302 old_cb_funcs = source->callback_funcs;
1304 source->callback_data = NULL;
1305 source->callback_funcs = NULL;
1307 if (context && !SOURCE_DESTROYED (source))
1309 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1310 source->ref_count++;
1313 g_source_list_remove (source, context);
1315 if (source->source_funcs->finalize)
1316 source->source_funcs->finalize (source);
1318 g_slist_free (source->poll_fds);
1319 source->poll_fds = NULL;
1323 if (!have_lock && context)
1324 UNLOCK_CONTEXT (context);
1329 UNLOCK_CONTEXT (context);
1331 old_cb_funcs->unref (old_cb_data);
1334 LOCK_CONTEXT (context);
1340 * @source: a #GSource
1342 * Decreases the reference count of a source by one. If the
1343 * resulting reference count is zero the source and associated
1344 * memory will be destroyed.
1347 g_source_unref (GSource *source)
1349 g_return_if_fail (source != NULL);
1351 g_source_unref_internal (source, source->context, FALSE);
1355 * g_main_context_find_source_by_id:
1356 * @context: a #GMainContext (if %NULL, the default context will be used)
1357 * @source_id: the source ID, as returned by g_source_get_id()
1359 * Finds a #GSource given a pair of context and ID
1361 * Return value: the #GSource if found, otherwise, %NULL
1364 g_main_context_find_source_by_id (GMainContext *context,
1369 g_return_val_if_fail (source_id > 0, FALSE);
1371 if (context == NULL)
1372 context = g_main_context_default ();
1374 LOCK_CONTEXT (context);
1376 source = context->source_list;
1379 if (!SOURCE_DESTROYED (source) &&
1380 source->source_id == source_id)
1382 source = source->next;
1385 UNLOCK_CONTEXT (context);
1391 * g_main_context_find_source_by_funcs_user_data:
1392 * @context: a #GMainContext (if %NULL, the default context will be used).
1393 * @funcs: the @source_funcs passed to g_source_new().
1394 * @user_data: the user data from the callback.
1396 * Finds a source with the given source functions and user data. If
1397 * multiple sources exist with the same source function and user data,
1398 * the first one found will be returned.
1400 * Return value: the source, if one was found, otherwise %NULL
1403 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1404 GSourceFuncs *funcs,
1409 g_return_val_if_fail (funcs != NULL, FALSE);
1411 if (context == NULL)
1412 context = g_main_context_default ();
1414 LOCK_CONTEXT (context);
1416 source = context->source_list;
1419 if (!SOURCE_DESTROYED (source) &&
1420 source->source_funcs == funcs &&
1421 source->callback_funcs)
1423 GSourceFunc callback;
1424 gpointer callback_data;
1426 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1428 if (callback_data == user_data)
1431 source = source->next;
1434 UNLOCK_CONTEXT (context);
1440 * g_main_context_find_source_by_user_data:
1441 * @context: a #GMainContext
1442 * @user_data: the user_data for the callback.
1444 * Finds a source with the given user data for the callback. If
1445 * multiple sources exist with the same user data, the first
1446 * one found will be returned.
1448 * Return value: the source, if one was found, otherwise %NULL
1451 g_main_context_find_source_by_user_data (GMainContext *context,
1456 if (context == NULL)
1457 context = g_main_context_default ();
1459 LOCK_CONTEXT (context);
1461 source = context->source_list;
1464 if (!SOURCE_DESTROYED (source) &&
1465 source->callback_funcs)
1467 GSourceFunc callback;
1468 gpointer callback_data = NULL;
1470 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1472 if (callback_data == user_data)
1475 source = source->next;
1478 UNLOCK_CONTEXT (context);
1485 * @tag: the id of the source to remove.
1487 * Removes the source with the given id from the default main
1488 * context. The id of a #GSource is given by g_source_get_id(),
1489 * or will be returned by the functions g_source_attach(),
1490 * g_idle_add(), g_idle_add_full(), g_timeout_add(),
1491 * g_timeout_add_full(), g_io_add_watch, and g_io_add_watch_full().
1493 * See also g_source_destroy().
1495 * Return value: %TRUE if the source was found and removed.
1498 g_source_remove (guint tag)
1502 g_return_val_if_fail (tag > 0, FALSE);
1504 source = g_main_context_find_source_by_id (NULL, tag);
1506 g_source_destroy (source);
1508 return source != NULL;
1512 * g_source_remove_by_user_data:
1513 * @user_data: the user_data for the callback.
1515 * Removes a source from the default main loop context given the user
1516 * data for the callback. If multiple sources exist with the same user
1517 * data, only one will be destroyed.
1519 * Return value: %TRUE if a source was found and removed.
1522 g_source_remove_by_user_data (gpointer user_data)
1526 source = g_main_context_find_source_by_user_data (NULL, user_data);
1529 g_source_destroy (source);
1537 * g_source_remove_by_funcs_user_data:
1538 * @funcs: The @source_funcs passed to g_source_new()
1539 * @user_data: the user data for the callback
1541 * Removes a source from the default main loop context given the
1542 * source functions and user data. If multiple sources exist with the
1543 * same source functions and user data, only one will be destroyed.
1545 * Return value: %TRUE if a source was found and removed.
1548 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1553 g_return_val_if_fail (funcs != NULL, FALSE);
1555 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1558 g_source_destroy (source);
1566 * g_get_current_time:
1567 * @result: #GTimeVal structure in which to store current time.
1569 * Equivalent to the UNIX <function>gettimeofday()</function> function, but portable.
1572 g_get_current_time (GTimeVal *result)
1577 g_return_if_fail (result != NULL);
1579 /*this is required on alpha, there the timeval structs are int's
1580 not longs and a cast only would fail horribly*/
1581 gettimeofday (&r, NULL);
1582 result->tv_sec = r.tv_sec;
1583 result->tv_usec = r.tv_usec;
1585 /* Avoid calling time() except for the first time.
1586 * GetTickCount() should be pretty fast and low-level?
1587 * I could also use ftime() but it seems unnecessarily overheady.
1589 static DWORD start_tick = 0;
1590 static time_t start_time;
1593 g_return_if_fail (result != NULL);
1595 if (start_tick == 0)
1597 start_tick = GetTickCount ();
1601 tick = GetTickCount ();
1603 result->tv_sec = (tick - start_tick) / 1000 + start_time;
1604 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
1608 /* Running the main loop */
1610 /* HOLDS: context's lock */
1612 g_main_dispatch (GMainContext *context)
1616 for (i = 0; i < context->pending_dispatches->len; i++)
1618 GSource *source = context->pending_dispatches->pdata[i];
1620 context->pending_dispatches->pdata[i] = NULL;
1623 source->flags &= ~G_SOURCE_READY;
1625 if (!SOURCE_DESTROYED (source))
1627 gboolean was_in_call;
1628 gpointer user_data = NULL;
1629 GSourceFunc callback = NULL;
1630 GSourceCallbackFuncs *cb_funcs;
1632 gboolean need_destroy;
1634 gboolean (*dispatch) (GSource *,
1638 dispatch = source->source_funcs->dispatch;
1639 cb_funcs = source->callback_funcs;
1640 cb_data = source->callback_data;
1643 cb_funcs->ref (cb_data);
1645 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
1646 source->flags |= G_HOOK_FLAG_IN_CALL;
1649 cb_funcs->get (cb_data, source, &callback, &user_data);
1651 UNLOCK_CONTEXT (context);
1653 need_destroy = ! dispatch (source,
1656 LOCK_CONTEXT (context);
1659 cb_funcs->unref (cb_data);
1662 source->flags &= ~G_HOOK_FLAG_IN_CALL;
1664 /* Note: this depends on the fact that we can't switch
1665 * sources from one main context to another
1667 if (need_destroy && !SOURCE_DESTROYED (source))
1669 g_assert (source->context == context);
1670 g_source_destroy_internal (source, context, TRUE);
1674 SOURCE_UNREF (source, context);
1677 g_ptr_array_set_size (context->pending_dispatches, 0);
1680 /* Holds context's lock */
1681 static inline GSource *
1682 next_valid_source (GMainContext *context,
1685 GSource *new_source = source ? source->next : context->source_list;
1689 if (!SOURCE_DESTROYED (new_source))
1691 new_source->ref_count++;
1695 new_source = new_source->next;
1699 SOURCE_UNREF (source, context);
1705 * g_main_context_acquire:
1706 * @context: a #GMainContext
1708 * Tries to become the owner of the specified context.
1709 * If some other context is the owner of the context,
1710 * returns %FALSE immediately. Ownership is properly
1711 * recursive: the owner can require ownership again
1712 * and will release ownership when g_main_context_release()
1713 * is called as many times as g_main_context_acquire().
1715 * You must be the owner of a context before you
1716 * can call g_main_context_prepare(), g_main_context_query(),
1717 * g_main_context_check(), g_main_context_dispatch().
1719 * Return value: %TRUE if the operation succeeded, and
1720 * this thread is now the owner of @context.
1723 g_main_context_acquire (GMainContext *context)
1725 #ifdef G_THREADS_ENABLED
1726 gboolean result = FALSE;
1727 GThread *self = G_THREAD_SELF;
1729 if (context == NULL)
1730 context = g_main_context_default ();
1732 LOCK_CONTEXT (context);
1734 if (!context->owner)
1736 context->owner = self;
1737 g_assert (context->owner_count == 0);
1740 if (context->owner == self)
1742 context->owner_count++;
1746 UNLOCK_CONTEXT (context);
1749 #else /* !G_THREADS_ENABLED */
1751 #endif /* G_THREADS_ENABLED */
1755 * g_main_context_release:
1756 * @context: a #GMainContext
1758 * Releases ownership of a context previously acquired by this thread
1759 * with g_main_context_acquire(). If the context was acquired multiple
1760 * times, the only release ownership when g_main_context_release()
1761 * is called as many times as it was acquired.
1764 g_main_context_release (GMainContext *context)
1766 #ifdef G_THREADS_ENABLED
1767 if (context == NULL)
1768 context = g_main_context_default ();
1770 LOCK_CONTEXT (context);
1772 context->owner_count--;
1773 if (context->owner_count == 0)
1775 context->owner = NULL;
1777 if (context->waiters)
1779 GMainWaiter *waiter = context->waiters->data;
1780 gboolean loop_internal_waiter =
1781 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
1782 context->waiters = g_slist_delete_link (context->waiters,
1784 if (!loop_internal_waiter)
1785 g_mutex_lock (waiter->mutex);
1787 g_cond_signal (waiter->cond);
1789 if (!loop_internal_waiter)
1790 g_mutex_unlock (waiter->mutex);
1794 UNLOCK_CONTEXT (context);
1795 #endif /* G_THREADS_ENABLED */
1799 * g_main_context_wait:
1800 * @context: a #GMainContext
1801 * @cond: a condition variable
1802 * @mutex: a mutex, currently held
1804 * Tries to become the owner of the specified context,
1805 * as with g_main_context_acquire(). But if another thread
1806 * is the owner, atomically drop @mutex and wait on @cond until
1807 * that owner releases ownership or until @cond is signaled, then
1808 * try again (once) to become the owner.
1810 * Return value: %TRUE if the operation succeeded, and
1811 * this thread is now the owner of @context.
1814 g_main_context_wait (GMainContext *context,
1818 #ifdef G_THREADS_ENABLED
1819 gboolean result = FALSE;
1820 GThread *self = G_THREAD_SELF;
1821 gboolean loop_internal_waiter;
1823 if (context == NULL)
1824 context = g_main_context_default ();
1826 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
1828 if (!loop_internal_waiter)
1829 LOCK_CONTEXT (context);
1831 if (context->owner && context->owner != self)
1836 waiter.mutex = mutex;
1838 context->waiters = g_slist_append (context->waiters, &waiter);
1840 if (!loop_internal_waiter)
1841 UNLOCK_CONTEXT (context);
1842 g_cond_wait (cond, mutex);
1843 if (!loop_internal_waiter)
1844 LOCK_CONTEXT (context);
1846 context->waiters = g_slist_remove (context->waiters, &waiter);
1849 if (!context->owner)
1851 context->owner = self;
1852 g_assert (context->owner_count == 0);
1855 if (context->owner == self)
1857 context->owner_count++;
1861 if (!loop_internal_waiter)
1862 UNLOCK_CONTEXT (context);
1865 #else /* !G_THREADS_ENABLED */
1867 #endif /* G_THREADS_ENABLED */
1871 * g_main_context_prepare:
1872 * @context: a #GMainContext
1873 * @priority: location to store priority of highest priority
1874 * source already ready.
1876 * Prepares to poll sources within a main loop. The resulting information
1877 * for polling is determined by calling g_main_context_query ().
1879 * Return value: %TRUE if some source is ready to be dispatched
1883 g_main_context_prepare (GMainContext *context,
1888 gint current_priority = G_MAXINT;
1891 if (context == NULL)
1892 context = g_main_context_default ();
1894 LOCK_CONTEXT (context);
1896 context->time_is_current = FALSE;
1898 if (context->in_check_or_prepare)
1900 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
1901 "prepare() member.");
1902 UNLOCK_CONTEXT (context);
1906 #ifdef G_THREADS_ENABLED
1907 if (context->poll_waiting)
1909 g_warning("g_main_context_prepare(): main loop already active in another thread");
1910 UNLOCK_CONTEXT (context);
1914 context->poll_waiting = TRUE;
1915 #endif /* G_THREADS_ENABLED */
1918 /* If recursing, finish up current dispatch, before starting over */
1919 if (context->pending_dispatches)
1922 g_main_dispatch (context, ¤t_time);
1924 UNLOCK_CONTEXT (context);
1929 /* If recursing, clear list of pending dispatches */
1931 for (i = 0; i < context->pending_dispatches->len; i++)
1933 if (context->pending_dispatches->pdata[i])
1934 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
1936 g_ptr_array_set_size (context->pending_dispatches, 0);
1938 /* Prepare all sources */
1940 context->timeout = -1;
1942 source = next_valid_source (context, NULL);
1945 gint source_timeout = -1;
1947 if ((n_ready > 0) && (source->priority > current_priority))
1949 SOURCE_UNREF (source, context);
1952 if ((source->flags & G_HOOK_FLAG_IN_CALL) && !(source->flags & G_SOURCE_CAN_RECURSE))
1955 if (!(source->flags & G_SOURCE_READY))
1958 gboolean (*prepare) (GSource *source,
1961 prepare = source->source_funcs->prepare;
1962 context->in_check_or_prepare++;
1963 UNLOCK_CONTEXT (context);
1965 result = (*prepare) (source, &source_timeout);
1967 LOCK_CONTEXT (context);
1968 context->in_check_or_prepare--;
1971 source->flags |= G_SOURCE_READY;
1974 if (source->flags & G_SOURCE_READY)
1977 current_priority = source->priority;
1978 context->timeout = 0;
1981 if (source_timeout >= 0)
1983 if (context->timeout < 0)
1984 context->timeout = source_timeout;
1986 context->timeout = MIN (context->timeout, source_timeout);
1990 source = next_valid_source (context, source);
1993 UNLOCK_CONTEXT (context);
1996 *priority = current_priority;
1998 return (n_ready > 0);
2002 * g_main_context_query:
2003 * @context: a #GMainContext
2004 * @max_priority: maximum priority source to check
2005 * @timeout: location to store timeout to be used in polling
2006 * @fds: location to store #GPollFD records that need to be polled.
2007 * @n_fds: length of @fds.
2009 * Determines information necessary to poll this main loop.
2011 * Return value: the number of records actually stored in @fds,
2012 * or, if more than @n_fds records need to be stored, the number
2013 * of records that need to be stored.
2016 g_main_context_query (GMainContext *context,
2025 LOCK_CONTEXT (context);
2027 pollrec = context->poll_records;
2029 while (pollrec && max_priority >= pollrec->priority)
2031 if (pollrec->fd->events)
2035 fds[n_poll].fd = pollrec->fd->fd;
2036 /* In direct contradiction to the Unix98 spec, IRIX runs into
2037 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2038 * flags in the events field of the pollfd while it should
2039 * just ignoring them. So we mask them out here.
2041 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2042 fds[n_poll].revents = 0;
2047 pollrec = pollrec->next;
2050 #ifdef G_THREADS_ENABLED
2051 context->poll_changed = FALSE;
2056 *timeout = context->timeout;
2058 context->time_is_current = FALSE;
2061 UNLOCK_CONTEXT (context);
2067 * g_main_context_check:
2068 * @context: a #GMainContext
2069 * @max_priority: the maximum numerical priority of sources to check
2070 * @fds: array of #GPollFD's that was passed to the last call to
2071 * g_main_context_query()
2072 * @n_fds: return value of g_main_context_query()
2074 * Passes the results of polling back to the main loop.
2076 * Return value: %TRUE if some sources are ready to be dispatched.
2079 g_main_context_check (GMainContext *context,
2089 LOCK_CONTEXT (context);
2091 if (context->in_check_or_prepare)
2093 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2094 "prepare() member.");
2095 UNLOCK_CONTEXT (context);
2099 #ifdef G_THREADS_ENABLED
2100 if (!context->poll_waiting)
2104 read (context->wake_up_pipe[0], &c, 1);
2108 context->poll_waiting = FALSE;
2110 /* If the set of poll file descriptors changed, bail out
2111 * and let the main loop rerun
2113 if (context->poll_changed)
2115 UNLOCK_CONTEXT (context);
2118 #endif /* G_THREADS_ENABLED */
2120 pollrec = context->poll_records;
2124 if (pollrec->fd->events)
2126 pollrec->fd->revents = fds[i].revents;
2129 pollrec = pollrec->next;
2132 source = next_valid_source (context, NULL);
2135 if ((n_ready > 0) && (source->priority > max_priority))
2137 SOURCE_UNREF (source, context);
2140 if ((source->flags & G_HOOK_FLAG_IN_CALL) && !(source->flags & G_SOURCE_CAN_RECURSE))
2143 if (!(source->flags & G_SOURCE_READY))
2146 gboolean (*check) (GSource *source);
2148 check = source->source_funcs->check;
2150 context->in_check_or_prepare++;
2151 UNLOCK_CONTEXT (context);
2153 result = (*check) (source);
2155 LOCK_CONTEXT (context);
2156 context->in_check_or_prepare--;
2159 source->flags |= G_SOURCE_READY;
2162 if (source->flags & G_SOURCE_READY)
2164 source->ref_count++;
2165 g_ptr_array_add (context->pending_dispatches, source);
2169 /* never dispatch sources with less priority than the first
2170 * one we choose to dispatch
2172 max_priority = source->priority;
2176 source = next_valid_source (context, source);
2179 UNLOCK_CONTEXT (context);
2185 * g_main_context_dispatch:
2186 * @context: a #GMainContext
2188 * Dispatches all pending sources.
2191 g_main_context_dispatch (GMainContext *context)
2193 LOCK_CONTEXT (context);
2195 if (context->pending_dispatches->len > 0)
2197 g_main_dispatch (context);
2200 UNLOCK_CONTEXT (context);
2203 /* HOLDS context lock */
2205 g_main_context_iterate (GMainContext *context,
2212 gboolean some_ready;
2213 gint nfds, allocated_nfds;
2214 GPollFD *fds = NULL;
2216 UNLOCK_CONTEXT (context);
2218 #ifdef G_THREADS_ENABLED
2219 if (!g_main_context_acquire (context))
2221 gboolean got_ownership;
2223 g_return_val_if_fail (g_thread_supported (), FALSE);
2228 LOCK_CONTEXT (context);
2231 context->cond = g_cond_new ();
2233 got_ownership = g_main_context_wait (context,
2235 g_static_mutex_get_mutex (&context->mutex));
2239 UNLOCK_CONTEXT (context);
2244 LOCK_CONTEXT (context);
2245 #endif /* G_THREADS_ENABLED */
2247 if (!context->cached_poll_array)
2249 context->cached_poll_array_size = context->n_poll_records;
2250 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2253 allocated_nfds = context->cached_poll_array_size;
2254 fds = context->cached_poll_array;
2256 UNLOCK_CONTEXT (context);
2258 some_ready = g_main_context_prepare (context, &max_priority);
2260 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2261 allocated_nfds)) > allocated_nfds)
2263 LOCK_CONTEXT (context);
2265 context->cached_poll_array_size = allocated_nfds = nfds;
2266 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2267 UNLOCK_CONTEXT (context);
2273 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2275 g_main_context_check (context, max_priority, fds, nfds);
2278 g_main_context_dispatch (context);
2280 #ifdef G_THREADS_ENABLED
2281 g_main_context_release (context);
2282 #endif /* G_THREADS_ENABLED */
2284 LOCK_CONTEXT (context);
2290 * g_main_context_pending:
2291 * @context: a #GMainContext (if %NULL, the default context will be used)
2293 * Checks if any sources have pending events for the given context.
2295 * Return value: %TRUE if events are pending.
2298 g_main_context_pending (GMainContext *context)
2303 context = g_main_context_default();
2305 LOCK_CONTEXT (context);
2306 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2307 UNLOCK_CONTEXT (context);
2313 * g_main_context_iteration:
2314 * @context: a #GMainContext (if %NULL, the default context will be used)
2315 * @may_block: whether the call may block.
2317 * Runs a single iteration for the given main loop. This involves
2318 * checking to see if any event sources are ready to be processed,
2319 * then if no events sources are ready and @may_block is %TRUE, waiting
2320 * for a source to become ready, then dispatching the highest priority
2321 * events sources that are ready. Note that even when @may_block is %TRUE,
2322 * it is still possible for g_main_context_iteration() to return
2323 * %FALSE, since the the wait may be interrupted for other
2324 * reasons than an event source becoming ready.
2326 * Return value: %TRUE if events were dispatched.
2329 g_main_context_iteration (GMainContext *context, gboolean may_block)
2334 context = g_main_context_default();
2336 LOCK_CONTEXT (context);
2337 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2338 UNLOCK_CONTEXT (context);
2345 * @context: a #GMainContext (if %NULL, the default context will be used).
2346 * @is_running: set to %TRUE to indicate that the loop is running. This
2347 * is not very important since calling g_main_loop_run() will set this to
2350 * Creates a new #GMainLoop structure.
2352 * Return value: a new #GMainLoop.
2355 g_main_loop_new (GMainContext *context,
2356 gboolean is_running)
2361 context = g_main_context_default();
2363 g_main_context_ref (context);
2365 loop = g_new0 (GMainLoop, 1);
2366 loop->context = context;
2367 loop->is_running = is_running != FALSE;
2368 loop->ref_count = 1;
2375 * @loop: a #GMainLoop
2377 * Increases the reference count on a #GMainLoop object by one.
2379 * Return value: @loop
2382 g_main_loop_ref (GMainLoop *loop)
2384 g_return_val_if_fail (loop != NULL, NULL);
2385 g_return_val_if_fail (loop->ref_count > 0, NULL);
2387 LOCK_CONTEXT (loop->context);
2389 UNLOCK_CONTEXT (loop->context);
2395 g_main_loop_unref_and_unlock (GMainLoop *loop)
2398 if (loop->ref_count == 0)
2400 /* When the ref_count is 0, there can be nobody else using the
2401 * loop, so it is safe to unlock before destroying.
2403 g_main_context_unref_and_unlock (loop->context);
2407 UNLOCK_CONTEXT (loop->context);
2411 * g_main_loop_unref:
2412 * @loop: a #GMainLoop
2414 * Decreases the reference count on a #GMainLoop object by one. If
2415 * the result is zero, free the loop and free all associated memory.
2418 g_main_loop_unref (GMainLoop *loop)
2420 g_return_if_fail (loop != NULL);
2421 g_return_if_fail (loop->ref_count > 0);
2423 LOCK_CONTEXT (loop->context);
2425 g_main_loop_unref_and_unlock (loop);
2430 * @loop: a #GMainLoop
2432 * Runs a main loop until g_main_loop_quit() is called on the loop.
2433 * If this is called for the thread of the loop's #GMainContext,
2434 * it will process events from the loop, otherwise it will
2438 g_main_loop_run (GMainLoop *loop)
2440 GThread *self = G_THREAD_SELF;
2442 g_return_if_fail (loop != NULL);
2443 g_return_if_fail (loop->ref_count > 0);
2445 #ifdef G_THREADS_ENABLED
2446 if (!g_main_context_acquire (loop->context))
2448 gboolean got_ownership = FALSE;
2450 /* Another thread owns this context */
2451 if (!g_thread_supported ())
2453 g_warning ("g_main_loop_run() was called from second thread but "
2454 "g_thread_init() was never called.");
2458 LOCK_CONTEXT (loop->context);
2462 if (!loop->is_running)
2463 loop->is_running = TRUE;
2465 if (!loop->context->cond)
2466 loop->context->cond = g_cond_new ();
2468 while (loop->is_running || !got_ownership)
2469 got_ownership = g_main_context_wait (loop->context,
2470 loop->context->cond,
2471 g_static_mutex_get_mutex (&loop->context->mutex));
2473 if (!loop->is_running)
2475 UNLOCK_CONTEXT (loop->context);
2477 g_main_context_release (loop->context);
2478 g_main_loop_unref (loop);
2482 g_assert (got_ownership);
2485 LOCK_CONTEXT (loop->context);
2486 #endif /* G_THREADS_ENABLED */
2488 if (loop->context->in_check_or_prepare)
2490 g_warning ("g_main_loop_run(): called recursively from within a source's "
2491 "check() or prepare() member, iteration not possible.");
2496 loop->is_running = TRUE;
2497 while (loop->is_running)
2498 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2500 UNLOCK_CONTEXT (loop->context);
2502 #ifdef G_THREADS_ENABLED
2503 g_main_context_release (loop->context);
2504 #endif /* G_THREADS_ENABLED */
2506 g_main_loop_unref (loop);
2511 * @loop: a #GMainLoop
2513 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2514 * for the loop will return.
2517 g_main_loop_quit (GMainLoop *loop)
2519 g_return_if_fail (loop != NULL);
2520 g_return_if_fail (loop->ref_count > 0);
2522 LOCK_CONTEXT (loop->context);
2523 loop->is_running = FALSE;
2524 g_main_context_wakeup_unlocked (loop->context);
2526 #ifdef G_THREADS_ENABLED
2527 if (loop->context->cond)
2528 g_cond_broadcast (loop->context->cond);
2529 #endif /* G_THREADS_ENABLED */
2531 UNLOCK_CONTEXT (loop->context);
2535 * g_main_loop_is_running:
2536 * @loop: a #GMainLoop.
2538 * Checks to see if the main loop is currently being run via g_main_loop_run().
2540 * Return value: %TRUE if the mainloop is currently being run.
2543 g_main_loop_is_running (GMainLoop *loop)
2545 g_return_val_if_fail (loop != NULL, FALSE);
2546 g_return_val_if_fail (loop->ref_count > 0, FALSE);
2548 return loop->is_running;
2552 * g_main_loop_get_context:
2553 * @loop: a #GMainLoop.
2555 * Returns the #GMainContext of @loop.
2557 * Return value: the #GMainContext of @loop
2560 g_main_loop_get_context (GMainLoop *loop)
2562 g_return_val_if_fail (loop != NULL, NULL);
2563 g_return_val_if_fail (loop->ref_count > 0, NULL);
2565 return loop->context;
2568 /* HOLDS: context's lock */
2570 g_main_context_poll (GMainContext *context,
2576 #ifdef G_MAIN_POLL_DEBUG
2582 GPollFunc poll_func;
2584 if (n_fds || timeout != 0)
2586 #ifdef G_MAIN_POLL_DEBUG
2587 g_print ("g_main_poll(%d) timeout: %d\n", n_fds, timeout);
2588 poll_timer = g_timer_new ();
2591 LOCK_CONTEXT (context);
2593 poll_func = context->poll_func;
2595 UNLOCK_CONTEXT (context);
2596 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2597 g_warning ("poll(2) failed due to: %s.",
2598 g_strerror (errno));
2600 #ifdef G_MAIN_POLL_DEBUG
2601 LOCK_CONTEXT (context);
2603 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
2606 g_timer_elapsed (poll_timer, NULL));
2607 g_timer_destroy (poll_timer);
2608 pollrec = context->poll_records;
2612 if (pollrec->fd->events)
2616 g_print (" [%d:", fds[i].fd);
2617 if (fds[i].revents & G_IO_IN)
2619 if (fds[i].revents & G_IO_OUT)
2621 if (fds[i].revents & G_IO_PRI)
2623 if (fds[i].revents & G_IO_ERR)
2625 if (fds[i].revents & G_IO_HUP)
2627 if (fds[i].revents & G_IO_NVAL)
2633 pollrec = pollrec->next;
2637 UNLOCK_CONTEXT (context);
2639 } /* if (n_fds || timeout != 0) */
2643 * g_main_context_add_poll:
2644 * @context: a #GMainContext (or %NULL for the default context)
2645 * @fd: a #GPollFD structure holding information about a file
2646 * descriptor to watch.
2647 * @priority: the priority for this file descriptor which should be
2648 * the same as the priority used for g_source_attach() to ensure that the
2649 * file descriptor is polled whenever the results may be needed.
2651 * Adds a file descriptor to the set of file descriptors polled for
2652 * this context. This will very seldomly be used directly. Instead
2653 * a typical event source will use g_source_add_poll() instead.
2656 g_main_context_add_poll (GMainContext *context,
2661 context = g_main_context_default ();
2663 g_return_if_fail (context->ref_count > 0);
2664 g_return_if_fail (fd);
2666 LOCK_CONTEXT (context);
2667 g_main_context_add_poll_unlocked (context, priority, fd);
2668 UNLOCK_CONTEXT (context);
2671 /* HOLDS: main_loop_lock */
2673 g_main_context_add_poll_unlocked (GMainContext *context,
2677 GPollRec *lastrec, *pollrec, *newrec;
2679 if (!context->poll_chunk)
2680 context->poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
2682 if (context->poll_free_list)
2684 newrec = context->poll_free_list;
2685 context->poll_free_list = newrec->next;
2688 newrec = g_chunk_new (GPollRec, context->poll_chunk);
2690 /* This file descriptor may be checked before we ever poll */
2693 newrec->priority = priority;
2696 pollrec = context->poll_records;
2697 while (pollrec && priority >= pollrec->priority)
2700 pollrec = pollrec->next;
2704 lastrec->next = newrec;
2706 context->poll_records = newrec;
2708 newrec->next = pollrec;
2710 context->n_poll_records++;
2712 #ifdef G_THREADS_ENABLED
2713 context->poll_changed = TRUE;
2715 /* Now wake up the main loop if it is waiting in the poll() */
2716 g_main_context_wakeup_unlocked (context);
2721 * g_main_context_remove_poll:
2722 * @context:a #GMainContext
2723 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
2725 * Removes file descriptor from the set of file descriptors to be
2726 * polled for a particular context.
2729 g_main_context_remove_poll (GMainContext *context,
2733 context = g_main_context_default ();
2735 g_return_if_fail (context->ref_count > 0);
2736 g_return_if_fail (fd);
2738 LOCK_CONTEXT (context);
2739 g_main_context_remove_poll_unlocked (context, fd);
2740 UNLOCK_CONTEXT (context);
2744 g_main_context_remove_poll_unlocked (GMainContext *context,
2747 GPollRec *pollrec, *lastrec;
2750 pollrec = context->poll_records;
2754 if (pollrec->fd == fd)
2756 if (lastrec != NULL)
2757 lastrec->next = pollrec->next;
2759 context->poll_records = pollrec->next;
2761 #ifdef ENABLE_GC_FRIENDLY
2763 #endif /* ENABLE_GC_FRIENDLY */
2765 pollrec->next = context->poll_free_list;
2766 context->poll_free_list = pollrec;
2768 context->n_poll_records--;
2772 pollrec = pollrec->next;
2775 #ifdef G_THREADS_ENABLED
2776 context->poll_changed = TRUE;
2778 /* Now wake up the main loop if it is waiting in the poll() */
2779 g_main_context_wakeup_unlocked (context);
2784 * g_source_get_current_time:
2785 * @source: a #GSource
2786 * @timeval: #GTimeVal structure in which to store current time.
2788 * Gets the "current time" to be used when checking
2789 * this source. The advantage of calling this function over
2790 * calling g_get_current_time() directly is that when
2791 * checking multiple sources, GLib can cache a single value
2792 * instead of having to repeatedly get the system time.
2795 g_source_get_current_time (GSource *source,
2798 GMainContext *context;
2800 g_return_if_fail (source->context != NULL);
2802 context = source->context;
2804 LOCK_CONTEXT (context);
2806 if (!context->time_is_current)
2808 g_get_current_time (&context->current_time);
2809 context->time_is_current = TRUE;
2812 *timeval = context->current_time;
2814 UNLOCK_CONTEXT (context);
2818 * g_main_context_set_poll_func:
2819 * @context: a #GMainContext
2820 * @func: the function to call to poll all file descriptors
2822 * Sets the function to use to handle polling of file descriptors. It
2823 * will be used instead of the <function>poll()</function> system call
2824 * (or GLib's replacement function, which is used where
2825 * <function>poll()</function> isn't available).
2827 * This function could possibly be used to integrate the GLib event
2828 * loop with an external event loop.
2831 g_main_context_set_poll_func (GMainContext *context,
2835 context = g_main_context_default ();
2837 g_return_if_fail (context->ref_count > 0);
2839 LOCK_CONTEXT (context);
2842 context->poll_func = func;
2846 context->poll_func = (GPollFunc) poll;
2848 context->poll_func = (GPollFunc) g_poll;
2852 UNLOCK_CONTEXT (context);
2856 * g_main_context_get_poll_func:
2857 * @context: a #GMainContext
2859 * Gets the poll function set by g_main_context_set_poll_func().
2861 * Return value: the poll function
2864 g_main_context_get_poll_func (GMainContext *context)
2869 context = g_main_context_default ();
2871 g_return_val_if_fail (context->ref_count > 0, NULL);
2873 LOCK_CONTEXT (context);
2874 result = context->poll_func;
2875 UNLOCK_CONTEXT (context);
2880 /* HOLDS: context's lock */
2881 /* Wake the main loop up from a poll() */
2883 g_main_context_wakeup_unlocked (GMainContext *context)
2885 #ifdef G_THREADS_ENABLED
2886 if (g_thread_supported() && context->poll_waiting)
2888 context->poll_waiting = FALSE;
2890 write (context->wake_up_pipe[1], "A", 1);
2892 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
2899 * g_main_context_wakeup:
2900 * @context: a #GMainContext
2902 * If @context is currently waiting in a <function>poll()</function>, interrupt
2903 * the <function>poll()</function>, and continue the iteration process.
2906 g_main_context_wakeup (GMainContext *context)
2909 context = g_main_context_default ();
2911 g_return_if_fail (context->ref_count > 0);
2913 LOCK_CONTEXT (context);
2914 g_main_context_wakeup_unlocked (context);
2915 UNLOCK_CONTEXT (context);
2921 g_timeout_set_expiration (GTimeoutSource *timeout_source,
2922 GTimeVal *current_time)
2924 guint seconds = timeout_source->interval / 1000;
2925 guint msecs = timeout_source->interval - seconds * 1000;
2927 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
2928 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
2929 if (timeout_source->expiration.tv_usec >= 1000000)
2931 timeout_source->expiration.tv_usec -= 1000000;
2932 timeout_source->expiration.tv_sec++;
2937 g_timeout_prepare (GSource *source,
2942 GTimeVal current_time;
2944 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2946 g_source_get_current_time (source, ¤t_time);
2948 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
2949 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
2951 /* We do the following in a rather convoluted fashion to deal with
2952 * the fact that we don't have an integral type big enough to hold
2953 * the difference of two timevals in millseconds.
2955 if (sec < 0 || (sec == 0 && msec < 0))
2959 glong interval_sec = timeout_source->interval / 1000;
2960 glong interval_msec = timeout_source->interval % 1000;
2968 if (sec > interval_sec ||
2969 (sec == interval_sec && msec > interval_msec))
2971 /* The system time has been set backwards, so we
2972 * reset the expiration time to now + timeout_source->interval;
2973 * this at least avoids hanging for long periods of time.
2975 g_timeout_set_expiration (timeout_source, ¤t_time);
2976 msec = MIN (G_MAXINT, timeout_source->interval);
2980 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
2984 *timeout = (gint)msec;
2990 g_timeout_check (GSource *source)
2992 GTimeVal current_time;
2993 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2995 g_source_get_current_time (source, ¤t_time);
2997 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
2998 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
2999 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3003 g_timeout_dispatch (GSource *source,
3004 GSourceFunc callback,
3007 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3011 g_warning ("Timeout source dispatched without callback\n"
3012 "You must call g_source_set_callback().");
3016 if (callback (user_data))
3018 GTimeVal current_time;
3020 g_source_get_current_time (source, ¤t_time);
3021 g_timeout_set_expiration (timeout_source, ¤t_time);
3030 * g_timeout_source_new:
3031 * @interval: the timeout interval in milliseconds.
3033 * Creates a new timeout source.
3035 * The source will not initially be associated with any #GMainContext
3036 * and must be added to one with g_source_attach() before it will be
3039 * Return value: the newly-created timeout source
3042 g_timeout_source_new (guint interval)
3044 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3045 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3046 GTimeVal current_time;
3048 timeout_source->interval = interval;
3050 g_get_current_time (¤t_time);
3051 g_timeout_set_expiration (timeout_source, ¤t_time);
3057 * g_timeout_add_full:
3058 * @priority: the priority of the idle source. Typically this will be in the
3059 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3060 * @interval: the time between calls to the function, in milliseconds
3061 * (1/1000ths of a second)
3062 * @function: function to call
3063 * @data: data to pass to @function
3064 * @notify: function to call when the idle is removed, or %NULL
3066 * Sets a function to be called at regular intervals, with the given
3067 * priority. The function is called repeatedly until it returns
3068 * %FALSE, at which point the timeout is automatically destroyed and
3069 * the function will not be called again. The @notify function is
3070 * called when the timeout is destroyed. The first call to the
3071 * function will be at the end of the first @interval.
3073 * Note that timeout functions may be delayed, due to the processing of other
3074 * event sources. Thus they should not be relied on for precise timing.
3075 * After each call to the timeout function, the time of the next
3076 * timeout is recalculated based on the current time and the given interval
3077 * (it does not try to 'catch up' time lost in delays).
3079 * Return value: the id of event source.
3082 g_timeout_add_full (gint priority,
3084 GSourceFunc function,
3086 GDestroyNotify notify)
3091 g_return_val_if_fail (function != NULL, 0);
3093 source = g_timeout_source_new (interval);
3095 if (priority != G_PRIORITY_DEFAULT)
3096 g_source_set_priority (source, priority);
3098 g_source_set_callback (source, function, data, notify);
3099 id = g_source_attach (source, NULL);
3100 g_source_unref (source);
3107 * @interval: the time between calls to the function, in milliseconds
3108 * (1/1000ths of a second)
3109 * @function: function to call
3110 * @data: data to pass to @function
3112 * Sets a function to be called at regular intervals, with the default
3113 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3114 * until it returns %FALSE, at which point the timeout is automatically
3115 * destroyed and the function will not be called again. The first call
3116 * to the function will be at the end of the first @interval.
3118 * Note that timeout functions may be delayed, due to the processing of other
3119 * event sources. Thus they should not be relied on for precise timing.
3120 * After each call to the timeout function, the time of the next
3121 * timeout is recalculated based on the current time and the given interval
3122 * (it does not try to 'catch up' time lost in delays).
3124 * Return value: the id of event source.
3127 g_timeout_add (guint32 interval,
3128 GSourceFunc function,
3131 return g_timeout_add_full (G_PRIORITY_DEFAULT,
3132 interval, function, data, NULL);
3135 /* Idle functions */
3138 g_idle_prepare (GSource *source,
3147 g_idle_check (GSource *source)
3153 g_idle_dispatch (GSource *source,
3154 GSourceFunc callback,
3159 g_warning ("Idle source dispatched without callback\n"
3160 "You must call g_source_set_callback().");
3164 return callback (user_data);
3168 * g_idle_source_new:
3170 * Creates a new idle source.
3172 * The source will not initially be associated with any #GMainContext
3173 * and must be added to one with g_source_attach() before it will be
3176 * Return value: the newly-created idle source
3179 g_idle_source_new (void)
3181 return g_source_new (&g_idle_funcs, sizeof (GSource));
3186 * @priority: the priority of the idle source. Typically this will be in the
3187 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3188 * @function: function to call
3189 * @data: data to pass to @function
3190 * @notify: function to call when the idle is removed, or %NULL
3192 * Adds a function to be called whenever there are no higher priority
3193 * events pending. If the function returns %FALSE it is automatically
3194 * removed from the list of event sources and will not be called again.
3196 * Return value: the id of the event source.
3199 g_idle_add_full (gint priority,
3200 GSourceFunc function,
3202 GDestroyNotify notify)
3207 g_return_val_if_fail (function != NULL, 0);
3209 source = g_idle_source_new ();
3211 if (priority != G_PRIORITY_DEFAULT)
3212 g_source_set_priority (source, priority);
3214 g_source_set_callback (source, function, data, notify);
3215 id = g_source_attach (source, NULL);
3216 g_source_unref (source);
3223 * @function: function to call
3224 * @data: data to pass to @function.
3226 * Adds a function to be called whenever there are no higher priority
3227 * events pending to the default main loop. The function is given the
3228 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
3229 * returns %FALSE it is automatically removed from the list of event
3230 * sources and will not be called again.
3232 * Return value: the id of the event source.
3235 g_idle_add (GSourceFunc function,
3238 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
3242 * g_idle_remove_by_data:
3243 * @data: the data for the idle source's callback.
3245 * Removes the idle function with the given data.
3247 * Return value: %TRUE if an idle source was found and removed.
3250 g_idle_remove_by_data (gpointer data)
3252 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);