Skip to content

Commit

Permalink
pthreads maintenance
Browse files Browse the repository at this point in the history
1. Implement a set of dt_pthread_rwlock_??? inline functions with improved logs for debug builds
   and release builds with defined MUTEX_REPORTING
2. Implement dt_pthread_join also for improved logs
3. Write logs to stdout and ensure the logs are flushed for correct output in logfiles
4. Some readability improved assert()
  • Loading branch information
jenshannoschwalm committed Jan 4, 2025
1 parent e932ddb commit c14f0fe
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 101 deletions.
47 changes: 29 additions & 18 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,48 +31,61 @@
#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;

pthread_attr_t attr;

ret = pthread_attr_init(&attr);
if(ret != 0)
{
fprintf(stderr, "[dt_pthread_create] error: pthread_attr_init() returned %i\n", ret);
return ret;
printf("[dt_pthread_create] error: pthread_attr_init() returned %s\n", _pthread_ret_mess(ret));
fflush(stdout);
}
assert(ret == 0);

size_t stacksize;

ret = pthread_attr_getstacksize(&attr, &stacksize);

if(ret != 0)
{
fprintf(stderr, "[dt_pthread_create] error: pthread_attr_getstacksize() returned %i\n", 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);
printf("[dt_pthread_create] error: pthread_attr_setstacksize() returned %s\n", _pthread_ret_mess(ret));
fflush(stdout);
}
}
assert(ret == 0);

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

ret = pthread_create(thread, &attr, start_routine, arg);
if(ret != 0)
{
fprintf(stderr, "[dt_pthread_create] error: pthread_create() returned %i\n", ret);
printf("[dt_pthread_create] error: pthread_create() returned %s\n", _pthread_ret_mess(ret));
fflush(stdout);
}

pthread_attr_destroy(&attr);
assert(ret == 0);
return ret;
}

int dt_pthread_join(pthread_t thread)
{
#if defined(MUTEX_REPORTING) && ( defined(__linux__) || defined(__APPLE__) )
char name[256] = { "???" };
pthread_getname_np(thread, name, sizeof(name));
const int ret = pthread_join(thread, NULL);
printf("[dt_pthread_join] '%s' returned %s\n",
name, _pthread_ret_mess(ret));
fflush(stdout);
#else
const int ret = pthread_join(thread, NULL);
#endif

assert(ret == 0);
return ret;
}

Expand All @@ -94,8 +107,6 @@ void dt_pthread_setname(const char *name)
dtwin_set_thread_name((DWORD)-1, name);
#endif
}


// clang-format off
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
// vim: shiftwidth=2 expandtab tabstop=2 cindent
Expand Down
Loading

0 comments on commit c14f0fe

Please sign in to comment.