app: add gimp-parallel
Add gimp-parallel.[cc,h], which provides a set of parallel
algorithms.
These currently include:
- gimp_parallel_distribute(): Calls a callback function in
parallel on multiple threads, passing it the current thread
index, and the total number of threads. Allows specifying the
maximal number of threads used.
- gimp_parallel_distribute_range(): Splits a range of integers
between multiple threads, passing the sub-range to a callback
function. Allows specifying the minimal sub-range size.
- gimp_parallel_distribute_area(): Splits a rectangular area
between multiple threads, passing the sub-area to a callback
function. Allows specifying the minimal sub-area.
The callback function is passed using an appropriately-typed
function pointer, and a user-data pointer. Additionally, when used
in a C++ file, each of the above functions has an overloaded
template version, taking the callback through a generic parameter,
without a user-data pointer, which allows using function objects.
2018-04-05 03:16:42 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimp-parallel.h
|
|
|
|
* Copyright (C) 2018 Ell
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-07-12 05:27:07 +08:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
app: add gimp-parallel
Add gimp-parallel.[cc,h], which provides a set of parallel
algorithms.
These currently include:
- gimp_parallel_distribute(): Calls a callback function in
parallel on multiple threads, passing it the current thread
index, and the total number of threads. Allows specifying the
maximal number of threads used.
- gimp_parallel_distribute_range(): Splits a range of integers
between multiple threads, passing the sub-range to a callback
function. Allows specifying the minimal sub-range size.
- gimp_parallel_distribute_area(): Splits a rectangular area
between multiple threads, passing the sub-area to a callback
function. Allows specifying the minimal sub-area.
The callback function is passed using an appropriately-typed
function pointer, and a user-data pointer. Additionally, when used
in a C++ file, each of the above functions has an overloaded
template version, taking the callback through a generic parameter,
without a user-data pointer, which allows using function objects.
2018-04-05 03:16:42 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __GIMP_PARALLEL_H__
|
|
|
|
#define __GIMP_PARALLEL_H__
|
|
|
|
|
|
|
|
|
2018-11-14 23:58:58 +08:00
|
|
|
typedef void (* GimpParallelRunAsyncFunc) (GimpAsync *async,
|
|
|
|
gpointer user_data);
|
2018-05-11 23:43:06 +08:00
|
|
|
|
app: add gimp-parallel
Add gimp-parallel.[cc,h], which provides a set of parallel
algorithms.
These currently include:
- gimp_parallel_distribute(): Calls a callback function in
parallel on multiple threads, passing it the current thread
index, and the total number of threads. Allows specifying the
maximal number of threads used.
- gimp_parallel_distribute_range(): Splits a range of integers
between multiple threads, passing the sub-range to a callback
function. Allows specifying the minimal sub-range size.
- gimp_parallel_distribute_area(): Splits a rectangular area
between multiple threads, passing the sub-area to a callback
function. Allows specifying the minimal sub-area.
The callback function is passed using an appropriately-typed
function pointer, and a user-data pointer. Additionally, when used
in a C++ file, each of the above functions has an overloaded
template version, taking the callback through a generic parameter,
without a user-data pointer, which allows using function objects.
2018-04-05 03:16:42 +08:00
|
|
|
|
2018-11-14 23:58:58 +08:00
|
|
|
void gimp_parallel_init (Gimp *gimp);
|
|
|
|
void gimp_parallel_exit (Gimp *gimp);
|
app: add gimp-parallel
Add gimp-parallel.[cc,h], which provides a set of parallel
algorithms.
These currently include:
- gimp_parallel_distribute(): Calls a callback function in
parallel on multiple threads, passing it the current thread
index, and the total number of threads. Allows specifying the
maximal number of threads used.
- gimp_parallel_distribute_range(): Splits a range of integers
between multiple threads, passing the sub-range to a callback
function. Allows specifying the minimal sub-range size.
- gimp_parallel_distribute_area(): Splits a rectangular area
between multiple threads, passing the sub-area to a callback
function. Allows specifying the minimal sub-area.
The callback function is passed using an appropriately-typed
function pointer, and a user-data pointer. Additionally, when used
in a C++ file, each of the above functions has an overloaded
template version, taking the callback through a generic parameter,
without a user-data pointer, which allows using function objects.
2018-04-05 03:16:42 +08:00
|
|
|
|
2018-11-14 23:58:58 +08:00
|
|
|
GimpAsync * gimp_parallel_run_async (GimpParallelRunAsyncFunc func,
|
|
|
|
gpointer user_data);
|
|
|
|
GimpAsync * gimp_parallel_run_async_full (gint priority,
|
|
|
|
GimpParallelRunAsyncFunc func,
|
|
|
|
gpointer user_data,
|
|
|
|
GDestroyNotify user_data_destroy_func);
|
|
|
|
GimpAsync * gimp_parallel_run_async_independent (GimpParallelRunAsyncFunc func,
|
|
|
|
gpointer user_data);
|
app: add gimp_parallel_run_async_{full,independent}()
Remove the "independent" parameter of gimp_parallel_run_async(),
and have the function always execute the passed callback in the
shared async thread-pool.
Add a new gimp_parallel_run_async_full() function, taking, in
addition to a callback and a data pointer:
- A priority value, controlling the priority of the callback in
the async thread-pool queue. 0 is the default priority (used
by gimp_parallel_run_async()), negative values have higher
priority, and positive values have lower priority.
- A destructor function for the data pointer. This function is
called to free the user data in case the async operation is
canceled before execution of the callback function begins, and
the operation is dropped from the queue and aborted without
executing the callback. Note that if the callback *is*
executed, the destructor is *not* used -- it's the callback's
responsibility to free/recycle the user data.
Add a separate gimp_parallel_run_async_independent() function,
taking the same parameters, and executing the passed callback in
an independent thread, rather than the thread pool. This function
doesn't take a priority value or a destructor (and there's no
corresponding "_full()" variant that does), since they're pointless
for independent threads.
Adapt the rest of the code to the changes.
2018-07-01 21:57:46 +08:00
|
|
|
|
app: add gimp-parallel
Add gimp-parallel.[cc,h], which provides a set of parallel
algorithms.
These currently include:
- gimp_parallel_distribute(): Calls a callback function in
parallel on multiple threads, passing it the current thread
index, and the total number of threads. Allows specifying the
maximal number of threads used.
- gimp_parallel_distribute_range(): Splits a range of integers
between multiple threads, passing the sub-range to a callback
function. Allows specifying the minimal sub-range size.
- gimp_parallel_distribute_area(): Splits a rectangular area
between multiple threads, passing the sub-area to a callback
function. Allows specifying the minimal sub-area.
The callback function is passed using an appropriately-typed
function pointer, and a user-data pointer. Additionally, when used
in a C++ file, each of the above functions has an overloaded
template version, taking the callback through a generic parameter,
without a user-data pointer, which allows using function objects.
2018-04-05 03:16:42 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
extern "C++"
|
|
|
|
{
|
|
|
|
|
2018-05-11 23:43:06 +08:00
|
|
|
#include <new>
|
|
|
|
|
|
|
|
template <class ParallelRunAsyncFunc>
|
|
|
|
inline GimpAsync *
|
2018-07-03 12:45:17 +08:00
|
|
|
gimp_parallel_run_async (ParallelRunAsyncFunc func)
|
2018-05-11 23:43:06 +08:00
|
|
|
{
|
|
|
|
ParallelRunAsyncFunc *func_copy = g_new (ParallelRunAsyncFunc, 1);
|
|
|
|
|
|
|
|
new (func_copy) ParallelRunAsyncFunc (func);
|
|
|
|
|
app: add gimp_parallel_run_async_{full,independent}()
Remove the "independent" parameter of gimp_parallel_run_async(),
and have the function always execute the passed callback in the
shared async thread-pool.
Add a new gimp_parallel_run_async_full() function, taking, in
addition to a callback and a data pointer:
- A priority value, controlling the priority of the callback in
the async thread-pool queue. 0 is the default priority (used
by gimp_parallel_run_async()), negative values have higher
priority, and positive values have lower priority.
- A destructor function for the data pointer. This function is
called to free the user data in case the async operation is
canceled before execution of the callback function begins, and
the operation is dropped from the queue and aborted without
executing the callback. Note that if the callback *is*
executed, the destructor is *not* used -- it's the callback's
responsibility to free/recycle the user data.
Add a separate gimp_parallel_run_async_independent() function,
taking the same parameters, and executing the passed callback in
an independent thread, rather than the thread pool. This function
doesn't take a priority value or a destructor (and there's no
corresponding "_full()" variant that does), since they're pointless
for independent threads.
Adapt the rest of the code to the changes.
2018-07-01 21:57:46 +08:00
|
|
|
return gimp_parallel_run_async_full (0,
|
|
|
|
[] (GimpAsync *async,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
ParallelRunAsyncFunc *func_copy =
|
|
|
|
(ParallelRunAsyncFunc *) user_data;
|
|
|
|
|
|
|
|
(*func_copy) (async);
|
|
|
|
|
|
|
|
func_copy->~ParallelRunAsyncFunc ();
|
|
|
|
g_free (func_copy);
|
|
|
|
},
|
|
|
|
func_copy,
|
|
|
|
[] (gpointer user_data)
|
|
|
|
{
|
|
|
|
ParallelRunAsyncFunc *func_copy =
|
|
|
|
(ParallelRunAsyncFunc *) user_data;
|
|
|
|
|
|
|
|
func_copy->~ParallelRunAsyncFunc ();
|
|
|
|
g_free (func_copy);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParallelRunAsyncFunc,
|
|
|
|
class DestroyFunc>
|
|
|
|
inline GimpAsync *
|
|
|
|
gimp_parallel_run_async_full (gint priority,
|
|
|
|
ParallelRunAsyncFunc func,
|
|
|
|
DestroyFunc destroy_func)
|
|
|
|
{
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ParallelRunAsyncFunc func;
|
|
|
|
DestroyFunc destroy_func;
|
|
|
|
} Funcs;
|
|
|
|
|
|
|
|
Funcs *funcs_copy = g_new (Funcs, 1);
|
|
|
|
|
|
|
|
new (funcs_copy) Funcs {func, destroy_func};
|
|
|
|
|
|
|
|
return gimp_parallel_run_async_full (priority,
|
|
|
|
[] (GimpAsync *async,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
Funcs *funcs_copy =
|
|
|
|
(Funcs *) user_data;
|
|
|
|
|
|
|
|
funcs_copy->func (async);
|
|
|
|
|
|
|
|
funcs_copy->~Funcs ();
|
|
|
|
g_free (funcs_copy);
|
|
|
|
},
|
|
|
|
funcs_copy,
|
|
|
|
[] (gpointer user_data)
|
|
|
|
{
|
|
|
|
Funcs *funcs_copy =
|
|
|
|
(Funcs *) user_data;
|
|
|
|
|
|
|
|
funcs_copy->destroy_func ();
|
|
|
|
|
|
|
|
funcs_copy->~Funcs ();
|
|
|
|
g_free (funcs_copy);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParallelRunAsyncFunc>
|
|
|
|
inline GimpAsync *
|
|
|
|
gimp_parallel_run_async_independent (ParallelRunAsyncFunc func)
|
|
|
|
{
|
|
|
|
ParallelRunAsyncFunc *func_copy = g_new (ParallelRunAsyncFunc, 1);
|
|
|
|
|
|
|
|
new (func_copy) ParallelRunAsyncFunc (func);
|
|
|
|
|
|
|
|
return gimp_parallel_run_async_independent ([] (GimpAsync *async,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
ParallelRunAsyncFunc *func_copy =
|
|
|
|
(ParallelRunAsyncFunc *) user_data;
|
2018-05-11 23:43:06 +08:00
|
|
|
|
app: add gimp_parallel_run_async_{full,independent}()
Remove the "independent" parameter of gimp_parallel_run_async(),
and have the function always execute the passed callback in the
shared async thread-pool.
Add a new gimp_parallel_run_async_full() function, taking, in
addition to a callback and a data pointer:
- A priority value, controlling the priority of the callback in
the async thread-pool queue. 0 is the default priority (used
by gimp_parallel_run_async()), negative values have higher
priority, and positive values have lower priority.
- A destructor function for the data pointer. This function is
called to free the user data in case the async operation is
canceled before execution of the callback function begins, and
the operation is dropped from the queue and aborted without
executing the callback. Note that if the callback *is*
executed, the destructor is *not* used -- it's the callback's
responsibility to free/recycle the user data.
Add a separate gimp_parallel_run_async_independent() function,
taking the same parameters, and executing the passed callback in
an independent thread, rather than the thread pool. This function
doesn't take a priority value or a destructor (and there's no
corresponding "_full()" variant that does), since they're pointless
for independent threads.
Adapt the rest of the code to the changes.
2018-07-01 21:57:46 +08:00
|
|
|
(*func_copy) (async);
|
2018-05-11 23:43:06 +08:00
|
|
|
|
app: add gimp_parallel_run_async_{full,independent}()
Remove the "independent" parameter of gimp_parallel_run_async(),
and have the function always execute the passed callback in the
shared async thread-pool.
Add a new gimp_parallel_run_async_full() function, taking, in
addition to a callback and a data pointer:
- A priority value, controlling the priority of the callback in
the async thread-pool queue. 0 is the default priority (used
by gimp_parallel_run_async()), negative values have higher
priority, and positive values have lower priority.
- A destructor function for the data pointer. This function is
called to free the user data in case the async operation is
canceled before execution of the callback function begins, and
the operation is dropped from the queue and aborted without
executing the callback. Note that if the callback *is*
executed, the destructor is *not* used -- it's the callback's
responsibility to free/recycle the user data.
Add a separate gimp_parallel_run_async_independent() function,
taking the same parameters, and executing the passed callback in
an independent thread, rather than the thread pool. This function
doesn't take a priority value or a destructor (and there's no
corresponding "_full()" variant that does), since they're pointless
for independent threads.
Adapt the rest of the code to the changes.
2018-07-01 21:57:46 +08:00
|
|
|
func_copy->~ParallelRunAsyncFunc ();
|
|
|
|
g_free (func_copy);
|
|
|
|
},
|
|
|
|
func_copy);
|
2018-05-11 23:43:06 +08:00
|
|
|
}
|
|
|
|
|
app: add gimp-parallel
Add gimp-parallel.[cc,h], which provides a set of parallel
algorithms.
These currently include:
- gimp_parallel_distribute(): Calls a callback function in
parallel on multiple threads, passing it the current thread
index, and the total number of threads. Allows specifying the
maximal number of threads used.
- gimp_parallel_distribute_range(): Splits a range of integers
between multiple threads, passing the sub-range to a callback
function. Allows specifying the minimal sub-range size.
- gimp_parallel_distribute_area(): Splits a rectangular area
between multiple threads, passing the sub-area to a callback
function. Allows specifying the minimal sub-area.
The callback function is passed using an appropriately-typed
function pointer, and a user-data pointer. Additionally, when used
in a C++ file, each of the above functions has an overloaded
template version, taking the callback through a generic parameter,
without a user-data pointer, which allows using function objects.
2018-04-05 03:16:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* __GIMP_PARALLEL_H__ */
|