Skip to content

Commit

Permalink
drivers/sensors/gnss: using new buffer lock avoid deadlock
Browse files Browse the repository at this point in the history
Signed-off-by: dongjiuzhu1 <[email protected]>
  • Loading branch information
Donny9 authored and xiaoxiang781216 committed Sep 30, 2024
1 parent 58fbe31 commit 9599302
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions drivers/sensors/gnss_uorb.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct gnss_upperhalf_s
uint8_t crefs;
uint8_t flags;
mutex_t lock;
mutex_t bufferlock;
sem_t buffersem;
size_t parsenext;
char parsebuffer[GNSS_PARSE_BUFFERSIZE];
Expand Down Expand Up @@ -306,7 +307,7 @@ static ssize_t gnss_read(FAR struct file *filep, FAR char *buffer,
upper = filep->f_inode->i_private;
user = filep->f_priv;

nxmutex_lock(&upper->lock);
nxmutex_lock(&upper->bufferlock);
if (user->pos < upper->buffer.tail)
{
user->pos = upper->buffer.tail;
Expand All @@ -322,14 +323,14 @@ static ssize_t gnss_read(FAR struct file *filep, FAR char *buffer,
}
else
{
nxmutex_unlock(&upper->lock);
nxmutex_unlock(&upper->bufferlock);
ret = nxsem_wait_uninterruptible(&upper->buffersem);
if (ret < 0)
{
return ret;
}

nxmutex_lock(&upper->lock);
nxmutex_lock(&upper->bufferlock);
goto check;
}
}
Expand All @@ -339,7 +340,7 @@ static ssize_t gnss_read(FAR struct file *filep, FAR char *buffer,
user->pos += ret;

out:
nxmutex_unlock(&upper->lock);
nxmutex_unlock(&upper->bufferlock);
return ret;
}

Expand Down Expand Up @@ -614,7 +615,7 @@ static void gnss_push_data(FAR void *priv, FAR const void *data,
return;
}

nxmutex_lock(&upper->lock);
nxmutex_lock(&upper->bufferlock);
if (is_nmea)
{
gnss_parse(upper, data, bytes);
Expand All @@ -627,7 +628,7 @@ static void gnss_push_data(FAR void *priv, FAR const void *data,
poll_notify(&user->fds, 1, POLLIN);
}

nxmutex_unlock(&upper->lock);
nxmutex_unlock(&upper->bufferlock);

nxsem_get_value(&upper->buffersem, &semcount);
while (semcount++ <= 0)
Expand All @@ -647,7 +648,6 @@ static void gnss_push_event(FAR void *priv, FAR const void *data,
return;
}

nxmutex_lock(&upper->lock);
if (type == SENSOR_TYPE_GNSS)
{
lower = &upper->dev[GNSS_IDX].lower;
Expand All @@ -673,8 +673,6 @@ static void gnss_push_event(FAR void *priv, FAR const void *data,
lower = &upper->dev[GNSS_GEOFENCE].lower;
lower->push_event(lower->priv, data, bytes);
}

nxmutex_unlock(&upper->lock);
}

/****************************************************************************
Expand Down Expand Up @@ -723,6 +721,7 @@ int gnss_register(FAR struct gnss_lowerhalf_s *lower, int devno,
upper->lower = lower;

nxmutex_init(&upper->lock);
nxmutex_init(&upper->bufferlock);
nxsem_init(&upper->buffersem, 0, 0);
gnss_init_data(&upper->gnss);
list_initialize(&upper->userlist);
Expand Down Expand Up @@ -822,6 +821,7 @@ int gnss_register(FAR struct gnss_lowerhalf_s *lower, int devno,
sensor_unregister(&upper->dev[GNSS_IDX].lower, devno);
gnss_err:
nxmutex_destroy(&upper->lock);
nxmutex_destroy(&upper->bufferlock);
nxsem_destroy(&upper->buffersem);
kmm_free(upper);
return ret;
Expand Down

0 comments on commit 9599302

Please sign in to comment.