PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/src/wrappers/glib/library/core/g_main_context.e

http://github.com/tybor/Liberty
Specman e | 974 lines | 18 code | 286 blank | 670 comment | 0 complexity | 6ad324eb172dc8bd21a714872c5d179e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. indexing
  2. copyright: "(C) 2005 Paolo Redaelli "
  3. license: "LGPL v2 or later"
  4. date: "$Date:$"
  5. revision: "$REvision:$"
  6. class G_MAIN_CONTEXT
  7. -- Prev Up Home GLib Reference Manual Next
  8. -- Top | Description
  9. -- The Main Event Loop
  10. -- The Main Event Loop %G —%@ manages all available sources of events.
  11. -- Synopsis
  12. -- #include <glib.h>
  13. -- GMainLoop;
  14. -- GMainLoop* g_main_loop_new (GMainContext *context,
  15. -- gboolean is_running);
  16. -- GMainLoop* g_main_loop_ref (GMainLoop *loop);
  17. -- void g_main_loop_unref (GMainLoop *loop);
  18. -- void g_main_loop_run (GMainLoop *loop);
  19. -- void g_main_loop_quit (GMainLoop *loop);
  20. -- gboolean g_main_loop_is_running (GMainLoop *loop);
  21. -- GMainContext* g_main_loop_get_context (GMainLoop *loop);
  22. -- #define g_main_new (is_running)
  23. -- #define g_main_destroy (loop)
  24. -- #define g_main_run (loop)
  25. -- #define g_main_quit (loop)
  26. -- #define g_main_is_running (loop)
  27. -- #define G_PRIORITY_HIGH
  28. -- #define G_PRIORITY_DEFAULT
  29. -- #define G_PRIORITY_HIGH_IDLE
  30. -- #define G_PRIORITY_DEFAULT_IDLE
  31. -- #define G_PRIORITY_LOW
  32. -- GMainContext;
  33. -- GMainContext* g_main_context_new (void);
  34. -- GMainContext* g_main_context_ref (GMainContext *context);
  35. -- void g_main_context_unref (GMainContext *context);
  36. -- GMainContext* g_main_context_default (void);
  37. -- gboolean g_main_context_iteration (GMainContext *context,
  38. -- gboolean may_block);
  39. -- #define g_main_iteration (may_block)
  40. -- gboolean g_main_context_pending (GMainContext *context);
  41. -- #define g_main_pending ()
  42. -- GSource* g_main_context_find_source_by_id
  43. -- (GMainContext *context,
  44. -- guint source_id);
  45. -- GSource* g_main_context_find_source_by_user_data
  46. -- (GMainContext *context,
  47. -- gpointer user_data);
  48. -- GSource* g_main_context_find_source_by_funcs_user_data
  49. -- (GMainContext *context,
  50. -- GSourceFuncs *funcs,
  51. -- gpointer user_data);
  52. -- void g_main_context_wakeup (GMainContext *context);
  53. -- gboolean g_main_context_acquire (GMainContext *context);
  54. -- void g_main_context_release (GMainContext *context);
  55. -- gboolean g_main_context_wait (GMainContext *context,
  56. -- GCond *cond,
  57. -- GMutex *mutex);
  58. -- gboolean g_main_context_prepare (GMainContext *context,
  59. -- gint *priority);
  60. -- gint g_main_context_query (GMainContext *context,
  61. -- gint max_priority,
  62. -- gint *timeout_,
  63. -- GPollFD *fds,
  64. -- gint n_fds);
  65. -- gint g_main_context_check (GMainContext *context,
  66. -- gint max_priority,
  67. -- GPollFD *fds,
  68. -- gint n_fds);
  69. -- void g_main_context_dispatch (GMainContext *context);
  70. -- void g_main_context_set_poll_func (GMainContext *context,
  71. -- GPollFunc func);
  72. -- GPollFunc g_main_context_get_poll_func (GMainContext *context);
  73. -- gint (*GPollFunc) (GPollFD *ufds,
  74. -- guint nfsd,
  75. -- gint timeout_);
  76. -- void g_main_context_add_poll (GMainContext *context,
  77. -- GPollFD *fd,
  78. -- gint priority);
  79. -- void g_main_context_remove_poll (GMainContext *context,
  80. -- GPollFD *fd);
  81. -- int g_main_depth (void);
  82. -- #define g_main_set_poll_func (func)
  83. -- GSource* g_idle_source_new (void);
  84. -- guint g_idle_add (GSourceFunc function,
  85. -- gpointer data);
  86. -- guint g_idle_add_full (gint priority,
  87. -- GSourceFunc function,
  88. -- gpointer data,
  89. -- GDestroyNotify notify);
  90. -- gboolean g_idle_remove_by_data (gpointer data);
  91. -- typedef GPid;
  92. -- void (*GChildWatchFunc) (GPid pid,
  93. -- gint status,
  94. -- gpointer data);
  95. -- GSource* g_child_watch_source_new (GPid pid);
  96. -- guint g_child_watch_add (GPid pid,
  97. -- GChildWatchFunc function,
  98. -- gpointer data);
  99. -- guint g_child_watch_add_full (gint priority,
  100. -- GPid pid,
  101. -- GChildWatchFunc function,
  102. -- gpointer data,
  103. -- GDestroyNotify notify);
  104. -- GPollFD;
  105. -- GSource;
  106. -- void (*GSourceDummyMarshal) (void);
  107. -- GSourceFuncs;
  108. -- GSourceCallbackFuncs;
  109. -- GSource* g_source_new (GSourceFuncs *source_funcs,
  110. -- guint struct_size);
  111. -- GSource* g_source_ref (GSource *source);
  112. -- void g_source_unref (GSource *source);
  113. -- guint g_source_attach (GSource *source,
  114. -- GMainContext *context);
  115. -- void g_source_destroy (GSource *source);
  116. -- void g_source_set_priority (GSource *source,
  117. -- gint priority);
  118. -- gint g_source_get_priority (GSource *source);
  119. -- void g_source_set_can_recurse (GSource *source,
  120. -- gboolean can_recurse);
  121. -- gboolean g_source_get_can_recurse (GSource *source);
  122. -- guint g_source_get_id (GSource *source);
  123. -- GMainContext* g_source_get_context (GSource *source);
  124. -- void g_source_set_callback (GSource *source,
  125. -- GSourceFunc func,
  126. -- gpointer data,
  127. -- GDestroyNotify notify);
  128. -- gboolean (*GSourceFunc) (gpointer data);
  129. -- void g_source_set_callback_indirect (GSource *source,
  130. -- gpointer callback_data,
  131. -- GSourceCallbackFuncs *callback_funcs);
  132. -- void g_source_add_poll (GSource *source,
  133. -- GPollFD *fd);
  134. -- void g_source_remove_poll (GSource *source,
  135. -- GPollFD *fd);
  136. -- void g_source_get_current_time (GSource *source,
  137. -- GTimeVal *timeval);
  138. -- gboolean g_source_remove (guint tag);
  139. -- gboolean g_source_remove_by_funcs_user_data
  140. -- (GSourceFuncs *funcs,
  141. -- gpointer user_data);
  142. -- gboolean g_source_remove_by_user_data (gpointer user_data);
  143. -- Description
  144. -- The main event loop manages all the available sources of events for GLib and GTK+ applications. These events can come from any number of different types of sources such as file descriptors (plain files, pipes or sockets) and timeouts. New types of event sources can also be added using g_source_attach().
  145. -- To allow multiple independent sets of sources to be handled in different threads, each source is associated with a GMainContext. A GMainContext can only be running in a single thread, but sources can be added to it and removed from it from other threads.
  146. -- Each event source is assigned a priority. The default priority, G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. Values greater than 0 denote lower priorities. Events from high priority sources are always processed before events from lower priority sources.
  147. -- Idle functions can also be added, and assigned a priority. These will be run whenever no events with a higher priority are ready to be processed.
  148. -- The GMainLoop data type represents a main event loop. A GMainLoop is created with g_main_loop_new(). After adding the initial event sources, g_main_loop_run() is called. This continuously checks for new events from each of the event sources and dispatches them. Finally, the processing of an event from one of the sources leads to a call to g_main_loop_quit() to exit the main loop, and g_main_loop_run() returns.
  149. -- It is possible to create new instances of GMainLoop recursively. This is often used in GTK+ applications when showing modal dialog boxes. Note that event sources are associated with a particular GMainContext, and will be checked and dispatched for all main loops associated with that GMainContext.
  150. -- GTK+ contains wrappers of some of these functions, e.g. gtk_main(), gtk_main_quit() and gtk_events_pending().
  151. -- Creating new sources types
  152. -- One of the unusual features of the GTK+ main loop functionality is that new types of event source can be created and used in addition to the builtin type of event source. A new event source type is used for handling GDK events. A new source type is created by deriving from the GSource structure. The derived type of source is represented by a structure that has the GSource structure as a first element, and other elements specific to the new source type. To create an instance of the new source type, call g_source_new() passing in the size of the derived structure and a table of functions. These GSourceFuncs determine the behavior of the new source types.
  153. -- New source types basically interact with with the main context in two ways. Their prepare function in GSourceFuncs can set a timeout to determine the maximum amount of time that the main loop will sleep before checking the source again. In addition, or as well, the source can add file descriptors to the set that the main context checks using g_source_add_poll().
  154. -- Customizing the main loop iteration
  155. -- Single iterations of a GMainContext can be run with g_main_context_iteration(). In some cases, more detailed control of exactly how the details of the main loop work is desired, for instance, when integrating the GMainLoop with an external main loop. In such cases, you can call the component functions of g_main_context_iteration() directly. These functions are g_main_context_prepare(), g_main_context_query(), g_main_context_check() and g_main_context_dispatch().
  156. -- The operation of these functions can best be seen in terms of a state diagram, as shown in Figure 1, &#x2019;??States of a Main Context&#x2019;?É.
  157. -- Figure 1. States of a Main Context
  158. -- States of a Main Context
  159. -- Details
  160. -- GMainLoop
  161. -- typedef struct _GMainLoop GMainLoop;
  162. -- The GMainLoop struct is an opaque data type representing the main event loop of a GLib or GTK+ application.
  163. -- g_main_loop_new ()
  164. -- GMainLoop* g_main_loop_new (GMainContext *context,
  165. -- gboolean is_running);
  166. -- Creates a new GMainLoop structure.
  167. -- context : a GMainContext (if NULL, the default context will be used).
  168. -- is_running : set to TRUE to indicate that the loop is running. This is not very important since calling g_main_loop_run() will set this to TRUE anyway.
  169. -- Returns : a new GMainLoop.
  170. -- g_main_loop_ref ()
  171. -- GMainLoop* g_main_loop_ref (GMainLoop *loop);
  172. -- Increases the reference count on a GMainLoop object by one.
  173. -- loop : a GMainLoop
  174. -- Returns : loop
  175. -- g_main_loop_unref ()
  176. -- void g_main_loop_unref (GMainLoop *loop);
  177. -- Decreases the reference count on a GMainLoop object by one. If the result is zero, free the loop and free all associated memory.
  178. -- loop : a GMainLoop
  179. -- g_main_loop_run ()
  180. -- void g_main_loop_run (GMainLoop *loop);
  181. -- Runs a main loop until g_main_loop_quit() is called on the loop. If this is called for the thread of the loop's GMainContext, it will process events from the loop, otherwise it will simply wait.
  182. -- loop : a GMainLoop
  183. -- g_main_loop_quit ()
  184. -- void g_main_loop_quit (GMainLoop *loop);
  185. -- Stops a GMainLoop from running. Any calls to g_main_loop_run() for the loop will return.
  186. -- loop : a GMainLoop
  187. -- g_main_loop_is_running ()
  188. -- gboolean g_main_loop_is_running (GMainLoop *loop);
  189. -- Checks to see if the main loop is currently being run via g_main_loop_run().
  190. -- loop : a GMainLoop.
  191. -- Returns : TRUE if the mainloop is currently being run.
  192. -- g_main_loop_get_context ()
  193. -- GMainContext* g_main_loop_get_context (GMainLoop *loop);
  194. -- Returns the GMainContext of loop.
  195. -- loop : a GMainLoop.
  196. -- Returns : the GMainContext of loop
  197. -- g_main_new()
  198. -- #define g_main_new(is_running)
  199. -- Warning
  200. -- g_main_new is deprecated and should not be used in newly-written code. Use g_main_loop_new() instead.
  201. -- Creates a new GMainLoop for the default main loop.
  202. -- is_running : set to TRUE to indicate that the loop is running. This is not very important since calling g_main_run() will set this to TRUE anyway.
  203. -- Returns : a new GMainLoop.
  204. -- g_main_destroy()
  205. -- #define g_main_destroy(loop)
  206. -- Warning
  207. -- g_main_destroy is deprecated and should not be used in newly-written code. Use g_main_loop_unref() instead.
  208. -- Frees the memory allocated for the GMainLoop.
  209. -- loop : a GMainLoop.
  210. -- g_main_run()
  211. -- #define g_main_run(loop)
  212. -- Warning
  213. -- g_main_run is deprecated and should not be used in newly-written code. Use g_main_loop_run() instead.
  214. -- Runs a main loop until it stops running.
  215. -- loop : a GMainLoop.
  216. -- g_main_quit()
  217. -- #define g_main_quit(loop)
  218. -- Warning
  219. -- g_main_quit is deprecated and should not be used in newly-written code. Use g_main_loop_quit() instead.
  220. -- Stops the GMainLoop. If g_main_run() was called to run the GMainLoop, it will now return.
  221. -- loop : a GMainLoop.
  222. -- g_main_is_running()
  223. -- #define g_main_is_running(loop)
  224. -- Warning
  225. -- g_main_is_running is deprecated and should not be used in newly-written code. USe g_main_loop_is_running() instead.
  226. -- Checks if the main loop is running.
  227. -- loop : a GMainLoop.
  228. -- Returns : TRUE if the main loop is running.
  229. -- G_PRIORITY_HIGH
  230. -- #define G_PRIORITY_HIGH -100
  231. -- Use this for high priority event sources. It is not used within GLib or GTK+.
  232. -- G_PRIORITY_DEFAULT
  233. -- #define G_PRIORITY_DEFAULT 0
  234. -- Use this for default priority event sources. In GLib this priority is used when adding timeout functions with g_timeout_add(). In GDK this priority is used for events from the X server.
  235. -- G_PRIORITY_HIGH_IDLE
  236. -- #define G_PRIORITY_HIGH_IDLE 100
  237. -- Use this for high priority idle functions. GTK+ uses G_PRIORITY_HIGH_IDLE + 10 for resizing operations, and G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is done to ensure that any pending resizes are processed before any pending redraws, so that widgets are not redrawn twice unnecessarily.)
  238. -- G_PRIORITY_DEFAULT_IDLE
  239. -- #define G_PRIORITY_DEFAULT_IDLE 200
  240. -- Use this for default priority idle functions. In GLib this priority is used when adding idle functions with g_idle_add().
  241. -- G_PRIORITY_LOW
  242. -- #define G_PRIORITY_LOW 300
  243. -- Use this for very low priority background tasks. It is not used within GLib or GTK+.
  244. -- GMainContext
  245. -- typedef struct _GMainContext GMainContext;
  246. -- The GMainContext struct is an opaque data type representing a set of sources to be handled in a main loop.
  247. -- g_main_context_new ()
  248. -- GMainContext* g_main_context_new (void);
  249. -- Creates a new GMainContext strcuture
  250. -- Returns : the new GMainContext
  251. -- g_main_context_ref ()
  252. -- GMainContext* g_main_context_ref (GMainContext *context);
  253. -- Increases the reference count on a GMainContext object by one.
  254. -- context : a GMainContext
  255. -- Returns : the context that was passed in (since 2.6)
  256. -- g_main_context_unref ()
  257. -- void g_main_context_unref (GMainContext *context);
  258. -- Decreases the reference count on a GMainContext object by one. If the result is zero, free the context and free all associated memory.
  259. -- context : a GMainContext
  260. -- g_main_context_default ()
  261. -- GMainContext* g_main_context_default (void);
  262. -- Returns the default main context. This is the main context used for main loop functions when a main loop is not explicitly specified.
  263. -- Returns : the default main context.
  264. -- g_main_context_iteration ()
  265. -- gboolean g_main_context_iteration (GMainContext *context,
  266. -- gboolean may_block);
  267. -- Runs a single iteration for the given main loop. This involves checking to see if any event sources are ready to be processed, then if no events sources are ready and may_block is TRUE, waiting for a source to become ready, then dispatching the highest priority events sources that are ready. Note that even when may_block is TRUE, it is still possible for g_main_context_iteration() to return FALSE, since the the wait may be interrupted for other reasons than an event source becoming ready.
  268. -- context : a GMainContext (if NULL, the default context will be used)
  269. -- may_block : whether the call may block.
  270. -- Returns : TRUE if events were dispatched.
  271. -- g_main_iteration()
  272. -- #define g_main_iteration(may_block)
  273. -- Warning
  274. -- g_main_iteration is deprecated and should not be used in newly-written code. Use g_main_context_iteration() instead.
  275. -- Runs a single iteration for the default GMainContext.
  276. -- may_block : set to TRUE if it should block (i.e. wait) until an event source becomes ready. It will return after an event source has been processed. If set to FALSE it will return immediately if no event source is ready to be processed.
  277. -- Returns : TRUE if more events are pending.
  278. -- g_main_context_pending ()
  279. -- gboolean g_main_context_pending (GMainContext *context);
  280. -- Checks if any sources have pending events for the given context.
  281. -- context : a GMainContext (if NULL, the default context will be used)
  282. -- Returns : TRUE if events are pending.
  283. -- g_main_pending()
  284. -- #define g_main_pending()
  285. -- Warning
  286. -- g_main_pending is deprecated and should not be used in newly-written code. Use g_main_context_pending() instead.
  287. -- Checks if any events are pending for the default GMainContext (i.e. ready to be processed).
  288. -- Returns : TRUE if any events are pending.
  289. -- g_main_context_find_source_by_id ()
  290. -- GSource* g_main_context_find_source_by_id
  291. -- (GMainContext *context,
  292. -- guint source_id);
  293. -- Finds a GSource given a pair of context and ID
  294. -- context : a GMainContext (if NULL, the default context will be used)
  295. -- source_id : the source ID, as returned by g_source_get_id()
  296. -- Returns : the GSource if found, otherwise, NULL
  297. -- g_main_context_find_source_by_user_data ()
  298. -- GSource* g_main_context_find_source_by_user_data
  299. -- (GMainContext *context,
  300. -- gpointer user_data);
  301. -- Finds a source with the given user data for the callback. If multiple sources exist with the same user data, the first one found will be returned.
  302. -- context : a GMainContext
  303. -- user_data : the user_data for the callback.
  304. -- Returns : the source, if one was found, otherwise NULL
  305. -- g_main_context_find_source_by_funcs_user_data ()
  306. -- GSource* g_main_context_find_source_by_funcs_user_data
  307. -- (GMainContext *context,
  308. -- GSourceFuncs *funcs,
  309. -- gpointer user_data);
  310. -- Finds a source with the given source functions and user data. If multiple sources exist with the same source function and user data, the first one found will be returned.
  311. -- context : a GMainContext (if NULL, the default context will be used).
  312. -- funcs : the source_funcs passed to g_source_new().
  313. -- user_data : the user data from the callback.
  314. -- Returns : the source, if one was found, otherwise NULL
  315. -- g_main_context_wakeup ()
  316. -- void g_main_context_wakeup (GMainContext *context);
  317. -- If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process.
  318. -- context : a GMainContext
  319. -- g_main_context_acquire ()
  320. -- gboolean g_main_context_acquire (GMainContext *context);
  321. -- Tries to become the owner of the specified context. If some other context is the owner of the context, returns FALSE immediately. Ownership is properly recursive: the owner can require ownership again and will release ownership when g_main_context_release() is called as many times as g_main_context_acquire().
  322. -- You must be the owner of a context before you can call g_main_context_prepare(), g_main_context_query(), g_main_context_check(), g_main_context_dispatch().
  323. -- context : a GMainContext
  324. -- Returns : TRUE if the operation succeeded, and this thread is now the owner of context.
  325. -- g_main_context_release ()
  326. -- void g_main_context_release (GMainContext *context);
  327. -- Releases ownership of a context previously acquired by this thread with g_main_context_acquire(). If the context was acquired multiple times, the only release ownership when g_main_context_release() is called as many times as it was acquired.
  328. -- context : a GMainContext
  329. -- g_main_context_wait ()
  330. -- gboolean g_main_context_wait (GMainContext *context,
  331. -- GCond *cond,
  332. -- GMutex *mutex);
  333. -- Tries to become the owner of the specified context, as with g_main_context_acquire(). But if another thread is the owner, atomically drop mutex and wait on cond until that owner releases ownership or until cond is signaled, then try again (once) to become the owner.
  334. -- context : a GMainContext
  335. -- cond : a condition variable
  336. -- mutex : a mutex, currently held
  337. -- Returns : TRUE if the operation succeeded, and this thread is now the owner of context.
  338. -- g_main_context_prepare ()
  339. -- gboolean g_main_context_prepare (GMainContext *context,
  340. -- gint *priority);
  341. -- Prepares to poll sources within a main loop. The resulting information for polling is determined by calling g_main_context_query().
  342. -- context : a GMainContext
  343. -- priority : location to store priority of highest priority source already ready.
  344. -- Returns : TRUE if some source is ready to be dispatched prior to polling.
  345. -- g_main_context_query ()
  346. -- gint g_main_context_query (GMainContext *context,
  347. -- gint max_priority,
  348. -- gint *timeout_,
  349. -- GPollFD *fds,
  350. -- gint n_fds);
  351. -- Determines information necessary to poll this main loop.
  352. -- context : a GMainContext
  353. -- max_priority : maximum priority source to check
  354. -- timeout_ : location to store timeout to be used in polling
  355. -- fds : location to store GPollFD records that need to be polled.
  356. -- n_fds : length of fds.
  357. -- Returns : the number of records actually stored in fds, or, if more than n_fds records need to be stored, the number of records that need to be stored.
  358. -- g_main_context_check ()
  359. -- gint g_main_context_check (GMainContext *context,
  360. -- gint max_priority,
  361. -- GPollFD *fds,
  362. -- gint n_fds);
  363. -- Passes the results of polling back to the main loop.
  364. -- context : a GMainContext
  365. -- max_priority : the maximum numerical priority of sources to check
  366. -- fds : array of GPollFD's that was passed to the last call to g_main_context_query()
  367. -- n_fds : return value of g_main_context_query()
  368. -- Returns : TRUE if some sources are ready to be dispatched.
  369. -- g_main_context_dispatch ()
  370. -- void g_main_context_dispatch (GMainContext *context);
  371. -- Dispatches all pending sources.
  372. -- context : a GMainContext
  373. -- g_main_context_set_poll_func ()
  374. -- void g_main_context_set_poll_func (GMainContext *context,
  375. -- GPollFunc func);
  376. -- Sets the function to use to handle polling of file descriptors. It will be used instead of the poll() system call (or GLib's replacement function, which is used where poll() isn't available).
  377. -- This function could possibly be used to integrate the GLib event loop with an external event loop.
  378. -- context : a GMainContext
  379. -- func : the function to call to poll all file descriptors
  380. -- g_main_context_get_poll_func ()
  381. -- GPollFunc g_main_context_get_poll_func (GMainContext *context);
  382. -- Gets the poll function set by g_main_context_set_poll_func().
  383. -- context : a GMainContext
  384. -- Returns : the poll function
  385. -- GPollFunc ()
  386. -- gint (*GPollFunc) (GPollFD *ufds,
  387. -- guint nfsd,
  388. -- gint timeout_);
  389. -- Specifies the type of function passed to g_main_context_set_poll_func(). The semantics of the function should match those of the poll() system call.
  390. -- ufds : an array of GPollFD elements.
  391. -- nfsd : the number of elements in ufds.
  392. -- timeout_ : the maximum time to wait for an event of the file descriptors. A negative value indicates an infinite timeout.
  393. -- Returns : the number of GPollFD elements which have events or errors reported, or -1 if an error occurred.
  394. -- g_main_context_add_poll ()
  395. -- void g_main_context_add_poll (GMainContext *context,
  396. -- GPollFD *fd,
  397. -- gint priority);
  398. -- Adds a file descriptor to the set of file descriptors polled for this context. This will very seldomly be used directly. Instead a typical event source will use g_source_add_poll() instead.
  399. -- context : a GMainContext (or NULL for the default context)
  400. -- fd : a GPollFD structure holding information about a file descriptor to watch.
  401. -- priority : the priority for this file descriptor which should be the same as the priority used for g_source_attach() to ensure that the file descriptor is polled whenever the results may be needed.
  402. -- g_main_context_remove_poll ()
  403. -- void g_main_context_remove_poll (GMainContext *context,
  404. -- GPollFD *fd);
  405. -- Removes file descriptor from the set of file descriptors to be polled for a particular context.
  406. -- context : a GMainContext
  407. -- fd : a GPollFD descriptor previously added with g_main_context_add_poll()
  408. -- g_main_depth ()
  409. -- int g_main_depth (void);
  410. -- Return value: The main loop recursion level in the current thread
  411. -- Returns : the depth of the stack of calls to g_main_context_dispatch() on any GMainContext in the current thread. That is, when called from the toplevel, it gives 0. When called from within a callback from g_main_context_iteration() (or g_main_loop_run(), etc.) it returns 1. When called from within a callback to a recursive call to g_main_context_iterate(), it returns 2. And so forth. This function is useful in a situation like the following: Imagine an extremely simple "garbage collected" system.
  412. -- Example 1.
  413. -- static GList *free_list; gpointer allocate_memory (gsize size) { gpointer result = g_malloc (size); free_list = g_list_prepend (free_list, result); return result; } void free_allocated_memory (void) { GList *l; for (l = free_list; l; l = l->next); g_free (l->data); g_list_free (free_list); free_list = NULL; } [...] while (TRUE); { g_main_context_iteration (NULL, TRUE); free_allocated_memory(); }
  414. -- This works from an application, however, if you want to do the same thing from a library, it gets more difficult, since you no longer control the main loop. You might think you can simply use an idle function to make the call to free_allocated_memory(), but that doesn't work, since the idle function could be called from a recursive callback. This can be fixed by using g_main_depth()
  415. -- Example 2.
  416. -- gpointer allocate_memory (gsize size) { FreeListBlock *block = g_new (FreeListBlock, 1);\ block->mem = g_malloc (size); block->depth = g_main_depth(); free_list = g_list_prepend (free_list, block); return block->mem; } void free_allocated_memory (void) { GList *l; int depth = g_main_depth(); for (l = free_list; l; ); { GList *next = l->next; FreeListBlock *block = l->data; if (block->depth > depth) { g_free (block->mem); g_free (block); free_list = g_list_delete_link (free_list, l); } l = next; } }
  417. -- There is a temptation to use g_main_depth() to solve problems with reentrancy. For instance, while waiting for data to be received from the network in response to a menu item, the menu item might be selected again. It might seem that one could make the menu item's callback return immediately and do nothing if g_main_depth() returns a value greater than 1. However, this should be avoided since the user then sees selecting the menu item do nothing. Furthermore, you'll find yourself adding these checks all over your code, since there are doubtless many, many things that the user could do. Instead, you can use the following techniques:
  418. -- 1.
  419. -- Use gtk_widget_set_sensitive() or modal dialogs to prevent the user from interacting with elements while the main loop is recursing.
  420. -- 2.
  421. -- Avoid main loop recursion in situations where you can't handle arbitrary callbacks. Instead, structure your code so that you simply return to the main loop and then get called again when there is more work to do.
  422. -- g_main_set_poll_func()
  423. -- #define g_main_set_poll_func(func)
  424. -- Warning
  425. -- g_main_set_poll_func is deprecated and should not be used in newly-written code. Use g_main_context_set_poll_func() instead.
  426. -- Sets the function to use for the handle polling of file descriptors for the default main context.
  427. -- func : the function to call to poll all file descriptors.
  428. -- g_idle_source_new ()
  429. -- GSource* g_idle_source_new (void);
  430. -- Creates a new idle source.
  431. -- The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed. Note that the default priority for idle sources is G_PRIORITY_DEFAULT_IDLE, as compared to other sources which have a default priority of G_PRIORITY_DEFAULT.
  432. -- Returns : the newly-created idle source
  433. -- g_idle_add ()
  434. -- guint g_idle_add (GSourceFunc function,
  435. -- gpointer data);
  436. -- Adds a function to be called whenever there are no higher priority events pending to the default main loop. The function is given the default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function returns FALSE it is automatically removed from the list of event sources and will not be called again.
  437. -- function : function to call
  438. -- data : data to pass to function.
  439. -- Returns : the id of the event source.
  440. -- g_idle_add_full ()
  441. -- guint g_idle_add_full (gint priority,
  442. -- GSourceFunc function,
  443. -- gpointer data,
  444. -- GDestroyNotify notify);
  445. -- Adds a function to be called whenever there are no higher priority events pending. If the function returns FALSE it is automatically removed from the list of event sources and will not be called again.
  446. -- priority : the priority of the idle source. Typically this will be in the range btweeen G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
  447. -- function : function to call
  448. -- data : data to pass to function
  449. -- notify : function to call when the idle is removed, or NULL
  450. -- Returns : the id of the event source.
  451. -- g_idle_remove_by_data ()
  452. -- gboolean g_idle_remove_by_data (gpointer data);
  453. -- Removes the idle function with the given data.
  454. -- data : the data for the idle source's callback.
  455. -- Returns : TRUE if an idle source was found and removed.
  456. -- GPid
  457. -- typedef int GPid;
  458. -- A type which is used to hold a process identification. On Unix, processes are identified by a process id (an integer), while Windows uses process handles (which are pointers).
  459. -- GChildWatchFunc ()
  460. -- void (*GChildWatchFunc) (GPid pid,
  461. -- gint status,
  462. -- gpointer data);
  463. -- The type of functions to be called when a child exists.
  464. -- pid : the process id of the child process
  465. -- status : Status information about the child process, see waitpid(2) for more information about this field
  466. -- data : user data passed to g_child_watch_add()
  467. -- g_child_watch_source_new ()
  468. -- GSource* g_child_watch_source_new (GPid pid);
  469. -- Creates a new child_watch source.
  470. -- The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed.
  471. -- Note that on platforms where GPid must be explicitely closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the callback function for the source.
  472. -- Note further that using g_child_watch_source_new() is not compatible with calling waitpid(-1) in the application. Calling waitpid() for individual pids will still work fine.
  473. -- pid : process id of a child process to watch. On Windows, a HANDLE for the process to watch (which actually doesn't have to be a child).
  474. -- Returns : the newly-created child watch source
  475. -- Since 2.4
  476. -- g_child_watch_add ()
  477. -- guint g_child_watch_add (GPid pid,
  478. -- GChildWatchFunc function,
  479. -- gpointer data);
  480. -- Sets a function to be called when the child indicated by pid exits, at a default priority, G_PRIORITY_DEFAULT.
  481. -- Note that on platforms where GPid must be explicitely closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the callback function for the source.
  482. -- GLib supports only a single callback per process id.
  483. -- pid : process id of a child process to watch
  484. -- function : function to call
  485. -- data : data to pass to function
  486. -- Returns : the id of event source.
  487. -- Since 2.4
  488. -- g_child_watch_add_full ()
  489. -- guint g_child_watch_add_full (gint priority,
  490. -- GPid pid,
  491. -- GChildWatchFunc function,
  492. -- gpointer data,
  493. -- GDestroyNotify notify);
  494. -- Sets a function to be called when the child indicated by pid exits, at a default priority, G_PRIORITY_DEFAULT.
  495. -- Note that on platforms where GPid must be explicitely closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the callback function for the source.
  496. -- GLib supports only a single callback per process id.
  497. -- priority : the priority of the idle source. Typically this will be in the range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
  498. -- pid : process id of a child process to watch
  499. -- function : function to call
  500. -- data : data to pass to function
  501. -- notify : function to call when the idle is removed, or NULL
  502. -- Returns : the id of event source.
  503. -- Since 2.4
  504. -- GPollFD
  505. -- typedef struct {
  506. -- gint fd;
  507. -- gushort events;
  508. -- gushort revents;
  509. -- } GPollFD;
  510. -- gint fd; the file descriptor to poll (or a HANDLE on Win32 platforms).
  511. -- gushort events; a bitwise combination of flags from GIOCondition, specifying which events should be polled for. Typically for reading from a file descriptor you would use G_IO_IN | G_IO_HUP | G_IO_ERR, and for writing you would use G_IO_OUT | G_IO_ERR.
  512. -- gushort revents; a bitwise combination of flags from GIOCondition, returned from the poll() function to indicate which events occurred.
  513. -- GSource
  514. -- typedef struct {
  515. -- } GSource;
  516. -- The GSource struct is an opaque data type representing an event source.
  517. -- GSourceDummyMarshal ()
  518. -- void (*GSourceDummyMarshal) (void);
  519. -- This is just a placeholder for GClosureMarshal, which cannot be used here for dependency reasons.
  520. -- GSourceFuncs
  521. -- typedef struct {
  522. -- gboolean (*prepare) (GSource *source,
  523. -- gint *timeout_);
  524. -- gboolean (*check) (GSource *source);
  525. -- gboolean (*dispatch) (GSource *source,
  526. -- GSourceFunc callback,
  527. -- gpointer user_data);
  528. -- void (*finalize) (GSource *source); /* Can be NULL */
  529. -- /* For use by g_source_set_closure */
  530. -- GSourceFunc closure_callback;
  531. -- GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
  532. -- } GSourceFuncs;
  533. -- The GSourceFuncs struct contains a table of functions used to handle event sources in a generic manner.
  534. -- prepare Called before all the file descriptors are polled. If the source can determine that it is ready here (without waiting for the results of the poll() call) it should return TRUE. It can also return a timeout_ value which should be the maximum timeout (in milliseconds) which should be passed to the poll() call. The actual timeout used will be -1 if all sources returned -1, or it will be the minimum of all the timeout_ values returned which were >= 0.
  535. -- check Called after all the file descriptors are polled. The source should return TRUE if it is ready to be dispatched. Note that some time may have passed since the previous prepare function was called, so the source should be checked again here.
  536. -- dispatch Called to dispatch the event source, after it has returned TRUE in either its prepare or its check function. The dispatch function is passed in a callback function and data. The callback function may be NULL if the source was never connected to a callback using g_source_set_callback(). The dispatch function should call the callback function with user_data and whatever additional parameters are needed for this type of event source.
  537. -- finalize Called when the source is finalized.
  538. -- For idle sources, the prepare and check functions always return TRUE to indicate that the source is always ready to be processed. The prepare function also returns a timeout value of 0 to ensure that the poll() call doesn't block (since that would be time wasted which could have been spent running the idle function).
  539. -- For timeout sources, the prepare and check functions both return TRUE if the timeout interval has expired. The prepare function also returns a timeout value to ensure that the poll() call doesn't block too long and miss the next timeout.
  540. -- For file descriptor sources, the prepare function typically returns FALSE, since it must wait until poll() has been called before it knows whether any events need to be processed. It sets the returned timeout to -1 to indicate that it doesn't mind how long the poll() call blocks. In the check function, it tests the results of the poll() call to see if the required condition has been met, and returns TRUE if so.
  541. -- GSourceCallbackFuncs
  542. -- typedef struct {
  543. -- void (*ref) (gpointer cb_data);
  544. -- void (*unref) (gpointer cb_data);
  545. -- void (*get) (gpointer cb_data,
  546. -- GSource *source,
  547. -- GSourceFunc *func,
  548. -- gpointer *data);
  549. -- } GSourceCallbackFuncs;
  550. -- The GSourceCallbackFuncs struct contains functions for managing callback objects.
  551. -- ref () Called when a reference is added to the callback object.
  552. -- unref () Called when a reference to the callback object is dropped.
  553. -- get () Called to extract the callback function and data from the callback object.
  554. -- g_source_new ()
  555. -- GSource* g_source_new (GSourceFuncs *source_funcs,
  556. -- guint struct_size);
  557. -- Creates a new GSource structure. The size is specified to allow creating structures derived from GSource that contain additional data. The size passed in must be at least sizeof (GSource).
  558. -- The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed.
  559. -- source_funcs : structure containing functions that implement the sources behavior.
  560. -- struct_size : size of the GSource structure to create.
  561. -- Returns : the newly-created GSource.
  562. -- g_source_ref ()
  563. -- GSource* g_source_ref (GSource *source);
  564. -- Increases the reference count on a source by one.
  565. -- source : a GSource
  566. -- Returns : source
  567. -- g_source_unref ()
  568. -- void g_source_unref (GSource *source);
  569. -- Decreases the reference count of a source by one. If the resulting reference count is zero the source and associated memory will be destroyed.
  570. -- source : a GSource
  571. -- g_source_attach ()
  572. -- guint g_source_attach (GSource *source,
  573. -- GMainContext *context);
  574. -- Adds a GSource to a context so that it will be executed within that context.
  575. -- source : a GSource
  576. -- context : a GMainContext (if NULL, the default context will be used)
  577. -- Returns : the ID for the source within the GMainContext
  578. -- g_source_destroy ()
  579. -- void g_source_destroy (GSource *source);
  580. -- Removes a source from its GMainContext, if any, and mark it as destroyed. The source cannot be subsequently added to another context.
  581. -- source : a GSource
  582. -- g_source_set_priority ()
  583. -- void g_source_set_priority (GSource *source,
  584. -- gint priority);
  585. -- Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.
  586. -- source : a GSource
  587. -- priority : the new priority.
  588. -- g_source_get_priority ()
  589. -- gint g_source_get_priority (GSource *source);
  590. -- Gets the priority of a source.
  591. -- source : a GSource
  592. -- Returns : the priority of the source
  593. -- g_source_set_can_recurse ()
  594. -- void g_source_set_can_recurse (GSource *source,
  595. -- gboolean can_recurse);
  596. -- Sets whether a source can be called recursively. If can_recurse is TRUE, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.
  597. -- source : a GSource
  598. -- can_recurse : whether recursion is allowed for this source
  599. -- g_source_get_can_recurse ()
  600. -- gboolean g_source_get_can_recurse (GSource *source);
  601. -- Checks whether a source is allowed to be called recursively. see g_source_set_can_recurse().
  602. -- source : a GSource
  603. -- Returns : whether recursion is allowed.
  604. -- g_source_get_id ()
  605. -- guint g_source_get_id (GSource *source);
  606. -- Returns the numeric ID for a particular source. The ID of a source is unique within a particular main loop context. The reverse mapping from ID to source is done by g_main_context_find_source_by_id().
  607. -- source : a GSource
  608. -- Returns : the ID for the source
  609. -- g_source_get_context ()
  610. -- GMainContext* g_source_get_context (GSource *source);
  611. -- Gets the GMainContext with which the source is associated. Calling this function on a destroyed source is an error.
  612. -- source : a GSource
  613. -- Returns : the GMainContext with which the source is associated, or NULL if the context has not yet been added to a source.
  614. -- g_source_set_callback ()
  615. -- void g_source_set_callback (GSource *source,
  616. -- GSourceFunc func,
  617. -- gpointer data,
  618. -- GDestroyNotify notify);
  619. -- Sets the callback function for a source. The callback for a source is called from the source's dispatch function.
  620. -- The exact type of func depends on the type of source; ie. you should not count on func being called with data as its first parameter.
  621. -- Typically, you won't use this function. Instead use functions specific to the type of source you are using.
  622. -- source : the source
  623. -- func : a callback function
  624. -- data : the data to pass to callback function
  625. -- notify : a function to call when data is no longer in use, or NULL.
  626. -- GSourceFunc ()
  627. -- gboolean (*GSourceFunc) (gpointer data);
  628. -- Specifies the type of function passed to g_timeout_add(), g_timeout_add_full(), g_idle_add(), and g_idle_add_full().
  629. -- data : data passed to the function, set when the source was created with one of the above functions.
  630. -- Returns : it should return FALSE if the source should be removed.
  631. -- g_source_set_callback_indirect ()
  632. -- void g_source_set_callback_indirect (GSource *source,
  633. -- gpointer callback_data,
  634. -- GSourceCallbackFuncs *callback_funcs);
  635. -- Sets the callback function storing the data as a refcounted callback "object". This is used internally. Note that calling g_source_set_callback_indirect() assumes an initial reference count on callback_data, and thus callback_funcs->unref will eventually be called once more than callback_funcs->ref.
  636. -- source : the source
  637. -- callback_data : pointer to callback data "object"
  638. -- callback_funcs : functions for reference counting callback_data and getting the callback and data
  639. -- g_source_add_poll ()
  640. -- void g_source_add_poll (GSource *source,
  641. -- GPollFD *fd);
  642. -- Adds a file descriptor to the set of file descriptors polled for this source. This is usually combined with g_source_new() to add an event source. The event source's check function will typically test the revents field in the GPollFD struct and return TRUE if events need to be processed.
  643. -- source : a GSource
  644. -- fd : a GPollFD structure holding information about a file descriptor to watch.
  645. -- g_source_remove_poll ()
  646. -- void g_source_remove_poll (GSource *source,
  647. -- GPollFD *fd);
  648. -- Removes a file descriptor from the set of file descriptors polled for this source.
  649. -- source : a GSource
  650. -- fd : a GPollFD structure previously passed to g_source_add_poll().
  651. -- g_source_get_current_time ()
  652. -- void g_source_get_current_time (GSource *source,
  653. -- GTimeVal *timeval);
  654. -- Gets the "current time" to be used when checking this source. The advantage of calling this function over calling g_get_current_time() directly is that when checking multiple sources, GLib can cache a single value instead of having to repeatedly get the system time.
  655. -- source : a GSource
  656. -- timeval : GTimeVal structure in which to store current time.
  657. feature -- Operations
  658. source_remove (tag: INTEGER) is
  659. -- Removes the source with the given id `tag' from the default
  660. -- main context. The id of a GSource is given by
  661. -- g_source_get_id(), or will be returned by the functions
  662. -- g_source_attach(), g_idle_add(), g_idle_add_full(),
  663. -- g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
  664. -- g_child_watch_add_full(), g_io_add_watch(),
  665. -- and g_io_add_watch_full().
  666. do
  667. source_removed := (g_source_remove (tag) /= 0)
  668. end
  669. source_removed: BOOLEAN
  670. feature {} -- Externals
  671. g_source_remove (tag: INTEGER): INTEGER is
  672. external "C use <glib.h>"
  673. alias "g_source_remove"
  674. end
  675. -- g_source_remove_by_funcs_user_data ()
  676. -- gboolean g_source_remove_by_funcs_user_data
  677. -- (GSourceFuncs *funcs,
  678. -- gpointer user_data);
  679. -- Removes a source from the default main loop context given the source functions and user data. If multiple sources exist with the same source functions and user data, only one will be destroyed.
  680. -- funcs : The source_funcs passed to g_source_new()
  681. -- user_data : the user data for the callback
  682. -- Returns : TRUE if a source was found and removed.
  683. -- g_source_remove_by_user_data ()
  684. -- gboolean g_source_remove_by_user_data (gpointer user_data);
  685. -- Removes a source from the default main loop context given the user data for the callback. If multiple sources exist with the same user data, only one will be destroyed.
  686. -- user_data : the user_data for the callback.
  687. -- Returns : TRUE if a source was found and removed.
  688. end