infra: improve tatomic debugging

This commit is contained in:
Thomas Schoebel-Theuer 2013-04-12 10:55:51 +02:00
parent af0c99abf1
commit d1abe646a6
2 changed files with 13 additions and 11 deletions

View File

@ -24,8 +24,9 @@ typedef struct tatomic {
typedef struct atomic_trace { typedef struct atomic_trace {
#ifdef ATOMIC_DEBUGGING #ifdef ATOMIC_DEBUGGING
atomic_t at_count; atomic_t at_count;
short at_lines[ATOMIC_MAXTRACE]; short at_lines[ATOMIC_MAXTRACE];
const char *at_sources[ATOMIC_MAXTRACE];
#endif #endif
} atomic_trace_t; } atomic_trace_t;
@ -34,8 +35,9 @@ typedef struct atomic_trace {
#define tatomic_trace(_at, _cmd) \ #define tatomic_trace(_at, _cmd) \
({ \ ({ \
int _index = atomic_add_return(1, &(_at)->at_count) - 1; \ int _index = atomic_add_return(1, &(_at)->at_count) - 1; \
if (_index >= 0 && _index < ATOMIC_MAXTRACE) { \ if (likely(_index >= 0 && _index < ATOMIC_MAXTRACE)) { \
(_at)->at_lines[_index] = __LINE__; \ (_at)->at_lines[_index] = __LINE__; \
(_at)->at_sources[_index] = __BASE_FILE__; \
} \ } \
_cmd; \ _cmd; \
}) })
@ -45,19 +47,19 @@ typedef struct atomic_trace {
int __i; \ int __i; \
int __max = atomic_read(&(_at)->at_count); \ int __max = atomic_read(&(_at)->at_count); \
_MSG("at_count = %d\n", __max); \ _MSG("at_count = %d\n", __max); \
if (__max > ATOMIC_MAXTRACE) \ if (unlikely(__max > ATOMIC_MAXTRACE)) \
__max = ATOMIC_MAXTRACE; \ __max = ATOMIC_MAXTRACE; \
for (__i = 0; __i < __max; __i++) { \ for (__i = 0; __i < __max; __i++) { \
_MSG("%2d %4d\n", __i, (_at)->at_lines[__i]); \ _MSG("%2d %s:%d\n", __i, (_at)->at_sources[__i], (_at)->at_lines[__i]); \
} \ } \
}) })
#define _CHECK_TATOMIC(_at,_atom,OP,_minval) \ #define _CHECK_TATOMIC(_at,_atom,OP,_minval,_fixval) \
do { \ do { \
if (BRICK_CHECKING) { \ if (BRICK_CHECKING) { \
int __test = atomic_read(&(_atom)->ta_atomic); \ int __test = atomic_read(&(_atom)->ta_atomic); \
if (__test OP (_minval)) { \ if (unlikely(__test OP (_minval))) { \
atomic_set(&(_atom)->ta_atomic, _minval); \ atomic_set(&(_atom)->ta_atomic, _fixval); \
BRICK_ERR("%d: tatomic " #_atom " " #OP " " #_minval " (%d)\n", __LINE__, __test); \ BRICK_ERR("%d: tatomic " #_atom " " #OP " " #_minval " (%d)\n", __LINE__, __test); \
tatomic_out(_at, BRICK_DMP); \ tatomic_out(_at, BRICK_DMP); \
} \ } \
@ -68,13 +70,13 @@ do { \
#define tatomic_trace(_at,_cmd) _cmd #define tatomic_trace(_at,_cmd) _cmd
#define _CHECK_TATOMIC(_at,_atom,OP,_minval) \ #define _CHECK_TATOMIC(_at,_atom,OP,_minval,_fixval) \
_CHECK_ATOMIC(&(_atom)->ta_atomic, OP, _minval) _CHECK_ATOMIC(&(_atom)->ta_atomic, OP, _minval)
#endif #endif
#define CHECK_TATOMIC(_at,_atom,_minval) \ #define CHECK_TATOMIC(_at,_atom,_minval) \
_CHECK_TATOMIC(_at, _atom, <, _minval) _CHECK_TATOMIC(_at, _atom, <, _minval, _minval)
#define tatomic_inc(at,a) tatomic_trace(at, atomic_inc(&(a)->ta_atomic)) #define tatomic_inc(at,a) tatomic_trace(at, atomic_inc(&(a)->ta_atomic))
#define tatomic_dec(at,a) tatomic_trace(at, atomic_dec(&(a)->ta_atomic)) #define tatomic_dec(at,a) tatomic_trace(at, atomic_dec(&(a)->ta_atomic))

View File

@ -143,7 +143,7 @@ struct mref_object {
if (unlikely(BRICK_CHECKING && (mref)->ref_initialized)) { \ if (unlikely(BRICK_CHECKING && (mref)->ref_initialized)) { \
MARS_ERR("mref %p is already initialized\n", (mref)); \ MARS_ERR("mref %p is already initialized\n", (mref)); \
} \ } \
_CHECK_TATOMIC(&(mref)->ref_at, &(mref)->ref_count, !=, 0); \ _CHECK_TATOMIC(&(mref)->ref_at, &(mref)->ref_count, !=, 0, 0); \
(mref)->ref_initialized = true; \ (mref)->ref_initialized = true; \
tatomic_inc(&(mref)->ref_at, &(mref)->ref_count); \ tatomic_inc(&(mref)->ref_at, &(mref)->ref_count); \
}) })