mirror of https://github.com/GNOME/gimp.git
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.
This commit is contained in:
parent
ff679c66a1
commit
b74e600c12
|
@ -52,8 +52,10 @@ extern "C"
|
|||
typedef struct
|
||||
{
|
||||
GimpAsync *async;
|
||||
gint priority;
|
||||
GimpParallelRunAsyncFunc func;
|
||||
gpointer user_data;
|
||||
GDestroyNotify user_data_destroy_func;
|
||||
} GimpParallelRunAsyncTask;
|
||||
|
||||
typedef struct
|
||||
|
@ -161,9 +163,17 @@ gimp_parallel_exit (Gimp *gimp)
|
|||
}
|
||||
|
||||
GimpAsync *
|
||||
gimp_parallel_run_async (gboolean independent,
|
||||
GimpParallelRunAsyncFunc func,
|
||||
gimp_parallel_run_async (GimpParallelRunAsyncFunc func,
|
||||
gpointer user_data)
|
||||
{
|
||||
return gimp_parallel_run_async_full (0, func, user_data, NULL);
|
||||
}
|
||||
|
||||
GimpAsync *
|
||||
gimp_parallel_run_async_full (gint priority,
|
||||
GimpParallelRunAsyncFunc func,
|
||||
gpointer user_data,
|
||||
GDestroyNotify user_data_destroy_func)
|
||||
{
|
||||
GimpAsync *async;
|
||||
GimpParallelRunAsyncTask *task;
|
||||
|
@ -174,13 +184,90 @@ gimp_parallel_run_async (gboolean independent,
|
|||
|
||||
task = g_slice_new (GimpParallelRunAsyncTask);
|
||||
|
||||
task->async = GIMP_ASYNC (g_object_ref (async));
|
||||
task->priority = priority;
|
||||
task->func = func;
|
||||
task->user_data = user_data;
|
||||
task->user_data_destroy_func = user_data_destroy_func;
|
||||
|
||||
if (gimp_parallel_run_async_n_threads > 0)
|
||||
{
|
||||
GList *link;
|
||||
GList *iter;
|
||||
|
||||
link = g_list_alloc ();
|
||||
link->data = task;
|
||||
|
||||
g_object_set_data (G_OBJECT (async),
|
||||
"gimp-parallel-run-async-link", link);
|
||||
|
||||
g_signal_connect_after (async, "cancel",
|
||||
G_CALLBACK (gimp_parallel_run_async_cancel),
|
||||
NULL);
|
||||
|
||||
g_mutex_lock (&gimp_parallel_run_async_mutex);
|
||||
|
||||
for (iter = g_queue_peek_tail_link (&gimp_parallel_run_async_queue);
|
||||
iter;
|
||||
iter = g_list_previous (iter))
|
||||
{
|
||||
GimpParallelRunAsyncTask *other_task =
|
||||
(GimpParallelRunAsyncTask *) iter->data;
|
||||
|
||||
if (other_task->priority <= task->priority)
|
||||
break;
|
||||
}
|
||||
|
||||
if (iter)
|
||||
{
|
||||
link->prev = iter;
|
||||
link->next = iter->next;
|
||||
|
||||
iter->next = link;
|
||||
|
||||
if (link->next)
|
||||
link->next->prev = link;
|
||||
else
|
||||
gimp_parallel_run_async_queue.tail = link;
|
||||
|
||||
gimp_parallel_run_async_queue.length++;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_queue_push_head_link (&gimp_parallel_run_async_queue, link);
|
||||
}
|
||||
|
||||
g_cond_signal (&gimp_parallel_run_async_cond);
|
||||
|
||||
g_mutex_unlock (&gimp_parallel_run_async_mutex);
|
||||
}
|
||||
else
|
||||
{
|
||||
gimp_parallel_run_async_execute_task (task);
|
||||
}
|
||||
|
||||
return async;
|
||||
}
|
||||
|
||||
GimpAsync *
|
||||
gimp_parallel_run_async_independent (GimpParallelRunAsyncFunc func,
|
||||
gpointer user_data)
|
||||
{
|
||||
GimpAsync *async;
|
||||
GimpParallelRunAsyncTask *task;
|
||||
GThread *thread;
|
||||
|
||||
g_return_val_if_fail (func != NULL, NULL);
|
||||
|
||||
async = gimp_async_new ();
|
||||
|
||||
task = g_slice_new0 (GimpParallelRunAsyncTask);
|
||||
|
||||
task->async = GIMP_ASYNC (g_object_ref (async));
|
||||
task->func = func;
|
||||
task->user_data = user_data;
|
||||
|
||||
if (independent)
|
||||
{
|
||||
GThread *thread = g_thread_new (
|
||||
thread = g_thread_new (
|
||||
"async-ind",
|
||||
[] (gpointer data) -> gpointer
|
||||
{
|
||||
|
@ -207,35 +294,6 @@ gimp_parallel_run_async (gboolean independent,
|
|||
g_thread_join ((GThread *) thread);
|
||||
},
|
||||
thread);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (gimp_parallel_run_async_n_threads > 0)
|
||||
{
|
||||
GList *link;
|
||||
|
||||
link = g_list_alloc ();
|
||||
link->data = task;
|
||||
|
||||
g_object_set_data (G_OBJECT (async),
|
||||
"gimp-parallel-run-async-link", link);
|
||||
|
||||
g_signal_connect_after (async, "cancel",
|
||||
G_CALLBACK (gimp_parallel_run_async_cancel),
|
||||
NULL);
|
||||
|
||||
g_mutex_lock (&gimp_parallel_run_async_mutex);
|
||||
|
||||
g_queue_push_tail_link (&gimp_parallel_run_async_queue, link);
|
||||
g_cond_signal (&gimp_parallel_run_async_cond);
|
||||
|
||||
g_mutex_unlock (&gimp_parallel_run_async_mutex);
|
||||
}
|
||||
else
|
||||
{
|
||||
gimp_parallel_run_async_execute_task (task);
|
||||
}
|
||||
}
|
||||
|
||||
return async;
|
||||
}
|
||||
|
@ -508,6 +566,7 @@ static void
|
|||
gimp_parallel_run_async_cancel (GimpAsync *async)
|
||||
{
|
||||
GList *link;
|
||||
GimpParallelRunAsyncTask *task = NULL;
|
||||
|
||||
g_mutex_lock (&gimp_parallel_run_async_mutex);
|
||||
|
||||
|
@ -519,15 +578,20 @@ gimp_parallel_run_async_cancel (GimpAsync *async)
|
|||
g_object_set_data (G_OBJECT (async),
|
||||
"gimp-parallel-run-async-link", NULL);
|
||||
|
||||
g_slice_free (GimpParallelRunAsyncTask, link->data);
|
||||
task = (GimpParallelRunAsyncTask *) link->data;
|
||||
|
||||
g_queue_delete_link (&gimp_parallel_run_async_queue, link);
|
||||
}
|
||||
|
||||
g_mutex_unlock (&gimp_parallel_run_async_mutex);
|
||||
|
||||
if (link)
|
||||
if (task)
|
||||
{
|
||||
if (task->user_data && task->user_data_destroy_func)
|
||||
task->user_data_destroy_func (task->user_data);
|
||||
|
||||
g_slice_free (GimpParallelRunAsyncTask, task);
|
||||
|
||||
gimp_async_abort (async);
|
||||
|
||||
g_object_unref (async);
|
||||
|
|
|
@ -38,8 +38,13 @@ typedef void (* GimpParallelDistributeAreaFunc) (const GeglRectangle *area,
|
|||
void gimp_parallel_init (Gimp *gimp);
|
||||
void gimp_parallel_exit (Gimp *gimp);
|
||||
|
||||
GimpAsync * gimp_parallel_run_async (gboolean independent,
|
||||
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);
|
||||
|
||||
void gimp_parallel_distribute (gint max_n,
|
||||
|
@ -70,7 +75,7 @@ gimp_parallel_run_async (gboolean independent,
|
|||
|
||||
new (func_copy) ParallelRunAsyncFunc (func);
|
||||
|
||||
return gimp_parallel_run_async (independent,
|
||||
return gimp_parallel_run_async_full (0,
|
||||
[] (GimpAsync *async,
|
||||
gpointer user_data)
|
||||
{
|
||||
|
@ -82,6 +87,78 @@ gimp_parallel_run_async (gboolean independent,
|
|||
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;
|
||||
|
||||
(*func_copy) (async);
|
||||
|
||||
func_copy->~ParallelRunAsyncFunc ();
|
||||
g_free (func_copy);
|
||||
},
|
||||
func_copy);
|
||||
}
|
||||
|
||||
|
|
|
@ -348,7 +348,6 @@ gimp_histogram_calculate_async (GimpHistogram *histogram,
|
|||
}
|
||||
|
||||
histogram->priv->calculate_async = gimp_parallel_run_async (
|
||||
FALSE,
|
||||
(GimpParallelRunAsyncFunc) gimp_histogram_calculate_internal,
|
||||
context);
|
||||
|
||||
|
|
|
@ -544,8 +544,7 @@ gui_wait (Gimp *gimp,
|
|||
if (GIMP_IS_CANCELABLE (waitable))
|
||||
{
|
||||
/* listens for a cancellation request */
|
||||
input_async = gimp_parallel_run_async (
|
||||
TRUE,
|
||||
input_async = gimp_parallel_run_async_independent (
|
||||
(GimpParallelRunAsyncFunc) gui_wait_input_async,
|
||||
input_pipe);
|
||||
|
||||
|
|
|
@ -340,7 +340,7 @@ gimp_font_factory_load (GimpFontFactory *factory,
|
|||
* in the case a cache rebuild is to be done it will not block
|
||||
* the UI.
|
||||
*/
|
||||
async = gimp_parallel_run_async (TRUE,
|
||||
async = gimp_parallel_run_async_independent (
|
||||
(GimpParallelRunAsyncFunc) gimp_font_factory_load_async,
|
||||
config);
|
||||
|
||||
|
|
Loading…
Reference in New Issue