alternative to assert
[gtkD.git] / src / gthread / Thread.d
blobd5ef494da6f0761bb806374f2047180d7b819f66
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 =
26 * outPack = gthread
27 * outFile = Thread
28 * strct = GThread
29 * realStrct=
30 * ctorStrct=
31 * clss = Thread
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_thread_
40 * omit structs:
41 * omit prefixes:
42 * - g_mutex_
43 * - g_static_rec_mutex_
44 * - g_static_rw_lock_
45 * - g_cond_
46 * - g_private_
47 * - g_static_private_
48 * omit code:
49 * imports:
50 * - glib.ErrorG
51 * - gthread.Mutex
52 * - glib.Source
53 * - glib.Dataset
54 * - glib.ListG
55 * structWrap:
56 * - GDataset* -> Dataset
57 * - GList* -> ListG
58 * - GMutex* -> Mutex
59 * - GSource* -> Source
60 * - GThread* -> Thread
61 * local aliases:
64 module gthread.Thread;
66 private import gthread.gthreadtypes;
68 private import lib.gthread;
70 private import glib.ErrorG;
71 private import gthread.Mutex;
72 private import glib.Source;
73 private import glib.Dataset;
74 private import glib.ListG;
76 /**
77 * Description
78 * Threads act almost like processes, but unlike processes all threads of
79 * one process share the same memory. This is good, as it provides easy
80 * communication between the involved threads via this shared memory, and
81 * it is bad, because strange things (so called "Heisenbugs") might
82 * happen if the program is not carefully designed. In particular, due to
83 * the concurrent nature of threads, no assumptions on the order of
84 * execution of code running in different threads can be made, unless
85 * order is explicitly forced by the programmer through synchronization
86 * primitives.
87 * The aim of the thread related functions in GLib is to provide a
88 * portable means for writing multi-threaded software. There are
89 * primitives for mutexes to protect the access to portions of memory
90 * (GMutex, GStaticMutex, G_LOCK_DEFINE, GStaticRecMutex and
91 * GStaticRWLock). There are primitives for condition variables to allow
92 * synchronization of threads (GCond). There are primitives
93 * for thread-private data - data that every thread has a private instance of
94 * (GPrivate, GStaticPrivate). Last but definitely not least there are
95 * primitives to portably create and manage threads (GThread).
96 * You must call g_thread_init() before executing any other GLib
97 * functions in a threaded GLib program. After that, GLib is completely
98 * thread safe (all global data is automatically locked), but individual
99 * data structure instances are not automatically locked for performance
100 * reasons. So, for example you must coordinate accesses to the same
101 * GHashTable from multiple threads. The two notable exceptions from
102 * this rule are GMainLoop and GAsyncQueue,
103 * which are threadsafe and needs no further
104 * application-level locking to be accessed from multiple threads.
106 public class Thread
109 /** the main Gtk struct */
110 protected GThread* gThread;
113 public GThread* getThreadStruct()
115 return gThread;
119 /** the main Gtk struct as a void* */
120 protected void* getStruct()
122 return cast(void*)gThread;
126 * Sets our main struct and passes it to the parent class
128 public this (GThread* gThread)
130 this.gThread = gThread;
143 * If you use GLib from more than one thread, you must initialize
144 * the thread system by calling g_thread_init(). Most of the time you
145 * will only have to call g_thread_init (NULL).
146 * Note
147 * Do not call g_thread_init() with a non-NULL parameter unless you
148 * really know what you are doing.
149 * Note
150 * g_thread_init() must not be called directly or indirectly as a
151 * callback from GLib. Also no mutexes may be currently locked while
152 * calling g_thread_init().
153 * g_thread_init() might only be called once. On the second call
154 * it will abort with an error. If you want to make sure that the thread
155 * system is initialized, you can do this:
156 * if (!g_thread_supported ()) g_thread_init (NULL);
157 * After that line, either the thread system is initialized or, if no
158 * thread system is available in GLib (i.e. either G_THREADS_ENABLED is
159 * not defined or G_THREADS_IMPL_NONE is defined), the program will
160 * abort.
161 * If no thread system is available and vtable is NULL or if not all
162 * elements of vtable are non-NULL, then g_thread_init() will abort.
163 * Note
164 * To use g_thread_init() in your program, you have to link with the
165 * libraries that the command pkg-config --libs gthread-2.0
166 * outputs. This is not the case for all the other thread related functions of
167 * GLib. Those can be used without having to link with the thread libraries.
168 * vtable:
169 * a function table of type GThreadFunctions, that provides the
170 * entry points to the thread system to be used.
172 public static void init(GThreadFunctions* vtable)
174 // void g_thread_init (GThreadFunctions *vtable);
175 g_thread_init(vtable);
179 * This function returns TRUE if the thread system is initialized, and
180 * FALSE if it is not.
181 * Note
182 * This function is actually a macro. Apart from taking the address of it
183 * you can however use it as if it was a function.
184 * Returns:
185 * TRUE, if the thread system is initialized.
187 public static int supported()
189 // gboolean g_thread_supported ();
190 return g_thread_supported();
197 * This function creates a new thread with the default priority.
198 * If joinable is TRUE, you can wait for this threads termination
199 * calling g_thread_join(). Otherwise the thread will just disappear when
200 * it terminates.
201 * The new thread executes the function func with the argument
202 * data. If the thread was created successfully, it is returned.
203 * error can be NULL to ignore errors, or non-NULL to report errors. The
204 * error is set, if and only if the function returns NULL.
205 * func:
206 * a function to execute in the new thread.
207 * data:
208 * an argument to supply to the new thread.
209 * joinable:
210 * should this thread be joinable?
211 * error:
212 * return location for error.
213 * Returns:
214 * the new GThread on success.
216 public static Thread create(GThreadFunc func, void* data, int joinable, GError** error)
218 // GThread* g_thread_create (GThreadFunc func, gpointer data, gboolean joinable, GError **error);
219 return new Thread( g_thread_create(func, data, joinable, error) );
223 * This function creates a new thread with the priority priority. If the
224 * underlying thread implementation supports it, the thread gets a stack
225 * size of stack_size or the default value for the current platform, if
226 * stack_size is 0.
227 * If joinable is TRUE, you can wait for this threads termination
228 * calling g_thread_join(). Otherwise the thread will just disappear when
229 * it terminates. If bound is TRUE, this thread will be scheduled in
230 * the system scope, otherwise the implementation is free to do
231 * scheduling in the process scope. The first variant is more expensive
232 * resource-wise, but generally faster. On some systems (e.g. Linux) all
233 * threads are bound.
234 * The new thread executes the function func with the argument
235 * data. If the thread was created successfully, it is returned.
236 * error can be NULL to ignore errors, or non-NULL to report errors. The
237 * error is set, if and only if the function returns NULL.
238 * Note
239 * It is not guaranteed that threads with different priorities really
240 * behave accordingly. On some systems (e.g. Linux) there are no thread
241 * priorities. On other systems (e.g. Solaris) there doesn't seem to be
242 * different scheduling for different priorities. All in all try to avoid
243 * being dependent on priorities. Use G_THREAD_PRIORITY_NORMAL here as a
244 * default.
245 * Note
246 * Only use g_thread_create_full() if you really can't use
247 * g_thread_create() instead. g_thread_create() does not take
248 * stack_size, bound, and priority as arguments, as they should only
249 * be used in cases in which it is unavoidable.
250 * func:
251 * a function to execute in the new thread.
252 * data:
253 * an argument to supply to the new thread.
254 * stack_size:
255 * a stack size for the new thread.
256 * joinable:
257 * should this thread be joinable?
258 * bound:
259 * should this thread be bound to a system thread?
260 * priority:
261 * a priority for the thread.
262 * error:
263 * return location for error.
264 * Returns:
265 * the new GThread on success.
267 public static Thread createFull(GThreadFunc func, void* data, uint stackSize, int joinable, int bound, GThreadPriority priority, GError** error)
269 // GThread* g_thread_create_full (GThreadFunc func, gpointer data, gulong stack_size, gboolean joinable, gboolean bound, GThreadPriority priority, GError **error);
270 return new Thread( g_thread_create_full(func, data, stackSize, joinable, bound, priority, error) );
274 * This functions returns the GThread corresponding to the calling thread.
275 * Returns:
276 * the current thread.
278 public static Thread self()
280 // GThread* g_thread_self (void);
281 return new Thread( g_thread_self() );
285 * Waits until thread finishes, i.e. the function func, as given
286 * to g_thread_create(), returns or g_thread_exit() is called by
287 * thread. All resources of thread including the GThread struct are
288 * released. thread must have been created with joinable=TRUE in
289 * g_thread_create(). The value returned by func or given to
290 * g_thread_exit() by thread is returned by this function.
291 * thread:
292 * a GThread to be waited for.
293 * Returns:
294 * the return value of the thread.
296 public void* join()
298 // gpointer g_thread_join (GThread *thread);
299 return g_thread_join(gThread);
303 * Changes the priority of thread to priority.
304 * Note
305 * It is not guaranteed that threads with different priorities really
306 * behave accordingly. On some systems (e.g. Linux) there are no thread
307 * priorities. On other systems (e.g. Solaris) there doesn't seem to be
308 * different scheduling for different priorities. All in all try to avoid
309 * being dependent on priorities.
310 * thread:
311 * a GThread.
312 * priority:
313 * a new priority for thread.
315 public void setPriority(GThreadPriority priority)
317 // void g_thread_set_priority (GThread *thread, GThreadPriority priority);
318 g_thread_set_priority(gThread, priority);
322 * Gives way to other threads waiting to be scheduled.
323 * This function is often used as a method to make busy wait less
324 * evil. But in most cases you will encounter, there are better methods
325 * to do that. So in general you shouldn't use this function.
327 public static void yield()
329 // void g_thread_yield ();
330 g_thread_yield();
334 * Exits the current thread. If another thread is waiting for that thread
335 * using g_thread_join() and the current thread is joinable, the waiting
336 * thread will be woken up and get retval as the return value of
337 * g_thread_join(). If the current thread is not joinable, retval is
338 * ignored. Calling
339 * g_thread_exit (retval);
340 * is equivalent to calling
341 * return retval;
342 * in the function func, as given to g_thread_create().
343 * Note
344 * Never call g_thread_exit() from within a thread of a GThreadPool, as
345 * that will mess up the bookkeeping and lead to funny and unwanted results.
346 * retval:
347 * the return value of this thread.
349 public static void exit(void* retval)
351 // void g_thread_exit (gpointer retval);
352 g_thread_exit(retval);
356 * Call thread_func on all existing GThread structures. Note that
357 * threads may decide to exit while thread_func is running, so
358 * without intimate knowledge about the lifetime of foreign threads,
359 * thread_func shouldn't access the GThread* pointer passed in as
360 * first argument. However, thread_func will not be called for threads
361 * which are known to have exited already.
362 * Due to thread lifetime checks, this function has an execution complexity
363 * which is quadratic in the number of existing threads.
364 * thread_func:
365 * function to call for all GThread structures
366 * user_data:
367 * second argument to thread_func
368 * Since 2.10
370 public static void foreac(GFunc threadFunc, void* userData)
372 // void g_thread_foreach (GFunc thread_func, gpointer user_data);
373 g_thread_foreach(threadFunc, userData);
385 * Initializes mutex. Alternatively you can initialize it with
386 * G_STATIC_MUTEX_INIT.
387 * mutex:
388 * a GStaticMutex to be initialized.
390 public static void gStaticMutexInit(GStaticMutex* mutex)
392 // void g_static_mutex_init (GStaticMutex *mutex);
393 g_static_mutex_init(mutex);
397 * Works like g_mutex_lock(), but for a GStaticMutex.
398 * mutex:
399 * a GStaticMutex.
401 public static void gStaticMutexLock(GStaticMutex* mutex)
403 // void g_static_mutex_lock (GStaticMutex *mutex);
404 g_static_mutex_lock(mutex);
408 * Works like g_mutex_trylock(), but for a GStaticMutex.
409 * mutex:
410 * a GStaticMutex.
411 * Returns:
412 * TRUE, if the GStaticMutex could be locked.
414 public static int gStaticMutexTrylock(GStaticMutex* mutex)
416 // gboolean g_static_mutex_trylock (GStaticMutex *mutex);
417 return g_static_mutex_trylock(mutex);
421 * Works like g_mutex_unlock(), but for a GStaticMutex.
422 * mutex:
423 * a GStaticMutex.
425 public static void gStaticMutexUnlock(GStaticMutex* mutex)
427 // void g_static_mutex_unlock (GStaticMutex *mutex);
428 g_static_mutex_unlock(mutex);
432 * For some operations (like g_cond_wait()) you must have a GMutex
433 * instead of a GStaticMutex. This function will return the
434 * corresponding GMutex for mutex.
435 * mutex:
436 * a GStaticMutex.
437 * Returns:
438 * the GMutex corresponding to mutex.
440 public static Mutex gStaticMutexGetMutex(GStaticMutex* mutex)
442 // GMutex* g_static_mutex_get_mutex (GStaticMutex *mutex);
443 return new Mutex( g_static_mutex_get_mutex(mutex) );
447 * Releases all resources allocated to mutex.
448 * You don't have to call this functions for a GStaticMutex with an
449 * unbounded lifetime, i.e. objects declared 'static', but if you have a
450 * GStaticMutex as a member of a structure and the structure is freed,
451 * you should also free the GStaticMutex.
452 * mutex:
453 * a GStaticMutex to be freed.
455 public static void gStaticMutexFree(GStaticMutex* mutex)
457 // void g_static_mutex_free (GStaticMutex *mutex);
458 g_static_mutex_free(mutex);