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:
anjiahao
2022-09-06 14:18:45 +08:00
committed by Masayuki Ishikawa
parent 0dfd1f004d
commit d1d46335df
710 changed files with 7503 additions and 14852 deletions
+24 -62
View File
@@ -59,6 +59,7 @@
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/mutex.h>
#include <nuttx/wqueue.h>
#include <nuttx/net/arp.h>
#include <nuttx/net/netdev.h>
@@ -132,7 +133,7 @@ struct tun_device_s
bool write_wait;
struct work_s work; /* For deferring poll work to the work queue */
FAR struct pollfd *poll_fds;
sem_t waitsem;
mutex_t lock;
sem_t read_wait_sem;
sem_t write_wait_sem;
size_t read_d_len;
@@ -153,16 +154,13 @@ struct tun_device_s
struct tun_driver_s
{
uint8_t free_tuns;
sem_t waitsem;
mutex_t lock;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int tun_lock(FAR struct tun_device_s *priv);
static void tun_unlock(FAR struct tun_device_s *priv);
/* Common TX logic */
static void tun_fd_transmit(FAR struct tun_device_s *priv);
@@ -233,42 +231,6 @@ static const struct file_operations g_tun_file_ops =
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: tundev_lock
****************************************************************************/
static int tundev_lock(FAR struct tun_driver_s *tun)
{
return nxsem_wait_uninterruptible(&tun->waitsem);
}
/****************************************************************************
* Name: tundev_unlock
****************************************************************************/
static void tundev_unlock(FAR struct tun_driver_s *tun)
{
nxsem_post(&tun->waitsem);
}
/****************************************************************************
* Name: tun_lock
****************************************************************************/
static int tun_lock(FAR struct tun_device_s *priv)
{
return nxsem_wait_uninterruptible(&priv->waitsem);
}
/****************************************************************************
* Name: tun_unlock
****************************************************************************/
static void tun_unlock(FAR struct tun_device_s *priv)
{
nxsem_post(&priv->waitsem);
}
/****************************************************************************
* Name: tun_pollnotify
****************************************************************************/
@@ -837,7 +799,7 @@ static void tun_txavail_work(FAR void *arg)
FAR struct tun_device_s *priv = (FAR struct tun_device_s *)arg;
int ret;
ret = tun_lock(priv);
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
/* Thread has been canceled, skip poll-related work */
@@ -849,7 +811,7 @@ static void tun_txavail_work(FAR void *arg)
if (priv->read_d_len != 0)
{
tun_unlock(priv);
nxmutex_unlock(&priv->lock);
return;
}
@@ -863,7 +825,7 @@ static void tun_txavail_work(FAR void *arg)
}
net_unlock();
tun_unlock(priv);
nxmutex_unlock(&priv->lock);
}
/****************************************************************************
@@ -988,7 +950,7 @@ static int tun_dev_init(FAR struct tun_device_s *priv,
/* Initialize the mutual exlcusion and wait semaphore */
nxsem_init(&priv->waitsem, 0, 1);
nxmutex_init(&priv->lock);
nxsem_init(&priv->read_wait_sem, 0, 0);
nxsem_init(&priv->write_wait_sem, 0, 0);
@@ -1011,7 +973,7 @@ static int tun_dev_init(FAR struct tun_device_s *priv,
ret = netdev_register(&priv->dev, tun ? NET_LL_TUN : NET_LL_ETHERNET);
if (ret != OK)
{
nxsem_destroy(&priv->waitsem);
nxmutex_destroy(&priv->lock);
nxsem_destroy(&priv->read_wait_sem);
nxsem_destroy(&priv->write_wait_sem);
return ret;
@@ -1035,7 +997,7 @@ static void tun_dev_uninit(FAR struct tun_device_s *priv)
netdev_unregister(&priv->dev);
nxsem_destroy(&priv->waitsem);
nxmutex_destroy(&priv->lock);
nxsem_destroy(&priv->read_wait_sem);
nxsem_destroy(&priv->write_wait_sem);
}
@@ -1058,13 +1020,13 @@ static int tun_close(FAR struct file *filep)
}
intf = priv - g_tun_devices;
ret = tundev_lock(tun);
ret = nxmutex_lock(&tun->lock);
if (ret >= 0)
{
tun->free_tuns |= (1 << intf);
tun_dev_uninit(priv);
tundev_unlock(tun);
nxmutex_unlock(&tun->lock);
}
return ret;
@@ -1092,7 +1054,7 @@ static ssize_t tun_write(FAR struct file *filep, FAR const char *buffer,
* thread is canceled) and no data has yet been written.
*/
ret = nxsem_wait(&priv->waitsem);
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return nwritten == 0 ? (ssize_t)ret : nwritten;
@@ -1124,11 +1086,11 @@ static ssize_t tun_write(FAR struct file *filep, FAR const char *buffer,
}
priv->write_wait = true;
tun_unlock(priv);
nxmutex_unlock(&priv->lock);
nxsem_wait(&priv->write_wait_sem);
}
tun_unlock(priv);
nxmutex_unlock(&priv->lock);
return nwritten;
}
@@ -1154,7 +1116,7 @@ static ssize_t tun_read(FAR struct file *filep, FAR char *buffer,
* thread is canceled) and no data has yet been read.
*/
ret = nxsem_wait(&priv->waitsem);
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return nread == 0 ? (ssize_t)ret : nread;
@@ -1208,11 +1170,11 @@ static ssize_t tun_read(FAR struct file *filep, FAR char *buffer,
}
priv->read_wait = true;
tun_unlock(priv);
nxmutex_unlock(&priv->lock);
nxsem_wait(&priv->read_wait_sem);
}
tun_unlock(priv);
nxmutex_unlock(&priv->lock);
return nread;
}
@@ -1233,7 +1195,7 @@ int tun_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
return -EINVAL;
}
ret = tun_lock(priv);
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return ret;
@@ -1275,7 +1237,7 @@ int tun_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
}
errout:
tun_unlock(priv);
nxmutex_unlock(&priv->lock);
return ret;
}
@@ -1303,7 +1265,7 @@ static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
return -EINVAL;
}
ret = tundev_lock(tun);
ret = nxmutex_lock(&tun->lock);
if (ret < 0)
{
return ret;
@@ -1313,7 +1275,7 @@ static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
if (free_tuns == 0)
{
tundev_unlock(tun);
nxmutex_unlock(&tun->lock);
return -ENOMEM;
}
@@ -1326,7 +1288,7 @@ static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
(ifr->ifr_flags & IFF_MASK) == IFF_TUN);
if (ret != OK)
{
tundev_unlock(tun);
nxmutex_unlock(&tun->lock);
return ret;
}
@@ -1334,7 +1296,7 @@ static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
priv = filep->f_priv;
strlcpy(ifr->ifr_name, priv->dev.d_ifname, IFNAMSIZ);
tundev_unlock(tun);
nxmutex_unlock(&tun->lock);
return OK;
}
@@ -1363,7 +1325,7 @@ static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
int tun_initialize(void)
{
nxsem_init(&g_tun.waitsem, 0, 1);
nxmutex_init(&g_tun.lock);
g_tun.free_tuns = (1 << CONFIG_TUN_NINTERFACES) - 1;