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