source: appstream-generator/build/girepo/glib/ThreadPool.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

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