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