Skip to content

Commit

Permalink
Add dt_pthread_join()
Browse files Browse the repository at this point in the history
Including print to logs if there was an error.
  • Loading branch information
jenshannoschwalm committed Dec 14, 2024
1 parent 09a3d4f commit 586ba93
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/common/camera_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ void dt_camctl_camera_stop_live_view(const dt_camctl_t *c)
}
dt_print(DT_DEBUG_CAMCTL, "[camera_control] Stopping live view");
cam->is_live_viewing = FALSE;
pthread_join(cam->live_view_thread, NULL);
dt_pthread_join(cam->live_view_thread);
// tell camera to get back to normal state (close mirror)
dt_camctl_camera_set_property_int(camctl, NULL, "eosviewfinder", 0);
dt_camctl_camera_set_property_int(camctl, NULL, "viewfinder", 0);
Expand Down
26 changes: 15 additions & 11 deletions src/common/dtpthread.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of darktable,
Copyright (C) 2016-2023 darktable developers.
Copyright (C) 2016-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 @@ -31,6 +31,8 @@
#include "win/dtwin.h"
#endif // _WIN32

#include "common/dtpthread.h"

int dt_pthread_create(pthread_t *thread, void *(*start_routine)(void *), void *arg)
{
int ret = 0;
Expand All @@ -40,7 +42,7 @@ int dt_pthread_create(pthread_t *thread, void *(*start_routine)(void *), void *a
ret = pthread_attr_init(&attr);
if(ret != 0)
{
fprintf(stderr, "[dt_pthread_create] error: pthread_attr_init() returned %i\n", ret);
fprintf(stderr, "[dt_pthread_create] error: pthread_attr_init() returned %s\n", _pthread_ret_mess(ret));
return ret;
}

Expand All @@ -49,27 +51,21 @@ int dt_pthread_create(pthread_t *thread, void *(*start_routine)(void *), void *a
ret = pthread_attr_getstacksize(&attr, &stacksize);

if(ret != 0)
{
fprintf(stderr, "[dt_pthread_create] error: pthread_attr_getstacksize() returned %i\n", ret);
}
fprintf(stderr, "[dt_pthread_create] error: pthread_attr_getstacksize() returned %s\n", _pthread_ret_mess(ret));

if(ret != 0 || stacksize < WANTED_THREADS_STACK_SIZE)
{
// looks like we need to bump/set it...
ret = pthread_attr_setstacksize(&attr, WANTED_THREADS_STACK_SIZE);
if(ret != 0)
{
fprintf(stderr, "[dt_pthread_create] error: pthread_attr_setstacksize() returned %i\n", ret);
}
fprintf(stderr, "[dt_pthread_create] error: pthread_attr_setstacksize() returned %s\n", _pthread_ret_mess(ret));
}

if(ret == 0)
ret = pthread_create(thread, &attr, start_routine, arg);

if(ret != 0)
{
fprintf(stderr, "[dt_pthread_create] error: pthread_create() returned %i\n", ret);
}
fprintf(stderr, "[dt_pthread_create] error: pthread_create() returned %s\n", _pthread_ret_mess(ret));

pthread_attr_destroy(&attr);

Expand All @@ -95,6 +91,14 @@ void dt_pthread_setname(const char *name)
#endif
}

int dt_pthread_join(pthread_t thread)
{
const int ret = pthread_join(thread, NULL);
if(ret != 0)
fprintf(stderr, "[dt_pthread_join] error: %s\n", _pthread_ret_mess(ret));
return ret;
}


// clang-format off
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
Expand Down
2 changes: 1 addition & 1 deletion src/common/dtpthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ static inline int dt_pthread_mutex_BAD_unlock(dt_pthread_mutex_t *mutex)
}

int dt_pthread_create(pthread_t *thread, void *(*start_routine)(void *), void *arg);

int dt_pthread_join(pthread_t thread);
void dt_pthread_setname(const char *name);

// clang-format off
Expand Down
8 changes: 4 additions & 4 deletions src/control/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,17 @@ void dt_control_shutdown(dt_control_t *control)

#ifdef HAVE_GPHOTO2
/* first and always wait for gphoto device updater */
pthread_join(control->update_gphoto_thread, NULL);
dt_pthread_join(control->update_gphoto_thread);
#endif

/* wait for kick_on_workers_thread */
pthread_join(control->kick_on_workers_thread, NULL);
dt_pthread_join(control->kick_on_workers_thread);

for(int k = 0; k < control->num_threads-1; k++)
pthread_join(control->thread[k], NULL);
dt_pthread_join(control->thread[k]);

for(int k = 0; k < DT_CTL_WORKER_RESERVED; k++)
pthread_join(control->thread_res[k], NULL);
dt_pthread_join(control->thread_res[k]);
}

void dt_control_cleanup(dt_control_t *control)
Expand Down

0 comments on commit 586ba93

Please sign in to comment.