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 = 27 * outPack = gthread 28 * outFile = RecMutex 29 * strct = GRecMutex 30 * realStrct= 31 * ctorStrct= 32 * clss = RecMutex 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_rec_mutex_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * structWrap: 47 * module aliases: 48 * local aliases: 49 * overrides: 50 */ 51 52 module gthread.RecMutex; 53 54 public import gtkc.gthreadtypes; 55 56 private import gtkc.gthread; 57 private import glib.ConstructionException; 58 59 60 61 62 63 64 /** 65 * Threads act almost like processes, but unlike processes all threads 66 * of one process share the same memory. This is good, as it provides 67 * easy communication between the involved threads via this shared 68 * memory, and it is bad, because strange things (so called 69 * "Heisenbugs") might happen if the program is not carefully designed. 70 * In particular, due to the concurrent nature of threads, no 71 * assumptions on the order of execution of code running in different 72 * threads can be made, unless order is explicitly forced by the 73 * programmer through synchronization primitives. 74 * 75 * The aim of the thread-related functions in GLib is to provide a 76 * portable means for writing multi-threaded software. There are 77 * primitives for mutexes to protect the access to portions of memory 78 * (GMutex, GRecMutex and GRWLock). There is a facility to use 79 * individual bits for locks (g_bit_lock()). There are primitives 80 * for condition variables to allow synchronization of threads (GCond). 81 * There are primitives for thread-private data - data that every 82 * thread has a private instance of (GPrivate). There are facilities 83 * for one-time initialization (GOnce, g_once_init_enter()). Finally, 84 * there are primitives to create and manage threads (GThread). 85 * 86 * The GLib threading system used to be initialized with g_thread_init(). 87 * This is no longer necessary. Since version 2.32, the GLib threading 88 * system is automatically initialized at the start of your program, 89 * and all thread-creation functions and synchronization primitives 90 * are available right away. 91 * 92 * Note that it is not safe to assume that your program has no threads 93 * even if you don't call g_thread_new() yourself. GLib and GIO can 94 * and will create threads for their own purposes in some cases, such 95 * as when using g_unix_signal_source_new() or when using GDBus. 96 * 97 * Originally, UNIX did not have threads, and therefore some traditional 98 * UNIX APIs are problematic in threaded programs. Some notable examples 99 * are 100 * 101 * C library functions that return data in statically allocated 102 * buffers, such as strtok() or strerror(). For many of these, 103 * there are thread-safe variants with a _r suffix, or you can 104 * look at corresponding GLib APIs (like g_strsplit() or g_strerror()). 105 * 106 * setenv() and unsetenv() manipulate the process environment in 107 * a not thread-safe way, and may interfere with getenv() calls 108 * in other threads. Note that getenv() calls may be 109 * “hidden” behind other APIs. For example, GNU gettext() 110 * calls getenv() under the covers. In general, it is best to treat 111 * the environment as readonly. If you absolutely have to modify the 112 * environment, do it early in main(), when no other threads are around yet. 113 * 114 * setlocale() changes the locale for the entire process, affecting 115 * all threads. Temporary changes to the locale are often made to 116 * change the behavior of string scanning or formatting functions 117 * like scanf() or printf(). GLib offers a number of string APIs 118 * (like g_ascii_formatd() or g_ascii_strtod()) that can often be 119 * used as an alternative. Or you can use the uselocale() function 120 * to change the locale only for the current thread. 121 * 122 * fork() only takes the calling thread into the child's copy of the 123 * process image. If other threads were executing in critical 124 * sections they could have left mutexes locked which could easily 125 * cause deadlocks in the new child. For this reason, you should 126 * call exit() or exec() as soon as possible in the child and only 127 * make signal-safe library calls before that. 128 * 129 * daemon() uses fork() in a way contrary to what is described 130 * above. It should not be used with GLib programs. 131 * 132 * GLib itself is internally completely thread-safe (all global data is 133 * automatically locked), but individual data structure instances are 134 * not automatically locked for performance reasons. For example, 135 * you must coordinate accesses to the same GHashTable from multiple 136 * threads. The two notable exceptions from this rule are GMainLoop 137 * and GAsyncQueue, which are thread-safe and 138 * need no further application-level locking to be accessed from 139 * multiple threads. Most refcounting functions such as g_object_ref() 140 * are also thread-safe. 141 */ 142 public class RecMutex 143 { 144 145 /** the main Gtk struct */ 146 protected GRecMutex* gRecMutex; 147 148 149 public GRecMutex* getRecMutexStruct() 150 { 151 return gRecMutex; 152 } 153 154 155 /** the main Gtk struct as a void* */ 156 protected void* getStruct() 157 { 158 return cast(void*)gRecMutex; 159 } 160 161 /** 162 * Sets our main struct and passes it to the parent class 163 */ 164 public this (GRecMutex* gRecMutex) 165 { 166 this.gRecMutex = gRecMutex; 167 } 168 169 /** 170 */ 171 172 /** 173 * Initializes a GRecMutex so that it can be used. 174 * This function is useful to initialize a recursive mutex 175 * that has been allocated on the stack, or as part of a larger 176 * structure. 177 * It is not necessary to initialise a recursive mutex that has been 178 * statically allocated. 179 * $(DDOC_COMMENT example) 180 * Calling g_rec_mutex_init() on an already initialized GRecMutex 181 * leads to undefined behaviour. 182 * To undo the effect of g_rec_mutex_init() when a recursive mutex 183 * is no longer needed, use g_rec_mutex_clear(). 184 * Since 2.32 185 */ 186 public void init() 187 { 188 // void g_rec_mutex_init (GRecMutex *rec_mutex); 189 g_rec_mutex_init(gRecMutex); 190 } 191 192 /** 193 * Frees the resources allocated to a recursive mutex with 194 * g_rec_mutex_init(). 195 * This function should not be used with a GRecMutex that has been 196 * statically allocated. 197 * Calling g_rec_mutex_clear() on a locked recursive mutex leads 198 * to undefined behaviour. 199 * Sine: 2.32 200 */ 201 public void clear() 202 { 203 // void g_rec_mutex_clear (GRecMutex *rec_mutex); 204 g_rec_mutex_clear(gRecMutex); 205 } 206 207 /** 208 * Locks rec_mutex. If rec_mutex is already locked by another 209 * thread, the current thread will block until rec_mutex is 210 * unlocked by the other thread. If rec_mutex is already locked 211 * by the current thread, the 'lock count' of rec_mutex is increased. 212 * The mutex will only become available again when it is unlocked 213 * as many times as it has been locked. 214 * Since 2.32 215 */ 216 public void lock() 217 { 218 // void g_rec_mutex_lock (GRecMutex *rec_mutex); 219 g_rec_mutex_lock(gRecMutex); 220 } 221 222 /** 223 * Tries to lock rec_mutex. If rec_mutex is already locked 224 * by another thread, it immediately returns FALSE. Otherwise 225 * it locks rec_mutex and returns TRUE. 226 * Since 2.32 227 * Returns: TRUE if rec_mutex could be locked 228 */ 229 public int trylock() 230 { 231 // gboolean g_rec_mutex_trylock (GRecMutex *rec_mutex); 232 return g_rec_mutex_trylock(gRecMutex); 233 } 234 235 /** 236 * Unlocks rec_mutex. If another thread is blocked in a 237 * g_rec_mutex_lock() call for rec_mutex, it will become unblocked 238 * and can lock rec_mutex itself. 239 * Calling g_rec_mutex_unlock() on a recursive mutex that is not 240 * locked by the current thread leads to undefined behaviour. 241 * Since 2.32 242 */ 243 public void unlock() 244 { 245 // void g_rec_mutex_unlock (GRecMutex *rec_mutex); 246 g_rec_mutex_unlock(gRecMutex); 247 } 248 }