1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = glib-Threads.html
27  * outPack = gthread
28  * outFile = Thread
29  * strct   = GThread
30  * realStrct=
31  * ctorStrct=
32  * clss    = Thread
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_thread_
41  * 	- g_
42  * omit structs:
43  * omit prefixes:
44  * 	- g_mutex_
45  * 	- g_rec_mutex_
46  * 	- g_rw_lock_
47  * 	- g_cond_
48  * 	- g_private_
49  * 	- g_once_
50  * omit code:
51  * omit signals:
52  * imports:
53  * 	- glib.Str
54  * 	- glib.ErrorG
55  * 	- glib.GException
56  * structWrap:
57  * 	- GThread* -> Thread
58  * module aliases:
59  * local aliases:
60  * overrides:
61  */
62 
63 module gthread.Thread;
64 
65 public  import gtkc.gthreadtypes;
66 
67 private import gtkc.gthread;
68 private import glib.ConstructionException;
69 
70 
71 private import glib.Str;
72 private import glib.ErrorG;
73 private import glib.GException;
74 
75 
76 
77 
78 /**
79  * Threads act almost like processes, but unlike processes all threads
80  * of one process share the same memory. This is good, as it provides
81  * easy communication between the involved threads via this shared
82  * memory, and it is bad, because strange things (so called
83  * "Heisenbugs") might happen if the program is not carefully designed.
84  * In particular, due to the concurrent nature of threads, no
85  * assumptions on the order of execution of code running in different
86  * threads can be made, unless order is explicitly forced by the
87  * programmer through synchronization primitives.
88  *
89  * The aim of the thread-related functions in GLib is to provide a
90  * portable means for writing multi-threaded software. There are
91  * primitives for mutexes to protect the access to portions of memory
92  * (GMutex, GRecMutex and GRWLock). There is a facility to use
93  * individual bits for locks (g_bit_lock()). There are primitives
94  * for condition variables to allow synchronization of threads (GCond).
95  * There are primitives for thread-private data - data that every
96  * thread has a private instance of (GPrivate). There are facilities
97  * for one-time initialization (GOnce, g_once_init_enter()). Finally,
98  * there are primitives to create and manage threads (GThread).
99  *
100  * The GLib threading system used to be initialized with g_thread_init().
101  * This is no longer necessary. Since version 2.32, the GLib threading
102  * system is automatically initialized at the start of your program,
103  * and all thread-creation functions and synchronization primitives
104  * are available right away.
105  *
106  * Note that it is not safe to assume that your program has no threads
107  * even if you don't call g_thread_new() yourself. GLib and GIO can
108  * and will create threads for their own purposes in some cases, such
109  * as when using g_unix_signal_source_new() or when using GDBus.
110  *
111  * Originally, UNIX did not have threads, and therefore some traditional
112  * UNIX APIs are problematic in threaded programs. Some notable examples
113  * are
114  *
115  *  C library functions that return data in statically allocated
116  *  buffers, such as strtok() or strerror(). For many of these,
117  *  there are thread-safe variants with a _r suffix, or you can
118  *  look at corresponding GLib APIs (like g_strsplit() or g_strerror()).
119  *
120  * setenv() and unsetenv() manipulate the process environment in
121  *  a not thread-safe way, and may interfere with getenv() calls
122  *  in other threads. Note that getenv() calls may be
123  *  “hidden” behind other APIs. For example, GNU gettext()
124  *  calls getenv() under the covers. In general, it is best to treat
125  *  the environment as readonly. If you absolutely have to modify the
126  *  environment, do it early in main(), when no other threads are around yet.
127  *
128  * setlocale() changes the locale for the entire process, affecting
129  *  all threads. Temporary changes to the locale are often made to
130  *  change the behavior of string scanning or formatting functions
131  *  like scanf() or printf(). GLib offers a number of string APIs
132  *  (like g_ascii_formatd() or g_ascii_strtod()) that can often be
133  *  used as an alternative. Or you can use the uselocale() function
134  *  to change the locale only for the current thread.
135  *
136  * fork() only takes the calling thread into the child's copy of the
137  *  process image. If other threads were executing in critical
138  *  sections they could have left mutexes locked which could easily
139  *  cause deadlocks in the new child. For this reason, you should
140  *  call exit() or exec() as soon as possible in the child and only
141  *  make signal-safe library calls before that.
142  *
143  * daemon() uses fork() in a way contrary to what is described
144  *  above. It should not be used with GLib programs.
145  *
146  * GLib itself is internally completely thread-safe (all global data is
147  * automatically locked), but individual data structure instances are
148  * not automatically locked for performance reasons. For example,
149  * you must coordinate accesses to the same GHashTable from multiple
150  * threads. The two notable exceptions from this rule are GMainLoop
151  * and GAsyncQueue, which are thread-safe and
152  * need no further application-level locking to be accessed from
153  * multiple threads. Most refcounting functions such as g_object_ref()
154  * are also thread-safe.
155  */
156 public class Thread
157 {
158 	
159 	/** the main Gtk struct */
160 	protected GThread* gThread;
161 	
162 	
163 	public GThread* getThreadStruct()
164 	{
165 		return gThread;
166 	}
167 	
168 	
169 	/** the main Gtk struct as a void* */
170 	protected void* getStruct()
171 	{
172 		return cast(void*)gThread;
173 	}
174 	
175 	/**
176 	 * Sets our main struct and passes it to the parent class
177 	 */
178 	public this (GThread* gThread)
179 	{
180 		this.gThread = gThread;
181 	}
182 	
183 	/**
184 	 */
185 	
186 	/**
187 	 * This function creates a new thread. The new thread starts by invoking
188 	 * func with the argument data. The thread will run until func returns
189 	 * or until g_thread_exit() is called from the new thread. The return value
190 	 * of func becomes the return value of the thread, which can be obtained
191 	 * with g_thread_join().
192 	 * The name can be useful for discriminating threads in a debugger.
193 	 * It is not used for other purposes and does not have to be unique.
194 	 * Some systems restrict the length of name to 16 bytes.
195 	 * If the thread can not be created the program aborts. See
196 	 * g_thread_try_new() if you want to attempt to deal with failures.
197 	 * To free the struct returned by this function, use g_thread_unref().
198 	 * Note that g_thread_join() implicitly unrefs the GThread as well.
199 	 * Since 2.32
200 	 * Params:
201 	 * name = an (optional) name for the new thread. [allow-none]
202 	 * func = a function to execute in the new thread
203 	 * data = an argument to supply to the new thread
204 	 * Throws: ConstructionException GTK+ fails to create the object.
205 	 */
206 	public this (string name, GThreadFunc func, void* data)
207 	{
208 		// GThread * g_thread_new (const gchar *name,  GThreadFunc func,  gpointer data);
209 		auto p = g_thread_new(Str.toStringz(name), func, data);
210 		if(p is null)
211 		{
212 			throw new ConstructionException("null returned by g_thread_new(Str.toStringz(name), func, data)");
213 		}
214 		this(cast(GThread*) p);
215 	}
216 	
217 	/**
218 	 * This function is the same as g_thread_new() except that
219 	 * it allows for the possibility of failure.
220 	 * If a thread can not be created (due to resource limits),
221 	 * error is set and NULL is returned.
222 	 * Since 2.32
223 	 * Params:
224 	 * name = an (optional) name for the new thread. [allow-none]
225 	 * func = a function to execute in the new thread
226 	 * data = an argument to supply to the new thread
227 	 * Returns: the new GThread, or NULL if an error occurred
228 	 * Throws: GException on failure.
229 	 */
230 	public static Thread tryNew(string name, GThreadFunc func, void* data)
231 	{
232 		// GThread * g_thread_try_new (const gchar *name,  GThreadFunc func,  gpointer data,  GError **error);
233 		GError* err = null;
234 		
235 		auto p = g_thread_try_new(Str.toStringz(name), func, data, &err);
236 		
237 		if (err !is null)
238 		{
239 			throw new GException( new ErrorG(err) );
240 		}
241 		
242 		
243 		if(p is null)
244 		{
245 			return null;
246 		}
247 		
248 		return new Thread(cast(GThread*) p);
249 	}
250 	
251 	/**
252 	 * Increase the reference count on thread.
253 	 * Since 2.32
254 	 * Returns: a new reference to thread
255 	 */
256 	public Thread doref()
257 	{
258 		// GThread * g_thread_ref (GThread *thread);
259 		auto p = g_thread_ref(gThread);
260 		
261 		if(p is null)
262 		{
263 			return null;
264 		}
265 		
266 		return new Thread(cast(GThread*) p);
267 	}
268 	
269 	/**
270 	 * Decrease the reference count on thread, possibly freeing all
271 	 * resources associated with it.
272 	 * Note that each thread holds a reference to its GThread while
273 	 * it is running, so it is safe to drop your own reference to it
274 	 * if you don't need it anymore.
275 	 * Since 2.32
276 	 */
277 	public void unref()
278 	{
279 		// void g_thread_unref (GThread *thread);
280 		g_thread_unref(gThread);
281 	}
282 	
283 	/**
284 	 * Waits until thread finishes, i.e. the function func, as
285 	 * given to g_thread_new(), returns or g_thread_exit() is called.
286 	 * If thread has already terminated, then g_thread_join()
287 	 * returns immediately.
288 	 * Any thread can wait for any other thread by calling g_thread_join(),
289 	 * not just its 'creator'. Calling g_thread_join() from multiple threads
290 	 * for the same thread leads to undefined behaviour.
291 	 * The value returned by func or given to g_thread_exit() is
292 	 * returned by this function.
293 	 * g_thread_join() consumes the reference to the passed-in thread.
294 	 * This will usually cause the GThread struct and associated resources
295 	 * to be freed. Use g_thread_ref() to obtain an extra reference if you
296 	 * want to keep the GThread alive beyond the g_thread_join() call.
297 	 * Returns: the return value of the thread
298 	 */
299 	public void* join()
300 	{
301 		// gpointer g_thread_join (GThread *thread);
302 		return g_thread_join(gThread);
303 	}
304 	
305 	/**
306 	 * Causes the calling thread to voluntarily relinquish the CPU, so
307 	 * that other threads can run.
308 	 * This function is often used as a method to make busy wait less evil.
309 	 */
310 	public static void yield()
311 	{
312 		// void g_thread_yield ();
313 		g_thread_yield();
314 	}
315 	
316 	/**
317 	 * Terminates the current thread.
318 	 * If another thread is waiting for us using g_thread_join() then the
319 	 * waiting thread will be woken up and get retval as the return value
320 	 * of g_thread_join().
321 	 * Calling g_thread_exit (retval) is equivalent to
322 	 * returning retval from the function func, as given to g_thread_new().
323 	 * Note
324 	 *  You must only call g_thread_exit() from a thread that you created
325 	 *  yourself with g_thread_new() or related APIs. You must not call
326 	 *  this function from a thread created with another threading library
327 	 *  or or from within a GThreadPool.
328 	 * Params:
329 	 * retval = the return value of this thread
330 	 */
331 	public static void exit(void* retval)
332 	{
333 		// void g_thread_exit (gpointer retval);
334 		g_thread_exit(retval);
335 	}
336 	
337 	/**
338 	 * This functions returns the GThread corresponding to the
339 	 * current thread. Note that this function does not increase
340 	 * the reference count of the returned struct.
341 	 * This function will return a GThread even for threads that
342 	 * were not created by GLib (i.e. those created by other threading
343 	 * APIs). This may be useful for thread identification purposes
344 	 * (i.e. comparisons) but you must not use GLib functions (such
345 	 * as g_thread_join()) on these threads.
346 	 * Returns: the GThread representing the current thread
347 	 */
348 	public static Thread self()
349 	{
350 		// GThread * g_thread_self (void);
351 		auto p = g_thread_self();
352 		
353 		if(p is null)
354 		{
355 			return null;
356 		}
357 		
358 		return new Thread(cast(GThread*) p);
359 	}
360 	
361 	/**
362 	 * Sets the indicated lock_bit in address. If the bit is already
363 	 * set, this call will block until g_bit_unlock() unsets the
364 	 * corresponding bit.
365 	 * Attempting to lock on two different bits within the same integer is
366 	 * not supported and will very probably cause deadlocks.
367 	 * The value of the bit that is set is (1u << bit). If bit is not
368 	 * between 0 and 31 then the result is undefined.
369 	 * This function accesses address atomically. All other accesses to
370 	 * address must be atomic in order for this function to work
371 	 * reliably.
372 	 * Since 2.24
373 	 * Params:
374 	 * address = a pointer to an integer
375 	 * lockBit = a bit value between 0 and 31
376 	 */
377 	public static void bitLock(ref int address, int lockBit)
378 	{
379 		// void g_bit_lock (volatile gint *address,  gint lock_bit);
380 		g_bit_lock(&address, lockBit);
381 	}
382 	
383 	/**
384 	 * Sets the indicated lock_bit in address, returning TRUE if
385 	 * successful. If the bit is already set, returns FALSE immediately.
386 	 * Attempting to lock on two different bits within the same integer is
387 	 * not supported.
388 	 * The value of the bit that is set is (1u << bit). If bit is not
389 	 * between 0 and 31 then the result is undefined.
390 	 * This function accesses address atomically. All other accesses to
391 	 * address must be atomic in order for this function to work
392 	 * reliably.
393 	 * Since 2.24
394 	 * Params:
395 	 * address = a pointer to an integer
396 	 * lockBit = a bit value between 0 and 31
397 	 * Returns: TRUE if the lock was acquired
398 	 */
399 	public static int bitTrylock(ref int address, int lockBit)
400 	{
401 		// gboolean g_bit_trylock (volatile gint *address,  gint lock_bit);
402 		return g_bit_trylock(&address, lockBit);
403 	}
404 	
405 	/**
406 	 * Clears the indicated lock_bit in address. If another thread is
407 	 * currently blocked in g_bit_lock() on this same bit then it will be
408 	 * woken up.
409 	 * This function accesses address atomically. All other accesses to
410 	 * address must be atomic in order for this function to work
411 	 * reliably.
412 	 * Since 2.24
413 	 * Params:
414 	 * address = a pointer to an integer
415 	 * lockBit = a bit value between 0 and 31
416 	 */
417 	public static void bitUnlock(ref int address, int lockBit)
418 	{
419 		// void g_bit_unlock (volatile gint *address,  gint lock_bit);
420 		g_bit_unlock(&address, lockBit);
421 	}
422 	
423 	/**
424 	 * This is equivalent to g_bit_lock, but working on pointers (or other
425 	 * pointer-sized values).
426 	 * For portability reasons, you may only lock on the bottom 32 bits of
427 	 * the pointer.
428 	 * Since 2.30
429 	 * Params:
430 	 * address = a pointer to a gpointer-sized value
431 	 * lockBit = a bit value between 0 and 31
432 	 */
433 	public static void pointerBitLock(void* address, int lockBit)
434 	{
435 		// void g_pointer_bit_lock (volatile void *address,  gint lock_bit);
436 		g_pointer_bit_lock(address, lockBit);
437 	}
438 	
439 	/**
440 	 * This is equivalent to g_bit_trylock, but working on pointers (or
441 	 * other pointer-sized values).
442 	 * For portability reasons, you may only lock on the bottom 32 bits of
443 	 * the pointer.
444 	 * Since 2.30
445 	 * Params:
446 	 * address = a pointer to a gpointer-sized value
447 	 * lockBit = a bit value between 0 and 31
448 	 * Returns: TRUE if the lock was acquired
449 	 */
450 	public static int pointerBitTrylock(void* address, int lockBit)
451 	{
452 		// gboolean g_pointer_bit_trylock (volatile void *address,  gint lock_bit);
453 		return g_pointer_bit_trylock(address, lockBit);
454 	}
455 	
456 	/**
457 	 * This is equivalent to g_bit_unlock, but working on pointers (or other
458 	 * pointer-sized values).
459 	 * For portability reasons, you may only lock on the bottom 32 bits of
460 	 * the pointer.
461 	 * Since 2.30
462 	 * Params:
463 	 * address = a pointer to a gpointer-sized value
464 	 * lockBit = a bit value between 0 and 31
465 	 */
466 	public static void pointerBitUnlock(void* address, int lockBit)
467 	{
468 		// void g_pointer_bit_unlock (volatile void *address,  gint lock_bit);
469 		g_pointer_bit_unlock(address, lockBit);
470 	}
471 	
472 	/**
473 	 * Determine the approximate number of threads that the system will
474 	 * schedule simultaneously for this process. This is intended to be
475 	 * used as a parameter to g_thread_pool_new() for CPU bound tasks and
476 	 * similar cases.
477 	 * Since 2.36
478 	 * Returns: Number of schedulable threads, always greater than 0
479 	 */
480 	public static uint getNumProcessors()
481 	{
482 		// guint g_get_num_processors (void);
483 		return g_get_num_processors();
484 	}
485 }