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