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 55 /** Get the main Gtk struct */ 56 public GThread* getThreadStruct() 57 { 58 return gThread; 59 } 60 61 /** the main Gtk struct as a void* */ 62 protected void* getStruct() 63 { 64 return cast(void*)gThread; 65 } 66 67 /** 68 * Sets our main struct and passes it to the parent class. 69 */ 70 public this (GThread* gThread) 71 { 72 this.gThread = gThread; 73 } 74 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); 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 public static GQuark errorQuark() 176 { 177 return g_thread_error_quark(); 178 } 179 180 /** 181 * Terminates the current thread. 182 * 183 * If another thread is waiting for us using g_thread_join() then the 184 * waiting thread will be woken up and get @retval as the return value 185 * of g_thread_join(). 186 * 187 * Calling g_thread_exit() with a parameter @retval is equivalent to 188 * returning @retval from the function @func, as given to g_thread_new(). 189 * 190 * You must only call g_thread_exit() from a thread that you created 191 * yourself with g_thread_new() or related APIs. You must not call 192 * this function from a thread created with another threading library 193 * or or from within a #GThreadPool. 194 * 195 * Params: 196 * retval = the return value of this thread 197 */ 198 public static void exit(void* retval) 199 { 200 g_thread_exit(retval); 201 } 202 203 /** 204 * This function returns the #GThread corresponding to the 205 * current thread. Note that this function does not increase 206 * the reference count of the returned struct. 207 * 208 * This function will return a #GThread even for threads that 209 * were not created by GLib (i.e. those created by other threading 210 * APIs). This may be useful for thread identification purposes 211 * (i.e. comparisons) but you must not use GLib functions (such 212 * as g_thread_join()) on these threads. 213 * 214 * Return: the #GThread representing the current thread 215 */ 216 public static Thread self() 217 { 218 auto p = g_thread_self(); 219 220 if(p is null) 221 { 222 return null; 223 } 224 225 return new Thread(cast(GThread*) p); 226 } 227 228 /** 229 * Causes the calling thread to voluntarily relinquish the CPU, so 230 * that other threads can run. 231 * 232 * This function is often used as a method to make busy wait less evil. 233 */ 234 public static void yield() 235 { 236 g_thread_yield(); 237 } 238 239 /** 240 * Sets the indicated @lock_bit in @address. If the bit is already 241 * set, this call will block until g_bit_unlock() unsets the 242 * corresponding bit. 243 * 244 * Attempting to lock on two different bits within the same integer is 245 * not supported and will very probably cause deadlocks. 246 * 247 * The value of the bit that is set is (1u << @bit). If @bit is not 248 * between 0 and 31 then the result is undefined. 249 * 250 * This function accesses @address atomically. All other accesses to 251 * @address must be atomic in order for this function to work 252 * reliably. 253 * 254 * Params: 255 * address = a pointer to an integer 256 * lockBit = a bit value between 0 and 31 257 * 258 * Since: 2.24 259 */ 260 public static void bitLock(int* address, int lockBit) 261 { 262 g_bit_lock(address, lockBit); 263 } 264 265 /** 266 * Sets the indicated @lock_bit in @address, returning %TRUE if 267 * successful. If the bit is already set, returns %FALSE immediately. 268 * 269 * Attempting to lock on two different bits within the same integer is 270 * not supported. 271 * 272 * The value of the bit that is set is (1u << @bit). If @bit is not 273 * between 0 and 31 then the result is undefined. 274 * 275 * This function accesses @address atomically. All other accesses to 276 * @address must be atomic in order for this function to work 277 * reliably. 278 * 279 * Params: 280 * address = a pointer to an integer 281 * lockBit = a bit value between 0 and 31 282 * 283 * Return: %TRUE if the lock was acquired 284 * 285 * Since: 2.24 286 */ 287 public static bool bitTrylock(int* address, int lockBit) 288 { 289 return g_bit_trylock(address, lockBit) != 0; 290 } 291 292 /** 293 * Clears the indicated @lock_bit in @address. If another thread is 294 * currently blocked in g_bit_lock() on this same bit then it will be 295 * woken up. 296 * 297 * This function accesses @address atomically. All other accesses to 298 * @address must be atomic in order for this function to work 299 * reliably. 300 * 301 * Params: 302 * address = a pointer to an integer 303 * lockBit = a bit value between 0 and 31 304 * 305 * Since: 2.24 306 */ 307 public static void bitUnlock(int* address, int lockBit) 308 { 309 g_bit_unlock(address, lockBit); 310 } 311 312 /** 313 * Determine the approximate number of threads that the system will 314 * schedule simultaneously for this process. This is intended to be 315 * used as a parameter to g_thread_pool_new() for CPU bound tasks and 316 * similar cases. 317 * 318 * Return: Number of schedulable threads, always greater than 0 319 * 320 * Since: 2.36 321 */ 322 public static uint getNumProcessors() 323 { 324 return g_get_num_processors(); 325 } 326 327 /** 328 * This is equivalent to g_bit_lock, but working on pointers (or other 329 * pointer-sized values). 330 * 331 * For portability reasons, you may only lock on the bottom 32 bits of 332 * the pointer. 333 * 334 * Params: 335 * address = a pointer to a #gpointer-sized value 336 * lockBit = a bit value between 0 and 31 337 * 338 * Since: 2.30 339 */ 340 public static void pointerBitLock(void* address, int lockBit) 341 { 342 g_pointer_bit_lock(address, lockBit); 343 } 344 345 /** 346 * This is equivalent to g_bit_trylock, but working on pointers (or 347 * other pointer-sized values). 348 * 349 * For portability reasons, you may only lock on the bottom 32 bits of 350 * the pointer. 351 * 352 * Params: 353 * address = a pointer to a #gpointer-sized value 354 * lockBit = a bit value between 0 and 31 355 * 356 * Return: %TRUE if the lock was acquired 357 * 358 * Since: 2.30 359 */ 360 public static bool pointerBitTrylock(void* address, int lockBit) 361 { 362 return g_pointer_bit_trylock(address, lockBit) != 0; 363 } 364 365 /** 366 * This is equivalent to g_bit_unlock, but working on pointers (or other 367 * pointer-sized values). 368 * 369 * For portability reasons, you may only lock on the bottom 32 bits of 370 * the pointer. 371 * 372 * Params: 373 * address = a pointer to a #gpointer-sized value 374 * lockBit = a bit value between 0 and 31 375 * 376 * Since: 2.30 377 */ 378 public static void pointerBitUnlock(void* address, int lockBit) 379 { 380 g_pointer_bit_unlock(address, lockBit); 381 } 382 }