Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
dtpthread.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2016-2017 Peter Budai.
4 Copyright (C) 2016-2017 Roman Lebedev.
5 Copyright (C) 2017 Tobias Ellinghaus.
6 Copyright (C) 2019 Heiko Bauke.
7 Copyright (C) 2020 Pascal Obry.
8 Copyright (C) 2022 Martin Bařinka.
9 Copyright (C) 2025 Aurélien PIERRE.
10
11 darktable is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 darktable is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with darktable. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#define _GNU_SOURCE
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#include "control/conf.h"
32#include "common/fp_mode.h"
33
34#include <sched.h>
35#include <pthread.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <inttypes.h>
39#include <glib.h>
40
41#ifdef _WIN32
42#include "win/dtwin.h"
43#endif // _WIN32
44
45int dt_pthread_create(pthread_t *thread, void *(*start_routine)(void *), void *arg, const gboolean realtime)
46{
47 dt_fp_init(dt_conf_get_int("cpu_fp_mode"));
48
49 int ret;
50
51 pthread_attr_t attr;
52
53 ret = pthread_attr_init(&attr);
54 if(ret != 0)
55 {
56 fprintf(stderr, "[dt_pthread_create] error: pthread_attr_init() returned %i\n", ret);
57 return ret;
58 }
59
60 size_t stacksize;
61
62 ret = pthread_attr_getstacksize(&attr, &stacksize);
63
64 if(ret != 0)
65 {
66 fprintf(stderr, "[dt_pthread_create] error: pthread_attr_getstacksize() returned %i\n", ret);
67 }
68
69 if(ret != 0 || stacksize < WANTED_THREADS_STACK_SIZE /*|| 1*/)
70 {
71 // looks like we need to bump/set it...
72
73 fprintf(stderr, "[dt_pthread_create] info: bumping pthread's stacksize from %" G_GSIZE_FORMAT " to %"PRIuMAX"\n", stacksize,
74 (uintmax_t)WANTED_THREADS_STACK_SIZE);
75
76 ret = pthread_attr_setstacksize(&attr, WANTED_THREADS_STACK_SIZE);
77 if(ret != 0)
78 {
79 fprintf(stderr, "[dt_pthread_create] error: pthread_attr_setstacksize() returned %i\n", ret);
80 }
81 }
82
83 if(ret != 0 && realtime)
84 {
85 // Set SCHED_RR policy: realtime round robin
86 ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
87 if(ret != 0)
88 {
89 fprintf(stderr, "setschedpolicy: %s\n", strerror(ret));
90 exit(EXIT_FAILURE);
91 }
92
93 // Set thread priority (between 1 and 99)
94 struct sched_param param = { .sched_priority = 80 };
95 ret = pthread_attr_setschedparam(&attr, &param);
96 if(ret != 0)
97 {
98 fprintf(stderr, "setschedparam: %s\n", strerror(ret));
99 exit(EXIT_FAILURE);
100 }
101
102 // Make sure the thread uses the explicitly set policy and param
103 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
104 if(ret != 0)
105 {
106 fprintf(stderr, "setinheritsched: %s\n", strerror(ret));
107 exit(EXIT_FAILURE);
108 }
109 }
110
111 ret = pthread_create(thread, &attr, start_routine, arg);
112
113 pthread_attr_destroy(&attr);
114
115 return ret;
116}
117
118void dt_pthread_setname(const char *name)
119{
120#if defined __linux__
121 pthread_setname_np(pthread_self(), name);
122#elif defined __FreeBSD__ || defined __DragonFly__
123 // TODO: is this the right syntax?
124 // pthread_setname_np(pthread_self(), name, 0);
125#elif defined __NetBSD__
126 // TODO: is this the right syntax?
127 // pthread_setname_np(pthread_self(), name, NULL);
128#elif defined __OpenBSD__
129 // TODO: find out if there is pthread_setname_np() on OpenBSD and how to call it
130#elif defined __APPLE__
131 pthread_setname_np(name);
132#elif defined _WIN32
133 dtwin_set_thread_name((DWORD)-1, name);
134#endif
135}
136
137
138// clang-format off
139// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
140// vim: shiftwidth=2 expandtab tabstop=2 cindent
141// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
142// clang-format on
char * name
#define WANTED_THREADS_STACK_SIZE
int dt_conf_get_int(const char *name)
int dt_pthread_create(pthread_t *thread, void *(*start_routine)(void *), void *arg, const gboolean realtime)
Definition dtpthread.c:45
void dt_pthread_setname(const char *name)
Definition dtpthread.c:118
void dtwin_set_thread_name(DWORD dwThreadID, const char *threadName)
Definition dtwin.c:338
const float const float param