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 module glib.Mutex;
26 
27 private import glib.c.functions;
28 public  import glib.c.types;
29 
30 
31 /**
32  * The #GMutex struct is an opaque data structure to represent a mutex
33  * (mutual exclusion). It can be used to protect data against shared
34  * access.
35  * 
36  * Take for example the following function:
37  * |[<!-- language="C" -->
38  * int
39  * give_me_next_number (void)
40  * {
41  * static int current_number = 0;
42  * 
43  * // now do a very complicated calculation to calculate the new
44  * // number, this might for example be a random number generator
45  * current_number = calc_next_number (current_number);
46  * 
47  * return current_number;
48  * }
49  * ]|
50  * It is easy to see that this won't work in a multi-threaded
51  * application. There current_number must be protected against shared
52  * access. A #GMutex can be used as a solution to this problem:
53  * |[<!-- language="C" -->
54  * int
55  * give_me_next_number (void)
56  * {
57  * static GMutex mutex;
58  * static int current_number = 0;
59  * int ret_val;
60  * 
61  * g_mutex_lock (&mutex);
62  * ret_val = current_number = calc_next_number (current_number);
63  * g_mutex_unlock (&mutex);
64  * 
65  * return ret_val;
66  * }
67  * ]|
68  * Notice that the #GMutex is not initialised to any particular value.
69  * Its placement in static storage ensures that it will be initialised
70  * to all-zeros, which is appropriate.
71  * 
72  * If a #GMutex is placed in other contexts (eg: embedded in a struct)
73  * then it must be explicitly initialised using g_mutex_init().
74  * 
75  * A #GMutex should only be accessed via g_mutex_ functions.
76  */
77 public class Mutex
78 {
79 	/** the main Gtk struct */
80 	protected GMutex* gMutex;
81 	protected bool ownedRef;
82 
83 	/** Get the main Gtk struct */
84 	public GMutex* getMutexStruct(bool transferOwnership = false)
85 	{
86 		if (transferOwnership)
87 			ownedRef = false;
88 		return gMutex;
89 	}
90 
91 	/** the main Gtk struct as a void* */
92 	protected void* getStruct()
93 	{
94 		return cast(void*)gMutex;
95 	}
96 
97 	/**
98 	 * Sets our main struct and passes it to the parent class.
99 	 */
100 	public this (GMutex* gMutex, bool ownedRef = false)
101 	{
102 		this.gMutex = gMutex;
103 		this.ownedRef = ownedRef;
104 	}
105 
106 
107 	/**
108 	 * Frees the resources allocated to a mutex with g_mutex_init().
109 	 *
110 	 * This function should not be used with a #GMutex that has been
111 	 * statically allocated.
112 	 *
113 	 * Calling g_mutex_clear() on a locked mutex leads to undefined
114 	 * behaviour.
115 	 *
116 	 * Sine: 2.32
117 	 */
118 	public void clear()
119 	{
120 		g_mutex_clear(gMutex);
121 	}
122 
123 	/**
124 	 * Initializes a #GMutex so that it can be used.
125 	 *
126 	 * This function is useful to initialize a mutex that has been
127 	 * allocated on the stack, or as part of a larger structure.
128 	 * It is not necessary to initialize a mutex that has been
129 	 * statically allocated.
130 	 *
131 	 * |[<!-- language="C" -->
132 	 * typedef struct {
133 	 * GMutex m;
134 	 * ...
135 	 * } Blob;
136 	 *
137 	 * Blob *b;
138 	 *
139 	 * b = g_new (Blob, 1);
140 	 * g_mutex_init (&b->m);
141 	 * ]|
142 	 *
143 	 * To undo the effect of g_mutex_init() when a mutex is no longer
144 	 * needed, use g_mutex_clear().
145 	 *
146 	 * Calling g_mutex_init() on an already initialized #GMutex leads
147 	 * to undefined behaviour.
148 	 *
149 	 * Since: 2.32
150 	 */
151 	public void init()
152 	{
153 		g_mutex_init(gMutex);
154 	}
155 
156 	/**
157 	 * Locks @mutex. If @mutex is already locked by another thread, the
158 	 * current thread will block until @mutex is unlocked by the other
159 	 * thread.
160 	 *
161 	 * #GMutex is neither guaranteed to be recursive nor to be
162 	 * non-recursive.  As such, calling g_mutex_lock() on a #GMutex that has
163 	 * already been locked by the same thread results in undefined behaviour
164 	 * (including but not limited to deadlocks).
165 	 */
166 	public void lock()
167 	{
168 		g_mutex_lock(gMutex);
169 	}
170 
171 	/**
172 	 * Tries to lock @mutex. If @mutex is already locked by another thread,
173 	 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
174 	 * %TRUE.
175 	 *
176 	 * #GMutex is neither guaranteed to be recursive nor to be
177 	 * non-recursive.  As such, calling g_mutex_lock() on a #GMutex that has
178 	 * already been locked by the same thread results in undefined behaviour
179 	 * (including but not limited to deadlocks or arbitrary return values).
180 	 *
181 	 * Returns: %TRUE if @mutex could be locked
182 	 */
183 	public bool trylock()
184 	{
185 		return g_mutex_trylock(gMutex) != 0;
186 	}
187 
188 	/**
189 	 * Unlocks @mutex. If another thread is blocked in a g_mutex_lock()
190 	 * call for @mutex, it will become unblocked and can lock @mutex itself.
191 	 *
192 	 * Calling g_mutex_unlock() on a mutex that is not locked by the
193 	 * current thread leads to undefined behaviour.
194 	 */
195 	public void unlock()
196 	{
197 		g_mutex_unlock(gMutex);
198 	}
199 }