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.Thread;
26 
27 private import glib.ConstructionException;
28 private import glib.ErrorG;
29 private import glib.GException;
30 private import glib.Str;
31 private import gtkc.glib;
32 public  import gtkc.glibtypes;
33 
34 
35 /**
36  * The #GThread struct represents a running thread. This struct
37  * is returned by g_thread_new() or g_thread_try_new(). You can
38  * obtain the #GThread struct representing the current thread by
39  * calling g_thread_self().
40  * 
41  * GThread is refcounted, see g_thread_ref() and g_thread_unref().
42  * The thread represented by it holds a reference while it is running,
43  * and g_thread_join() consumes the reference that it is given, so
44  * it is normally not necessary to manage GThread references
45  * explicitly.
46  * 
47  * The structure is opaque -- none of its fields may be directly
48  * accessed.
49  */
50 public class Thread
51 {
52 	/** the main Gtk struct */
53 	protected GThread* gThread;
54 	protected bool ownedRef;
55 
56 	/** Get the main Gtk struct */
57 	public GThread* getThreadStruct()
58 	{
59 		return gThread;
60 	}
61 
62 	/** the main Gtk struct as a void* */
63 	protected void* getStruct()
64 	{
65 		return cast(void*)gThread;
66 	}
67 
68 	/**
69 	 * Sets our main struct and passes it to the parent class.
70 	 */
71 	public this (GThread* gThread, bool ownedRef = false)
72 	{
73 		this.gThread = gThread;
74 		this.ownedRef = ownedRef;
75 	}
76 
77 
78 	/**
79 	 * This function is the same as g_thread_new() except that
80 	 * it allows for the possibility of failure.
81 	 *
82 	 * If a thread can not be created (due to resource limits),
83 	 * @error is set and %NULL is returned.
84 	 *
85 	 * Params:
86 	 *     name = an (optional) name for the new thread
87 	 *     func = a function to execute in the new thread
88 	 *     data = an argument to supply to the new thread
89 	 *
90 	 * Return: the new #GThread, or %NULL if an error occurred
91 	 *
92 	 * Since: 2.32
93 	 *
94 	 * Throws: GException on failure.
95 	 * Throws: ConstructionException GTK+ fails to create the object.
96 	 */
97 	public this(string name, GThreadFunc func, void* data)
98 	{
99 		GError* err = null;
100 		
101 		auto p = g_thread_try_new(Str.toStringz(name), func, data, &err);
102 		
103 		if (err !is null)
104 		{
105 			throw new GException( new ErrorG(err) );
106 		}
107 		
108 		if(p is null)
109 		{
110 			throw new ConstructionException("null returned by try_new");
111 		}
112 		
113 		this(cast(GThread*) p);
114 	}
115 
116 	/**
117 	 * Waits until @thread finishes, i.e. the function @func, as
118 	 * given to g_thread_new(), returns or g_thread_exit() is called.
119 	 * If @thread has already terminated, then g_thread_join()
120 	 * returns immediately.
121 	 *
122 	 * Any thread can wait for any other thread by calling g_thread_join(),
123 	 * not just its 'creator'. Calling g_thread_join() from multiple threads
124 	 * for the same @thread leads to undefined behaviour.
125 	 *
126 	 * The value returned by @func or given to g_thread_exit() is
127 	 * returned by this function.
128 	 *
129 	 * g_thread_join() consumes the reference to the passed-in @thread.
130 	 * This will usually cause the #GThread struct and associated resources
131 	 * to be freed. Use g_thread_ref() to obtain an extra reference if you
132 	 * want to keep the GThread alive beyond the g_thread_join() call.
133 	 *
134 	 * Return: the return value of the thread
135 	 */
136 	public void* join()
137 	{
138 		return g_thread_join(gThread);
139 	}
140 
141 	/**
142 	 * Increase the reference count on @thread.
143 	 *
144 	 * Return: a new reference to @thread
145 	 *
146 	 * Since: 2.32
147 	 */
148 	public Thread doref()
149 	{
150 		auto p = g_thread_ref(gThread);
151 		
152 		if(p is null)
153 		{
154 			return null;
155 		}
156 		
157 		return new Thread(cast(GThread*) p, true);
158 	}
159 
160 	/**
161 	 * Decrease the reference count on @thread, possibly freeing all
162 	 * resources associated with it.
163 	 *
164 	 * Note that each thread holds a reference to its #GThread while
165 	 * it is running, so it is safe to drop your own reference to it
166 	 * if you don't need it anymore.
167 	 *
168 	 * Since: 2.32
169 	 */
170 	public void unref()
171 	{
172 		g_thread_unref(gThread);
173 	}
174 
175 	/** */
176 	public static GQuark errorQuark()
177 	{
178 		return g_thread_error_quark();
179 	}
180 
181 	/**
182 	 * Terminates the current thread.
183 	 *
184 	 * If another thread is waiting for us using g_thread_join() then the
185 	 * waiting thread will be woken up and get @retval as the return value
186 	 * of g_thread_join().
187 	 *
188 	 * Calling g_thread_exit() with a parameter @retval is equivalent to
189 	 * returning @retval from the function @func, as given to g_thread_new().
190 	 *
191 	 * You must only call g_thread_exit() from a thread that you created
192 	 * yourself with g_thread_new() or related APIs. You must not call
193 	 * this function from a thread created with another threading library
194 	 * or or from within a #GThreadPool.
195 	 *
196 	 * Params:
197 	 *     retval = the return value of this thread
198 	 */
199 	public static void exit(void* retval)
200 	{
201 		g_thread_exit(retval);
202 	}
203 
204 	/**
205 	 * This function returns the #GThread corresponding to the
206 	 * current thread. Note that this function does not increase
207 	 * the reference count of the returned struct.
208 	 *
209 	 * This function will return a #GThread even for threads that
210 	 * were not created by GLib (i.e. those created by other threading
211 	 * APIs). This may be useful for thread identification purposes
212 	 * (i.e. comparisons) but you must not use GLib functions (such
213 	 * as g_thread_join()) on these threads.
214 	 *
215 	 * Return: the #GThread representing the current thread
216 	 */
217 	public static Thread self()
218 	{
219 		auto p = g_thread_self();
220 		
221 		if(p is null)
222 		{
223 			return null;
224 		}
225 		
226 		return new Thread(cast(GThread*) p, true);
227 	}
228 
229 	/**
230 	 * Causes the calling thread to voluntarily relinquish the CPU, so
231 	 * that other threads can run.
232 	 *
233 	 * This function is often used as a method to make busy wait less evil.
234 	 */
235 	public static void yield()
236 	{
237 		g_thread_yield();
238 	}
239 
240 	/**
241 	 * Sets the indicated @lock_bit in @address.  If the bit is already
242 	 * set, this call will block until g_bit_unlock() unsets the
243 	 * corresponding bit.
244 	 *
245 	 * Attempting to lock on two different bits within the same integer is
246 	 * not supported and will very probably cause deadlocks.
247 	 *
248 	 * The value of the bit that is set is (1u << @bit).  If @bit is not
249 	 * between 0 and 31 then the result is undefined.
250 	 *
251 	 * This function accesses @address atomically.  All other accesses to
252 	 * @address must be atomic in order for this function to work
253 	 * reliably.
254 	 *
255 	 * Params:
256 	 *     address = a pointer to an integer
257 	 *     lockBit = a bit value between 0 and 31
258 	 *
259 	 * Since: 2.24
260 	 */
261 	public static void bitLock(int* address, int lockBit)
262 	{
263 		g_bit_lock(address, lockBit);
264 	}
265 
266 	/**
267 	 * Sets the indicated @lock_bit in @address, returning %TRUE if
268 	 * successful.  If the bit is already set, returns %FALSE immediately.
269 	 *
270 	 * Attempting to lock on two different bits within the same integer is
271 	 * not supported.
272 	 *
273 	 * The value of the bit that is set is (1u << @bit).  If @bit is not
274 	 * between 0 and 31 then the result is undefined.
275 	 *
276 	 * This function accesses @address atomically.  All other accesses to
277 	 * @address must be atomic in order for this function to work
278 	 * reliably.
279 	 *
280 	 * Params:
281 	 *     address = a pointer to an integer
282 	 *     lockBit = a bit value between 0 and 31
283 	 *
284 	 * Return: %TRUE if the lock was acquired
285 	 *
286 	 * Since: 2.24
287 	 */
288 	public static bool bitTrylock(int* address, int lockBit)
289 	{
290 		return g_bit_trylock(address, lockBit) != 0;
291 	}
292 
293 	/**
294 	 * Clears the indicated @lock_bit in @address.  If another thread is
295 	 * currently blocked in g_bit_lock() on this same bit then it will be
296 	 * woken up.
297 	 *
298 	 * This function accesses @address atomically.  All other accesses to
299 	 * @address must be atomic in order for this function to work
300 	 * reliably.
301 	 *
302 	 * Params:
303 	 *     address = a pointer to an integer
304 	 *     lockBit = a bit value between 0 and 31
305 	 *
306 	 * Since: 2.24
307 	 */
308 	public static void bitUnlock(int* address, int lockBit)
309 	{
310 		g_bit_unlock(address, lockBit);
311 	}
312 
313 	/**
314 	 * Determine the approximate number of threads that the system will
315 	 * schedule simultaneously for this process.  This is intended to be
316 	 * used as a parameter to g_thread_pool_new() for CPU bound tasks and
317 	 * similar cases.
318 	 *
319 	 * Return: Number of schedulable threads, always greater than 0
320 	 *
321 	 * Since: 2.36
322 	 */
323 	public static uint getNumProcessors()
324 	{
325 		return g_get_num_processors();
326 	}
327 
328 	/**
329 	 * This is equivalent to g_bit_lock, but working on pointers (or other
330 	 * pointer-sized values).
331 	 *
332 	 * For portability reasons, you may only lock on the bottom 32 bits of
333 	 * the pointer.
334 	 *
335 	 * Params:
336 	 *     address = a pointer to a #gpointer-sized value
337 	 *     lockBit = a bit value between 0 and 31
338 	 *
339 	 * Since: 2.30
340 	 */
341 	public static void pointerBitLock(void* address, int lockBit)
342 	{
343 		g_pointer_bit_lock(address, lockBit);
344 	}
345 
346 	/**
347 	 * This is equivalent to g_bit_trylock, but working on pointers (or
348 	 * other pointer-sized values).
349 	 *
350 	 * For portability reasons, you may only lock on the bottom 32 bits of
351 	 * the pointer.
352 	 *
353 	 * Params:
354 	 *     address = a pointer to a #gpointer-sized value
355 	 *     lockBit = a bit value between 0 and 31
356 	 *
357 	 * Return: %TRUE if the lock was acquired
358 	 *
359 	 * Since: 2.30
360 	 */
361 	public static bool pointerBitTrylock(void* address, int lockBit)
362 	{
363 		return g_pointer_bit_trylock(address, lockBit) != 0;
364 	}
365 
366 	/**
367 	 * This is equivalent to g_bit_unlock, but working on pointers (or other
368 	 * pointer-sized values).
369 	 *
370 	 * For portability reasons, you may only lock on the bottom 32 bits of
371 	 * the pointer.
372 	 *
373 	 * Params:
374 	 *     address = a pointer to a #gpointer-sized value
375 	 *     lockBit = a bit value between 0 and 31
376 	 *
377 	 * Since: 2.30
378 	 */
379 	public static void pointerBitUnlock(void* address, int lockBit)
380 	{
381 		g_pointer_bit_unlock(address, lockBit);
382 	}
383 }