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