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