planner: avoid starting unneeded variation thread

When updating the dive profile, a thread is started to calculate
plan-variations. This is done even when only editing the profile
or when variation calculation is disabled by the user. The thread
then exits if it shouldn't calculate the variations.

Turn this around: test whether variations should be calculated
before starting the thread.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-02-28 17:28:09 +01:00 committed by Dirk Hohndel
parent 0f8560276d
commit a0f6b4d0b4

View file

@ -1037,6 +1037,11 @@ void DivePlannerPointsModel::createTemporaryPlan()
#endif #endif
} }
static bool shouldComputeVariations()
{
return prefs.display_variations && decoMode(true) != RECREATIONAL;
}
void DivePlannerPointsModel::updateDiveProfile() void DivePlannerPointsModel::updateDiveProfile()
{ {
createTemporaryPlan(); createTemporaryPlan();
@ -1046,25 +1051,27 @@ void DivePlannerPointsModel::updateDiveProfile()
struct deco_state *cache = NULL; struct deco_state *cache = NULL;
struct decostop stoptable[60]; struct decostop stoptable[60];
struct deco_state plan_deco_state; struct deco_state plan_deco_state;
struct diveplan *plan_copy;
memset(&plan_deco_state, 0, sizeof(struct deco_state)); memset(&plan_deco_state, 0, sizeof(struct deco_state));
plan(&plan_deco_state, &diveplan, d, DECOTIMESTEP, stoptable, &cache, isPlanner(), false); plan(&plan_deco_state, &diveplan, d, DECOTIMESTEP, stoptable, &cache, isPlanner(), false);
updateMaxDepth(); updateMaxDepth();
plan_copy = (struct diveplan *)malloc(sizeof(struct diveplan));
lock_planner(); if (isPlanner() && shouldComputeVariations()) {
cloneDiveplan(&diveplan, plan_copy); struct diveplan *plan_copy = (struct diveplan *)malloc(sizeof(struct diveplan));
unlock_planner(); lock_planner();
cloneDiveplan(&diveplan, plan_copy);
unlock_planner();
#ifdef VARIATIONS_IN_BACKGROUND #ifdef VARIATIONS_IN_BACKGROUND
// Since we're calling computeVariations asynchronously and plan_deco_state is allocated // Since we're calling computeVariations asynchronously and plan_deco_state is allocated
// on the stack, it must be copied and freed by the worker-thread. // on the stack, it must be copied and freed by the worker-thread.
struct deco_state *plan_deco_state_copy = new deco_state(plan_deco_state); struct deco_state *plan_deco_state_copy = new deco_state(plan_deco_state);
QtConcurrent::run(this, &DivePlannerPointsModel::computeVariationsFreeDeco, plan_copy, plan_deco_state_copy); QtConcurrent::run(this, &DivePlannerPointsModel::computeVariationsFreeDeco, plan_copy, plan_deco_state_copy);
#else #else
computeVariations(plan_copy, &plan_deco_state); computeVariations(plan_copy, &plan_deco_state);
#endif #endif
final_deco_state = plan_deco_state; final_deco_state = plan_deco_state;
emit calculatedPlanNotes(QString(d->notes)); emit calculatedPlanNotes(QString(d->notes));
}
// throw away the cache // throw away the cache
free(cache); free(cache);
@ -1164,77 +1171,75 @@ void DivePlannerPointsModel::computeVariations(struct diveplan *original_plan, c
struct divedatapoint *last_segment; struct divedatapoint *last_segment;
struct deco_state ds = *previous_ds; struct deco_state ds = *previous_ds;
if (isPlanner() && prefs.display_variations && decoMode(true) != RECREATIONAL) { int my_instance = ++instanceCounter;
int my_instance = ++instanceCounter; cache_deco_state(&ds, &save);
cache_deco_state(&ds, &save);
duration_t delta_time = { .seconds = 60 }; duration_t delta_time = { .seconds = 60 };
QString time_units = tr("min"); QString time_units = tr("min");
depth_t delta_depth; depth_t delta_depth;
QString depth_units; QString depth_units;
if (prefs.units.length == units::METERS) { if (prefs.units.length == units::METERS) {
delta_depth.mm = 1000; // 1m delta_depth.mm = 1000; // 1m
depth_units = tr("m"); depth_units = tr("m");
} else { } else {
delta_depth.mm = feet_to_mm(1.0); // 1ft delta_depth.mm = feet_to_mm(1.0); // 1ft
depth_units = tr("ft"); depth_units = tr("ft");
}
last_segment = cloneDiveplan(original_plan, &plan_copy);
if (!last_segment)
goto finish;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, original, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
last_segment = cloneDiveplan(original_plan, &plan_copy);
last_segment->depth.mm += delta_depth.mm;
last_segment->next->depth.mm += delta_depth.mm;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, deeper, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
last_segment = cloneDiveplan(original_plan, &plan_copy);
last_segment->depth.mm -= delta_depth.mm;
last_segment->next->depth.mm -= delta_depth.mm;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, shallower, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
last_segment = cloneDiveplan(original_plan, &plan_copy);
last_segment->next->time += delta_time.seconds;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, longer, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
last_segment = cloneDiveplan(original_plan, &plan_copy);
last_segment->next->time -= delta_time.seconds;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, shorter, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
char buf[200];
sprintf(buf, ", %s: + %d:%02d /%s + %d:%02d /min", qPrintable(tr("Stop times")),
FRACTION(analyzeVariations(shallower, original, deeper, qPrintable(depth_units)), 60), qPrintable(depth_units),
FRACTION(analyzeVariations(shorter, original, longer, qPrintable(time_units)), 60));
// By using a signal, we can transport the variations to the main thread.
emit variationsComputed(QString(buf));
#ifdef DEBUG_STOPVAR
printf("\n\n");
#endif
} }
last_segment = cloneDiveplan(original_plan, &plan_copy);
if (!last_segment)
goto finish;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, original, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
last_segment = cloneDiveplan(original_plan, &plan_copy);
last_segment->depth.mm += delta_depth.mm;
last_segment->next->depth.mm += delta_depth.mm;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, deeper, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
last_segment = cloneDiveplan(original_plan, &plan_copy);
last_segment->depth.mm -= delta_depth.mm;
last_segment->next->depth.mm -= delta_depth.mm;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, shallower, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
last_segment = cloneDiveplan(original_plan, &plan_copy);
last_segment->next->time += delta_time.seconds;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, longer, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
last_segment = cloneDiveplan(original_plan, &plan_copy);
last_segment->next->time -= delta_time.seconds;
if (my_instance != instanceCounter)
goto finish;
plan(&ds, &plan_copy, dive, 1, shorter, &cache, true, false);
free_dps(&plan_copy);
restore_deco_state(save, &ds, false);
char buf[200];
sprintf(buf, ", %s: + %d:%02d /%s + %d:%02d /min", qPrintable(tr("Stop times")),
FRACTION(analyzeVariations(shallower, original, deeper, qPrintable(depth_units)), 60), qPrintable(depth_units),
FRACTION(analyzeVariations(shorter, original, longer, qPrintable(time_units)), 60));
// By using a signal, we can transport the variations to the main thread.
emit variationsComputed(QString(buf));
#ifdef DEBUG_STOPVAR
printf("\n\n");
#endif
finish: finish:
free_dps(original_plan); free_dps(original_plan);
free(original_plan); free(original_plan);
@ -1261,12 +1266,15 @@ void DivePlannerPointsModel::createPlan(bool replanCopy)
//TODO: C-based function here? //TODO: C-based function here?
struct decostop stoptable[60]; struct decostop stoptable[60];
plan(&ds_after_previous_dives, &diveplan, d, DECOTIMESTEP, stoptable, &cache, isPlanner(), true); plan(&ds_after_previous_dives, &diveplan, d, DECOTIMESTEP, stoptable, &cache, isPlanner(), true);
struct diveplan *plan_copy;
plan_copy = (struct diveplan *)malloc(sizeof(struct diveplan)); if (shouldComputeVariations()) {
lock_planner(); struct diveplan *plan_copy;
cloneDiveplan(&diveplan, plan_copy); plan_copy = (struct diveplan *)malloc(sizeof(struct diveplan));
unlock_planner(); lock_planner();
computeVariations(plan_copy, &ds_after_previous_dives); cloneDiveplan(&diveplan, plan_copy);
unlock_planner();
computeVariations(plan_copy, &ds_after_previous_dives);
}
free(cache); free(cache);