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