mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 08:36:24 +08:00
Replace nxsem API when used as a lock with nxmutex API
Signed-off-by: anjiahao <anjiahao@xiaomi.com> Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
committed by
Masayuki Ishikawa
parent
0dfd1f004d
commit
d1d46335df
+23
-22
@@ -41,6 +41,7 @@
|
||||
|
||||
#include <nuttx/arch.h>
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/mutex.h>
|
||||
#include <nuttx/semaphore.h>
|
||||
#include <nuttx/fs/fs.h>
|
||||
#include <nuttx/fs/ioctl.h>
|
||||
@@ -62,7 +63,7 @@ struct ramlog_dev_s
|
||||
#endif
|
||||
volatile size_t rl_head; /* The head index (where data is added) */
|
||||
volatile size_t rl_tail; /* The tail index (where data is removed) */
|
||||
sem_t rl_exclsem; /* Enforces mutually exclusive access */
|
||||
mutex_t rl_lock; /* Enforces mutually exclusive access */
|
||||
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
||||
sem_t rl_waitsem; /* Used to wait for data */
|
||||
#endif
|
||||
@@ -143,9 +144,10 @@ static struct ramlog_dev_s g_sysdev =
|
||||
# endif
|
||||
CONFIG_RAMLOG_BUFSIZE, /* rl_head */
|
||||
CONFIG_RAMLOG_BUFSIZE, /* rl_tail */
|
||||
SEM_INITIALIZER(1), /* rl_exclsem */
|
||||
NXMUTEX_INITIALIZER, /* rl_lock */
|
||||
# ifndef CONFIG_RAMLOG_NONBLOCKING
|
||||
SEM_INITIALIZER(0), /* rl_waitsem */
|
||||
NXSEM_INITIALIZER(0,
|
||||
PRIOINHERIT_FLAGS_DISABLE), /* rl_waitsem */
|
||||
# endif
|
||||
CONFIG_RAMLOG_BUFSIZE, /* rl_bufsize */
|
||||
g_sysbuffer /* rl_buffer */
|
||||
@@ -363,7 +365,7 @@ static ssize_t ramlog_addbuf(FAR struct ramlog_dev_s *priv,
|
||||
char ch;
|
||||
int ret;
|
||||
|
||||
ret = nxsem_wait(&priv->rl_exclsem);
|
||||
ret = nxmutex_lock(&priv->rl_lock);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
@@ -425,7 +427,7 @@ static ssize_t ramlog_addbuf(FAR struct ramlog_dev_s *priv,
|
||||
* probably retry, causing same error condition again.
|
||||
*/
|
||||
|
||||
nxsem_post(&priv->rl_exclsem);
|
||||
nxmutex_unlock(&priv->rl_lock);
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -455,7 +457,7 @@ static ssize_t ramlog_file_read(FAR struct file *filep, FAR char *buffer,
|
||||
|
||||
/* Get exclusive access to the rl_tail index */
|
||||
|
||||
ret = nxsem_wait(&priv->rl_exclsem);
|
||||
ret = nxmutex_lock(&priv->rl_lock);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
@@ -503,7 +505,7 @@ static ssize_t ramlog_file_read(FAR struct file *filep, FAR char *buffer,
|
||||
|
||||
sched_lock();
|
||||
priv->rl_nwaiters++;
|
||||
nxsem_post(&priv->rl_exclsem);
|
||||
nxmutex_unlock(&priv->rl_lock);
|
||||
|
||||
/* We may now be pre-empted! But that should be okay because we
|
||||
* have already incremented nwaiters. Pre-emptions is disabled
|
||||
@@ -523,13 +525,13 @@ static ssize_t ramlog_file_read(FAR struct file *filep, FAR char *buffer,
|
||||
|
||||
if (ret >= 0)
|
||||
{
|
||||
/* Yes... then retake the mutual exclusion semaphore */
|
||||
/* Yes... then retake the mutual exclusion mutex */
|
||||
|
||||
ret = nxsem_wait(&priv->rl_exclsem);
|
||||
ret = nxmutex_lock(&priv->rl_lock);
|
||||
}
|
||||
|
||||
/* Was the semaphore wait successful? Did we successful re-take the
|
||||
* mutual exclusion semaphore?
|
||||
/* Was the mutex wait successful? Did we successful re-take the
|
||||
* mutual exclusion mutex?
|
||||
*/
|
||||
|
||||
if (ret < 0)
|
||||
@@ -544,10 +546,10 @@ static ssize_t ramlog_file_read(FAR struct file *filep, FAR char *buffer,
|
||||
|
||||
/* Break out to return what we have. Note, we can't exactly
|
||||
* "break" out because whichever error occurred, we do not hold
|
||||
* the exclusion semaphore.
|
||||
* the exclusion mutex.
|
||||
*/
|
||||
|
||||
goto errout_without_sem;
|
||||
goto errout_without_lock;
|
||||
}
|
||||
#endif /* CONFIG_RAMLOG_NONBLOCKING */
|
||||
}
|
||||
@@ -574,14 +576,14 @@ static ssize_t ramlog_file_read(FAR struct file *filep, FAR char *buffer,
|
||||
}
|
||||
}
|
||||
|
||||
/* Relinquish the mutual exclusion semaphore */
|
||||
/* Relinquish the mutual exclusion mutex */
|
||||
|
||||
nxsem_post(&priv->rl_exclsem);
|
||||
nxmutex_unlock(&priv->rl_lock);
|
||||
|
||||
/* Notify all poll/select waiters that they can write to the FIFO */
|
||||
|
||||
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
||||
errout_without_sem:
|
||||
errout_without_lock:
|
||||
#endif
|
||||
|
||||
if (nread > 0)
|
||||
@@ -626,7 +628,7 @@ static int ramlog_file_ioctl(FAR struct file *filep, int cmd,
|
||||
DEBUGASSERT(inode && inode->i_private);
|
||||
priv = (FAR struct ramlog_dev_s *)inode->i_private;
|
||||
|
||||
ret = nxsem_wait(&priv->rl_exclsem);
|
||||
ret = nxmutex_lock(&priv->rl_lock);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
@@ -642,8 +644,7 @@ static int ramlog_file_ioctl(FAR struct file *filep, int cmd,
|
||||
break;
|
||||
}
|
||||
|
||||
nxsem_post(&priv->rl_exclsem);
|
||||
|
||||
nxmutex_unlock(&priv->rl_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -669,7 +670,7 @@ static int ramlog_file_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
||||
|
||||
/* Get exclusive access to the poll structures */
|
||||
|
||||
ret = nxsem_wait(&priv->rl_exclsem);
|
||||
ret = nxmutex_lock(&priv->rl_lock);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
@@ -754,7 +755,7 @@ static int ramlog_file_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
||||
}
|
||||
|
||||
errout:
|
||||
nxsem_post(&priv->rl_exclsem);
|
||||
nxmutex_unlock(&priv->rl_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -786,7 +787,7 @@ int ramlog_register(FAR const char *devpath, FAR char *buffer, size_t buflen)
|
||||
{
|
||||
/* Initialize the non-zero values in the RAM logging device structure */
|
||||
|
||||
nxsem_init(&priv->rl_exclsem, 0, 1);
|
||||
nxmutex_init(&priv->rl_lock);
|
||||
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
||||
nxsem_init(&priv->rl_waitsem, 0, 0);
|
||||
|
||||
|
||||
@@ -119,10 +119,10 @@ static const uint8_t g_syscrlf[2] =
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: syslog_dev_takesem
|
||||
* Name: syslog_dev_lock
|
||||
****************************************************************************/
|
||||
|
||||
static inline int syslog_dev_takesem(FAR struct syslog_dev_s *syslog_dev)
|
||||
static inline int syslog_dev_lock(FAR struct syslog_dev_s *syslog_dev)
|
||||
{
|
||||
/* Does this thread already hold the lock? That could happen if
|
||||
* we were called recursively, i.e., if the logic kicked off by
|
||||
@@ -144,15 +144,6 @@ static inline int syslog_dev_takesem(FAR struct syslog_dev_s *syslog_dev)
|
||||
return nxrmutex_lock(&syslog_dev->sl_lock);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: syslog_dev_givesem
|
||||
****************************************************************************/
|
||||
|
||||
static inline void syslog_dev_givesem(FAR struct syslog_dev_s *syslog_dev)
|
||||
{
|
||||
nxrmutex_unlock(&syslog_dev->sl_lock);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: syslog_dev_open
|
||||
*
|
||||
@@ -264,7 +255,7 @@ static int syslog_dev_open(FAR struct syslog_dev_s *syslog_dev,
|
||||
* (SYSLOG_INITIALIZING).
|
||||
* (3) While we are generating SYSLOG output. The case could happen if
|
||||
* debug output is generated while syslog_dev_putc() executes
|
||||
* (This case is actually handled inside of syslog_semtake()).
|
||||
* (This case is actually handled inside of syslog_lock()).
|
||||
* (4) Any debug output generated from interrupt handlers. A disadvantage
|
||||
* of using the generic character device for the SYSLOG is that it
|
||||
* cannot handle debug output generated from interrupt level handlers.
|
||||
@@ -276,7 +267,7 @@ static int syslog_dev_open(FAR struct syslog_dev_s *syslog_dev,
|
||||
*
|
||||
* NOTE: That the third case is different. It applies only to the thread
|
||||
* that currently holds the sl_lock. Other threads should wait.
|
||||
* that is why that case is handled in syslog_semtake().
|
||||
* that is why that case is handled in syslog_lock().
|
||||
*
|
||||
* Input Parameters:
|
||||
* syslog_dev - Handle to syslog device to be used.
|
||||
@@ -313,7 +304,7 @@ static int syslog_dev_outputready(FAR struct syslog_dev_s *syslog_dev)
|
||||
}
|
||||
|
||||
/* NOTE that the scheduler is locked. That is because we do not have
|
||||
* fully initialized semaphore capability until the SYSLOG device is
|
||||
* fully initialized mutex capability until the SYSLOG device is
|
||||
* successfully initialized.
|
||||
*/
|
||||
|
||||
@@ -401,10 +392,10 @@ static ssize_t syslog_dev_write(FAR struct syslog_channel_s *channel,
|
||||
|
||||
/* The syslog device is ready for writing */
|
||||
|
||||
ret = syslog_dev_takesem(syslog_dev);
|
||||
ret = syslog_dev_lock(syslog_dev);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* We probably already hold the semaphore and were probably
|
||||
/* We probably already hold the mutex and were probably
|
||||
* re-entered by the logic kicked off by file_write().
|
||||
* We might also have been interrupted by a signal. Either
|
||||
* way, we are outta here.
|
||||
@@ -438,7 +429,7 @@ static ssize_t syslog_dev_write(FAR struct syslog_channel_s *channel,
|
||||
if (nwritten < 0)
|
||||
{
|
||||
ret = (int)nwritten;
|
||||
goto errout_with_sem;
|
||||
goto errout_with_lock;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,7 +472,7 @@ static ssize_t syslog_dev_write(FAR struct syslog_channel_s *channel,
|
||||
if (nwritten < 0)
|
||||
{
|
||||
ret = (int)nwritten;
|
||||
goto errout_with_sem;
|
||||
goto errout_with_lock;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -504,16 +495,16 @@ static ssize_t syslog_dev_write(FAR struct syslog_channel_s *channel,
|
||||
if (nwritten < 0)
|
||||
{
|
||||
ret = (int)nwritten;
|
||||
goto errout_with_sem;
|
||||
goto errout_with_lock;
|
||||
}
|
||||
}
|
||||
|
||||
syslog_dev_givesem(syslog_dev);
|
||||
nxrmutex_unlock(&syslog_dev->sl_lock);
|
||||
return buflen;
|
||||
|
||||
errout_with_sem:
|
||||
errout_with_lock:
|
||||
syslog_dev->sl_state = SYSLOG_FAILURE;
|
||||
syslog_dev_givesem(syslog_dev);
|
||||
nxrmutex_unlock(&syslog_dev->sl_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -560,10 +551,10 @@ static int syslog_dev_putc(FAR struct syslog_channel_s *channel, int ch)
|
||||
* value to write.
|
||||
*/
|
||||
|
||||
ret = syslog_dev_takesem(syslog_dev);
|
||||
ret = syslog_dev_lock(syslog_dev);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* We probably already hold the semaphore and were probably
|
||||
/* We probably already hold the lock and were probably
|
||||
* re-entered by the logic kicked off by file_write().
|
||||
* We might also have been interrupted by a signal. Either
|
||||
* way, we are outta here.
|
||||
@@ -597,7 +588,7 @@ static int syslog_dev_putc(FAR struct syslog_channel_s *channel, int ch)
|
||||
nbytes = file_write(&syslog_dev->sl_file, &uch, 1);
|
||||
}
|
||||
|
||||
syslog_dev_givesem(syslog_dev);
|
||||
nxrmutex_unlock(&syslog_dev->sl_lock);
|
||||
|
||||
/* Check if the write was successful. If not, nbytes will be
|
||||
* a negated errno value.
|
||||
@@ -760,7 +751,7 @@ void syslog_dev_uninitialize(FAR struct syslog_channel_s *channel)
|
||||
sched_lock();
|
||||
syslog_dev_flush(channel);
|
||||
|
||||
/* Close the detached file instance, and destroy the semaphore. These are
|
||||
/* Close the detached file instance, and destroy the mutex. These are
|
||||
* both only created when the device is in SYSLOG_OPENED or SYSLOG_FAILURE
|
||||
* state.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user