Skip to content

Commit

Permalink
Merge pull request #18422 from jenshannoschwalm/control_cleanup
Browse files Browse the repository at this point in the history
Control, image and mipmap cache maintenance
  • Loading branch information
TurboGit authored Feb 16, 2025
2 parents d4015d3 + 8bd2f96 commit 084e910
Show file tree
Hide file tree
Showing 78 changed files with 973 additions and 1,051 deletions.
4 changes: 2 additions & 2 deletions src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ int main(int argc, char *arg[])
for(GList *iter = id_list; iter; iter = g_list_next(iter))
{
int id = GPOINTER_TO_INT(iter->data);
dt_image_t *image = dt_image_cache_get(darktable.image_cache, id, 'w');
dt_image_t *image = dt_image_cache_get(id, 'w');
if(dt_exif_xmp_read(image, xmp_filename, 1))
{
fprintf(stderr, _("error: can't open XMP file %s"), xmp_filename);
Expand All @@ -612,7 +612,7 @@ int main(int argc, char *arg[])
exit(1);
}
// don't write new xmp:
dt_image_cache_write_release(darktable.image_cache, image, DT_IMAGE_CACHE_RELAXED);
dt_image_cache_write_release(image, DT_IMAGE_CACHE_RELAXED);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/act_on.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ static void _insert_in_list(GList **list,
return;
}

const dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'r');
const dt_image_t *image = dt_image_cache_get(imgid, 'r');
if(image)
{
const dt_imgid_t img_group_id = image->group_id;
dt_image_cache_read_release(darktable.image_cache, image);
dt_image_cache_read_release(image);

if(!darktable.gui
|| !darktable.gui->grouping
Expand Down
30 changes: 12 additions & 18 deletions src/common/cache.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of darktable,
Copyright (C) 2011-2024 darktable developers.
Copyright (C) 2011-2025 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 @@ -78,10 +78,9 @@ int32_t dt_cache_contains(dt_cache_t *cache,
return result;
}

int dt_cache_for_all
(dt_cache_t *cache,
int (*process)(const uint32_t key, const void *data, void *user_data),
void *user_data)
int dt_cache_for_all(dt_cache_t *cache,
int (*process)(const uint32_t key, const void *data, void *user_data),
void *user_data)
{
dt_pthread_mutex_lock(&cache->lock);
GHashTableIter iter;
Expand Down Expand Up @@ -173,11 +172,9 @@ dt_cache_entry_t *dt_cache_get_with_caller(dt_cache_t *cache,
if(res)
{ // yay, found. read lock and pass on.
dt_cache_entry_t *entry = (dt_cache_entry_t *)value;
int result;
if(mode == 'w')
result = dt_pthread_rwlock_trywrlock_with_caller(&entry->lock, file, line);
else
result = dt_pthread_rwlock_tryrdlock_with_caller(&entry->lock, file, line);
const int result = (mode == 'w')
? dt_pthread_rwlock_trywrlock_with_caller(&entry->lock, file, line)
: dt_pthread_rwlock_tryrdlock_with_caller(&entry->lock, file, line);
if(result)
{ // need to give up mutex so other threads have a chance to get in between and
// free the lock we're trying to acquire:
Expand Down Expand Up @@ -224,9 +221,8 @@ dt_cache_entry_t *dt_cache_get_with_caller(dt_cache_t *cache,
}

// here dies your 32-bit system:
dt_cache_entry_t *entry = (dt_cache_entry_t *)g_slice_alloc(sizeof(dt_cache_entry_t));
const int ret = dt_pthread_rwlock_init(&entry->lock, 0);
if(ret) dt_print(DT_DEBUG_ALWAYS, "rwlock init: %d", ret);
dt_cache_entry_t *entry = g_slice_alloc(sizeof(dt_cache_entry_t));
dt_pthread_rwlock_init(&entry->lock, 0);

entry->data = 0;
entry->data_size = cache->entry_size;
Expand All @@ -248,8 +244,7 @@ dt_cache_entry_t *dt_cache_get_with_caller(dt_cache_t *cache,
ASAN_POISON_MEMORY_REGION(entry->data, entry->data_size);

// if allocate callback is given, always return a write lock
const int write = ((mode == 'w') || cache->allocate);

const gboolean write = ((mode == 'w') || cache->allocate);
// write lock in case the caller requests it:
if(write)
dt_pthread_rwlock_wrlock_with_caller(&entry->lock, file, line);
Expand Down Expand Up @@ -290,8 +285,7 @@ int dt_cache_remove(dt_cache_t *cache,
return 1;
}
// need write lock to be able to delete:
const int result = dt_pthread_rwlock_trywrlock(&entry->lock);
if(result)
if(dt_pthread_rwlock_trywrlock(&entry->lock))
{
dt_pthread_mutex_unlock(&cache->lock);
g_usleep(5);
Expand All @@ -308,7 +302,7 @@ int dt_cache_remove(dt_cache_t *cache,
goto restart;
}

gboolean removed = g_hash_table_remove(cache->hashtable, GINT_TO_POINTER(key));
const gboolean removed = g_hash_table_remove(cache->hashtable, GINT_TO_POINTER(key));
(void)removed; // make non-assert compile happy
assert(removed);
cache->lru = g_list_delete_link(cache->lru, entry->link);
Expand Down
2 changes: 1 addition & 1 deletion src/common/collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2702,7 +2702,7 @@ gboolean dt_collection_hint_message_internal(void *message)
}
g_free(message);

dt_control_hinter_message(darktable.control, "");
dt_control_hinter_message("");

return FALSE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/cups_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void dt_printers_discovery(void (*cb)(dt_printer_info_t *pr, void *user_data), v
prtctl->user_data = user_data;

dt_control_job_set_params(job, prtctl, g_free);
dt_control_add_job(darktable.control, DT_JOB_QUEUE_SYSTEM_BG, job);
dt_control_add_job(DT_JOB_QUEUE_SYSTEM_BG, job);
}
}

Expand Down
63 changes: 24 additions & 39 deletions src/common/darktable.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,9 @@ dt_imgid_t dt_load_from_string(const gchar *input,
dt_film_open(filmid);
// make sure buffers are loaded (load full for testing)
dt_mipmap_buffer_t buf;
dt_mipmap_cache_get(darktable.mipmap_cache, &buf, imgid,
DT_MIPMAP_FULL, DT_MIPMAP_BLOCKING, 'r');
const gboolean loaded = (buf.buf != NULL);
dt_mipmap_cache_release(darktable.mipmap_cache, &buf);
dt_mipmap_cache_get(&buf, imgid, DT_MIPMAP_FULL, DT_MIPMAP_BLOCKING, 'r');
const gboolean loaded = buf.buf != NULL;
dt_mipmap_cache_release(&buf);
if(!loaded)
{
imgid = NO_IMGID;
Expand Down Expand Up @@ -741,8 +740,7 @@ void dt_start_backtumbs_crawler(void)
{
// don't write thumbs if using memory database or on a non-sufficient system
if(!darktable.backthumbs.running && darktable.backthumbs.capable)
dt_control_add_job(darktable.control, DT_JOB_QUEUE_SYSTEM_BG,
_backthumbs_job_create());
dt_control_add_job(DT_JOB_QUEUE_SYSTEM_BG, _backthumbs_job_create());
}

static char *_get_version_string(void)
Expand Down Expand Up @@ -916,15 +914,15 @@ int dt_init(int argc, char *argv[], const gboolean init_gui, const gboolean load
pthread_mutexattr_settype(&recursive_locking, PTHREAD_MUTEX_RECURSIVE);
for(int k=0; k<DT_IMAGE_DBLOCKS; k++)
{
dt_pthread_mutex_init(&(darktable.db_image[k]),&(recursive_locking));
dt_pthread_mutex_init(&darktable.db_image[k], &recursive_locking);
}
dt_pthread_mutex_init(&(darktable.plugin_threadsafe), NULL);
dt_pthread_mutex_init(&(darktable.dev_threadsafe), NULL);
dt_pthread_mutex_init(&(darktable.capabilities_threadsafe), NULL);
dt_pthread_mutex_init(&(darktable.exiv2_threadsafe), NULL);
dt_pthread_mutex_init(&(darktable.readFile_mutex), NULL);
dt_pthread_mutex_init(&(darktable.metadata_threadsafe), NULL);
darktable.control = (dt_control_t *)calloc(1, sizeof(dt_control_t));
dt_pthread_mutex_init(&darktable.plugin_threadsafe, NULL);
dt_pthread_mutex_init(&darktable.dev_threadsafe, NULL);
dt_pthread_mutex_init(&darktable.capabilities_threadsafe, NULL);
dt_pthread_mutex_init(&darktable.exiv2_threadsafe, NULL);
dt_pthread_mutex_init(&darktable.readFile_mutex, NULL);
dt_pthread_mutex_init(&darktable.metadata_threadsafe, NULL);
darktable.control = calloc(1, sizeof(dt_control_t));

// database
char *dbfilename_from_command = NULL;
Expand Down Expand Up @@ -1458,7 +1456,7 @@ int dt_init(int argc, char *argv[], const gboolean init_gui, const gboolean load

// make sure that we have no stale global progress bar
// visible. thus it's run as early as possible
dt_control_progress_init(darktable.control);
dt_control_progress_init();

// ensure that we can load the Gtk theme early enough that the splash screen
// doesn't change as we progress through startup
Expand Down Expand Up @@ -1598,20 +1596,15 @@ int dt_init(int argc, char *argv[], const gboolean init_gui, const gboolean load
// Initialize the signal system
darktable.signals = dt_control_signal_init();

dt_control_init(init_gui);
if(init_gui)
{
dt_control_init(darktable.control);

// initialize undo struct
darktable.undo = dt_undo_init();
}
else
{
if(dbfilename_from_command && !strcmp(dbfilename_from_command, ":memory:"))
dt_gui_presets_init(); // init preset db schema.

dt_atomic_set_int(&darktable.control->running, DT_CONTROL_STATE_DISABLED);
dt_pthread_mutex_init(&darktable.control->log_mutex, NULL);
}

// import default styles from shared directory
Expand Down Expand Up @@ -1737,8 +1730,7 @@ int dt_init(int argc, char *argv[], const gboolean init_gui, const gboolean load
darktable_splash_screen_set_progress(_("starting OpenCL"));
darktable.opencl = (dt_opencl_t *)calloc(1, sizeof(dt_opencl_t));
if(init_gui)
dt_control_add_job(darktable.control, DT_JOB_QUEUE_SYSTEM_BG,
_detect_opencl_job_create(exclude_opencl));
dt_control_add_job(DT_JOB_QUEUE_SYSTEM_BG, _detect_opencl_job_create(exclude_opencl));
else
dt_opencl_init(darktable.opencl, exclude_opencl, print_statistics);

Expand All @@ -1752,11 +1744,9 @@ int dt_init(int argc, char *argv[], const gboolean init_gui, const gboolean load

// must come before mipmap_cache, because that one will need to access
// image dimensions stored in here:
darktable.image_cache = (dt_image_cache_t *)calloc(1, sizeof(dt_image_cache_t));
dt_image_cache_init(darktable.image_cache);
dt_image_cache_init();

darktable.mipmap_cache = (dt_mipmap_cache_t *)calloc(1, sizeof(dt_mipmap_cache_t));
dt_mipmap_cache_init(darktable.mipmap_cache);
dt_mipmap_cache_init();

// set up the list of exiv2 metadata
dt_exif_set_exiv2_taglist();
Expand Down Expand Up @@ -1892,8 +1882,7 @@ int dt_init(int argc, char *argv[], const gboolean init_gui, const gboolean load
// fire up a background job to import them after switching to
// lighttable showing the filmroll for the first one
_switch_to_new_filmroll(argv[1]);
dt_control_add_job(darktable.control,
DT_JOB_QUEUE_USER_BG, dt_pathlist_import_create(argc,argv));
dt_control_add_job(DT_JOB_QUEUE_USER_BG, dt_pathlist_import_create(argc,argv));
}

// there might be some info created in dt_configure_runtime_performance() for feedback
Expand Down Expand Up @@ -2074,7 +2063,7 @@ void dt_cleanup()
5. After dt_control_shutdown() has finished we are sure there are no background threads running any
more so we can safely close all mentioned subsystems and continue.
*/
dt_control_shutdown(darktable.control);
dt_control_shutdown();
}
#ifdef USE_LUA
dt_lua_finalize();
Expand All @@ -2091,22 +2080,18 @@ void dt_cleanup()
dt_imageio_cleanup(darktable.imageio);
free(darktable.imageio);
darktable.imageio = NULL;
dt_control_cleanup(darktable.control);
free(darktable.control);
darktable.control = NULL;
dt_control_cleanup(TRUE);
dt_undo_cleanup(darktable.undo);
darktable.undo = NULL;
free(darktable.gui);
darktable.gui = NULL;
}
else
dt_control_cleanup(FALSE);

dt_image_cache_cleanup(darktable.image_cache);
free(darktable.image_cache);
darktable.image_cache = NULL;

dt_mipmap_cache_cleanup(darktable.mipmap_cache);
free(darktable.mipmap_cache);
darktable.mipmap_cache = NULL;
dt_image_cache_cleanup();
dt_mipmap_cache_cleanup();

dt_colorspaces_cleanup(darktable.color_profiles);
dt_conf_cleanup(darktable.conf);
Expand Down
4 changes: 2 additions & 2 deletions src/common/exif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2805,7 +2805,7 @@ int dt_exif_read_blob(uint8_t **buf,

// GPS data
_remove_exif_geotag(exifData);
const dt_image_t *cimg = dt_image_cache_get(darktable.image_cache, imgid, 'r');
const dt_image_t *cimg = dt_image_cache_get(imgid, 'r');
if(cimg && !std::isnan(cimg->geoloc.longitude) && !std::isnan(cimg->geoloc.latitude))
{
exifData["Exif.GPSInfo.GPSVersionID"] = "02 02 00 00";
Expand Down Expand Up @@ -2853,7 +2853,7 @@ int dt_exif_read_blob(uint8_t **buf,
if(g_strcmp0(&datetime[DT_DATETIME_EXIF_LENGTH], "000"))
exifData["Exif.Photo.SubSecTimeOriginal"] = &datetime[DT_DATETIME_EXIF_LENGTH];

dt_image_cache_read_release(darktable.image_cache, cimg);
dt_image_cache_read_release(cimg);
}

Exiv2::Blob blob;
Expand Down
6 changes: 3 additions & 3 deletions src/common/film.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ dt_filmid_t dt_film_import(const char *dirname)
"DELETE FROM main.selected_images", NULL, NULL, NULL);

// launch import job
dt_control_add_job(darktable.control, DT_JOB_QUEUE_USER_BG, dt_film_import1_create(film));
dt_control_add_job(DT_JOB_QUEUE_USER_BG, dt_film_import1_create(film));

return filmid;
}
Expand Down Expand Up @@ -487,8 +487,8 @@ void dt_film_remove(const dt_filmid_t id)
{
const dt_imgid_t imgid = sqlite3_column_int(stmt, 0);
dt_image_local_copy_reset(imgid);
dt_mipmap_cache_remove(darktable.mipmap_cache, imgid);
dt_image_cache_remove(darktable.image_cache, imgid);
dt_mipmap_cache_remove(imgid);
dt_image_cache_remove(imgid);
}
sqlite3_finalize(stmt);

Expand Down
4 changes: 2 additions & 2 deletions src/common/focus.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ static void dt_focus_draw_clusters(cairo_t *cr, int width, int height, dt_imgid_
cairo_save(cr);
cairo_translate(cr, width / 2.0, height / 2.0f);

const dt_image_t *img = dt_image_cache_get(darktable.image_cache, imgid, 'r');
const dt_image_t *img = dt_image_cache_get(imgid, 'r');
dt_image_t image = *img;
dt_image_cache_read_release(darktable.image_cache, img);
dt_image_cache_read_release(img);

// FIXME: get those from rawprepare IOP somehow !!!
int wd = buffer_width + image.crop_x;
Expand Down
4 changes: 2 additions & 2 deletions src/common/gimp.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ gboolean dt_export_gimp_file(const dt_imgid_t imgid)
printf("<<<gimp\n%s%s\n", path, thumb ? ".jpg" : ".exr");
if(thumb)
{
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'r');
dt_image_t *image = dt_image_cache_get(imgid, 'r');
printf("%i %i\n", image->width, image->height);
dt_image_cache_read_release(darktable.image_cache, image);
dt_image_cache_read_release(image);
}
printf("gimp>>>\n");
res = TRUE;
Expand Down
Loading

0 comments on commit 084e910

Please sign in to comment.