gmain.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /* gmain.h - the GLib Main loop
  2. * Copyright (C) 1998-2000 Red Hat, Inc.
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __G_MAIN_H__
  20. #define __G_MAIN_H__
  21. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  22. #error "Only <glib.h> can be included directly."
  23. #endif
  24. #include <glib/gpoll.h>
  25. #include <glib/gslist.h>
  26. #include <glib/gthread.h>
  27. G_BEGIN_DECLS
  28. typedef enum /*< flags >*/
  29. {
  30. G_IO_IN GLIB_SYSDEF_POLLIN,
  31. G_IO_OUT GLIB_SYSDEF_POLLOUT,
  32. G_IO_PRI GLIB_SYSDEF_POLLPRI,
  33. G_IO_ERR GLIB_SYSDEF_POLLERR,
  34. G_IO_HUP GLIB_SYSDEF_POLLHUP,
  35. G_IO_NVAL GLIB_SYSDEF_POLLNVAL
  36. } GIOCondition;
  37. /**
  38. * GMainContextFlags:
  39. * @G_MAIN_CONTEXT_FLAGS_NONE: Default behaviour.
  40. * @G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING: Assume that polling for events will
  41. * free the thread to process other jobs. That's useful if you're using
  42. * `g_main_context_{prepare,query,check,dispatch}` to integrate GMainContext in
  43. * other event loops.
  44. *
  45. * Flags to pass to [ctor@GLib.MainContext.new_with_flags] which affect the
  46. * behaviour of a [struct@GLib.MainContext].
  47. *
  48. * Since: 2.72
  49. */
  50. GLIB_AVAILABLE_TYPE_IN_2_72
  51. typedef enum /*< flags >*/
  52. {
  53. G_MAIN_CONTEXT_FLAGS_NONE = 0,
  54. G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING = 1
  55. } GMainContextFlags;
  56. /**
  57. * GMainContext:
  58. *
  59. * The `GMainContext` struct is an opaque data
  60. * type representing a set of sources to be handled in a main loop.
  61. */
  62. typedef struct _GMainContext GMainContext;
  63. /**
  64. * GMainLoop:
  65. *
  66. * The `GMainLoop` struct is an opaque data type
  67. * representing the main event loop of a GLib or GTK application.
  68. */
  69. typedef struct _GMainLoop GMainLoop;
  70. /**
  71. * GSource:
  72. *
  73. * The `GSource` struct is an opaque data type
  74. * representing an event source.
  75. */
  76. typedef struct _GSource GSource;
  77. typedef struct _GSourcePrivate GSourcePrivate;
  78. /**
  79. * GSourceCallbackFuncs:
  80. * @ref: Called when a reference is added to the callback object
  81. * @unref: Called when a reference to the callback object is dropped
  82. * @get: Called to extract the callback function and data from the
  83. * callback object.
  84. *
  85. * The `GSourceCallbackFuncs` struct contains
  86. * functions for managing callback objects.
  87. */
  88. typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs;
  89. /**
  90. * GSourceFuncs:
  91. * @prepare: Called before all the file descriptors are polled. If the
  92. * source can determine that it is ready here (without waiting for the
  93. * results of the poll() call) it should return %TRUE. It can also return
  94. * a @timeout_ value which should be the maximum timeout (in milliseconds)
  95. * which should be passed to the poll() call. The actual timeout used will
  96. * be -1 if all sources returned -1, or it will be the minimum of all
  97. * the @timeout_ values returned which were >= 0. Since 2.36 this may
  98. * be %NULL, in which case the effect is as if the function always returns
  99. * %FALSE with a timeout of -1. If @prepare returns a
  100. * timeout and the source also has a ready time set, then the
  101. * lower of the two will be used.
  102. * @check: Called after all the file descriptors are polled. The source
  103. * should return %TRUE if it is ready to be dispatched. Note that some
  104. * time may have passed since the previous prepare function was called,
  105. * so the source should be checked again here. Since 2.36 this may
  106. * be %NULL, in which case the effect is as if the function always returns
  107. * %FALSE.
  108. * @dispatch: Called to dispatch the event source, after it has returned
  109. * %TRUE in either its @prepare or its @check function, or if a ready time
  110. * has been reached. The @dispatch function receives a callback function and
  111. * user data. The callback function may be %NULL if the source was never
  112. * connected to a callback using [method@GLib.Source.set_callback]. The
  113. * @dispatch function should call the callback function with @user_data and
  114. * whatever additional parameters are needed for this type of event source.
  115. * The return value of the @dispatch function should be
  116. * [const@GLib.SOURCE_REMOVE] if the source should be removed or
  117. * [const@GLib.SOURCE_CONTINUE] to keep it.
  118. * @finalize: Called when the source is finalized. At this point, the source
  119. * will have been destroyed, had its callback cleared, and have been removed
  120. * from its [struct@GLib.MainContext], but it will still have its final
  121. * reference count, so methods can be called on it from within this
  122. * function.
  123. *
  124. * The `GSourceFuncs` struct contains a table of
  125. * functions used to handle event sources in a generic manner.
  126. *
  127. * For idle sources, the prepare and check functions always return %TRUE
  128. * to indicate that the source is always ready to be processed. The prepare
  129. * function also returns a timeout value of 0 to ensure that the poll() call
  130. * doesn't block (since that would be time wasted which could have been spent
  131. * running the idle function).
  132. *
  133. * For timeout sources, the prepare and check functions both return %TRUE
  134. * if the timeout interval has expired. The prepare function also returns
  135. * a timeout value to ensure that the poll() call doesn't block too long
  136. * and miss the next timeout.
  137. *
  138. * For file descriptor sources, the prepare function typically returns %FALSE,
  139. * since it must wait until poll() has been called before it knows whether
  140. * any events need to be processed. It sets the returned timeout to -1 to
  141. * indicate that it doesn't mind how long the poll() call blocks. In the
  142. * check function, it tests the results of the poll() call to see if the
  143. * required condition has been met, and returns %TRUE if so.
  144. */
  145. typedef struct _GSourceFuncs GSourceFuncs;
  146. /**
  147. * GPid:
  148. *
  149. * A type which is used to hold a process identification.
  150. *
  151. * On UNIX, processes are identified by a process id (an integer),
  152. * while Windows uses process handles (which are pointers).
  153. *
  154. * GPid is used in GLib only for descendant processes spawned with
  155. * the g_spawn functions.
  156. */
  157. /* defined in glibconfig.h */
  158. /**
  159. * G_PID_FORMAT:
  160. *
  161. * A format specifier that can be used in printf()-style format strings
  162. * when printing a #GPid.
  163. *
  164. * Since: 2.50
  165. */
  166. /* defined in glibconfig.h */
  167. /**
  168. * GSourceFunc:
  169. * @user_data: data passed to the function, set when the source was
  170. * created with one of the above functions
  171. *
  172. * Specifies the type of function passed to [func@GLib.timeout_add],
  173. * [func@GLib.timeout_add_full], [func@GLib.idle_add], and
  174. * [func@GLib.idle_add_full].
  175. *
  176. * When calling [method@GLib.Source.set_callback], you may need to cast a
  177. * function of a different type to this type. Use [func@GLib.SOURCE_FUNC] to
  178. * avoid warnings about incompatible function types.
  179. *
  180. * Returns: %FALSE if the source should be removed.
  181. * [const@GLib.SOURCE_CONTINUE] and [const@GLib.SOURCE_REMOVE] are more
  182. * memorable names for the return value.
  183. */
  184. typedef gboolean (*GSourceFunc) (gpointer user_data);
  185. /**
  186. * GSourceOnceFunc:
  187. * @user_data: data passed to the function, set when the source was
  188. * created
  189. *
  190. * A source function that is only called once before being removed from the main
  191. * context automatically.
  192. *
  193. * See: [func@GLib.idle_add_once], [func@GLib.timeout_add_once]
  194. *
  195. * Since: 2.74
  196. */
  197. typedef void (* GSourceOnceFunc) (gpointer user_data);
  198. /**
  199. * G_SOURCE_FUNC:
  200. * @f: a function pointer.
  201. *
  202. * Cast a function pointer to a [callback@GLib.SourceFunc], suppressing
  203. * warnings from GCC 8 onwards with `-Wextra` or `-Wcast-function-type` enabled
  204. * about the function types being incompatible.
  205. *
  206. * For example, the correct type of callback for a source created by
  207. * [func@GLib.child_watch_source_new] is #GChildWatchFunc, which accepts more
  208. * arguments than [callback@GLib.SourceFunc]. Casting the function with
  209. * `(GSourceFunc)` to call [method@GLib.Source.set_callback] will trigger a
  210. * warning, even though it will be cast back to the correct type before it is
  211. * called by the source.
  212. *
  213. * Since: 2.58
  214. */
  215. #define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f)) GLIB_AVAILABLE_MACRO_IN_2_58
  216. /**
  217. * GChildWatchFunc:
  218. * @pid: the process id of the child process
  219. * @wait_status: Status information about the child process, encoded
  220. * in a platform-specific manner
  221. * @user_data: user data passed to [func@GLib.child_watch_add]
  222. *
  223. * Prototype of a #GChildWatchSource callback, called when a child
  224. * process has exited.
  225. *
  226. * To interpret @wait_status, see the documentation for
  227. * [func@GLib.spawn_check_wait_status]. In particular,
  228. * on Unix platforms, note that it is usually not equal
  229. * to the integer passed to `exit()` or returned from `main()`.
  230. */
  231. typedef void (*GChildWatchFunc) (GPid pid,
  232. gint wait_status,
  233. gpointer user_data);
  234. /**
  235. * GSourceDisposeFunc:
  236. * @source: #GSource that is currently being disposed
  237. *
  238. * Dispose function for @source. See [method@GLib.Source.set_dispose_function]
  239. * for details.
  240. *
  241. * Since: 2.64
  242. */
  243. GLIB_AVAILABLE_TYPE_IN_2_64
  244. typedef void (*GSourceDisposeFunc) (GSource *source);
  245. struct _GSource
  246. {
  247. /*< private >*/
  248. gpointer callback_data;
  249. GSourceCallbackFuncs *callback_funcs;
  250. const GSourceFuncs *source_funcs;
  251. guint ref_count;
  252. GMainContext *context;
  253. gint priority;
  254. guint flags;
  255. guint source_id;
  256. GSList *poll_fds;
  257. GSource *prev;
  258. GSource *next;
  259. char *name;
  260. GSourcePrivate *priv;
  261. };
  262. struct _GSourceCallbackFuncs
  263. {
  264. void (*ref) (gpointer cb_data);
  265. void (*unref) (gpointer cb_data);
  266. void (*get) (gpointer cb_data,
  267. GSource *source,
  268. GSourceFunc *func,
  269. gpointer *data);
  270. };
  271. /**
  272. * GSourceDummyMarshal:
  273. *
  274. * This is just a placeholder for #GClosureMarshal,
  275. * which cannot be used here for dependency reasons.
  276. */
  277. typedef void (*GSourceDummyMarshal) (void);
  278. /**
  279. * GSourceFuncsPrepareFunc:
  280. * @source: The #GSource
  281. * @timeout_: (out) (optional): the maximum timeout (in milliseconds) which should be passed to the poll call
  282. *
  283. * Checks the source for readiness.
  284. *
  285. * Called before all the file descriptors are polled. If the
  286. * source can determine that it is ready here (without waiting for the
  287. * results of the poll call) it should return %TRUE. It can also return
  288. * a @timeout_ value which should be the maximum timeout (in milliseconds)
  289. * which should be passed to the poll call. The actual timeout used will
  290. * be `-1` if all sources returned `-1`, or it will be the minimum of all
  291. * the @timeout_ values returned which were greater than or equal to `0`.
  292. * If the prepare function returns a timeout and the source also has a
  293. * ready time set, then the lower of the two will be used.
  294. *
  295. * Since 2.36 this may be `NULL`, in which case the effect is as if the
  296. * function always returns `FALSE` with a timeout of `-1`.
  297. *
  298. * Returns: %TRUE if the source is ready, %FALSE otherwise
  299. *
  300. * Since: 2.82
  301. */
  302. typedef gboolean (*GSourceFuncsPrepareFunc) (GSource *source,
  303. gint *timeout_);
  304. /**
  305. * GSourceFuncsCheckFunc:
  306. * @source: The #GSource
  307. *
  308. * Checks if the source is ready to be dispatched.
  309. *
  310. * Called after all the file descriptors are polled. The source
  311. * should return %TRUE if it is ready to be dispatched. Note that some
  312. * time may have passed since the previous prepare function was called,
  313. * so the source should be checked again here.
  314. *
  315. * Since 2.36 this may be `NULL`, in which case the effect is
  316. * as if the function always returns `FALSE`.
  317. *
  318. * Returns: %TRUE if ready to be dispatched, %FALSE otherwise
  319. *
  320. * Since: 2.82
  321. */
  322. typedef gboolean (*GSourceFuncsCheckFunc) (GSource *source);
  323. /**
  324. * GSourceFuncsDispatchFunc:
  325. * @source: The #GSource
  326. * @callback: (nullable): The #GSourceFunc to call
  327. * @user_data: (nullable): data to pass to @callback
  328. *
  329. * Dispatches the source callback.
  330. *
  331. * Called to dispatch the event source, after it has returned
  332. * `TRUE` in either its prepare or its check function, or if a ready time
  333. * has been reached. The dispatch function receives a callback function and
  334. * user data. The callback function may be `NULL` if the source was never
  335. * connected to a callback using [method@GLib.Source.set_callback]. The dispatch
  336. * function should call the callback function with @user_data and whatever
  337. * additional parameters are needed for this type of event source. The
  338. * return value of the dispatch function should be [const@GLib.SOURCE_REMOVE]
  339. * if the source should be removed or [const@GLib.SOURCE_CONTINUE] to keep it.
  340. *
  341. * Returns: [const@GLib.SOURCE_REMOVE] if the source should be removed,
  342. * [const@GLib.SOURCE_CONTINUE] otherwise.
  343. *
  344. * Since: 2.82
  345. */
  346. typedef gboolean (*GSourceFuncsDispatchFunc) (GSource *source,
  347. GSourceFunc callback,
  348. gpointer user_data);
  349. /**
  350. * GSourceFuncsFinalizeFunc:
  351. * @source: The #GSource
  352. *
  353. * Finalizes the source.
  354. *
  355. * Called when the source is finalized. At this point, the source
  356. * will have been destroyed, had its callback cleared, and have been removed
  357. * from its [type@GLib.MainContext], but it will still have its final reference
  358. * count, so methods can be called on it from within this function.
  359. *
  360. * Since: 2.82
  361. */
  362. typedef void (*GSourceFuncsFinalizeFunc) (GSource *source);
  363. struct _GSourceFuncs
  364. {
  365. GSourceFuncsPrepareFunc prepare; /* Can be NULL */
  366. GSourceFuncsCheckFunc check; /* Can be NULL */
  367. GSourceFuncsDispatchFunc dispatch;
  368. GSourceFuncsFinalizeFunc finalize; /* Can be NULL */
  369. /*< private >*/
  370. /* For use by g_source_set_closure */
  371. GSourceFunc closure_callback;
  372. GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
  373. };
  374. /* Standard priorities */
  375. /**
  376. * G_PRIORITY_HIGH:
  377. *
  378. * Use this for high priority event sources.
  379. *
  380. * It is not used within GLib or GTK.
  381. */
  382. #define G_PRIORITY_HIGH -100
  383. /**
  384. * G_PRIORITY_DEFAULT:
  385. *
  386. * Use this for default priority event sources.
  387. *
  388. * In GLib this priority is used when adding timeout functions
  389. * with [func@GLib.timeout_add]. In GDK this priority is used for events
  390. * from the X server.
  391. */
  392. #define G_PRIORITY_DEFAULT 0
  393. /**
  394. * G_PRIORITY_HIGH_IDLE:
  395. *
  396. * Use this for high priority idle functions.
  397. *
  398. * GTK uses %G_PRIORITY_HIGH_IDLE + 10 for resizing operations,
  399. * and %G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is
  400. * done to ensure that any pending resizes are processed before any
  401. * pending redraws, so that widgets are not redrawn twice unnecessarily.)
  402. */
  403. #define G_PRIORITY_HIGH_IDLE 100
  404. /**
  405. * G_PRIORITY_DEFAULT_IDLE:
  406. *
  407. * Use this for default priority idle functions.
  408. *
  409. * In GLib this priority is used when adding idle functions with
  410. * [func@GLib.idle_add].
  411. */
  412. #define G_PRIORITY_DEFAULT_IDLE 200
  413. /**
  414. * G_PRIORITY_LOW:
  415. *
  416. * Use this for very low priority background tasks.
  417. *
  418. * It is not used within GLib or GTK.
  419. */
  420. #define G_PRIORITY_LOW 300
  421. /**
  422. * G_SOURCE_REMOVE:
  423. *
  424. * Use this macro as the return value of a [callback@GLib.SourceFunc] to remove
  425. * the [struct@GLib.Source] from the main loop.
  426. *
  427. * Since: 2.32
  428. */
  429. #define G_SOURCE_REMOVE FALSE
  430. /**
  431. * G_SOURCE_CONTINUE:
  432. *
  433. * Use this macro as the return value of a [callback@GLib.SourceFunc] to leave
  434. * the [struct@GLib.Source] in the main loop.
  435. *
  436. * Since: 2.32
  437. */
  438. #define G_SOURCE_CONTINUE TRUE
  439. /* GMainContext: */
  440. GLIB_AVAILABLE_IN_ALL
  441. GMainContext *g_main_context_new (void);
  442. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  443. GLIB_AVAILABLE_IN_2_72
  444. GMainContext *g_main_context_new_with_flags (GMainContextFlags flags);
  445. G_GNUC_END_IGNORE_DEPRECATIONS
  446. GLIB_AVAILABLE_IN_ALL
  447. GMainContext *g_main_context_ref (GMainContext *context);
  448. GLIB_AVAILABLE_IN_ALL
  449. void g_main_context_unref (GMainContext *context);
  450. GLIB_AVAILABLE_IN_ALL
  451. GMainContext *g_main_context_default (void);
  452. GLIB_AVAILABLE_IN_ALL
  453. gboolean g_main_context_iteration (GMainContext *context,
  454. gboolean may_block);
  455. GLIB_AVAILABLE_IN_ALL
  456. gboolean g_main_context_pending (GMainContext *context);
  457. /* For implementation of legacy interfaces
  458. */
  459. GLIB_AVAILABLE_IN_ALL
  460. GSource *g_main_context_find_source_by_id (GMainContext *context,
  461. guint source_id);
  462. GLIB_AVAILABLE_IN_ALL
  463. GSource *g_main_context_find_source_by_user_data (GMainContext *context,
  464. gpointer user_data);
  465. GLIB_AVAILABLE_IN_ALL
  466. GSource *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
  467. GSourceFuncs *funcs,
  468. gpointer user_data);
  469. /* Low level functions for implementing custom main loops.
  470. */
  471. GLIB_AVAILABLE_IN_ALL
  472. void g_main_context_wakeup (GMainContext *context);
  473. GLIB_AVAILABLE_IN_ALL
  474. gboolean g_main_context_acquire (GMainContext *context);
  475. GLIB_AVAILABLE_IN_ALL
  476. void g_main_context_release (GMainContext *context);
  477. GLIB_AVAILABLE_IN_ALL
  478. gboolean g_main_context_is_owner (GMainContext *context);
  479. GLIB_DEPRECATED_IN_2_58_FOR(g_main_context_is_owner)
  480. gboolean g_main_context_wait (GMainContext *context,
  481. GCond *cond,
  482. GMutex *mutex);
  483. GLIB_AVAILABLE_IN_ALL
  484. gboolean g_main_context_prepare (GMainContext *context,
  485. gint *priority);
  486. GLIB_AVAILABLE_IN_ALL
  487. gint g_main_context_query (GMainContext *context,
  488. gint max_priority,
  489. gint *timeout_,
  490. GPollFD *fds,
  491. gint n_fds);
  492. GLIB_AVAILABLE_IN_ALL
  493. gboolean g_main_context_check (GMainContext *context,
  494. gint max_priority,
  495. GPollFD *fds,
  496. gint n_fds);
  497. GLIB_AVAILABLE_IN_ALL
  498. void g_main_context_dispatch (GMainContext *context);
  499. GLIB_AVAILABLE_IN_ALL
  500. void g_main_context_set_poll_func (GMainContext *context,
  501. GPollFunc func);
  502. GLIB_AVAILABLE_IN_ALL
  503. GPollFunc g_main_context_get_poll_func (GMainContext *context);
  504. /* Low level functions for use by source implementations
  505. */
  506. GLIB_AVAILABLE_IN_ALL
  507. void g_main_context_add_poll (GMainContext *context,
  508. GPollFD *fd,
  509. gint priority);
  510. GLIB_AVAILABLE_IN_ALL
  511. void g_main_context_remove_poll (GMainContext *context,
  512. GPollFD *fd);
  513. GLIB_AVAILABLE_IN_ALL
  514. gint g_main_depth (void);
  515. GLIB_AVAILABLE_IN_ALL
  516. GSource *g_main_current_source (void);
  517. /* GMainContexts for other threads
  518. */
  519. GLIB_AVAILABLE_IN_ALL
  520. void g_main_context_push_thread_default (GMainContext *context);
  521. GLIB_AVAILABLE_IN_ALL
  522. void g_main_context_pop_thread_default (GMainContext *context);
  523. GLIB_AVAILABLE_IN_ALL
  524. GMainContext *g_main_context_get_thread_default (void);
  525. GLIB_AVAILABLE_IN_ALL
  526. GMainContext *g_main_context_ref_thread_default (void);
  527. /**
  528. * GMainContextPusher:
  529. *
  530. * Opaque type. See g_main_context_pusher_new() for details.
  531. *
  532. * Since: 2.64
  533. */
  534. typedef void GMainContextPusher GLIB_AVAILABLE_TYPE_IN_2_64;
  535. /**
  536. * g_main_context_pusher_new:
  537. * @main_context: (transfer none): a main context to push
  538. *
  539. * Push @main_context as the new thread-default main context for the current
  540. * thread, using [method@GLib.MainContext.push_thread_default], and return a
  541. * new [alias@GLib.MainContextPusher]. Pop with g_main_context_pusher_free().
  542. * Using [method@GLib.MainContext.pop_thread_default] on @main_context while a
  543. * [alias@GLib.MainContextPusher] exists for it can lead to undefined behaviour.
  544. *
  545. * Using two [alias@GLib.MainContextPusher]s in the same scope is not allowed,
  546. * as it leads to an undefined pop order.
  547. *
  548. * This is intended to be used with g_autoptr(). Note that g_autoptr()
  549. * is only available when using GCC or clang, so the following example
  550. * will only work with those compilers:
  551. * |[
  552. * typedef struct
  553. * {
  554. * ...
  555. * GMainContext *context;
  556. * ...
  557. * } MyObject;
  558. *
  559. * static void
  560. * my_object_do_stuff (MyObject *self)
  561. * {
  562. * g_autoptr(GMainContextPusher) pusher = g_main_context_pusher_new (self->context);
  563. *
  564. * // Code with main context as the thread default here
  565. *
  566. * if (cond)
  567. * // No need to pop
  568. * return;
  569. *
  570. * // Optionally early pop
  571. * g_clear_pointer (&pusher, g_main_context_pusher_free);
  572. *
  573. * // Code with main context no longer the thread default here
  574. * }
  575. * ]|
  576. *
  577. * Returns: (transfer full): a #GMainContextPusher
  578. * Since: 2.64
  579. */
  580. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  581. GLIB_AVAILABLE_STATIC_INLINE_IN_2_64
  582. static inline GMainContextPusher *
  583. g_main_context_pusher_new (GMainContext *main_context)
  584. {
  585. g_main_context_push_thread_default (main_context);
  586. return (GMainContextPusher *) main_context;
  587. }
  588. G_GNUC_END_IGNORE_DEPRECATIONS
  589. /**
  590. * g_main_context_pusher_free:
  591. * @pusher: (transfer full): a #GMainContextPusher
  592. *
  593. * Pop @pusher’s main context as the thread default main context.
  594. * See g_main_context_pusher_new() for details.
  595. *
  596. * This will pop the [struct@GLib.MainContext] as the current thread-default
  597. * main context, but will not call [method@GLib.MainContext.unref] on it.
  598. *
  599. * Since: 2.64
  600. */
  601. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  602. GLIB_AVAILABLE_STATIC_INLINE_IN_2_64
  603. static inline void
  604. g_main_context_pusher_free (GMainContextPusher *pusher)
  605. {
  606. g_main_context_pop_thread_default ((GMainContext *) pusher);
  607. }
  608. G_GNUC_END_IGNORE_DEPRECATIONS
  609. /* GMainLoop: */
  610. GLIB_AVAILABLE_IN_ALL
  611. GMainLoop *g_main_loop_new (GMainContext *context,
  612. gboolean is_running);
  613. GLIB_AVAILABLE_IN_ALL
  614. void g_main_loop_run (GMainLoop *loop);
  615. GLIB_AVAILABLE_IN_ALL
  616. void g_main_loop_quit (GMainLoop *loop);
  617. GLIB_AVAILABLE_IN_ALL
  618. GMainLoop *g_main_loop_ref (GMainLoop *loop);
  619. GLIB_AVAILABLE_IN_ALL
  620. void g_main_loop_unref (GMainLoop *loop);
  621. GLIB_AVAILABLE_IN_ALL
  622. gboolean g_main_loop_is_running (GMainLoop *loop);
  623. GLIB_AVAILABLE_IN_ALL
  624. GMainContext *g_main_loop_get_context (GMainLoop *loop);
  625. /* GSource: */
  626. GLIB_AVAILABLE_IN_ALL
  627. GSource *g_source_new (GSourceFuncs *source_funcs,
  628. guint struct_size);
  629. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  630. GLIB_AVAILABLE_IN_2_64
  631. void g_source_set_dispose_function (GSource *source,
  632. GSourceDisposeFunc dispose);
  633. G_GNUC_END_IGNORE_DEPRECATIONS
  634. GLIB_AVAILABLE_IN_ALL
  635. GSource *g_source_ref (GSource *source);
  636. GLIB_AVAILABLE_IN_ALL
  637. void g_source_unref (GSource *source);
  638. GLIB_AVAILABLE_IN_ALL
  639. guint g_source_attach (GSource *source,
  640. GMainContext *context);
  641. GLIB_AVAILABLE_IN_ALL
  642. void g_source_destroy (GSource *source);
  643. GLIB_AVAILABLE_IN_ALL
  644. void g_source_set_priority (GSource *source,
  645. gint priority);
  646. GLIB_AVAILABLE_IN_ALL
  647. gint g_source_get_priority (GSource *source);
  648. GLIB_AVAILABLE_IN_ALL
  649. void g_source_set_can_recurse (GSource *source,
  650. gboolean can_recurse);
  651. GLIB_AVAILABLE_IN_ALL
  652. gboolean g_source_get_can_recurse (GSource *source);
  653. GLIB_AVAILABLE_IN_ALL
  654. guint g_source_get_id (GSource *source);
  655. GLIB_AVAILABLE_IN_ALL
  656. GMainContext *g_source_get_context (GSource *source);
  657. GLIB_AVAILABLE_IN_ALL
  658. void g_source_set_callback (GSource *source,
  659. GSourceFunc func,
  660. gpointer data,
  661. GDestroyNotify notify);
  662. GLIB_AVAILABLE_IN_ALL
  663. void g_source_set_funcs (GSource *source,
  664. GSourceFuncs *funcs);
  665. GLIB_AVAILABLE_IN_ALL
  666. gboolean g_source_is_destroyed (GSource *source);
  667. GLIB_AVAILABLE_IN_ALL
  668. void g_source_set_name (GSource *source,
  669. const char *name);
  670. GLIB_AVAILABLE_IN_2_70
  671. void g_source_set_static_name (GSource *source,
  672. const char *name);
  673. GLIB_AVAILABLE_IN_ALL
  674. const char * g_source_get_name (GSource *source);
  675. GLIB_AVAILABLE_IN_ALL
  676. void g_source_set_name_by_id (guint tag,
  677. const char *name);
  678. GLIB_AVAILABLE_IN_2_36
  679. void g_source_set_ready_time (GSource *source,
  680. gint64 ready_time);
  681. GLIB_AVAILABLE_IN_2_36
  682. gint64 g_source_get_ready_time (GSource *source);
  683. #ifdef G_OS_UNIX
  684. GLIB_AVAILABLE_IN_2_36
  685. gpointer g_source_add_unix_fd (GSource *source,
  686. gint fd,
  687. GIOCondition events);
  688. GLIB_AVAILABLE_IN_2_36
  689. void g_source_modify_unix_fd (GSource *source,
  690. gpointer tag,
  691. GIOCondition new_events);
  692. GLIB_AVAILABLE_IN_2_36
  693. void g_source_remove_unix_fd (GSource *source,
  694. gpointer tag);
  695. GLIB_AVAILABLE_IN_2_36
  696. GIOCondition g_source_query_unix_fd (GSource *source,
  697. gpointer tag);
  698. #endif
  699. /* Used to implement g_source_connect_closure and internally*/
  700. GLIB_AVAILABLE_IN_ALL
  701. void g_source_set_callback_indirect (GSource *source,
  702. gpointer callback_data,
  703. GSourceCallbackFuncs *callback_funcs);
  704. GLIB_AVAILABLE_IN_ALL
  705. void g_source_add_poll (GSource *source,
  706. GPollFD *fd);
  707. GLIB_AVAILABLE_IN_ALL
  708. void g_source_remove_poll (GSource *source,
  709. GPollFD *fd);
  710. GLIB_AVAILABLE_IN_ALL
  711. void g_source_add_child_source (GSource *source,
  712. GSource *child_source);
  713. GLIB_AVAILABLE_IN_ALL
  714. void g_source_remove_child_source (GSource *source,
  715. GSource *child_source);
  716. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  717. GLIB_DEPRECATED_IN_2_28_FOR(g_source_get_time)
  718. void g_source_get_current_time (GSource *source,
  719. GTimeVal *timeval);
  720. G_GNUC_END_IGNORE_DEPRECATIONS
  721. GLIB_AVAILABLE_IN_ALL
  722. gint64 g_source_get_time (GSource *source);
  723. /* void g_source_connect_closure (GSource *source,
  724. GClosure *closure);
  725. */
  726. /* Specific source types
  727. */
  728. GLIB_AVAILABLE_IN_ALL
  729. GSource *g_idle_source_new (void);
  730. GLIB_AVAILABLE_IN_ALL
  731. GSource *g_child_watch_source_new (GPid pid);
  732. GLIB_AVAILABLE_IN_ALL
  733. GSource *g_timeout_source_new (guint interval);
  734. GLIB_AVAILABLE_IN_ALL
  735. GSource *g_timeout_source_new_seconds (guint interval);
  736. /* Miscellaneous functions
  737. */
  738. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  739. GLIB_DEPRECATED_IN_2_62_FOR(g_get_real_time)
  740. void g_get_current_time (GTimeVal *result);
  741. G_GNUC_END_IGNORE_DEPRECATIONS
  742. GLIB_AVAILABLE_IN_ALL
  743. gint64 g_get_monotonic_time (void);
  744. GLIB_AVAILABLE_IN_ALL
  745. gint64 g_get_real_time (void);
  746. /* Source manipulation by ID */
  747. GLIB_AVAILABLE_IN_ALL
  748. gboolean g_source_remove (guint tag);
  749. GLIB_AVAILABLE_IN_ALL
  750. gboolean g_source_remove_by_user_data (gpointer user_data);
  751. GLIB_AVAILABLE_IN_ALL
  752. gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
  753. gpointer user_data);
  754. /**
  755. * GClearHandleFunc:
  756. * @handle_id: the handle ID to clear
  757. *
  758. * Specifies the type of function passed to [func@GLib.clear_handle_id] The
  759. * implementation is expected to free the resource identified by @handle_id;
  760. * for instance, if @handle_id is a [struct@GLib.Source] ID,
  761. * [func@GLib.Source.remove] can be used.
  762. *
  763. * Since: 2.56
  764. */
  765. typedef void (* GClearHandleFunc) (guint handle_id);
  766. GLIB_AVAILABLE_IN_2_56
  767. void g_clear_handle_id (guint *tag_ptr,
  768. GClearHandleFunc clear_func);
  769. #define g_clear_handle_id(tag_ptr, clear_func) \
  770. G_STMT_START { \
  771. G_STATIC_ASSERT (sizeof *(tag_ptr) == sizeof (guint)); \
  772. guint *_tag_ptr = (guint *) (tag_ptr); \
  773. guint _handle_id; \
  774. \
  775. _handle_id = *_tag_ptr; \
  776. if (_handle_id > 0) \
  777. { \
  778. *_tag_ptr = 0; \
  779. clear_func (_handle_id); \
  780. } \
  781. } G_STMT_END \
  782. GLIB_AVAILABLE_MACRO_IN_2_56
  783. /* Idles, child watchers and timeouts */
  784. GLIB_AVAILABLE_IN_ALL
  785. guint g_timeout_add_full (gint priority,
  786. guint interval,
  787. GSourceFunc function,
  788. gpointer data,
  789. GDestroyNotify notify);
  790. GLIB_AVAILABLE_IN_ALL
  791. guint g_timeout_add (guint interval,
  792. GSourceFunc function,
  793. gpointer data);
  794. GLIB_AVAILABLE_IN_2_74
  795. guint g_timeout_add_once (guint interval,
  796. GSourceOnceFunc function,
  797. gpointer data);
  798. GLIB_AVAILABLE_IN_ALL
  799. guint g_timeout_add_seconds_full (gint priority,
  800. guint interval,
  801. GSourceFunc function,
  802. gpointer data,
  803. GDestroyNotify notify);
  804. GLIB_AVAILABLE_IN_ALL
  805. guint g_timeout_add_seconds (guint interval,
  806. GSourceFunc function,
  807. gpointer data);
  808. GLIB_AVAILABLE_IN_2_78
  809. guint g_timeout_add_seconds_once (guint interval,
  810. GSourceOnceFunc function,
  811. gpointer data);
  812. GLIB_AVAILABLE_IN_ALL
  813. guint g_child_watch_add_full (gint priority,
  814. GPid pid,
  815. GChildWatchFunc function,
  816. gpointer data,
  817. GDestroyNotify notify);
  818. GLIB_AVAILABLE_IN_ALL
  819. guint g_child_watch_add (GPid pid,
  820. GChildWatchFunc function,
  821. gpointer data);
  822. GLIB_AVAILABLE_IN_ALL
  823. guint g_idle_add (GSourceFunc function,
  824. gpointer data);
  825. GLIB_AVAILABLE_IN_ALL
  826. guint g_idle_add_full (gint priority,
  827. GSourceFunc function,
  828. gpointer data,
  829. GDestroyNotify notify);
  830. GLIB_AVAILABLE_IN_2_74
  831. guint g_idle_add_once (GSourceOnceFunc function,
  832. gpointer data);
  833. GLIB_AVAILABLE_IN_ALL
  834. gboolean g_idle_remove_by_data (gpointer data);
  835. GLIB_AVAILABLE_IN_ALL
  836. void g_main_context_invoke_full (GMainContext *context,
  837. gint priority,
  838. GSourceFunc function,
  839. gpointer data,
  840. GDestroyNotify notify);
  841. GLIB_AVAILABLE_IN_ALL
  842. void g_main_context_invoke (GMainContext *context,
  843. GSourceFunc function,
  844. gpointer data);
  845. GLIB_AVAILABLE_STATIC_INLINE_IN_2_70
  846. static inline int
  847. g_steal_fd (int *fd_ptr)
  848. {
  849. int fd = *fd_ptr;
  850. *fd_ptr = -1;
  851. return fd;
  852. }
  853. /* Hook for GClosure / GSource integration. Don't touch */
  854. GLIB_VAR GSourceFuncs g_timeout_funcs;
  855. GLIB_VAR GSourceFuncs g_child_watch_funcs;
  856. GLIB_VAR GSourceFuncs g_idle_funcs;
  857. #ifdef G_OS_UNIX
  858. GLIB_VAR GSourceFuncs g_unix_signal_funcs;
  859. GLIB_VAR GSourceFuncs g_unix_fd_source_funcs;
  860. #endif
  861. G_END_DECLS
  862. #endif /* __G_MAIN_H__ */