@@ -55,8 +55,9 @@ static inline uint64_t timediff(const struct timespec start,
55
55
return stop .tv_nsec + nsecs - start .tv_nsec ;
56
56
}
57
57
58
- #define TRACE_INIT (M ) \
58
+ #define TRACE_INIT (M , N ) \
59
59
do { \
60
+ (M)->name = qd_strdup((N)); \
60
61
(M)->nsec_wait = 0; \
61
62
(M)->lock_count = 0; \
62
63
(M)->max_wait[0] = 0; \
@@ -65,15 +66,19 @@ static inline uint64_t timediff(const struct timespec start,
65
66
} while (0);
66
67
67
68
#define TRACE_DUMP (M ) \
68
- if ((M)->lock_count) { \
69
- fprintf(stderr, \
70
- "%30s: total(nsec)= %-10"PRIu64 \
71
- " avg= %-10"PRIu64" count= %-6"PRIu64 \
72
- " max= %"PRIu64" %"PRIu64" %"PRIu64"\n", \
73
- (M)->name, (M)->nsec_wait, \
74
- (M)->nsec_wait/(M)->lock_count, (M)->lock_count, \
75
- (M)->max_wait[0], (M)->max_wait[1], (M)->max_wait[2]); \
76
- }
69
+ do { \
70
+ if ((M)->lock_count) { \
71
+ fprintf(stderr, \
72
+ "%30s: total(nsec)= %-10"PRIu64 \
73
+ " avg= %-10"PRIu64" count= %-6"PRIu64 \
74
+ " max= %"PRIu64" %"PRIu64" %"PRIu64"\n", \
75
+ (M)->name, (M)->nsec_wait, \
76
+ (M)->nsec_wait/(M)->lock_count, (M)->lock_count, \
77
+ (M)->max_wait[0], (M)->max_wait[1], \
78
+ (M)->max_wait[2]); \
79
+ } \
80
+ free((M)->name); \
81
+ } while (0)
77
82
78
83
#define TRACE_UPDATE (M ,N ) \
79
84
if ((N) > 1000) { \
@@ -96,15 +101,15 @@ static inline uint64_t timediff(const struct timespec start,
96
101
}
97
102
98
103
#else
99
- #define TRACE_INIT (M )
104
+ #define TRACE_INIT (M , N ) (void)(N )
100
105
#define TRACE_DUMP (M )
101
106
#endif
102
107
103
108
104
109
struct sys_mutex_t {
105
110
pthread_mutex_t mutex ;
106
- char * name ;
107
111
#ifdef QD_TRACE_LOCK_CONTENTION
112
+ char * name ;
108
113
uint64_t nsec_wait ;
109
114
uint64_t lock_count ;
110
115
uint64_t max_wait [3 ];
@@ -117,10 +122,9 @@ sys_mutex_t *sys_mutex(const char *name)
117
122
sys_mutex_t * mutex = 0 ;
118
123
NEW_CACHE_ALIGNED (sys_mutex_t , mutex );
119
124
_CHECK (mutex != 0 );
120
- mutex -> name = qd_strdup (name );
121
125
int result = pthread_mutex_init (& (mutex -> mutex ), 0 );
122
126
_CHECK (result == 0 );
123
- TRACE_INIT (mutex );
127
+ TRACE_INIT (mutex , name );
124
128
return mutex ;
125
129
}
126
130
@@ -130,7 +134,6 @@ void sys_mutex_free(sys_mutex_t *mutex)
130
134
int result = pthread_mutex_destroy (& (mutex -> mutex ));
131
135
_CHECK (result == 0 );
132
136
TRACE_DUMP (mutex );
133
- free (mutex -> name );
134
137
free (mutex );
135
138
}
136
139
@@ -206,8 +209,8 @@ void sys_cond_signal_all(sys_cond_t *cond)
206
209
207
210
struct sys_rwlock_t {
208
211
pthread_rwlock_t lock ;
209
- char * name ;
210
212
#ifdef QD_TRACE_LOCK_CONTENTION
213
+ char * name ;
211
214
uint64_t nsec_wait ;
212
215
uint64_t lock_count ;
213
216
uint64_t max_wait [3 ];
@@ -218,10 +221,9 @@ struct sys_rwlock_t {
218
221
sys_rwlock_t * sys_rwlock (const char * name )
219
222
{
220
223
sys_rwlock_t * lock = NEW (sys_rwlock_t );
221
- lock -> name = qd_strdup (name );
222
224
int result = pthread_rwlock_init (& (lock -> lock ), 0 );
223
225
_CHECK (result == 0 );
224
- TRACE_INIT (lock );
226
+ TRACE_INIT (lock , name );
225
227
return lock ;
226
228
}
227
229
@@ -231,7 +233,6 @@ void sys_rwlock_free(sys_rwlock_t *lock)
231
233
int result = pthread_rwlock_destroy (& (lock -> lock ));
232
234
_CHECK (result == 0 );
233
235
TRACE_DUMP (lock );
234
- free (lock -> name );
235
236
free (lock );
236
237
}
237
238
0 commit comments