alternative to assert
[gtkD.git] / src / gdk / Threads.d
blob5990399d17952e3578dcc255e304c4429b61e64b
1 /*
2 * This file is part of duit.
4 * duit 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 * duit 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 duit; 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 * local aliases:
47 module gdk.Threads;
49 private import gdk.gdktypes;
51 private import lib.gdk;
54 /**
55 * Description
56 * For thread safety, GDK relies on the thread primitives in GLib,
57 * and on the thread-safe GLib main loop.
58 * GLib is completely thread safe (all global data is automatically
59 * locked), but individual data structure instances are not automatically
60 * locked for performance reasons. So e.g. you must coordinate
61 * accesses to the same GHashTable from multiple threads.
62 * GTK+ is "thread aware" but not thread safe it provides a
63 * global lock controlled by gdk_threads_enter()/gdk_threads_leave()
64 * which protects all use of GTK+. That is, only one thread can use GTK+
65 * at any given time.
66 * Unfortunately the above holds with the X11 backend only. With the
67 * Win32 backend, GDK calls should not be attempted from multiple threads
68 * at all.
69 * You must call g_thread_init() and gdk_threads_init() before executing
70 * any other GTK+ or GDK functions in a threaded GTK+ program.
71 * Idles, timeouts, and input functions are executed outside
72 * of the main GTK+ lock. So, if you need to call GTK+
73 * inside of such a callback, you must surround the callback
74 * with a gdk_threads_enter()/gdk_threads_leave() pair.
75 * (However, signals are still executed within the main
76 * GTK+ lock.)
77 * In particular, this means, if you are writing widgets that might
78 * be used in threaded programs, you must surround
79 * timeouts and idle functions in this matter.
80 * As always, you must also surround any calls to GTK+ not made within
81 * a signal handler with a gdk_threads_enter()/gdk_threads_leave() pair.
82 * Before calling gdk_threads_leave() from a thread other
83 * than your main thread, you probably want to call gdk_flush()
84 * to send all pending commands to the windowing system.
85 * (The reason you don't need to do this from the main thread
86 * is that GDK always automatically flushes pending commands
87 * when it runs out of incoming events to process and has
88 * to sleep while waiting for more events.)
89 * A minimal main program for a threaded GTK+ application
90 * looks like:
91 * int
92 * main (int argc, char *argv[])
93 * {
94 * GtkWidget *window;
95 * g_thread_init (NULL);
96 * gdk_threads_init ();
97 * gdk_threads_enter ();
98 * gtk_init (argc, argv);
99 * window = create_window ();
100 * gtk_widget_show (window);
101 * gtk_main ();
102 * gdk_threads_leave ();
103 * return 0;
105 * Callbacks require a bit of attention. Callbacks from GTK+ signals
106 * are made within the GTK+ lock. However callbacks from GLib (timeouts,
107 * IO callbacks, and idle functions) are made outside of the GTK+
108 * lock. So, within a signal handler you do not need to call
109 * gdk_threads_enter(), but within the other types of callbacks, you
110 * do.
111 * Erik Mouw contributed the following code example to
112 * illustrate how to use threads within GTK+ programs.
113 * /+*-------------------------------------------------------------------------
114 * * Filename: gtk-thread.c
115 * * Version: 0.99.1
116 * * Copyright: Copyright (C) 1999, Erik Mouw
117 * * Author: Erik Mouw <J.A.K.Mouwits.tudelft.nl>
118 * * Description: GTK threads example.
119 * * Created at: Sun Oct 17 21:27:09 1999
120 * * Modified by: Erik Mouw <J.A.K.Mouwits.tudelft.nl>
121 * * Modified at: Sun Oct 24 17:21:41 1999
122 * *-----------------------------------------------------------------------+/
123 * /+*
124 * * Compile with:
126 * * cc -o gtk-thread gtk-thread.c `gtk-config --cflags --libs gthread`
128 * * Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some
129 * * bugs.
131 * +/
132 * #include <stdio.h>
133 * #include <stdlib.h>
134 * #include <unistd.h>
135 * #include <time.h>
136 * #include <gtk/gtk.h>
137 * #include <glib.h>
138 * #include <pthread.h>
139 * #define YES_IT_IS (1)
140 * #define NO_IT_IS_NOT (0)
141 * typedef struct
143 * GtkWidget *label;
144 * int what;
145 * } yes_or_no_args;
146 * G_LOCK_DEFINE_STATIC (yes_or_no);
147 * static volatile int yes_or_no = YES_IT_IS;
148 * void destroy (GtkWidget *widget, gpointer data)
150 * gtk_main_quit ();
152 * void *argument_thread (void *args)
154 * yes_or_no_args *data = (yes_or_no_args *)args;
155 * gboolean say_something;
156 * for (;;)
158 * /+* sleep a while +/
159 * sleep(rand() / (RAND_MAX / 3) + 1);
160 * /+* lock the yes_or_no_variable +/
161 * G_LOCK(yes_or_no);
162 * /+* do we have to say something? +/
163 * say_something = (yes_or_no != data->what);
164 * if(say_something)
166 * /+* set the variable +/
167 * yes_or_no = data->what;
169 * /+* Unlock the yes_or_no variable +/
170 * G_UNLOCK (yes_or_no);
171 * if (say_something)
173 * /+* get GTK thread lock +/
174 * gdk_threads_enter ();
175 * /+* set label text +/
176 * if(data->what == YES_IT_IS)
177 * gtk_label_set_text (GTK_LABEL (data->label), "O yes, it is!");
178 * else
179 * gtk_label_set_text (GTK_LABEL (data->label), "O no, it isn't!");
180 * /+* release GTK thread lock +/
181 * gdk_threads_leave ();
184 * return NULL;
186 * int main (int argc, char *argv[])
188 * GtkWidget *window;
189 * GtkWidget *label;
190 * yes_or_no_args yes_args, no_args;
191 * pthread_t no_tid, yes_tid;
192 * /+* init threads +/
193 * g_thread_init (NULL);
194 * gdk_threads_init ();
195 * gdk_threads_enter ();
196 * /+* init gtk +/
197 * gtk_init(argc, argv);
198 * /+* init random number generator +/
199 * srand ((unsigned int) time (NULL));
200 * /+* create a window +/
201 * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
202 * gtk_signal_connect (GTK_OBJECT (window), "destroy",
203 * GTK_SIGNAL_FUNC (destroy), NULL);
204 * gtk_container_set_border_width (GTK_CONTAINER (window), 10);
205 * /+* create a label +/
206 * label = gtk_label_new ("And now for something completely different ...");
207 * gtk_container_add (GTK_CONTAINER (window), label);
208 * /+* show everything +/
209 * gtk_widget_show (label);
210 * gtk_widget_show (window);
211 * /+* create the threads +/
212 * yes_args.label = label;
213 * yes_args.what = YES_IT_IS;
214 * pthread_create (yes_tid, NULL, argument_thread, yes_args);
215 * no_args.label = label;
216 * no_args.what = NO_IT_IS_NOT;
217 * pthread_create (no_tid, NULL, argument_thread, no_args);
218 * /+* enter the GTK main loop +/
219 * gtk_main ();
220 * gdk_threads_leave ();
221 * return 0;
231 * Initializes GDK so that it can be used from multiple threads
232 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
233 * g_thread_init() must be called previous to this function.
234 * This call must be made before any use of the main loop from
235 * GTK+; to be safe, call it before gtk_init().
237 public static void gdkThreadsInit()
239 // void gdk_threads_init (void);
240 gdk_threads_init();
244 * This macro marks the beginning of a critical section
245 * in which GDK and GTK+ functions can be called.
246 * Only one thread at a time can be in such a critial
247 * section.
249 public static void gdkThreadsEnter()
251 // void gdk_threads_enter (void);
252 gdk_threads_enter();
256 * Leaves a critical region begun with gdk_threads_enter().
258 public static void gdkThreadsLeave()
260 // void gdk_threads_leave (void);
261 gdk_threads_leave();
266 * Allows the application to replace the standard method that
267 * GDK uses to protect its data structures. Normally, GDK
268 * creates a single GMutex that is locked by gdk_threads_enter(),
269 * and released by gdk_threads_leave(); using this function an
270 * application provides, instead, a function enter_fn that is
271 * called by gdk_threads_enter() and a function leave_fn that is
272 * called by gdk_threads_leave().
273 * The functions must provide at least same locking functionality
274 * as the default implementation, but can also do extra application
275 * specific processing.
276 * As an example, consider an application that has its own recursive
277 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
278 * the GTK+ lock when entering a recursive main loop, the application
279 * must temporarily release its lock as well.
280 * Most threaded GTK+ apps won't need to use this method.
281 * This method must be called before gdk_threads_init(), and cannot
282 * be called multiple times.
283 * enter_fn:
284 * function called to guard GDK
285 * leave_fn:
286 * function called to release the guard
287 * Since 2.4
289 public static void gdkThreadsSetLockFunctions(GCallback enterFn, GCallback leaveFn)
291 // void gdk_threads_set_lock_functions (GCallback enter_fn, GCallback leave_fn);
292 gdk_threads_set_lock_functions(enterFn, leaveFn);