Skip to content

Commit

Permalink
Progress safety
Browse files Browse the repository at this point in the history
While working on progress data we make sure the progress and control pointers are available.
  • Loading branch information
jenshannoschwalm committed Dec 15, 2024
1 parent d87fa8e commit e7f554a
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/control/progress.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of darktable,
Copyright (C) 2014-2023 darktable developers.
Copyright (C) 2014-2024 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -248,7 +248,7 @@ static void global_progress_end(dt_control_t *control, dt_progress_t *progress)
#endif
}

void dt_control_progress_init(struct dt_control_t *control)
void dt_control_progress_init(dt_control_t *control)
{
#ifndef _WIN32

Expand Down Expand Up @@ -298,6 +298,8 @@ void dt_control_progress_init(struct dt_control_t *control)
dt_progress_t *dt_control_progress_create(dt_control_t *control, gboolean has_progress_bar,
const gchar *message)
{
if(!dt_control_running())
return NULL;
// create the object
dt_progress_t *progress = calloc(1, sizeof(dt_progress_t));
dt_pthread_mutex_init(&(progress->mutex), NULL);
Expand Down Expand Up @@ -325,6 +327,7 @@ dt_progress_t *dt_control_progress_create(dt_control_t *control, gboolean has_pr

void dt_control_progress_destroy(dt_control_t *control, dt_progress_t *progress)
{
if(!control || !progress) return;
dt_pthread_mutex_lock(&control->progress_system.mutex);

// tell the gui
Expand All @@ -344,9 +347,10 @@ void dt_control_progress_destroy(dt_control_t *control, dt_progress_t *progress)
free(progress);
}

void dt_control_progress_make_cancellable(struct dt_control_t *control, dt_progress_t *progress,
void dt_control_progress_make_cancellable(dt_control_t *control, dt_progress_t *progress,
dt_progress_cancel_callback_t cancel, void *data)
{
if(!control || !progress) return;
// set the value
dt_pthread_mutex_lock(&progress->mutex);
progress->cancel = cancel;
Expand All @@ -361,18 +365,19 @@ void dt_control_progress_make_cancellable(struct dt_control_t *control, dt_progr
dt_pthread_mutex_unlock(&control->progress_system.mutex);
}

static void dt_control_progress_cancel_callback(dt_progress_t *progress, void *data)
static void _control_progress_cancel_callback(dt_progress_t *progress, void *data)
{
dt_control_job_cancel((dt_job_t *)data);
}

void dt_control_progress_attach_job(dt_control_t *control, dt_progress_t *progress, dt_job_t *job)
{
dt_control_progress_make_cancellable(control, progress, &dt_control_progress_cancel_callback, job);
dt_control_progress_make_cancellable(control, progress, &_control_progress_cancel_callback, job);
}

void dt_control_progress_cancel(dt_control_t *control, dt_progress_t *progress)
{
if(!control || !progress) return;
dt_pthread_mutex_lock(&progress->mutex);
if(progress->cancel == NULL)
{
Expand All @@ -390,6 +395,7 @@ void dt_control_progress_cancel(dt_control_t *control, dt_progress_t *progress)

void dt_control_progress_set_progress(dt_control_t *control, dt_progress_t *progress, double value)
{
if(!dt_control_running() || !progress) return;
// set the value
value = CLAMP(value, 0.0, 1.0);
dt_pthread_mutex_lock(&progress->mutex);
Expand All @@ -408,6 +414,7 @@ void dt_control_progress_set_progress(dt_control_t *control, dt_progress_t *prog

double dt_control_progress_get_progress(dt_progress_t *progress)
{
if(!progress) return 0.0;
dt_pthread_mutex_lock(&progress->mutex);
double res = progress->progress;
dt_pthread_mutex_unlock(&progress->mutex);
Expand All @@ -416,6 +423,7 @@ double dt_control_progress_get_progress(dt_progress_t *progress)

const gchar *dt_control_progress_get_message(dt_progress_t *progress)
{
if(!progress) return "";
dt_pthread_mutex_lock(&progress->mutex);
const gchar *res = progress->message;
dt_pthread_mutex_unlock(&progress->mutex);
Expand All @@ -424,6 +432,7 @@ const gchar *dt_control_progress_get_message(dt_progress_t *progress)

void dt_control_progress_set_message(dt_control_t *control, dt_progress_t *progress, const char *message)
{
if(!control || !progress) return;
dt_pthread_mutex_lock(&progress->mutex);
g_free(progress->message);
progress->message = g_strdup(message);
Expand All @@ -439,13 +448,15 @@ void dt_control_progress_set_message(dt_control_t *control, dt_progress_t *progr

void dt_control_progress_set_gui_data(dt_progress_t *progress, void *data)
{
if(!progress) return;
dt_pthread_mutex_lock(&progress->mutex);
progress->gui_data = data;
dt_pthread_mutex_unlock(&progress->mutex);
}

void *dt_control_progress_get_gui_data(dt_progress_t *progress)
{
if(!progress) return NULL;
dt_pthread_mutex_lock(&progress->mutex);
void *res = progress->gui_data;
dt_pthread_mutex_unlock(&progress->mutex);
Expand All @@ -454,6 +465,7 @@ void *dt_control_progress_get_gui_data(dt_progress_t *progress)

gboolean dt_control_progress_has_progress_bar(dt_progress_t *progress)
{
if(!progress) return FALSE;
dt_pthread_mutex_lock(&progress->mutex);
gboolean res = progress->has_progress_bar;
dt_pthread_mutex_unlock(&progress->mutex);
Expand All @@ -462,6 +474,7 @@ gboolean dt_control_progress_has_progress_bar(dt_progress_t *progress)

gboolean dt_control_progress_cancellable(dt_progress_t *progress)
{
if(!progress) return FALSE;
dt_pthread_mutex_lock(&progress->mutex);
gboolean res = progress->cancel != NULL;
dt_pthread_mutex_unlock(&progress->mutex);
Expand Down

0 comments on commit e7f554a

Please sign in to comment.