forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtpthread.h
More file actions
189 lines (166 loc) · 6.24 KB
/
Copy pathdtpthread.h
File metadata and controls
189 lines (166 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
This file is part of darktable,
copyright (c) 2009--2010 johannes hanika.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DT_PTHREAD_H_
#define DT_PTHREAD_H_
#include <pthread.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#ifdef _DEBUG
// copied from darktable.h so we don't need to include the header
#include <sys/time.h>
static inline double dt_pthread_get_wtime()
{
struct timeval time;
gettimeofday(&time, NULL);
return time.tv_sec - 1290608000 + (1.0/1000000.0)*time.tv_usec;
}
#define TOPN 3
typedef struct dt_pthread_mutex_t
{
pthread_mutex_t mutex;
char name[256];
double time_locked;
double time_sum_wait;
double time_sum_locked;
char top_locked_name[TOPN][256];
double top_locked_sum[TOPN];
char top_wait_name[TOPN][256];
double top_wait_sum[TOPN];
}
dt_pthread_mutex_t;
static inline int
dt_pthread_mutex_destroy(dt_pthread_mutex_t *mutex)
{
const int ret = pthread_mutex_destroy(&(mutex->mutex));
//printf("\n[mutex] stats for mutex `%s':\n", mutex->name);
//printf("[mutex] total time locked: %.3f secs\n", mutex->time_sum_locked);
//printf("[mutex] total wait time : %.3f secs\n", mutex->time_sum_wait);
//printf("[mutex] top %d lockers :\n", TOPN);
//for(int k=0; k<TOPN; k++) printf("[mutex] %.3f secs : `%s'\n", mutex->top_locked_sum[k], mutex->top_locked_name[k]);
//printf("[mutex] top %d waiters :\n", TOPN);
//for(int k=0; k<TOPN; k++) printf("[mutex] %.3f secs : `%s'\n", mutex->top_wait_sum[k], mutex->top_wait_name[k]);
return ret;
}
#define dt_pthread_mutex_init(A, B) dt_pthread_mutex_init_with_caller(A, B, __FILE__, __LINE__, __FUNCTION__)
static inline int
dt_pthread_mutex_init_with_caller(dt_pthread_mutex_t *mutex, const pthread_mutexattr_t *attr, const char *file, const int line, const char *function)
{
memset(mutex, 0x0, sizeof(dt_pthread_mutex_t));
snprintf(mutex->name, 256, "%s:%d (%s)", file, line, function);
const int ret = pthread_mutex_init(&(mutex->mutex), attr);
return ret;
}
#define dt_pthread_mutex_lock(A) dt_pthread_mutex_lock_with_caller(A, __FILE__, __LINE__, __FUNCTION__)
static inline int
dt_pthread_mutex_lock_with_caller(dt_pthread_mutex_t *mutex, const char *file, const int line, const char *function)
{
const double t0 = dt_pthread_get_wtime();
const int ret = pthread_mutex_lock(&(mutex->mutex));
mutex->time_locked = dt_pthread_get_wtime();
double wait = mutex->time_locked - t0;
mutex->time_sum_wait += wait;
char name[256];
snprintf(name, 256, "%s:%d (%s)", file, line, function);
int min_wait_slot = 0;
for(int k=0; k<TOPN; k++)
{
if(mutex->top_wait_sum[k] < mutex->top_wait_sum[min_wait_slot]) min_wait_slot = k;
if(!strncmp(name, mutex->top_wait_name[k], 256))
{
mutex->top_wait_sum[k] += wait;
return ret;
}
}
g_strlcpy(mutex->top_wait_name[min_wait_slot], name, 256);
mutex->top_wait_sum[min_wait_slot] = wait;
return ret;
}
#define dt_pthread_mutex_trylock(A) dt_pthread_mutex_trylock_with_caller(A, __FILE__, __LINE__, __FUNCTION__)
static inline int
dt_pthread_mutex_trylock_with_caller(dt_pthread_mutex_t *mutex, const char *file, const int line, const char *function)
{
const double t0 = dt_pthread_get_wtime();
const int ret = pthread_mutex_trylock(&(mutex->mutex));
if(ret) return ret;
mutex->time_locked = dt_pthread_get_wtime();
double wait = mutex->time_locked - t0;
mutex->time_sum_wait += wait;
char name[256];
snprintf(name, 256, "%s:%d (%s)", file, line, function);
int min_wait_slot = 0;
for(int k=0; k<TOPN; k++)
{
if(mutex->top_wait_sum[k] < mutex->top_wait_sum[min_wait_slot]) min_wait_slot = k;
if(!strncmp(name, mutex->top_wait_name[k], 256))
{
mutex->top_wait_sum[k] += wait;
return ret;
}
}
g_strlcpy(mutex->top_wait_name[min_wait_slot], name, 256);
mutex->top_wait_sum[min_wait_slot] = wait;
return ret;
}
#define dt_pthread_mutex_unlock(A) dt_pthread_mutex_unlock_with_caller(A, __FILE__, __LINE__, __FUNCTION__)
static inline int
dt_pthread_mutex_unlock_with_caller(dt_pthread_mutex_t *mutex, const char *file, const int line, const char *function)
{
const double t0 = dt_pthread_get_wtime();
const double locked = t0 - mutex->time_locked;
mutex->time_sum_locked += locked;
char name[256];
snprintf(name, 256, "%s:%d (%s)", file, line, function);
int min_locked_slot = 0;
for(int k=0; k<TOPN; k++)
{
if(mutex->top_locked_sum[k] < mutex->top_locked_sum[min_locked_slot]) min_locked_slot = k;
if(!strncmp(name, mutex->top_locked_name[k], 256))
{
mutex->top_locked_sum[k] += locked;
min_locked_slot = -1;
break;
}
}
if(min_locked_slot >= 0)
{
g_strlcpy(mutex->top_locked_name[min_locked_slot], name, 256);
mutex->top_locked_sum[min_locked_slot] = locked;
}
// need to unlock last, to shield our internal data.
const int ret = pthread_mutex_unlock(&(mutex->mutex));
return ret;
}
static inline int
dt_pthread_cond_wait(pthread_cond_t *cond, dt_pthread_mutex_t *mutex)
{
return pthread_cond_wait(cond, &(mutex->mutex));
}
#undef TOPN
#else
#define dt_pthread_mutex_t pthread_mutex_t
#define dt_pthread_mutex_destroy pthread_mutex_destroy
#define dt_pthread_mutex_init pthread_mutex_init
#define dt_pthread_mutex_lock pthread_mutex_lock
#define dt_pthread_mutex_trylock pthread_mutex_trylock
#define dt_pthread_mutex_unlock pthread_mutex_unlock
#define dt_pthread_cond_wait pthread_cond_wait
#endif
#endif
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;