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