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.MemorySlice;
31 private import glib.c.functions;
32 public  import glib.c.types;
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 			sliceFree(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, this function 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 this 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 	 * Pass g_get_num_processors() to @max_threads to create as many threads as
349 	 * there are logical processors on the system. This will not pin each thread to
350 	 * a specific processor.
351 	 *
352 	 * The parameter @exclusive determines whether the thread pool owns
353 	 * all threads exclusive or shares them with other thread pools.
354 	 * If @exclusive is %TRUE, @max_threads threads are started
355 	 * immediately and they will run exclusively for this thread pool
356 	 * until it is destroyed by g_thread_pool_free(). If @exclusive is
357 	 * %FALSE, threads are created when needed and shared between all
358 	 * non-exclusive thread pools. This implies that @max_threads may
359 	 * not be -1 for exclusive thread pools. Besides, exclusive thread
360 	 * pools are not affected by g_thread_pool_set_max_idle_time()
361 	 * since their threads are never considered idle and returned to the
362 	 * global pool.
363 	 *
364 	 * @error can be %NULL to ignore errors, or non-%NULL to report
365 	 * errors. An error can only occur when @exclusive is set to %TRUE
366 	 * and not all @max_threads threads could be created.
367 	 * See #GThreadError for possible errors that may occur.
368 	 * Note, even in case of error a valid #GThreadPool is returned.
369 	 *
370 	 * Params:
371 	 *     func = a function to execute in the threads of the new thread pool
372 	 *     userData = user data that is handed over to @func every time it
373 	 *         is called
374 	 *     maxThreads = the maximal number of threads to execute concurrently
375 	 *         in  the new thread pool, -1 means no limit
376 	 *     exclusive = should this thread pool be exclusive?
377 	 *
378 	 * Returns: the new #GThreadPool
379 	 *
380 	 * Throws: GException on failure.
381 	 * Throws: ConstructionException GTK+ fails to create the object.
382 	 */
383 	public this(GFunc func, void* userData, int maxThreads, bool exclusive)
384 	{
385 		GError* err = null;
386 
387 		auto __p = g_thread_pool_new(func, userData, maxThreads, exclusive, &err);
388 
389 		if (err !is null)
390 		{
391 			throw new GException( new ErrorG(err) );
392 		}
393 
394 		if(__p is null)
395 		{
396 			throw new ConstructionException("null returned by new");
397 		}
398 
399 		this(cast(GThreadPool*) __p);
400 	}
401 
402 	/**
403 	 * This function will set the maximum @interval that a thread
404 	 * waiting in the pool for new tasks can be idle for before
405 	 * being stopped. This function is similar to calling
406 	 * g_thread_pool_stop_unused_threads() on a regular timeout,
407 	 * except this is done on a per thread basis.
408 	 *
409 	 * By setting @interval to 0, idle threads will not be stopped.
410 	 *
411 	 * The default value is 15000 (15 seconds).
412 	 *
413 	 * Params:
414 	 *     interval = the maximum @interval (in milliseconds)
415 	 *         a thread can be idle
416 	 *
417 	 * Since: 2.10
418 	 */
419 	public static void setMaxIdleTime(uint interval)
420 	{
421 		g_thread_pool_set_max_idle_time(interval);
422 	}
423 
424 	/**
425 	 * Sets the maximal number of unused threads to @max_threads.
426 	 * If @max_threads is -1, no limit is imposed on the number
427 	 * of unused threads.
428 	 *
429 	 * The default value is 2.
430 	 *
431 	 * Params:
432 	 *     maxThreads = maximal number of unused threads
433 	 */
434 	public static void setMaxUnusedThreads(int maxThreads)
435 	{
436 		g_thread_pool_set_max_unused_threads(maxThreads);
437 	}
438 
439 	/**
440 	 * Stops all currently unused threads. This does not change the
441 	 * maximal number of unused threads. This function can be used to
442 	 * regularly stop all unused threads e.g. from g_timeout_add().
443 	 */
444 	public static void stopUnusedThreads()
445 	{
446 		g_thread_pool_stop_unused_threads();
447 	}
448 }