alternative to assert
[gtkD.git] / gtkD / src / gdk / Threads.d
blobd423f8b7425bab3afae6c7220a722298344c524d
1 /*
2 * This file is part of gtkD.
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * gtkD is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile = gdk-Threads.html
26 * outPack = gdk
27 * outFile = Threads
28 * strct =
29 * realStrct=
30 * ctorStrct=
31 * clss =
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * omit structs:
40 * omit prefixes:
41 * omit code:
42 * imports:
43 * structWrap:
44 * module aliases:
45 * local aliases:
48 module gdk.Threads;
50 version(noAssert)
52 version(Tango)
54 import tango.io.Stdout; // use the tango loging?
58 private import gtkc.gdktypes;
60 private import gtkc.gdk;
67 /**
68 * Description
69 * For thread safety, GDK relies on the thread primitives in GLib,
70 * and on the thread-safe GLib main loop.
71 * GLib is completely thread safe (all global data is automatically
72 * locked), but individual data structure instances are not automatically
73 * locked for performance reasons. So e.g. you must coordinate
74 * accesses to the same GHashTable from multiple threads.
75 * GTK+ is "thread aware" but not thread safe it provides a
76 * global lock controlled by gdk_threads_enter()/gdk_threads_leave()
77 * which protects all use of GTK+. That is, only one thread can use GTK+
78 * at any given time.
79 * Unfortunately the above holds with the X11 backend only. With the
80 * Win32 backend, GDK calls should not be attempted from multiple threads
81 * at all.
82 * You must call g_thread_init() and gdk_threads_init() before executing
83 * any other GTK+ or GDK functions in a threaded GTK+ program.
84 * Idles, timeouts, and input functions are executed outside
85 * of the main GTK+ lock. So, if you need to call GTK+
86 * inside of such a callback, you must surround the callback
87 * with a gdk_threads_enter()/gdk_threads_leave() pair.
88 * (However, signals are still executed within the main
89 * GTK+ lock.)
90 * In particular, this means, if you are writing widgets that might
91 * be used in threaded programs, you must surround
92 * timeouts and idle functions in this matter.
93 * As always, you must also surround any calls to GTK+ not made within
94 * a signal handler with a gdk_threads_enter()/gdk_threads_leave() pair.
95 * Before calling gdk_threads_leave() from a thread other
96 * than your main thread, you probably want to call gdk_flush()
97 * to send all pending commands to the windowing system.
98 * (The reason you don't need to do this from the main thread
99 * is that GDK always automatically flushes pending commands
100 * when it runs out of incoming events to process and has
101 * to sleep while waiting for more events.)
102 * A minimal main program for a threaded GTK+ application
103 * looks like:
104 * int
105 * main (int argc, char *argv[])
107 * GtkWidget *window;
108 * g_thread_init (NULL);
109 * gdk_threads_init ();
110 * gdk_threads_enter ();
111 * gtk_init (argc, argv);
112 * window = create_window ();
113 * gtk_widget_show (window);
114 * gtk_main ();
115 * gdk_threads_leave ();
116 * return 0;
118 * Callbacks require a bit of attention. Callbacks from GTK+ signals
119 * are made within the GTK+ lock. However callbacks from GLib (timeouts,
120 * IO callbacks, and idle functions) are made outside of the GTK+
121 * lock. So, within a signal handler you do not need to call
122 * gdk_threads_enter(), but within the other types of callbacks, you
123 * do.
124 * Erik Mouw contributed the following code example to
125 * illustrate how to use threads within GTK+ programs.
126 * /+*-------------------------------------------------------------------------
127 * * Filename: gtk-thread.c
128 * * Version: 0.99.1
129 * * Copyright: Copyright (C) 1999, Erik Mouw
130 * * Author: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
131 * * Description: GTK threads example.
132 * * Created at: Sun Oct 17 21:27:09 1999
133 * * Modified by: Erik Mouw <J.A.K.Mouw@its.tudelft.nl>
134 * * Modified at: Sun Oct 24 17:21:41 1999
135 * *-----------------------------------------------------------------------+/
136 * /+*
137 * * Compile with:
139 * * cc -o gtk-thread gtk-thread.c `gtk-config --cflags --libs gthread`
141 * * Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some
142 * * bugs.
144 * +/
145 * #include <stdio.h>
146 * #include <stdlib.h>
147 * #include <unistd.h>
148 * #include <time.h>
149 * #include <gtk/gtk.h>
150 * #include <glib.h>
151 * #include <pthread.h>
152 * #define YES_IT_IS (1)
153 * #define NO_IT_IS_NOT (0)
154 * typedef struct
156 * GtkWidget *label;
157 * int what;
158 * } yes_or_no_args;
159 * G_LOCK_DEFINE_STATIC (yes_or_no);
160 * static volatile int yes_or_no = YES_IT_IS;
161 * void destroy (GtkWidget *widget, gpointer data)
163 * gtk_main_quit ();
165 * void *argument_thread (void *args)
167 * yes_or_no_args *data = (yes_or_no_args *)args;
168 * gboolean say_something;
169 * for (;;)
171 * /+* sleep a while +/
172 * sleep(rand() / (RAND_MAX / 3) + 1);
173 * /+* lock the yes_or_no_variable +/
174 * G_LOCK(yes_or_no);
175 * /+* do we have to say something? +/
176 * say_something = (yes_or_no != data->what);
177 * if(say_something)
179 * /+* set the variable +/
180 * yes_or_no = data->what;
182 * /+* Unlock the yes_or_no variable +/
183 * G_UNLOCK (yes_or_no);
184 * if (say_something)
186 * /+* get GTK thread lock +/
187 * gdk_threads_enter ();
188 * /+* set label text +/
189 * if(data->what == YES_IT_IS)
190 * gtk_label_set_text (GTK_LABEL (data->label), "O yes, it is!");
191 * else
192 * gtk_label_set_text (GTK_LABEL (data->label), "O no, it isn't!");
193 * /+* release GTK thread lock +/
194 * gdk_threads_leave ();
197 * return NULL;
199 * int main (int argc, char *argv[])
201 * GtkWidget *window;
202 * GtkWidget *label;
203 * yes_or_no_args yes_args, no_args;
204 * pthread_t no_tid, yes_tid;
205 * /+* init threads +/
206 * g_thread_init (NULL);
207 * gdk_threads_init ();
208 * gdk_threads_enter ();
209 * /+* init gtk +/
210 * gtk_init(argc, argv);
211 * /+* init random number generator +/
212 * srand ((unsigned int) time (NULL));
213 * /+* create a window +/
214 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
215 * gtk_signal_connect (GTK_OBJECT (window), "destroy",
216 * GTK_SIGNAL_FUNC (destroy), NULL);
217 * gtk_container_set_border_width (GTK_CONTAINER (window), 10);
218 * /+* create a label +/
219 * label = gtk_label_new ("And now for something completely different ...");
220 * gtk_container_add (GTK_CONTAINER (window), label);
221 * /+* show everything +/
222 * gtk_widget_show (label);
223 * gtk_widget_show (window);
224 * /+* create the threads +/
225 * yes_args.label = label;
226 * yes_args.what = YES_IT_IS;
227 * pthread_create (yes_tid, NULL, argument_thread, yes_args);
228 * no_args.label = label;
229 * no_args.what = NO_IT_IS_NOT;
230 * pthread_create (no_tid, NULL, argument_thread, no_args);
231 * /+* enter the GTK main loop +/
232 * gtk_main ();
233 * gdk_threads_leave ();
234 * return 0;
244 * Initializes GDK so that it can be used from multiple threads
245 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
246 * g_thread_init() must be called previous to this function.
247 * This call must be made before any use of the main loop from
248 * GTK+; to be safe, call it before gtk_init().
250 public static void gdkThreadsInit()
252 // void gdk_threads_init (void);
253 gdk_threads_init();
257 * This macro marks the beginning of a critical section
258 * in which GDK and GTK+ functions can be called.
259 * Only one thread at a time can be in such a critial
260 * section.
262 public static void gdkThreadsEnter()
264 // void gdk_threads_enter (void);
265 gdk_threads_enter();
269 * Leaves a critical region begun with gdk_threads_enter().
271 public static void gdkThreadsLeave()
273 // void gdk_threads_leave (void);
274 gdk_threads_leave();
279 * Allows the application to replace the standard method that
280 * GDK uses to protect its data structures. Normally, GDK
281 * creates a single GMutex that is locked by gdk_threads_enter(),
282 * and released by gdk_threads_leave(); using this function an
283 * application provides, instead, a function enter_fn that is
284 * called by gdk_threads_enter() and a function leave_fn that is
285 * called by gdk_threads_leave().
286 * The functions must provide at least same locking functionality
287 * as the default implementation, but can also do extra application
288 * specific processing.
289 * As an example, consider an application that has its own recursive
290 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
291 * the GTK+ lock when entering a recursive main loop, the application
292 * must temporarily release its lock as well.
293 * Most threaded GTK+ apps won't need to use this method.
294 * This method must be called before gdk_threads_init(), and cannot
295 * be called multiple times.
296 * enter_fn:
297 * function called to guard GDK
298 * leave_fn:
299 * function called to release the guard
300 * Since 2.4
302 public static void gdkThreadsSetLockFunctions(GCallback enterFn, GCallback leaveFn)
304 // void gdk_threads_set_lock_functions (GCallback enter_fn, GCallback leave_fn);
305 gdk_threads_set_lock_functions(enterFn, leaveFn);
309 * A wrapper for the common usage of gdk_threads_add_idle_full()
310 * assigning the default priority, G_PRIORITY_DEFAULT_IDLE.
311 * See gdk_threads_add_idle_full().
312 * function:
313 * function to call
314 * data:
315 * data to pass to function
316 * Returns:
317 * Since 2.12
319 public static uint gdkThreadsAddIdle(GSourceFunc funct, void* data)
321 // guint gdk_threads_add_idle (GSourceFunc function, gpointer data);
322 return gdk_threads_add_idle(funct, data);
326 * Adds a function to be called whenever there are no higher priority
327 * events pending. If the function returns FALSE it is automatically
328 * removed from the list of event sources and will not be called again.
329 * This variant of g_idle_add_full() calls function with the GDK lock
330 * held. It can be thought of a MT-safe version for GTK+ widgets for the
331 * following use case, where you have to worry about idle_callback()
332 * running in thread A and accessing self after it has been finalized
333 * in thread B:
334 * static gboolean idle_callback (gpointer data)
336 * SomeWidget *self = data;
337 * /+* do stuff with self +/
338 * self->idle_id = 0;
339 * return FALSE;
341 * static void some_widget_do_stuff_later (SomeWidget *self)
343 * self->idle_id = g_idle_add (idle_callback, self)
345 * static void some_widget_finalize (GObject *object)
347 * SomeWidget *self = SOME_WIDGET(object);
348 * if (self->idle_id)
349 * g_source_remove (self->idle_id);
350 * G_OBJECT_CLASS (parent_class)->finalize (object);
352 * priority:
353 * the priority of the idle source. Typically this will be in the
354 * range btweeen G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE
355 * function:
356 * function to call
357 * data:
358 * data to pass to function
359 * notify:
360 * function to call when the idle is removed, or NULL
361 * Returns:
362 * the ID (greater than 0) of the event source.
363 * Since 2.12
365 public static uint gdkThreadsAddIdleFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify)
367 // guint gdk_threads_add_idle_full (gint priority, GSourceFunc function, gpointer data, GDestroyNotify notify);
368 return gdk_threads_add_idle_full(priority, funct, data, notify);
372 * A wrapper for the common usage of gdk_threads_add_timeout_full()
373 * assigning the default priority, G_PRIORITY_DEFAULT.
374 * See gdk_threads_add_timeout_full().
375 * interval:
376 * the time between calls to the function, in milliseconds
377 * (1/1000ths of a second)
378 * function:
379 * function to call
380 * data:
381 * data to pass to function
382 * Returns:
383 * Since 2.12
385 public static uint gdkThreadsAddTimeout(uint interval, GSourceFunc funct, void* data)
387 // guint gdk_threads_add_timeout (guint interval, GSourceFunc function, gpointer data);
388 return gdk_threads_add_timeout(interval, funct, data);
392 * Sets a function to be called at regular intervals holding the GDK lock,
393 * with the given priority. The function is called repeatedly until it
394 * returns FALSE, at which point the timeout is automatically destroyed
395 * and the function will not be called again. The notify function is
396 * called when the timeout is destroyed. The first call to the
397 * function will be at the end of the first interval.
398 * Note that timeout functions may be delayed, due to the processing of other
399 * event sources. Thus they should not be relied on for precise timing.
400 * After each call to the timeout function, the time of the next
401 * timeout is recalculated based on the current time and the given interval
402 * (it does not try to 'catch up' time lost in delays).
403 * This variant of g_timeout_add_full() can be thought of a MT-safe version
404 * for GTK+ widgets for the following use case:
405 * static gboolean timeout_callback (gpointer data)
407 * SomeWidget *self = data;
408 * /+* do stuff with self +/
409 * self->timeout_id = 0;
410 * return FALSE;
412 * static void some_widget_do_stuff_later (SomeWidget *self)
414 * self->timeout_id = g_timeout_add (timeout_callback, self)
416 * static void some_widget_finalize (GObject *object)
418 * SomeWidget *self = SOME_WIDGET(object);
419 * if (self->timeout_id)
420 * g_source_remove (self->timeout_id);
421 * G_OBJECT_CLASS (parent_class)->finalize (object);
423 * priority:
424 * the priority of the timeout source. Typically this will be in the
425 * range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
426 * interval:
427 * the time between calls to the function, in milliseconds
428 * (1/1000ths of a second)
429 * function:
430 * function to call
431 * data:
432 * data to pass to function
433 * notify:
434 * function to call when the timeout is removed, or NULL
435 * Returns:
436 * the ID (greater than 0) of the event source.
437 * Since 2.12
439 public static uint gdkThreadsAddTimeoutFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify)
441 // guint gdk_threads_add_timeout_full (gint priority, guint interval, GSourceFunc function, gpointer data, GDestroyNotify notify);
442 return gdk_threads_add_timeout_full(priority, interval, funct, data, notify);