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