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.ThreadPool; 26 27 private import glib.ConstructionException; 28 private import glib.ErrorG; 29 private import glib.GException; 30 private import glib.c.functions; 31 public import glib.c.types; 32 public import gtkc.glibtypes; 33 private import gtkd.Loader; 34 35 36 /** 37 * The #GThreadPool struct represents a thread pool. It has three 38 * public read-only members, but the underlying struct is bigger, 39 * so you must not copy this struct. 40 */ 41 public final class ThreadPool 42 { 43 /** the main Gtk struct */ 44 protected GThreadPool* gThreadPool; 45 protected bool ownedRef; 46 47 /** Get the main Gtk struct */ 48 public GThreadPool* getThreadPoolStruct(bool transferOwnership = false) 49 { 50 if (transferOwnership) 51 ownedRef = false; 52 return gThreadPool; 53 } 54 55 /** the main Gtk struct as a void* */ 56 protected void* getStruct() 57 { 58 return cast(void*)gThreadPool; 59 } 60 61 /** 62 * Sets our main struct and passes it to the parent class. 63 */ 64 public this (GThreadPool* gThreadPool, bool ownedRef = false) 65 { 66 this.gThreadPool = gThreadPool; 67 this.ownedRef = ownedRef; 68 } 69 70 ~this () 71 { 72 if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef ) 73 g_free(gThreadPool); 74 } 75 76 77 /** 78 * the function to execute in the threads of this pool 79 */ 80 public @property GFunc func() 81 { 82 return gThreadPool.func; 83 } 84 85 /** Ditto */ 86 public @property void func(GFunc value) 87 { 88 gThreadPool.func = value; 89 } 90 91 /** 92 * the user data for the threads of this pool 93 */ 94 public @property void* userData() 95 { 96 return gThreadPool.userData; 97 } 98 99 /** Ditto */ 100 public @property void userData(void* value) 101 { 102 gThreadPool.userData = value; 103 } 104 105 /** 106 * are all threads exclusive to this pool 107 */ 108 public @property bool exclusive() 109 { 110 return gThreadPool.exclusive != 0; 111 } 112 113 /** Ditto */ 114 public @property void exclusive(bool value) 115 { 116 gThreadPool.exclusive = value; 117 } 118 119 /** 120 * Frees all resources allocated for @pool. 121 * 122 * If @immediate is %TRUE, no new task is processed for @pool. 123 * Otherwise @pool is not freed before the last task is processed. 124 * Note however, that no thread of this pool is interrupted while 125 * processing a task. Instead at least all still running threads 126 * can finish their tasks before the @pool is freed. 127 * 128 * If @wait_ is %TRUE, the functions does not return before all 129 * tasks to be processed (dependent on @immediate, whether all 130 * or only the currently running) are ready. 131 * Otherwise the function returns immediately. 132 * 133 * After calling this function @pool must not be used anymore. 134 * 135 * Params: 136 * immediate = should @pool shut down immediately? 137 * wait = should the function wait for all tasks to be finished? 138 */ 139 public void free(bool immediate, bool wait) 140 { 141 g_thread_pool_free(gThreadPool, immediate, wait); 142 } 143 144 /** 145 * Returns the maximal number of threads for @pool. 146 * 147 * Returns: the maximal number of threads 148 */ 149 public int getMaxThreads() 150 { 151 return g_thread_pool_get_max_threads(gThreadPool); 152 } 153 154 /** 155 * Returns the number of threads currently running in @pool. 156 * 157 * Returns: the number of threads currently running 158 */ 159 public uint getNumThreads() 160 { 161 return g_thread_pool_get_num_threads(gThreadPool); 162 } 163 164 /** 165 * Moves the item to the front of the queue of unprocessed 166 * items, so that it will be processed next. 167 * 168 * Params: 169 * data = an unprocessed item in the pool 170 * 171 * Returns: %TRUE if the item was found and moved 172 * 173 * Since: 2.46 174 */ 175 public bool moveToFront(void* data) 176 { 177 return g_thread_pool_move_to_front(gThreadPool, data) != 0; 178 } 179 180 /** 181 * Inserts @data into the list of tasks to be executed by @pool. 182 * 183 * When the number of currently running threads is lower than the 184 * maximal allowed number of threads, a new thread is started (or 185 * reused) with the properties given to g_thread_pool_new(). 186 * Otherwise, @data stays in the queue until a thread in this pool 187 * finishes its previous task and processes @data. 188 * 189 * @error can be %NULL to ignore errors, or non-%NULL to report 190 * errors. An error can only occur when a new thread couldn't be 191 * created. In that case @data is simply appended to the queue of 192 * work to do. 193 * 194 * Before version 2.32, this function did not return a success status. 195 * 196 * Params: 197 * data = a new task for @pool 198 * 199 * Returns: %TRUE on success, %FALSE if an error occurred 200 * 201 * Throws: GException on failure. 202 */ 203 public bool push(void* data) 204 { 205 GError* err = null; 206 207 auto p = g_thread_pool_push(gThreadPool, data, &err) != 0; 208 209 if (err !is null) 210 { 211 throw new GException( new ErrorG(err) ); 212 } 213 214 return p; 215 } 216 217 /** 218 * Sets the maximal allowed number of threads for @pool. 219 * A value of -1 means that the maximal number of threads 220 * is unlimited. If @pool is an exclusive thread pool, setting 221 * the maximal number of threads to -1 is not allowed. 222 * 223 * Setting @max_threads to 0 means stopping all work for @pool. 224 * It is effectively frozen until @max_threads is set to a non-zero 225 * value again. 226 * 227 * A thread is never terminated while calling @func, as supplied by 228 * g_thread_pool_new(). Instead the maximal number of threads only 229 * has effect for the allocation of new threads in g_thread_pool_push(). 230 * A new thread is allocated, whenever the number of currently 231 * running threads in @pool is smaller than the maximal number. 232 * 233 * @error can be %NULL to ignore errors, or non-%NULL to report 234 * errors. An error can only occur when a new thread couldn't be 235 * created. 236 * 237 * Before version 2.32, this function did not return a success status. 238 * 239 * Params: 240 * maxThreads = a new maximal number of threads for @pool, 241 * or -1 for unlimited 242 * 243 * Returns: %TRUE on success, %FALSE if an error occurred 244 * 245 * Throws: GException on failure. 246 */ 247 public bool setMaxThreads(int maxThreads) 248 { 249 GError* err = null; 250 251 auto p = g_thread_pool_set_max_threads(gThreadPool, maxThreads, &err) != 0; 252 253 if (err !is null) 254 { 255 throw new GException( new ErrorG(err) ); 256 } 257 258 return p; 259 } 260 261 /** 262 * Sets the function used to sort the list of tasks. This allows the 263 * tasks to be processed by a priority determined by @func, and not 264 * just in the order in which they were added to the pool. 265 * 266 * Note, if the maximum number of threads is more than 1, the order 267 * that threads are executed cannot be guaranteed 100%. Threads are 268 * scheduled by the operating system and are executed at random. It 269 * cannot be assumed that threads are executed in the order they are 270 * created. 271 * 272 * Params: 273 * func = the #GCompareDataFunc used to sort the list of tasks. 274 * This function is passed two tasks. It should return 275 * 0 if the order in which they are handled does not matter, 276 * a negative value if the first task should be processed before 277 * the second or a positive value if the second task should be 278 * processed first. 279 * userData = user data passed to @func 280 * 281 * Since: 2.10 282 */ 283 public void setSortFunction(GCompareDataFunc func, void* userData) 284 { 285 g_thread_pool_set_sort_function(gThreadPool, func, userData); 286 } 287 288 /** 289 * Returns the number of tasks still unprocessed in @pool. 290 * 291 * Returns: the number of unprocessed tasks 292 */ 293 public uint unprocessed() 294 { 295 return g_thread_pool_unprocessed(gThreadPool); 296 } 297 298 /** 299 * This function will return the maximum @interval that a 300 * thread will wait in the thread pool for new tasks before 301 * being stopped. 302 * 303 * If this function returns 0, threads waiting in the thread 304 * pool for new work are not stopped. 305 * 306 * Returns: the maximum @interval (milliseconds) to wait 307 * for new tasks in the thread pool before stopping the 308 * thread 309 * 310 * Since: 2.10 311 */ 312 public static uint getMaxIdleTime() 313 { 314 return g_thread_pool_get_max_idle_time(); 315 } 316 317 /** 318 * Returns the maximal allowed number of unused threads. 319 * 320 * Returns: the maximal number of unused threads 321 */ 322 public static int getMaxUnusedThreads() 323 { 324 return g_thread_pool_get_max_unused_threads(); 325 } 326 327 /** 328 * Returns the number of currently unused threads. 329 * 330 * Returns: the number of currently unused threads 331 */ 332 public static uint getNumUnusedThreads() 333 { 334 return g_thread_pool_get_num_unused_threads(); 335 } 336 337 /** 338 * This function creates a new thread pool. 339 * 340 * Whenever you call g_thread_pool_push(), either a new thread is 341 * created or an unused one is reused. At most @max_threads threads 342 * are running concurrently for this thread pool. @max_threads = -1 343 * allows unlimited threads to be created for this thread pool. The 344 * newly created or reused thread now executes the function @func 345 * with the two arguments. The first one is the parameter to 346 * g_thread_pool_push() and the second one is @user_data. 347 * 348 * The parameter @exclusive determines whether the thread pool owns 349 * all threads exclusive or shares them with other thread pools. 350 * If @exclusive is %TRUE, @max_threads threads are started 351 * immediately and they will run exclusively for this thread pool 352 * until it is destroyed by g_thread_pool_free(). If @exclusive is 353 * %FALSE, threads are created when needed and shared between all 354 * non-exclusive thread pools. This implies that @max_threads may 355 * not be -1 for exclusive thread pools. Besides, exclusive thread 356 * pools are not affected by g_thread_pool_set_max_idle_time() 357 * since their threads are never considered idle and returned to the 358 * global pool. 359 * 360 * @error can be %NULL to ignore errors, or non-%NULL to report 361 * errors. An error can only occur when @exclusive is set to %TRUE 362 * and not all @max_threads threads could be created. 363 * See #GThreadError for possible errors that may occur. 364 * Note, even in case of error a valid #GThreadPool is returned. 365 * 366 * Params: 367 * func = a function to execute in the threads of the new thread pool 368 * userData = user data that is handed over to @func every time it 369 * is called 370 * maxThreads = the maximal number of threads to execute concurrently 371 * in the new thread pool, -1 means no limit 372 * exclusive = should this thread pool be exclusive? 373 * 374 * Returns: the new #GThreadPool 375 * 376 * Throws: GException on failure. 377 * Throws: ConstructionException GTK+ fails to create the object. 378 */ 379 public this(GFunc func, void* userData, int maxThreads, bool exclusive) 380 { 381 GError* err = null; 382 383 auto p = g_thread_pool_new(func, userData, maxThreads, exclusive, &err); 384 385 if (err !is null) 386 { 387 throw new GException( new ErrorG(err) ); 388 } 389 390 if(p is null) 391 { 392 throw new ConstructionException("null returned by new"); 393 } 394 395 this(cast(GThreadPool*) p); 396 } 397 398 /** 399 * This function will set the maximum @interval that a thread 400 * waiting in the pool for new tasks can be idle for before 401 * being stopped. This function is similar to calling 402 * g_thread_pool_stop_unused_threads() on a regular timeout, 403 * except this is done on a per thread basis. 404 * 405 * By setting @interval to 0, idle threads will not be stopped. 406 * 407 * The default value is 15000 (15 seconds). 408 * 409 * Params: 410 * interval = the maximum @interval (in milliseconds) 411 * a thread can be idle 412 * 413 * Since: 2.10 414 */ 415 public static void setMaxIdleTime(uint interval) 416 { 417 g_thread_pool_set_max_idle_time(interval); 418 } 419 420 /** 421 * Sets the maximal number of unused threads to @max_threads. 422 * If @max_threads is -1, no limit is imposed on the number 423 * of unused threads. 424 * 425 * The default value is 2. 426 * 427 * Params: 428 * maxThreads = maximal number of unused threads 429 */ 430 public static void setMaxUnusedThreads(int maxThreads) 431 { 432 g_thread_pool_set_max_unused_threads(maxThreads); 433 } 434 435 /** 436 * Stops all currently unused threads. This does not change the 437 * maximal number of unused threads. This function can be used to 438 * regularly stop all unused threads e.g. from g_timeout_add(). 439 */ 440 public static void stopUnusedThreads() 441 { 442 g_thread_pool_stop_unused_threads(); 443 } 444 }