alternative to assert
[gtkD.git] / gtkD / src / gthread / Mutex.d
blob76238740d4c37fd5113fe412e45bad7cc414eea1
1 /*
2 * This file is part of gtkD.
4 * gtkD 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 * gtkD 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 gtkD; 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 = glib-Threads.html
26 * outPack = gthread
27 * outFile = Mutex
28 * strct = GMutex
29 * realStrct=
30 * ctorStrct=
31 * clss = Mutex
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_mutex_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * structWrap:
45 * module aliases:
46 * local aliases:
49 module gthread.Mutex;
51 version(noAssert)
53 version(Tango)
55 import tango.io.Stdout; // use the tango loging?
59 private import gtkc.gthreadtypes;
61 private import gtkc.gthread;
68 /**
69 * Description
70 * Threads act almost like processes, but unlike processes all threads of
71 * one process share the same memory. This is good, as it provides easy
72 * communication between the involved threads via this shared memory, and
73 * it is bad, because strange things (so called "Heisenbugs") might
74 * happen if the program is not carefully designed. In particular, due to
75 * the concurrent nature of threads, no assumptions on the order of
76 * execution of code running in different threads can be made, unless
77 * order is explicitly forced by the programmer through synchronization
78 * primitives.
79 * The aim of the thread related functions in GLib is to provide a
80 * portable means for writing multi-threaded software. There are
81 * primitives for mutexes to protect the access to portions of memory
82 * (GMutex, GStaticMutex, G_LOCK_DEFINE, GStaticRecMutex and
83 * GStaticRWLock). There are primitives for condition variables to allow
84 * synchronization of threads (GCond). There are primitives
85 * for thread-private data - data that every thread has a private instance of
86 * (GPrivate, GStaticPrivate). Last but definitely not least there are
87 * primitives to portably create and manage threads (GThread).
88 * You must call g_thread_init() before executing any other GLib
89 * functions in a threaded GLib program. After that, GLib is completely
90 * thread safe (all global data is automatically locked), but individual
91 * data structure instances are not automatically locked for performance
92 * reasons. So, for example you must coordinate accesses to the same
93 * GHashTable from multiple threads. The two notable exceptions from
94 * this rule are GMainLoop and GAsyncQueue,
95 * which are threadsafe and needs no further
96 * application-level locking to be accessed from multiple threads.
98 public class Mutex
101 /** the main Gtk struct */
102 protected GMutex* gMutex;
105 public GMutex* getMutexStruct()
107 return gMutex;
111 /** the main Gtk struct as a void* */
112 protected void* getStruct()
114 return cast(void*)gMutex;
118 * Sets our main struct and passes it to the parent class
120 public this (GMutex* gMutex)
122 version(noAssert)
124 if ( gMutex is null )
126 int zero = 0;
127 version(Tango)
129 Stdout("struct gMutex is null on constructor").newline;
131 else
133 printf("struct gMutex is null on constructor");
135 zero = zero / zero;
138 else
140 assert(gMutex !is null, "struct gMutex is null on constructor");
142 this.gMutex = gMutex;
169 * Creates a new GMutex.
170 * Note
171 * This function will abort if g_thread_init() has not been called yet.
172 * Returns:
173 * a new GMutex.
175 public this ()
177 // GMutex* g_mutex_new ();
178 this(cast(GMutex*)g_mutex_new() );
182 * Locks mutex. If mutex is already locked by another thread, the
183 * current thread will block until mutex is unlocked by the other
184 * thread.
185 * This function can be used even if g_thread_init() has not yet been
186 * called, and, in that case, will do nothing.
187 * Note
188 * GMutex is neither guaranteed to be recursive nor to be non-recursive,
189 * i.e. a thread could deadlock while calling g_mutex_lock(), if it
190 * already has locked mutex. Use GStaticRecMutex, if you need recursive
191 * mutexes.
192 * mutex:
193 * a GMutex.
195 public void lock()
197 // void g_mutex_lock (GMutex *mutex);
198 g_mutex_lock(gMutex);
202 * Tries to lock mutex. If mutex is already locked by another
203 * thread, it immediately returns FALSE. Otherwise it locks mutex
204 * and returns TRUE.
205 * This function can be used even if g_thread_init() has not yet been
206 * called, and, in that case, will immediately return TRUE.
207 * Note
208 * GMutex is neither guaranteed to be recursive nor to be non-recursive,
209 * i.e. the return value of g_mutex_trylock() could be both FALSE or
210 * TRUE, if the current thread already has locked mutex. Use
211 * GStaticRecMutex, if you need recursive mutexes.
212 * mutex:
213 * a GMutex.
214 * Returns:
215 * TRUE, if mutex could be locked.
217 public int trylock()
219 // gboolean g_mutex_trylock (GMutex *mutex);
220 return g_mutex_trylock(gMutex);
224 * Unlocks mutex. If another thread is blocked in a g_mutex_lock() call
225 * for mutex, it will be woken and can lock mutex itself.
226 * This function can be used even if g_thread_init() has not yet been
227 * called, and, in that case, will do nothing.
228 * mutex:
229 * a GMutex.
231 public void unlock()
233 // void g_mutex_unlock (GMutex *mutex);
234 g_mutex_unlock(gMutex);
238 * Destroys mutex.
239 * mutex:
240 * a GMutex.
242 public void free()
244 // void g_mutex_free (GMutex *mutex);
245 g_mutex_free(gMutex);