Skip to content

Commit

Permalink
Fix thread sanitizer warnings, add macro to disable stat counters
Browse files Browse the repository at this point in the history
Summary: This diff upstreams a change that fixes thread sanitizer warnings.

Differential Revision:
D62447578

Privacy Context Container: L1120261
  • Loading branch information
SanderMertens committed Sep 10, 2024
1 parent 549fc46 commit 7ac2a9f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
18 changes: 10 additions & 8 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -14284,7 +14284,7 @@ void flecs_observer_invoke(
bool match_this = query->flags & EcsQueryMatchThis;
if (match_this) {
callback(it);
query->eval_count ++;
ecs_os_inc(&query->eval_count);
} else {
ecs_entity_t observer_src = ECS_TERM_REF_ID(&term->src);
if (observer_src && !(term->src.id & EcsIsEntity)) {
Expand All @@ -14300,7 +14300,7 @@ void flecs_observer_invoke(
it->entities = &e;
if (!observer_src) {
callback(it);
query->eval_count ++;
ecs_os_inc(&query->eval_count);
} else if (observer_src == e) {
ecs_entity_t dummy = 0;
it->entities = &dummy;
Expand All @@ -14309,7 +14309,7 @@ void flecs_observer_invoke(
}

callback(it);
query->eval_count ++;
ecs_os_inc(&query->eval_count);
it->sources[0] = src;
break;
}
Expand Down Expand Up @@ -52131,9 +52131,11 @@ int32_t flecs_run_pipeline_ops(
ecs_system_t* sys = (ecs_system_t*)poly->poly;

/* Keep track of the last frame for which the system has ran, so we
* know from where to resume the schedule in case the schedule
* changes during a merge. */
sys->last_frame = world->info.frame_count_total + 1;
* know from where to resume the schedule in case the schedule
* changes during a merge. */
if (stage_index == 0) {
sys->last_frame = world->info.frame_count_total + 1;
}

ecs_stage_t* s = NULL;
if (!op->immediate) {
Expand All @@ -52145,7 +52147,7 @@ int32_t flecs_run_pipeline_ops(
flecs_run_intern(world, s, system, sys, stage_index,
stage_count, delta_time, NULL);

world->info.systems_ran_frame++;
ecs_os_linc(&world->info.systems_ran_frame);
ran_since_merge++;

if (ran_since_merge == op->count) {
Expand Down Expand Up @@ -69889,7 +69891,7 @@ ecs_iter_t ecs_query_iter(
}

/* Ok, only for stats */
ECS_CONST_CAST(ecs_query_t*, q)->eval_count ++;
ecs_os_linc(&ECS_CONST_CAST(ecs_query_t*, q)->eval_count);

ecs_query_impl_t *impl = flecs_query_impl(q);
ecs_query_cache_t *cache = impl->cache;
Expand Down
15 changes: 15 additions & 0 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@
*/
// #define FLECS_ACCURATE_COUNTERS

/** @def FLECS_DISABLE_COUNTERS
* Disables counters used for statistics. Improves performance, but
* will prevent some features that rely on statistics from working,
* like the statistics pages in the explorer.
*/
// #define FLECS_DISABLE_COUNTERS

/* Make sure provided configuration is valid */
#if defined(FLECS_DEBUG) && defined(FLECS_NDEBUG)
#error "invalid configuration: cannot both define FLECS_DEBUG and FLECS_NDEBUG"
Expand Down Expand Up @@ -2707,6 +2714,7 @@ void ecs_os_set_api_defaults(void);
#define ecs_os_now() ecs_os_api.now_()
#define ecs_os_get_time(time_out) ecs_os_api.get_time_(time_out)

#ifndef FLECS_DISABLE_COUNTERS
#ifdef FLECS_ACCURATE_COUNTERS
#define ecs_os_inc(v) (ecs_os_ainc(v))
#define ecs_os_linc(v) (ecs_os_lainc(v))
Expand All @@ -2718,6 +2726,13 @@ void ecs_os_set_api_defaults(void);
#define ecs_os_dec(v) (--(*v))
#define ecs_os_ldec(v) (--(*v))
#endif
#else
#define ecs_os_inc(v)
#define ecs_os_linc(v)
#define ecs_os_dec(v)
#define ecs_os_ldec(v)
#endif


#ifdef ECS_TARGET_MINGW
/* mingw bug: without this a conversion error is thrown, but isnan/isinf should
Expand Down
7 changes: 7 additions & 0 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
*/
// #define FLECS_ACCURATE_COUNTERS

/** @def FLECS_DISABLE_COUNTERS
* Disables counters used for statistics. Improves performance, but
* will prevent some features that rely on statistics from working,
* like the statistics pages in the explorer.
*/
// #define FLECS_DISABLE_COUNTERS

/* Make sure provided configuration is valid */
#if defined(FLECS_DEBUG) && defined(FLECS_NDEBUG)
#error "invalid configuration: cannot both define FLECS_DEBUG and FLECS_NDEBUG"
Expand Down
8 changes: 8 additions & 0 deletions include/flecs/os_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ void ecs_os_set_api_defaults(void);
#define ecs_os_now() ecs_os_api.now_()
#define ecs_os_get_time(time_out) ecs_os_api.get_time_(time_out)

#ifndef FLECS_DISABLE_COUNTERS
#ifdef FLECS_ACCURATE_COUNTERS
#define ecs_os_inc(v) (ecs_os_ainc(v))
#define ecs_os_linc(v) (ecs_os_lainc(v))
Expand All @@ -528,6 +529,13 @@ void ecs_os_set_api_defaults(void);
#define ecs_os_dec(v) (--(*v))
#define ecs_os_ldec(v) (--(*v))
#endif
#else
#define ecs_os_inc(v)
#define ecs_os_linc(v)
#define ecs_os_dec(v)
#define ecs_os_ldec(v)
#endif


#ifdef ECS_TARGET_MINGW
/* mingw bug: without this a conversion error is thrown, but isnan/isinf should
Expand Down
10 changes: 6 additions & 4 deletions src/addons/pipeline/pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,11 @@ int32_t flecs_run_pipeline_ops(
ecs_system_t* sys = (ecs_system_t*)poly->poly;

/* Keep track of the last frame for which the system has ran, so we
* know from where to resume the schedule in case the schedule
* changes during a merge. */
sys->last_frame = world->info.frame_count_total + 1;
* know from where to resume the schedule in case the schedule
* changes during a merge. */
if (stage_index == 0) {
sys->last_frame = world->info.frame_count_total + 1;
}

ecs_stage_t* s = NULL;
if (!op->immediate) {
Expand All @@ -574,7 +576,7 @@ int32_t flecs_run_pipeline_ops(
flecs_run_intern(world, s, system, sys, stage_index,
stage_count, delta_time, NULL);

world->info.systems_ran_frame++;
ecs_os_linc(&world->info.systems_ran_frame);
ran_since_merge++;

if (ran_since_merge == op->count) {
Expand Down
6 changes: 3 additions & 3 deletions src/observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ void flecs_observer_invoke(
bool match_this = query->flags & EcsQueryMatchThis;
if (match_this) {
callback(it);
query->eval_count ++;
ecs_os_inc(&query->eval_count);
} else {
ecs_entity_t observer_src = ECS_TERM_REF_ID(&term->src);
if (observer_src && !(term->src.id & EcsIsEntity)) {
Expand All @@ -330,7 +330,7 @@ void flecs_observer_invoke(
it->entities = &e;
if (!observer_src) {
callback(it);
query->eval_count ++;
ecs_os_inc(&query->eval_count);
} else if (observer_src == e) {
ecs_entity_t dummy = 0;
it->entities = &dummy;
Expand All @@ -339,7 +339,7 @@ void flecs_observer_invoke(
}

callback(it);
query->eval_count ++;
ecs_os_inc(&query->eval_count);
it->sources[0] = src;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/query/engine/eval_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ ecs_iter_t ecs_query_iter(
}

/* Ok, only for stats */
ECS_CONST_CAST(ecs_query_t*, q)->eval_count ++;
ecs_os_linc(&ECS_CONST_CAST(ecs_query_t*, q)->eval_count);

ecs_query_impl_t *impl = flecs_query_impl(q);
ecs_query_cache_t *cache = impl->cache;
Expand Down

0 comments on commit 7ac2a9f

Please sign in to comment.