mirror of
https://github.com/apache/nuttx.git
synced 2026-08-01 20:28:58 +00:00
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:
parent
7713e88871
commit
68a4632f5d
3 changed files with 24 additions and 21 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue