alternative to assert
[gtkD.git] / gtkD / src / gthread / RWLock.d
blob3eaf816508a086ebb0593483625455ed18494ae2
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 =
26 * outPack = gthread
27 * outFile = RWLock
28 * strct = GStaticRWLock
29 * realStrct=
30 * ctorStrct=
31 * clss = RWLock
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_static_rw_lock_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * structWrap:
45 * module aliases:
46 * local aliases:
49 module gthread.RWLock;
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 RWLock
101 /** the main Gtk struct */
102 protected GStaticRWLock* gStaticRWLock;
105 public GStaticRWLock* getRWLockStruct()
107 return gStaticRWLock;
111 /** the main Gtk struct as a void* */
112 protected void* getStruct()
114 return cast(void*)gStaticRWLock;
118 * Sets our main struct and passes it to the parent class
120 public this (GStaticRWLock* gStaticRWLock)
122 version(noAssert)
124 if ( gStaticRWLock is null )
126 int zero = 0;
127 version(Tango)
129 Stdout("struct gStaticRWLock is null on constructor").newline;
131 else
133 printf("struct gStaticRWLock is null on constructor");
135 zero = zero / zero;
138 else
140 assert(gStaticRWLock !is null, "struct gStaticRWLock is null on constructor");
142 this.gStaticRWLock = gStaticRWLock;
199 * A GStaticRWLock must be initialized with this function before it can
200 * be used. Alternatively you can initialize it with
201 * G_STATIC_RW_LOCK_INIT.
202 * lock:
203 * a GStaticRWLock to be initialized.
205 public void init()
207 // void g_static_rw_lock_init (GStaticRWLock *lock);
208 g_static_rw_lock_init(gStaticRWLock);
212 * Locks lock for reading. There may be unlimited concurrent locks for
213 * reading of a GStaticRWLock at the same time. If lock is already
214 * locked for writing by another thread or if another thread is already
215 * waiting to lock lock for writing, this function will block until
216 * lock is unlocked by the other writing thread and no other writing
217 * threads want to lock lock. This lock has to be unlocked by
218 * g_static_rw_lock_reader_unlock().
219 * GStaticRWLock is not recursive. It might seem to be possible to
220 * recursively lock for reading, but that can result in a deadlock, due
221 * to writer preference.
222 * lock:
223 * a GStaticRWLock to lock for reading.
225 public void readerLock()
227 // void g_static_rw_lock_reader_lock (GStaticRWLock *lock);
228 g_static_rw_lock_reader_lock(gStaticRWLock);
232 * Tries to lock lock for reading. If lock is already locked for
233 * writing by another thread or if another thread is already waiting to
234 * lock lock for writing, immediately returns FALSE. Otherwise locks
235 * lock for reading and returns TRUE. This lock has to be unlocked by
236 * g_static_rw_lock_reader_unlock().
237 * lock:
238 * a GStaticRWLock to lock for reading.
239 * Returns:
240 * TRUE, if lock could be locked for reading.
242 public int readerTrylock()
244 // gboolean g_static_rw_lock_reader_trylock (GStaticRWLock *lock);
245 return g_static_rw_lock_reader_trylock(gStaticRWLock);
249 * Unlocks lock. If a thread waits to lock lock for writing and all
250 * locks for reading have been unlocked, the waiting thread is woken up
251 * and can lock lock for writing.
252 * lock:
253 * a GStaticRWLock to unlock after reading.
255 public void readerUnlock()
257 // void g_static_rw_lock_reader_unlock (GStaticRWLock *lock);
258 g_static_rw_lock_reader_unlock(gStaticRWLock);
262 * Locks lock for writing. If lock is already locked for writing or
263 * reading by other threads, this function will block until lock is
264 * completely unlocked and then lock lock for writing. While this
265 * functions waits to lock lock, no other thread can lock lock for
266 * reading. When lock is locked for writing, no other thread can lock
267 * lock (neither for reading nor writing). This lock has to be unlocked
268 * by g_static_rw_lock_writer_unlock().
269 * lock:
270 * a GStaticRWLock to lock for writing.
272 public void writerLock()
274 // void g_static_rw_lock_writer_lock (GStaticRWLock *lock);
275 g_static_rw_lock_writer_lock(gStaticRWLock);
279 * Tries to lock lock for writing. If lock is already locked (for
280 * either reading or writing) by another thread, it immediately returns
281 * FALSE. Otherwise it locks lock for writing and returns TRUE. This
282 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
283 * lock:
284 * a GStaticRWLock to lock for writing.
285 * Returns:
286 * TRUE, if lock could be locked for writing.
288 public int writerTrylock()
290 // gboolean g_static_rw_lock_writer_trylock (GStaticRWLock *lock);
291 return g_static_rw_lock_writer_trylock(gStaticRWLock);
295 * Unlocks lock. If a thread is waiting to lock lock for writing and
296 * all locks for reading have been unlocked, the waiting thread is woken
297 * up and can lock lock for writing. If no thread is waiting to lock
298 * lock for writing, and some thread or threads are waiting to lock lock
299 * for reading, the waiting threads are woken up and can lock lock for
300 * reading.
301 * lock:
302 * a GStaticRWLock to unlock after writing.
304 public void writerUnlock()
306 // void g_static_rw_lock_writer_unlock (GStaticRWLock *lock);
307 g_static_rw_lock_writer_unlock(gStaticRWLock);
311 * Releases all resources allocated to lock.
312 * You don't have to call this functions for a GStaticRWLock with an
313 * unbounded lifetime, i.e. objects declared 'static', but if you have a
314 * GStaticRWLock as a member of a structure, and the structure is freed,
315 * you should also free the GStaticRWLock.
316 * lock:
317 * a GStaticRWLock to be freed.
319 public void free()
321 // void g_static_rw_lock_free (GStaticRWLock *lock);
322 g_static_rw_lock_free(gStaticRWLock);