drivers/battery: fix race condition in event handling

Move mutex lock before checking flist to prevent race condition where
events could be lost when occurring simultaneously with service opening.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1
2024-11-19 21:00:04 +08:00
committed by Xiang Xiao
parent e83cde2f64
commit b4c6a92757
3 changed files with 24 additions and 21 deletions
+8 -7
View File
@@ -474,18 +474,18 @@ int battery_charger_changed(FAR struct battery_charger_dev_s *dev,
/* Event happen too early? */
ret = nxmutex_lock(&dev->batlock);
if (ret < 0)
{
return ret;
}
if (list_is_clear(&dev->flist))
{
/* Yes, record it and return directly */
dev->mask |= mask;
return 0;
}
ret = nxmutex_lock(&dev->batlock);
if (ret < 0)
{
return ret;
goto out;
}
dev->mask |= mask;
@@ -495,6 +495,7 @@ int battery_charger_changed(FAR struct battery_charger_dev_s *dev,
battery_charger_notify(priv, mask);
}
out:
nxmutex_unlock(&dev->batlock);
return OK;
}
+8 -7
View File
@@ -434,18 +434,18 @@ int battery_gauge_changed(FAR struct battery_gauge_dev_s *dev,
/* Event happen too early? */
ret = nxmutex_lock(&dev->batlock);
if (ret < 0)
{
return ret;
}
if (list_is_clear(&dev->flist))
{
/* Yes, record it and return directly */
dev->mask |= mask;
return 0;
}
ret = nxmutex_lock(&dev->batlock);
if (ret < 0)
{
return ret;
goto out;
}
dev->mask |= mask;
@@ -455,6 +455,7 @@ int battery_gauge_changed(FAR struct battery_gauge_dev_s *dev,
battery_gauge_notify(priv, mask);
}
out:
nxmutex_unlock(&dev->batlock);
return OK;
}
+8 -7
View File
@@ -499,18 +499,18 @@ int battery_monitor_changed(FAR struct battery_monitor_dev_s *dev,
/* Event happen too early? */
ret = nxmutex_lock(&dev->batlock);
if (ret < 0)
{
return ret;
}
if (list_is_clear(&dev->flist))
{
/* Yes, record it and return directly */
dev->mask |= mask;
return 0;
}
ret = nxmutex_lock(&dev->batlock);
if (ret < 0)
{
return ret;
goto out;
}
dev->mask |= mask;
@@ -520,6 +520,7 @@ int battery_monitor_changed(FAR struct battery_monitor_dev_s *dev,
battery_monitor_notify(priv, mask);
}
out:
nxmutex_unlock(&dev->batlock);
return OK;
}