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 gtkc.glib;
28 public  import gtkc.glibtypes;
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 
82 	/** Get the main Gtk struct */
83 	public GMutex* getMutexStruct()
84 	{
85 		return gMutex;
86 	}
87 
88 	/** the main Gtk struct as a void* */
89 	protected void* getStruct()
90 	{
91 		return cast(void*)gMutex;
92 	}
93 
94 	/**
95 	 * Sets our main struct and passes it to the parent class.
96 	 */
97 	public this (GMutex* gMutex)
98 	{
99 		this.gMutex = gMutex;
100 	}
101 
102 
103 	/**
104 	 * Frees the resources allocated to a mutex with g_mutex_init().
105 	 *
106 	 * This function should not be used with a #GMutex that has been
107 	 * statically allocated.
108 	 *
109 	 * Calling g_mutex_clear() on a locked mutex leads to undefined
110 	 * behaviour.
111 	 *
112 	 * Sine: 2.32
113 	 */
114 	public void clear()
115 	{
116 		g_mutex_clear(gMutex);
117 	}
118 
119 	/**
120 	 * Initializes a #GMutex so that it can be used.
121 	 *
122 	 * This function is useful to initialize a mutex that has been
123 	 * allocated on the stack, or as part of a larger structure.
124 	 * It is not necessary to initialize a mutex that has been
125 	 * statically allocated.
126 	 *
127 	 * |[<!-- language="C" -->
128 	 * typedef struct {
129 	 * GMutex m;
130 	 * ...
131 	 * } Blob;
132 	 *
133 	 * Blob *b;
134 	 *
135 	 * b = g_new (Blob, 1);
136 	 * g_mutex_init (&b->m);
137 	 * ]|
138 	 *
139 	 * To undo the effect of g_mutex_init() when a mutex is no longer
140 	 * needed, use g_mutex_clear().
141 	 *
142 	 * Calling g_mutex_init() on an already initialized #GMutex leads
143 	 * to undefined behaviour.
144 	 *
145 	 * Since: 2.32
146 	 */
147 	public void init()
148 	{
149 		g_mutex_init(gMutex);
150 	}
151 
152 	/**
153 	 * Locks @mutex. If @mutex is already locked by another thread, the
154 	 * current thread will block until @mutex is unlocked by the other
155 	 * thread.
156 	 *
157 	 * #GMutex is neither guaranteed to be recursive nor to be
158 	 * non-recursive.  As such, calling g_mutex_lock() on a #GMutex that has
159 	 * already been locked by the same thread results in undefined behaviour
160 	 * (including but not limited to deadlocks).
161 	 */
162 	public void lock()
163 	{
164 		g_mutex_lock(gMutex);
165 	}
166 
167 	/**
168 	 * Tries to lock @mutex. If @mutex is already locked by another thread,
169 	 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
170 	 * %TRUE.
171 	 *
172 	 * #GMutex is neither guaranteed to be recursive nor to be
173 	 * non-recursive.  As such, calling g_mutex_lock() on a #GMutex that has
174 	 * already been locked by the same thread results in undefined behaviour
175 	 * (including but not limited to deadlocks or arbitrary return values).
176 	 *
177 	 * Return: %TRUE if @mutex could be locked
178 	 */
179 	public bool trylock()
180 	{
181 		return g_mutex_trylock(gMutex) != 0;
182 	}
183 
184 	/**
185 	 * Unlocks @mutex. If another thread is blocked in a g_mutex_lock()
186 	 * call for @mutex, it will become unblocked and can lock @mutex itself.
187 	 *
188 	 * Calling g_mutex_unlock() on a mutex that is not locked by the
189 	 * current thread leads to undefined behaviour.
190 	 */
191 	public void unlock()
192 	{
193 		g_mutex_unlock(gMutex);
194 	}
195 }