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