Skip to content

Commit

Permalink
Replace nxsem API when used as a lock with nxmutex API
Browse files Browse the repository at this point in the history
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
  • Loading branch information
anjiahao1 authored and masayuki2009 committed Oct 17, 2022
1 parent 0dfd1f0 commit d1d4633
Show file tree
Hide file tree
Showing 710 changed files with 7,503 additions and 14,852 deletions.
113 changes: 18 additions & 95 deletions arch/arm/src/am335x/am335x_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/clock.h>
#include <nuttx/mutex.h>
#include <nuttx/semaphore.h>
#include <nuttx/i2c/i2c_master.h>

Expand Down Expand Up @@ -181,7 +182,7 @@ struct am335x_i2c_priv_s
const struct am335x_i2c_config_s *config;

int refs; /* Reference count */
sem_t sem_excl; /* Mutual exclusion semaphore */
mutex_t lock; /* Mutual exclusion mutex */
#ifndef CONFIG_I2C_POLLED
sem_t sem_isr; /* Interrupt wait semaphore */
#endif
Expand Down Expand Up @@ -219,9 +220,6 @@ static inline void am335x_i2c_putreg(struct am335x_i2c_priv_s *priv,
static inline void am335x_i2c_modifyreg(struct am335x_i2c_priv_s *priv,
uint16_t offset, uint32_t clearbits,
uint32_t setbits);
static inline int am335x_i2c_sem_wait(struct am335x_i2c_priv_s *priv);
static int
am335x_i2c_sem_wait_noncancelable(struct am335x_i2c_priv_s *priv);

#ifdef CONFIG_AM335X_I2C_DYNTIMEO
static uint32_t am335x_i2c_toticks(int msgc, struct i2c_msg_s *msgs);
Expand All @@ -231,10 +229,6 @@ static inline int
am335x_i2c_sem_waitdone(struct am335x_i2c_priv_s *priv);
static inline bool
am335x_i2c_sem_waitstop(struct am335x_i2c_priv_s *priv);
static inline void am335x_i2c_sem_post(struct am335x_i2c_priv_s *priv);
static inline void am335x_i2c_sem_init(struct am335x_i2c_priv_s *priv);
static inline void
am335x_i2c_sem_destroy(struct am335x_i2c_priv_s *priv);

#ifdef CONFIG_I2C_TRACE
static void am335x_i2c_tracereset(struct am335x_i2c_priv_s *priv);
Expand Down Expand Up @@ -320,6 +314,10 @@ static struct am335x_i2c_priv_s am335x_i2c0_priv =
.ops = &am335x_i2c_ops,
.config = &am335x_i2c0_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
#ifndef CONFIG_I2C_POLLED
.sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE),
#endif
.intstate = INTSTATE_IDLE,
.msgc = 0,
.msgv = NULL,
Expand Down Expand Up @@ -351,6 +349,10 @@ static struct am335x_i2c_priv_s am335x_i2c1_priv =
.ops = &am335x_i2c_ops,
.config = &am335x_i2c1_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
#ifndef CONFIG_I2C_POLLED
.sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE),
#endif
.intstate = INTSTATE_IDLE,
.msgc = 0,
.msgv = NULL,
Expand Down Expand Up @@ -382,6 +384,10 @@ static struct am335x_i2c_priv_s am335x_i2c2_priv =
.ops = &am335x_i2c_ops,
.config = &am335x_i2c2_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
#ifndef CONFIG_I2C_POLLED
.sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE),
#endif
.intstate = INTSTATE_IDLE,
.msgc = 0,
.msgv = NULL,
Expand Down Expand Up @@ -439,34 +445,6 @@ static inline void am335x_i2c_modifyreg(struct am335x_i2c_priv_s *priv,
modifyreg32(priv->config->base + offset, clearbits, setbits);
}

/****************************************************************************
* Name: am335x_i2c_sem_wait
*
* Description:
* Take the exclusive access, waiting as necessary. May be interrupted by
* a signal.
*
****************************************************************************/

static inline int am335x_i2c_sem_wait(struct am335x_i2c_priv_s *priv)
{
return nxsem_wait(&priv->sem_excl);
}

/****************************************************************************
* Name: am335x_i2c_sem_wait_noncancelable
*
* Description:
* Take the exclusive access, waiting as necessary.
*
****************************************************************************/

static int
am335x_i2c_sem_wait_noncancelable(struct am335x_i2c_priv_s *priv)
{
return nxsem_wait_uninterruptible(&priv->sem_excl);
}

/****************************************************************************
* Name: am335x_i2c_toticks
*
Expand Down Expand Up @@ -691,57 +669,6 @@ am335x_i2c_sem_waitstop(struct am335x_i2c_priv_s *priv)
return false;
}

/****************************************************************************
* Name: am335x_i2c_sem_post
*
* Description:
* Release the mutual exclusion semaphore
*
****************************************************************************/

static inline void am335x_i2c_sem_post(struct am335x_i2c_priv_s *priv)
{
nxsem_post(&priv->sem_excl);
}

/****************************************************************************
* Name: am335x_i2c_sem_init
*
* Description:
* Initialize semaphores
*
****************************************************************************/

static inline void am335x_i2c_sem_init(struct am335x_i2c_priv_s *priv)
{
nxsem_init(&priv->sem_excl, 0, 1);

#ifndef CONFIG_I2C_POLLED
/* This semaphore is used for signaling and, hence, should not have
* priority inheritance enabled.
*/

nxsem_init(&priv->sem_isr, 0, 0);
nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE);
#endif
}

/****************************************************************************
* Name: am335x_i2c_sem_destroy
*
* Description:
* Destroy semaphores.
*
****************************************************************************/

static inline void am335x_i2c_sem_destroy(struct am335x_i2c_priv_s *priv)
{
nxsem_destroy(&priv->sem_excl);
#ifndef CONFIG_I2C_POLLED
nxsem_destroy(&priv->sem_isr);
#endif
}

/****************************************************************************
* Name: am335x_i2c_trace*
*
Expand Down Expand Up @@ -1382,7 +1309,7 @@ static int am335x_i2c_transfer(struct i2c_master_s *dev,

/* Ensure that address or flags don't change meanwhile */

ret = am335x_i2c_sem_wait(priv);
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return ret;
Expand Down Expand Up @@ -1498,7 +1425,7 @@ static int am335x_i2c_transfer(struct i2c_master_s *dev,
priv->ptr = NULL;
}

am335x_i2c_sem_post(priv);
nxmutex_unlock(&priv->lock);
return ret;
}

Expand Down Expand Up @@ -1535,7 +1462,7 @@ static int am335x_i2c_reset(struct i2c_master_s *dev)

/* Lock out other clients */

ret = am335x_i2c_sem_wait_noncancelable(priv);
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return ret;
Expand Down Expand Up @@ -1630,7 +1557,7 @@ static int am335x_i2c_reset(struct i2c_master_s *dev)

/* Release the port for re-use by other clients */

am335x_i2c_sem_post(priv);
nxmutex_unlock(&priv->lock);
return ret;
}
#endif /* CONFIG_I2C_RESET */
Expand Down Expand Up @@ -1683,7 +1610,6 @@ struct i2c_master_s *am335x_i2cbus_initialize(int port)

if ((volatile int)priv->refs++ == 0)
{
am335x_i2c_sem_init(priv);
am335x_i2c_init(priv);
}

Expand Down Expand Up @@ -1728,9 +1654,6 @@ int am335x_i2cbus_uninitialize(struct i2c_master_s *dev)

am335x_i2c_deinit(priv);

/* Release unused resources */

am335x_i2c_sem_destroy(priv);
return OK;
}

Expand Down
3 changes: 0 additions & 3 deletions arch/arm/src/am335x/am335x_lcdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
#include <debug.h>

#include <nuttx/arch.h>
#include <nuttx/semaphore.h>
#include <nuttx/video/fb.h>

#include "arm_internal.h"
Expand Down Expand Up @@ -143,7 +142,6 @@ struct am335x_lcd_dev_s

struct am335x_panel_info_s panel;

sem_t exclsem; /* Assure mutually exclusive access */
nxgl_coord_t stride; /* Width of framebuffer in bytes */
size_t fbsize; /* Size of the framebuffer allocation */
};
Expand Down Expand Up @@ -586,7 +584,6 @@ int am335x_lcd_initialize(const struct am335x_panel_info_s *panel)

/* Initialize the device state singleton */

nxsem_init(&priv->exclsem, 0, 1);
memcpy(&priv->panel, panel, sizeof(struct am335x_panel_info_s));

/* Save framebuffer information */
Expand Down
34 changes: 16 additions & 18 deletions arch/arm/src/cxd56xx/cxd56_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/irq.h>
#include <nuttx/semaphore.h>
#include <nuttx/mutex.h>
#include <arch/chip/scu.h>
#include <arch/chip/adc.h>

Expand Down Expand Up @@ -174,7 +174,7 @@ struct cxd56adc_dev_s
struct scufifo_wm_s *wm; /* water mark */
struct math_filter_s *filter; /* math filter */
struct scuev_notify_s * notify; /* notify */
sem_t exclsem; /* exclusive semaphore */
mutex_t lock; /* exclusive mutex */
int crefs; /* reference count */
};

Expand Down Expand Up @@ -718,14 +718,14 @@ static int cxd56_adc_open(struct file *filep)

/* Increment reference counter */

nxsem_wait_uninterruptible(&priv->exclsem);
nxmutex_lock(&priv->lock);

priv->crefs++;
DEBUGASSERT(priv->crefs > 0);

if (priv->crefs > 1)
{
nxsem_post(&priv->exclsem);
nxmutex_unlock(&priv->lock);
return OK;
}

Expand All @@ -738,7 +738,7 @@ static int cxd56_adc_open(struct file *filep)
priv->seq = seq_open(SEQ_TYPE_NORMAL, type);
if (!priv->seq)
{
nxsem_post(&priv->exclsem);
nxmutex_unlock(&priv->lock);
return -ENOENT;
}

Expand All @@ -751,14 +751,13 @@ static int cxd56_adc_open(struct file *filep)
ret = set_ofstgain(priv);
if (ret < 0)
{
nxsem_post(&priv->exclsem);
nxmutex_unlock(&priv->lock);
return ret;
}

ainfo("open ch%d freq%d scufifo%d\n", priv->ch, priv->freq, priv->fsize);

nxsem_post(&priv->exclsem);

nxmutex_unlock(&priv->lock);
return OK;
}

Expand All @@ -781,14 +780,14 @@ static int cxd56_adc_close(struct file *filep)

/* Decrement reference counter */

nxsem_wait_uninterruptible(&priv->exclsem);
nxmutex_lock(&priv->lock);

DEBUGASSERT(priv->crefs > 0);
priv->crefs--;

if (priv->crefs > 0)
{
nxsem_post(&priv->exclsem);
nxmutex_unlock(&priv->lock);
return OK;
}

Expand All @@ -815,8 +814,7 @@ static int cxd56_adc_close(struct file *filep)
priv->notify = NULL;
}

nxsem_post(&priv->exclsem);

nxmutex_unlock(&priv->lock);
return OK;
}

Expand Down Expand Up @@ -1110,7 +1108,7 @@ int cxd56_adcinitialize(void)
return ret;
}

nxsem_init(&g_lpadc0priv.exclsem, 0, 1);
nxmutex_init(&g_lpadc0priv.lock);
#endif
#if defined (CONFIG_CXD56_LPADC1) || defined (CONFIG_CXD56_LPADC0_1) || defined (CONFIG_CXD56_LPADC_ALL)
ret = register_driver("/dev/lpadc1", &g_adcops, 0666, &g_lpadc1priv);
Expand All @@ -1120,7 +1118,7 @@ int cxd56_adcinitialize(void)
return ret;
}

nxsem_init(&g_lpadc1priv.exclsem, 0, 1);
nxmutex_init(&g_lpadc1priv.lock);
#endif
#if defined (CONFIG_CXD56_LPADC2) || defined (CONFIG_CXD56_LPADC_ALL)
ret = register_driver("/dev/lpadc2", &g_adcops, 0666, &g_lpadc2priv);
Expand All @@ -1130,7 +1128,7 @@ int cxd56_adcinitialize(void)
return ret;
}

nxsem_init(&g_lpadc2priv.exclsem, 0, 1);
nxmutex_init(&g_lpadc2priv.lock);
#endif
#if defined (CONFIG_CXD56_LPADC3) || defined (CONFIG_CXD56_LPADC_ALL)
ret = register_driver("/dev/lpadc3", &g_adcops, 0666, &g_lpadc3priv);
Expand All @@ -1140,7 +1138,7 @@ int cxd56_adcinitialize(void)
return ret;
}

nxsem_init(&g_lpadc3priv.exclsem, 0, 1);
nxmutex_init(&g_lpadc3priv.lock);
#endif
#ifdef CONFIG_CXD56_HPADC0
ret = register_driver("/dev/hpadc0", &g_adcops, 0666, &g_hpadc0priv);
Expand All @@ -1150,7 +1148,7 @@ int cxd56_adcinitialize(void)
return ret;
}

nxsem_init(&g_hpadc0priv.exclsem, 0, 1);
nxmutex_init(&g_hpadc0priv.lock);
#endif
#ifdef CONFIG_CXD56_HPADC1
ret = register_driver("/dev/hpadc1", &g_adcops, 0666, &g_hpadc1priv);
Expand All @@ -1160,7 +1158,7 @@ int cxd56_adcinitialize(void)
return ret;
}

nxsem_init(&g_hpadc1priv.exclsem, 0, 1);
nxmutex_init(&g_hpadc1priv.lock);
#endif

return ret;
Expand Down
Loading

0 comments on commit d1d4633

Please sign in to comment.