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 = Mutex 29 * strct = GMutex 30 * realStrct= 31 * ctorStrct= 32 * clss = Mutex 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_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.Mutex; 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 * Description 66 * Threads act almost like processes, but unlike processes all threads 67 * of one process share the same memory. This is good, as it provides 68 * easy communication between the involved threads via this shared 69 * memory, and it is bad, because strange things (so called 70 * "Heisenbugs") might happen if the program is not carefully designed. 71 * In particular, due to the concurrent nature of threads, no 72 * assumptions on the order of execution of code running in different 73 * threads can be made, unless order is explicitly forced by the 74 * programmer through synchronization primitives. 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, GStaticMutex, G_LOCK_DEFINE, GStaticRecMutex and 79 * GStaticRWLock). There are primitives for condition variables to 80 * allow synchronization of threads (GCond). There are primitives for 81 * thread-private data - data that every thread has a private instance 82 * of (GPrivate, GStaticPrivate). Last but definitely not least there 83 * are primitives to portably create and manage threads (GThread). 84 * The threading system is initialized with g_thread_init(), which 85 * takes an optional custom thread implementation or NULL for the 86 * default implementation. If you want to call g_thread_init() with a 87 * non-NULL argument this must be done before executing any other GLib 88 * functions (except g_mem_set_vtable()). This is a requirement even if 89 * no threads are in fact ever created by the process. 90 * Calling g_thread_init() with a NULL argument is somewhat more 91 * relaxed. You may call any other glib functions in the main thread 92 * before g_thread_init() as long as g_thread_init() is not called from 93 * a glib callback, or with any locks held. However, many libraries 94 * above glib does not support late initialization of threads, so doing 95 * this should be avoided if possible. 96 * Please note that since version 2.24 the GObject initialization 97 * function g_type_init() initializes threads (with a NULL argument), 98 * so most applications, including those using Gtk+ will run with 99 * threads enabled. If you want a special thread implementation, make 100 * sure you call g_thread_init() before g_type_init() is called. 101 * After calling g_thread_init(), GLib is completely thread safe (all 102 * global data is automatically locked), but individual data structure 103 * instances are not automatically locked for performance reasons. So, 104 * for example you must coordinate accesses to the same GHashTable 105 * from multiple threads. The two notable exceptions from this rule 106 * are GMainLoop and GAsyncQueue, which are 107 * threadsafe and need no further application-level locking to be 108 * accessed from multiple threads. 109 * To help debugging problems in multithreaded applications, GLib 110 * supports error-checking mutexes that will give you helpful error 111 * messages on common problems. To use error-checking mutexes, define 112 * the symbol G_ERRORCHECK_MUTEXES when compiling the application. 113 */ 114 public class Mutex 115 { 116 117 /** the main Gtk struct */ 118 protected GMutex* gMutex; 119 120 121 public GMutex* getMutexStruct() 122 { 123 return gMutex; 124 } 125 126 127 /** the main Gtk struct as a void* */ 128 protected void* getStruct() 129 { 130 return cast(void*)gMutex; 131 } 132 133 /** 134 * Sets our main struct and passes it to the parent class 135 */ 136 public this (GMutex* gMutex) 137 { 138 this.gMutex = gMutex; 139 } 140 141 /** 142 */ 143 144 /** 145 * Creates a new GMutex. 146 * Note 147 * This function will abort if g_thread_init() has not been 148 * called yet. 149 * Throws: ConstructionException GTK+ fails to create the object. 150 */ 151 public this () 152 { 153 // GMutex * g_mutex_new (); 154 auto p = g_mutex_new(); 155 if(p is null) 156 { 157 throw new ConstructionException("null returned by g_mutex_new()"); 158 } 159 this(cast(GMutex*) p); 160 } 161 162 /** 163 * Locks mutex. If mutex is already locked by another thread, the 164 * current thread will block until mutex is unlocked by the other 165 * thread. 166 * This function can be used even if g_thread_init() has not yet been 167 * called, and, in that case, will do nothing. 168 * Note 169 * GMutex is neither guaranteed to be recursive nor to be 170 * non-recursive, i.e. a thread could deadlock while calling 171 * g_mutex_lock(), if it already has locked mutex. Use 172 * GStaticRecMutex, if you need recursive mutexes. 173 */ 174 public void lock() 175 { 176 // void g_mutex_lock (GMutex *mutex); 177 g_mutex_lock(gMutex); 178 } 179 180 /** 181 * Tries to lock mutex. If mutex is already locked by another thread, 182 * it immediately returns FALSE. Otherwise it locks mutex and returns 183 * TRUE. 184 * This function can be used even if g_thread_init() has not yet been 185 * called, and, in that case, will immediately return TRUE. 186 * Note 187 * GMutex is neither guaranteed to be recursive nor to be 188 * non-recursive, i.e. the return value of g_mutex_trylock() could be 189 * both FALSE or TRUE, if the current thread already has locked 190 * mutex. Use GStaticRecMutex, if you need recursive 191 * mutexes. 192 * Returns: TRUE, if mutex could be locked. 193 */ 194 public int trylock() 195 { 196 // gboolean g_mutex_trylock (GMutex *mutex); 197 return g_mutex_trylock(gMutex); 198 } 199 200 /** 201 * Unlocks mutex. If another thread is blocked in a g_mutex_lock() 202 * call for mutex, it will be woken and can lock mutex itself. 203 * This function can be used even if g_thread_init() has not yet been 204 * called, and, in that case, will do nothing. 205 */ 206 public void unlock() 207 { 208 // void g_mutex_unlock (GMutex *mutex); 209 g_mutex_unlock(gMutex); 210 } 211 212 /** 213 * Destroys mutex. 214 * Note 215 * Calling g_mutex_free() on a locked mutex may result in 216 * undefined behaviour. 217 */ 218 public void free() 219 { 220 // void g_mutex_free (GMutex *mutex); 221 g_mutex_free(gMutex); 222 } 223 }