diff --git a/arch/arm/src/am335x/am335x_i2c.c b/arch/arm/src/am335x/am335x_i2c.c index cf922620017ba..5df0429d18af8 100644 --- a/arch/arm/src/am335x/am335x_i2c.c +++ b/arch/arm/src/am335x/am335x_i2c.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -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 @@ -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); @@ -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); @@ -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, @@ -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, @@ -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, @@ -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 * @@ -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* * @@ -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; @@ -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; } @@ -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; @@ -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 */ @@ -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); } @@ -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; } diff --git a/arch/arm/src/am335x/am335x_lcdc.c b/arch/arm/src/am335x/am335x_lcdc.c index 75ffe5950901c..be8e5e64c9eba 100644 --- a/arch/arm/src/am335x/am335x_lcdc.c +++ b/arch/arm/src/am335x/am335x_lcdc.c @@ -53,7 +53,6 @@ #include #include -#include #include #include "arm_internal.h" @@ -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 */ }; @@ -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 */ diff --git a/arch/arm/src/cxd56xx/cxd56_adc.c b/arch/arm/src/cxd56xx/cxd56_adc.c index c1df7dee00bbb..69687774c221e 100644 --- a/arch/arm/src/cxd56xx/cxd56_adc.c +++ b/arch/arm/src/cxd56xx/cxd56_adc.c @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #include @@ -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 */ }; @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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; diff --git a/arch/arm/src/cxd56xx/cxd56_charger.c b/arch/arm/src/cxd56xx/cxd56_charger.c index 562f869809c66..325eb27ff5026 100644 --- a/arch/arm/src/cxd56xx/cxd56_charger.c +++ b/arch/arm/src/cxd56xx/cxd56_charger.c @@ -39,6 +39,7 @@ #include #include +#include #include #include @@ -64,7 +65,7 @@ struct charger_dev_s { - sem_t batsem; + mutex_t batlock; }; /**************************************************************************** @@ -451,7 +452,7 @@ static int charger_ioctl(struct file *filep, int cmd, unsigned long arg) struct charger_dev_s *priv = inode->i_private; int ret = -ENOTTY; - nxsem_wait_uninterruptible(&priv->batsem); + nxmutex_lock(&priv->batlock); switch (cmd) { @@ -593,8 +594,7 @@ static int charger_ioctl(struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->batsem); - + nxmutex_unlock(&priv->batlock); return ret; } @@ -623,7 +623,7 @@ int cxd56_charger_initialize(const char *devpath) /* Initialize the CXD5247 device structure */ - nxsem_init(&priv->batsem, 0, 1); + nxmutex_init(&priv->batlock); /* Register battery driver */ diff --git a/arch/arm/src/cxd56xx/cxd56_clock.c b/arch/arm/src/cxd56xx/cxd56_clock.c index 313f047407a0b..f5796fe50c837 100644 --- a/arch/arm/src/cxd56xx/cxd56_clock.c +++ b/arch/arm/src/cxd56xx/cxd56_clock.c @@ -31,6 +31,7 @@ #include #include +#include #include "arm_internal.h" #include "chip.h" @@ -155,7 +156,7 @@ static uint32_t g_active_imgdevs = 0; /* Exclusive control */ -static sem_t g_clockexc = SEM_INITIALIZER(1); +static mutex_t g_clocklock = NXMUTEX_INITIALIZER; /* For peripherals inside SCU block * @@ -219,19 +220,19 @@ const struct scu_peripheral g_scuhpadc = * Private Functions ****************************************************************************/ -static void clock_semtake(sem_t *id) +static void clock_lock(mutex_t *lock) { if (!up_interrupt_context()) { - nxsem_wait_uninterruptible(id); + nxmutex_lock(lock); } } -static void clock_semgive(sem_t *id) +static void clock_unlock(mutex_t *lock) { if (!up_interrupt_context()) { - nxsem_post(id); + nxmutex_unlock(lock); } } @@ -889,11 +890,11 @@ static void cxd56_spim_clock_disable(void) static void cxd56_img_spi_clock_enable(void) { - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); enable_pwd(PDID_APP_SUB); cxd56_img_clock_enable(); putreg32(0x00010002, CXD56_CRG_GEAR_IMG_SPI); - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } /**************************************************************************** @@ -906,11 +907,11 @@ static void cxd56_img_spi_clock_enable(void) static void cxd56_img_spi_clock_disable(void) { - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); putreg32(0, CXD56_CRG_GEAR_IMG_SPI); cxd56_img_clock_disable(); disable_pwd(PDID_APP_SUB); - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } #endif @@ -926,11 +927,11 @@ static void cxd56_img_spi_clock_disable(void) static void cxd56_img_wspi_clock_enable(void) { - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); enable_pwd(PDID_APP_SUB); cxd56_img_clock_enable(); putreg32(0x00010004, CXD56_CRG_GEAR_IMG_WSPI); - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } /**************************************************************************** @@ -943,11 +944,11 @@ static void cxd56_img_wspi_clock_enable(void) static void cxd56_img_wspi_clock_disable(void) { - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); putreg32(0, CXD56_CRG_GEAR_IMG_WSPI); cxd56_img_clock_disable(); disable_pwd(PDID_APP_SUB); - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } #endif @@ -1089,7 +1090,7 @@ void cxd56_spi_clock_gear_adjust(int port, uint32_t maxfreq) return; } - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); baseclock = cxd56_get_appsmp_baseclock(); if (baseclock != 0) { @@ -1108,7 +1109,7 @@ void cxd56_spi_clock_gear_adjust(int port, uint32_t maxfreq) putreg32(gear, addr); } - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } #if defined(CONFIG_CXD56_I2C2) @@ -1320,7 +1321,7 @@ void cxd56_img_uart_clock_enable() { uint32_t val = 0; - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); enable_pwd(PDID_APP_SUB); cxd56_img_clock_enable(); @@ -1333,7 +1334,7 @@ void cxd56_img_uart_clock_enable() #endif /* CONFIG_CXD56_UART2 */ putreg32(val, CXD56_CRG_GEAR_IMG_UART); - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } /**************************************************************************** @@ -1348,7 +1349,7 @@ void cxd56_img_uart_clock_disable() { uint32_t val = 0; - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); val = getreg32(CXD56_CRG_GEAR_IMG_UART); val &= ~(1UL << 16); @@ -1357,7 +1358,7 @@ void cxd56_img_uart_clock_disable() cxd56_img_clock_disable(); disable_pwd(PDID_APP_SUB); - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } /**************************************************************************** @@ -1370,13 +1371,13 @@ void cxd56_img_uart_clock_disable() void cxd56_img_cisif_clock_enable(void) { - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); enable_pwd(PDID_APP_SUB); cxd56_img_clock_enable(); g_active_imgdevs |= FLAG_IMG_CISIF; - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } /**************************************************************************** @@ -1389,13 +1390,13 @@ void cxd56_img_cisif_clock_enable(void) void cxd56_img_cisif_clock_disable(void) { - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); g_active_imgdevs &= ~FLAG_IMG_CISIF; cxd56_img_clock_disable(); disable_pwd(PDID_APP_SUB); - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } /**************************************************************************** @@ -1408,13 +1409,13 @@ void cxd56_img_cisif_clock_disable(void) void cxd56_img_ge2d_clock_enable(void) { - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); enable_pwd(PDID_APP_SUB); cxd56_img_clock_enable(); g_active_imgdevs |= FLAG_IMG_GE2D; - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } /**************************************************************************** @@ -1427,13 +1428,13 @@ void cxd56_img_ge2d_clock_enable(void) void cxd56_img_ge2d_clock_disable(void) { - clock_semtake(&g_clockexc); + clock_lock(&g_clocklock); g_active_imgdevs &= ~FLAG_IMG_GE2D; cxd56_img_clock_disable(); disable_pwd(PDID_APP_SUB); - clock_semgive(&g_clockexc); + clock_unlock(&g_clocklock); } static uint32_t cxd56_get_clock(enum clock_source cs) diff --git a/arch/arm/src/cxd56xx/cxd56_dmac.c b/arch/arm/src/cxd56xx/cxd56_dmac.c index 434ba055a24c3..80cab93047eac 100644 --- a/arch/arm/src/cxd56xx/cxd56_dmac.c +++ b/arch/arm/src/cxd56xx/cxd56_dmac.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include "cxd56_dmac.h" @@ -290,7 +290,7 @@ struct dma_channel_s /* This is the array of all DMA channels */ static struct dma_channel_s g_dmach[NCHANNELS]; -static sem_t g_dmaexc; +static mutex_t g_dmalock; static int dma_init(int ch); static int dma_uninit(int ch); @@ -727,7 +727,7 @@ void weak_function arm_dma_initialize(void) up_enable_irq(irq_map[i]); } - nxsem_init(&g_dmaexc, 0, 1); + nxmutex_init(&g_dmalock); } /**************************************************************************** @@ -762,7 +762,7 @@ DMA_HANDLE cxd56_dmachannel(int ch, ssize_t maxsize) /* Get exclusive access to allocate channel */ - nxsem_wait_uninterruptible(&g_dmaexc); + nxmutex_lock(&g_dmalock); if (ch < 0 || ch >= NCHANNELS) { @@ -806,12 +806,11 @@ DMA_HANDLE cxd56_dmachannel(int ch, ssize_t maxsize) dmach->inuse = true; - nxsem_post(&g_dmaexc); - + nxmutex_unlock(&g_dmalock); return (DMA_HANDLE)dmach; err: - nxsem_post(&g_dmaexc); + nxmutex_unlock(&g_dmalock); return NULL; } @@ -845,7 +844,7 @@ void cxd56_dmafree(DMA_HANDLE handle) return; } - nxsem_wait_uninterruptible(&g_dmaexc); + nxmutex_lock(&g_dmalock); if (!dmach->inuse) { @@ -863,7 +862,7 @@ void cxd56_dmafree(DMA_HANDLE handle) dmach->inuse = false; err: - nxsem_post(&g_dmaexc); + nxmutex_unlock(&g_dmalock); } /**************************************************************************** diff --git a/arch/arm/src/cxd56xx/cxd56_emmc.c b/arch/arm/src/cxd56xx/cxd56_emmc.c index 39555781188e7..5fd3fbc9609d3 100644 --- a/arch/arm/src/cxd56xx/cxd56_emmc.c +++ b/arch/arm/src/cxd56xx/cxd56_emmc.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -91,7 +92,7 @@ struct emmc_dma_desc_s struct cxd56_emmc_state_s { - sem_t excsem; + mutex_t lock; int crefs; uint32_t total_sectors; }; @@ -143,16 +144,6 @@ struct cxd56_emmc_state_s g_emmcdev; * Private Functions ****************************************************************************/ -static int emmc_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -static void emmc_givesem(sem_t *sem) -{ - nxsem_post(sem); -} - static void emmc_cmdstarted(void) { uint32_t val; @@ -429,7 +420,7 @@ static void emmc_send(int datatype, uint32_t opcode, uint32_t arg, /* Wait for command or data transfer done */ - ret = emmc_takesem(&g_waitsem); + ret = nxsem_wait_uninterruptible(&g_waitsem); if (ret < 0) { return; @@ -592,8 +583,7 @@ static int emmc_interrupt(int irq, void *context, void *arg) ferr("End-bit error/write no CRC.\n"); } - emmc_givesem(&g_waitsem); - + nxsem_post(&g_waitsem); return OK; } @@ -705,7 +695,7 @@ static int cxd56_emmc_readsectors(struct cxd56_emmc_state_s *priv, return -ENOMEM; } - ret = emmc_takesem(&priv->excsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { kmm_free(descs); @@ -746,9 +736,8 @@ static int cxd56_emmc_readsectors(struct cxd56_emmc_state_s *priv, } finish: - emmc_givesem(&priv->excsem); + nxmutex_unlock(&priv->lock); kmm_free(descs); - return ret; } @@ -767,7 +756,7 @@ static int cxd56_emmc_writesectors(struct cxd56_emmc_state_s *priv, return -ENOMEM; } - ret = emmc_takesem(&priv->excsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { kmm_free(descs); @@ -822,9 +811,8 @@ static int cxd56_emmc_writesectors(struct cxd56_emmc_state_s *priv, emmc_flushwritefifo(); finish: - emmc_givesem(&priv->excsem); + nxmutex_unlock(&priv->lock); kmm_free(descs); - return ret; } #endif @@ -839,14 +827,14 @@ static int cxd56_emmc_open(struct inode *inode) /* Just increment the reference count on the driver */ - ret = emmc_takesem(&priv->excsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } priv->crefs++; - emmc_givesem(&priv->excsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -861,14 +849,14 @@ static int cxd56_emmc_close(struct inode *inode) /* Decrement the reference count on the block driver */ DEBUGASSERT(priv->crefs > 0); - ret = emmc_takesem(&priv->excsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } priv->crefs--; - emmc_givesem(&priv->excsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -952,7 +940,7 @@ int cxd56_emmcinitialize(void) priv = &g_emmcdev; memset(priv, 0, sizeof(struct cxd56_emmc_state_s)); - nxsem_init(&priv->excsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&g_waitsem, 0, 0); nxsem_set_protocol(&g_waitsem, SEM_PRIO_NONE); diff --git a/arch/arm/src/cxd56xx/cxd56_farapi.c b/arch/arm/src/cxd56xx/cxd56_farapi.c index f9501af8dae5f..61ae9b1f36812 100644 --- a/arch/arm/src/cxd56xx/cxd56_farapi.c +++ b/arch/arm/src/cxd56xx/cxd56_farapi.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -113,7 +114,7 @@ extern struct modulelist_s _image_modlist_base[]; ****************************************************************************/ static sem_t g_farwait; -static sem_t g_farlock; +static mutex_t g_farlock; static struct pm_cpu_wakelock_s g_wlock = { .count = 0, @@ -124,11 +125,6 @@ static struct pm_cpu_wakelock_s g_wlock = * Private Functions ****************************************************************************/ -static int farapi_semtake(sem_t *id) -{ - return nxsem_wait_uninterruptible(id); -} - #ifdef CONFIG_CXD56_FARAPI_DEBUG static void dump_farapi_message(struct farmsg_s *msg) { @@ -225,7 +221,7 @@ void farapi_main(int id, void *arg, struct modulelist_s *mlist) } #endif - farapi_semtake(&g_farlock); + nxmutex_lock(&g_farlock); api = &msg.u.api; @@ -256,7 +252,7 @@ void farapi_main(int id, void *arg, struct modulelist_s *mlist) /* Wait event flag message as Far API done */ - farapi_semtake(&g_farwait); + nxsem_wait_uninterruptible(&g_farwait); /* Permit hot sleep with Far API done */ @@ -265,8 +261,7 @@ void farapi_main(int id, void *arg, struct modulelist_s *mlist) dump_farapi_message(&msg); err: - nxsem_post(&g_farlock); - + nxmutex_unlock(&g_farlock); #ifdef CONFIG_SMP if (0 != cpu) { @@ -295,7 +290,7 @@ void cxd56_farapiinitialize(void) } #endif - nxsem_init(&g_farlock, 0, 1); + nxmutex_init(&g_farlock); nxsem_init(&g_farwait, 0, 0); nxsem_set_protocol(&g_farwait, SEM_PRIO_NONE); diff --git a/arch/arm/src/cxd56xx/cxd56_gauge.c b/arch/arm/src/cxd56xx/cxd56_gauge.c index b26ed62088ce7..dc73d50240a0a 100644 --- a/arch/arm/src/cxd56xx/cxd56_gauge.c +++ b/arch/arm/src/cxd56xx/cxd56_gauge.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -55,7 +56,7 @@ struct bat_gauge_dev_s { - sem_t batsem; + mutex_t batlock; }; /**************************************************************************** @@ -279,7 +280,7 @@ static int gauge_ioctl(struct file *filep, int cmd, unsigned long arg) struct bat_gauge_dev_s *priv = inode->i_private; int ret = -ENOTTY; - nxsem_wait_uninterruptible(&priv->batsem); + nxmutex_lock(&priv->batlock); switch (cmd) { @@ -317,8 +318,7 @@ static int gauge_ioctl(struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->batsem); - + nxmutex_unlock(&priv->batlock); return ret; } @@ -347,7 +347,7 @@ int cxd56_gauge_initialize(const char *devpath) /* Initialize the CXD5247 device structure */ - nxsem_init(&priv->batsem, 0, 1); + nxmutex_init(&priv->batlock); /* Register battery driver */ diff --git a/arch/arm/src/cxd56xx/cxd56_ge2d.c b/arch/arm/src/cxd56xx/cxd56_ge2d.c index be8f61b31fb47..5b15795576d54 100644 --- a/arch/arm/src/cxd56xx/cxd56_ge2d.c +++ b/arch/arm/src/cxd56xx/cxd56_ge2d.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -50,8 +51,6 @@ static ssize_t ge2d_read(struct file *filep, char *buffer, static ssize_t ge2d_write(struct file *filep, const char *buffer, size_t len); static int ge2d_ioctl(struct file *filep, int cmd, unsigned long arg); -static int ge2d_semtake(sem_t *id); -static void ge2d_semgive(sem_t *id); static int ge2d_irqhandler(int irq, void *context, void *arg); /**************************************************************************** @@ -66,30 +65,12 @@ static const struct file_operations g_ge2dfops = }; static sem_t g_wait; -static sem_t g_lock; +static mutex_t g_lock; /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: ge2d_semtake - ****************************************************************************/ - -static int ge2d_semtake(sem_t *id) -{ - return nxsem_wait_uninterruptible(id); -} - -/**************************************************************************** - * Name: ge2d_semgive - ****************************************************************************/ - -static void ge2d_semgive(sem_t *id) -{ - nxsem_post(id); -} - /**************************************************************************** * Name: ge2d_read ****************************************************************************/ @@ -119,7 +100,7 @@ static ssize_t ge2d_write(struct file *filep, const char *buffer, /* Get exclusive access */ - ge2d_semtake(&g_lock); + nxmutex_lock(&g_lock); /* Set operation buffer and start processing. * Descriptor start address bit 0 is select to bus, always 1 (memory), @@ -141,14 +122,13 @@ static ssize_t ge2d_write(struct file *filep, const char *buffer, /* Wait for interrupts for processing done. */ - ge2d_semtake(&g_wait); + nxsem_wait_uninterruptible(&g_wait); /* Disable interrupts */ putreg32(0, GE2D_INTR_ENABLE); - ge2d_semgive(&g_lock); - + nxmutex_unlock(&g_lock); return len; } @@ -192,8 +172,7 @@ static int ge2d_irqhandler(int irq, void *context, void *arg) /* Release semaphore anyway */ - ge2d_semgive(&g_wait); - + nxsem_post(&g_wait); return OK; } @@ -205,7 +184,7 @@ int cxd56_ge2dinitialize(const char *devname) { int ret; - nxsem_init(&g_lock, 0, 1); + nxmutex_init(&g_lock); nxsem_init(&g_wait, 0, 0); nxsem_set_protocol(&g_wait, SEM_PRIO_NONE); @@ -238,7 +217,7 @@ void cxd56_ge2duninitialize(const char *devname) cxd56_img_ge2d_clock_disable(); - nxsem_destroy(&g_lock); + nxmutex_destroy(&g_lock); nxsem_destroy(&g_wait); unregister_driver(devname); diff --git a/arch/arm/src/cxd56xx/cxd56_geofence.c b/arch/arm/src/cxd56xx/cxd56_geofence.c index 50dd06cb79011..572f3188c0f8e 100644 --- a/arch/arm/src/cxd56xx/cxd56_geofence.c +++ b/arch/arm/src/cxd56xx/cxd56_geofence.c @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -58,7 +59,7 @@ struct cxd56_geofence_dev_s { - sem_t devsem; + mutex_t devlock; struct pollfd *fds[CONFIG_GEOFENCE_NPOLLWAITERS]; }; @@ -435,7 +436,7 @@ static void cxd56_geofence_sighandler(uint32_t data, void *userdata) (struct cxd56_geofence_dev_s *)userdata; int ret; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return; @@ -443,7 +444,7 @@ static void cxd56_geofence_sighandler(uint32_t data, void *userdata) poll_notify(priv->fds, CONFIG_GEOFENCE_NPOLLWAITERS, POLLIN); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); } /**************************************************************************** @@ -557,7 +558,7 @@ static int cxd56_geofence_poll(struct file *filep, inode = filep->f_inode; priv = (struct cxd56_geofence_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -608,7 +609,7 @@ static int cxd56_geofence_poll(struct file *filep, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -640,7 +641,7 @@ static int cxd56_geofence_register(const char *devpath) } memset(priv, 0, sizeof(struct cxd56_geofence_dev_s)); - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); ret = cxd56_geofence_initialize(priv); if (ret < 0) diff --git a/arch/arm/src/cxd56xx/cxd56_gnss.c b/arch/arm/src/cxd56xx/cxd56_gnss.c index bc4dcd2fb704f..b7868061b0d56 100644 --- a/arch/arm/src/cxd56xx/cxd56_gnss.c +++ b/arch/arm/src/cxd56xx/cxd56_gnss.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -154,7 +155,7 @@ struct cxd56_devsig_table_s struct cxd56_gnss_dev_s { - sem_t devsem; + mutex_t devlock; sem_t syncsem; uint8_t num_open; uint8_t notify_data; @@ -165,7 +166,7 @@ struct cxd56_gnss_dev_s struct cxd56_gnss_sig_s sigs[CONFIG_CXD56_GNSS_NSIGNALRECEIVERS]; #endif struct cxd56_gnss_shared_info_s shared_info; - sem_t ioctllock; + mutex_t ioctllock; sem_t apiwait; int apiret; }; @@ -1483,7 +1484,7 @@ static int cxd56_gnss_set_signal(struct file *filep, unsigned long arg) inode = filep->f_inode; priv = (struct cxd56_gnss_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -2270,7 +2271,7 @@ static void cxd56_gnss_common_signalhandler(uint32_t data, int i; int ret; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return; @@ -2294,7 +2295,7 @@ static void cxd56_gnss_common_signalhandler(uint32_t data, fw_gd_setnotifymask(sigtype, FALSE); } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); } #endif /* CONFIG_CXD56_GNSS_NSIGNALRECEIVERS != 0 */ @@ -2378,7 +2379,7 @@ static void cxd56_gnss_default_sighandler(uint32_t data, void *userdata) break; } - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return; @@ -2386,7 +2387,7 @@ static void cxd56_gnss_default_sighandler(uint32_t data, void *userdata) poll_notify(priv->fds, CONFIG_CXD56_GNSS_NPOLLWAITERS, POLLIN); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); #if CONFIG_CXD56_GNSS_NSIGNALRECEIVERS != 0 cxd56_gnss_common_signalhandler(data, userdata); @@ -2594,7 +2595,7 @@ static int cxd56_gnss_open(struct file *filep) usleep(100 * 1000); } - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -2695,7 +2696,7 @@ static int cxd56_gnss_close(struct file *filep) inode = filep->f_inode; priv = (struct cxd56_gnss_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -2842,7 +2843,7 @@ static int cxd56_gnss_ioctl(struct file *filep, int cmd, return -EINVAL; } - ret = nxsem_wait(&priv->ioctllock); + ret = nxmutex_lock(&priv->ioctllock); if (ret < 0) { return ret; @@ -2850,8 +2851,7 @@ static int cxd56_gnss_ioctl(struct file *filep, int cmd, ret = g_cmdlist[cmd](filep, arg); - nxsem_post(&priv->ioctllock); - + nxmutex_unlock(&priv->ioctllock); return ret; } @@ -2882,7 +2882,7 @@ static int cxd56_gnss_poll(struct file *filep, struct pollfd *fds, inode = filep->f_inode; priv = (struct cxd56_gnss_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -2933,7 +2933,7 @@ static int cxd56_gnss_poll(struct file *filep, struct pollfd *fds, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -3015,7 +3015,7 @@ static int cxd56_gnss_register(const char *devpath) memset(priv, 0, sizeof(struct cxd56_gnss_dev_s)); - ret = nxsem_init(&priv->devsem, 0, 1); + ret = nxmutex_init(&priv->devlock); if (ret < 0) { gnsserr("Failed to initialize gnss devsem!\n"); @@ -3031,7 +3031,7 @@ static int cxd56_gnss_register(const char *devpath) nxsem_set_protocol(&priv->apiwait, SEM_PRIO_NONE); - ret = nxsem_init(&priv->ioctllock, 0, 1); + ret = nxmutex_init(&priv->ioctllock); if (ret < 0) { gnsserr("Failed to initialize gnss ioctllock!\n"); diff --git a/arch/arm/src/cxd56xx/cxd56_hostif.c b/arch/arm/src/cxd56xx/cxd56_hostif.c index a86db14fb7e18..087c1587f563b 100644 --- a/arch/arm/src/cxd56xx/cxd56_hostif.c +++ b/arch/arm/src/cxd56xx/cxd56_hostif.c @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -89,7 +90,7 @@ struct cxd56_hifdev_s uint32_t flags; const void *buffer; size_t len; - sem_t exclsem; + mutex_t lock; int crefs; }; @@ -220,14 +221,14 @@ static int hif_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; } @@ -238,8 +239,7 @@ static int hif_open(struct file *filep) priv->flags |= O_NONBLOCK; } - nxsem_post(&priv->exclsem); - + nxmutex_unlock(&priv->lock); return OK; } @@ -256,13 +256,12 @@ static int hif_close(struct file *filep) /* Decrement reference counter */ - nxsem_wait_uninterruptible(&priv->exclsem); + nxmutex_lock(&priv->lock); DEBUGASSERT(priv->crefs > 0); priv->crefs--; - nxsem_post(&priv->exclsem); - + nxmutex_unlock(&priv->lock); return OK; } @@ -424,7 +423,7 @@ static int hif_initialize(struct hostif_buff_s *buffer) return ret; } - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->crefs = 0; } diff --git a/arch/arm/src/cxd56xx/cxd56_i2c.c b/arch/arm/src/cxd56xx/cxd56_i2c.c index 165a16c3c19fb..931a6e57f499a 100644 --- a/arch/arm/src/cxd56xx/cxd56_i2c.c +++ b/arch/arm/src/cxd56xx/cxd56_i2c.c @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -79,7 +80,7 @@ struct cxd56_i2cdev_s int8_t port; /* Port number */ uint32_t base_freq; /* branch frequency */ - sem_t mutex; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t wait; /* Place to wait for transfer completion */ struct wdog_s timeout; /* watchdog to timeout when bus hung */ uint32_t frequency; /* Current I2C frequency */ @@ -103,6 +104,8 @@ static struct cxd56_i2cdev_s g_i2c0dev = .port = 0, .base = CXD56_SCU_I2C0_BASE, .irqid = CXD56_IRQ_SCU_I2C0, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .refs = 0, }; #endif @@ -112,6 +115,8 @@ static struct cxd56_i2cdev_s g_i2c1dev = .port = 1, .base = CXD56_SCU_I2C1_BASE, .irqid = CXD56_IRQ_SCU_I2C1, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .refs = 0, }; #endif @@ -121,6 +126,8 @@ static struct cxd56_i2cdev_s g_i2c2dev = .port = 2, .base = CXD56_I2CM_BASE, .irqid = CXD56_IRQ_I2CM, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .refs = 0, }; #endif @@ -129,9 +136,6 @@ static struct cxd56_i2cdev_s g_i2c2dev = * Private Functions ****************************************************************************/ -static inline int i2c_takesem(sem_t *sem); -static inline int i2c_givesem(sem_t *sem); - static inline uint32_t i2c_reg_read(struct cxd56_i2cdev_s *priv, uint32_t offset); static inline void i2c_reg_write(struct cxd56_i2cdev_s *priv, @@ -158,24 +162,6 @@ static int cxd56_i2c_transfer_scu(struct i2c_master_s *dev, struct i2c_msg_s *msgs, int count); #endif -/**************************************************************************** - * Name: i2c_takesem - ****************************************************************************/ - -static inline int i2c_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: i2c_givesem - ****************************************************************************/ - -static inline int i2c_givesem(sem_t *sem) -{ - return nxsem_post(sem); -} - /**************************************************************************** * Name: cxd56_i2c_pincontrol * @@ -372,7 +358,7 @@ static void cxd56_i2c_timeout(wdparm_t arg) irqstate_t flags = enter_critical_section(); priv->error = -ENODEV; - i2c_givesem(&priv->wait); + nxsem_post(&priv->wait); leave_critical_section(flags); } @@ -479,7 +465,7 @@ static int cxd56_i2c_interrupt(int irq, void *context, void *arg) ret = wd_cancel(&priv->timeout); if (ret == OK) { - i2c_givesem(&priv->wait); + nxsem_post(&priv->wait); } } @@ -542,7 +528,7 @@ static int cxd56_i2c_receive(struct cxd56_i2cdev_s *priv, int last) i2c_reg_rmw(priv, CXD56_IC_INTR_MASK, INTR_RX_FULL, INTR_RX_FULL); leave_critical_section(flags); - i2c_takesem(&priv->wait); + nxsem_wait_uninterruptible(&priv->wait); if (priv->error != OK) { @@ -589,8 +575,7 @@ static int cxd56_i2c_send(struct cxd56_i2cdev_s *priv, int last) i2c_reg_rmw(priv, CXD56_IC_INTR_MASK, INTR_TX_EMPTY, INTR_TX_EMPTY); leave_critical_section(flags); - i2c_takesem(&priv->wait); - + nxsem_wait_uninterruptible(&priv->wait); return 0; } @@ -618,7 +603,7 @@ static int cxd56_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - i2c_takesem(&priv->mutex); + nxmutex_lock(&priv->lock); /* Check wait semaphore value. If the value is not 0, the transfer can not * be performed normally. @@ -696,8 +681,7 @@ static int cxd56_i2c_transfer(struct i2c_master_s *dev, cxd56_i2c_clock_gate_enable(priv->port); - i2c_givesem(&priv->mutex); - + nxmutex_unlock(&priv->lock); return ret; } @@ -835,7 +819,7 @@ static int cxd56_i2c_transfer_scu(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - i2c_takesem(&priv->mutex); + nxmutex_lock(&priv->lock); /* Apply frequency for request msgs */ @@ -869,8 +853,7 @@ static int cxd56_i2c_transfer_scu(struct i2c_master_s *dev, } } - i2c_givesem(&priv->mutex); - + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -957,7 +940,7 @@ struct i2c_master_s *cxd56_i2cbus_initialize(int port) #ifdef CONFIG_CXD56_I2C0 if (port == 0) { - priv = &g_i2c0dev; + priv = &g_i2c0dev; # ifndef CONFIG_CXD56_I2C0_SCUSEQ priv->dev.ops = &cxd56_i2c_ops; # else @@ -969,7 +952,7 @@ struct i2c_master_s *cxd56_i2cbus_initialize(int port) #ifdef CONFIG_CXD56_I2C1 if (port == 1) { - priv = &g_i2c1dev; + priv = &g_i2c1dev; # ifndef CONFIG_CXD56_I2C1_SCUSEQ priv->dev.ops = &cxd56_i2c_ops; # else @@ -1034,10 +1017,6 @@ struct i2c_master_s *cxd56_i2cbus_initialize(int port) cxd56_i2c_pincontrol(port, true); - nxsem_init(&priv->mutex, 0, 1); - nxsem_init(&priv->wait, 0, 0); - nxsem_set_protocol(&priv->wait, SEM_PRIO_NONE); - /* Attach Interrupt Handler */ irq_attach(priv->irqid, cxd56_i2c_interrupt, priv); @@ -1100,8 +1079,6 @@ int cxd56_i2cbus_uninitialize(struct i2c_master_s *dev) irq_detach(priv->irqid); wd_cancel(&priv->timeout); - nxsem_destroy(&priv->mutex); - nxsem_destroy(&priv->wait); return OK; } diff --git a/arch/arm/src/cxd56xx/cxd56_icc.c b/arch/arm/src/cxd56xx/cxd56_icc.c index ca0622b272018..01c2441068e36 100644 --- a/arch/arm/src/cxd56xx/cxd56_icc.c +++ b/arch/arm/src/cxd56xx/cxd56_icc.c @@ -145,21 +145,6 @@ static struct iccdev_s *g_cpumsg[NCPUS]; * Private Functions ****************************************************************************/ -static int icc_semtake(sem_t *semid) -{ - return nxsem_wait_uninterruptible(semid); -} - -static int icc_semtrytake(sem_t *semid) -{ - return nxsem_trywait(semid); -} - -static void icc_semgive(sem_t *semid) -{ - nxsem_post(semid); -} - static struct iccdev_s *icc_getprotocol(int protoid) { if (protoid < 0 || protoid >= NPROTOCOLS) @@ -234,7 +219,7 @@ static int icc_irqhandler(int cpuid, uint32_t word[2]) sq_addlast((sq_entry_t *)req, &priv->recvq); - icc_semgive(&priv->rxwait); + nxsem_post(&priv->rxwait); /* If signal registered by cxd56_iccnotify(), then send POSIX signal to * process. @@ -295,7 +280,7 @@ static int icc_msghandler(int cpuid, int protoid, uint32_t pdata, static void icc_rxtimeout(wdparm_t arg) { struct iccdev_s *priv = (struct iccdev_s *)arg; - icc_semgive(&priv->rxwait); + nxsem_post(&priv->rxwait); } static int icc_recv(struct iccdev_s *priv, iccmsg_t *msg, int32_t ms) @@ -308,7 +293,7 @@ static int icc_recv(struct iccdev_s *priv, iccmsg_t *msg, int32_t ms) { /* Try to take the semaphore without waiging. */ - ret = icc_semtrytake(&priv->rxwait); + ret = nxsem_trywait(&priv->rxwait); if (ret < 0) { return ret; @@ -316,7 +301,7 @@ static int icc_recv(struct iccdev_s *priv, iccmsg_t *msg, int32_t ms) } else if (ms == 0) { - icc_semtake(&priv->rxwait); + nxsem_wait_uninterruptible(&priv->rxwait); } else { @@ -324,8 +309,7 @@ static int icc_recv(struct iccdev_s *priv, iccmsg_t *msg, int32_t ms) timo = ms * 1000 / CONFIG_USEC_PER_TICK; wd_start(&priv->rxtimeout, timo, icc_rxtimeout, (wdparm_t)priv); - icc_semtake(&priv->rxwait); - + nxsem_wait_uninterruptible(&priv->rxwait); wd_cancel(&priv->rxtimeout); } diff --git a/arch/arm/src/cxd56xx/cxd56_powermgr.c b/arch/arm/src/cxd56xx/cxd56_powermgr.c index 264d5ad0bc2fa..0d99196233a6c 100644 --- a/arch/arm/src/cxd56xx/cxd56_powermgr.c +++ b/arch/arm/src/cxd56xx/cxd56_powermgr.c @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -149,7 +150,6 @@ static int cxd56_pm_do_callback(uint8_t id, static int cxd56_pm_needcallback(uint32_t target, struct cxd56_pm_target_id_s *table); static void cxd56_pm_clkchange(struct cxd56_pm_message_s *message); -static int cxd56_pm_semtake(sem_t *id); static void cxd56_pm_checkfreqlock(void); static int cxd56_pm_maintask(int argc, char *argv[]); #if defined(CONFIG_CXD56_HOT_SLEEP) @@ -167,8 +167,8 @@ static int cxd56_pmmsghandler(int cpuid, int protoid, uint32_t pdata, static struct cxd56_pm_target_id_s g_target_id_table; static struct file g_queuedesc; static sem_t g_bootsync; -static sem_t g_regcblock; -static sem_t g_freqlock; +static mutex_t g_regcblock; +static mutex_t g_freqlock; static sem_t g_freqlockwait; static dq_queue_t g_cbqueue; static sq_queue_t g_freqlockqueue; @@ -179,11 +179,6 @@ static int g_freqlock_flag; static struct pm_cpu_wakelock_s g_wlock = PM_CPUWAKELOCK_INIT(PM_CPUWAKELOCK_TAG('P', 'M', 0)); -static int cxd56_pm_semtake(sem_t *id) -{ - return nxsem_wait_uninterruptible(id); -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -311,13 +306,13 @@ static void cxd56_pm_clkchange(struct cxd56_pm_message_s *message) return; } - cxd56_pm_semtake(&g_regcblock); + nxmutex_lock(&g_regcblock); ret = cxd56_pm_do_callback(id, &g_target_id_table); cxd56_pmsendmsg(mid, ret); - nxsem_post(&g_regcblock); + nxmutex_unlock(&g_regcblock); } static void cxd56_pm_checkfreqlock(void) @@ -337,7 +332,7 @@ static void cxd56_pm_checkfreqlock(void) { g_freqlock_flag = flag; cxd56_pmsendmsg(MSGID_FREQLOCK, flag); - cxd56_pm_semtake(&g_freqlockwait); + nxsem_wait_uninterruptible(&g_freqlockwait); } } @@ -486,12 +481,12 @@ void *cxd56_pm_register_callback(uint32_t target, { struct pm_cbentry_s *entry = NULL; - cxd56_pm_semtake(&g_regcblock); + nxmutex_lock(&g_regcblock); entry = (struct pm_cbentry_s *)kmm_malloc(sizeof(struct pm_cbentry_s)); if (entry == NULL) { - nxsem_post(&g_regcblock); + nxmutex_unlock(&g_regcblock); return NULL; } @@ -499,19 +494,19 @@ void *cxd56_pm_register_callback(uint32_t target, entry->callback = callback; dq_addlast((dq_entry_t *)entry, &g_cbqueue); - nxsem_post(&g_regcblock); + nxmutex_unlock(&g_regcblock); return (void *)entry; } void cxd56_pm_unregister_callback(void *handle) { - cxd56_pm_semtake(&g_regcblock); + nxmutex_lock(&g_regcblock); dq_rem((dq_entry_t *)handle, &g_cbqueue); kmm_free(handle); - nxsem_post(&g_regcblock); + nxmutex_unlock(&g_regcblock); } static int cxd56_pmmsghandler(int cpuid, int protoid, uint32_t pdata, @@ -583,7 +578,7 @@ void up_pm_acquire_freqlock(struct pm_cpu_freqlock_s *lock) up_pm_acquire_wakelock(&g_wlock); - cxd56_pm_semtake(&g_freqlock); + nxmutex_lock(&g_freqlock); if (lock->flag == PM_CPUFREQLOCK_FLAG_HOLD) { @@ -608,8 +603,7 @@ void up_pm_acquire_freqlock(struct pm_cpu_freqlock_s *lock) lock->count++; - nxsem_post(&g_freqlock); - + nxmutex_unlock(&g_freqlock); up_pm_release_wakelock(&g_wlock); } @@ -640,7 +634,7 @@ void up_pm_release_freqlock(struct pm_cpu_freqlock_s *lock) up_pm_acquire_wakelock(&g_wlock); - cxd56_pm_semtake(&g_freqlock); + nxmutex_lock(&g_freqlock); for (entry = sq_peek(&g_freqlockqueue); entry; entry = sq_next(entry)) { @@ -657,8 +651,7 @@ void up_pm_release_freqlock(struct pm_cpu_freqlock_s *lock) } exit: - nxsem_post(&g_freqlock); - + nxmutex_unlock(&g_freqlock); up_pm_release_wakelock(&g_wlock); } @@ -683,7 +676,7 @@ int up_pm_get_freqlock_count(struct pm_cpu_freqlock_s *lock) DEBUGASSERT(lock); - cxd56_pm_semtake(&g_freqlock); + nxmutex_lock(&g_freqlock); for (entry = sq_peek(&g_freqlockqueue); entry; entry = sq_next(entry)) { @@ -694,7 +687,7 @@ int up_pm_get_freqlock_count(struct pm_cpu_freqlock_s *lock) } } - nxsem_post(&g_freqlock); + nxmutex_unlock(&g_freqlock); return count; } @@ -831,13 +824,13 @@ int cxd56_pm_initialize(void) sq_init(&g_freqlockqueue); sq_init(&g_wakelockqueue); - ret = nxsem_init(&g_regcblock, 0, 1); + ret = nxmutex_init(&g_regcblock); if (ret < 0) { return ret; } - ret = nxsem_init(&g_freqlock, 0, 1); + ret = nxmutex_init(&g_freqlock); if (ret < 0) { return ret; @@ -867,8 +860,7 @@ int cxd56_pm_initialize(void) /* wait until cxd56_pm_maintask thread is ready */ - cxd56_pm_semtake(&g_bootsync); - + nxsem_wait_uninterruptible(&g_bootsync); return OK; } diff --git a/arch/arm/src/cxd56xx/cxd56_scu.c b/arch/arm/src/cxd56xx/cxd56_scu.c index 31e9812890965..d912669e06f77 100644 --- a/arch/arm/src/cxd56xx/cxd56_scu.c +++ b/arch/arm/src/cxd56xx/cxd56_scu.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -186,8 +187,8 @@ struct decimator_s struct cxd56_scudev_s { - sem_t syncwait; /* Semaphore for synchronize with SCU firmware */ - sem_t syncexc; /* Semaphore for exclusive access to sync */ + sem_t syncwait; /* Semaphore for synchronize with SCU firmware */ + mutex_t synclock; /* Mutex for exclusive access to sync */ /* SCU hardware resource management bitmaps (1 = allocated) */ @@ -230,8 +231,6 @@ static int8_t seq_alloc(void); static inline void seq_free(int8_t sid); static inline int8_t oneshot_alloc(void); static inline void oneshot_free(int8_t tid); -static int seq_semtake(sem_t *id); -static void seq_semgive(sem_t *id); static void seq_fifosetactive(struct seq_s *seq, int fifoid); static void seq_fifosetinactive(struct seq_s *seq, int fifoid); static int seq_fifoisactive(struct seq_s *seq, int fifoid); @@ -359,24 +358,6 @@ static const struct coeff_addr_s g_caddrs[3][2] = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: seq_semtake - ****************************************************************************/ - -static int seq_semtake(sem_t *id) -{ - return nxsem_wait_uninterruptible(id); -} - -/**************************************************************************** - * Name: seq_semgive - ****************************************************************************/ - -static void seq_semgive(sem_t *id) -{ - nxsem_post(id); -} - /**************************************************************************** * Name: seq_fifosetactive ****************************************************************************/ @@ -1083,7 +1064,7 @@ static int seq_oneshot(int bustype, int slave, uint16_t *inst, /* Wait for one shot is done */ - seq_semtake(&priv->oneshotwait[tid]); + nxsem_wait_uninterruptible(&priv->oneshotwait[tid]); /* Disable interrupt for one shot sequencer */ @@ -1451,7 +1432,7 @@ static void seq_sync(struct seq_s *seq, int req) { struct cxd56_scudev_s *priv = &g_scudev; - seq_semtake(&priv->syncexc); + nxmutex_lock(&priv->synclock); /* Save current request */ @@ -1468,11 +1449,11 @@ static void seq_sync(struct seq_s *seq, int req) /* Wait for interrupt from SCU firmware */ - seq_semtake(&priv->syncwait); + nxsem_wait_uninterruptible(&priv->syncwait); priv->currentreq = 0; - seq_semgive(&priv->syncexc); + nxmutex_unlock(&priv->synclock); } /**************************************************************************** @@ -1628,7 +1609,7 @@ static void seq_handleoneshot(struct cxd56_scudev_s *priv, uint32_t intr) { putreg32(bit, SCU_INT_CLEAR_MAIN); - seq_semgive(&priv->oneshotwait[i]); + nxsem_post(&priv->oneshotwait[i]); } } } @@ -1655,7 +1636,7 @@ static void seq_handleisopdoneintr(struct cxd56_scudev_s *priv, putreg32(1 << 27, SCU_INT_DISABLE_MAIN); putreg32(1 << 27, SCU_INT_CLEAR_MAIN); - seq_semgive(&priv->syncwait); + nxsem_post(&priv->syncwait); } } @@ -1754,7 +1735,7 @@ static int seq_scuirqhandler(int irq, void *context, void *arg) tid = out - 1; priv->oneshoterr[tid] = -EIO; - seq_semgive(&priv->oneshotwait[tid]); + nxsem_post(&priv->oneshotwait[tid]); } } } @@ -2886,7 +2867,7 @@ static void seq_fifodmadone(DMA_HANDLE handle, uint8_t status, void *arg) { struct scufifo_s *fifo = (struct scufifo_s *)arg; fifo->dmaresult = status; - seq_semgive(&fifo->dmawait); + nxsem_post(&fifo->dmawait); } #else /**************************************************************************** @@ -3045,7 +3026,7 @@ int seq_read(struct seq_s *seq, int fifoid, char *buffer, int length) /* Wait for DMA is done */ - seq_semtake(&fifo->dmawait); + nxsem_wait_uninterruptible(&fifo->dmawait); if (fifo->dmaresult) { /* ERROR */ @@ -3446,7 +3427,7 @@ void scu_initialize(void) memset(priv, 0, sizeof(struct cxd56_scudev_s)); - nxsem_init(&priv->syncexc, 0, 1); + nxmutex_init(&priv->synclock); nxsem_init(&priv->syncwait, 0, 0); nxsem_set_protocol(&priv->syncwait, SEM_PRIO_NONE); @@ -3520,7 +3501,7 @@ void scu_uninitialize(void) cxd56_scuseq_clock_disable(); nxsem_destroy(&priv->syncwait); - nxsem_destroy(&priv->syncexc); + nxmutex_destroy(&priv->synclock); for (i = 0; i < 3; i++) { diff --git a/arch/arm/src/cxd56xx/cxd56_sdhci.c b/arch/arm/src/cxd56xx/cxd56_sdhci.c index e9012f8ab3c00..9084db6932a16 100644 --- a/arch/arm/src/cxd56xx/cxd56_sdhci.c +++ b/arch/arm/src/cxd56xx/cxd56_sdhci.c @@ -308,8 +308,6 @@ struct cxd56_sdhcregs_s /* Low-level helpers ********************************************************/ -static int cxd56_takesem(struct cxd56_sdiodev_s *priv); -#define cxd56_givesem(priv) (nxsem_post(&(priv)->waitsem)) static void cxd56_configwaitints(struct cxd56_sdiodev_s *priv, uint32_t waitints, sdio_eventset_t waitevents, sdio_eventset_t wkupevents); @@ -484,27 +482,6 @@ static uint32_t cxd56_sdhci_adma_dscr[CXD56_SDIO_MAX_LEN_ADMA_DSCR * 2]; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: cxd56_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int cxd56_takesem(struct cxd56_sdiodev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: cxd56_configwaitints * @@ -1006,7 +983,7 @@ static void cxd56_endwait(struct cxd56_sdiodev_s *priv, /* Wake up the waiting thread */ - cxd56_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2599,7 +2576,7 @@ static sdio_eventset_t cxd56_sdio_eventwait(struct sdio_dev_s *dev) * there will be no wait. */ - ret = cxd56_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and @@ -2641,7 +2618,7 @@ static sdio_eventset_t cxd56_sdio_eventwait(struct sdio_dev_s *dev) cxd56_configwaitints(priv, 0, 0, 0); #ifdef CONFIG_SDIO_DMA - priv->xfrflags = 0; + priv->xfrflags = 0; if (priv->aligned_buffer) { if (priv->dma_cmd == MMCSD_CMD17 || priv->dma_cmd == MMCSD_CMD18) @@ -2655,7 +2632,6 @@ static sdio_eventset_t cxd56_sdio_eventwait(struct sdio_dev_s *dev) /* Free aligned buffer */ kmm_free(priv->aligned_buffer); - priv->aligned_buffer = NULL; } #endif diff --git a/arch/arm/src/cxd56xx/cxd56_sph.c b/arch/arm/src/cxd56xx/cxd56_sph.c index 13a13cf9a6e65..310686d5908fd 100644 --- a/arch/arm/src/cxd56xx/cxd56_sph.c +++ b/arch/arm/src/cxd56xx/cxd56_sph.c @@ -78,8 +78,6 @@ struct sph_dev_s static int sph_open(struct file *filep); static int sph_ioctl(struct file *filep, int cmd, unsigned long arg); -static int sph_semtake(sem_t *id); -static void sph_semgive(sem_t *id); static int sph_lock(struct sph_dev_s *priv); static int sph_trylock(struct sph_dev_s *priv); static inline int sph_unlock(struct sph_dev_s *priv); @@ -148,16 +146,6 @@ static int sph_ioctl(struct file *filep, int cmd, unsigned long arg) return ret; } -static int sph_semtake(sem_t *id) -{ - return nxsem_wait_uninterruptible(id); -} - -static void sph_semgive(sem_t *id) -{ - nxsem_post(id); -} - static int sph_lock(struct sph_dev_s *priv) { uint32_t sts; @@ -182,7 +170,7 @@ static int sph_lock(struct sph_dev_s *priv) sts = getreg32(CXD56_SPH_STS(priv->id)); if (sph_state_busy(sts)) { - sph_semtake(&priv->wait); + nxsem_wait_uninterruptible(&priv->wait); } /* Get latest status for determining locked owner. */ @@ -268,8 +256,7 @@ static int cxd56_sphirqhandler(int irq, void *context, void *arg) /* Give semaphore for hardware semaphore is locked */ - sph_semgive(&g_sphdev[id].wait); - + nxsem_post(&g_sphdev[id].wait); return OK; } diff --git a/arch/arm/src/cxd56xx/cxd56_spi.c b/arch/arm/src/cxd56xx/cxd56_spi.c index 587a8db229113..313b73c024260 100644 --- a/arch/arm/src/cxd56xx/cxd56_spi.c +++ b/arch/arm/src/cxd56xx/cxd56_spi.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -69,7 +69,7 @@ struct cxd56_spidev_s #ifdef CONFIG_CXD56_SPI_INTERRUPTS uint8_t spiirq; /* SPI IRQ number */ #endif - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 to 16) */ @@ -402,13 +402,13 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - /* Take the semaphore (perhaps waiting) */ + /* Take the mutex (perhaps waiting) */ - return nxsem_wait_uninterruptible(&priv->exclsem); + return nxmutex_lock(&priv->lock); } else { - return nxsem_post(&priv->exclsem); + return nxmutex_unlock(&priv->lock); } } @@ -1244,9 +1244,9 @@ struct spi_dev_s *cxd56_spibus_initialize(int port) spi_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_CXD56_SPI3_SCUSEQ /* Enable the SPI, but not enable port 3 when SCU support enabled. diff --git a/arch/arm/src/cxd56xx/cxd56_sysctl.c b/arch/arm/src/cxd56xx/cxd56_sysctl.c index 06781693f3ce5..d121167b942a1 100644 --- a/arch/arm/src/cxd56xx/cxd56_sysctl.c +++ b/arch/arm/src/cxd56xx/cxd56_sysctl.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -57,8 +58,6 @@ ****************************************************************************/ static int sysctl_ioctl(struct file *filep, int cmd, unsigned long arg); -static int sysctl_semtake(sem_t *semid); -static void sysctl_semgive(sem_t *semid); static int sysctl_rxhandler(int cpuid, int protoid, uint32_t pdata, uint32_t data, void *userdata); @@ -67,7 +66,7 @@ static int sysctl_rxhandler(int cpuid, int protoid, * Private Data ****************************************************************************/ -static sem_t g_exc; +static mutex_t g_lock; static sem_t g_sync; static int g_errcode = 0; @@ -96,16 +95,6 @@ static int sysctl_ioctl(struct file *filep, int cmd, unsigned long arg) return ret; } -static int sysctl_semtake(sem_t *semid) -{ - return nxsem_wait_uninterruptible(semid); -} - -static void sysctl_semgive(sem_t *semid) -{ - nxsem_post(semid); -} - static int sysctl_rxhandler(int cpuid, int protoid, uint32_t pdata, uint32_t data, void *userdata) @@ -115,8 +104,7 @@ static int sysctl_rxhandler(int cpuid, int protoid, g_errcode = (int)data; - sysctl_semgive(&g_sync); - + nxsem_post(&g_sync); return OK; } @@ -131,7 +119,7 @@ int cxd56_sysctlcmd(uint8_t id, uint32_t data) /* Get exclusive access */ - ret = sysctl_semtake(&g_exc); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -146,17 +134,17 @@ int cxd56_sysctlcmd(uint8_t id, uint32_t data) ret = cxd56_iccsend(CXD56_PROTO_SYSCTL, &msg, SYSCTL_TIMEOUT); if (ret < 0) { - sysctl_semgive(&g_exc); + nxmutex_unlock(&g_lock); _err("Timeout.\n"); return ret; } /* Wait for reply message from system CPU */ - ret = sysctl_semtake(&g_sync); + ret = nxsem_wait_uninterruptible(&g_sync); if (ret < 0) { - sysctl_semgive(&g_exc); + nxmutex_unlock(&g_lock); return ret; } @@ -164,8 +152,7 @@ int cxd56_sysctlcmd(uint8_t id, uint32_t data) ret = g_errcode; - sysctl_semgive(&g_exc); - + nxmutex_unlock(&g_lock); return ret; } @@ -173,7 +160,7 @@ void cxd56_sysctlinitialize(void) { cxd56_iccinit(CXD56_PROTO_SYSCTL); - nxsem_init(&g_exc, 0, 1); + nxmutex_init(&g_lock); nxsem_init(&g_sync, 0, 0); nxsem_set_protocol(&g_sync, SEM_PRIO_NONE); diff --git a/arch/arm/src/cxd56xx/cxd56_uart0.c b/arch/arm/src/cxd56xx/cxd56_uart0.c index bb00f6191e90d..9b0a2fb1b4b5c 100644 --- a/arch/arm/src/cxd56xx/cxd56_uart0.c +++ b/arch/arm/src/cxd56xx/cxd56_uart0.c @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include @@ -71,8 +71,6 @@ static ssize_t uart0_read(struct file *filep, char *buffer, size_t len); static ssize_t uart0_write(struct file *filep, const char *buffer, size_t len); -static int uart0_semtake(sem_t *id); -static void uart0_semgive(sem_t *id); /**************************************************************************** * FarAPI prototypes @@ -99,30 +97,12 @@ static const struct file_operations g_uart0fops = .write = uart0_write }; -static sem_t g_lock; +static mutex_t g_lock; /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: uart0_semtake - ****************************************************************************/ - -static int uart0_semtake(sem_t *id) -{ - return nxsem_wait_uninterruptible(id); -} - -/**************************************************************************** - * Name: uart0_semgive - ****************************************************************************/ - -static void uart0_semgive(sem_t *id) -{ - nxsem_post(id); -} - /**************************************************************************** * Name: uart0_open ****************************************************************************/ @@ -218,13 +198,12 @@ static ssize_t uart0_read(struct file *filep, { int ret; - uart0_semtake(&g_lock); + nxmutex_lock(&g_lock); ret = fw_pd_uartreceive(0, buffer, len, ((filep->f_oflags & O_NONBLOCK) != 0)); - uart0_semgive(&g_lock); - + nxmutex_unlock(&g_lock); return (ssize_t)ret; } @@ -237,13 +216,12 @@ static ssize_t uart0_write(struct file *filep, { int ret; - uart0_semtake(&g_lock); + nxmutex_lock(&g_lock); ret = fw_pd_uartsend(0, (void *)buffer, len, ((filep->f_oflags & O_NONBLOCK) != 0)); - uart0_semgive(&g_lock); - + nxmutex_unlock(&g_lock); return (ssize_t)ret; } @@ -255,7 +233,7 @@ int cxd56_uart0initialize(const char *devname) { int ret; - nxsem_init(&g_lock, 0, 1); + nxmutex_init(&g_lock); ret = register_driver(devname, &g_uart0fops, 0666, NULL); if (ret != 0) @@ -273,7 +251,7 @@ int cxd56_uart0initialize(const char *devname) void cxd56_uart0uninitialize(const char *devname) { unregister_driver(devname); - nxsem_destroy(&g_lock); + nxmutex_destroy(&g_lock); } #endif /* CONFIG_CXD56_UART0 */ diff --git a/arch/arm/src/cxd56xx/cxd56_udmac.c b/arch/arm/src/cxd56xx/cxd56_udmac.c index d8b93f45bf807..1d1a89389de7c 100644 --- a/arch/arm/src/cxd56xx/cxd56_udmac.c +++ b/arch/arm/src/cxd56xx/cxd56_udmac.c @@ -33,6 +33,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -67,8 +68,8 @@ struct dma_channel_s struct dma_controller_s { - sem_t exclsem; /* Protects channel table */ - sem_t chansem; /* Count of free channels */ + mutex_t lock; /* Protects channel table */ + sem_t chansem; /* Count of free channels */ }; /**************************************************************************** @@ -238,7 +239,7 @@ void cxd56_udmainitialize(void) /* Initialize the channel list */ - nxsem_init(&g_dmac.exclsem, 0, 1); + nxmutex_init(&g_dmac.lock); nxsem_init(&g_dmac.chansem, 0, CXD56_DMA_NCHANNELS); for (i = 0; i < CXD56_DMA_NCHANNELS; i++) @@ -306,7 +307,7 @@ DMA_HANDLE cxd56_udmachannel(void) /* Get exclusive access to the DMA channel list */ - ret = nxsem_wait_uninterruptible(&g_dmac.exclsem); + ret = nxmutex_lock(&g_dmac.lock); if (ret < 0) { nxsem_post(&g_dmac.chansem); @@ -332,7 +333,7 @@ DMA_HANDLE cxd56_udmachannel(void) } } - nxsem_post(&g_dmac.exclsem); + nxmutex_unlock(&g_dmac.lock); /* Attach DMA interrupt vector */ diff --git a/arch/arm/src/efm32/efm32_dma.c b/arch/arm/src/efm32/efm32_dma.c index 4ab79bceb62da..37976b19414d7 100644 --- a/arch/arm/src/efm32/efm32_dma.c +++ b/arch/arm/src/efm32/efm32_dma.c @@ -33,6 +33,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -67,7 +68,7 @@ struct dma_channel_s struct dma_controller_s { - sem_t exclsem; /* Protects channel table */ + mutex_t lock; /* Protects channel table */ sem_t chansem; /* Count of free channels */ }; @@ -265,7 +266,7 @@ void weak_function arm_dma_initialize(void) /* Initialize the channel list */ - nxsem_init(&g_dmac.exclsem, 0, 1); + nxmutex_init(&g_dmac.lock); nxsem_init(&g_dmac.chansem, 0, EFM32_DMA_NCHANNELS); for (i = 0; i < EFM32_DMA_NCHANNELS; i++) @@ -343,7 +344,7 @@ DMA_HANDLE efm32_dmachannel(void) /* Get exclusive access to the DMA channel list */ - ret = nxsem_wait_uninterruptible(&g_dmac.exclsem); + ret = nxmutex_lock(&g_dmac.lock); if (ret < 0) { nxsem_post(&g_dmac.chansem); @@ -372,7 +373,7 @@ DMA_HANDLE efm32_dmachannel(void) } } - nxsem_post(&g_dmac.exclsem); + nxmutex_unlock(&g_dmac.lock); /* Since we have reserved a DMA descriptor by taking a count from chansem, * it would be a serious logic failure if we could not find a free channel diff --git a/arch/arm/src/efm32/efm32_i2c.c b/arch/arm/src/efm32/efm32_i2c.c index 2889495b089f8..8538771e2d6a0 100644 --- a/arch/arm/src/efm32/efm32_i2c.c +++ b/arch/arm/src/efm32/efm32_i2c.c @@ -53,6 +53,7 @@ #include #include #include +#include #include #include @@ -218,7 +219,7 @@ struct efm32_i2c_priv_s const struct efm32_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 @@ -260,18 +261,12 @@ static inline void efm32_i2c_putreg(struct efm32_i2c_priv_s *priv, static inline void efm32_i2c_modifyreg(struct efm32_i2c_priv_s *priv, uint8_t offset, uint32_t clearbits, uint32_t setbits); -static inline int efm32_i2c_sem_wait(struct efm32_i2c_priv_s *priv); -static int -efm32_i2c_sem_wait_noncancelable(struct efm32_i2c_priv_s *priv); #ifdef CONFIG_EFM32_I2C_DYNTIMEO static uint32_t efm32_i2c_toticks(int msgc, struct i2c_msg_s *msgs); #endif /* CONFIG_EFM32_I2C_DYNTIMEO */ static inline int efm32_i2c_sem_waitdone(struct efm32_i2c_priv_s *priv); -static inline void efm32_i2c_sem_post(struct efm32_i2c_priv_s *priv); -static inline void efm32_i2c_sem_init(struct efm32_i2c_priv_s *priv); -static inline void efm32_i2c_sem_destroy(struct efm32_i2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void efm32_i2c_tracereset(struct efm32_i2c_priv_s *priv); @@ -335,6 +330,10 @@ static struct efm32_i2c_priv_s efm32_i2c0_priv = .ops = &efm32_i2c_ops, .config = &efm32_i2c0_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .result = I2CRESULT_NONE, .msgc = 0, .msgv = NULL, @@ -362,6 +361,10 @@ static struct efm32_i2c_priv_s efm32_i2c1_priv = .ops = &efm32_i2c_ops, .config = &efm32_i2c1_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .result = I2CRESULT_NONE, .msgc = 0, .msgv = NULL, @@ -459,34 +462,6 @@ static const char *efm32_i2c_state_str(int i2c_state) } #endif -/**************************************************************************** - * Name: efm32_i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. May be interrupted by - * a signal. - * - ****************************************************************************/ - -static inline int efm32_i2c_sem_wait(struct efm32_i2c_priv_s *priv) -{ - return nxsem_wait(&priv->sem_excl); -} - -/**************************************************************************** - * Name: efm32_i2c_sem_wait_noncancelable - * - * Description: - * Take the exclusive access, waiting as necessary - * - ****************************************************************************/ - -static int -efm32_i2c_sem_wait_noncancelable(struct efm32_i2c_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - /**************************************************************************** * Name: efm32_i2c_toticks * @@ -628,57 +603,6 @@ static inline int efm32_i2c_sem_waitdone(struct efm32_i2c_priv_s *priv) } #endif -/**************************************************************************** - * Name: efm32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void efm32_i2c_sem_post(struct efm32_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: efm32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void efm32_i2c_sem_init(struct efm32_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: efm32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void efm32_i2c_sem_destroy(struct efm32_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - /**************************************************************************** * Name: efm32_i2c_trace* * @@ -1415,7 +1339,7 @@ static int efm32_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = efm32_i2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1546,7 +1470,7 @@ static int efm32_i2c_transfer(struct i2c_master_s *dev, priv->dcnt = 0; priv->ptr = NULL; - efm32_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -1582,7 +1506,7 @@ int efm32_i2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - ret = efm32_i2c_sem_wait_noncancelable(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1671,7 +1595,7 @@ int efm32_i2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - efm32_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -1721,7 +1645,6 @@ struct i2c_master_s *efm32_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - efm32_i2c_sem_init(priv); efm32_i2c_init(priv); } @@ -1765,9 +1688,6 @@ int efm32_i2cbus_uninitialize(struct i2c_master_s *dev) efm32_i2c_deinit(priv); - /* Release unused resources */ - - efm32_i2c_sem_destroy(priv); return OK; } diff --git a/arch/arm/src/efm32/efm32_spi.c b/arch/arm/src/efm32/efm32_spi.c index 0421e48fb1d0d..a7cc78955e810 100644 --- a/arch/arm/src/efm32/efm32_spi.c +++ b/arch/arm/src/efm32/efm32_spi.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -116,7 +117,7 @@ struct efm32_spidev_s sem_t txdmasem; /* Wait for TX DMA to complete */ #endif - sem_t exclsem; /* Supports mutually exclusive access */ + mutex_t lock; /* Supports mutually exclusive access */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t mode; /* Mode 0,1,2,3 */ @@ -718,11 +719,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1576,9 +1577,9 @@ static int spi_portinitialize(struct efm32_spidev_s *priv) spi_putreg(config, EFM32_USART_CMD_OFFSET, USART_CMD_MASTEREN); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_EFM32_SPI_DMA /* Allocate two DMA channels... one for the RX and one for the TX side of diff --git a/arch/arm/src/efm32/efm32_usbhost.c b/arch/arm/src/efm32/efm32_usbhost.c index 60535480f6c74..d0db19b06cc60 100644 --- a/arch/arm/src/efm32/efm32_usbhost.c +++ b/arch/arm/src/efm32/efm32_usbhost.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -250,7 +251,7 @@ struct efm32_usbhost_s volatile bool connected; /* Connected to device */ volatile bool change; /* Connection change */ volatile bool pscwait; /* True: Thread is waiting for a port event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for a port event */ struct efm32_ctrlinfo_s ep0; /* Root hub port EP0 description */ @@ -303,11 +304,6 @@ static inline void efm32_modifyreg(uint32_t addr, uint32_t clrbits, # define efm32_pktdump(m,b,n) #endif -/* Semaphores ***************************************************************/ - -static int efm32_takesem(sem_t *sem); -#define efm32_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t efm32_getle16(const uint8_t *val); @@ -746,20 +742,6 @@ static inline void efm32_modifyreg(uint32_t addr, uint32_t clrbits, efm32_putreg(addr, (((efm32_getreg(addr)) & ~clrbits) | setbits)); } -/**************************************************************************** - * Name: efm32_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int efm32_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /**************************************************************************** * Name: efm32_getle16 * @@ -1269,7 +1251,7 @@ static void efm32_chan_wakeup(struct efm32_usbhost_s *priv, USBHOST_VTRACE2_CHANWAKEUP_OUT, chan->epno, chan->result); - efm32_givesem(&chan->waitsem); + nxsem_post(chan->waitsem); chan->waiter = false; } @@ -2965,7 +2947,7 @@ static void efm32_gint_connected(struct efm32_usbhost_s *priv) priv->smstate = SMSTATE_ATTACHED; if (priv->pscwait) { - efm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3012,7 +2994,7 @@ static void efm32_gint_disconnected(struct efm32_usbhost_s *priv) if (priv->pscwait) { - efm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3918,7 +3900,7 @@ static int efm32_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = efm32_takesem(&priv->pscsem); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -4099,7 +4081,7 @@ static int efm32_ep0configure(struct usbhost_driver_s *drvr, * structures. */ - ret = efm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4123,7 +4105,7 @@ static int efm32_ep0configure(struct usbhost_driver_s *drvr, efm32_chan_configure(priv, ep0info->inndx); - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4166,7 +4148,7 @@ static int efm32_epalloc(struct usbhost_driver_s *drvr, * structures. */ - ret = efm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4187,7 +4169,7 @@ static int efm32_epalloc(struct usbhost_driver_s *drvr, ret = efm32_xfrep_alloc(priv, epdesc, ep); } - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4220,7 +4202,7 @@ static int efm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* We must have exclusive access to the USB host hardware and structures */ - ret = efm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4252,7 +4234,7 @@ static int efm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) kmm_free(ctrlep); } - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4490,7 +4472,7 @@ static int efm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * structures. */ - ret = efm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4534,7 +4516,7 @@ static int efm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactions exit here */ - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4549,7 +4531,7 @@ static int efm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4577,7 +4559,7 @@ static int efm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = efm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4625,7 +4607,7 @@ static int efm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactins exit here */ - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4641,7 +4623,7 @@ static int efm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4698,7 +4680,7 @@ static ssize_t efm32_transfer(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = efm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -4715,7 +4697,7 @@ static ssize_t efm32_transfer(struct usbhost_driver_s *drvr, nbytes = efm32_out_transfer(priv, chidx, buffer, buflen); } - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nbytes; } @@ -4770,7 +4752,7 @@ static int efm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* We must have exclusive access to the USB host hardware and structures */ - ret = efm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4787,7 +4769,7 @@ static int efm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, ret = efm32_out_asynch(priv, chidx, buffer, buflen, callback, arg); } - efm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4846,7 +4828,7 @@ static int efm32_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake'em up! */ - efm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -4923,7 +4905,7 @@ static int efm32_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - efm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -5277,10 +5259,10 @@ static inline void efm32_sw_initialize(struct efm32_usbhost_s *priv) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/gd32f4/gd32f4xx_dma.c b/arch/arm/src/gd32f4/gd32f4xx_dma.c index fb374baa25eaa..777e7dfb856a5 100644 --- a/arch/arm/src/gd32f4/gd32f4xx_dma.c +++ b/arch/arm/src/gd32f4/gd32f4xx_dma.c @@ -211,32 +211,6 @@ static void gd32_dma_clock_enable(uint32_t dmabase) modifyreg32(regaddr, 0, rcu_en); } -/**************************************************************************** - * Name: gd32_dmasem_take - * - * Description: - * Used to request exclusive access to a DMA channel. - * - ****************************************************************************/ - -static int gd32_dmasem_take(struct gd32_dma_channel_s *dmachan) -{ - return nxsem_wait_uninterruptible(&dmachan->chsem); -} - -/**************************************************************************** - * Name: gd32_dmasem_give - * - * Description: - * Used to free exclusive access to a DMA channel. - * - ****************************************************************************/ - -static inline void gd32_dmasem_give(struct gd32_dma_channel_s *dmachan) -{ - nxsem_post(&dmachan->chsem); -} - /**************************************************************************** * Name: gd32_dma_channel_get * @@ -647,10 +621,9 @@ DMA_HANDLE gd32_dma_channel_alloc(uint8_t periph_req) /* Get exclusive access to the DMA channel */ - ret = gd32_dmasem_take(dmachan); + ret = nxsem_wait_uninterruptible(&dmachan->chsem); if (ret < 0) { - gd32_dmasem_give(dmachan); return NULL; } @@ -692,7 +665,7 @@ void gd32_dma_channel_free(DMA_HANDLE handle) /* Release the channel */ - gd32_dmasem_give(dmachan); + nxsem_post(&dmachan->chsem); } /**************************************************************************** diff --git a/arch/arm/src/gd32f4/gd32f4xx_fmc.c b/arch/arm/src/gd32f4/gd32f4xx_fmc.c index 2716f02b6d5ef..44a35af109667 100644 --- a/arch/arm/src/gd32f4/gd32f4xx_fmc.c +++ b/arch/arm/src/gd32f4/gd32f4xx_fmc.c @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include @@ -41,43 +41,12 @@ * Private Data ****************************************************************************/ -static sem_t g_gd32_fmc_sem = SEM_INITIALIZER(1); +static mutex_t g_gd32_fmc_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: gd32_fmc_sem_lock - * - * Description: - * Lock semaphore - * - * Return Value: - * Zero(OK) - On success - * EINVAL - Invalid attempt to get the semaphore - * ECANCELED - May be returned if the thread is canceled while waiting - * - ****************************************************************************/ - -static int gd32_fmc_sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_gd32_fmc_sem); -} - -/**************************************************************************** - * Name: gd32_fmc_sem_unlock - * - * Description: - * Lock semaphore - * - ****************************************************************************/ - -static void gd32_fmc_sem_unlock(void) -{ - nxsem_post(&g_gd32_fmc_sem); -} - /**************************************************************************** * Name: gd32_fmc_state_get * @@ -203,7 +172,7 @@ int gd32_fmc_unlock(void) { int ret; - ret = gd32_fmc_sem_lock(); + ret = nxmutex_lock(&g_gd32_fmc_lock); if (ret < 0) { return ret; @@ -217,8 +186,7 @@ int gd32_fmc_unlock(void) putreg32(FMC_UNLOCK_KEY1, GD32_FMC_KEY); } - gd32_fmc_sem_unlock(); - + nxmutex_unlock(&g_gd32_fmc_lock); return ret; } @@ -234,7 +202,7 @@ int gd32_fmc_lock(void) { int ret; - ret = gd32_fmc_sem_lock(); + ret = nxmutex_lock(&g_gd32_fmc_lock); if (ret < 0) { return ret; @@ -244,8 +212,7 @@ int gd32_fmc_lock(void) modifyreg32(GD32_FMC_CTL, 0, FMC_CTL_LK); - gd32_fmc_sem_unlock(); - + nxmutex_unlock(&g_gd32_fmc_lock); return ret; } diff --git a/arch/arm/src/gd32f4/gd32f4xx_progmem.c b/arch/arm/src/gd32f4/gd32f4xx_progmem.c index 25c0e52880b06..19156c7d5aaf4 100644 --- a/arch/arm/src/gd32f4/gd32f4xx_progmem.c +++ b/arch/arm/src/gd32f4/gd32f4xx_progmem.c @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include "gd32f4xx_progmem.h" #include "gd32f4xx_fmc.h" @@ -126,7 +126,7 @@ typedef struct static const size_t sector_sizes[FMC_PROGMEM_SECTOR_NUM] = FMC_PROGMEM_SECTOR_SIZES; -static sem_t g_gd32_progmem_sem = SEM_INITIALIZER(1); +static mutex_t g_gd32_progmem_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Function Prototypes @@ -248,37 +248,6 @@ static fmc_sector_info_struct gd32_fmc_sector_info_get(uint32_t addr) return sector_info; } -/**************************************************************************** - * Name: gd32_progmem_sem_lock - * - * Description: - * Lock semaphore - * - * Return Value: - * Zero(OK) - On success - * EINVAL - Invalid attempt to get the semaphore - * ECANCELED - May be returned if the thread is canceled while waiting - * - ****************************************************************************/ - -static int gd32_progmem_sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_gd32_progmem_sem); -} - -/**************************************************************************** - * Name: gd32_progmem_sem_unlock - * - * Description: - * Lock semaphore - * - ****************************************************************************/ - -static void gd32_progmem_sem_unlock(void) -{ - nxsem_post(&g_gd32_progmem_sem); -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -572,8 +541,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) return -EFAULT; } - ret = gd32_progmem_sem_lock(); - + ret = nxmutex_lock(&g_gd32_progmem_lock); if (ret < 0) { return -EFAULT; @@ -590,7 +558,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) if (getreg8(addr) != *byte) { gd32_fmc_lock(); - gd32_progmem_sem_unlock(); + nxmutex_unlock(&g_gd32_progmem_lock); return -EIO; } @@ -599,8 +567,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) } gd32_fmc_lock(); - - gd32_progmem_sem_unlock(); + nxmutex_unlock(&g_gd32_progmem_lock); return count; } @@ -653,8 +620,7 @@ ssize_t up_progmem_read(size_t addr, void *buf, size_t count) return -EFAULT; } - ret = gd32_progmem_sem_lock(); - + ret = nxmutex_lock(&g_gd32_progmem_lock); if (ret < 0) { return -EFAULT; @@ -668,7 +634,7 @@ ssize_t up_progmem_read(size_t addr, void *buf, size_t count) addr++; } - gd32_progmem_sem_unlock(); + nxmutex_unlock(&g_gd32_progmem_lock); } #endif diff --git a/arch/arm/src/gd32f4/gd32f4xx_spi.c b/arch/arm/src/gd32f4/gd32f4xx_spi.c index 86cfec3ca4ecc..a9807b8a06a80 100644 --- a/arch/arm/src/gd32f4/gd32f4xx_spi.c +++ b/arch/arm/src/gd32f4/gd32f4xx_spi.c @@ -57,6 +57,7 @@ #include #include +#include #include #include @@ -148,7 +149,7 @@ struct gd32_spidev_s uint32_t spiclock; /* Clocking for the SPI module */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint8_t nbits; /* Width of word in bits (8 to 16) */ uint8_t mode; /* Mode 0,1,2,3 */ #ifdef CONFIG_GD32F4_SPI_INTERRUPT @@ -1096,11 +1097,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2111,9 +2112,9 @@ static void spi_bus_initialize(struct gd32_spidev_s *priv) spi_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI lock that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_GD32F4_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion. diff --git a/arch/arm/src/imx1/imx_spi.c b/arch/arm/src/imx1/imx_spi.c index 1521fc0b1f1a5..192675048a8a7 100644 --- a/arch/arm/src/imx1/imx_spi.c +++ b/arch/arm/src/imx1/imx_spi.c @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -85,7 +86,7 @@ struct imx_spidev_s #ifndef CONFIG_SPI_POLLWAIT sem_t waitsem; /* Wait for transfer to complete */ #endif - sem_t exclsem; /* Supports mutually exclusive access */ + mutex_t lock; /* Supports mutually exclusive access */ /* These following are the source and destination buffers of the transfer. * they are retained in this structure so that they will be accessible @@ -703,11 +704,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1116,7 +1117,7 @@ struct spi_dev_s *imx_spibus_initialize(int port) nxsem_init(&priv->waitsem, 0, 0); nxsem_set_protocol(&priv->waitsem, SEM_PRIO_NONE); #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Initialize control register: * min frequency, ignore ready, master mode, mode=0, 8-bit diff --git a/arch/arm/src/imx6/imx_ecspi.c b/arch/arm/src/imx6/imx_ecspi.c index cf55b92848477..a9bb01eb1819f 100644 --- a/arch/arm/src/imx6/imx_ecspi.c +++ b/arch/arm/src/imx6/imx_ecspi.c @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -128,7 +129,7 @@ struct imx_spidev_s #ifndef CONFIG_SPI_POLLWAIT sem_t waitsem; /* Wait for transfer to complete */ #endif - sem_t exclsem; /* Supports mutually exclusive access */ + mutex_t lock; /* Supports mutually exclusive access */ /* These following are the source and destination buffers of the transfer. * they are retained in this structure so that they will be accessible @@ -777,11 +778,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1292,7 +1293,7 @@ struct spi_dev_s *imx_spibus_initialize(int port) nxsem_init(&priv->waitsem, 0, 0); nxsem_set_protocol(&priv->waitsem, SEM_PRIO_NONE); #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Initialize control register: * min frequency, ignore ready, master mode, mode=0, 8-bit diff --git a/arch/arm/src/imxrt/imxrt_edma.c b/arch/arm/src/imxrt/imxrt_edma.c index cb74c81afe678..3125b87908aa7 100644 --- a/arch/arm/src/imxrt/imxrt_edma.c +++ b/arch/arm/src/imxrt/imxrt_edma.c @@ -57,6 +57,7 @@ #include #include #include +#include #include #include "arm_internal.h" @@ -136,9 +137,9 @@ struct imxrt_dmach_s struct imxrt_edma_s { - /* These semaphores protect the DMA channel and descriptor tables */ + /* These mutex protect the DMA channel and descriptor tables */ - sem_t chsem; /* Protects channel table */ + mutex_t chlock; /* Protects channel table */ #if CONFIG_IMXRT_EDMA_NTCD > 0 sem_t dsem; /* Supports wait for free descriptors */ #endif @@ -171,25 +172,6 @@ static struct imxrt_edmatcd_s g_tcd_pool[CONFIG_IMXRT_EDMA_NTCD] * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: imxrt_takechsem() and imxrt_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table for channel - * allocation. - * - ****************************************************************************/ - -static int imxrt_takechsem(void) -{ - return nxsem_wait_uninterruptible(&g_edma.chsem); -} - -static inline void imxrt_givechsem(void) -{ - nxsem_post(&g_edma.chsem); -} - /**************************************************************************** * Name: imxrt_takedsem() and imxrt_givedsem() * @@ -764,9 +746,9 @@ void weak_function arm_dma_initialize(void) g_edma.dmach[i].chan = i; } - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&g_edma.chsem, 0, 1); + nxmutex_init(&g_edma.chlock); #if CONFIG_IMXRT_EDMA_NTCD > 0 nxsem_init(&g_edma.dsem, 0, CONFIG_IMXRT_EDMA_NTCD); @@ -880,7 +862,7 @@ DMACH_HANDLE imxrt_dmach_alloc(uint32_t dmamux, uint8_t dchpri) /* Search for an available DMA channel */ dmach = NULL; - ret = imxrt_takechsem(); + ret = nxmutex_lock(&g_edma.chlock); if (ret < 0) { return NULL; @@ -917,7 +899,7 @@ DMACH_HANDLE imxrt_dmach_alloc(uint32_t dmamux, uint8_t dchpri) } } - imxrt_givechsem(); + nxmutex_unlock(&g_edma.chlock); /* Show the result of the allocation */ diff --git a/arch/arm/src/imxrt/imxrt_ehci.c b/arch/arm/src/imxrt/imxrt_ehci.c index 9a7e0be44c72d..e91d373c52c11 100644 --- a/arch/arm/src/imxrt/imxrt_ehci.c +++ b/arch/arm/src/imxrt/imxrt_ehci.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -275,7 +276,7 @@ struct imxrt_ehci_s { volatile bool pscwait; /* TRUE: Thread is waiting for port status change event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for port status change events */ struct imxrt_epinfo_s ep0; /* Endpoint 0 */ @@ -416,12 +417,6 @@ static inline void imxrt_putreg(uint32_t regval, volatile uint32_t *regaddr); static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, unsigned int delay); -/* Semaphores ***************************************************************/ - -static int imxrt_takesem(sem_t *sem); -static int imxrt_takesem_noncancelable(sem_t *sem); -#define imxrt_givesem(s) nxsem_post(s); - /* Allocators ***************************************************************/ static struct imxrt_qh_s *imxrt_qh_alloc(void); @@ -1053,61 +1048,13 @@ static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, return (regval == donebits) ? OK : -ETIMEDOUT; } -/**************************************************************************** - * Name: imxrt_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int imxrt_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: imxrt_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int imxrt_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: imxrt_qh_alloc * * Description: * Allocate a Queue Head (QH) structure by removing it from the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1133,7 +1080,7 @@ static struct imxrt_qh_s *imxrt_qh_alloc(void) * Description: * Free a Queue Head (QH) structure by returning it to the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1154,7 +1101,7 @@ static void imxrt_qh_free(struct imxrt_qh_s *qh) * Allocate a Queue Element Transfer Descriptor (qTD) by removing it from * the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1182,7 +1129,7 @@ static struct imxrt_qtd_s *imxrt_qtd_alloc(void) * free list * * Assumption: - * Caller holds the exclsem + * Caller holds the lock * ****************************************************************************/ @@ -1630,7 +1577,7 @@ static inline uint8_t imxrt_ehci_speed(uint8_t usbspeed) * this to minimize race conditions. This logic would have to be expanded * if we want to have more than one packet in flight at a time! * - * Assumption: The caller holds the EHCI exclsem + * Assumption: The caller holds the EHCI lock * ****************************************************************************/ @@ -1676,7 +1623,7 @@ static int imxrt_ioc_setup(struct imxrt_rhport_s *rhport, * Description: * Wait for the IOC event. * - * Assumption: The caller does *NOT* hold the EHCI exclsem. That would + * Assumption: The caller does *NOT* hold the EHCI lock. That would * cause a deadlock when the bottom-half, worker thread needs to take the * semaphore. * @@ -1692,7 +1639,7 @@ static int imxrt_ioc_wait(struct imxrt_epinfo_s *epinfo) while (epinfo->iocwait) { - ret = imxrt_takesem(&epinfo->iocsem); + ret = nxsem_wait_uninterruptible(&epinfo->iocsem); if (ret < 0) { break; @@ -1708,7 +1655,7 @@ static int imxrt_ioc_wait(struct imxrt_epinfo_s *epinfo) * Description: * Add a new, ready-to-go QH w/attached qTDs to the asynchronous queue. * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -2155,7 +2102,7 @@ static struct imxrt_qtd_s *imxrt_qtd_statusphase(uint32_t tokenbits) * This is a blocking function; it will not return until the control * transfer has completed. * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2438,7 +2385,7 @@ static int imxrt_async_setup(struct imxrt_rhport_s *rhport, * frame list), followed by shorter poll rates, with queue heads with a * poll rate of one, on the very end." * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2542,8 +2489,8 @@ static int imxrt_intr_setup(struct imxrt_rhport_s *rhport, * Description: * Wait for an IN or OUT transfer to complete. * - * Assumption: The caller holds the EHCI exclsem. The caller must be aware - * that the EHCI exclsem will released while waiting for the transfer to + * Assumption: The caller holds the EHCI lock. The caller must be aware + * that the EHCI lock will released while waiting for the transfer to * complete, but will be re-acquired when before returning. The state of * EHCI resources could be very different upon return. * @@ -2561,29 +2508,29 @@ static ssize_t imxrt_transfer_wait(struct imxrt_epinfo_s *epinfo) int ret; int ret2; - /* Release the EHCI semaphore while we wait. Other threads need the + /* Release the EHCI lock while we wait. Other threads need the * opportunity to access the EHCI resources while we wait. * * REVISIT: Is this safe? NO. This is a bug and needs rethinking. * We need to lock all of the port-resources (not EHCI common) until - * the transfer is complete. But we can't use the common EHCI exclsem + * the transfer is complete. But we can't use the common EHCI lock * or we will deadlock while waiting (because the working thread that - * wakes this thread up needs the exclsem). + * wakes this thread up needs the lock). */ /* REVISIT */ - imxrt_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Wait for the IOC completion event */ ret = imxrt_ioc_wait(epinfo); - /* Re-acquire the EHCI semaphore. The caller expects to be holding + /* Re-acquire the EHCI lock. The caller expects to be holding * this upon return. */ - ret2 = imxrt_takesem_noncancelable(&g_ehci.exclsem); + ret2 = nxmutex_lock(&g_ehci.lock); if (ret >= 0 && ret2 < 0) { ret = ret2; @@ -2607,9 +2554,7 @@ static ssize_t imxrt_transfer_wait(struct imxrt_epinfo_s *epinfo) } #endif - /* Did imxrt_ioc_wait() or imxrt_takesem_noncancelable() report an - * error? - */ + /* Did imxrt_ioc_wait() or nxmutex_lock() report an error? */ if (ret < 0) { @@ -2917,7 +2862,7 @@ static int imxrt_qh_ioccheck(struct imxrt_qh_s *qh, uint32_t **bp, void *arg) /* Yes... wake it up */ epinfo->iocwait = false; - imxrt_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); } #ifdef CONFIG_USBHOST_ASYNCH @@ -3076,7 +3021,7 @@ static int imxrt_qh_cancel(struct imxrt_qh_s *qh, uint32_t **bp, void *arg) * detected (actual number of bytes received was less than the expected * number of bytes)." * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -3213,7 +3158,7 @@ static inline void imxrt_portsc_bottomhalf(void) if (g_ehci.pscwait) { - imxrt_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3253,7 +3198,7 @@ static inline void imxrt_portsc_bottomhalf(void) if (g_ehci.pscwait) { - imxrt_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3330,7 +3275,7 @@ static void imxrt_ehci_bottomhalf(void *arg) * real option (other than to reschedule and delay). */ - imxrt_takesem_noncancelable(&g_ehci.exclsem); + nxmutex_lock(&g_ehci.lock); /* Handle all unmasked interrupt sources * USB Interrupt (USBINT) @@ -3441,7 +3386,7 @@ static void imxrt_ehci_bottomhalf(void *arg) /* We are done with the EHCI structures */ - imxrt_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Re-enable relevant EHCI interrupts. Interrupts should still be enabled * at the level of the interrupt controller. @@ -3600,7 +3545,7 @@ static int imxrt_wait(struct usbhost_connection_s *conn, */ g_ehci.pscwait = true; - ret = imxrt_takesem(&g_ehci.pscsem); + ret = nxsem_wait_uninterruptible(&g_ehci.pscsem); if (ret < 0) { return ret; @@ -3935,7 +3880,7 @@ static int imxrt_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the EHCI data structures. */ - ret = imxrt_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret >= 0) { /* Remember the new device address and max packet size */ @@ -3944,7 +3889,7 @@ static int imxrt_ep0configure(struct usbhost_driver_s *drvr, epinfo->speed = speed; epinfo->maxpacket = maxpacketsize; - imxrt_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); } return ret; @@ -4307,7 +4252,7 @@ static int imxrt_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * structures. */ - ret = imxrt_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4319,7 +4264,7 @@ static int imxrt_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Now initiate the transfer */ @@ -4334,13 +4279,13 @@ static int imxrt_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* And wait for the transfer to complete */ nbytes = imxrt_transfer_wait(ep0info); - imxrt_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes >= 0 ? OK : (int)nbytes; errout_with_iocwait: ep0info->iocwait = false; -errout_with_sem: - imxrt_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4409,7 +4354,7 @@ static ssize_t imxrt_transfer(struct usbhost_driver_s *drvr, * structures. */ - ret = imxrt_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return (ssize_t)ret; @@ -4423,7 +4368,7 @@ static ssize_t imxrt_transfer(struct usbhost_driver_s *drvr, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4461,14 +4406,14 @@ static ssize_t imxrt_transfer(struct usbhost_driver_s *drvr, /* Then wait for the transfer to complete */ nbytes = imxrt_transfer_wait(epinfo); - imxrt_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes; errout_with_iocwait: epinfo->iocwait = false; -errout_with_sem: +errout_with_lock: uerr("!!!\n"); - imxrt_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return (ssize_t)ret; } @@ -4523,7 +4468,7 @@ static int imxrt_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * structures. */ - ret = imxrt_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4535,7 +4480,7 @@ static int imxrt_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4572,14 +4517,14 @@ static int imxrt_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* The transfer is in progress */ - imxrt_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return OK; errout_with_callback: epinfo->callback = NULL; epinfo->arg = NULL; -errout_with_sem: - imxrt_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4627,7 +4572,7 @@ static int imxrt_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * interrupt level. */ - ret = imxrt_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4670,7 +4615,7 @@ static int imxrt_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #endif { ret = OK; - goto errout_with_sem; + goto errout_with_lock; } /* Handle the cancellation according to the type of the transfer */ @@ -4733,7 +4678,7 @@ static int imxrt_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) default: usbhost_trace1(EHCI_TRACE1_BADXFRTYPE, epinfo->xfrtype); ret = -ENOSYS; - goto errout_with_sem; + goto errout_with_lock; } /* Find and remove the QH. There are four possibilities: @@ -4763,7 +4708,7 @@ static int imxrt_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Yes... wake it up */ DEBUGASSERT(callback == NULL); - imxrt_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); } /* No.. Is there a pending asynchronous transfer? */ @@ -4778,11 +4723,11 @@ static int imxrt_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #else /* Wake up the waiting thread */ - imxrt_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); #endif -errout_with_sem: - imxrt_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4829,7 +4774,7 @@ static int imxrt_connect(struct usbhost_driver_s *drvr, if (g_ehci.pscwait) { g_ehci.pscwait = false; - imxrt_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); } leave_critical_section(flags); @@ -5055,7 +5000,7 @@ struct usbhost_connection_s *imxrt_ehci_initialize(int controller) /* Initialize the EHCI state data structure */ - nxsem_init(&g_ehci.exclsem, 0, 1); + nxmutex_init(&g_ehci.lock); nxsem_init(&g_ehci.pscsem, 0, 0); /* The pscsem semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/imxrt/imxrt_enc.c b/arch/arm/src/imxrt/imxrt_enc.c index 5a8f49a4876a9..92ed32915d285 100644 --- a/arch/arm/src/imxrt/imxrt_enc.c +++ b/arch/arm/src/imxrt/imxrt_enc.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include "chip.h" #include "arm_internal.h" @@ -302,7 +302,7 @@ struct imxrt_enc_lowerhalf_s const struct imxrt_qeconfig_s *config; /* static configuration */ struct qe_index_s *data; - sem_t sem_excl; /* Mutual exclusion semaphore to + mutex_t lock; /* Mutual exclusion mutex to * ensure atomic 32-bit reads. */ }; @@ -324,10 +324,6 @@ static inline void imxrt_enc_modifyreg16 static void imxrt_enc_clock_enable (uint32_t base); static void imxrt_enc_clock_disable (uint32_t base); -static inline int imxrt_enc_sem_wait(struct imxrt_enc_lowerhalf_s *priv); -static inline void imxrt_enc_sem_post - (struct imxrt_enc_lowerhalf_s *priv); - static int imxrt_enc_reconfig(struct imxrt_enc_lowerhalf_s *priv, uint16_t args); static void imxrt_enc_set_initial_val(struct imxrt_enc_lowerhalf_s *priv, @@ -635,32 +631,6 @@ void imxrt_enc_clock_disable(uint32_t base) #endif /* CONFIG_ARCH_FAMILY_IMXRT105x || CONFIG_ARCH_FAMILY_IMXRT106x */ } -/**************************************************************************** - * Name: imxrt_enc_sem_wait - * - * Description: - * Take exclusive access to the position register, waiting as necessary - * - ****************************************************************************/ - -static inline int imxrt_enc_sem_wait(struct imxrt_enc_lowerhalf_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - -/**************************************************************************** - * Name: imxrt_enc_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void imxrt_enc_sem_post(struct imxrt_enc_lowerhalf_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - /**************************************************************************** * Name: imxrt_enc_reconfig * @@ -939,7 +909,7 @@ static int imxrt_setup(struct qe_lowerhalf_s *lower) uint32_t regval; int ret; - ret = imxrt_enc_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1003,8 +973,7 @@ static int imxrt_setup(struct qe_lowerhalf_s *lower) regval = ((config->init_flags >> MOD_SHIFT) & 1) ? ENC_CTRL2_MOD : 0; imxrt_enc_putreg16(priv, IMXRT_ENC_CTRL2_OFFSET, regval); - imxrt_enc_sem_post(priv); - + nxmutex_unlock(&priv->lock); return OK; } @@ -1026,7 +995,7 @@ static int imxrt_shutdown(struct qe_lowerhalf_s *lower) /* Ensure any in-progress operations are done. */ - ret = imxrt_enc_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1059,7 +1028,7 @@ static int imxrt_shutdown(struct qe_lowerhalf_s *lower) imxrt_enc_clock_disable(priv->config->base); - imxrt_enc_sem_post(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -1080,7 +1049,7 @@ static int imxrt_position(struct qe_lowerhalf_s *lower, int32_t *pos) int i; int ret; - ret = imxrt_enc_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1107,13 +1076,13 @@ static int imxrt_position(struct qe_lowerhalf_s *lower, int32_t *pos) if (lpos != imxrt_enc_getreg16(priv, IMXRT_ENC_LPOSH_OFFSET)) { - imxrt_enc_sem_post(priv); + nxmutex_unlock(&priv->lock); return -EAGAIN; } upos = imxrt_enc_getreg16(priv, IMXRT_ENC_UPOSH_OFFSET); - imxrt_enc_sem_post(priv); + nxmutex_unlock(&priv->lock); *pos = (int32_t)((upos << 16) | lpos); return OK; @@ -1135,14 +1104,14 @@ static int imxrt_reset(struct qe_lowerhalf_s *lower) /* Write a 1 to the SWIP bit to load UINIT and LINIT into UPOS and LPOS */ - ret = imxrt_enc_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } imxrt_enc_modifyreg16(priv, IMXRT_ENC_CTRL_OFFSET, 0, ENC_CTRL_SWIP); - imxrt_enc_sem_post(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -1258,7 +1227,7 @@ int imxrt_qeinitialize(const char *devpath, int enc) /* Initialize private data */ - nxsem_init(&priv->sem_excl, 0, 1); + nxmutex_init(&priv->lock); /* Register the upper-half driver */ diff --git a/arch/arm/src/imxrt/imxrt_flexspi.c b/arch/arm/src/imxrt/imxrt_flexspi.c index 7c836af4feb9c..60473461d57fe 100644 --- a/arch/arm/src/imxrt/imxrt_flexspi.c +++ b/arch/arm/src/imxrt/imxrt_flexspi.c @@ -39,7 +39,7 @@ #include #include #include -#include +#include #include "arm_internal.h" #include "barriers.h" @@ -71,7 +71,7 @@ struct imxrt_flexspidev_s bool initialized; /* TRUE: Controller has been initialized */ - sem_t exclsem; /* Assures mutually exclusive access to + mutex_t lock; /* Assures mutually exclusive access to * FlexSPI */ }; @@ -1104,11 +1104,11 @@ static int imxrt_flexspi_lock(struct flexspi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1268,11 +1268,11 @@ struct flexspi_dev_s *imxrt_flexspi_initialize(int intf) { /* No perform one time initialization */ - /* Initialize the FlexSPI semaphore that enforces mutually exclusive + /* Initialize the FlexSPI mutex that enforces mutually exclusive * access to the FlexSPI registers. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Perform hardware initialization. Puts the FlexSPI into an active * state. diff --git a/arch/arm/src/imxrt/imxrt_lpi2c.c b/arch/arm/src/imxrt/imxrt_lpi2c.c index 1c968b134dcef..6cc97bbac7a55 100644 --- a/arch/arm/src/imxrt/imxrt_lpi2c.c +++ b/arch/arm/src/imxrt/imxrt_lpi2c.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -192,7 +193,7 @@ struct imxrt_lpi2c_priv_s const struct imxrt_lpi2c_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 @@ -235,11 +236,6 @@ static inline void imxrt_lpi2c_putreg(struct imxrt_lpi2c_priv_s *priv, static inline void imxrt_lpi2c_modifyreg(struct imxrt_lpi2c_priv_s *priv, uint16_t offset, uint32_t clearbits, uint32_t setbits); -static inline int imxrt_lpi2c_sem_wait(struct imxrt_lpi2c_priv_s *priv); -#ifdef CONFIG_I2C_RESET -static int -imxrt_lpi2c_sem_wait_noncancelable(struct imxrt_lpi2c_priv_s *priv); -#endif #ifdef CONFIG_IMXRT_LPI2C_DYNTIMEO static uint32_t imxrt_lpi2c_toticks(int msgc, struct i2c_msg_s *msgs); @@ -249,12 +245,6 @@ static inline int imxrt_lpi2c_sem_waitdone(struct imxrt_lpi2c_priv_s *priv); static inline void imxrt_lpi2c_sem_waitstop(struct imxrt_lpi2c_priv_s *priv); -static inline void -imxrt_lpi2c_sem_post(struct imxrt_lpi2c_priv_s *priv); -static inline void -imxrt_lpi2c_sem_init(struct imxrt_lpi2c_priv_s *priv); -static inline void -imxrt_lpi2c_sem_destroy(struct imxrt_lpi2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void imxrt_lpi2c_tracereset(struct imxrt_lpi2c_priv_s *priv); @@ -363,6 +353,10 @@ static struct imxrt_lpi2c_priv_s imxrt_lpi2c1_priv = .ops = &imxrt_lpi2c_ops, .config = &imxrt_lpi2c1_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, @@ -404,6 +398,10 @@ static struct imxrt_lpi2c_priv_s imxrt_lpi2c2_priv = .ops = &imxrt_lpi2c_ops, .config = &imxrt_lpi2c2_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, @@ -445,6 +443,10 @@ static struct imxrt_lpi2c_priv_s imxrt_lpi2c3_priv = .ops = &imxrt_lpi2c_ops, .config = &imxrt_lpi2c3_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, @@ -486,6 +488,10 @@ static struct imxrt_lpi2c_priv_s imxrt_lpi2c4_priv = .ops = &imxrt_lpi2c_ops, .config = &imxrt_lpi2c4_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, @@ -543,36 +549,6 @@ static inline void imxrt_lpi2c_modifyreg(struct imxrt_lpi2c_priv_s *priv, modifyreg32(priv->config->base + offset, clearbits, setbits); } -/**************************************************************************** - * Name: imxrt_lpi2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. May be interrupted by - * a signal. - * - ****************************************************************************/ - -static inline int imxrt_lpi2c_sem_wait(struct imxrt_lpi2c_priv_s *priv) -{ - return nxsem_wait(&priv->sem_excl); -} - -/**************************************************************************** - * Name: imxrt_lpi2c_sem_wait_noncancelable - * - * Description: - * Take the exclusive access, waiting as necessary. - * - ****************************************************************************/ - -#ifdef CONFIG_I2C_RESET -static int -imxrt_lpi2c_sem_wait_noncancelable(struct imxrt_lpi2c_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} -#endif - /**************************************************************************** * Name: imxrt_lpi2c_toticks * @@ -856,58 +832,6 @@ imxrt_lpi2c_sem_waitstop(struct imxrt_lpi2c_priv_s *priv) i2cinfo("Timeout with Status Register: %" PRIx32 "\n", regval); } -/**************************************************************************** - * Name: imxrt_lpi2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void imxrt_lpi2c_sem_post(struct imxrt_lpi2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: imxrt_lpi2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void imxrt_lpi2c_sem_init(struct imxrt_lpi2c_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: imxrt_lpi2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void -imxrt_lpi2c_sem_destroy(struct imxrt_lpi2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - /**************************************************************************** * Name: imxrt_dma_callback * @@ -915,6 +839,7 @@ imxrt_lpi2c_sem_destroy(struct imxrt_lpi2c_priv_s *priv) * This function performs the next I2C operation * ****************************************************************************/ + #ifdef CONFIG_IMXRT_LPI2C_DMA static void imxrt_dma_callback(DMACH_HANDLE handle, void *arg, bool done, int result) @@ -2077,7 +2002,7 @@ static int imxrt_lpi2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = imxrt_lpi2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2178,7 +2103,7 @@ static int imxrt_lpi2c_transfer(struct i2c_master_s *dev, priv->dcnt = 0; priv->ptr = NULL; - imxrt_lpi2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -2215,7 +2140,7 @@ static int imxrt_lpi2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - ret = imxrt_lpi2c_sem_wait_noncancelable(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2313,7 +2238,7 @@ static int imxrt_lpi2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - imxrt_lpi2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2371,7 +2296,6 @@ struct i2c_master_s *imxrt_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - imxrt_lpi2c_sem_init(priv); imxrt_lpi2c_init(priv); #ifdef CONFIG_IMXRT_LPI2C_DMA @@ -2434,9 +2358,6 @@ int imxrt_i2cbus_uninitialize(struct i2c_master_s *dev) imxrt_lpi2c_deinit(priv); - /* Release unused resources */ - - imxrt_lpi2c_sem_destroy(priv); return OK; } diff --git a/arch/arm/src/imxrt/imxrt_lpspi.c b/arch/arm/src/imxrt/imxrt_lpspi.c index cdc32c329473c..79a445bf0c35a 100644 --- a/arch/arm/src/imxrt/imxrt_lpspi.c +++ b/arch/arm/src/imxrt/imxrt_lpspi.c @@ -59,7 +59,7 @@ #include #include -#include +#include #include #include @@ -112,7 +112,7 @@ struct imxrt_lpspidev_s #ifdef CONFIG_IMXRT_LPSPI_INTERRUPTS uint8_t spiirq; /* SPI IRQ number */ #endif - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ int8_t nbits; /* Width of word in bits */ @@ -883,11 +883,11 @@ static int imxrt_lpspi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1711,9 +1711,9 @@ static void imxrt_lpspi_bus_initialize(struct imxrt_lpspidev_s *priv) imxrt_lpspi_setmode((struct spi_dev_s *)priv, SPIDEV_MODE0); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Enable LPSPI */ diff --git a/arch/arm/src/imxrt/imxrt_rtc_lowerhalf.c b/arch/arm/src/imxrt/imxrt_rtc_lowerhalf.c index a9415697bfe19..bbf765f8f94e9 100644 --- a/arch/arm/src/imxrt/imxrt_rtc_lowerhalf.c +++ b/arch/arm/src/imxrt/imxrt_rtc_lowerhalf.c @@ -32,6 +32,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -61,7 +62,7 @@ struct imxrt_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -297,10 +298,10 @@ static int imxrt_setalarm(struct rtc_lowerhalf_s *lower, /* Get exclusive access to the alarm */ - ret = nxsem_wait(&rtc->devsem); + ret = nxmutex_lock(&rtc->devlock); if (ret < 0) { - rtcerr("ERROR: nxsem_wait failed: %d\n", ret); + rtcerr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -329,7 +330,7 @@ static int imxrt_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&rtc->devsem); + nxmutex_unlock(&rtc->devlock); return ret; } #endif @@ -364,10 +365,10 @@ static int imxrt_setrelative(struct rtc_lowerhalf_s *lower, /* Get exclusive access to the alarm */ - ret = nxsem_wait(&rtc->devsem); + ret = nxmutex_lock(&rtc->devlock); if (ret < 0) { - rtcerr("ERROR: nxsem_wait failed: %d\n", ret); + rtcerr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -402,7 +403,7 @@ static int imxrt_setrelative(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&rtc->devsem); + nxmutex_unlock(&rtc->devlock); return ret; } #endif @@ -513,8 +514,7 @@ static int imxrt_rdalarm(struct rtc_lowerhalf_s *lower, struct rtc_lowerhalf_s *imxrt_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/arm/src/imxrt/imxrt_usdhc.c b/arch/arm/src/imxrt/imxrt_usdhc.c index 3f39b06af394f..2546421229ff9 100644 --- a/arch/arm/src/imxrt/imxrt_usdhc.c +++ b/arch/arm/src/imxrt/imxrt_usdhc.c @@ -246,8 +246,6 @@ struct imxrt_sdhcregs_s /* Low-level helpers ********************************************************/ -static int imxrt_takesem(struct imxrt_dev_s *priv); -#define imxrt_givesem(priv) (nxsem_post(&priv->waitsem)) static void imxrt_configwaitints(struct imxrt_dev_s *priv, uint32_t waitints, sdio_eventset_t waitevents, sdio_eventset_t wkupevents); static void imxrt_configxfrints(struct imxrt_dev_s *priv, uint32_t xfrints); @@ -497,27 +495,6 @@ static struct imxrt_sdhcregs_s g_sampleregs[DEBUG_NSAMPLES]; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: imxrt_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int imxrt_takesem(struct imxrt_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: imxrt_configwaitints * @@ -1150,7 +1127,7 @@ static void imxrt_endwait(struct imxrt_dev_s *priv, /* Wake up the waiting thread */ - imxrt_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2818,7 +2795,7 @@ static sdio_eventset_t imxrt_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = imxrt_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/arm/src/kinetis/kinetis_edma.c b/arch/arm/src/kinetis/kinetis_edma.c index 2785e72ec459f..f513d28ba21a2 100644 --- a/arch/arm/src/kinetis/kinetis_edma.c +++ b/arch/arm/src/kinetis/kinetis_edma.c @@ -59,6 +59,7 @@ #include #include #include +#include #include #include "arm_internal.h" @@ -138,9 +139,9 @@ struct kinetis_dmach_s struct kinetis_edma_s { - /* These semaphores protect the DMA channel and descriptor tables */ + /* These mutex protect the DMA channel and descriptor tables */ - sem_t chsem; /* Protects channel table */ + mutex_t chlock; /* Protects channel table */ #if CONFIG_KINETIS_EDMA_NTCD > 0 sem_t dsem; /* Supports wait for free descriptors */ #endif @@ -173,25 +174,6 @@ static struct kinetis_edmatcd_s g_tcd_pool[CONFIG_KINETIS_EDMA_NTCD] * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: kinetis_takechsem() and kinetis_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table for channel - * allocation. - * - ****************************************************************************/ - -static int kinetis_takechsem(void) -{ - return nxsem_wait_uninterruptible(&g_edma.chsem); -} - -static inline void kinetis_givechsem(void) -{ - nxsem_post(&g_edma.chsem); -} - /**************************************************************************** * Name: kinetis_takedsem() and kinetis_givedsem() * @@ -745,9 +727,9 @@ void weak_function arm_dma_initialize(void) g_edma.dmach[i].chan = i; } - /* Initialize semaphores */ + /* Initialize mutex & semaphore */ - nxsem_init(&g_edma.chsem, 0, 1); + nxmutex_init(&g_edma.chlock); #if CONFIG_KINETIS_EDMA_NTCD > 0 nxsem_init(&g_edma.dsem, 0, CONFIG_KINETIS_EDMA_NTCD); @@ -857,7 +839,7 @@ DMACH_HANDLE kinetis_dmach_alloc(uint8_t dmamux, uint8_t dchpri) /* Search for an available DMA channel */ dmach = NULL; - ret = kinetis_takechsem(); + ret = nxmutex_lock(&g_edma.chlock); if (ret < 0) { return NULL; @@ -894,7 +876,7 @@ DMACH_HANDLE kinetis_dmach_alloc(uint8_t dmamux, uint8_t dchpri) } } - kinetis_givechsem(); + nxmutex_unlock(&g_edma.chlock); /* Show the result of the allocation */ diff --git a/arch/arm/src/kinetis/kinetis_i2c.c b/arch/arm/src/kinetis/kinetis_i2c.c index 6cac05ce2ea68..14da11ac2132e 100644 --- a/arch/arm/src/kinetis/kinetis_i2c.c +++ b/arch/arm/src/kinetis/kinetis_i2c.c @@ -35,6 +35,7 @@ #include #include +#include #include #include @@ -112,7 +113,7 @@ struct kinetis_i2cdev_s int refs; /* Reference count */ volatile uint8_t state; /* State of state machine */ bool restart; /* Should next transfer restart or not */ - sem_t mutex; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t wait; /* Place to wait for state machine completion */ struct wdog_s timeout; /* watchdog to timeout when bus hung */ struct i2c_msg_s *msgs; /* Remaining transfers - first one is in @@ -130,20 +131,6 @@ static uint8_t kinetis_i2c_getreg(struct kinetis_i2cdev_s *priv, static void kinetis_i2c_putreg(struct kinetis_i2cdev_s *priv, uint8_t value, uint8_t offset); -/* Exclusion Helpers */ - -static inline void kinetis_i2c_sem_init(struct kinetis_i2cdev_s *priv); -static inline void -kinetis_i2c_sem_destroy(struct kinetis_i2cdev_s *priv); -static inline int kinetis_i2c_sem_wait(struct kinetis_i2cdev_s *priv); - -#ifdef CONFIG_I2C_RESET -static int -kinetis_i2c_sem_wait_noncancelable(struct kinetis_i2cdev_s *priv); -#endif - -static inline void kinetis_i2c_sem_post(struct kinetis_i2cdev_s *priv); - /* Signal Helper */ static inline void kinetis_i2c_endwait(struct kinetis_i2cdev_s *priv); @@ -203,6 +190,8 @@ static struct kinetis_i2cdev_s g_i2c0_dev = .dev.ops = &kinetis_i2c_ops, .config = &kinetis_i2c0_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .state = STATE_OK, .msgs = NULL, }; @@ -224,6 +213,8 @@ static struct kinetis_i2cdev_s g_i2c1_dev = .dev.ops = &kinetis_i2c_ops, .config = &kinetis_i2c1_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .state = STATE_OK, .msgs = NULL, }; @@ -245,6 +236,8 @@ static struct kinetis_i2cdev_s g_i2c2_dev = .dev.ops = &kinetis_i2c_ops, .config = &kinetis_i2c2_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .state = STATE_OK, .msgs = NULL, }; @@ -266,6 +259,8 @@ static struct kinetis_i2cdev_s g_i2c3_dev = .dev.ops = &kinetis_i2c_ops, .config = &kinetis_i2c3_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .state = STATE_OK, .msgs = NULL, }; @@ -303,83 +298,6 @@ static void kinetis_i2c_putreg(struct kinetis_i2cdev_s *priv, uint8_t value, putreg8(value, priv->config->base + offset); } -/**************************************************************************** - * Name: kinetis_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void kinetis_i2c_sem_init(struct kinetis_i2cdev_s *priv) -{ - nxsem_init(&priv->mutex, 0, 1); - - /* This semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ - - nxsem_init(&priv->wait, 0, 0); - nxsem_set_protocol(&priv->wait, SEM_PRIO_NONE); -} - -/**************************************************************************** - * Name: kinetis_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void kinetis_i2c_sem_destroy(struct kinetis_i2cdev_s *priv) -{ - nxsem_destroy(&priv->mutex); - nxsem_destroy(&priv->wait); -} - -/**************************************************************************** - * Name: kinetis_i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. May be interrupted by - * a signal. - * - ****************************************************************************/ - -static inline int kinetis_i2c_sem_wait(struct kinetis_i2cdev_s *priv) -{ - return nxsem_wait(&priv->mutex); -} - -#ifdef CONFIG_I2C_RESET -/**************************************************************************** - * Name: kinetis_i2c_sem_wait_noncancelable - * - * Description: - * Take the exclusive access, waiting as necessary - * - ****************************************************************************/ - -static int -kinetis_i2c_sem_wait_noncancelable(struct kinetis_i2cdev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->mutex); -} -#endif - -/**************************************************************************** - * Name: kinetis_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void kinetis_i2c_sem_post(struct kinetis_i2cdev_s *priv) -{ - nxsem_post(&priv->mutex); -} - /**************************************************************************** * Name: kinetis_i2c_wait * @@ -1142,7 +1060,7 @@ static int kinetis_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - ret = kinetis_i2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1232,8 +1150,7 @@ static int kinetis_i2c_transfer(struct i2c_master_s *dev, /* Release access to I2C bus */ - kinetis_i2c_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -1270,7 +1187,7 @@ static int kinetis_i2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - ret = kinetis_i2c_sem_wait_noncancelable(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1369,7 +1286,7 @@ static int kinetis_i2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - kinetis_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -1397,25 +1314,25 @@ struct i2c_master_s *kinetis_i2cbus_initialize(int port) { #ifdef CONFIG_KINETIS_I2C0 case 0: - priv = &g_i2c0_dev; + priv = &g_i2c0_dev; break; #endif #ifdef CONFIG_KINETIS_I2C1 case 1: - priv = &g_i2c1_dev; + priv = &g_i2c1_dev; break; #endif #ifdef CONFIG_KINETIS_I2C2 case 2: - priv = &g_i2c2_dev; + priv = &g_i2c2_dev; break; #endif #ifdef CONFIG_KINETIS_I2C3 case 3: - priv = &g_i2c3_dev; + priv = &g_i2c3_dev; break; #endif @@ -1427,7 +1344,6 @@ struct i2c_master_s *kinetis_i2cbus_initialize(int port) flags = enter_critical_section(); if ((volatile int)priv->refs++ == 0) { - kinetis_i2c_sem_init(priv); kinetis_i2c_init(priv); } @@ -1471,7 +1387,6 @@ int kinetis_i2cbus_uninitialize(struct i2c_master_s *dev) /* Disable power and other HW resource (GPIO's) */ kinetis_i2c_deinit(priv); - kinetis_i2c_sem_destroy(priv); wd_cancel(&priv->timeout); return OK; } diff --git a/arch/arm/src/kinetis/kinetis_sdhc.c b/arch/arm/src/kinetis/kinetis_sdhc.c index 5521332d0bc20..1b629d0d4e477 100644 --- a/arch/arm/src/kinetis/kinetis_sdhc.c +++ b/arch/arm/src/kinetis/kinetis_sdhc.c @@ -229,8 +229,6 @@ struct kinetis_sdhcregs_s /* Low-level helpers ********************************************************/ -static int kinetis_takesem(struct kinetis_dev_s *priv); -#define kinetis_givesem(priv) (nxsem_post(&priv->waitsem)) static void kinetis_configwaitints(struct kinetis_dev_s *priv, uint32_t waitints, sdio_eventset_t waitevents, sdio_eventset_t wkupevents); @@ -411,27 +409,6 @@ static struct kinetis_sdhcregs_s g_sampleregs[DEBUG_NSAMPLES]; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: kinetis_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int kinetis_takesem(struct kinetis_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: kinetis_configwaitints * @@ -985,7 +962,7 @@ static void kinetis_endwait(struct kinetis_dev_s *priv, /* Wake up the waiting thread */ - kinetis_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2534,7 +2511,7 @@ static sdio_eventset_t kinetis_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = kinetis_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/arm/src/kinetis/kinetis_spi.c b/arch/arm/src/kinetis/kinetis_spi.c index a401dbb02cf09..4c1c8af2950e2 100644 --- a/arch/arm/src/kinetis/kinetis_spi.c +++ b/arch/arm/src/kinetis/kinetis_spi.c @@ -58,7 +58,7 @@ #include #include -#include +#include #include #include @@ -96,7 +96,7 @@ struct kinetis_spidev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ uint32_t spibase; /* Base address of SPI registers */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (8 to 16) */ @@ -672,11 +672,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1666,9 +1666,9 @@ struct spi_dev_s *kinetis_spibus_initialize(int port) priv->frequency = 0; spi_setfrequency(&priv->spidev, KINETIS_SPI_CLK_INIT); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_KINETIS_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion. * This semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/kinetis/kinetis_usbhshost.c b/arch/arm/src/kinetis/kinetis_usbhshost.c index d228edd692ee8..a077b4295d628 100644 --- a/arch/arm/src/kinetis/kinetis_usbhshost.c +++ b/arch/arm/src/kinetis/kinetis_usbhshost.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -284,7 +285,7 @@ struct kinetis_ehci_s { volatile bool pscwait; /* TRUE: Thread is waiting for port status change event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for port status change events */ struct kinetis_epinfo_s ep0; /* Endpoint 0 */ @@ -427,12 +428,6 @@ static inline void kinetis_putreg(uint32_t regval, static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, unsigned int delay); -/* Semaphores ***************************************************************/ - -static int kinetis_takesem(sem_t *sem); -static int kinetis_takesem_noncancelable(sem_t *sem); -#define kinetis_givesem(s) nxsem_post(s); - /* Allocators ***************************************************************/ static struct kinetis_qh_s *kinetis_qh_alloc(void); @@ -1075,61 +1070,13 @@ static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, return (regval == donebits) ? OK : -ETIMEDOUT; } -/**************************************************************************** - * Name: kinetis_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int kinetis_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: kinetis_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int kinetis_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: kinetis_qh_alloc * * Description: * Allocate a Queue Head (QH) structure by removing it from the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1156,7 +1103,7 @@ static struct kinetis_qh_s *kinetis_qh_alloc(void) * Let a Queue Head (QH) structure wait for free by adding it to the * aawait list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1179,7 +1126,7 @@ static void kinetis_qh_aawait(struct kinetis_qh_s *qh) * Description: * Free a Queue Head (QH) structure by returning it to the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1198,7 +1145,7 @@ static void kinetis_qh_free(struct kinetis_qh_s *qh) * Allocate a Queue Element Transfer Descriptor (qTD) by removing it from * the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1226,7 +1173,7 @@ static struct kinetis_qtd_s *kinetis_qtd_alloc(void) * free list * * Assumption: - * Caller holds the exclsem + * Caller holds the lock * ****************************************************************************/ @@ -1677,7 +1624,7 @@ static inline uint8_t kinetis_ehci_speed(uint8_t usbspeed) * this to minimize race conditions. This logic would have to be expanded * if we want to have more than one packet in flight at a time! * - * Assumption: The caller holds the EHCI exclsem + * Assumption: The caller holds the EHCI lock * ****************************************************************************/ @@ -1723,7 +1670,7 @@ static int kinetis_ioc_setup(struct kinetis_rhport_s *rhport, * Description: * Wait for the IOC event. * - * Assumption: The caller does *NOT* hold the EHCI exclsem. That would + * Assumption: The caller does *NOT* hold the EHCI lock. That would * cause a deadlock when the bottom-half, worker thread needs to take the * semaphore. * @@ -1739,7 +1686,7 @@ static int kinetis_ioc_wait(struct kinetis_epinfo_s *epinfo) while (epinfo->iocwait) { - ret = kinetis_takesem(&epinfo->iocsem); + ret = nxsem_wait_uninterruptible(&epinfo->iocsem); if (ret < 0) { break; @@ -1755,7 +1702,7 @@ static int kinetis_ioc_wait(struct kinetis_epinfo_s *epinfo) * Description: * Add a new, ready-to-go QH w/attached qTDs to the asynchronous queue. * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -2205,7 +2152,7 @@ static struct kinetis_qtd_s *kinetis_qtd_statusphase(uint32_t tokenbits) * This is a blocking function; it will not return until the control * transfer has completed. * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2488,7 +2435,7 @@ static int kinetis_async_setup(struct kinetis_rhport_s *rhport, * frame list), followed by shorter poll rates, with queue heads with a * poll rate of one, on the very end." * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2592,8 +2539,8 @@ static int kinetis_intr_setup(struct kinetis_rhport_s *rhport, * Description: * Wait for an IN or OUT transfer to complete. * - * Assumption: The caller holds the EHCI exclsem. The caller must be aware - * that the EHCI exclsem will released while waiting for the transfer to + * Assumption: The caller holds the EHCI lock. The caller must be aware + * that the EHCI lock will released while waiting for the transfer to * complete, but will be re-acquired when before returning. The state of * EHCI resources could be very different upon return. * @@ -2611,29 +2558,29 @@ static ssize_t kinetis_transfer_wait(struct kinetis_epinfo_s *epinfo) int ret; int ret2; - /* Release the EHCI semaphore while we wait. Other threads need the + /* Release the EHCI mutex while we wait. Other threads need the * opportunity to access the EHCI resources while we wait. * * REVISIT: Is this safe? NO. This is a bug and needs rethinking. * We need to lock all of the port-resources (not EHCI common) until - * the transfer is complete. But we can't use the common EHCI exclsem + * the transfer is complete. But we can't use the common EHCI lock * or we will deadlock while waiting (because the working thread that - * wakes this thread up needs the exclsem). + * wakes this thread up needs the lock). */ /* REVISIT */ - kinetis_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Wait for the IOC completion event */ ret = kinetis_ioc_wait(epinfo); - /* Re-acquire the EHCI semaphore. The caller expects to be holding + /* Re-acquire the EHCI mutex. The caller expects to be holding * this upon return. */ - ret2 = kinetis_takesem_noncancelable(&g_ehci.exclsem); + ret2 = nxmutex_lock(&g_ehci.lock); if (ret >= 0 && ret2 < 0) { ret = ret2; @@ -2657,9 +2604,7 @@ static ssize_t kinetis_transfer_wait(struct kinetis_epinfo_s *epinfo) } #endif - /* Did kinetis_ioc_wait() or kinetis_takesem_noncancelable() report an - * error? - */ + /* Did kinetis_ioc_wait() or nxmutex_lock() report an error? */ if (ret < 0) { @@ -2968,7 +2913,7 @@ static int kinetis_qh_ioccheck(struct kinetis_qh_s *qh, /* Yes... wake it up */ epinfo->iocwait = false; - kinetis_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); } #ifdef CONFIG_USBHOST_ASYNCH @@ -3129,7 +3074,7 @@ static int kinetis_qh_cancel(struct kinetis_qh_s *qh, * detected (actual number of bytes received was less than the expected * number of bytes)." * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -3266,7 +3211,7 @@ static inline void kinetis_portsc_bottomhalf(void) if (g_ehci.pscwait) { - kinetis_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3318,7 +3263,7 @@ static inline void kinetis_portsc_bottomhalf(void) if (g_ehci.pscwait) { - kinetis_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3401,7 +3346,7 @@ static void kinetis_ehci_bottomhalf(void *arg) * real option (other than to reschedule and delay). */ - kinetis_takesem_noncancelable(&g_ehci.exclsem); + nxmutex_lock(&g_ehci.lock); /* Handle all unmasked interrupt sources * Interrupt on Async Advance @@ -3518,7 +3463,7 @@ static void kinetis_ehci_bottomhalf(void *arg) /* We are done with the EHCI structures */ - kinetis_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Re-enable relevant EHCI interrupts. Interrupts should still be enabled * at the level of the interrupt controller. @@ -3671,7 +3616,7 @@ static int kinetis_wait(struct usbhost_connection_s *conn, */ g_ehci.pscwait = true; - ret = kinetis_takesem(&g_ehci.pscsem); + ret = nxsem_wait_uninterruptible(&g_ehci.pscsem); if (ret < 0) { return ret; @@ -4004,7 +3949,7 @@ static int kinetis_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the EHCI data structures. */ - ret = kinetis_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret >= 0) { /* Remember the new device address and max packet size */ @@ -4013,7 +3958,7 @@ static int kinetis_ep0configure(struct usbhost_driver_s *drvr, epinfo->speed = speed; epinfo->maxpacket = maxpacketsize; - kinetis_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); } return ret; @@ -4378,7 +4323,7 @@ static int kinetis_ctrlin(struct usbhost_driver_s *drvr, * structures. */ - ret = kinetis_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4390,7 +4335,7 @@ static int kinetis_ctrlin(struct usbhost_driver_s *drvr, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Now initiate the transfer */ @@ -4405,13 +4350,13 @@ static int kinetis_ctrlin(struct usbhost_driver_s *drvr, /* And wait for the transfer to complete */ nbytes = kinetis_transfer_wait(ep0info); - kinetis_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes >= 0 ? OK : (int)nbytes; errout_with_iocwait: ep0info->iocwait = false; -errout_with_sem: - kinetis_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4481,7 +4426,7 @@ static ssize_t kinetis_transfer(struct usbhost_driver_s *drvr, * structures. */ - ret = kinetis_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return (ssize_t)ret; @@ -4493,7 +4438,7 @@ static ssize_t kinetis_transfer(struct usbhost_driver_s *drvr, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4531,14 +4476,14 @@ static ssize_t kinetis_transfer(struct usbhost_driver_s *drvr, /* Then wait for the transfer to complete */ nbytes = kinetis_transfer_wait(epinfo); - kinetis_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes; errout_with_iocwait: epinfo->iocwait = false; -errout_with_sem: +errout_with_lock: uerr("!!!\n"); - kinetis_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return (ssize_t)ret; } @@ -4593,7 +4538,7 @@ static int kinetis_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * structures. */ - ret = kinetis_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4605,7 +4550,7 @@ static int kinetis_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4642,14 +4587,14 @@ static int kinetis_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* The transfer is in progress */ - kinetis_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return OK; errout_with_callback: epinfo->callback = NULL; epinfo->arg = NULL; -errout_with_sem: - kinetis_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4697,7 +4642,7 @@ static int kinetis_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * interrupt level. */ - ret = kinetis_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4740,7 +4685,7 @@ static int kinetis_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #endif { ret = OK; - goto errout_with_sem; + goto errout_with_lock; } /* Handle the cancellation according to the type of the transfer */ @@ -4803,7 +4748,7 @@ static int kinetis_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) default: usbhost_trace1(EHCI_TRACE1_BADXFRTYPE, epinfo->xfrtype); ret = -ENOSYS; - goto errout_with_sem; + goto errout_with_lock; } /* Find and remove the QH. There are four possibilities: @@ -4833,7 +4778,7 @@ static int kinetis_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Yes... wake it up */ DEBUGASSERT(callback == NULL); - kinetis_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); } /* No.. Is there a pending asynchronous transfer? */ @@ -4848,11 +4793,11 @@ static int kinetis_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #else /* Wake up the waiting thread */ - kinetis_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); #endif -errout_with_sem: - kinetis_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4899,7 +4844,7 @@ static int kinetis_connect(struct usbhost_driver_s *drvr, if (g_ehci.pscwait) { g_ehci.pscwait = false; - kinetis_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); } leave_critical_section(flags); @@ -5125,7 +5070,7 @@ struct usbhost_connection_s *kinetis_ehci_initialize(int controller) /* Initialize the EHCI state data structure */ - nxsem_init(&g_ehci.exclsem, 0, 1); + nxmutex_init(&g_ehci.lock); nxsem_init(&g_ehci.pscsem, 0, 0); /* The pscsem semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/kl/kl_spi.c b/arch/arm/src/kl/kl_spi.c index 1443964ebf5c4..248614b081a7b 100644 --- a/arch/arm/src/kl/kl_spi.c +++ b/arch/arm/src/kl/kl_spi.c @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include @@ -54,7 +54,7 @@ struct kl_spidev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ uint32_t spibase; /* Base address of SPI registers */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (8 to 16) */ @@ -236,11 +236,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -689,9 +689,9 @@ struct spi_dev_s *kl_spibus_initialize(int port) spi_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); return &priv->spidev; } diff --git a/arch/arm/src/lc823450/lc823450_adc.c b/arch/arm/src/lc823450/lc823450_adc.c index c9b6a4adeadab..db1dcfd2729b0 100644 --- a/arch/arm/src/lc823450/lc823450_adc.c +++ b/arch/arm/src/lc823450/lc823450_adc.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include "arm_internal.h" @@ -80,7 +81,7 @@ struct lc823450_adc_inst_s const struct adc_callback_s *cb; struct adc_dev_s dev; - sem_t sem_excl; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ #ifndef CONFIG_ADC_POLLED sem_t sem_isr; /* Interrupt wait semaphore */ #endif @@ -93,10 +94,6 @@ struct lc823450_adc_inst_s ****************************************************************************/ static inline void lc823450_adc_clearirq(void); -static inline int lc823450_adc_sem_wait( - struct lc823450_adc_inst_s *inst); -static inline void lc823450_adc_sem_post( - struct lc823450_adc_inst_s *inst); static int lc823450_adc_bind(struct adc_dev_s *dev, const struct adc_callback_s *callback); @@ -256,33 +253,6 @@ static void lc823450_adc_start(struct lc823450_adc_inst_s *inst) #endif } -/**************************************************************************** - * Name: lc823450_adc_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary - * - ****************************************************************************/ - -static inline int lc823450_adc_sem_wait(struct lc823450_adc_inst_s *inst) -{ - return nxsem_wait_uninterruptible(&inst->sem_excl); -} - -/**************************************************************************** - * Name: lc823450_adc_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void lc823450_adc_sem_post( - struct lc823450_adc_inst_s *inst) -{ - nxsem_post(&inst->sem_excl); -} - /**************************************************************************** * Name: lc823450_adc_isr * @@ -399,7 +369,7 @@ static void lc823450_adc_rxint(struct adc_dev_s *dev, bool enable) ainfo("enable: %d\n", enable); - ret = lc823450_adc_sem_wait(inst); + ret = nxmutex_lock(&inst->lock); if (ret < 0) { return; @@ -416,7 +386,7 @@ static void lc823450_adc_rxint(struct adc_dev_s *dev, bool enable) } #endif - lc823450_adc_sem_post(inst); + nxmutex_unlock(&inst->lock); } /**************************************************************************** @@ -442,7 +412,7 @@ static int lc823450_adc_ioctl(struct adc_dev_s *dev, int cmd, ainfo("cmd=%xh\n", cmd); - ret = lc823450_adc_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -493,8 +463,7 @@ static int lc823450_adc_ioctl(struct adc_dev_s *dev, int cmd, break; } - lc823450_adc_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -538,12 +507,12 @@ struct adc_dev_s *lc823450_adcinitialize(void) inst->nchannels = CONFIG_LC823450_ADC_NCHANNELS; inst->chanlist = lc823450_chanlist; - nxsem_init(&inst->sem_excl, 0, 1); + nxmutex_init(&inst->lock); #ifndef CONFIG_ADC_POLLED nxsem_init(&inst->sem_isr, 0, 0); #endif - ret = lc823450_adc_sem_wait(inst); + ret = nxmutex_lock(&inst->lock); if (ret < 0) { aerr("adc_register failed: %d\n", ret); @@ -578,7 +547,7 @@ struct adc_dev_s *lc823450_adcinitialize(void) if (ret < 0) { aerr("adc_register failed: %d\n", ret); - lc823450_adc_sem_post(inst); + nxmutex_unlock(&inst->lock); kmm_free(g_inst); return NULL; } @@ -590,8 +559,7 @@ struct adc_dev_s *lc823450_adcinitialize(void) /* Now we are initialized */ g_inst = inst; - - lc823450_adc_sem_post(inst); + nxmutex_unlock(&inst->lock); } return &g_inst->dev; @@ -619,7 +587,7 @@ int lc823450_adc_receive(struct adc_dev_s *dev, return -EINVAL; } - ret = lc823450_adc_sem_wait(inst); + ret = nxmutex_lock(&inst->lock); if (ret < 0) { return ret; @@ -635,7 +603,7 @@ int lc823450_adc_receive(struct adc_dev_s *dev, } lc823450_adc_standby(1); - lc823450_adc_sem_post(inst); + nxmutex_unlock(&inst->lock); return OK; } diff --git a/arch/arm/src/lc823450/lc823450_dma.c b/arch/arm/src/lc823450/lc823450_dma.c index f4dac0f31622a..777eb5b4f520e 100644 --- a/arch/arm/src/lc823450/lc823450_dma.c +++ b/arch/arm/src/lc823450/lc823450_dma.c @@ -36,6 +36,7 @@ #include #include +#include #include "arm_internal.h" #include "lc823450_dma.h" @@ -106,7 +107,7 @@ struct lc823450_dmach_s struct lc823450_dma_s { - sem_t exclsem; /* For exclusive access to the DMA channel list */ + mutex_t lock; /* For exclusive access to the DMA channel list */ /* This is the state of each DMA channel */ @@ -341,7 +342,7 @@ void arm_dma_initialize(void) sq_init(&g_dma.phydmach[i].req_q); } - nxsem_init(&g_dma.exclsem, 0, 1); + nxmutex_init(&g_dma.lock); if (irq_attach(LC823450_IRQ_DMAC, dma_interrupt, NULL) != 0) { diff --git a/arch/arm/src/lc823450/lc823450_i2c.c b/arch/arm/src/lc823450/lc823450_i2c.c index f86bcab483239..36e82cda611e1 100644 --- a/arch/arm/src/lc823450/lc823450_i2c.c +++ b/arch/arm/src/lc823450/lc823450_i2c.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -117,7 +118,7 @@ struct lc823450_i2c_priv_s const struct lc823450_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 @@ -137,10 +138,6 @@ struct lc823450_i2c_priv_s * Private Function Prototypes ****************************************************************************/ -static inline int -lc823450_i2c_sem_wait(struct lc823450_i2c_priv_s *priv); -static inline void -lc823450_i2c_sem_post(struct lc823450_i2c_priv_s *priv); static inline int lc823450_i2c_sem_waitdone(struct lc823450_i2c_priv_s *priv); @@ -209,6 +206,10 @@ static struct lc823450_i2c_priv_s lc823450_i2c0_priv = .ops = &lc823450_i2c_ops, .config = &lc823450_i2c0_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .irqstate = IRQSTATE_IDLE, .msgc = 0, .msgv = NULL, @@ -235,6 +236,10 @@ static struct lc823450_i2c_priv_s lc823450_i2c1_priv = .ops = &lc823450_i2c_ops, .config = &lc823450_i2c1_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .irqstate = IRQSTATE_IDLE, .msgc = 0, .msgv = NULL, @@ -250,34 +255,6 @@ static struct lc823450_i2c_priv_s lc823450_i2c1_priv = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: lc823450_i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. May be interrupted by - * a signal. - * - ****************************************************************************/ - -static inline int lc823450_i2c_sem_wait(struct lc823450_i2c_priv_s *priv) -{ - return nxsem_wait(&priv->sem_excl); -} - -/**************************************************************************** - * Name: lc823450_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void -lc823450_i2c_sem_post(struct lc823450_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - /**************************************************************************** * Name: lc823450_i2c_sem_waitdone * @@ -978,7 +955,7 @@ static int lc823450_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = lc823450_i2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1073,8 +1050,7 @@ static int lc823450_i2c_transfer(struct i2c_master_s *dev, #endif exit: - lc823450_i2c_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -1120,10 +1096,6 @@ struct i2c_master_s *lc823450_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - nxsem_init(&priv->sem_excl, 0, 1); -#ifndef CONFIG_I2C_POLLED - nxsem_init(&priv->sem_isr, 0, 0); -#endif lc823450_i2c_init(priv, port); } @@ -1188,13 +1160,6 @@ int lc823450_i2cbus_uninitialize(struct i2c_master_s *dev) lc823450_i2c_deinit(priv, port); - /* Release unused resources */ - - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif - return OK; } diff --git a/arch/arm/src/lc823450/lc823450_i2s.c b/arch/arm/src/lc823450/lc823450_i2s.c index 9a745f42b23b6..7b3f2d770e6ed 100644 --- a/arch/arm/src/lc823450/lc823450_i2s.c +++ b/arch/arm/src/lc823450/lc823450_i2s.c @@ -306,15 +306,6 @@ static void _setup_audio_pll(uint32_t freq) ); } -/**************************************************************************** - * Name: _i2s_semtake - ****************************************************************************/ - -static int _i2s_semtake(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /**************************************************************************** * Name: lc823450_i2s_rxsamplerate ****************************************************************************/ @@ -521,7 +512,7 @@ static int lc823450_i2s_receive(struct i2s_dev_s *dev, /* Wait for Audio Buffer */ - ret = _i2s_semtake(&_sem_buf_over); + ret = nxsem_wait_uninterruptible(&_sem_buf_over); if (ret < 0) { /* Disable J Buffer Over Level IRQ */ @@ -550,10 +541,10 @@ static int lc823450_i2s_receive(struct i2s_dev_s *dev, _i2s_rxdma_callback, &_sem_rxdma); - ret = _i2s_semtake(&_sem_rxdma); + ret = nxsem_wait_uninterruptible(&_sem_rxdma); if (ret < 0) { - /* Stop DMA because semtake failed */ + /* Stop DMA because semwait failed */ lc823450_dmastop(_hrxdma); @@ -686,7 +677,7 @@ static int lc823450_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Wait for Audio Buffer */ - ret = _i2s_semtake(&_sem_buf_under); + ret = nxsem_wait_uninterruptible(&_sem_buf_under); if (ret < 0) { /* Disable C Buffer Under Level IRQ */ @@ -734,10 +725,10 @@ static int lc823450_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, _i2s_txdma_callback, &_sem_txdma); - ret = _i2s_semtake(&_sem_txdma); + ret = nxsem_wait_uninterruptible(&_sem_txdma); if (ret < 0) { - /* Stop DMA because semtake failed */ + /* Stop DMA because semwait failed */ lc823450_dmastop(_htxdma); diff --git a/arch/arm/src/lc823450/lc823450_mtd.c b/arch/arm/src/lc823450/lc823450_mtd.c index 4a339faa32deb..c6277333b116f 100644 --- a/arch/arm/src/lc823450/lc823450_mtd.c +++ b/arch/arm/src/lc823450/lc823450_mtd.c @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #include "lc823450_mtd.h" @@ -74,7 +74,7 @@ struct lc823450_mtd_dev_s /* Other implementation specific data may follow here */ - sem_t sem; /* Assures mutually exclusive access to the slot */ + mutex_t lock; /* Assures mutually exclusive access to the slot */ uint32_t nblocks; /* Number of blocks */ uint32_t blocksize; /* Size of one read/write blocks */ uint32_t channel; /* 0: eMMC, 1: SDC */ @@ -90,7 +90,7 @@ struct lc823450_partinfo_s * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; static struct mtd_dev_s *g_mtdpart[LC823450_NPARTS]; static struct mtd_dev_s *g_mtdmaster[CONFIG_MTD_DEV_MAX]; /* 0: eMMC, 1: SDC */ @@ -118,24 +118,6 @@ static struct lc823450_partinfo_s partinfo[LC823450_NPARTS] = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: mtd_semtake - ****************************************************************************/ - -static int mtd_semtake(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: mtd_semgive - ****************************************************************************/ - -static void mtd_semgive(sem_t *sem) -{ - nxsem_post(sem); -} - /**************************************************************************** * Name: lc823450_erase * @@ -191,7 +173,7 @@ static ssize_t lc823450_bread(struct mtd_dev_s *dev, off_t startblock, return -EINVAL; } - ret = mtd_semtake(&priv->sem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -200,7 +182,7 @@ static ssize_t lc823450_bread(struct mtd_dev_s *dev, off_t startblock, if (!g_mtdmaster[priv->channel]) { finfo("device removed\n"); - mtd_semgive(&priv->sem); + nxmutex_unlock(&priv->lock); return -ENODEV; } @@ -211,8 +193,7 @@ static ssize_t lc823450_bread(struct mtd_dev_s *dev, off_t startblock, ret = lc823450_sdc_readsector(priv->channel, (unsigned long)(startblock), (unsigned short)nblocks, buf, type); - - mtd_semgive(&priv->sem); + nxmutex_unlock(&priv->lock); if (ret != OK) { @@ -264,7 +245,7 @@ static ssize_t lc823450_bwrite(struct mtd_dev_s *dev, off_t startblock, return -EINVAL; } - ret = mtd_semtake(&priv->sem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -273,7 +254,7 @@ static ssize_t lc823450_bwrite(struct mtd_dev_s *dev, off_t startblock, if (!g_mtdmaster[priv->channel]) { finfo("device removed\n"); - mtd_semgive(&priv->sem); + nxmutex_unlock(&priv->lock); return -ENODEV; } @@ -284,8 +265,7 @@ static ssize_t lc823450_bwrite(struct mtd_dev_s *dev, off_t startblock, ret = lc823450_sdc_writesector(priv->channel, (unsigned long)(startblock), (unsigned short)nblocks, (void *)buf, type); - - mtd_semgive(&priv->sem); + nxmutex_unlock(&priv->lock); if (ret != OK) { @@ -312,7 +292,7 @@ static int lc823450_ioctl(struct mtd_dev_s *dev, int cmd, finfo("cmd=%xh, arg=%lxh\n", cmd, arg); - ret = mtd_semtake(&priv->sem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -323,7 +303,7 @@ static int lc823450_ioctl(struct mtd_dev_s *dev, int cmd, if (!g_mtdmaster[priv->channel]) { finfo("device removed\n"); - mtd_semgive(&priv->sem); + nxmutex_unlock(&priv->lock); return -ENODEV; } @@ -399,7 +379,7 @@ static int lc823450_ioctl(struct mtd_dev_s *dev, int cmd, break; } - mtd_semgive(&priv->sem); + nxmutex_unlock(&priv->lock); return ret; } @@ -422,7 +402,7 @@ static int mtd_mediainitialize(struct lc823450_mtd_dev_s *dev) finfo("enter\n"); - ret = mtd_semtake(&dev->sem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -508,7 +488,7 @@ static int mtd_mediainitialize(struct lc823450_mtd_dev_s *dev) dev->channel, (uint64_t)blocksize * (uint64_t)nblocks); exit_with_error: - mtd_semgive(&dev->sem); + nxmutex_unlock(&dev->lock); return ret; } @@ -538,7 +518,7 @@ static struct mtd_dev_s *lc823450_mtd_allocdev(uint32_t channel) return NULL; } - nxsem_init(&priv->sem, 0, 1); + nxmutex_init(&priv->lock); priv->mtd.erase = lc823450_erase; priv->mtd.bread = lc823450_bread; @@ -558,7 +538,7 @@ static struct mtd_dev_s *lc823450_mtd_allocdev(uint32_t channel) if (ret != OK) { finfo("ERROR: Failed to initialize media\n"); - nxsem_destroy(&priv->sem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return NULL; } @@ -608,7 +588,7 @@ int lc823450_mtd_initialize(uint32_t devno) * /dev/mtdblock0pN : Nth child partition */ - ret = mtd_semtake(&g_sem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -617,7 +597,7 @@ int lc823450_mtd_initialize(uint32_t devno) if (g_mtdmaster[ch]) { finfo("Device already registered\n"); - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); return -EBUSY; } @@ -627,7 +607,7 @@ int lc823450_mtd_initialize(uint32_t devno) if (!g_mtdmaster[ch]) { finfo("Failed to create master partition: ch=%" PRId32 "\n", ch); - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); return -ENODEV; } @@ -639,7 +619,7 @@ int lc823450_mtd_initialize(uint32_t devno) ch); kmm_free(g_mtdmaster[ch]); g_mtdmaster[ch] = NULL; - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); return -ENODEV; } @@ -655,7 +635,7 @@ int lc823450_mtd_initialize(uint32_t devno) if (devno == CONFIG_MTD_DEVNO_SDC) { finfo("SDC has no child partitions.\n"); - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); return OK; } #endif @@ -696,7 +676,7 @@ int lc823450_mtd_initialize(uint32_t devno) finfo("%s(): mtd_partition failed. startblock=%" PRIuOFF " nblocks=%" PRIuOFF "\n", __func__, partinfo[i].startblock, partinfo[i].nblocks); - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); DEBUGPANIC(); return -EIO; } @@ -706,13 +686,13 @@ int lc823450_mtd_initialize(uint32_t devno) { finfo("%s(): mmcl_initialize part%d failed: %d\n", __func__, partno, ret); - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); DEBUGPANIC(); return ret; } } - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); return OK; } @@ -793,7 +773,7 @@ int lc823450_mtd_uninitialize(uint32_t devno) DEBUGASSERT(devno == CONFIG_MTD_DEVNO_SDC); - ret = mtd_semtake(&g_sem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -803,7 +783,7 @@ int lc823450_mtd_uninitialize(uint32_t devno) if (!priv) { finfo("SD card is not identified yet\n"); - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); return -ENODEV; } @@ -813,17 +793,17 @@ int lc823450_mtd_uninitialize(uint32_t devno) mtd_unregister(g_mtdmaster[ch]); #endif - ret = mtd_semtake(&priv->sem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); return ret; } ret = lc823450_sdc_clearcardinfo(ch); DEBUGASSERT(ret == OK); - mtd_semgive(&priv->sem); + nxmutex_unlock(&priv->lock); ret = mmcl_uninitialize(devname); if (ret != OK) @@ -834,13 +814,13 @@ int lc823450_mtd_uninitialize(uint32_t devno) ret = lc823450_sdc_finalize(ch); DEBUGASSERT(ret == OK); - nxsem_destroy(&priv->sem); + nxmutex_destroy(&priv->lock); kmm_free(g_mtdmaster[ch]); g_mtdmaster[ch] = NULL; - mtd_semgive(&g_sem); + nxmutex_unlock(&g_lock); #ifdef CONFIG_DEBUG finfo("/dev/mtdblock%d deleted\n", devno); diff --git a/arch/arm/src/lc823450/lc823450_sdc.c b/arch/arm/src/lc823450/lc823450_sdc.c index 50c5980434aba..81238c20a866f 100644 --- a/arch/arm/src/lc823450/lc823450_sdc.c +++ b/arch/arm/src/lc823450/lc823450_sdc.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include @@ -78,10 +79,10 @@ * Private Data ****************************************************************************/ -static sem_t _sdc_sem[2] = +static mutex_t _sdc_lock[2] = { - SEM_INITIALIZER(1), - SEM_INITIALIZER(1) + NXMUTEX_INITIALIZER, + NXMUTEX_INITIALIZER }; static struct sddrcfg_s _sdch0; @@ -133,24 +134,6 @@ extern SINT_T sddep_write(void *src, void *dst, UI_32 size, SINT_T type, * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: _sdc_semtake - ****************************************************************************/ - -static int _sdc_semtake(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: _sdc_semgive - ****************************************************************************/ - -static void _sdc_semgive(sem_t *sem) -{ - nxsem_post(sem); -} - /**************************************************************************** * Name: _lc823450_sdc_support_trim ****************************************************************************/ @@ -202,7 +185,7 @@ int lc823450_sdc_clearcardinfo(uint32_t ch) mcinfo("++++ start\n"); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret < 0) { return ret; @@ -218,7 +201,7 @@ int lc823450_sdc_clearcardinfo(uint32_t ch) } #endif - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); return ret; } @@ -277,11 +260,11 @@ int lc823450_sdc_initialize(uint32_t ch) mcinfo("++++ start\n"); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret >= 0) { ret = sddr_initialize(_cfg[ch]); - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); } @@ -298,11 +281,11 @@ int lc823450_sdc_finalize(uint32_t ch) mcinfo("++++ start ch=%ld\n", ch); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret >= 0) { ret = sddr_finalize(_cfg[ch]); - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); } @@ -319,7 +302,7 @@ int lc823450_sdc_identifycard(uint32_t ch) mcinfo("++++ start\n"); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret < 0) { return ret; @@ -335,7 +318,7 @@ int lc823450_sdc_identifycard(uint32_t ch) } #endif - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); return ret; } @@ -351,11 +334,11 @@ int lc823450_sdc_setclock(uint32_t ch, uint32_t limitclk, uint32_t sysclk) mcinfo("++++ start ch=%ld limitClk=%ld sysClk=%ld\n", ch, limitclk, sysclk); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret >= 0) { ret = sddr_setclock(limitclk, sysclk, _cfg[ch]); - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); } @@ -376,11 +359,11 @@ int lc823450_sdc_refmediatype(uint32_t ch) mcinfo("++++ start\n"); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret >= 0) { ret = sddr_refmediatype(_cfg[ch]); - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); } @@ -398,12 +381,12 @@ int lc823450_sdc_getcardsize(uint32_t ch, mcinfo("++++ start\n"); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret >= 0) { ret = sddr_getcardsize(psecnum, psecsize, _cfg[ch]); - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); } @@ -421,7 +404,7 @@ int lc823450_sdc_readsector(uint32_t ch, int ret; int i = 0; - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret < 0) { return ret; @@ -435,7 +418,7 @@ int lc823450_sdc_readsector(uint32_t ch, if (ch && _sec_cache_enabled && 1 == cnt && addr == _sec_cache_add) { memcpy(pbuf, _sec_cache, sizeof(_sec_cache)); - goto errout_with_semaphore; + goto errout_with_lock; } #endif @@ -488,9 +471,9 @@ int lc823450_sdc_readsector(uint32_t ch, } } -errout_with_semaphore: +errout_with_lock: #endif - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); return ret; @@ -506,7 +489,7 @@ int lc823450_sdc_writesector(uint32_t ch, { int ret; - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret < 0) { return ret; @@ -536,7 +519,7 @@ int lc823450_sdc_writesector(uint32_t ch, mcinfo("ret=%d ch=%" PRId32 " add=%ld cnt=%d\n", ret, ch, addr, cnt); } - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); return ret; @@ -560,7 +543,7 @@ int lc823450_sdc_trimsector(uint32_t ch, unsigned long addr, { int ret; - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret < 0) { return ret; @@ -582,7 +565,7 @@ int lc823450_sdc_trimsector(uint32_t ch, unsigned long addr, mcinfo("ret=%d ch=%" PRId32 " add=%ld cnt=%d\n", ret, ch, addr, cnt); } - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); return ret; @@ -598,12 +581,12 @@ int lc823450_sdc_cachectl(uint32_t ch, int ctrl) mcinfo("++++ ch=%" PRId32 ", ctrl=%d\n", ch, ctrl); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret >= 0) { ret = sddr_cachectrl(ctrl, _cfg[ch]); - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); } @@ -620,7 +603,7 @@ int lc823450_sdc_changespeedmode(uint32_t ch, int mode) mcinfo("++++ ch=%" PRId32 ", mode=%d\n", ch, mode); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret < 0) { return ret; @@ -646,7 +629,7 @@ int lc823450_sdc_changespeedmode(uint32_t ch, int mode) } } - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); return ret; } @@ -662,7 +645,7 @@ int lc823450_sdc_getcid(uint32_t ch, char *cidstr, int length) mcinfo("++++ ch=%" PRId32 "\n", ch); - ret = _sdc_semtake(&_sdc_sem[ch]); + ret = nxmutex_lock(&_sdc_lock[ch]); if (ret < 0) { return ret; @@ -683,7 +666,7 @@ int lc823450_sdc_getcid(uint32_t ch, char *cidstr, int length) *cidstr = '\0'; } - _sdc_semgive(&_sdc_sem[ch]); + nxmutex_unlock(&_sdc_lock[ch]); mcinfo("---- end ret=%d\n", ret); return ret; } @@ -694,7 +677,6 @@ int lc823450_sdc_getcid(uint32_t ch, char *cidstr, int length) int lc823450_sdc_locked(void) { - int val; int ret; int i; @@ -702,8 +684,7 @@ int lc823450_sdc_locked(void) for (i = 0; i < 2; i++) { - nxsem_get_value(&_sdc_sem[i], &val); - if (1 != val) + if (nxmutex_is_locked(&_sdc_lock[i])) { ret = 1; break; diff --git a/arch/arm/src/lc823450/lc823450_sddrv_dep.c b/arch/arm/src/lc823450/lc823450_sddrv_dep.c index 271be2f9f7e5c..17dbfbc3909f9 100644 --- a/arch/arm/src/lc823450/lc823450_sddrv_dep.c +++ b/arch/arm/src/lc823450/lc823450_sddrv_dep.c @@ -128,15 +128,6 @@ static void dma_callback(DMA_HANDLE hdma, void *arg, int result) } #endif /* CONFIG_LC823450_SDC_DMA */ -/**************************************************************************** - * Name: _sddep_semtake - ****************************************************************************/ - -static int _sddep_semtake(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -427,7 +418,7 @@ SINT_T sddep_read(void *src, void *dst, UI_32 size, SINT_T type, } lc823450_dmastart(_hrdma[ch], dma_callback, &_sem_rwait[ch]); - return _sddep_semtake(&_sem_rwait[ch]); + return nxsem_wait_uninterruptible(&_sem_rwait[ch]); #else SINT_T i; UI_32 *p = (UI_32 *)src; @@ -518,7 +509,7 @@ SINT_T sddep_write(void *src, void *dst, UI_32 size, SINT_T type, } lc823450_dmastart(_hwdma[ch], dma_callback, &_sem_wwait[ch]); - return _sddep_semtake(&_sem_wwait[ch]); + return nxsem_wait_uninterruptible(&_sem_wwait[ch]); #else SINT_T i; diff --git a/arch/arm/src/lc823450/lc823450_spi.c b/arch/arm/src/lc823450/lc823450_spi.c index 3c8eb2791d865..660a4b410617b 100644 --- a/arch/arm/src/lc823450/lc823450_spi.c +++ b/arch/arm/src/lc823450/lc823450_spi.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -65,7 +65,7 @@ struct lc823450_spidev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ #ifndef CONFIG_SPI_OWNBUS - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (8 to 16) */ @@ -144,11 +144,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -524,7 +524,7 @@ struct spi_dev_s *lc823450_spibus_initialize(int port) modifyreg32(MRSTCNTAPB, 0, MRSTCNTAPB_PORT5_RSTB); #ifndef CONFIG_SPI_OWNBUS - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #endif /* Initialize SPI mode. It must be done before starting SPI transfer */ diff --git a/arch/arm/src/lpc17xx_40xx/lpc17_40_gpdma.c b/arch/arm/src/lpc17xx_40xx/lpc17_40_gpdma.c index 28b29943fc928..850064cacb03a 100644 --- a/arch/arm/src/lpc17xx_40xx/lpc17_40_gpdma.c +++ b/arch/arm/src/lpc17xx_40xx/lpc17_40_gpdma.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include "arm_internal.h" #include "chip.h" @@ -67,7 +67,7 @@ struct lpc17_40_dmach_s struct lpc17_40_gpdma_s { - sem_t exclsem; /* For exclusive access to the DMA channel list */ + mutex_t lock; /* For exclusive access to the DMA channel list */ /* This is the state of each DMA channel */ @@ -291,7 +291,7 @@ void weak_function arm_dma_initialize(void) /* Initialize the DMA state structure */ - nxsem_init(&g_gpdma.exclsem, 0, 1); + nxmutex_init(&g_gpdma.lock); for (i = 0; i < LPC17_40_NDMACH; i++) { @@ -379,7 +379,7 @@ DMA_HANDLE lpc17_40_dmachannel(void) /* Get exclusive access to the GPDMA state structure */ - ret = nxsem_wait_uninterruptible(&g_gpdma.exclsem); + ret = nxmutex_lock(&g_gpdma.lock); if (ret < 0) { return NULL; @@ -401,7 +401,7 @@ DMA_HANDLE lpc17_40_dmachannel(void) /* Return what we found (or not) */ - nxsem_post(&g_gpdma.exclsem); + nxmutex_unlock(&g_gpdma.lock); return (DMA_HANDLE)dmach; } diff --git a/arch/arm/src/lpc17xx_40xx/lpc17_40_i2c.c b/arch/arm/src/lpc17xx_40xx/lpc17_40_i2c.c index f2706a7d7c038..398ac7f927dfa 100644 --- a/arch/arm/src/lpc17xx_40xx/lpc17_40_i2c.c +++ b/arch/arm/src/lpc17xx_40xx/lpc17_40_i2c.c @@ -60,6 +60,7 @@ #include #include +#include #include #include @@ -108,7 +109,7 @@ struct lpc17_40_i2cdev_s unsigned int base; /* Base address of registers */ uint16_t irqid; /* IRQ for this device */ - sem_t mutex; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t wait; /* Place to wait for state machine completion */ volatile uint8_t state; /* State of state machine */ struct wdog_s timeout; /* Watchdog to timeout when bus hung */ @@ -302,7 +303,7 @@ static int lpc17_40_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - nxsem_wait(&priv->mutex); + nxmutex_lock(&priv->lock); /* Set up for the transfer */ @@ -323,7 +324,7 @@ static int lpc17_40_i2c_transfer(struct i2c_master_s *dev, ret = lpc17_40_i2c_start(priv); - nxsem_post(&priv->mutex); + nxmutex_unlock(&priv->lock); return ret; } @@ -616,9 +617,9 @@ struct i2c_master_s *lpc17_40_i2cbus_initialize(int port) putreg32(I2C_CONSET_I2EN, priv->base + LPC17_40_I2C_CONSET_OFFSET); - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->mutex, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->wait, 0, 0); /* The wait semaphore is used for signaling and, hence, should not have @@ -659,7 +660,7 @@ int lpc17_40_i2cbus_uninitialize(struct i2c_master_s * dev) /* Reset data structures */ - nxsem_destroy(&priv->mutex); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->wait); /* Cancel the watchdog timer */ diff --git a/arch/arm/src/lpc17xx_40xx/lpc17_40_sdcard.c b/arch/arm/src/lpc17xx_40xx/lpc17_40_sdcard.c index c4a64f1e100a2..e4f639390e2ba 100644 --- a/arch/arm/src/lpc17xx_40xx/lpc17_40_sdcard.c +++ b/arch/arm/src/lpc17xx_40xx/lpc17_40_sdcard.c @@ -292,8 +292,6 @@ struct lpc17_40_sampleregs_s /* Low-level helpers ********************************************************/ -static int lpc17_40_takesem(struct lpc17_40_dev_s *priv); -#define lpc17_40_givesem(priv) (nxsem_post(&priv->waitsem)) static inline void lpc17_40_setclock(uint32_t clkcr); static void lpc17_40_configwaitints(struct lpc17_40_dev_s *priv, uint32_t waitmask, sdio_eventset_t waitevents, @@ -463,27 +461,6 @@ static struct lpc17_40_sampleregs_s g_sampleregs[DEBUG_NSAMPLES]; * Low-level Helpers ****************************************************************************/ -/**************************************************************************** - * Name: lpc17_40_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SD card device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int lpc17_40_takesem(struct lpc17_40_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: lpc17_40_setclock * @@ -1122,7 +1099,7 @@ static void lpc17_40_endwait(struct lpc17_40_dev_s *priv, /* Wake up the waiting thread */ - lpc17_40_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2350,7 +2327,7 @@ static sdio_eventset_t lpc17_40_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = lpc17_40_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/arm/src/lpc17xx_40xx/lpc17_40_spi.c b/arch/arm/src/lpc17xx_40xx/lpc17_40_spi.c index c0342cfafdae5..09c1fe05b6c4a 100644 --- a/arch/arm/src/lpc17xx_40xx/lpc17_40_spi.c +++ b/arch/arm/src/lpc17xx_40xx/lpc17_40_spi.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -84,7 +84,7 @@ struct lpc17_40_spidev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (8 to 16) */ @@ -180,11 +180,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -564,9 +564,9 @@ struct spi_dev_s *lpc17_40_spibus_initialize(int port) spi_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); return &priv->spidev; } diff --git a/arch/arm/src/lpc17xx_40xx/lpc17_40_ssp.c b/arch/arm/src/lpc17xx_40xx/lpc17_40_ssp.c index 2047883a36fe2..39dbb6a4b3cfc 100644 --- a/arch/arm/src/lpc17xx_40xx/lpc17_40_ssp.c +++ b/arch/arm/src/lpc17xx_40xx/lpc17_40_ssp.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -102,7 +102,7 @@ struct lpc17_40_sspdev_s #ifdef CONFIG_LPC17_40_SSP_INTERRUPTS uint8_t sspirq; /* SPI IRQ number */ #endif - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 to 16) */ @@ -336,11 +336,11 @@ static int ssp_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -993,9 +993,9 @@ struct spi_dev_s *lpc17_40_sspbus_initialize(int port) ssp_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Enable the SPI */ diff --git a/arch/arm/src/lpc17xx_40xx/lpc17_40_usbhost.c b/arch/arm/src/lpc17xx_40xx/lpc17_40_usbhost.c index fce85730180cd..8066c4008c35b 100644 --- a/arch/arm/src/lpc17xx_40xx/lpc17_40_usbhost.c +++ b/arch/arm/src/lpc17xx_40xx/lpc17_40_usbhost.c @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -163,7 +164,7 @@ struct lpc17_40_usbhost_s uint8_t outinterval; /* Minimum periodic IN EP polling interval: 2, 4, 6, 16, or 32 */ #endif - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait Writeback Done Head event */ #ifdef CONFIG_USBHOST_HUB @@ -272,10 +273,6 @@ static void lpc17_40_putreg(uint32_t val, uint32_t addr); /* Semaphores ***************************************************************/ -static int lpc17_40_takesem(sem_t *sem); -static int lpc17_40_takesem_noncancelable(sem_t *sem); -#define lpc17_40_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t lpc17_40_getle16(const uint8_t *val); @@ -565,54 +562,6 @@ static void lpc17_40_putreg(uint32_t val, uint32_t addr) } #endif -/**************************************************************************** - * Name: lpc17_40_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int lpc17_40_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: lpc17_40_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int lpc17_40_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: lpc17_40_getle16 * @@ -1645,7 +1594,7 @@ static int lpc17_40_ctrltd(struct lpc17_40_usbhost_s *priv, /* Wait for the Writeback Done Head interrupt */ - ret = lpc17_40_takesem(&ed->wdhsem); + ret = nxsem_wait_uninterruptible(&ed->wdhsem); if (ret < 0) { /* Task has been canceled */ @@ -1744,7 +1693,7 @@ static int lpc17_40_usbinterrupt(int irq, void *context, void *arg) if (priv->pscwait) { - lpc17_40_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -1803,7 +1752,7 @@ static int lpc17_40_usbinterrupt(int irq, void *context, void *arg) if (priv->pscwait) { - lpc17_40_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -1942,7 +1891,7 @@ static int lpc17_40_usbinterrupt(int irq, void *context, void *arg) { /* Wake up the thread waiting for the WDH event */ - lpc17_40_givesem(&ed->wdhsem); + nxsem_post(&ed->wdhsem); xfrinfo->wdhwait = false; } @@ -2063,7 +2012,7 @@ static int lpc17_40_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = lpc17_40_takesem(&priv->pscsem); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -2219,7 +2168,7 @@ static int lpc17_40_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to EP0 and the control list */ - ret = lpc17_40_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2237,8 +2186,7 @@ static int lpc17_40_ep0configure(struct usbhost_driver_s *drvr, } ed->hw.ctrl = hwctrl; - - lpc17_40_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); uinfo("EP0 CTRL:%08" PRIx32 "\n", ed->hw.ctrl); return OK; @@ -2285,7 +2233,7 @@ static int lpc17_40_epalloc(struct usbhost_driver_s *drvr, * periodic list and the interrupt table. */ - ret = lpc17_40_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2407,7 +2355,7 @@ static int lpc17_40_epalloc(struct usbhost_driver_s *drvr, } } - lpc17_40_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2446,7 +2394,7 @@ static int lpc17_40_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * periodic list and the interrupt table. */ - ret = lpc17_40_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2484,7 +2432,7 @@ static int lpc17_40_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Put the ED back into the free list */ lpc17_40_edfree(ed); - lpc17_40_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2531,7 +2479,7 @@ static int lpc17_40_alloc(struct usbhost_driver_s *drvr, /* We must have exclusive access to the transfer buffer pool */ - ret = lpc17_40_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2546,7 +2494,7 @@ static int lpc17_40_alloc(struct usbhost_driver_s *drvr, ret = OK; } - lpc17_40_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2583,9 +2531,9 @@ static int lpc17_40_free(struct usbhost_driver_s *drvr, uint8_t *buffer) /* We must have exclusive access to the transfer buffer pool */ - ret = lpc17_40_takesem_noncancelable(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); lpc17_40_tbfree(buffer); - lpc17_40_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2730,7 +2678,7 @@ static int lpc17_40_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to EP0 and the control list */ - ret = lpc17_40_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2752,7 +2700,7 @@ static int lpc17_40_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, } } - lpc17_40_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2773,7 +2721,7 @@ static int lpc17_40_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to EP0 and the control list */ - ret = lpc17_40_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2796,7 +2744,7 @@ static int lpc17_40_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, } } - lpc17_40_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -3072,7 +3020,7 @@ static ssize_t lpc17_40_transfer(struct usbhost_driver_s *drvr, * table. */ - ret = lpc17_40_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -3089,7 +3037,7 @@ static ssize_t lpc17_40_transfer(struct usbhost_driver_s *drvr, { uerr("ERROR: lpc17_40_alloc_xfrinfo failed\n"); nbytes = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } /* Initialize the transfer structure */ @@ -3144,7 +3092,7 @@ static ssize_t lpc17_40_transfer(struct usbhost_driver_s *drvr, /* Wait for the Writeback Done Head interrupt */ - ret = lpc17_40_takesem(&ed->wdhsem); + ret = nxsem_wait_uninterruptible(&ed->wdhsem); if (ret < 0) { return ret; @@ -3203,8 +3151,8 @@ static ssize_t lpc17_40_transfer(struct usbhost_driver_s *drvr, lpc17_40_free_xfrinfo(xfrinfo); ed->xfrinfo = NULL; -errout_with_sem: - lpc17_40_givesem(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); return nbytes; } @@ -3351,7 +3299,7 @@ static int lpc17_40_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * buffer pool, the bulk and interrupt lists, and the HCCA interrupt table. */ - ret = lpc17_40_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -3368,7 +3316,7 @@ static int lpc17_40_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, { uerr("ERROR: lpc17_40_alloc_xfrinfo failed\n"); ret = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } /* Initialize the transfer structure */ @@ -3388,7 +3336,7 @@ static int lpc17_40_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, if (ret < 0) { uerr("ERROR: lpc17_40_dma_alloc failed: %d\n", ret); - goto errout_with_sem; + goto errout_with_lock; } /* If a buffer was allocated, then use it instead of the callers buffer */ @@ -3412,7 +3360,7 @@ static int lpc17_40_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * completes. */ - lpc17_40_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; errout_with_asynch: @@ -3427,8 +3375,8 @@ static int lpc17_40_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, lpc17_40_free_xfrinfo(xfrinfo); ed->xfrinfo = NULL; -errout_with_sem: - lpc17_40_givesem(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -3551,7 +3499,7 @@ static int lpc17_40_cancel(struct usbhost_driver_s *drvr, /* Wake up the waiting thread */ - lpc17_40_givesem(&ed->wdhsem); + nxsem_post(&ed->wdhsem); xfrinfo->wdhwait = false; /* And free the transfer structure */ @@ -3628,7 +3576,7 @@ static int lpc17_40_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - lpc17_40_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -3795,10 +3743,10 @@ struct usbhost_connection_s *lpc17_40_usbhost_initialize(int controller) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not * have priority inheritance enabled. diff --git a/arch/arm/src/lpc2378/lpc23xx_i2c.c b/arch/arm/src/lpc2378/lpc23xx_i2c.c index 025135b79c684..87a0a1f686e3d 100644 --- a/arch/arm/src/lpc2378/lpc23xx_i2c.c +++ b/arch/arm/src/lpc2378/lpc23xx_i2c.c @@ -65,6 +65,7 @@ #include #include +#include #include #include @@ -114,7 +115,7 @@ struct lpc2378_i2cdev_s unsigned int base; /* Base address of registers */ uint16_t irqid; /* IRQ for this device */ - sem_t mutex; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t wait; /* Place to wait for state machine completion */ volatile uint8_t state; /* State of state machine */ struct wdog_s timeout; /* Watchdog to timeout when bus hung */ @@ -404,7 +405,7 @@ static int lpc2378_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - nxsem_wait(&priv->mutex); + nxmutex_lock(&priv->lock); /* Set up for the transfer */ @@ -425,7 +426,7 @@ static int lpc2378_i2c_transfer(struct i2c_master_s *dev, ret = lpc2378_i2c_start(priv); - nxsem_post(&priv->mutex); + nxmutex_unlock(&priv->lock); return ret; } @@ -579,9 +580,9 @@ struct i2c_master_s *lpc2378_i2cbus_initialize(int port) putreg32(I2C_CONSET_I2EN, priv->base + I2C_CONSET_OFFSET); - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->mutex, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->wait, 0, 0); /* The wait semaphore is used for signaling and, hence, should not have @@ -622,7 +623,7 @@ int lpc2378_i2cbus_uninitialize(struct i2c_master_s * dev) /* Reset data structures */ - nxsem_destroy(&priv->mutex); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->wait); /* Cancel the watchdog timer */ diff --git a/arch/arm/src/lpc2378/lpc23xx_spi.c b/arch/arm/src/lpc2378/lpc23xx_spi.c index 63abd01f3ff78..d31abe09cd7e1 100644 --- a/arch/arm/src/lpc2378/lpc23xx_spi.c +++ b/arch/arm/src/lpc2378/lpc23xx_spi.c @@ -54,7 +54,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -103,7 +103,7 @@ struct lpc23xx_spidev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (8 to 16) */ @@ -199,11 +199,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -589,9 +589,9 @@ struct spi_dev_s *lpc23_spibus_initialize(int port) spi_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); return &priv->spidev; } diff --git a/arch/arm/src/lpc31xx/lpc31_ehci.c b/arch/arm/src/lpc31xx/lpc31_ehci.c index 14dacb7d388d8..b937b2381c8c8 100644 --- a/arch/arm/src/lpc31xx/lpc31_ehci.c +++ b/arch/arm/src/lpc31xx/lpc31_ehci.c @@ -277,7 +277,7 @@ struct lpc31_ehci_s { volatile bool pscwait; /* TRUE: Thread is waiting for port status change event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for port status change events */ struct lpc31_epinfo_s ep0; /* Endpoint 0 */ @@ -418,12 +418,6 @@ static inline void lpc31_putreg(uint32_t regval, volatile uint32_t *regaddr); static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, unsigned int delay); -/* Semaphores ***************************************************************/ - -static int lpc31_takesem(sem_t *sem); -static int lpc31_takesem_noncancelable(sem_t *sem); -#define lpc31_givesem(s) nxsem_post(s); - /* Allocators ***************************************************************/ static struct lpc31_qh_s *lpc31_qh_alloc(void); @@ -1052,61 +1046,13 @@ static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, return (regval == donebits) ? OK : -ETIMEDOUT; } -/**************************************************************************** - * Name: lpc31_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int lpc31_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: lpc31_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int lpc31_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: lpc31_qh_alloc * * Description: * Allocate a Queue Head (QH) structure by removing it from the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1132,7 +1078,7 @@ static struct lpc31_qh_s *lpc31_qh_alloc(void) * Description: * Free a Queue Head (QH) structure by returning it to the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1153,7 +1099,7 @@ static void lpc31_qh_free(struct lpc31_qh_s *qh) * Allocate a Queue Element Transfer Descriptor (qTD) by removing it from * the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1180,7 +1126,7 @@ static struct lpc31_qtd_s *lpc31_qtd_alloc(void) * Free a Queue Element Transfer Descriptor (qTD) by returning it to the * free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1629,7 +1575,7 @@ static inline uint8_t lpc31_ehci_speed(uint8_t usbspeed) * this to minimize race conditions. This logic would have to be expanded * if we want to have more than one packet in flight at a time! * - * Assumption: The caller holds tex EHCI exclsem + * Assumption: The caller holds tex EHCI lock * ****************************************************************************/ @@ -1675,7 +1621,7 @@ static int lpc31_ioc_setup(struct lpc31_rhport_s *rhport, * Description: * Wait for the IOC event. * - * Assumption: The caller does *NOT* hold the EHCI exclsem. That would + * Assumption: The caller does *NOT* hold the EHCI lock. That would * cause a deadlock when the bottom-half, worker thread needs to take the * semaphore. * @@ -1691,7 +1637,7 @@ static int lpc31_ioc_wait(struct lpc31_epinfo_s *epinfo) while (epinfo->iocwait) { - ret = lpc31_takesem(&epinfo->iocsem); + ret = nxsem_wait_uninterruptible(&epinfo->iocsem); if (ret < 0) { break; @@ -1707,7 +1653,7 @@ static int lpc31_ioc_wait(struct lpc31_epinfo_s *epinfo) * Description: * Add a new, ready-to-go QH w/attached qTDs to the asynchronous queue. * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -2153,7 +2099,7 @@ static struct lpc31_qtd_s *lpc31_qtd_statusphase(uint32_t tokenbits) * This is a blocking function; it will not return until the control * transfer has completed. * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2437,7 +2383,7 @@ static int lpc31_async_setup(struct lpc31_rhport_s *rhport, * frame list), followed by shorter poll rates, with queue heads with a * poll rate of one, on the very end." * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2541,8 +2487,8 @@ static int lpc31_intr_setup(struct lpc31_rhport_s *rhport, * Description: * Wait for an IN or OUT transfer to complete. * - * Assumption: The caller holds the EHCI exclsem. The caller must be aware - * that the EHCI exclsem will released while waiting for the transfer to + * Assumption: The caller holds the EHCI lock. The caller must be aware + * that the EHCI lock will released while waiting for the transfer to * complete, but will be re-acquired when before returning. The state of * EHCI resources could be very different upon return. * @@ -2560,27 +2506,27 @@ static ssize_t lpc31_transfer_wait(struct lpc31_epinfo_s *epinfo) int ret; int ret2; - /* Release the EHCI semaphore while we wait. Other threads need the + /* Release the EHCI mutex while we wait. Other threads need the * opportunity to access the EHCI resources while we wait. * * REVISIT: Is this safe? NO. This is a bug and needs rethinking. * We need to lock all of the port-resources (not EHCI common) until - * the transfer is complete. But we can't use the common EHCI exclsem + * the transfer is complete. But we can't use the common EHCI lock * or we will deadlock while waiting (because the working thread that - * wakes this thread up needs the exclsem). + * wakes this thread up needs the lock). */ #warning REVISIT - lpc31_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Wait for the IOC completion event */ ret = lpc31_ioc_wait(epinfo); - /* Re-acquire the EHCI semaphore. The caller expects to be holding + /* Re-acquire the EHCI mutex. The caller expects to be holding * this upon return. */ - ret2 = lpc31_takesem_noncancelable(&g_ehci.exclsem); + ret2 = nxmutex_lock(&g_ehci.lock); if (ret2 < 0) { ret = ret2; @@ -2604,9 +2550,7 @@ static ssize_t lpc31_transfer_wait(struct lpc31_epinfo_s *epinfo) } #endif - /* Did lpc31_ioc_wait() or lpc31_takesem_noncancelable() report an - * error? - */ + /* Did lpc31_ioc_wait() or nxmutex_lock() report an error? */ if (ret < 0) { @@ -2913,7 +2857,7 @@ static int lpc31_qh_ioccheck(struct lpc31_qh_s *qh, uint32_t **bp, void *arg) { /* Yes... wake it up */ - lpc31_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); epinfo->iocwait = 0; } @@ -3073,7 +3017,7 @@ static int lpc31_qh_cancel(struct lpc31_qh_s *qh, uint32_t **bp, void *arg) * detected (actual number of bytes received was less than the expected * number of bytes)." * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -3209,7 +3153,7 @@ static inline void lpc31_portsc_bottomhalf(void) if (g_ehci.pscwait) { - lpc31_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3249,7 +3193,7 @@ static inline void lpc31_portsc_bottomhalf(void) if (g_ehci.pscwait) { - lpc31_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3327,7 +3271,7 @@ static void lpc31_ehci_bottomhalf(void *arg) * real option (other than to reschedule and delay). */ - lpc31_takesem_noncancelable(&g_ehci.exclsem); + nxmutex_lock(&g_ehci.lock); /* Handle all unmasked interrupt sources */ @@ -3437,7 +3381,7 @@ static void lpc31_ehci_bottomhalf(void *arg) /* We are done with the EHCI structures */ - lpc31_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Re-enable relevant EHCI interrupts. Interrupts should still be enabled * at the level of the interrupt controller. @@ -3596,7 +3540,7 @@ static int lpc31_wait(struct usbhost_connection_s *conn, */ g_ehci.pscwait = true; - ret = lpc31_takesem(&g_ehci.pscsem); + ret = nxsem_wait_uninterruptible(&g_ehci.pscsem); if (ret < 0) { return ret; @@ -3955,7 +3899,7 @@ static int lpc31_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the EHCI data structures. */ - ret = lpc31_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret >= 0) { /* Remember the new device address and max packet size */ @@ -3964,7 +3908,7 @@ static int lpc31_ep0configure(struct usbhost_driver_s *drvr, epinfo->speed = speed; epinfo->maxpacket = maxpacketsize; - lpc31_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); } return ret; @@ -4328,7 +4272,7 @@ static int lpc31_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * structures. */ - ret = lpc31_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4342,7 +4286,7 @@ static int lpc31_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Now initiate the transfer */ @@ -4357,13 +4301,13 @@ static int lpc31_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* And wait for the transfer to complete */ nbytes = lpc31_transfer_wait(ep0info); - lpc31_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes >= 0 ? OK : (int)nbytes; errout_with_iocwait: ep0info->iocwait = false; -errout_with_sem: - lpc31_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4432,7 +4376,7 @@ static ssize_t lpc31_transfer(struct usbhost_driver_s *drvr, * structures. */ - ret = lpc31_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return (ssize_t)ret; @@ -4446,7 +4390,7 @@ static ssize_t lpc31_transfer(struct usbhost_driver_s *drvr, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4484,13 +4428,13 @@ static ssize_t lpc31_transfer(struct usbhost_driver_s *drvr, /* Then wait for the transfer to complete */ nbytes = lpc31_transfer_wait(epinfo); - lpc31_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes; errout_with_iocwait: epinfo->iocwait = false; -errout_with_sem: - lpc31_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return (ssize_t)ret; } @@ -4545,7 +4489,7 @@ static int lpc31_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * structures. */ - ret = lpc31_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4559,7 +4503,7 @@ static int lpc31_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4596,14 +4540,14 @@ static int lpc31_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* The transfer is in progress */ - lpc31_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return OK; errout_with_callback: epinfo->callback = NULL; epinfo->arg = NULL; -errout_with_sem: - lpc31_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4651,7 +4595,7 @@ static int lpc31_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * interrupt level. */ - ret = lpc31_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4694,7 +4638,7 @@ static int lpc31_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #endif { ret = OK; - goto errout_with_sem; + goto errout_with_lock; } /* Handle the cancellation according to the type of the transfer */ @@ -4757,7 +4701,7 @@ static int lpc31_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) default: usbhost_trace1(EHCI_TRACE1_BADXFRTYPE, epinfo->xfrtype); ret = -ENOSYS; - goto errout_with_sem; + goto errout_with_lock; } /* Find and remove the QH. There are four possibilities: @@ -4787,7 +4731,7 @@ static int lpc31_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Yes... wake it up */ DEBUGASSERT(callback == NULL); - lpc31_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); } /* No.. Is there a pending asynchronous transfer? */ @@ -4802,11 +4746,11 @@ static int lpc31_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #else /* Wake up the waiting thread */ - sam_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); #endif -errout_with_sem: - lpc31_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4853,7 +4797,7 @@ static int lpc31_connect(struct usbhost_driver_s *drvr, if (g_ehci.pscwait) { g_ehci.pscwait = false; - lpc31_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); } leave_critical_section(flags); @@ -5079,7 +5023,7 @@ struct usbhost_connection_s *lpc31_ehci_initialize(int controller) /* Initialize the EHCI state data structure */ - nxsem_init(&g_ehci.exclsem, 0, 1); + nxmutex_init(&g_ehci.lock); nxsem_init(&g_ehci.pscsem, 0, 0); /* The pscsem semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/lpc31xx/lpc31_i2c.c b/arch/arm/src/lpc31xx/lpc31_i2c.c index 9fb22b1f363a9..44462b1b7d882 100644 --- a/arch/arm/src/lpc31xx/lpc31_i2c.c +++ b/arch/arm/src/lpc31xx/lpc31_i2c.c @@ -35,6 +35,7 @@ #include #include +#include #include #include @@ -69,7 +70,7 @@ struct lpc31_i2cdev_s uint16_t rstid; /* Reset for this device */ uint16_t irqid; /* IRQ for this device */ - sem_t mutex; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t wait; /* Place to wait for state machine completion */ volatile uint8_t state; /* State of state machine */ struct wdog_s timeout; /* Watchdog to timeout when bus hung */ @@ -473,7 +474,7 @@ static int i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - nxsem_wait(&priv->mutex); + nxmutex_lock(&priv->lock); flags = enter_critical_section(); /* Set up for the transfer */ @@ -509,7 +510,7 @@ static int i2c_transfer(struct i2c_master_s *dev, ret = count - priv->nmsg; leave_critical_section(flags); - nxsem_post(&priv->mutex); + nxmutex_unlock(&priv->lock); return ret; } @@ -555,9 +556,9 @@ struct i2c_master_s *lpc31_i2cbus_initialize(int port) priv->rstid = (port == 0) ? RESETID_I2C0RST : RESETID_I2C1RST; priv->irqid = (port == 0) ? LPC31_IRQ_I2C0 : LPC31_IRQ_I2C1; - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->mutex, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->wait, 0, 0); /* The wait semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/lpc31xx/lpc31_spi.c b/arch/arm/src/lpc31xx/lpc31_spi.c index 2aa2dcfa19384..74df6b7655b5c 100644 --- a/arch/arm/src/lpc31xx/lpc31_spi.c +++ b/arch/arm/src/lpc31xx/lpc31_spi.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include @@ -72,7 +72,7 @@ struct lpc31_spidev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of work in bits (8 or 16) */ @@ -457,11 +457,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -964,9 +964,9 @@ struct spi_dev_s *lpc31_spibus_initialize(int port) lpc31_softreset(RESETID_SPIRSTAPB); lpc31_softreset(RESETID_SPIRSTIP); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Reset the SPI block */ diff --git a/arch/arm/src/lpc43xx/lpc43_ehci.c b/arch/arm/src/lpc43xx/lpc43_ehci.c index ab8a8375aa191..7c7b4b732854f 100644 --- a/arch/arm/src/lpc43xx/lpc43_ehci.c +++ b/arch/arm/src/lpc43xx/lpc43_ehci.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -278,7 +279,7 @@ struct lpc43_ehci_s { volatile bool pscwait; /* TRUE: Thread is waiting for port status change event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for port status change events */ struct lpc43_epinfo_s ep0; /* Endpoint 0 */ @@ -419,12 +420,6 @@ static inline void lpc43_putreg(uint32_t regval, volatile uint32_t *regaddr); static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, unsigned int delay); -/* Semaphores ***************************************************************/ - -static int lpc43_takesem(sem_t *sem); -static int lpc43_takesem_noncancelable(sem_t *sem); -#define lpc43_givesem(s) nxsem_post(s); - /* Allocators ***************************************************************/ static struct lpc43_qh_s *lpc43_qh_alloc(void); @@ -1041,61 +1036,13 @@ static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, return (regval == donebits) ? OK : -ETIMEDOUT; } -/**************************************************************************** - * Name: lpc43_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int lpc43_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: lpc43_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int lpc43_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: lpc43_qh_alloc * * Description: * Allocate a Queue Head (QH) structure by removing it from the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1121,7 +1068,7 @@ static struct lpc43_qh_s *lpc43_qh_alloc(void) * Description: * Free a Queue Head (QH) structure by returning it to the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1142,7 +1089,7 @@ static void lpc43_qh_free(struct lpc43_qh_s *qh) * Allocate a Queue Element Transfer Descriptor (qTD) by removing it from * the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1169,7 +1116,7 @@ static struct lpc43_qtd_s *lpc43_qtd_alloc(void) * Free a Queue Element Transfer Descriptor (qTD) by returning it to the * free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1527,7 +1474,7 @@ static inline uint8_t lpc43_ehci_speed(uint8_t usbspeed) * this to minimize race conditions. This logic would have to be expanded * if we want to have more than one packet in flight at a time! * - * Assumption: The caller holds tex EHCI exclsem + * Assumption: The caller holds tex EHCI lock * ****************************************************************************/ @@ -1573,7 +1520,7 @@ static int lpc43_ioc_setup(struct lpc43_rhport_s *rhport, * Description: * Wait for the IOC event. * - * Assumption: The caller does *NOT* hold the EHCI exclsem. That would + * Assumption: The caller does *NOT* hold the EHCI lock. That would * cause a deadlock when the bottom-half, worker thread needs to take the * semaphore. * @@ -1589,7 +1536,7 @@ static int lpc43_ioc_wait(struct lpc43_epinfo_s *epinfo) while (epinfo->iocwait) { - ret = lpc43_takesem(&epinfo->iocsem); + ret = nxsem_wait_uninterruptible(&epinfo->iocsem); if (ret < 0) { break; @@ -1605,7 +1552,7 @@ static int lpc43_ioc_wait(struct lpc43_epinfo_s *epinfo) * Description: * Add a new, ready-to-go QH w/attached qTDs to the asynchronous queue. * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -2040,7 +1987,7 @@ static struct lpc43_qtd_s *lpc43_qtd_statusphase(uint32_t tokenbits) * This is a blocking function; it will not return until the control * transfer has completed. * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2323,7 +2270,7 @@ static int lpc43_async_setup(struct lpc43_rhport_s *rhport, * frame list), followed by shorter poll rates, with queue heads with a * poll rate of one, on the very end." * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2427,8 +2374,8 @@ static int lpc43_intr_setup(struct lpc43_rhport_s *rhport, * Description: * Wait for an IN or OUT transfer to complete. * - * Assumption: The caller holds the EHCI exclsem. The caller must be aware - * that the EHCI exclsem will released while waiting for the transfer to + * Assumption: The caller holds the EHCI lock. The caller must be aware + * that the EHCI lock will released while waiting for the transfer to * complete, but will be re-acquired when before returning. The state of * EHCI resources could be very different upon return. * @@ -2446,35 +2393,33 @@ static ssize_t lpc43_transfer_wait(struct lpc43_epinfo_s *epinfo) int ret; int ret2; - /* Release the EHCI semaphore while we wait. Other threads need the + /* Release the EHCI mutex while we wait. Other threads need the * opportunity to access the EHCI resources while we wait. * * REVISIT: Is this safe? NO. This is a bug and needs rethinking. * We need to lock all of the port-resources (not EHCI common) until - * the transfer is complete. But we can't use the common EHCI exclsem + * the transfer is complete. But we can't use the common EHCI lock * or we will deadlock while waiting (because the working thread that - * wakes this thread up needs the exclsem). + * wakes this thread up needs the lock). */ #warning REVISIT - lpc43_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Wait for the IOC completion event */ ret = lpc43_ioc_wait(epinfo); - /* Re-acquire the EHCI semaphore. The caller expects to be holding + /* Re-acquire the EHCI mutex. The caller expects to be holding * this upon return. */ - ret2 = lpc43_takesem_noncancelable(&g_ehci.exclsem); + ret2 = nxmutex_lock(&g_ehci.lock); if (ret2 < 0) { ret = ret2; } - /* Did lpc43_ioc_wait() or lpc43_takesem_noncancelable()report an - * error? - */ + /* Did lpc43_ioc_wait() or nxmutex_lock()report an error? */ if (ret < 0) { @@ -2772,7 +2717,7 @@ static int lpc43_qh_ioccheck(struct lpc43_qh_s *qh, uint32_t **bp, void *arg) { /* Yes... wake it up */ - lpc43_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); epinfo->iocwait = 0; } @@ -2923,7 +2868,7 @@ static int lpc43_qh_cancel(struct lpc43_qh_s *qh, uint32_t **bp, void *arg) * detected (actual number of bytes received was less than the expected * number of bytes)." * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -3046,7 +2991,7 @@ static inline void lpc43_portsc_bottomhalf(void) if (g_ehci.pscwait) { - lpc43_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3086,7 +3031,7 @@ static inline void lpc43_portsc_bottomhalf(void) if (g_ehci.pscwait) { - lpc43_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3164,7 +3109,7 @@ static void lpc43_ehci_bottomhalf(void *arg) * real option (other than to reschedule and delay). */ - lpc43_takesem_noncancelable(&g_ehci.exclsem); + nxmutex_lock(&g_ehci.lock); /* Handle all unmasked interrupt sources */ @@ -3274,7 +3219,7 @@ static void lpc43_ehci_bottomhalf(void *arg) /* We are done with the EHCI structures */ - lpc43_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Re-enable relevant EHCI interrupts. Interrupts should still be enabled * at the level of the interrupt controller. @@ -3433,7 +3378,7 @@ static int lpc43_wait(struct usbhost_connection_s *conn, */ g_ehci.pscwait = true; - ret = lpc43_takesem(&g_ehci.pscsem); + ret = nxsem_wait_uninterruptible(&g_ehci.pscsem); if (ret < 0) { return ret; @@ -3792,7 +3737,7 @@ static int lpc43_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the EHCI data structures. */ - ret = lpc43_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret >= 0) { /* Remember the new device address and max packet size */ @@ -3801,7 +3746,7 @@ static int lpc43_ep0configure(struct usbhost_driver_s *drvr, epinfo->speed = speed; epinfo->maxpacket = maxpacketsize; - lpc43_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); } return ret; @@ -4158,7 +4103,7 @@ static int lpc43_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * structures. */ - ret = lpc43_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4170,7 +4115,7 @@ static int lpc43_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Now initiate the transfer */ @@ -4185,13 +4130,13 @@ static int lpc43_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* And wait for the transfer to complete */ nbytes = lpc43_transfer_wait(ep0info); - lpc43_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes >= 0 ? OK : (int)nbytes; errout_with_iocwait: ep0info->iocwait = false; -errout_with_sem: - lpc43_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4260,7 +4205,7 @@ static ssize_t lpc43_transfer(struct usbhost_driver_s *drvr, * data structures. */ - ret = lpc43_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return (ssize_t)ret; @@ -4272,7 +4217,7 @@ static ssize_t lpc43_transfer(struct usbhost_driver_s *drvr, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4310,13 +4255,13 @@ static ssize_t lpc43_transfer(struct usbhost_driver_s *drvr, /* Then wait for the transfer to complete */ nbytes = lpc43_transfer_wait(epinfo); - lpc43_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes; errout_with_iocwait: epinfo->iocwait = false; -errout_with_sem: - lpc43_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return (ssize_t)ret; } @@ -4371,7 +4316,7 @@ static int lpc43_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * data structures. */ - ret = lpc43_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4383,7 +4328,7 @@ static int lpc43_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4420,14 +4365,14 @@ static int lpc43_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* The transfer is in progress */ - lpc43_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return OK; errout_with_callback: epinfo->callback = NULL; epinfo->arg = NULL; -errout_with_sem: - lpc43_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4475,7 +4420,7 @@ static int lpc43_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * interrupt level. */ - ret = lpc43_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4518,7 +4463,7 @@ static int lpc43_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #endif { ret = OK; - goto errout_with_sem; + goto errout_with_lock; } /* Handle the cancellation according to the type of the transfer */ @@ -4581,7 +4526,7 @@ static int lpc43_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) default: usbhost_trace1(EHCI_TRACE1_BADXFRTYPE, epinfo->xfrtype); ret = -ENOSYS; - goto errout_with_sem; + goto errout_with_lock; } /* Find and remove the QH. There are four possibilities: @@ -4611,7 +4556,7 @@ static int lpc43_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Yes... wake it up */ DEBUGASSERT(callback == NULL); - lpc43_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); } /* No.. Is there a pending asynchronous transfer? */ @@ -4626,11 +4571,11 @@ static int lpc43_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #else /* Wake up the waiting thread */ - lpc43_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); #endif -errout_with_sem: - lpc43_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4677,7 +4622,7 @@ static int lpc43_connect(struct usbhost_driver_s *drvr, if (g_ehci.pscwait) { g_ehci.pscwait = false; - lpc43_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); } leave_critical_section(flags); @@ -4903,7 +4848,7 @@ struct usbhost_connection_s *lpc43_ehci_initialize(int controller) /* Initialize the EHCI state data structure */ - nxsem_init(&g_ehci.exclsem, 0, 1); + nxmutex_init(&g_ehci.lock); nxsem_init(&g_ehci.pscsem, 0, 0); /* The pscsem semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/lpc43xx/lpc43_gpdma.c b/arch/arm/src/lpc43xx/lpc43_gpdma.c index cb6cd3df40b15..201e68c4b28fd 100644 --- a/arch/arm/src/lpc43xx/lpc43_gpdma.c +++ b/arch/arm/src/lpc43xx/lpc43_gpdma.c @@ -32,6 +32,7 @@ #include #include +#include #include "arm_internal.h" #include "chip.h" @@ -66,7 +67,7 @@ struct lpc43_dmach_s struct lpc43_gpdma_s { - sem_t exclsem; /* For exclusive access to the DMA channel list */ + mutex_t lock; /* For exclusive access to the DMA channel list */ /* This is the state of each DMA channel */ @@ -290,7 +291,7 @@ void weak_function arm_dma_initialize(void) /* Initialize the DMA state structure */ - nxsem_init(&g_gpdma.exclsem, 0, 1); + nxmutex_init(&g_gpdma.lock); for (i = 0; i < LPC43_NDMACH; i++) { @@ -379,7 +380,7 @@ DMA_HANDLE lpc43_dmachannel(void) /* Get exclusive access to the GPDMA state structure */ - ret = nxsem_wait_uninterruptible(&g_gpdma.exclsem); + ret = nxmutex_lock(&g_gpdma.lock); if (ret < 0) { return NULL; @@ -401,7 +402,7 @@ DMA_HANDLE lpc43_dmachannel(void) /* Return what we found (or not) */ - nxsem_post(&g_gpdma.exclsem); + nxmutex_unlock(&g_gpdma.lock); return (DMA_HANDLE)dmach; } diff --git a/arch/arm/src/lpc43xx/lpc43_i2c.c b/arch/arm/src/lpc43xx/lpc43_i2c.c index c77b0cdddf3e4..953bca87058cf 100644 --- a/arch/arm/src/lpc43xx/lpc43_i2c.c +++ b/arch/arm/src/lpc43xx/lpc43_i2c.c @@ -63,6 +63,7 @@ #include #include +#include #include #include @@ -103,7 +104,7 @@ struct lpc43_i2cdev_s uint16_t irqid; /* IRQ for this device */ uint32_t base_freq; /* branch frequency */ - sem_t mutex; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t wait; /* Place to wait for state machine completion */ volatile uint8_t state; /* State of state machine */ struct wdog_s timeout; /* watchdog to timeout when bus hung */ @@ -387,7 +388,7 @@ static int lpc43_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - nxsem_wait(&priv->mutex); + nxmutex_lock(&priv->lock); /* Set up for the transfer */ @@ -408,7 +409,7 @@ static int lpc43_i2c_transfer(struct i2c_master_s *dev, ret = lpc43_i2c_start(priv); - nxsem_post(&priv->mutex); + nxmutex_unlock(&priv->lock); return ret; } @@ -527,9 +528,9 @@ struct i2c_master_s *lpc43_i2cbus_initialize(int port) putreg32(I2C_CONSET_I2EN, priv->base + LPC43_I2C_CONSET_OFFSET); - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->mutex, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->wait, 0, 0); /* The wait semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/lpc43xx/lpc43_sdmmc.c b/arch/arm/src/lpc43xx/lpc43_sdmmc.c index aba9a5f08d549..e387026f123ca 100644 --- a/arch/arm/src/lpc43xx/lpc43_sdmmc.c +++ b/arch/arm/src/lpc43xx/lpc43_sdmmc.c @@ -251,8 +251,6 @@ static void lpc43_putreg(uint32_t val, uint32_t addr); /* Low-level helpers ********************************************************/ -static int lpc43_takesem(struct lpc43_dev_s *priv); -#define lpc43_givesem(priv) (nxsem_post(&priv->waitsem)) static inline void lpc43_setclock(uint32_t clkdiv); static inline void lpc43_sdcard_clock(bool enable); static int lpc43_ciu_sendcmd(uint32_t cmd, uint32_t arg); @@ -493,27 +491,6 @@ static void lpc43_putreg(uint32_t val, uint32_t addr) } #endif -/**************************************************************************** - * Name: lpc43_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SD card device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int lpc43_takesem(struct lpc43_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: lpc43_setclock * @@ -869,7 +846,7 @@ static void lpc43_endwait(struct lpc43_dev_s *priv, /* Wake up the waiting thread */ - lpc43_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2308,7 +2285,7 @@ static sdio_eventset_t lpc43_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = lpc43_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog -- assuming it was started and diff --git a/arch/arm/src/lpc43xx/lpc43_spi.c b/arch/arm/src/lpc43xx/lpc43_spi.c index da099eababc3d..35095c290d0e7 100644 --- a/arch/arm/src/lpc43xx/lpc43_spi.c +++ b/arch/arm/src/lpc43xx/lpc43_spi.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -73,7 +73,7 @@ struct lpc43_spidev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (8 to 16) */ @@ -171,11 +171,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -533,9 +533,9 @@ static struct spi_dev_s *lpc43_spiport_initialize(int port) spi_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); return &priv->spidev; } #endif /* CONFIG_LPC43_SPI */ diff --git a/arch/arm/src/lpc43xx/lpc43_ssp.c b/arch/arm/src/lpc43xx/lpc43_ssp.c index 3d630b3355611..90e0c88f1f27e 100644 --- a/arch/arm/src/lpc43xx/lpc43_ssp.c +++ b/arch/arm/src/lpc43xx/lpc43_ssp.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -63,7 +63,7 @@ struct lpc43_sspdev_s #ifdef CONFIG_LPC43_SSP_INTERRUPTS uint8_t sspirq; /* SPI IRQ number */ #endif - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 to 16) */ @@ -274,11 +274,11 @@ static int ssp_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -827,9 +827,9 @@ struct spi_dev_s *lpc43_sspbus_initialize(int port) ssp_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Enable the SPI */ diff --git a/arch/arm/src/lpc54xx/lpc54_dma.c b/arch/arm/src/lpc54xx/lpc54_dma.c index 50b1e4fbc0bab..79658d39c4ee8 100644 --- a/arch/arm/src/lpc54xx/lpc54_dma.c +++ b/arch/arm/src/lpc54xx/lpc54_dma.c @@ -32,6 +32,7 @@ #include #include +#include #include "arm_internal.h" #include "hardware/lpc54_inputmux.h" @@ -59,7 +60,7 @@ struct lpc54_dmach_s struct lpc54_dma_s { - sem_t exclsem; /* For exclusive access to the DMA channel list */ + mutex_t lock; /* For exclusive access to the DMA channel list */ /* This is the state of each DMA channel */ @@ -233,7 +234,7 @@ void weak_function arm_dma_initialize(void) /* Initialize the DMA state structure */ - nxsem_init(&g_dma.exclsem, 0, 1); + nxmutex_init(&g_dma.lock); /* Set the SRAMBASE to the beginning a array of DMA descriptors, one for * each DMA channel. @@ -295,7 +296,7 @@ int lpc54_dma_setup(int ch, uint32_t cfg, uint32_t xfrcfg, uint8_t trigsrc, /* Get exclusive access to the DMA data structures and interface */ - ret = nxsem_wait(&g_dma.exclsem); + ret = nxmutex_lock(&g_dma.lock); if (ret < 0) { return ret; @@ -307,7 +308,7 @@ int lpc54_dma_setup(int ch, uint32_t cfg, uint32_t xfrcfg, uint8_t trigsrc, if (dmach->inuse) { ret = -EBUSY; - goto errout_with_exclsem; + goto errout_with_excllock; } dmach->inuse = true; @@ -437,8 +438,8 @@ int lpc54_dma_setup(int ch, uint32_t cfg, uint32_t xfrcfg, uint8_t trigsrc, putreg32(xfrcfg, base + LPC54_DMA_XFERCFG_OFFSET); ret = OK; -errout_with_exclsem: - nxsem_post(&g_dma.exclsem); +errout_with_excllock: + nxmutex_unlock(&g_dma.lock); return ret; } diff --git a/arch/arm/src/lpc54xx/lpc54_i2c_master.c b/arch/arm/src/lpc54xx/lpc54_i2c_master.c index b33366af37d16..ecb435ebc9ea3 100644 --- a/arch/arm/src/lpc54xx/lpc54_i2c_master.c +++ b/arch/arm/src/lpc54xx/lpc54_i2c_master.c @@ -58,6 +58,7 @@ #include #include #include +#include #include #include @@ -136,7 +137,7 @@ struct lpc54_i2cdev_s int16_t nmsgs; /* Number of transfer remaining */ int16_t result; /* The result of the transfer */ - sem_t exclsem; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ #ifndef CONFIG_I2C_POLLED sem_t waitsem; /* Supports wait for state machine completion */ uint16_t irq; /* Flexcomm IRQ number */ @@ -756,7 +757,7 @@ static int lpc54_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - nxsem_wait(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Set up for the transfer */ @@ -789,7 +790,7 @@ static int lpc54_i2c_transfer(struct i2c_master_s *dev, ret = priv->result; i2cinfo("Done, result=%d\n", ret); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1206,9 +1207,9 @@ struct i2c_master_s *lpc54_i2cbus_initialize(int port) lpc54_i2c_setfrequency(priv, I2C_DEFAULT_FREQUENCY); - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifndef CONFIG_I2C_POLLED nxsem_init(&priv->waitsem, 0, 0); diff --git a/arch/arm/src/lpc54xx/lpc54_rng.c b/arch/arm/src/lpc54xx/lpc54_rng.c index d83a634773d54..65ff4fa2fce59 100644 --- a/arch/arm/src/lpc54xx/lpc54_rng.c +++ b/arch/arm/src/lpc54xx/lpc54_rng.c @@ -27,7 +27,7 @@ #include #include -#include +#include #include #include @@ -48,7 +48,7 @@ static ssize_t lpc54_read(struct file *filep, char *buffer, size_t); struct rng_dev_s { - sem_t rd_devsem; /* Threads can only exclusively access the RNG */ + mutex_t rd_devlock; /* Threads can only exclusively access the RNG */ }; /**************************************************************************** @@ -93,7 +93,7 @@ static ssize_t lpc54_read(struct file *filep, char *buffer, size_t buflen) /* Get exclusive access to ROM random number generator API */ - ret = nxsem_wait(&g_rngdev.rd_devsem); + ret = nxmutex_lock(&g_rngdev.rd_devlock); if (ret < 0) { return ret; @@ -119,7 +119,7 @@ static ssize_t lpc54_read(struct file *filep, char *buffer, size_t buflen) } } - nxsem_post(&g_rngdev.rd_devsem); + nxmutex_unlock(&g_rngdev.rd_devlock); return buflen; } @@ -145,7 +145,7 @@ static ssize_t lpc54_read(struct file *filep, char *buffer, size_t buflen) #ifdef CONFIG_DEV_RANDOM void devrandom_register(void) { - nxsem_init(&g_rngdev.rd_devsem, 0, 1); + nxmutex_init(&g_rngdev.rd_devlock); register_driver("/dev/random", &g_rngops, 0444, NULL); } #endif @@ -168,7 +168,7 @@ void devrandom_register(void) void devurandom_register(void) { #ifndef CONFIG_DEV_RANDOM - nxsem_init(&g_rngdev.rd_devsem, 0, 1); + nxmutex_init(&g_rngdev.rd_devlock); #endif register_driver("/dev/urandom", &g_rngops, 0444, NULL); } diff --git a/arch/arm/src/lpc54xx/lpc54_rtc_lowerhalf.c b/arch/arm/src/lpc54xx/lpc54_rtc_lowerhalf.c index fa0c78896b663..b8c4bfc9a0a39 100644 --- a/arch/arm/src/lpc54xx/lpc54_rtc_lowerhalf.c +++ b/arch/arm/src/lpc54xx/lpc54_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -67,7 +68,7 @@ struct lpc54_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -307,7 +308,7 @@ static int lpc54_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->id == 0); priv = (struct lpc54_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -339,7 +340,7 @@ static int lpc54_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -556,17 +557,16 @@ static int lpc54_setperiodic(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL); priv = (struct lpc54_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } memcpy(&priv->periodic, alarminfo, sizeof(struct lower_setperiodic_s)); - ret = lpc54_rtc_setperiodic(&alarminfo->period, lpc54_periodic_callback); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -598,7 +598,7 @@ static int lpc54_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) DEBUGASSERT(id == 0); - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -606,7 +606,7 @@ static int lpc54_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) ret = lpc54_rtc_cancelperiodic(); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -639,8 +639,7 @@ static int lpc54_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) struct rtc_lowerhalf_s *lpc54_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/arm/src/lpc54xx/lpc54_sdmmc.c b/arch/arm/src/lpc54xx/lpc54_sdmmc.c index 429e906c89ccd..30d5784d27b43 100644 --- a/arch/arm/src/lpc54xx/lpc54_sdmmc.c +++ b/arch/arm/src/lpc54xx/lpc54_sdmmc.c @@ -251,8 +251,6 @@ static void lpc54_putreg(uint32_t val, uint32_t addr); /* Low-level helpers ********************************************************/ -static int lpc54_takesem(struct lpc54_dev_s *priv); -#define lpc54_givesem(priv) (nxsem_post(&priv->waitsem)) static inline void lpc54_setclock(uint32_t clkdiv); static inline void lpc54_sdcard_clock(bool enable); static int lpc54_ciu_sendcmd(uint32_t cmd, uint32_t arg); @@ -493,27 +491,6 @@ static void lpc54_putreg(uint32_t val, uint32_t addr) } #endif -/**************************************************************************** - * Name: lpc54_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SD card device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int lpc54_takesem(struct lpc54_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: lpc54_setclock * @@ -865,7 +842,7 @@ static void lpc54_endwait(struct lpc54_dev_s *priv, /* Wake up the waiting thread */ - lpc54_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2304,7 +2281,7 @@ static sdio_eventset_t lpc54_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = lpc54_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/arm/src/lpc54xx/lpc54_spi_master.c b/arch/arm/src/lpc54xx/lpc54_spi_master.c index 139011b4fc284..e2d7f0dc56f5c 100644 --- a/arch/arm/src/lpc54xx/lpc54_spi_master.c +++ b/arch/arm/src/lpc54xx/lpc54_spi_master.c @@ -43,7 +43,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -82,7 +82,7 @@ struct lpc54_spidev_s { struct spi_dev_s dev; /* Externally visible part of the SPI interface */ uintptr_t base; /* Base address of Flexcomm registers */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t fclock; /* Flexcomm function clock frequency */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ @@ -1254,11 +1254,11 @@ static int lpc54_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2016,9 +2016,9 @@ struct spi_dev_s *lpc54_spibus_initialize(int port) priv->nbits = 8; priv->mode = SPIDEV_MODE0; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Configure master mode in mode 0: * diff --git a/arch/arm/src/lpc54xx/lpc54_usb0_ohci.c b/arch/arm/src/lpc54xx/lpc54_usb0_ohci.c index 5e9a04c7207c0..7327ad076a9df 100644 --- a/arch/arm/src/lpc54xx/lpc54_usb0_ohci.c +++ b/arch/arm/src/lpc54xx/lpc54_usb0_ohci.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include #include @@ -260,7 +260,7 @@ struct lpc54_usbhost_s uint8_t outinterval; /* Minimum periodic IN EP polling interval: 2, 4, 6, 16, or 32 */ #endif - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait Writeback Done Head event */ #ifdef CONFIG_OHCI_HUB @@ -366,12 +366,6 @@ static void lpc54_putreg(uint32_t val, uint32_t addr); # define lpc54_putreg(val,addr) putreg32(val,addr) #endif -/* Semaphores ***************************************************************/ - -static int lpc54_takesem(sem_t *sem); -static int lpc54_takesem_noncancelable(sem_t *sem); -#define lpc54_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t lpc54_getle16(const uint8_t *val); @@ -676,54 +670,6 @@ static void lpc54_putreg(uint32_t val, uint32_t addr) } #endif -/**************************************************************************** - * Name: lpc54_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int lpc54_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: lpc54_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int lpc54_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: lpc54_getle16 * @@ -1746,7 +1692,7 @@ static int lpc54_ctrltd(struct lpc54_usbhost_s *priv, struct lpc54_ed_s *ed, /* Wait for the Writeback Done Head interrupt */ - ret = lpc54_takesem(&ed->wdhsem); + ret = nxsem_wait_uninterruptible(&ed->wdhsem); if (ret < 0) { /* Task has been canceled */ @@ -1843,7 +1789,7 @@ static int lpc54_usbinterrupt(int irq, void *context, void *arg) if (priv->pscwait) { - lpc54_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -1902,7 +1848,7 @@ static int lpc54_usbinterrupt(int irq, void *context, void *arg) if (priv->pscwait) { - lpc54_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -2037,7 +1983,7 @@ static int lpc54_usbinterrupt(int irq, void *context, void *arg) { /* Wake up the thread waiting for the WDH event */ - lpc54_givesem(&ed->wdhsem); + nxsem_post(&ed->wdhsem); xfrinfo->wdhwait = false; } @@ -2162,7 +2108,7 @@ static int lpc54_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = lpc54_takesem(&priv->pscsem); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -2315,7 +2261,7 @@ static int lpc54_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to EP0 and the control list */ - ret = lpc54_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2333,8 +2279,7 @@ static int lpc54_ep0configure(struct usbhost_driver_s *drvr, } ed->hw.ctrl = hwctrl; - - lpc54_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); uinfo("EP0 CTRL:%08x\n", ed->hw.ctrl); return OK; @@ -2381,7 +2326,7 @@ static int lpc54_epalloc(struct usbhost_driver_s *drvr, * periodic list, and the interrupt table. */ - ret = lpc54_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2504,7 +2449,7 @@ static int lpc54_epalloc(struct usbhost_driver_s *drvr, } } - lpc54_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2543,7 +2488,7 @@ static int lpc54_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * periodic list and the interrupt table. */ - ret = lpc54_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2581,7 +2526,7 @@ static int lpc54_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Put the ED back into the free list */ lpc54_edfree(ed); - lpc54_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2627,7 +2572,7 @@ static int lpc54_alloc(struct usbhost_driver_s *drvr, /* We must have exclusive access to the transfer buffer pool */ - ret = lpc54_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2642,7 +2587,7 @@ static int lpc54_alloc(struct usbhost_driver_s *drvr, ret = OK; } - lpc54_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2679,9 +2624,9 @@ static int lpc54_free(struct usbhost_driver_s *drvr, uint8_t *buffer) /* We must have exclusive access to the transfer buffer pool */ - ret = lpc54_takesem_noncancelable(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); lpc54_tbfree(buffer); - lpc54_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2824,7 +2769,7 @@ static int lpc54_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to EP0 and the control list */ - ret = lpc54_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2846,7 +2791,7 @@ static int lpc54_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, } } - lpc54_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2867,7 +2812,7 @@ static int lpc54_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to EP0 and the control list */ - ret = lpc54_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2890,7 +2835,7 @@ static int lpc54_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, } } - lpc54_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -3162,7 +3107,7 @@ static ssize_t lpc54_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * table. */ - ret = lpc54_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -3179,7 +3124,7 @@ static ssize_t lpc54_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, { uerr("ERROR: lpc54_alloc_xfrinfo failed\n"); nbytes = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } /* Initialize the transfer structure */ @@ -3234,7 +3179,7 @@ static ssize_t lpc54_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* Wait for the Writeback Done Head interrupt */ - ret = lpc54_takesem(&ed->wdhsem); + ret = nxsem_wait_uninterruptible(&ed->wdhsem); if (ret < 0) { nbytes = (ssize_t)ret; @@ -3294,8 +3239,8 @@ static ssize_t lpc54_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, lpc54_free_xfrinfo(xfrinfo); ed->xfrinfo = NULL; -errout_with_sem: - lpc54_givesem(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); return nbytes; } @@ -3442,7 +3387,7 @@ static int lpc54_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * table. */ - ret lpc54_takesem(&priv->exclsem); + ret nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -3459,7 +3404,7 @@ static int lpc54_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, { uerr("ERROR: lpc54_alloc_xfrinfo failed\n"); ret = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } /* Initialize the transfer structure */ @@ -3479,7 +3424,7 @@ static int lpc54_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, if (ret < 0) { uerr("ERROR: lpc54_dma_alloc failed: %d\n", ret); - goto errout_with_sem; + goto errout_with_lock; } /* If a buffer was allocated, then use it instead of the callers buffer */ @@ -3503,7 +3448,7 @@ static int lpc54_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * completes. */ - lpc54_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; errout_with_asynch: @@ -3518,8 +3463,8 @@ static int lpc54_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, lpc54_free_xfrinfo(xfrinfo); ed->xfrinfo = NULL; -errout_with_sem: - lpc54_givesem(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_OHCI_ASYNCH */ @@ -3641,7 +3586,7 @@ static int lpc54_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake up the waiting thread */ - lpc54_givesem(&ed->wdhsem); + nxsem_post(&ed->wdhsem); xfrinfo->wdhwait = false; /* And free the transfer structure */ @@ -3718,7 +3663,7 @@ static int lpc54_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - lpc54_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -3884,10 +3829,10 @@ struct usbhost_connection_s *lpc54_usbhost_initialize(int controller) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/max326xx/common/max326_rtc_lowerhalf.c b/arch/arm/src/max326xx/common/max326_rtc_lowerhalf.c index 9c98bbed0d00b..46cf0d4c5c907 100644 --- a/arch/arm/src/max326xx/common/max326_rtc_lowerhalf.c +++ b/arch/arm/src/max326xx/common/max326_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -73,7 +74,7 @@ struct max326_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -369,7 +370,7 @@ static int max326_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->id == 0); priv = (struct max326_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -401,8 +402,7 @@ static int max326_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -665,18 +665,16 @@ static int max326_setperiodic(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL); priv = (struct max326_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } memcpy(&priv->periodic, alarminfo, sizeof(struct lower_setperiodic_s)); - ret = max326_rtc_setperiodic(&alarminfo->period, max326_periodic_callback); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -708,7 +706,7 @@ static int max326_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) DEBUGASSERT(id == 0); - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -716,8 +714,7 @@ static int max326_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) ret = max326_rtc_cancelperiodic(); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -750,8 +747,7 @@ static int max326_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) struct rtc_lowerhalf_s *max326_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/arm/src/max326xx/max32660/max32660_spim.c b/arch/arm/src/max326xx/max32660/max32660_spim.c index 93186b920af12..4cb783f8fec6a 100644 --- a/arch/arm/src/max326xx/max32660/max32660_spim.c +++ b/arch/arm/src/max326xx/max32660/max32660_spim.c @@ -57,7 +57,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -97,7 +97,7 @@ struct max326_spidev_s uint32_t base; /* SPI base address */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint16_t rxbytes; /* Number of bytes received into rxbuffer */ uint16_t txbytes; /* Number of bytes sent from txbuffer */ uint16_t xfrlen; /* Transfer length */ @@ -820,11 +820,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1433,9 +1433,9 @@ static void spi_bus_initialize(struct max326_spidev_s *priv) regval = priv->wire3 ? SPI_CTRL2_DATWIDTH_SINGLE : SPI_CTRL2_DATWIDTH_DUAL; spi_modify_ctrl2(priv, regval, SPI_CTRL2_DATWIDTH_MASK); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Disable all interrupts at the peripheral */ diff --git a/arch/arm/src/nrf52/nrf52_i2c.c b/arch/arm/src/nrf52/nrf52_i2c.c index db01e5dd637f9..a7a5fcfa2c882 100644 --- a/arch/arm/src/nrf52/nrf52_i2c.c +++ b/arch/arm/src/nrf52/nrf52_i2c.c @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include @@ -73,13 +73,13 @@ struct nrf52_i2c_priv_s uint8_t copy_buf[CONFIG_NRF52_I2C_MASTER_COPY_BUF_SIZE]; #endif - uint32_t freq; /* Current I2C frequency */ - int dcnt; /* Current message length */ - uint16_t flags; /* Current message flags */ - uint16_t addr; /* Current I2C address */ - sem_t sem_excl; /* Mutual exclusion semaphore */ + uint32_t freq; /* Current I2C frequency */ + int dcnt; /* Current message length */ + uint16_t flags; /* Current message flags */ + uint16_t addr; /* Current I2C address */ + mutex_t lock; /* Mutual exclusion mutex */ #ifndef CONFIG_I2C_POLLED - sem_t sem_isr; /* Interrupt wait semaphore */ + sem_t sem_isr; /* Interrupt wait semaphore */ #endif }; @@ -129,7 +129,9 @@ static struct nrf52_i2c_priv_s g_nrf52_i2c0_priv = .scl_pin = BOARD_I2C0_SCL_PIN, .sda_pin = BOARD_I2C0_SDA_PIN, .refs = 0, + .lock = NXMUTEX_INITIALIZER, #ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .irq = NRF52_IRQ_SPI_TWI_0, #endif .msgc = 0, @@ -152,7 +154,9 @@ static struct nrf52_i2c_priv_s g_nrf52_i2c1_priv = .scl_pin = BOARD_I2C1_SCL_PIN, .sda_pin = BOARD_I2C1_SDA_PIN, .refs = 0, + .lock = NXMUTEX_INITIALIZER, #ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .irq = NRF52_IRQ_SPI_TWI_1, #endif .msgc = 0, @@ -217,7 +221,7 @@ static int nrf52_i2c_transfer(struct i2c_master_s *dev, uint8_t *pack_buf = NULL; #endif - ret = nxsem_wait(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -516,7 +520,7 @@ static int nrf52_i2c_transfer(struct i2c_master_s *dev, } #endif - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return ret; } @@ -694,52 +698,6 @@ static int nrf52_i2c_init(struct nrf52_i2c_priv_s *priv) return OK; } -/**************************************************************************** - * Name: nrf52_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static int nrf52_i2c_sem_init(struct nrf52_i2c_priv_s *priv) -{ - /* Initialize semaphores */ - - 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 - - return OK; -} - -/**************************************************************************** - * Name: nrf52_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static int nrf52_i2c_sem_destroy(struct nrf52_i2c_priv_s *priv) -{ - /* Release unused resources */ - - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif - - return OK; -} - /**************************************************************************** * Name: nrf52_i2c_deinit * @@ -789,7 +747,6 @@ static int nrf52_i2c_deinit(struct nrf52_i2c_priv_s *priv) struct i2c_master_s *nrf52_i2cbus_initialize(int port) { struct nrf52_i2c_priv_s *priv = NULL; - irqstate_t flags; i2cinfo("I2C INITIALIZE port=%d\n", port); @@ -827,10 +784,6 @@ struct i2c_master_s *nrf52_i2cbus_initialize(int port) if (priv->refs++ == 0) { - /* Initialize sempaphores */ - - nrf52_i2c_sem_init(priv); - /* Initialize I2C */ nrf52_i2c_init(priv); @@ -877,9 +830,5 @@ int nrf52_i2cbus_uninitialize(struct i2c_master_s *dev) nrf52_i2c_deinit(priv); - /* Release semaphores */ - - nrf52_i2c_sem_destroy(priv); - return OK; } diff --git a/arch/arm/src/nrf52/nrf52_radio.c b/arch/arm/src/nrf52/nrf52_radio.c index f515984e9bc6b..550d31f88d099 100644 --- a/arch/arm/src/nrf52/nrf52_radio.c +++ b/arch/arm/src/nrf52/nrf52_radio.c @@ -726,7 +726,7 @@ static int nrf52_radio_write(struct nrf52_radio_dev_s *dev, /* Lock device */ - ret = nxsem_wait(&dev->sem_excl); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -766,8 +766,7 @@ static int nrf52_radio_write(struct nrf52_radio_dev_s *dev, /* Unlock device */ - nxsem_post(&dev->sem_excl); - + nxmutex_unlock(&dev->lock); return ret; } @@ -786,7 +785,7 @@ static int nrf52_radio_read(struct nrf52_radio_dev_s *dev, /* Lock radio */ - ret = nxsem_wait(&dev->sem_excl); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -826,8 +825,7 @@ static int nrf52_radio_read(struct nrf52_radio_dev_s *dev, /* Unlock radio */ - nxsem_post(&dev->sem_excl); - + nxmutex_unlock(&dev->lock); return ret; } @@ -1162,9 +1160,9 @@ nrf52_radio_initialize(int intf, struct nrf52_radio_board_s *board) irq_attach(dev->irq, nrf52_radio_isr, dev); up_enable_irq(dev->irq); - /* Initialize semaphores */ + /* Initialize mutex */ - nxsem_init(&dev->sem_excl, 0, 1); + nxmutex_init(&dev->lock); /* This semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/nrf52/nrf52_radio.h b/arch/arm/src/nrf52/nrf52_radio.h index 8f7478b254e3d..a0e01804d629f 100644 --- a/arch/arm/src/nrf52/nrf52_radio.h +++ b/arch/arm/src/nrf52/nrf52_radio.h @@ -29,6 +29,7 @@ #include +#include #include #include "chip.h" @@ -252,7 +253,7 @@ struct nrf52_radio_dev_s uint16_t txbuf_len; /* TX buffer length */ uint8_t *rxbuf; /* RX buffer */ uint8_t *txbuf; /* TX buffer */ - sem_t sem_excl; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ sem_t sem_isr; /* Interrupt wait semaphore */ uint16_t tifs; /* Interframe spacing time */ uint8_t txpower; /* TX power */ diff --git a/arch/arm/src/nrf52/nrf52_rng.c b/arch/arm/src/nrf52/nrf52_rng.c index bf72ad046b4d4..a2263fd5d658e 100644 --- a/arch/arm/src/nrf52/nrf52_rng.c +++ b/arch/arm/src/nrf52/nrf52_rng.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -64,7 +65,7 @@ struct rng_dev_s size_t rd_count; size_t buflen; sem_t rd_sem; /* semaphore for read RNG data */ - sem_t excl_sem; /* semaphore for access RNG dev */ + mutex_t lock; /* mutex for access RNG dev */ }; /**************************************************************************** @@ -130,8 +131,7 @@ static int nrf52_rng_initialize(void) nxsem_init(&g_rngdev.rd_sem, 0, 0); nxsem_set_protocol(&g_rngdev.rd_sem, SEM_PRIO_NONE); - nxsem_init(&g_rngdev.excl_sem, 0, 1); - nxsem_set_protocol(&g_rngdev.excl_sem, SEM_PRIO_NONE); + nxmutex_init(&g_rngdev.lock); _info("Ready to stop\n"); nrf52_rng_stop(); @@ -200,7 +200,7 @@ static ssize_t nrf52_rng_read(struct file *filep, char *buffer, struct rng_dev_s *priv = (struct rng_dev_s *)&g_rngdev; ssize_t read_len; - if (nxsem_wait(&priv->excl_sem) != OK) + if (nxmutex_lock(&priv->lock) != OK) { return -EBUSY; } @@ -224,8 +224,7 @@ static ssize_t nrf52_rng_read(struct file *filep, char *buffer, /* Now , got data, and release rd_sem for next read */ - nxsem_post(&priv->excl_sem); - + nxmutex_unlock(&priv->lock); return read_len; } diff --git a/arch/arm/src/nrf52/nrf52_sdc.c b/arch/arm/src/nrf52/nrf52_sdc.c index ca1860f3c615b..6152f51ab4b3b 100644 --- a/arch/arm/src/nrf52/nrf52_sdc.c +++ b/arch/arm/src/nrf52/nrf52_sdc.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -102,7 +103,7 @@ struct nrf52_sdc_dev_s uint8_t mempool[MEMPOOL_SIZE]; uint8_t msg_buffer[HCI_MSG_BUFFER_MAX_SIZE]; - sem_t exclsem; + mutex_t lock; struct work_s work; }; @@ -204,7 +205,7 @@ static int bt_hci_send(struct bt_driver_s *btdev, /* Ensure non-concurrent access to SDC operations */ - nxsem_wait_uninterruptible(&g_sdc_dev.exclsem); + nxmutex_lock(&g_sdc_dev.lock); if (type == BT_CMD) { @@ -215,8 +216,7 @@ static int bt_hci_send(struct bt_driver_s *btdev, ret = sdc_hci_data_put(data); } - nxsem_post(&g_sdc_dev.exclsem); - + nxmutex_unlock(&g_sdc_dev.lock); if (ret >= 0) { ret = len; @@ -258,9 +258,9 @@ static void low_prio_worker(void *arg) * internally when required. */ - nxsem_wait_uninterruptible(&g_sdc_dev.exclsem); + nxmutex_lock(&g_sdc_dev.lock); mpsl_low_priority_process(); - nxsem_post(&g_sdc_dev.exclsem); + nxmutex_unlock(&g_sdc_dev.lock); } /**************************************************************************** @@ -274,9 +274,9 @@ static void on_hci_worker(void *arg) * worker */ - nxsem_wait_uninterruptible(&g_sdc_dev.exclsem); + nxmutex_lock(&g_sdc_dev.lock); on_hci(); - nxsem_post(&g_sdc_dev.exclsem); + nxmutex_unlock(&g_sdc_dev.lock); } /**************************************************************************** @@ -502,7 +502,7 @@ int nrf52_sdc_initialize(void) /* Initialize device data */ memset(&g_sdc_dev, 0, sizeof(g_sdc_dev)); - nxsem_init(&g_sdc_dev.exclsem, 0, 1); + nxmutex_init(&g_sdc_dev.lock); /* Register interrupt handler for normal-priority events. SWI5 will be * used by MPSL to delegate low-priority work diff --git a/arch/arm/src/nrf52/nrf52_spi.c b/arch/arm/src/nrf52/nrf52_spi.c index afbb26e684a2d..32033df45e0a5 100644 --- a/arch/arm/src/nrf52/nrf52_spi.c +++ b/arch/arm/src/nrf52/nrf52_spi.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include @@ -83,7 +83,7 @@ struct nrf52_spidev_s uint32_t frequency; /* Requested clock frequency */ uint8_t mode; /* Mode 0,1,2,3 */ - sem_t exclsem; /* Held while chip is selected for mutual + mutex_t lock; /* Held while chip is selected for mutual * exclusion */ #ifdef CONFIG_NRF52_SPI_MASTER_INTERRUPTS @@ -687,11 +687,11 @@ static int nrf52_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1470,9 +1470,9 @@ struct spi_dev_s *nrf52_spibus_initialize(int port) priv->initialized = true; - /* Initialize the SPI semaphore */ + /* Initialize the SPI mutex */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_NRF52_SPI_MASTER_INTERRUPTS /* This semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/rp2040/rp2040_cyw43439.c b/arch/arm/src/rp2040/rp2040_cyw43439.c index d5e6726e1cb5d..45a7348c469a9 100644 --- a/arch/arm/src/rp2040/rp2040_cyw43439.c +++ b/arch/arm/src/rp2040/rp2040_cyw43439.c @@ -400,7 +400,7 @@ static int my_write(struct gspi_dev_s *gspi, /* Claim the exclusive lock */ - nxsem_wait(&gspi->exclsem); + nxmutex_lock(&gspi->lock); /* Reset the PIO state machine just to be sure. */ @@ -541,8 +541,7 @@ static int my_write(struct gspi_dev_s *gspi, /* Release the exclusive lock */ - nxsem_post(&gspi->exclsem); - + nxmutex_unlock(&gspi->lock); return dma_info.status; } @@ -616,7 +615,7 @@ static int my_read(struct gspi_dev_s *gspi, /* Claim the exclusive lock */ - nxsem_wait(&gspi->exclsem); + nxmutex_lock(&gspi->lock); /* Reset the PIO state machine just to be sure. */ @@ -761,8 +760,7 @@ static int my_read(struct gspi_dev_s *gspi, /* Release the exclusive lock */ - nxsem_post(&gspi->exclsem); - + nxmutex_unlock(&gspi->lock); return dma_info.status; } @@ -820,7 +818,7 @@ gspi_dev_t *rp2040_cyw_setup(uint8_t gpio_on, rp_io->gpio_clock = gpio_clock; rp_io->gpio_intr = gpio_intr; - nxsem_init(&gspi->exclsem, 0, 1); + nxmutex_init(&gspi->lock); /* Initialize the cyw43439 power-on and chip select lines. */ diff --git a/arch/arm/src/rp2040/rp2040_dmac.c b/arch/arm/src/rp2040/rp2040_dmac.c index 3d22127229c08..115209df7e47e 100644 --- a/arch/arm/src/rp2040/rp2040_dmac.c +++ b/arch/arm/src/rp2040/rp2040_dmac.c @@ -33,6 +33,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -61,8 +62,8 @@ struct dma_channel_s struct dma_controller_s { - sem_t exclsem; /* Protects channel table */ - sem_t chansem; /* Count of free channels */ + mutex_t lock; /* Protects channel table */ + sem_t chansem; /* Count of free channels */ }; /**************************************************************************** @@ -160,7 +161,7 @@ void weak_function arm_dma_initialize(void) /* Initialize the channel list */ - nxsem_init(&g_dmac.exclsem, 0, 1); + nxmutex_init(&g_dmac.lock); nxsem_init(&g_dmac.chansem, 0, RP2040_DMA_NCHANNELS); for (i = 0; i < RP2040_DMA_NCHANNELS; i++) @@ -222,7 +223,7 @@ DMA_HANDLE rp2040_dmachannel(void) /* Get exclusive access to the DMA channel list */ - ret = nxsem_wait_uninterruptible(&g_dmac.exclsem); + ret = nxmutex_lock(&g_dmac.lock); if (ret < 0) { nxsem_post(&g_dmac.chansem); @@ -244,7 +245,7 @@ DMA_HANDLE rp2040_dmachannel(void) } } - nxsem_post(&g_dmac.exclsem); + nxmutex_unlock(&g_dmac.lock); setbits_reg32(bit, RP2040_DMA_INTS0); setbits_reg32(bit, RP2040_DMA_INTE0); diff --git a/arch/arm/src/rp2040/rp2040_flash_mtd.c b/arch/arm/src/rp2040/rp2040_flash_mtd.c index be0783c83877f..cef367e116aff 100644 --- a/arch/arm/src/rp2040/rp2040_flash_mtd.c +++ b/arch/arm/src/rp2040/rp2040_flash_mtd.c @@ -46,7 +46,7 @@ #include #include #include -#include +#include #include "rp2040_flash_mtd.h" #include "rp2040_rom.h" @@ -103,7 +103,7 @@ typedef struct rp2040_flash_dev_s { struct mtd_dev_s mtd_dev; /* Embedded mdt_dev structure */ - sem_t sem; /* file access serialization */ + mutex_t lock; /* file access serialization */ uint32_t boot_2[BOOT_2_SIZE / 4]; /* RAM copy of boot_2 */ } rp2040_flash_dev_t; @@ -256,8 +256,7 @@ static int rp2040_flash_erase(struct mtd_dev_s *dev, nblocks, FLASH_BLOCK_SIZE * nblocks); - ret = nxsem_wait(&(rp_dev->sem)); - + ret = nxmutex_lock(&rp_dev->lock); if (ret < 0) { return ret; @@ -280,8 +279,7 @@ static int rp2040_flash_erase(struct mtd_dev_s *dev, ret = nblocks; - nxsem_post(&(rp_dev->sem)); - + nxmutex_unlock(&rp_dev->lock); return ret; } @@ -299,8 +297,7 @@ static ssize_t rp2040_flash_block_read(struct mtd_dev_s *dev, int length; int ret = OK; - ret = nxsem_wait(&(rp_dev->sem)); - + ret = nxmutex_lock(&rp_dev->lock); if (ret < 0) { return ret; @@ -325,8 +322,7 @@ static ssize_t rp2040_flash_block_read(struct mtd_dev_s *dev, /* Update the file position */ - nxsem_post(&(rp_dev->sem)); - + nxmutex_unlock(&rp_dev->lock); return nblocks; } @@ -341,10 +337,9 @@ static ssize_t rp2040_flash_block_write(struct mtd_dev_s *dev, { rp2040_flash_dev_t *rp_dev = (rp2040_flash_dev_t *) dev; irqstate_t flags; - int ret = OK; - - ret = nxsem_wait(&(rp_dev->sem)); + int ret; + ret = nxmutex_lock(&rp_dev->lock); if (ret < 0) { return ret; @@ -385,8 +380,7 @@ static ssize_t rp2040_flash_block_write(struct mtd_dev_s *dev, ret = nblocks; - nxsem_post(&(rp_dev->sem)); - + nxmutex_unlock(&rp_dev->lock); return ret; } @@ -394,17 +388,16 @@ static ssize_t rp2040_flash_block_write(struct mtd_dev_s *dev, * Name: rp2040_flash_byte_read ****************************************************************************/ -static ssize_t rp2040_flash_byte_read (struct mtd_dev_s *dev, - off_t offset, - size_t nbytes, - uint8_t *buffer) +static ssize_t rp2040_flash_byte_read(struct mtd_dev_s *dev, + off_t offset, + size_t nbytes, + uint8_t *buffer) { - rp2040_flash_dev_t *rp_dev = (rp2040_flash_dev_t *) dev; + rp2040_flash_dev_t *rp_dev = (rp2040_flash_dev_t *)dev; int length; int ret = OK; - ret = nxsem_wait(&(rp_dev->sem)); - + ret = nxmutex_lock(&rp_dev->lock); if (ret < 0) { return ret; @@ -437,8 +430,7 @@ static ssize_t rp2040_flash_byte_read (struct mtd_dev_s *dev, /* Update the file position */ - nxsem_post(&(rp_dev->sem)); - + nxmutex_unlock(&rp_dev->lock); return length; } @@ -511,7 +503,7 @@ struct mtd_dev_s *rp2040_flash_mtd_initialize(void) initialized = true; - nxsem_init(&(my_dev.sem), 0, 1); + nxmutex_init(&my_dev.lock); if (FLASH_BLOCK_COUNT < 4) { diff --git a/arch/arm/src/rp2040/rp2040_i2c.c b/arch/arm/src/rp2040/rp2040_i2c.c index 0b975fdf9a89b..fc826509264a7 100644 --- a/arch/arm/src/rp2040/rp2040_i2c.c +++ b/arch/arm/src/rp2040/rp2040_i2c.c @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -75,7 +76,7 @@ struct rp2040_i2cdev_s int8_t port; /* Port number */ uint32_t base_freq; /* branch frequency */ - sem_t mutex; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t wait; /* Place to wait for transfer completion */ struct wdog_s timeout; /* watchdog to timeout when bus hung */ uint32_t frequency; /* Current I2C frequency */ @@ -94,6 +95,8 @@ static struct rp2040_i2cdev_s g_i2c0dev = .port = 0, .base = RP2040_I2C0_BASE, .irqid = RP2040_I2C0_IRQ, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .refs = 0, }; #endif @@ -103,6 +106,8 @@ static struct rp2040_i2cdev_s g_i2c1dev = .port = 1, .base = RP2040_I2C1_BASE, .irqid = RP2040_I2C1_IRQ, + .lock = NXMUTEX_INITIALIZER, + .wait = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .refs = 0, }; #endif @@ -111,9 +116,6 @@ static struct rp2040_i2cdev_s g_i2c1dev = * Private Functions ****************************************************************************/ -static inline int i2c_takesem(sem_t *sem); -static inline int i2c_givesem(sem_t *sem); - static inline uint32_t i2c_reg_read(struct rp2040_i2cdev_s *priv, uint32_t offset); static inline void i2c_reg_write(struct rp2040_i2cdev_s *priv, @@ -137,24 +139,6 @@ static int rp2040_i2c_transfer(struct i2c_master_s *dev, static int rp2040_i2c_reset(struct i2c_master_s *dev); #endif -/**************************************************************************** - * Name: i2c_takesem - ****************************************************************************/ - -static inline int i2c_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: i2c_givesem - ****************************************************************************/ - -static inline int i2c_givesem(sem_t *sem) -{ - return nxsem_post(sem); -} - /**************************************************************************** * I2C device operations ****************************************************************************/ @@ -284,7 +268,7 @@ static void rp2040_i2c_timeout(wdparm_t arg) irqstate_t flags = enter_critical_section(); priv->error = -ENODEV; - i2c_givesem(&priv->wait); + nxsem_post(&priv->wait); leave_critical_section(flags); } @@ -394,7 +378,7 @@ static int rp2040_i2c_interrupt(int irq, void *context, void *arg) ret = wd_cancel(&priv->timeout); if (ret == OK) { - i2c_givesem(&priv->wait); + nxsem_post(&priv->wait); } } @@ -462,7 +446,7 @@ static int rp2040_i2c_receive(struct rp2040_i2cdev_s *priv, int last) RP2040_I2C_IC_INTR_STAT_R_RX_FULL, RP2040_I2C_IC_INTR_STAT_R_RX_FULL); leave_critical_section(flags); - i2c_takesem(&priv->wait); + nxsem_wait_uninterruptible(&priv->wait); if (priv->error != OK) { @@ -517,8 +501,7 @@ static int rp2040_i2c_send(struct rp2040_i2cdev_s *priv, int last) RP2040_I2C_IC_INTR_STAT_R_TX_EMPTY); leave_critical_section(flags); - i2c_takesem(&priv->wait); - + nxsem_wait_uninterruptible(&priv->wait); return 0; } @@ -546,7 +529,7 @@ static int rp2040_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - i2c_takesem(&priv->mutex); + nxmutex_lock(&priv->lock); /* Check wait semaphore value. If the value is not 0, the transfer can not * be performed normally. @@ -618,8 +601,7 @@ static int rp2040_i2c_transfer(struct i2c_master_s *dev, rp2040_i2c_disable(priv); } - i2c_givesem(&priv->mutex); - + nxmutex_unlock(&priv->lock); return ret; } @@ -658,8 +640,7 @@ static int rp2040_i2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - i2c_takesem(&priv->mutex); - + nxmutex_lock(&priv->lock); ret = -EIO; /* De-init the port */ @@ -775,7 +756,7 @@ static int rp2040_i2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - i2c_givesem(&priv->mutex); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -880,7 +861,7 @@ struct i2c_master_s *rp2040_i2cbus_initialize(int port) #ifdef CONFIG_RP2040_I2C0 if (port == 0) { - priv = &g_i2c0dev; + priv = &g_i2c0dev; priv->dev.ops = &rp2040_i2c_ops; } else @@ -888,7 +869,7 @@ struct i2c_master_s *rp2040_i2cbus_initialize(int port) #ifdef CONFIG_RP2040_I2C1 if (port == 1) { - priv = &g_i2c1dev; + priv = &g_i2c1dev; priv->dev.ops = &rp2040_i2c_ops; } else @@ -920,10 +901,6 @@ struct i2c_master_s *rp2040_i2cbus_initialize(int port) leave_critical_section(flags); - nxsem_init(&priv->mutex, 0, 1); - nxsem_init(&priv->wait, 0, 0); - nxsem_set_protocol(&priv->wait, SEM_PRIO_NONE); - /* Attach Interrupt Handler */ irq_attach(priv->irqid, rp2040_i2c_interrupt, priv); @@ -965,8 +942,6 @@ int rp2040_i2cbus_uninitialize(struct i2c_master_s *dev) irq_detach(priv->irqid); wd_cancel(&priv->timeout); - nxsem_destroy(&priv->mutex); - nxsem_destroy(&priv->wait); return OK; } diff --git a/arch/arm/src/rp2040/rp2040_i2c_slave.c b/arch/arm/src/rp2040/rp2040_i2c_slave.c index 4f9d98360e403..af9e057dc58d6 100644 --- a/arch/arm/src/rp2040/rp2040_i2c_slave.c +++ b/arch/arm/src/rp2040/rp2040_i2c_slave.c @@ -285,7 +285,7 @@ static int i2c_interrupt(int irq, void *context, void *arg) ret = wd_cancel(&priv->timeout); if (ret == OK) { - i2c_givesem(&priv->wait); + nxsem_post(&priv->wait); } } #endif diff --git a/arch/arm/src/rp2040/rp2040_i2s.c b/arch/arm/src/rp2040/rp2040_i2s.c index b2d47a824fc20..725d77dea9420 100644 --- a/arch/arm/src/rp2040/rp2040_i2s.c +++ b/arch/arm/src/rp2040/rp2040_i2s.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -138,7 +139,7 @@ struct rp2040_transport_s struct rp2040_i2s_s { struct i2s_dev_s dev; /* Externally visible I2S interface */ - sem_t exclsem; /* Assures mutually exclusive access to I2S */ + mutex_t lock; /* Assures mutually exclusive access to I2S */ bool initialized; /* Has I2S interface been initialized */ uint8_t datalen; /* Data width (8 or 16) */ #ifdef CONFIG_DEBUG_FEATURES @@ -168,14 +169,6 @@ struct rp2040_i2s_s # define i2s_dump_buffer(m,b,s) #endif -/* Semaphore helpers */ - -static int i2s_exclsem_take(struct rp2040_i2s_s *priv); -#define i2s_exclsem_give(priv) nxsem_post(&priv->exclsem) - -static int i2s_bufsem_take(struct rp2040_i2s_s *priv); -#define i2s_bufsem_give(priv) nxsem_post(&priv->bufsem) - /* Buffer container helpers */ static struct rp2040_buffer_s * @@ -235,46 +228,6 @@ static const struct i2s_ops_s g_i2sops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: i2s_exclsem_take - * - * Description: - * Take the exclusive access semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the i2s peripheral state - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int i2s_exclsem_take(struct rp2040_i2s_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -/**************************************************************************** - * Name: i2s_bufsem_take - * - * Description: - * Take the buffer semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the i2s peripheral state - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int i2s_bufsem_take(struct rp2040_i2s_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->bufsem); -} - /**************************************************************************** * Name: i2s_buf_allocate * @@ -305,7 +258,7 @@ static struct rp2040_buffer_s *i2s_buf_allocate(struct rp2040_i2s_s *priv) * have at least one free buffer container. */ - ret = i2s_bufsem_take(priv); + ret = nxsem_wait_uninterruptible(&priv->bufsem); if (ret < 0) { return NULL; @@ -356,7 +309,7 @@ static void i2s_buf_free(struct rp2040_i2s_s *priv, /* Wake up any threads waiting for a buffer container */ - i2s_bufsem_give(priv); + nxsem_post(&priv->bufsem); } /**************************************************************************** @@ -913,7 +866,7 @@ static int rp2040_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the I2S driver data */ - ret = i2s_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout_with_buf; @@ -937,7 +890,7 @@ static int rp2040_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, sq_addlast((sq_entry_t *)bfcontainer, &priv->tx.pend); leave_critical_section(flags); - i2s_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; errout_with_buf: @@ -1351,7 +1304,7 @@ struct i2s_dev_s *rp2040_i2sbus_initialize(int port) /* Initialize the common parts for the I2S device structure */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->dev.ops = &g_i2sops; /* Initialize buffering */ @@ -1379,7 +1332,7 @@ struct i2s_dev_s *rp2040_i2sbus_initialize(int port) /* Failure exits */ errout_with_alloc: - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return NULL; } diff --git a/arch/arm/src/rp2040/rp2040_spi.c b/arch/arm/src/rp2040/rp2040_spi.c index 824d6c59d266d..ae0eb410115e6 100644 --- a/arch/arm/src/rp2040/rp2040_spi.c +++ b/arch/arm/src/rp2040/rp2040_spi.c @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -73,7 +73,7 @@ struct rp2040_spidev_s #ifdef CONFIG_RP2040_SPI_INTERRUPTS uint8_t spiirq; /* SPI IRQ number */ #endif - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 to 16) */ @@ -320,13 +320,13 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - /* Take the semaphore (perhaps waiting) */ + /* Take the mutex (perhaps waiting) */ - return nxsem_wait_uninterruptible(&priv->exclsem); + return nxmutex_lock(&priv->lock); } else { - return nxsem_post(&priv->exclsem); + return nxmutex_unlock(&priv->lock); } } @@ -858,9 +858,9 @@ struct spi_dev_s *rp2040_spibus_initialize(int port) spi_setfrequency((struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); regval = spi_getreg(priv, RP2040_SPI_SSPCR1_OFFSET); spi_putreg(priv, RP2040_SPI_SSPCR1_OFFSET, regval | RP2040_SPI_SSPCR1_SSE); diff --git a/arch/arm/src/rp2040/rp2040_ws2812.c b/arch/arm/src/rp2040/rp2040_ws2812.c index baef691b196bc..f6aeb25093c71 100644 --- a/arch/arm/src/rp2040/rp2040_ws2812.c +++ b/arch/arm/src/rp2040/rp2040_ws2812.c @@ -102,8 +102,7 @@ void dma_complete(DMA_HANDLE handle, uint8_t status, void *arg) rp2040_dmafree(handle); priv->last_dma = clock_systime_ticks(); - - nxsem_post(&dev_data->exclsem); + nxmutex_unlock(&dev_data->lock); } /**************************************************************************** @@ -147,7 +146,7 @@ static void update_pixels(struct ws2812_dev_s *dev_data) rp2040_dmastart(dma_handle, dma_complete, dev_data); - /* NOTE: we don't post exclsem here, the dma_complete does that */ + /* NOTE: we don't post lock here, the dma_complete does that */ } /**************************************************************************** @@ -321,7 +320,6 @@ static int my_open(struct file *filep) post_and_return: leave_critical_section(flags); - return ret; } @@ -344,7 +342,7 @@ static int my_close(struct file *filep) struct ws2812_dev_s *dev_data = inode->i_private; struct instance *priv = (struct instance *)dev_data->private; - nxsem_wait(&dev_data->exclsem); + nxmutex_lock(&dev_data->lock); ledinfo("rp2040_ws2812 close dev: 0x%08lX\n", (uint32_t) dev_data); @@ -355,8 +353,7 @@ static int my_close(struct file *filep) rp2040_gpio_put(priv->power_pin, false); } - nxsem_post(&dev_data->exclsem); - + nxmutex_unlock(&dev_data->lock); return OK; } @@ -391,7 +388,7 @@ static ssize_t my_write(struct file *filep, return 0; } - nxsem_wait(&dev_data->exclsem); + nxmutex_lock(&dev_data->lock); ledinfo("rp2040_ws2812 write dev: 0x%08lX\n", (uint32_t) dev_data); @@ -435,7 +432,7 @@ static ssize_t my_write(struct file *filep, update_pixels(dev_data); - /* NOTE: we don't post exclsem here, so update_pixels must make sure + /* NOTE: we don't post lock here, so update_pixels must make sure * that happens. */ @@ -473,7 +470,7 @@ static ssize_t my_read(struct file *filep, return 0; } - nxsem_wait(&dev_data->exclsem); + nxmutex_lock(&dev_data->lock); /* Copy the data from the buffer swapping the * red and green, since ws2812 use a GRB order @@ -510,8 +507,7 @@ static ssize_t my_read(struct file *filep, filep->f_pos = position; - nxsem_wait(&dev_data->exclsem); - + nxmutex_unlock(&dev_data->lock); return xfer_index; } @@ -577,7 +573,7 @@ void * rp2040_ws2812_setup(const char *path, dev_data->clock = CONFIG_WS2812_FREQUENCY; dev_data->private = priv; - nxsem_init(&dev_data->exclsem, 0, 1); + nxmutex_init(&dev_data->lock); priv->power_pin = power_pin; @@ -617,7 +613,7 @@ int rp2040_ws2812_release(void * driver) int ret = OK; - nxsem_wait(&dev_data->exclsem); + nxmutex_lock(&dev_data->lock); if (priv->open_count == 0) { @@ -626,7 +622,7 @@ int rp2040_ws2812_release(void * driver) rp2040_pio_sm_set_enabled(priv->pio, priv->pio_sm, false); rp2040_pio_sm_unclaim(priv->pio, priv->pio_sm); - nxsem_post(&dev_data->exclsem); + nxmutex_unlock(&dev_data->lock); kmm_free(priv->pixels); kmm_free(priv); @@ -634,7 +630,7 @@ int rp2040_ws2812_release(void * driver) else { ret = -EBUSY; - nxsem_post(&dev_data->exclsem); + nxmutex_unlock(&dev_data->lock); } return ret; diff --git a/arch/arm/src/rtl8720c/amebaz_driver.c b/arch/arm/src/rtl8720c/amebaz_driver.c index e1d4766ab502d..0c021480094e9 100644 --- a/arch/arm/src/rtl8720c/amebaz_driver.c +++ b/arch/arm/src/rtl8720c/amebaz_driver.c @@ -64,7 +64,7 @@ static void amebaz_state_timeout(wdparm_t arg) } state->status = AMEBAZ_STATUS_TIMEOUT; - nxsem_post(&state->mutex); + nxsem_post(&state->sem); } static int amebaz_state_run(struct amebaz_state_s *state, int32_t delay) @@ -84,7 +84,7 @@ static int amebaz_state_wait(struct amebaz_state_s *state) int ret = 0; while (state->status == AMEBAZ_STATUS_RUN) { - ret = nxsem_wait_uninterruptible(&state->mutex); + ret = nxsem_wait_uninterruptible(&state->sem); if (ret != 0) { break; @@ -101,7 +101,7 @@ static void amebaz_state_post(struct amebaz_state_s *state, int status) if (_status == AMEBAZ_STATUS_RUN) { wd_cancel(&state->timeout); - nxsem_post(&state->mutex); + nxsem_post(&state->sem); } } @@ -112,7 +112,7 @@ static void amebaz_state_deinit(struct amebaz_state_s *state) static int amebaz_state_init(struct amebaz_state_s *state) { - if (nxsem_init(&state->mutex, 0, 0) != OK) + if (nxsem_init(&state->sem, 0, 0) != OK) { return -ENOMEM; } @@ -509,7 +509,7 @@ int amebaz_wl_get_scan_results(struct amebaz_dev_s *priv, struct iwreq *iwr) iwr->u.data.length = amebaz_wl_format_scan_results(priv, iwr); exit_sem_post: - nxsem_post(&state->mutex); + nxsem_post(&state->sem); exit_failed: if (ret < 0) { diff --git a/arch/arm/src/rtl8720c/amebaz_driver.h b/arch/arm/src/rtl8720c/amebaz_driver.h index 2225e23803e93..9ed964573bc19 100644 --- a/arch/arm/src/rtl8720c/amebaz_driver.h +++ b/arch/arm/src/rtl8720c/amebaz_driver.h @@ -26,7 +26,7 @@ ****************************************************************************/ #include -#include +#include #include #include #include @@ -56,7 +56,7 @@ enum struct amebaz_state_s { - sem_t mutex; + sem_t sem; struct wdog_s timeout; int status; }; diff --git a/arch/arm/src/s32k1xx/s32k1xx_edma.c b/arch/arm/src/s32k1xx/s32k1xx_edma.c index 3b11d66ad95e0..85e0fcfd7c385 100644 --- a/arch/arm/src/s32k1xx/s32k1xx_edma.c +++ b/arch/arm/src/s32k1xx/s32k1xx_edma.c @@ -58,6 +58,7 @@ #include #include #include +#include #include #include "arm_internal.h" @@ -136,9 +137,9 @@ struct s32k1xx_dmach_s struct s32k1xx_edma_s { - /* These semaphores protect the DMA channel and descriptor tables */ + /* These mutex protect the DMA channel and descriptor tables */ - sem_t chsem; /* Protects channel table */ + mutex_t chlock; /* Protects channel table */ #if CONFIG_S32K1XX_EDMA_NTCD > 0 sem_t dsem; /* Supports wait for free descriptors */ #endif @@ -171,25 +172,6 @@ static struct s32k1xx_edmatcd_s g_tcd_pool[CONFIG_S32K1XX_EDMA_NTCD] * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: s32k1xx_takechsem() and s32k1xx_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table for channel - * allocation. - * - ****************************************************************************/ - -static int s32k1xx_takechsem(void) -{ - return nxsem_wait_uninterruptible(&g_edma.chsem); -} - -static inline void s32k1xx_givechsem(void) -{ - nxsem_post(&g_edma.chsem); -} - /**************************************************************************** * Name: s32k1xx_takedsem() and s32k1xx_givedsem() * @@ -730,9 +712,9 @@ void weak_function arm_dma_initialize(void) g_edma.dmach[i].chan = i; } - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&g_edma.chsem, 0, 1); + nxmutex_init(&g_edma.chlock); #if CONFIG_S32K1XX_EDMA_NTCD > 0 nxsem_init(&g_edma.dsem, 0, CONFIG_S32K1XX_EDMA_NTCD); @@ -842,7 +824,7 @@ DMACH_HANDLE s32k1xx_dmach_alloc(uint8_t dmamux, uint8_t dchpri) /* Search for an available DMA channel */ dmach = NULL; - ret = s32k1xx_takechsem(); + ret = nxmutex_lock(&g_edma.chlock); if (ret < 0) { return NULL; @@ -879,7 +861,7 @@ DMACH_HANDLE s32k1xx_dmach_alloc(uint8_t dmamux, uint8_t dchpri) } } - s32k1xx_givechsem(); + nxmutex_unlock(&g_edma.chlock); /* Show the result of the allocation */ diff --git a/arch/arm/src/s32k1xx/s32k1xx_lpi2c.c b/arch/arm/src/s32k1xx/s32k1xx_lpi2c.c index cccfa6f150342..548a87248d7da 100644 --- a/arch/arm/src/s32k1xx/s32k1xx_lpi2c.c +++ b/arch/arm/src/s32k1xx/s32k1xx_lpi2c.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -184,7 +185,7 @@ struct s32k1xx_lpi2c_priv_s const struct s32k1xx_lpi2c_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 @@ -230,8 +231,6 @@ static inline void s32k1xx_lpi2c_modifyreg(struct s32k1xx_lpi2c_priv_s *priv, uint16_t offset, uint32_t clearbits, uint32_t setbits); -static inline int -s32k1xx_lpi2c_sem_wait(struct s32k1xx_lpi2c_priv_s *priv); #ifdef CONFIG_S32K1XX_I2C_DYNTIMEO static uint32_t @@ -242,12 +241,6 @@ static inline int s32k1xx_lpi2c_sem_waitdone(struct s32k1xx_lpi2c_priv_s *priv); static inline void s32k1xx_lpi2c_sem_waitstop(struct s32k1xx_lpi2c_priv_s *priv); -static inline void -s32k1xx_lpi2c_sem_post(struct s32k1xx_lpi2c_priv_s *priv); -static inline void -s32k1xx_lpi2c_sem_init(struct s32k1xx_lpi2c_priv_s *priv); -static inline void -s32k1xx_lpi2c_sem_destroy(struct s32k1xx_lpi2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void s32k1xx_lpi2c_tracereset(struct s32k1xx_lpi2c_priv_s *priv); @@ -347,6 +340,10 @@ static struct s32k1xx_lpi2c_priv_s s32k1xx_lpi2c0_priv = .ops = &s32k1xx_lpi2c_ops, .config = &s32k1xx_lpi2c0_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, @@ -381,6 +378,10 @@ static struct s32k1xx_lpi2c_priv_s s32k1xx_lpi2c1_priv = .ops = &s32k1xx_lpi2c_ops, .config = &s32k1xx_lpi2c1_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, @@ -441,21 +442,6 @@ s32k1xx_lpi2c_modifyreg(struct s32k1xx_lpi2c_priv_s *priv, modifyreg32(priv->config->base + offset, clearbits, setbits); } -/**************************************************************************** - * Name: s32k1xx_lpi2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. May be interrupted by - * a signal. - * - ****************************************************************************/ - -static inline int -s32k1xx_lpi2c_sem_wait(struct s32k1xx_lpi2c_priv_s *priv) -{ - return nxsem_wait(&priv->sem_excl); -} - /**************************************************************************** * Name: s32k1xx_lpi2c_toticks * @@ -735,59 +721,6 @@ s32k1xx_lpi2c_sem_waitstop(struct s32k1xx_lpi2c_priv_s *priv) i2cinfo("Timeout with Status Register: %" PRIx32 "\n", regval); } -/**************************************************************************** - * Name: s32k1xx_lpi2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void s32k1xx_lpi2c_sem_post(struct s32k1xx_lpi2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: s32k1xx_lpi2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void -s32k1xx_lpi2c_sem_init(struct s32k1xx_lpi2c_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: s32k1xx_lpi2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void -s32k1xx_lpi2c_sem_destroy(struct s32k1xx_lpi2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - /**************************************************************************** * Name: s32k1xx_rxdma_callback * @@ -1959,7 +1892,7 @@ static int s32k1xx_lpi2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = s32k1xx_lpi2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2064,7 +1997,7 @@ static int s32k1xx_lpi2c_transfer(struct i2c_master_s *dev, priv->dcnt = 0; priv->ptr = NULL; - s32k1xx_lpi2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -2101,7 +2034,7 @@ static int s32k1xx_lpi2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - ret = s32k1xx_lpi2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2204,7 +2137,7 @@ static int s32k1xx_lpi2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - s32k1xx_lpi2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2254,7 +2187,6 @@ struct i2c_master_s *s32k1xx_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - s32k1xx_lpi2c_sem_init(priv); s32k1xx_lpi2c_init(priv); #ifdef CONFIG_S32K1XX_LPI2C_DMA @@ -2331,9 +2263,6 @@ int s32k1xx_i2cbus_uninitialize(struct i2c_master_s *dev) s32k1xx_lpi2c_deinit(priv); - /* Release unused resources */ - - s32k1xx_lpi2c_sem_destroy(priv); return OK; } diff --git a/arch/arm/src/s32k1xx/s32k1xx_lpspi.c b/arch/arm/src/s32k1xx/s32k1xx_lpspi.c index 1fc9e2b412a8e..0cec7e82ae145 100644 --- a/arch/arm/src/s32k1xx/s32k1xx_lpspi.c +++ b/arch/arm/src/s32k1xx/s32k1xx_lpspi.c @@ -64,7 +64,7 @@ #include #include -#include +#include #include #include @@ -128,7 +128,7 @@ struct s32k1xx_lpspidev_s #ifdef CONFIG_S32K1XX_LPSPI_INTERRUPTS uint8_t spiirq; /* SPI IRQ number */ #endif - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ int8_t nbits; /* Width of word in bits */ @@ -951,11 +951,11 @@ static int s32k1xx_lpspi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1834,9 +1834,9 @@ static void s32k1xx_lpspi_bus_initialize(struct s32k1xx_lpspidev_s *priv) s32k1xx_lpspi_setmode((struct spi_dev_s *)priv, SPIDEV_MODE0); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Enable LPSPI */ diff --git a/arch/arm/src/s32k3xx/s32k3xx_edma.c b/arch/arm/src/s32k3xx/s32k3xx_edma.c index 0be0a0b8a9d52..669c551966a96 100644 --- a/arch/arm/src/s32k3xx/s32k3xx_edma.c +++ b/arch/arm/src/s32k3xx/s32k3xx_edma.c @@ -61,6 +61,7 @@ #include #include #include +#include #include #include "arm_internal.h" @@ -148,9 +149,9 @@ struct s32k3xx_dmach_s struct s32k3xx_edma_s { - /* These semaphores protect the DMA channel and descriptor tables */ + /* These mutex protect the DMA channel and descriptor tables */ - sem_t chsem; /* Protects channel table */ + mutex_t chlock; /* Protects channel table */ #if CONFIG_S32K3XX_EDMA_NTCD > 0 sem_t dsem; /* Supports wait for free descriptors */ #endif @@ -415,25 +416,6 @@ const struct peripheral_clock_config_s edma_clockconfig[] = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: s32k3xx_takechsem() and s32k3xx_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table for channel - * allocation. - * - ****************************************************************************/ - -static int s32k3xx_takechsem(void) -{ - return nxsem_wait_uninterruptible(&g_edma.chsem); -} - -static inline void s32k3xx_givechsem(void) -{ - nxsem_post(&g_edma.chsem); -} - /**************************************************************************** * Name: s32k3xx_takedsem() and s32k3xx_givedsem() * @@ -940,9 +922,9 @@ void weak_function arm_dma_initialize(void) g_edma.dmach[i].chan = i; } - /* Initialize semaphores */ + /* Initialize mutex & semaphore */ - nxsem_init(&g_edma.chsem, 0, 1); + nxmutex_init(&g_edma.chlock); #if CONFIG_S32K3XX_EDMA_NTCD > 0 nxsem_init(&g_edma.dsem, 0, CONFIG_S32K3XX_EDMA_NTCD); @@ -1037,7 +1019,7 @@ DMACH_HANDLE s32k3xx_dmach_alloc(uint16_t dmamux, uint8_t dchpri) /* Search for an available DMA channel */ dmach = NULL; - ret = s32k3xx_takechsem(); + ret = nxmutex_lock(&g_edma.chlock); if (ret < 0) { return NULL; @@ -1082,7 +1064,7 @@ DMACH_HANDLE s32k3xx_dmach_alloc(uint16_t dmamux, uint8_t dchpri) } } - s32k3xx_givechsem(); + nxmutex_unlock(&g_edma.chlock); /* Show the result of the allocation */ diff --git a/arch/arm/src/s32k3xx/s32k3xx_lpi2c.c b/arch/arm/src/s32k3xx/s32k3xx_lpi2c.c index af872fa7653f9..d56bbfad8f166 100644 --- a/arch/arm/src/s32k3xx/s32k3xx_lpi2c.c +++ b/arch/arm/src/s32k3xx/s32k3xx_lpi2c.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -193,7 +194,7 @@ struct s32k3xx_lpi2c_priv_s const struct s32k3xx_lpi2c_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 @@ -231,8 +232,6 @@ static inline void s32k3xx_lpi2c_putreg(struct s32k3xx_lpi2c_priv_s *priv, static inline void s32k3xx_lpi2c_modifyreg(struct s32k3xx_lpi2c_priv_s *priv, uint16_t offset, uint32_t clearbits, uint32_t setbits); -static inline int - s32k3xx_lpi2c_sem_wait(struct s32k3xx_lpi2c_priv_s *priv); #ifdef CONFIG_S32K3XX_I2C_DYNTIMEO static uint32_t s32k3xx_lpi2c_toticks(int msgc, struct i2c_msg_s *msgs); @@ -242,10 +241,6 @@ static inline int s32k3xx_lpi2c_sem_waitdone(struct s32k3xx_lpi2c_priv_s *priv); static inline void s32k3xx_lpi2c_sem_waitstop(struct s32k3xx_lpi2c_priv_s *priv); -static inline void s32k3xx_lpi2c_sem_post(struct s32k3xx_lpi2c_priv_s *priv); -static inline void s32k3xx_lpi2c_sem_init(struct s32k3xx_lpi2c_priv_s *priv); -static inline void - s32k3xx_lpi2c_sem_destroy(struct s32k3xx_lpi2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void s32k3xx_lpi2c_tracereset(struct s32k3xx_lpi2c_priv_s *priv); @@ -266,7 +261,7 @@ static inline void s32k3xx_lpi2c_sendstop(struct s32k3xx_lpi2c_priv_s *priv); static inline uint32_t s32k3xx_lpi2c_getstatus(struct s32k3xx_lpi2c_priv_s *priv); -static int s32k3xx_lpi2c_isr_process(struct s32k3xx_lpi2c_priv_s * priv); +static int s32k3xx_lpi2c_isr_process(struct s32k3xx_lpi2c_priv_s *priv); #ifndef CONFIG_I2C_POLLED static int s32k3xx_lpi2c_isr(int irq, void *context, void *arg); @@ -336,6 +331,10 @@ static struct s32k3xx_lpi2c_priv_s s32k3xx_lpi2c0_priv = .ops = &s32k3xx_lpi2c_ops, .config = &s32k3xx_lpi2c0_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, @@ -370,6 +369,10 @@ static struct s32k3xx_lpi2c_priv_s s32k3xx_lpi2c1_priv = .ops = &s32k3xx_lpi2c_ops, .config = &s32k3xx_lpi2c1_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, @@ -427,20 +430,6 @@ static inline void modifyreg32(priv->config->base + offset, clearbits, setbits); } -/**************************************************************************** - * Name: s32k3xx_lpi2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. May be interrupted by - * a signal. - * - ****************************************************************************/ - -static inline int s32k3xx_lpi2c_sem_wait(struct s32k3xx_lpi2c_priv_s *priv) -{ - return nxsem_wait(&priv->sem_excl); -} - /**************************************************************************** * Name: s32k3xx_lpi2c_toticks * @@ -712,58 +701,6 @@ static inline void i2cinfo("Timeout with Status Register: %" PRIx32 "\n", regval); } -/**************************************************************************** - * Name: s32k3xx_lpi2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void s32k3xx_lpi2c_sem_post(struct s32k3xx_lpi2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: s32k3xx_lpi2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void s32k3xx_lpi2c_sem_init(struct s32k3xx_lpi2c_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: s32k3xx_lpi2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void - s32k3xx_lpi2c_sem_destroy(struct s32k3xx_lpi2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - /**************************************************************************** * Name: s32k3xx_lpi2c_trace* * @@ -1491,7 +1428,7 @@ static int s32k3xx_lpi2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = s32k3xx_lpi2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1577,7 +1514,7 @@ static int s32k3xx_lpi2c_transfer(struct i2c_master_s *dev, priv->dcnt = 0; priv->ptr = NULL; - s32k3xx_lpi2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -1614,7 +1551,7 @@ static int s32k3xx_lpi2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - ret = s32k3xx_lpi2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1717,7 +1654,7 @@ static int s32k3xx_lpi2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - s32k3xx_lpi2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -1767,7 +1704,6 @@ struct i2c_master_s *s32k3xx_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - s32k3xx_lpi2c_sem_init(priv); s32k3xx_lpi2c_init(priv); } @@ -1812,9 +1748,6 @@ int s32k3xx_i2cbus_uninitialize(struct i2c_master_s *dev) s32k3xx_lpi2c_deinit(priv); - /* Release unused resources */ - - s32k3xx_lpi2c_sem_destroy(priv); return OK; } diff --git a/arch/arm/src/s32k3xx/s32k3xx_lpspi.c b/arch/arm/src/s32k3xx/s32k3xx_lpspi.c index 39db621a50a34..ca730b5feebda 100644 --- a/arch/arm/src/s32k3xx/s32k3xx_lpspi.c +++ b/arch/arm/src/s32k3xx/s32k3xx_lpspi.c @@ -60,7 +60,7 @@ #include #include -#include +#include #include #include @@ -117,7 +117,7 @@ struct s32k3xx_lpspidev_s #ifdef CONFIG_S32K3XX_LPSPI_INTERRUPTS uint8_t spiirq; /* SPI IRQ number */ #endif - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ int8_t nbits; /* Width of word in bits */ @@ -1052,11 +1052,11 @@ static int s32k3xx_lpspi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2032,9 +2032,9 @@ static void s32k3xx_lpspi_bus_initialize(struct s32k3xx_lpspidev_s *priv) s32k3xx_lpspi_setmode((struct spi_dev_s *)priv, SPIDEV_MODE0); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Enable LPSPI */ diff --git a/arch/arm/src/s32k3xx/s32k3xx_qspi.c b/arch/arm/src/s32k3xx/s32k3xx_qspi.c index 0456cd9781516..d810567da2fc0 100644 --- a/arch/arm/src/s32k3xx/s32k3xx_qspi.c +++ b/arch/arm/src/s32k3xx/s32k3xx_qspi.c @@ -44,7 +44,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -122,7 +122,7 @@ struct s32k3xx_qspidev_s uint8_t nbits; /* Width of word in bits (8 to 32) */ uint8_t intf; /* QSPI controller number (0) */ bool initialized; /* TRUE: Controller has been initialized */ - sem_t exclsem; /* Assures mutually exclusive access to QSPI */ + mutex_t lock; /* Assures mutually exclusive access to QSPI */ bool memmap; /* TRUE: Controller is in memory mapped mode */ #ifdef CONFIG_S32K3XX_QSPI_INTERRUPTS xcpt_t handler; /* Interrupt handler */ @@ -1094,11 +1094,11 @@ static int qspi_lock(struct qspi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1791,7 +1791,7 @@ struct qspi_dev_s *s32k3xx_qspi_initialize(int intf) * access to the QSPI registers. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_S32K3XX_QSPI_INTERRUPTS /* Attach the interrupt handler */ @@ -1862,7 +1862,7 @@ struct qspi_dev_s *s32k3xx_qspi_initialize(int intf) return &priv->qspi; - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); return NULL; } diff --git a/arch/arm/src/sam34/sam4cm_tc.c b/arch/arm/src/sam34/sam4cm_tc.c index 634fac42a7f03..0e30bc85ad5c7 100644 --- a/arch/arm/src/sam34/sam4cm_tc.c +++ b/arch/arm/src/sam34/sam4cm_tc.c @@ -59,7 +59,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -110,7 +110,7 @@ struct sam_chan_s tc_handler_t handler; /* Attached interrupt handler */ void *arg; /* Interrupt handler argument */ uint8_t chan; /* Channel number (0, 1, or 2 OR 3, 4, or 5) */ - sem_t exclsem; /* Assures mutually exclusive access to TC */ + mutex_t lock; /* Assures mutually exclusive access to TC */ bool initialized; /* True: channel data has been initialized */ bool inuse; /* True: channel is in use */ @@ -128,11 +128,6 @@ struct sam_chan_s * Private Function Prototypes ****************************************************************************/ -/* Low-level helpers ********************************************************/ - -static int sam_takesem(struct sam_chan_s *chan); -#define sam_givesem(chan) (nxsem_post(&chan->exclsem)) - #ifdef CONFIG_SAM34_TC_REGDEBUG static void sam_regdump(struct sam_chan_s *chan, const char *msg); static bool sam_checkreg(struct sam_chan_s *chan, bool wr, uint32_t regaddr, @@ -350,26 +345,6 @@ static const uint8_t g_regoffset[TC_NREGISTERS] = * Low-level Helpers ****************************************************************************/ -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int sam_takesem(struct sam_chan_s *chan) -{ - return nxsem_wait_uninterruptible(&chan->exclsem); -} - /**************************************************************************** * Name: sam_regdump * @@ -756,7 +731,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) tmrerr("ERROR: Initializing TC%d\n", chconfig->chan); memset(chan, 0, sizeof(struct sam_chan_s)); - nxsem_init(&chan->exclsem, 0, 1); + nxmutex_init(&chan->lock); chan->base = chconfig->base; chan->pid = chconfig->pid; chan->irq = chconfig->irq; @@ -805,7 +780,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) /* Get exclusive access to the timer/count data structure */ - ret = sam_takesem(chan); + ret = nxmutex_lock(&chan->lock); if (ret < 0) { leave_critical_section(flags); @@ -821,7 +796,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) /* No.. return a failure */ tmrerr("ERROR: Channel %d is in-use\n", channel); - sam_givesem(chan); + nxmutex_unlock(&chan->lock); return NULL; } @@ -887,7 +862,7 @@ TC_HANDLE sam_tc_allocate(int channel, int mode) sam_chan_putreg(chan, SAM_TC_CMR_OFFSET, mode); sam_regdump(chan, "Allocated"); - sam_givesem(chan); + nxmutex_unlock(&chan->lock); } /* Return an opaque reference to the channel */ diff --git a/arch/arm/src/sam34/sam_aes.c b/arch/arm/src/sam34/sam_aes.c index ac63145c2c74c..c5bb2f2d6fbcf 100644 --- a/arch/arm/src/sam34/sam_aes.c +++ b/arch/arm/src/sam34/sam_aes.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -59,8 +59,8 @@ * Private Data ****************************************************************************/ -static sem_t g_samaes_lock; -static bool g_samaes_initdone = false; +static mutex_t g_samaes_lock; +static bool g_samaes_initdone = false; /**************************************************************************** * Public Data @@ -70,16 +70,6 @@ static bool g_samaes_initdone = false; * Private Functions ****************************************************************************/ -static void samaes_lock(void) -{ - nxsem_wait(&g_samaes_lock); -} - -static void samaes_unlock(void) -{ - nxsem_post(&g_samaes_lock); -} - static void samaes_memcpy(void *out, const void *in, size_t size) { size_t i; @@ -169,7 +159,7 @@ static int samaes_setup_mr(uint32_t keysize, int mode, int encrypt) static int samaes_initialize(void) { - nxsem_init(&g_samaes_lock, 0, 1); + nxmutex_init(&g_samaes_lock); sam_aes_enableclk(); putreg32(AES_CR_SWRST, SAM_AES_CR); return OK; @@ -197,12 +187,12 @@ int aes_cypher(void *out, const void *in, size_t size, return -EINVAL; } - samaes_lock(); + nxmutex_lock(&g_samaes_lock); ret = samaes_setup_mr(keysize, mode & AES_MODE_MASK, encrypt); if (ret < 0) { - samaes_unlock(); + nxmutex_unlock(&g_samaes_lock); return ret; } @@ -232,6 +222,6 @@ int aes_cypher(void *out, const void *in, size_t size, size -= AES_BLOCK_SIZE; } - samaes_unlock(); + nxmutex_unlock(&g_samaes_lock); return ret; } diff --git a/arch/arm/src/sam34/sam_dmac.c b/arch/arm/src/sam34/sam_dmac.c index dc87ed1781f8d..109d22bae79b5 100644 --- a/arch/arm/src/sam34/sam_dmac.c +++ b/arch/arm/src/sam34/sam_dmac.c @@ -33,6 +33,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -101,9 +102,9 @@ struct sam_dma_s * Private Data ****************************************************************************/ -/* These semaphores protect the DMA channel and descriptor tables */ +/* These mutex protect the DMA channel and descriptor tables */ -static sem_t g_chsem; +static mutex_t g_chlock; static sem_t g_dsem; /* CTRLA field lookups */ @@ -245,46 +246,6 @@ static struct sam_dma_s g_dma[SAM34_NDMACHAN] = #endif }; -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: sam_takechsem() and sam_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table - * - ****************************************************************************/ - -static int sam_takechsem(void) -{ - return nxsem_wait_uninterruptible(&g_chsem); -} - -static inline void sam_givechsem(void) -{ - nxsem_post(&g_chsem); -} - -/**************************************************************************** - * Name: sam_takedsem() and sam_givedsem() - * - * Description: - * Used to wait for availability of descriptors in the descriptor table. - * - ****************************************************************************/ - -static void sam_takedsem(void) -{ - nxsem_wait_uninterruptible(&g_dsem); -} - -static inline void sam_givedsem(void) -{ - nxsem_post(&g_dsem); -} - /**************************************************************************** * Name: sam_fifosize * @@ -882,7 +843,7 @@ sam_allocdesc(struct sam_dma_s *dmach, struct dma_linklist_s *prev, * there is at least one free descriptor in the table and it is ours. */ - sam_takedsem(); + nxsem_wait_uninterruptible(&g_dsem); /* Examine each link list entry to find an available one -- i.e., one * with src == 0. That src field is set to zero by the DMA transfer @@ -890,7 +851,7 @@ sam_allocdesc(struct sam_dma_s *dmach, struct dma_linklist_s *prev, * that is an atomic operation. */ - ret = sam_takechsem(); + ret = nxmutex_lock(&g_chlock); if (ret < 0) { return NULL; @@ -957,7 +918,7 @@ sam_allocdesc(struct sam_dma_s *dmach, struct dma_linklist_s *prev, * search loop should always be successful. */ - sam_givechsem(); + nxmutex_unlock(&g_chlock); DEBUGASSERT(desc != NULL); } @@ -994,7 +955,7 @@ static void sam_freelinklist(struct sam_dma_s *dmach) next = (struct dma_linklist_s *)desc->next; DEBUGASSERT(desc->src != 0); memset(desc, 0, sizeof(struct dma_linklist_s)); - sam_givedsem(); + nxsem_post(&g_dsem); desc = next; } } @@ -1380,9 +1341,9 @@ void weak_function arm_dma_initialize(void) putreg32(DMAC_EN_ENABLE, SAM_DMAC_EN); - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&g_chsem, 0, 1); + nxmutex_init(&g_chlock); nxsem_init(&g_dsem, 0, CONFIG_SAM34_NLLDESC); } @@ -1422,7 +1383,7 @@ DMA_HANDLE sam_dmachannel(uint32_t chflags) */ dmach = NULL; - ret = sam_takechsem(); + ret = nxmutex_lock(&g_chlock); if (ret < 0) { return NULL; @@ -1463,7 +1424,7 @@ DMA_HANDLE sam_dmachannel(uint32_t chflags) } } - sam_givechsem(); + nxmutex_unlock(&g_chlock); dmainfo("chflags: %08x returning dmach: %p\n", (int)chflags, dmach); return (DMA_HANDLE)dmach; diff --git a/arch/arm/src/sam34/sam_hsmci.c b/arch/arm/src/sam34/sam_hsmci.c index 7e7e4b7a69672..e6cedd9723abb 100644 --- a/arch/arm/src/sam34/sam_hsmci.c +++ b/arch/arm/src/sam34/sam_hsmci.c @@ -395,9 +395,6 @@ struct sam_xfrregs_s /* Low-level helpers ********************************************************/ -static int sam_takesem(struct sam_dev_s *priv); -#define sam_givesem(priv) (nxsem_post(&priv->waitsem)) - static void sam_configwaitints(struct sam_dev_s *priv, uint32_t waitmask, sdio_eventset_t waitevents); static void sam_disablewaitints(struct sam_dev_s *priv, @@ -564,27 +561,6 @@ static bool g_cmdinitialized; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int sam_takesem(struct sam_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: sam_configwaitints * @@ -1135,7 +1111,7 @@ static void sam_endwait(struct sam_dev_s *priv, sdio_eventset_t wkupevent) /* Wake up the waiting thread */ - sam_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2361,7 +2337,7 @@ static sdio_eventset_t sam_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = sam_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started), diff --git a/arch/arm/src/sam34/sam_spi.c b/arch/arm/src/sam34/sam_spi.c index 71d6147668363..15bcaf20dbe9c 100644 --- a/arch/arm/src/sam34/sam_spi.c +++ b/arch/arm/src/sam34/sam_spi.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -186,7 +186,7 @@ typedef void (*select_t)(uint32_t devid, bool selected); struct sam_spidev_s { uint32_t base; /* SPI controller register base address */ - sem_t spisem; /* Assures mutually exclusive access to SPI */ + mutex_t spilock; /* Assures mutually exclusive access to SPI */ select_t select; /* SPI select call-out */ bool initialized; /* TRUE: Controller has been initialized */ #ifdef CONFIG_SAM34_SPI_DMA @@ -872,11 +872,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&spi->spisem); + ret = nxmutex_lock(&spi->spilock); } else { - ret = nxsem_post(&spi->spisem); + ret = nxmutex_unlock(&spi->spilock); } return ret; @@ -1857,11 +1857,11 @@ struct spi_dev_s *sam_spibus_initialize(int port) spi_getreg(spi, SAM_SPI_SR_OFFSET); spi_getreg(spi, SAM_SPI_RDR_OFFSET); - /* Initialize the SPI semaphore that enforces mutually exclusive + /* Initialize the SPI mutex that enforces mutually exclusive * access to the SPI registers. */ - nxsem_init(&spi->spisem, 0, 1); + nxmutex_init(&spi->spilock); spi->initialized = true; #ifdef CONFIG_SAM34_SPI_DMA diff --git a/arch/arm/src/sam34/sam_twi.c b/arch/arm/src/sam34/sam_twi.c index 5c4b53d90546d..7cddc117f08b3 100644 --- a/arch/arm/src/sam34/sam_twi.c +++ b/arch/arm/src/sam34/sam_twi.c @@ -58,6 +58,7 @@ #include #include +#include #include #include @@ -120,7 +121,7 @@ struct twi_dev_s uint8_t twi; /* TWI peripheral number (for debug output) */ uint8_t pid; /* TWI peripheral ID */ - sem_t exclsem; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t waitsem; /* Wait for TWI transfer completion */ struct wdog_s timeout; /* Watchdog to recover from bus hangs */ volatile int result; /* The result of the transfer */ @@ -140,11 +141,6 @@ struct twi_dev_s * Private Function Prototypes ****************************************************************************/ -/* Low-level helper functions */ - -static int twi_takesem(sem_t *sem); -#define twi_givesem(sem) (nxsem_post(sem)) - #ifdef CONFIG_SAM34_TWI_REGDEBUG static bool twi_checkreg(struct twi_dev_s *priv, bool wr, uint32_t value, uintptr_t address); @@ -211,26 +207,6 @@ static const struct i2c_ops_s g_twiops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: twi_takesem - * - * Description: - * Take the wait semaphore. May be interrupted by a signal. - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * EINVAL - Invalid attempt to get the semaphore - * EINTR - The wait was interrupted by the receipt of a signal. - * - ****************************************************************************/ - -static int twi_takesem(sem_t *sem) -{ - return nxsem_wait(sem); -} - /**************************************************************************** * Name: twi_checkreg * @@ -385,7 +361,7 @@ static int twi_wait(struct twi_dev_s *priv) do { i2cinfo("TWI%d Waiting...\n", priv->twi); - ret = twi_takesem(&priv->waitsem); + ret = nxsem_wait(&priv->waitsem); i2cinfo("TWI%d Awakened with result: %d\n", priv->twi, priv->result); if (ret < 0) @@ -424,7 +400,7 @@ static void twi_wakeup(struct twi_dev_s *priv, int result) /* Wake up the waiting thread with the result of the transfer */ priv->result = result; - twi_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -692,7 +668,7 @@ static int twi_transfer(struct i2c_master_s *dev, /* Get exclusive access to the device */ - ret = twi_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -730,7 +706,7 @@ static int twi_transfer(struct i2c_master_s *dev, } leave_critical_section(flags); - twi_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -974,9 +950,9 @@ struct i2c_master_s *sam_i2cbus_initialize(int bus) priv->dev.ops = &g_twiops; - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->waitsem, 0, 0); /* The waitsem semaphore is used for signaling and, hence, should not have @@ -1021,7 +997,7 @@ int sam_i2cbus_uninitialize(struct i2c_master_s * dev) /* Reset data structures */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->waitsem); /* Cancel the watchdog timer */ diff --git a/arch/arm/src/sama5/sam_adc.c b/arch/arm/src/sama5/sam_adc.c index 9a871a03cf3bd..965377194f61c 100644 --- a/arch/arm/src/sama5/sam_adc.c +++ b/arch/arm/src/sama5/sam_adc.c @@ -66,7 +66,7 @@ #include #include #include -#include +#include #include "arm_internal.h" #include "chip.h" @@ -384,7 +384,7 @@ struct sam_adc_s { const struct adc_callback_s *cb; - sem_t exclsem; /* Supports exclusive access to the ADC interface */ + mutex_t lock; /* Supports exclusive access to the ADC interface */ bool initialized; /* The ADC driver is already initialized */ uint32_t frequency; /* ADC clock frequency */ @@ -2049,7 +2049,7 @@ struct adc_dev_s *sam_adc_initialize(void) /* Initialize the private ADC device data structure */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->cb = NULL; #ifdef CONFIG_SAMA5_ADC_DMA @@ -2172,7 +2172,7 @@ struct adc_dev_s *sam_adc_initialize(void) int sam_adc_lock(struct sam_adc_s *priv) { ainfo("Locking\n"); - return nxsem_wait_uninterruptible(&priv->exclsem); + return nxmutex_lock(&priv->lock); } /**************************************************************************** @@ -2186,7 +2186,7 @@ int sam_adc_lock(struct sam_adc_s *priv) void sam_adc_unlock(struct sam_adc_s *priv) { ainfo("Unlocking\n"); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /**************************************************************************** diff --git a/arch/arm/src/sama5/sam_can.c b/arch/arm/src/sama5/sam_can.c index 171ea7d1fe4ba..4f2b1c58c33ac 100644 --- a/arch/arm/src/sama5/sam_can.c +++ b/arch/arm/src/sama5/sam_can.c @@ -62,7 +62,7 @@ #include #include #include -#include +#include #include "arm_internal.h" #include "hardware/sam_pinmap.h" @@ -177,7 +177,7 @@ struct sam_can_s uint8_t rxmbset; /* The set of mailboxes configured for receive */ volatile uint8_t txmbset; /* The set of mailboxes actively transmitting */ bool txdisabled; /* TRUE: Keep TX interrupts disabled */ - sem_t exclsem; /* Enforces mutually exclusive access */ + mutex_t lock; /* Enforces mutually exclusive access */ uint32_t frequency; /* CAN clock frequency */ #ifdef CONFIG_SAMA5_CAN_REGDEBUG @@ -205,12 +205,6 @@ static void can_dumpmbregs(struct sam_can_s *priv, const char *msg); # define can_dumpmbregs(priv,msg) #endif -/* Semaphore helpers */ - -static int can_semtake(struct sam_can_s *priv); -static int can_semtake_noncancelable(struct sam_can_s *priv); -#define can_semgive(priv) nxsem_post(&priv->exclsem) - /* Mailboxes */ static int can_recvsetup(struct sam_can_s *priv); @@ -554,60 +548,6 @@ static void can_dumpmbregs(struct sam_can_s *priv, const char *msg) } #endif -/**************************************************************************** - * Name: can_semtake - * - * Description: - * Take a semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the CAN peripheral state - * - * Returned Value: - * Normally success (OK) is returned, but the error -ECANCELED may be - * return in the event that task has been canceled. - * - ****************************************************************************/ - -static int can_semtake(struct sam_can_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -/**************************************************************************** - * Name: can_semtake_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int can_semtake_noncancelable(struct sam_can_s *priv) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(&priv->exclsem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: can_mballoc * @@ -815,7 +755,7 @@ static void can_reset(struct can_dev_s *dev) /* Get exclusive access to the CAN peripheral */ - can_semtake_noncancelable(); + nxmutex_lock(&priv->lock); /* Disable all interrupts */ @@ -835,7 +775,7 @@ static void can_reset(struct can_dev_s *dev) /* Disable the CAN controller */ can_putreg(priv, SAM_CAN_MR_OFFSET, 0); - can_semgive(priv); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -871,7 +811,7 @@ static int can_setup(struct can_dev_s *dev) /* Get exclusive access to the CAN peripheral */ - ret = can_semtake(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -922,7 +862,7 @@ static int can_setup(struct can_dev_s *dev) /* Enable the interrupts at the AIC. */ up_enable_irq(config->pid); - can_semgive(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -956,7 +896,7 @@ static void can_shutdown(struct can_dev_s *dev) /* Get exclusive access to the CAN peripheral */ - can_semtake_noncancelable(priv); + nxmutex_lock(&priv->lock); /* Disable the CAN interrupts */ @@ -969,7 +909,7 @@ static void can_shutdown(struct can_dev_s *dev) /* And reset the hardware */ can_reset(dev); - can_semgive(priv); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -1028,7 +968,7 @@ static void can_txint(struct can_dev_s *dev, bool enable) /* Get exclusive access to the CAN peripheral */ - can_semtake_noncancelable(priv); + nxmutex_lock(&priv->lock); /* Support disabling interrupts on any mailboxes that are actively * transmitting (txmbset); also suppress enabling new TX mailbox until @@ -1046,7 +986,7 @@ static void can_txint(struct can_dev_s *dev, bool enable) priv->txdisabled = true; } - can_semgive(priv); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -1131,7 +1071,7 @@ static int can_send(struct can_dev_s *dev, struct can_msg_s *msg) /* Get exclusive access to the CAN peripheral */ - ret = can_semtake(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1228,7 +1168,7 @@ static int can_send(struct can_dev_s *dev, struct can_msg_s *msg) } can_dumpmbregs(priv, "After send"); - can_semgive(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -1253,7 +1193,7 @@ static bool can_txready(struct can_dev_s *dev) /* Get exclusive access to the CAN peripheral */ - ret = can_semtake(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return false; @@ -1263,7 +1203,7 @@ static bool can_txready(struct can_dev_s *dev) txready = ((priv->rxmbset | priv->txmbset) != CAN_ALL_MAILBOXES); - can_semgive(priv); + nxmutex_unlock(&priv->lock); return txready; } @@ -2008,7 +1948,7 @@ struct can_dev_s *sam_caninitialize(int port) priv->freemb = CAN_ALL_MAILBOXES; priv->initialized = true; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); dev->cd_ops = &g_canops; dev->cd_priv = (void *)priv; diff --git a/arch/arm/src/sama5/sam_dmac.c b/arch/arm/src/sama5/sam_dmac.c index ca9953a0dfee4..fc972665ca821 100644 --- a/arch/arm/src/sama5/sam_dmac.c +++ b/arch/arm/src/sama5/sam_dmac.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include "arm_internal.h" #include "sched/sched.h" @@ -130,9 +130,9 @@ struct sam_dmach_s struct sam_dmac_s { - /* These semaphores protect the DMA channel and descriptor tables */ + /* These mutex protect the DMA channel and descriptor tables */ - sem_t chsem; /* Protects channel table */ + mutex_t chlock; /* Protects channel table */ sem_t dsem; /* Protects descriptor table */ uint32_t base; /* DMA register channel base address */ @@ -463,24 +463,6 @@ static struct sam_dmac_s g_dmac1 = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takechsem() and sam_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table - * - ****************************************************************************/ - -static int sam_takechsem(struct sam_dmac_s *dmac) -{ - return nxsem_wait_uninterruptible(&dmac->chsem); -} - -static inline void sam_givechsem(struct sam_dmac_s *dmac) -{ - nxsem_post(&dmac->chsem); -} - /**************************************************************************** * Name: sam_takedsem() and sam_givedsem() * @@ -1364,7 +1346,7 @@ sam_allocdesc(struct sam_dmach_s *dmach, struct dma_linklist_s *prev, * because that is an atomic operation. */ - ret = sam_takechsem(dmac); + ret = nxmutex_lock(&dmac->chlock); if (ret < 0) { sam_givedsem(dmac); @@ -1439,11 +1421,11 @@ sam_allocdesc(struct sam_dmach_s *dmach, struct dma_linklist_s *prev, } } - /* Because we hold a count from the counting semaphore, the above + /* Because we hold a count from the counting mutex, the above * search loop should always be successful. */ - sam_givechsem(dmac); + nxmutex_unlock(&dmac->chlock); DEBUGASSERT(desc != NULL); } @@ -1873,9 +1855,9 @@ void sam_dmainitialize(struct sam_dmac_s *dmac) sam_putdmac(dmac, DMAC_EN_ENABLE, SAM_DMAC_EN_OFFSET); - /* Initialize semaphores */ + /* Initialize muttex & semaphores */ - nxsem_init(&dmac->chsem, 0, 1); + nxmutex_init(&dmac->chlock); nxsem_init(&dmac->dsem, 0, SAM_NDMACHAN); } @@ -1990,7 +1972,7 @@ DMA_HANDLE sam_dmachannel(uint8_t dmacno, uint32_t chflags) */ dmach = NULL; - ret = sam_takechsem(dmac); + ret = nxmutex_lock(&dmac->chlock); if (ret < 0) { return NULL; @@ -2023,7 +2005,7 @@ DMA_HANDLE sam_dmachannel(uint8_t dmacno, uint32_t chflags) } } - sam_givechsem(dmac); + nxmutex_unlock(&dmac->chlock); /* Show the result of the allocation */ diff --git a/arch/arm/src/sama5/sam_ehci.c b/arch/arm/src/sama5/sam_ehci.c index 637b9b1429ad1..17534dbeacc8f 100644 --- a/arch/arm/src/sama5/sam_ehci.c +++ b/arch/arm/src/sama5/sam_ehci.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -243,7 +244,7 @@ struct sam_ehci_s { volatile bool pscwait; /* TRUE: Thread is waiting for port status change event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for port status change events */ struct sam_epinfo_s ep0; /* Endpoint 0 */ @@ -297,12 +298,6 @@ static inline void sam_putreg(uint32_t regval, volatile uint32_t *regaddr); static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, unsigned int delay); -/* Semaphores ***************************************************************/ - -static int sam_takesem(sem_t *sem); -static int sam_takesem_noncancelable(sem_t *sem); -#define sam_givesem(s) nxsem_post(s); - /* Allocators ***************************************************************/ static struct sam_qh_s *sam_qh_alloc(void); @@ -799,61 +794,13 @@ static int ehci_wait_usbsts(uint32_t maskbits, uint32_t donebits, return (regval == donebits) ? OK : -ETIMEDOUT; } -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int sam_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: sam_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int sam_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: sam_qh_alloc * * Description: * Allocate a Queue Head (QH) structure by removing it from the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -879,7 +826,7 @@ static struct sam_qh_s *sam_qh_alloc(void) * Description: * Free a Queue Head (QH) structure by returning it to the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -900,7 +847,7 @@ static void sam_qh_free(struct sam_qh_s *qh) * Allocate a Queue Element Transfer Descriptor (qTD) by removing it from * the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -927,7 +874,7 @@ static struct sam_qtd_s *sam_qtd_alloc(void) * Free a Queue Element Transfer Descriptor (qTD) by returning it to the * free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -1389,7 +1336,7 @@ static inline uint8_t sam_ehci_speed(uint8_t usbspeed) * this to minimize race conditions. This logic would have to be expanded * if we want to have more than one packet in flight at a time! * - * Assumption: The caller holds tex EHCI exclsem + * Assumption: The caller holds tex EHCI lock * ****************************************************************************/ @@ -1435,7 +1382,7 @@ static int sam_ioc_setup(struct sam_rhport_s *rhport, * Description: * Wait for the IOC event. * - * Assumption: The caller does *NOT* hold the EHCI exclsem. That would + * Assumption: The caller does *NOT* hold the EHCI lock. That would * cause a deadlock when the bottom-half, worker thread needs to take the * semaphore. * @@ -1451,7 +1398,7 @@ static int sam_ioc_wait(struct sam_epinfo_s *epinfo) while (epinfo->iocwait) { - ret = sam_takesem(&epinfo->iocsem); + ret = nxsem_wait_uninterruptible(&epinfo->iocsem); if (ret < 0) { break; @@ -1467,7 +1414,7 @@ static int sam_ioc_wait(struct sam_epinfo_s *epinfo) * Description: * Add a new, ready-to-go QH w/attached qTDs to the asynchronous queue. * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -1916,7 +1863,7 @@ static struct sam_qtd_s *sam_qtd_statusphase(uint32_t tokenbits) * This is a blocking function; it will not return until the control * transfer has completed. * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2199,7 +2146,7 @@ static int sam_async_setup(struct sam_rhport_s *rhport, * frame list), followed by shorter poll rates, with queue heads with a * poll rate of one, on the very end." * - * Assumption: The caller holds the EHCI exclsem. + * Assumption: The caller holds the EHCI lock. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is return on @@ -2303,8 +2250,8 @@ static int sam_intr_setup(struct sam_rhport_s *rhport, * Description: * Wait for an IN or OUT transfer to complete. * - * Assumption: The caller holds the EHCI exclsem. The caller must be aware - * that the EHCI exclsem will released while waiting for the transfer to + * Assumption: The caller holds the EHCI lock. The caller must be aware + * that the EHCI lock will released while waiting for the transfer to * complete, but will be re-acquired when before returning. The state of * EHCI resources could be very different upon return. * @@ -2327,12 +2274,12 @@ static ssize_t sam_transfer_wait(struct sam_epinfo_s *epinfo) * * REVISIT: Is this safe? NO. This is a bug and needs rethinking. * We need to lock all of the port-resources (not EHCI common) until - * the transfer is complete. But we can't use the common EHCI exclsem + * the transfer is complete. But we can't use the common EHCI lock * or we will deadlock while waiting (because the working thread that - * wakes this thread up needs the exclsem). + * wakes this thread up needs the lock). */ #warning REVISIT - sam_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Wait for the IOC completion event */ @@ -2342,7 +2289,7 @@ static ssize_t sam_transfer_wait(struct sam_epinfo_s *epinfo) * this upon return. */ - ret2 = sam_takesem_noncancelable(&g_ehci.exclsem); + ret2 = nxmutex_lock(&g_ehci.lock); if (ret2 < 0) { ret = ret2; @@ -2366,7 +2313,7 @@ static ssize_t sam_transfer_wait(struct sam_epinfo_s *epinfo) } #endif - /* Did sam_ioc_wait() or sam_takesem_noncancelable() report an error? */ + /* Did sam_ioc_wait() or nxmutex_lock report an error? */ if (ret < 0) { @@ -2671,7 +2618,7 @@ static int sam_qh_ioccheck(struct sam_qh_s *qh, uint32_t **bp, void *arg) { /* Yes... wake it up */ - sam_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); epinfo->iocwait = 0; } @@ -2826,7 +2773,7 @@ static int sam_qh_cancel(struct sam_qh_s *qh, uint32_t **bp, void *arg) * detected (actual number of bytes received was less than the expected * number of bytes)." * - * Assumptions: The caller holds the EHCI exclsem + * Assumptions: The caller holds the EHCI lock * ****************************************************************************/ @@ -2960,7 +2907,7 @@ static inline void sam_portsc_bottomhalf(void) if (g_ehci.pscwait) { - sam_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -2999,7 +2946,7 @@ static inline void sam_portsc_bottomhalf(void) if (g_ehci.pscwait) { - sam_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); g_ehci.pscwait = false; } } @@ -3076,7 +3023,7 @@ static void sam_ehci_bottomhalf(void *arg) * real option (other than to reschedule and delay). */ - sam_takesem_noncancelable(&g_ehci.exclsem); + nxmutex_lock(&g_ehci.lock); /* Handle all unmasked interrupt sources */ @@ -3186,7 +3133,7 @@ static void sam_ehci_bottomhalf(void *arg) /* We are done with the EHCI structures */ - sam_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); /* Re-enable relevant EHCI interrupts. Interrupts should still be enabled * at the level of the AIC. @@ -3372,7 +3319,7 @@ static int sam_wait(struct usbhost_connection_s *conn, */ g_ehci.pscwait = true; - ret = sam_takesem(&g_ehci.pscsem); + ret = nxsem_wait_uninterruptible(&g_ehci.pscsem); if (ret < 0) { return ret; @@ -3714,7 +3661,7 @@ static int sam_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the EHCI data structures. */ - ret = sam_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret >= 0) { /* Remember the new device address and max packet size */ @@ -3723,7 +3670,7 @@ static int sam_ep0configure(struct usbhost_driver_s *drvr, epinfo->speed = speed; epinfo->maxpacket = maxpacketsize; - sam_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); } return ret; @@ -4084,7 +4031,7 @@ static int sam_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * structures. */ - ret = sam_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4096,7 +4043,7 @@ static int sam_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Now initiate the transfer */ @@ -4111,13 +4058,13 @@ static int sam_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* And wait for the transfer to complete */ nbytes = sam_transfer_wait(ep0info); - sam_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes >= 0 ? OK : (int)nbytes; errout_with_iocwait: ep0info->iocwait = false; -errout_with_sem: - sam_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4186,7 +4133,7 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, * structures. */ - ret = sam_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return (ssize_t)ret; @@ -4198,7 +4145,7 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4237,13 +4184,13 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, /* Then wait for the transfer to complete */ nbytes = sam_transfer_wait(epinfo); - sam_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return nbytes; errout_with_iocwait: epinfo->iocwait = false; -errout_with_sem: - sam_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return (ssize_t)ret; } @@ -4298,7 +4245,7 @@ static int sam_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * structures. */ - ret = sam_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return ret; @@ -4310,7 +4257,7 @@ static int sam_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, if (ret != OK) { usbhost_trace1(EHCI_TRACE1_DEVDISCONNECTED, -ret); - goto errout_with_sem; + goto errout_with_lock; } /* Initiate the transfer */ @@ -4347,14 +4294,14 @@ static int sam_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* The transfer is in progress */ - sam_givesem(&g_ehci.exclsem); + nxmutex_unlock(&g_ehci.lock); return OK; errout_with_callback: epinfo->callback = NULL; epinfo->arg = NULL; -errout_with_sem: - sam_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4402,7 +4349,7 @@ static int sam_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * interrupt level. */ - ret = sam_takesem(&g_ehci.exclsem); + ret = nxmutex_lock(&g_ehci.lock); if (ret < 0) { return (ssize_t)ret; @@ -4437,7 +4384,7 @@ static int sam_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #endif { ret = OK; - goto errout_with_sem; + goto errout_with_lock; } /* Handle the cancellation according to the type of the transfer */ @@ -4500,7 +4447,7 @@ static int sam_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) default: usbhost_trace1(EHCI_TRACE1_BADXFRTYPE, epinfo->xfrtype); ret = -ENOSYS; - goto errout_with_sem; + goto errout_with_lock; } /* Find and remove the QH. There are four possibilities: @@ -4530,7 +4477,7 @@ static int sam_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Yes... wake it up */ DEBUGASSERT(callback == NULL); - sam_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); } /* No.. Is there a pending asynchronous transfer? */ @@ -4545,11 +4492,11 @@ static int sam_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) #else /* Wake up the waiting thread */ - sam_givesem(&epinfo->iocsem); + nxsem_post(&epinfo->iocsem); #endif -errout_with_sem: - sam_givesem(&g_ehci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ehci.lock); return ret; } @@ -4596,7 +4543,7 @@ static int sam_connect(struct usbhost_driver_s *drvr, if (g_ehci.pscwait) { g_ehci.pscwait = false; - sam_givesem(&g_ehci.pscsem); + nxsem_post(&g_ehci.pscsem); } leave_critical_section(flags); @@ -4881,7 +4828,7 @@ struct usbhost_connection_s *sam_ehci_initialize(int controller) /* Initialize the EHCI state data structure */ - nxsem_init(&g_ehci.exclsem, 0, 1); + nxmutex_init(&g_ehci.lock); nxsem_init(&g_ehci.pscsem, 0, 0); /* The pscsem semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/sama5/sam_hsmci.c b/arch/arm/src/sama5/sam_hsmci.c index 8caa5098aab6b..8492911d8f738 100644 --- a/arch/arm/src/sama5/sam_hsmci.c +++ b/arch/arm/src/sama5/sam_hsmci.c @@ -458,9 +458,6 @@ struct sam_dev_s /* Low-level helpers ********************************************************/ -static int sam_takesem(struct sam_dev_s *priv); -#define sam_givesem(priv) (nxsem_post(&priv->waitsem)) - #ifdef CONFIG_SAMA5_HSMCI_REGDEBUG static bool sam_checkreg(struct sam_dev_s *priv, bool wr, uint32_t value, uint32_t address); @@ -656,27 +653,6 @@ static struct sam_dev_s g_hsmci2; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int sam_takesem(struct sam_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: sam_checkreg * @@ -1372,7 +1348,7 @@ static void sam_endwait(struct sam_dev_s *priv, sdio_eventset_t wkupevent) /* Wake up the waiting thread */ - sam_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2775,7 +2751,7 @@ static sdio_eventset_t sam_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = sam_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started), diff --git a/arch/arm/src/sama5/sam_nand.c b/arch/arm/src/sama5/sam_nand.c index 2d8d791f0eb71..a411cfff7a7d8 100644 --- a/arch/arm/src/sama5/sam_nand.c +++ b/arch/arm/src/sama5/sam_nand.c @@ -314,7 +314,7 @@ struct sam_nand_s g_nand; #if NAND_NBANKS > 1 static int nand_lock(void) { - return nxsem_wait_uninterruptible(&g_nand.exclsem); + return nxmutex_lock(&g_nand.lock); } #endif @@ -335,7 +335,7 @@ static int nand_lock(void) #if NAND_NBANKS > 1 static void nand_unlock(void) { - nxsem_post(&g_nand.exclsem); + nxmutex_unlock(&g_nand.lock); } #endif @@ -2996,7 +2996,7 @@ struct mtd_dev_s *sam_nand_initialize(int cs) /* Initialize the global nand state structure */ #if NAND_NBANKS > 1 - nxsem_init(&g_nand.exclsem, 0, 1); + nxmutex_init(&g_nand.lock); #endif #ifdef CONFIG_SAMA5_NAND_HSMCINTERRUPTS diff --git a/arch/arm/src/sama5/sam_nand.h b/arch/arm/src/sama5/sam_nand.h index 1db83d4f0cafb..ef038b2ba6907 100644 --- a/arch/arm/src/sama5/sam_nand.h +++ b/arch/arm/src/sama5/sam_nand.h @@ -33,7 +33,7 @@ #include #include -#include +#include #include "arm_internal.h" #include "chip.h" @@ -321,7 +321,7 @@ struct sam_nand_s { bool initialized; /* True: One time initialization is complete */ #if NAND_NBANKS > 1 - sem_t exclsem; /* Enforce exclusive access to the SMC hardware */ + mutex_t lock; /* Enforce exclusive access to the SMC hardware */ #endif /* Dynamic state */ diff --git a/arch/arm/src/sama5/sam_ohci.c b/arch/arm/src/sama5/sam_ohci.c index 1f22dae16f797..093609638071b 100644 --- a/arch/arm/src/sama5/sam_ohci.c +++ b/arch/arm/src/sama5/sam_ohci.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -257,7 +258,7 @@ struct sam_ohci_s uint8_t outinterval; /* Minimum periodic IN EP polling interval: 2, 4, 6, 16, or 32 */ #endif - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait Writeback Done Head event */ struct work_s work; /* Supports interrupt bottom half */ @@ -341,12 +342,6 @@ static void sam_putreg(uint32_t val, uint32_t addr); # define sam_putreg(val,addr) putreg32(val,addr) #endif -/* Semaphores ***************************************************************/ - -static int sam_takesem(sem_t *sem); -static int sam_takesem_noncancelable(sem_t *sem); -#define sam_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t sam_getle16(const uint8_t *val); @@ -625,54 +620,6 @@ static void sam_putreg(uint32_t val, uint32_t addr) } #endif -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int sam_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: sam_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int sam_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: sam_getle16 * @@ -1913,12 +1860,12 @@ static int sam_ctrltd(struct sam_rhport_s *rhport, * * REVISIT: Is this safe? NO. This is a bug and needs rethinking. * We need to lock all of the port-resources (not OHCI common) until - * the transfer is complete. But we can't use the common OHCI exclsem + * the transfer is complete. But we can't use the common OHCI lock * or we will deadlock while waiting (because the working thread that - * wakes this thread up needs the exclsem). + * wakes this thread up needs the lock). */ #warning REVISIT - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); /* Wait for the Writeback Done Head interrupt Loop to handle any false * alarm semaphore counts. @@ -1926,14 +1873,14 @@ static int sam_ctrltd(struct sam_rhport_s *rhport, while (eplist->wdhwait && ret >= 0) { - ret = sam_takesem(&eplist->wdhsem); + ret = nxsem_wait_uninterruptible(&eplist->wdhsem); } /* Re-acquire the ECHI semaphore. The caller expects to be holding * this upon return. */ - ret2 = sam_takesem_noncancelable(&g_ohci.exclsem); + ret2 = nxmutex_lock(&g_ohci.lock); if (ret2 < 0) { ret = ret2; @@ -2027,7 +1974,7 @@ static void sam_rhsc_bottomhalf(void) if (g_ohci.pscwait) { - sam_givesem(&g_ohci.pscsem); + nxsem_post(&g_ohci.pscsem); g_ohci.pscwait = false; } } @@ -2087,7 +2034,7 @@ static void sam_rhsc_bottomhalf(void) if (g_ohci.pscwait) { - sam_givesem(&g_ohci.pscsem); + nxsem_post(&g_ohci.pscsem); g_ohci.pscwait = false; } } @@ -2251,7 +2198,7 @@ static void sam_wdh_bottomhalf(void) if (eplist->wdhwait) { - sam_givesem(&eplist->wdhsem); + nxsem_post(&eplist->wdhsem); eplist->wdhwait = false; } @@ -2289,7 +2236,7 @@ static void sam_ohci_bottomhalf(void *arg) * real option (other than to reschedule and delay). */ - sam_takesem_noncancelable(&g_ohci.exclsem); + nxmutex_lock(&g_ohci.lock); /* Root hub status change interrupt */ @@ -2342,7 +2289,7 @@ static void sam_ohci_bottomhalf(void *arg) /* Now re-enable interrupts */ sam_putreg(OHCI_INT_MIE, SAM_USBHOST_INTEN); - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); } /**************************************************************************** @@ -2450,7 +2397,7 @@ static int sam_wait(struct usbhost_connection_s *conn, */ g_ohci.pscwait = true; - ret = sam_takesem(&g_ohci.pscsem); + ret = nxsem_wait_uninterruptible(&g_ohci.pscsem); if (ret < 0) { return ret; @@ -2630,7 +2577,7 @@ static int sam_ep0configure(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to EP0 and the control list */ - ret = sam_takesem(&g_ohci.exclsem); + ret = nxmutex_lock(&g_ohci.lock); if (ret < 0) { return ret; @@ -2653,7 +2600,7 @@ static int sam_ep0configure(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, up_clean_dcache((uintptr_t)edctrl, (uintptr_t)edctrl + sizeof(struct ohci_ed_s)); - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); usbhost_vtrace2(OHCI_VTRACE2_EP0CTRLED, RHPORT(rhport), (uint16_t)edctrl->hw.ctrl); @@ -2728,7 +2675,7 @@ static int sam_epalloc(struct usbhost_driver_s *drvr, * periodic list, and the interrupt table. */ - ret = sam_takesem(&g_ohci.exclsem); + ret = nxmutex_lock(&g_ohci.lock); if (ret < 0) { goto errout_with_eplist; @@ -2740,7 +2687,7 @@ static int sam_epalloc(struct usbhost_driver_s *drvr, if (!ed) { usbhost_trace1(OHCI_TRACE1_EDALLOC_FAILED, 0); - goto errout_with_semaphore; + goto errout_with_lock; } td = sam_tdalloc(); @@ -2859,15 +2806,15 @@ static int sam_epalloc(struct usbhost_driver_s *drvr, /* Success.. return an opaque reference to the endpoint list container */ *ep = (usbhost_ep_t)eplist; - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return OK; errout_with_td: sam_tdfree(td); errout_with_ed: sam_edfree(ed); -errout_with_semaphore: - sam_givesem(&g_ohci.exclsem); +errout_with_lock: + nxmutex_unlock(&g_ohci.lock); errout_with_eplist: kmm_free(eplist); errout: @@ -2914,7 +2861,7 @@ static int sam_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * periodic list and the interrupt table. */ - ret2 = sam_takesem_noncancelable(&g_ohci.exclsem); + ret2 = nxmutex_lock(&g_ohci.lock); /* Remove the ED to the correct list depending on the transfer type */ @@ -2950,7 +2897,7 @@ static int sam_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) nxsem_destroy(&eplist->wdhsem); kmm_free(eplist); - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return ret < 0 ? ret : ret2; } @@ -2996,7 +2943,7 @@ static int sam_alloc(struct usbhost_driver_s *drvr, /* We must have exclusive access to the transfer buffer pool */ - ret = sam_takesem(&g_ohci.exclsem); + ret = nxmutex_lock(&g_ohci.lock); if (ret < 0) { return ret; @@ -3011,7 +2958,7 @@ static int sam_alloc(struct usbhost_driver_s *drvr, ret = OK; } - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return ret; } @@ -3047,9 +2994,9 @@ static int sam_free(struct usbhost_driver_s *drvr, uint8_t *buffer) /* We must have exclusive access to the transfer buffer pool */ - ret = sam_takesem_noncancelable(&g_ohci.exclsem); + ret = nxmutex_lock(&g_ohci.lock); sam_tbfree(buffer); - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return ret; } @@ -3185,7 +3132,7 @@ static int sam_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to EP0 and the control list */ - ret = sam_takesem(&g_ohci.exclsem); + ret = nxmutex_lock(&g_ohci.lock); if (ret < 0) { return ret; @@ -3211,7 +3158,7 @@ static int sam_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * it to be reloaded from RAM after the DMA. */ - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); up_invalidate_dcache((uintptr_t)buffer, (uintptr_t)buffer + len); return ret; } @@ -3239,7 +3186,7 @@ static int sam_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to EP0 and the control list */ - ret = sam_takesem(&g_ohci.exclsem); + ret = nxmutex_lock(&g_ohci.lock); if (ret < 0) { return ret; @@ -3262,7 +3209,7 @@ static int sam_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, } } - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return ret; } @@ -3411,7 +3358,7 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * table. */ - ret = sam_takesem(&g_ohci.exclsem); + ret = nxmutex_lock(&g_ohci.lock); if (ret < 0) { return (ssize_t)ret; @@ -3442,13 +3389,13 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * * REVISIT: Is this safe? NO. This is a bug and needs rethinking. * We need to lock all of the port-resources (not OHCI common) until - * the transfer is complete. But we can't use the common OHCI exclsem + * the transfer is complete. But we can't use the common OHCI lock * or we will deadlock while waiting (because the working thread that - * wakes this thread up needs the exclsem). + * wakes this thread up needs the lock). */ #warning REVISIT - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); /* Wait for the Writeback Done Head interrupt Loop to handle any false * alarm semaphore counts. @@ -3456,14 +3403,14 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, while (eplist->wdhwait && ret >= 0) { - ret = sam_takesem(&eplist->wdhsem); + ret = nxsem_wait_uninterruptible(&eplist->wdhsem); } /* Re-acquire the OCHI semaphore. The caller expects to be holding * this upon return. */ - ret2 = sam_takesem(&g_ohci.exclsem); + ret2 = nxmutex_lock(&g_ohci.lock); if (ret2 < 0) { ret = ret2; @@ -3496,7 +3443,7 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, nbytes = eplist->xfrd; DEBUGASSERT(nbytes >= 0 && nbytes <= buflen); - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return nbytes; } @@ -3523,7 +3470,7 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* Make sure that there is no outstanding request on this endpoint */ eplist->wdhwait = false; - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return (ssize_t)ret; } @@ -3679,7 +3626,7 @@ static int sam_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * table. */ - ret = sam_takesem(&g_ohci.exclsem); + ret = nxmutex_lock(&g_ohci.lock); if (ret < 0) { return ret; @@ -3709,7 +3656,7 @@ static int sam_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * when the transfer completes. */ - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return OK; errout: @@ -3718,7 +3665,7 @@ static int sam_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, eplist->callback = NULL; eplist->arg = NULL; - sam_givesem(&g_ohci.exclsem); + nxmutex_unlock(&g_ohci.lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -3839,7 +3786,7 @@ static int sam_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake up the waiting thread */ - sam_givesem(&eplist->wdhsem); + nxsem_post(&eplist->wdhsem); eplist->wdhwait = false; } #ifdef CONFIG_USBHOST_ASYNCH @@ -3909,7 +3856,7 @@ static int sam_connect(struct usbhost_driver_s *drvr, if (g_ohci.pscwait) { g_ohci.pscwait = false; - sam_givesem(&g_ohci.pscsem); + nxsem_post(&g_ohci.pscsem); } leave_critical_section(flags); @@ -4016,7 +3963,7 @@ struct usbhost_connection_s *sam_ohci_initialize(int controller) /* Initialize the state data structure */ nxsem_init(&g_ohci.pscsem, 0, 0); - nxsem_init(&g_ohci.exclsem, 0, 1); + nxmutex_init(&g_ohci.lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/sama5/sam_pmecc.c b/arch/arm/src/sama5/sam_pmecc.c index 66a02535e01df..7d886b2754c9d 100644 --- a/arch/arm/src/sama5/sam_pmecc.c +++ b/arch/arm/src/sama5/sam_pmecc.c @@ -59,7 +59,7 @@ #include #include -#include +#include #include "sam_pmecc.h" #include "sam_nand.h" @@ -135,7 +135,7 @@ struct sam_pmecc_s { bool configured; /* True: Configured for some HSMC NAND bank */ #if NAND_NPMECC_BANKS > 1 - sem_t exclem; /* For mutually exclusive access to the PMECC */ + mutex_t lock; /* For mutually exclusive access to the PMECC */ uint8_t cs; /* Currently configured for this bank */ #endif bool sector1k; /* True: 1024B sector size; False: 512B sector size */ @@ -467,7 +467,7 @@ static uint32_t pmecc_getsigma(void) /* Init smu[i+1] with 0 */ - for (k = 0; k < (2 * PMECC_MAX_CORRECTABILITY + 1); k ++) + for (k = 0; k < (2 * PMECC_MAX_CORRECTABILITY + 1); k++) { g_pmecc.desc.smu[i + 1][k] = 0; } @@ -1017,7 +1017,7 @@ static int pmecc_pagelayout(uint16_t datasize, uint16_t eccsize) #if NAND_NPMECC_BANKS > 1 void pmecc_initialize(void) { - nxsem_init(&g_pmecc.exclsem, 0, 1); + nxmutex_init(&g_pmecc.lock); } #endif @@ -1282,7 +1282,7 @@ int pmecc_configure(struct sam_nandcs_s *priv, bool protected) #if NAND_NPMECC_BANKS > 1 int pmecc_lock(void) { - return nxsem_wait_uninterruptible(&g_pmecc.exclsem); + return nxmutex_lock(&g_pmecc.lock); } #endif @@ -1303,7 +1303,7 @@ int pmecc_lock(void) #if NAND_NPMECC_BANKS > 1 void pmecc_unlock(void) { - nxsem_post(&g_pmecc.exclsem); + nxmutex_unlock(&g_pmecc.lock); } #endif diff --git a/arch/arm/src/sama5/sam_sdmmc.c b/arch/arm/src/sama5/sam_sdmmc.c index 2c4baf7d15161..e5d95c2c52aa1 100644 --- a/arch/arm/src/sama5/sam_sdmmc.c +++ b/arch/arm/src/sama5/sam_sdmmc.c @@ -245,8 +245,6 @@ struct sam_sdmmcregs_s /* Low-level helpers ********************************************************/ -static void sam_takesem(struct sam_dev_s *priv); -#define sam_givesem(priv) (nxsem_post(&priv->waitsem)) static void sam_configwaitints(struct sam_dev_s *priv, uint32_t waitints, sdio_eventset_t waitevents, sdio_eventset_t wkupevents); static void sam_configxfrints(struct sam_dev_s *priv, uint32_t xfrints); @@ -728,26 +726,6 @@ static inline void sam_putreg(struct sam_dev_s *priv, uint32_t value, return sam_putreg32(priv, value, offset); } -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void sam_takesem(struct sam_dev_s *priv) -{ - nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: sam_configwaitints * @@ -1221,7 +1199,7 @@ static void sam_endwait(struct sam_dev_s *priv, sdio_eventset_t wkupevent) /* Wake up the waiting thread */ - sam_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2939,7 +2917,7 @@ static sdio_eventset_t sam_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - sam_takesem(priv); + nxsem_wait_uninterruptible(&priv->waitsem); wkupevent = priv->wkupevent; /* Check if the event has occurred. When the event has occurred, then diff --git a/arch/arm/src/sama5/sam_spi.c b/arch/arm/src/sama5/sam_spi.c index 8bcbdfa1ab4f3..c77d9eb4739fa 100644 --- a/arch/arm/src/sama5/sam_spi.c +++ b/arch/arm/src/sama5/sam_spi.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -174,7 +174,7 @@ typedef void (*select_t)(uint32_t devid, bool selected); struct sam_spidev_s { uint32_t base; /* SPI controller register base address */ - sem_t spisem; /* Assures mutually exclusive access to SPI */ + mutex_t spilock; /* Assures mutually exclusive access to SPI */ select_t select; /* SPI select callout */ bool initialized; /* TRUE: Controller has been initialized */ #ifdef CONFIG_SAMA5_SPI_DMA @@ -857,11 +857,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&spi->spisem); + ret = nxmutex_lock(&spi->spilock); } else { - ret = nxsem_post(&spi->spisem); + ret = nxmutex_unlock(&spi->spilock); } return ret; @@ -1783,11 +1783,11 @@ struct spi_dev_s *sam_spibus_initialize(int port) spi_getreg(spi, SAM_SPI_SR_OFFSET); spi_getreg(spi, SAM_SPI_RDR_OFFSET); - /* Initialize the SPI semaphore that enforces mutually exclusive + /* Initialize the SPI mutex that enforces mutually exclusive * access to the SPI registers. */ - nxsem_init(&spi->spisem, 0, 1); + nxmutex_init(&spi->spilock); spi->initialized = true; #ifdef CONFIG_SAMA5_SPI_DMA diff --git a/arch/arm/src/sama5/sam_ssc.c b/arch/arm/src/sama5/sam_ssc.c index cbb8ed2234592..52581364c6530 100644 --- a/arch/arm/src/sama5/sam_ssc.c +++ b/arch/arm/src/sama5/sam_ssc.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include "arm_internal.h" @@ -441,7 +442,7 @@ struct sam_ssc_s { struct i2s_dev_s dev; /* Externally visible I2S interface */ uintptr_t base; /* SSC controller register base address */ - sem_t exclsem; /* Assures mutually exclusive access to SSC */ + mutex_t lock; /* Assures mutually exclusive access to SSC */ uint8_t datalen; /* Data width (8, 16, or 32) */ uint8_t align; /* Log2 of data width (0, 1, or 3) */ uint8_t pid; /* Peripheral ID */ @@ -529,14 +530,6 @@ static void ssc_dump_queues(struct sam_transport_s *xpt, # define ssc_dump_buffer(m,b,s) #endif -/* Semaphore helpers */ - -static int ssc_exclsem_take(struct sam_ssc_s *priv); -#define ssc_exclsem_give(priv) nxsem_post(&priv->exclsem) - -static int ssc_bufsem_take(struct sam_ssc_s *priv); -#define ssc_bufsem_give(priv) nxsem_post(&priv->bufsem) - /* Buffer container helpers */ static struct sam_buffer_s * @@ -861,46 +854,6 @@ static void ssc_dump_queues(struct sam_transport_s *xpt, const char *msg) } #endif -/**************************************************************************** - * Name: ssc_exclsem_take - * - * Description: - * Take the exclusive access semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the SSC peripheral state - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int ssc_exclsem_take(struct sam_ssc_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -/**************************************************************************** - * Name: ssc_bufsem_take - * - * Description: - * Take the buffer semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the SSC peripheral state - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int ssc_bufsem_take(struct sam_ssc_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->bufsem); -} - /**************************************************************************** * Name: ssc_buf_allocate * @@ -931,7 +884,7 @@ static struct sam_buffer_s *ssc_buf_allocate(struct sam_ssc_s *priv) * have at least one free buffer container. */ - ret = ssc_bufsem_take(priv); + ret = nxsem_wait_uninterruptible(&priv->bufsem); if (ret < 0) { return NULL; @@ -982,7 +935,7 @@ static void ssc_buf_free(struct sam_ssc_s *priv, /* Wake up any threads waiting for a buffer container */ - ssc_bufsem_give(priv); + nxsem_post(&priv->bufsem); } /**************************************************************************** @@ -2199,7 +2152,7 @@ static int ssc_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the SSC driver data */ - ret = ssc_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout_with_buf; @@ -2211,7 +2164,7 @@ static int ssc_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: SSC%d has no receiver\n", priv->sscno); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Add a reference to the audio buffer */ @@ -2239,11 +2192,11 @@ static int ssc_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, ret = ssc_rxdma_setup(priv); DEBUGASSERT(ret == OK); leave_critical_section(flags); - ssc_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - ssc_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); errout_with_buf: ssc_buf_free(priv, bfcontainer); @@ -2422,7 +2375,7 @@ static int ssc_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the SSC driver data */ - ret = ssc_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout_with_buf; @@ -2434,7 +2387,7 @@ static int ssc_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: SSC%d has no transmitter\n", priv->sscno); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Add a reference to the audio buffer */ @@ -2462,11 +2415,11 @@ static int ssc_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, ret = ssc_txdma_setup(priv); DEBUGASSERT(ret == OK); leave_critical_section(flags); - ssc_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - ssc_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); errout_with_buf: ssc_buf_free(priv, bfcontainer); @@ -3396,7 +3349,7 @@ struct i2s_dev_s *sam_ssc_initialize(int port) /* Initialize the common parts for the SSC device structure */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->dev.ops = &g_sscops; priv->sscno = port; @@ -3468,7 +3421,7 @@ struct i2s_dev_s *sam_ssc_initialize(int port) ssc_dma_free(priv); errout_with_alloc: - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return NULL; } diff --git a/arch/arm/src/sama5/sam_tc.c b/arch/arm/src/sama5/sam_tc.c index 85330247606fa..40d49b030b4ed 100644 --- a/arch/arm/src/sama5/sam_tc.c +++ b/arch/arm/src/sama5/sam_tc.c @@ -59,7 +59,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -125,7 +125,7 @@ struct sam_chan_s struct sam_tc_s { - sem_t exclsem; /* Assures mutually exclusive access to TC */ + mutex_t lock; /* Assures mutually exclusive access to TC */ uintptr_t base; /* Register base address */ uint8_t pid; /* Peripheral ID/irq number */ uint8_t tc; /* Timer/channel number (0 or 1) */ @@ -151,9 +151,6 @@ struct sam_tc_s /* Low-level helpers ********************************************************/ -static int sam_takesem(struct sam_tc_s *tc); -#define sam_givesem(tc) (nxsem_post(&tc->exclsem)) - #ifdef CONFIG_SAMA5_TC_REGDEBUG static void sam_regdump(struct sam_chan_s *chan, const char *msg); static bool sam_checkreg(struct sam_tc_s *tc, bool wr, uint32_t regaddr, @@ -461,26 +458,6 @@ static const uint8_t g_regoffset[TC_NREGISTERS] = * Low-level Helpers ****************************************************************************/ -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int sam_takesem(struct sam_tc_s *tc) -{ - return nxsem_wait_uninterruptible(&tc->exclsem); -} - /**************************************************************************** * Name: sam_regdump * @@ -697,7 +674,7 @@ static inline void sam_chan_putreg(struct sam_chan_s *chan, * and channel. NULL is returned on any failure. * * On successful return, the caller holds the tc exclusive access - * semaphore. + * mutex. * ****************************************************************************/ @@ -765,7 +742,7 @@ static int sam_tc_interrupt(struct sam_tc_s *tc) * and channel. NULL is returned on any failure. * * On successful return, the caller holds the tc exclusive access - * semaphore. + * mutex. * ****************************************************************************/ @@ -916,7 +893,7 @@ static uint32_t sam_tc_divfreq_lookup(uint32_t ftcin, int ndx) * and channel. NULL is returned on any failure. * * On successful return, the caller holds the tc exclusive access - * semaphore. + * mutex. * ****************************************************************************/ @@ -971,7 +948,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) } /* Has the timer counter been initialized. We have to be careful here - * because there is no semaphore protection. + * because there is no mutex protection. */ flags = enter_critical_section(); @@ -980,7 +957,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) /* Initialize the timer counter data structure. */ memset(tc, 0, sizeof(struct sam_tc_s)); - nxsem_init(&tc->exclsem, 0, 1); + nxmutex_init(&tc->lock); tc->base = tcconfig->base; tc->tc = channel < 3 ? 0 : 1; tc->pid = tcconfig->pid; @@ -1057,7 +1034,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) /* Get exclusive access to the timer/count data structure */ - ret = sam_takesem(tc); + ret = nxmutex_lock(&tc->lock); if (ret < 0) { leave_critical_section(flags); @@ -1077,7 +1054,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) /* No.. return a failure */ tmrerr("ERROR: Channel %d is in-use\n", channel); - sam_givesem(tc); + nxmutex_unlock(&tc->lock); return NULL; } @@ -1085,7 +1062,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) chan->inuse = true; - /* And return the channel with the semaphore locked */ + /* And return the channel with the mutex locked */ sam_regdump(chan, "Initialized"); return chan; @@ -1143,7 +1120,7 @@ TC_HANDLE sam_tc_allocate(int channel, int mode) sam_chan_putreg(chan, SAM_TC_CMR_OFFSET, mode); sam_regdump(chan, "Allocated"); - sam_givesem(chan->tc); + nxmutex_unlock(&chan->tc->lock); } /* Return an opaque reference to the channel */ diff --git a/arch/arm/src/sama5/sam_trng.c b/arch/arm/src/sama5/sam_trng.c index df2ea09039d06..ea21cd8fe3cc5 100644 --- a/arch/arm/src/sama5/sam_trng.c +++ b/arch/arm/src/sama5/sam_trng.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include "arm_internal.h" @@ -61,7 +62,7 @@ static ssize_t sam_read(struct file *filep, char *buffer, size_t); struct trng_dev_s { - sem_t exclsem; /* Enforces exclusive access to the TRNG */ + mutex_t lock; /* Enforces exclusive access to the TRNG */ sem_t waitsem; /* Wait for buffer full */ uint32_t *samples; /* Current buffer being filled */ size_t maxsamples; /* Size of the current buffer (in 32-bit words) */ @@ -236,7 +237,7 @@ static ssize_t sam_read(struct file *filep, char *buffer, size_t buflen) /* Get exclusive access to the TRNG hardware */ - ret = nxsem_wait(&g_trngdev.exclsem); + ret = nxmutex_lock(&g_trngdev.lock); if (ret < 0) { return ret; @@ -306,7 +307,7 @@ static ssize_t sam_read(struct file *filep, char *buffer, size_t buflen) /* Release our lock on the TRNG hardware */ - nxsem_post(&g_trngdev.exclsem); + nxmutex_unlock(&g_trngdev.lock); finfo("Return %d\n", (int)retval); return retval; @@ -336,9 +337,9 @@ static int sam_rng_initialize(void) memset(&g_trngdev, 0, sizeof(struct trng_dev_s)); - /* Initialize semphores */ + /* Initialize mutex & semphores */ - nxsem_init(&g_trngdev.exclsem, 0, 1); + nxmutex_init(&g_trngdev.lock); nxsem_init(&g_trngdev.waitsem, 0, 0); /* The waitsem semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/sama5/sam_twi.c b/arch/arm/src/sama5/sam_twi.c index 00d667f102b35..921c71a6e164a 100644 --- a/arch/arm/src/sama5/sam_twi.c +++ b/arch/arm/src/sama5/sam_twi.c @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -161,7 +162,7 @@ struct twi_dev_s uint32_t frequency; /* TWI transfer clock frequency */ uint8_t msgc; /* Number of message in the message list */ - sem_t exclsem; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t waitsem; /* Wait for TWI transfer completion */ struct wdog_s timeout; /* Watchdog to recover from bus hangs */ volatile int result; /* The result of the transfer */ @@ -183,9 +184,6 @@ struct twi_dev_s /* Low-level helper functions */ -static int twi_takesem(sem_t *sem); -#define twi_givesem(sem) (nxsem_post(sem)) - #ifdef CONFIG_SAMA5_TWI_REGDEBUG static bool twi_checkreg(struct twi_dev_s *priv, bool wr, uint32_t value, uintptr_t address); @@ -299,25 +297,6 @@ static const struct i2c_ops_s g_twiops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: twi_takesem - * - * Description: - * Take the wait semaphore. May be interrupted by a signal. - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int twi_takesem(sem_t *sem) -{ - return nxsem_wait(sem); -} - /**************************************************************************** * Name: twi_checkreg * @@ -489,7 +468,7 @@ static int twi_wait(struct twi_dev_s *priv, unsigned int size) do { i2cinfo("TWI%d Waiting...\n", priv->attr->twi); - ret = twi_takesem(&priv->waitsem); + ret = nxsem_wait(&priv->waitsem); i2cinfo("TWI%d Awakened with result: %d\n", priv->attr->twi, priv->result); @@ -529,7 +508,7 @@ static void twi_wakeup(struct twi_dev_s *priv, int result) /* Wake up the waiting thread with the result of the transfer */ priv->result = result; - twi_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -814,7 +793,7 @@ static int twi_transfer(struct i2c_master_s *dev, /* Get exclusive access to the device */ - ret = twi_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -852,7 +831,7 @@ static int twi_transfer(struct i2c_master_s *dev, } leave_critical_section(flags); - twi_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -884,7 +863,7 @@ static int twi_reset(struct i2c_master_s *dev) /* Get exclusive access to the TWI device */ - ret = twi_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -989,7 +968,7 @@ static int twi_reset(struct i2c_master_s *dev) /* Release our lock on the bus */ - twi_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -1273,9 +1252,9 @@ struct i2c_master_s *sam_i2cbus_initialize(int bus) priv->dev.ops = &g_twiops; - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->waitsem, 0, 0); /* The waitsem semaphore is used for signaling and, hence, should not have @@ -1315,7 +1294,7 @@ int sam_i2cbus_uninitialize(struct i2c_master_s *dev) /* Reset data structures */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->waitsem); /* Cancel the watchdog timer */ diff --git a/arch/arm/src/sama5/sam_udphs.c b/arch/arm/src/sama5/sam_udphs.c index 0f0bd09b6041d..35a76cd84cc2d 100644 --- a/arch/arm/src/sama5/sam_udphs.c +++ b/arch/arm/src/sama5/sam_udphs.c @@ -845,7 +845,7 @@ static void sam_dumpep(struct sam_usbdev_s *priv, int epno) * Description: * Allocate a DMA transfer descriptor by removing it from the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -873,7 +873,7 @@ static struct sam_dtd_s *sam_dtd_alloc(struct sam_usbdev_s *priv) * Description: * Free a DMA transfer descriptor by returning it to the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ diff --git a/arch/arm/src/sama5/sam_xdmac.c b/arch/arm/src/sama5/sam_xdmac.c index acb4a66f8e670..4ab5e8b0140db 100644 --- a/arch/arm/src/sama5/sam_xdmac.c +++ b/arch/arm/src/sama5/sam_xdmac.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include "arm_internal.h" #include "sched/sched.h" @@ -125,9 +125,9 @@ struct sam_xdmach_s struct sam_xdmac_s { - /* These semaphores protect the DMA channel and descriptor tables */ + /* These mutex protect the DMA channel and descriptor tables */ - sem_t chsem; /* Protects channel table */ + mutex_t chlock; /* Protects channel table */ sem_t dsem; /* Protects descriptor table */ uint32_t base; /* DMA register channel base address */ @@ -732,24 +732,6 @@ static struct sam_xdmac_s g_xdmac1 = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takechsem() and sam_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table - * - ****************************************************************************/ - -static int sam_takechsem(struct sam_xdmac_s *xdmac) -{ - return nxsem_wait_uninterruptible(&xdmac->chsem); -} - -static inline void sam_givechsem(struct sam_xdmac_s *xdmac) -{ - nxsem_post(&xdmac->chsem); -} - /**************************************************************************** * Name: sam_takedsem() and sam_givedsem() * @@ -1422,7 +1404,7 @@ sam_allocdesc(struct sam_xdmach_s *xdmach, struct chnext_view1_s *prev, * that is an atomic operation. */ - ret = sam_takechsem(xdmac); + ret = nxmutex_lock(&xdmac->chlock); if (ret < 0) { sam_givedsem(xdmac); @@ -1494,7 +1476,7 @@ sam_allocdesc(struct sam_xdmach_s *xdmach, struct chnext_view1_s *prev, * search loop should always be successful. */ - sam_givechsem(xdmac); + nxmutex_unlock(&xdmac->chlock); DEBUGASSERT(descr != NULL); } @@ -1998,9 +1980,9 @@ void sam_dmainitialize(struct sam_xdmac_s *xdmac) sam_putdmac(xdmac, XDMAC_CHAN_ALL, SAM_XDMAC_GD_OFFSET); - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&xdmac->chsem, 0, 1); + nxmutex_init(&xdmac->chlock); nxsem_init(&xdmac->dsem, 0, SAM_NDMACHAN); /* The 'dsem' is used for signaling rather than mutual exclusion and, @@ -2121,7 +2103,7 @@ DMA_HANDLE sam_dmachannel(uint8_t dmacno, uint32_t chflags) */ xdmach = NULL; - ret = sam_takechsem(xdmac); + ret = nxmutex_lock(&xdmac->chlock); if (ret < 0) { return NULL; @@ -2154,7 +2136,7 @@ DMA_HANDLE sam_dmachannel(uint8_t dmacno, uint32_t chflags) } } - sam_givechsem(xdmac); + nxmutex_unlock(&xdmac->chlock); /* Show the result of the allocation */ diff --git a/arch/arm/src/samd2l2/sam_dmac.c b/arch/arm/src/samd2l2/sam_dmac.c index 132427b3da904..496136c253f24 100644 --- a/arch/arm/src/samd2l2/sam_dmac.c +++ b/arch/arm/src/samd2l2/sam_dmac.c @@ -33,6 +33,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -98,8 +99,6 @@ struct sam_dmach_s * Private Function Prototypes ****************************************************************************/ -static int sam_takechsem(void); -static inline void sam_givechsem(void); #if CONFIG_SAMD2L2_DMAC_NDESC > 0 static void sam_takedsem(void); static inline void sam_givedsem(void); @@ -122,9 +121,9 @@ static int sam_rxbuffer(struct sam_dmach_s *dmach, uint32_t paddr, * Private Data ****************************************************************************/ -/* These semaphores protect the DMA channel and descriptor tables */ +/* These mutex protect the DMA channel and descriptor tables */ -static sem_t g_chsem; +static mutex_t g_chlock; #if CONFIG_SAMD2L2_DMAC_NDESC > 0 static sem_t g_dsem; #endif @@ -159,24 +158,6 @@ static struct dma_desc_s g_dma_desc[CONFIG_SAMD2L2_DMAC_NDESC] * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takechsem() and sam_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table - * - ****************************************************************************/ - -static int sam_takechsem(void) -{ - return nxsem_wait_uninterruptible(&g_chsem); -} - -static inline void sam_givechsem(void) -{ - nxsem_post(&g_chsem); -} - /**************************************************************************** * Name: sam_takedsem() and sam_givedsem() * @@ -770,9 +751,9 @@ void weak_function arm_dma_initialize(void) dmainfo("Initialize DMAC\n"); int i; - /* Initialize global semaphores */ + /* Initialize global lock and semaphores */ - nxsem_init(&g_chsem, 0, 1); + nxmutex_init(&g_chlock); #if CONFIG_SAMD2L2_DMAC_NDESC > 0 nxsem_init(&g_dsem, 0, CONFIG_SAMD2L2_DMAC_NDESC); #endif @@ -853,7 +834,7 @@ DMA_HANDLE sam_dmachannel(uint32_t chflags) /* Search for an available DMA channel */ dmach = NULL; - ret = sam_takechsem(); + ret = nxmutex_lock(&g_chlock); if (ret < 0) { return NULL; @@ -889,7 +870,7 @@ DMA_HANDLE sam_dmachannel(uint32_t chflags) } } - sam_givechsem(); + nxmutex_unlock(&g_chlock); dmainfo("chflags: %08x returning dmach: %p\n", (int)chflags, dmach); return (DMA_HANDLE)dmach; diff --git a/arch/arm/src/samd2l2/sam_i2c_master.c b/arch/arm/src/samd2l2/sam_i2c_master.c index 83b7d334e596f..b591e1f1a2ff0 100644 --- a/arch/arm/src/samd2l2/sam_i2c_master.c +++ b/arch/arm/src/samd2l2/sam_i2c_master.c @@ -63,6 +63,7 @@ #include #include #include +#include #include #include @@ -171,7 +172,7 @@ struct sam_i2c_dev_s uint16_t flags; /* Transfer flags */ uint16_t nextflags; /* Next message flags */ - sem_t exclsem; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t waitsem; /* Wait for I2C transfer completion */ volatile int result; /* The result of the transfer */ volatile int xfrd; /* Number of bytes transfers */ @@ -205,10 +206,6 @@ static uint32_t i2c_getreg32(struct sam_i2c_dev_s *priv, static void i2c_putreg32(struct sam_i2c_dev_s *priv, uint32_t regval, unsigned int offset); -static int i2c_takesem(sem_t * sem); -static int i2c_takesem_noncancelable(sem_t * sem); -#define i2c_givesem(sem) (nxsem_post(sem)) - #ifdef CONFIG_SAM_I2C_REGDEBUG static bool i2c_checkreg(struct sam_i2c_dev_s *priv, bool wr, uint32_t value, uintptr_t address); @@ -458,45 +455,6 @@ static void i2c_putreg32(struct sam_i2c_dev_s *priv, uint32_t regval, putreg32(regval, priv->attr->base + offset); } -/**************************************************************************** - * Name: i2c_takesem - * - * Description: - * Take the wait semaphore. May be interrupted by a signal. - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int i2c_takesem(sem_t *sem) -{ - return nxsem_wait(sem); -} - -/**************************************************************************** - * Name: i2c_takesem_noncancelable - * - * Description: - * Take the wait semaphore (handling false alarm wake-ups due to the - * receipt of signals). May be interrupted by a signal. - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int i2c_takesem_noncancelable(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /**************************************************************************** * Name: i2c_checkreg * @@ -989,7 +947,7 @@ static int sam_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the device */ - ret = i2c_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1031,7 +989,7 @@ static int sam_i2c_transfer(struct i2c_master_s *dev, SAM_I2C_INTENCLR_OFFSET); leave_critical_section(flags); - i2c_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1042,7 +1000,7 @@ static int sam_i2c_transfer(struct i2c_master_s *dev, msgs++; } - i2c_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1400,7 +1358,7 @@ struct i2c_master_s *sam_i2c_master_initialize(int bus) priv->dev.ops = &g_i2cops; priv->flags = 0; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->waitsem, 0, 0); /* Perform repeatable I2C hardware initialization */ @@ -1430,7 +1388,7 @@ int sam_i2c_uninitialize(struct i2c_master_s *dev) /* Reset data structures */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->waitsem); /* Detach Interrupt Handler */ @@ -1457,7 +1415,7 @@ int sam_i2c_reset(struct i2c_master_s *dev) /* Get exclusive access to the I2C device */ - ret = i2c_takesem_noncancelable(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1490,7 +1448,7 @@ int sam_i2c_reset(struct i2c_master_s *dev) /* Release our lock on the bus */ - i2c_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ diff --git a/arch/arm/src/samd2l2/sam_spi.c b/arch/arm/src/samd2l2/sam_spi.c index 9b9f38d3a2df9..c7ee79ef1800d 100644 --- a/arch/arm/src/samd2l2/sam_spi.c +++ b/arch/arm/src/samd2l2/sam_spi.c @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -100,7 +100,7 @@ struct sam_spidev_s /* Dynamic configuration */ - sem_t spilock; /* Used to managed exclusive access to the bus */ + mutex_t spilock; /* Used to managed exclusive access to the bus */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t mode; /* Mode 0,1,2,3 */ @@ -238,7 +238,7 @@ static struct sam_spidev_s g_spi0dev = .muxconfig = BOARD_SERCOM0_MUXCONFIG, .srcfreq = BOARD_SERCOM0_FREQUENCY, .base = SAM_SERCOM0_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD2L2_SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM0_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM0_RX, @@ -288,7 +288,7 @@ static struct sam_spidev_s g_spi1dev = .muxconfig = BOARD_SERCOM1_MUXCONFIG, .srcfreq = BOARD_SERCOM1_FREQUENCY, .base = SAM_SERCOM1_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD2L2_SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM1_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM1_RX, @@ -338,7 +338,7 @@ static struct sam_spidev_s g_spi2dev = .muxconfig = BOARD_SERCOM2_MUXCONFIG, .srcfreq = BOARD_SERCOM2_FREQUENCY, .base = SAM_SERCOM2_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD2L2_SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM2_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM2_RX, @@ -388,7 +388,7 @@ static struct sam_spidev_s g_spi3dev = .muxconfig = BOARD_SERCOM3_MUXCONFIG, .srcfreq = BOARD_SERCOM3_FREQUENCY, .base = SAM_SERCOM3_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD2L2_SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM3_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM3_RX, @@ -438,7 +438,7 @@ static struct sam_spidev_s g_spi4dev = .muxconfig = BOARD_SERCOM4_MUXCONFIG, .srcfreq = BOARD_SERCOM4_FREQUENCY, .base = SAM_SERCOM4_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD2L2_SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM4_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM4_RX, @@ -488,7 +488,7 @@ static struct sam_spidev_s g_spi5dev = .muxconfig = BOARD_SERCOM5_MUXCONFIG, .srcfreq = BOARD_SERCOM5_FREQUENCY, .base = SAM_SERCOM5_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD2L2_SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM5_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM5_RX, @@ -811,11 +811,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->spilock); + ret = nxmutex_lock(&priv->spilock); } else { - ret = nxsem_post(&priv->spilock); + ret = nxmutex_unlock(&priv->spilock); } return ret; diff --git a/arch/arm/src/samd5e5/sam_dmac.c b/arch/arm/src/samd5e5/sam_dmac.c index 118d23f3e446f..a8ff9854e1e72 100644 --- a/arch/arm/src/samd5e5/sam_dmac.c +++ b/arch/arm/src/samd5e5/sam_dmac.c @@ -33,6 +33,7 @@ #include #include +#include #include #include @@ -99,8 +100,6 @@ struct sam_dmach_s * Private Function Prototypes ****************************************************************************/ -static int sam_takechsem(void); -static inline void sam_givechsem(void); #if CONFIG_SAMD5E5_DMAC_NDESC > 0 static void sam_takedsem(void); static inline void sam_givedsem(void); @@ -124,9 +123,9 @@ static int sam_rxbuffer(struct sam_dmach_s *dmach, uint32_t paddr, * Private Data ****************************************************************************/ -/* These semaphores protect the DMA channel and descriptor tables */ +/* These mutex protect the DMA channel and descriptor tables */ -static sem_t g_chsem; +static mutex_t g_chlock; #if CONFIG_SAMD5E5_DMAC_NDESC > 0 static sem_t g_dsem; #endif @@ -158,24 +157,6 @@ static struct dma_desc_s g_dma_desc[CONFIG_SAMD5E5_DMAC_NDESC] * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takechsem() and sam_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table - * - ****************************************************************************/ - -static int sam_takechsem(void) -{ - return nxsem_wait_uninterruptible(&g_chsem); -} - -static inline void sam_givechsem(void) -{ - nxsem_post(&g_chsem); -} - /**************************************************************************** * Name: sam_takedsem() and sam_givedsem() * @@ -743,9 +724,9 @@ void weak_function arm_dma_initialize(void) dmainfo("Initialize DMAC\n"); int i; - /* Initialize global semaphores */ + /* Initialize global mutex & semaphore */ - nxsem_init(&g_chsem, 0, 1); + nxmutex_init(&g_chlock); #if CONFIG_SAMD5E5_DMAC_NDESC > 0 nxsem_init(&g_dsem, 0, CONFIG_SAMD5E5_DMAC_NDESC); #endif @@ -837,7 +818,7 @@ DMA_HANDLE sam_dmachannel(uint32_t txflags, uint32_t rxflags) /* Search for an available DMA channel */ dmach = NULL; - ret = sam_takechsem(); + ret = nxmutex_lock(&g_chlock); if (ret < 0) { return NULL; @@ -873,7 +854,7 @@ DMA_HANDLE sam_dmachannel(uint32_t txflags, uint32_t rxflags) } } - sam_givechsem(); + nxmutex_unlock(&g_chlock); dmainfo("chflags: %08x returning dmach: %p\n", (int)chflags, dmach); return (DMA_HANDLE)dmach; diff --git a/arch/arm/src/samd5e5/sam_i2c_master.c b/arch/arm/src/samd5e5/sam_i2c_master.c index cb4db72537031..7920af236c500 100644 --- a/arch/arm/src/samd5e5/sam_i2c_master.c +++ b/arch/arm/src/samd5e5/sam_i2c_master.c @@ -56,6 +56,7 @@ #include #include #include +#include #include #include @@ -161,7 +162,7 @@ struct sam_i2c_dev_s uint16_t flags; /* Transfer flags */ uint16_t nextflags; /* Next message flags */ - sem_t exclsem; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t waitsem; /* Wait for I2C transfer completion */ volatile int result; /* The result of the transfer */ volatile int xfrd; /* Number of bytes transfers */ @@ -195,9 +196,6 @@ static uint32_t i2c_getreg32(struct sam_i2c_dev_s *priv, static void i2c_putreg32(struct sam_i2c_dev_s *priv, uint32_t regval, unsigned int offset); -static void i2c_takesem(sem_t * sem); -#define i2c_givesem(sem) (nxsem_post(sem)) - #ifdef CONFIG_SAMD5E5_I2C_REGDEBUG static bool i2c_checkreg(struct sam_i2c_dev_s *priv, bool wr, uint32_t value, uintptr_t address); @@ -482,39 +480,6 @@ static void i2c_putreg32(struct sam_i2c_dev_s *priv, uint32_t regval, putreg32(regval, priv->attr->base + offset); } -/**************************************************************************** - * Name: i2c_takesem - * - * Description: - * Take the wait semaphore. May be interrupted by a signal. - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void i2c_takesem(sem_t *sem) -{ - int ret; - - do - { - /* Take the semaphore (perhaps waiting) */ - - ret = nxsem_wait(sem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); -} - /**************************************************************************** * Name: i2c_checkreg * @@ -1015,7 +980,7 @@ static int sam_i2c_transfer(struct i2c_master_s *dev, /* Get exclusive access to the device */ - i2c_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Initiate the message transfer */ @@ -1040,7 +1005,7 @@ static int sam_i2c_transfer(struct i2c_master_s *dev, if (ret < 0) { leave_critical_section(flags); - i2c_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1051,7 +1016,7 @@ static int sam_i2c_transfer(struct i2c_master_s *dev, msgs++; } - i2c_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1451,7 +1416,7 @@ struct i2c_master_s *sam_i2c_master_initialize(int bus) priv->dev.ops = &g_i2cops; priv->flags = 0; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->waitsem, 0, 0); /* Perform repeatable I2C hardware initialization */ @@ -1482,7 +1447,7 @@ int sam_i2c_uninitialize(struct i2c_master_s *dev) /* Reset data structures */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->waitsem); /* Detach Interrupt Handler */ @@ -1509,7 +1474,7 @@ int sam_i2c_reset(struct i2c_master_s *dev) /* Get exclusive access to the I2C device */ - i2c_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Disable I2C interrupts */ @@ -1539,7 +1504,7 @@ int sam_i2c_reset(struct i2c_master_s *dev) /* Release our lock on the bus */ - i2c_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ diff --git a/arch/arm/src/samd5e5/sam_progmem.c b/arch/arm/src/samd5e5/sam_progmem.c index 3bfac6e24c3e1..dd280b959d832 100644 --- a/arch/arm/src/samd5e5/sam_progmem.c +++ b/arch/arm/src/samd5e5/sam_progmem.c @@ -30,6 +30,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -154,47 +155,12 @@ ****************************************************************************/ static uint32_t g_page_buffer[SAMD5E5_PAGE_WORDS]; -static sem_t g_page_sem; +static mutex_t g_page_lock; /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: page_buffer_lock - * - * Description: - * Get exclusive access to the global page buffer - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void page_buffer_lock(void) -{ - int ret; - - do - { - /* Take the semaphore (perhaps waiting) */ - - ret = nxsem_wait(&g_page_sem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); -} - -#define page_buffer_unlock() nxsem_post(&g_page_sem) - /**************************************************************************** * Name: nvm_command * @@ -385,11 +351,11 @@ void sam_progmem_initialize(void) NVMCTRL_CTRLA_AUTOWS; putreg16(ctrla, SAM_NVMCTRL_CTRLA); - /* Initialize the semaphore that manages exclusive access to the global + /* Initialize the mutex that manages exclusive access to the global * page buffer. */ - nxsem_init(&g_page_sem, 0, 1); + nxmutex_init(&g_page_lock); } /**************************************************************************** @@ -686,7 +652,7 @@ ssize_t up_progmem_write(size_t address, const void *buffer, size_t buflen) /* Get exclusive access to the global page buffer */ - page_buffer_lock(); + nxmutex_lock(&g_page_lock); /* Get the page number corresponding to the flash offset and the byte * offset into the page. @@ -864,7 +830,7 @@ ssize_t up_progmem_write(size_t address, const void *buffer, size_t buflen) #endif leave_critical_section(flags); - page_buffer_unlock(); + nxmutex_unlock(&g_page_lock); return written; } diff --git a/arch/arm/src/samd5e5/sam_spi.c b/arch/arm/src/samd5e5/sam_spi.c index 3217c5904a59c..df4586f77628d 100644 --- a/arch/arm/src/samd5e5/sam_spi.c +++ b/arch/arm/src/samd5e5/sam_spi.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -94,7 +94,7 @@ struct sam_spidev_s /* Dynamic configuration */ - sem_t spilock; /* Used to managed exclusive access to the bus */ + mutex_t spilock; /* Used to managed exclusive access to the bus */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t mode; /* Mode 0,1,2,3 */ @@ -232,7 +232,7 @@ static struct sam_spidev_s g_spi0dev = .muxconfig = BOARD_SERCOM0_MUXCONFIG, .srcfreq = BOARD_SERCOM0_FREQUENCY, .base = SAM_SERCOM0_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD5E5SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM0_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM0_RX, @@ -282,7 +282,7 @@ static struct sam_spidev_s g_spi1dev = .muxconfig = BOARD_SERCOM1_MUXCONFIG, .srcfreq = BOARD_SERCOM1_FREQUENCY, .base = SAM_SERCOM1_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD5E5SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM1_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM1_RX, @@ -332,7 +332,7 @@ static struct sam_spidev_s g_spi2dev = .muxconfig = BOARD_SERCOM2_MUXCONFIG, .srcfreq = BOARD_SERCOM2_FREQUENCY, .base = SAM_SERCOM2_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD5E5SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM2_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM2_RX, @@ -382,7 +382,7 @@ static struct sam_spidev_s g_spi3dev = .muxconfig = BOARD_SERCOM3_MUXCONFIG, .srcfreq = BOARD_SERCOM3_FREQUENCY, .base = SAM_SERCOM3_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD5E5SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM3_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM3_RX, @@ -432,7 +432,7 @@ static struct sam_spidev_s g_spi4dev = .muxconfig = BOARD_SERCOM4_MUXCONFIG, .srcfreq = BOARD_SERCOM4_FREQUENCY, .base = SAM_SERCOM4_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD5E5SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM4_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM4_RX, @@ -482,7 +482,7 @@ static struct sam_spidev_s g_spi5dev = .muxconfig = BOARD_SERCOM5_MUXCONFIG, .srcfreq = BOARD_SERCOM5_FREQUENCY, .base = SAM_SERCOM5_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD5E5SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM5_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM5_RX, @@ -532,7 +532,7 @@ static struct sam_spidev_s g_spi6dev = .muxconfig = BOARD_SERCOM6_MUXCONFIG, .srcfreq = BOARD_SERCOM6_FREQUENCY, .base = SAM_SERCOM6_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD5E5SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM6_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM6_RX, @@ -582,7 +582,7 @@ static struct sam_spidev_s g_spi7dev = .muxconfig = BOARD_SERCOM7_MUXCONFIG, .srcfreq = BOARD_SERCOM7_FREQUENCY, .base = SAM_SERCOM7_BASE, - .spilock = SEM_INITIALIZER(1), + .spilock = NXMUTEX_INITIALIZER, #ifdef CONFIG_SAMD5E5SPI_DMA .dma_tx_trig = DMAC_TRIGSRC_SERCOM7_TX, .dma_rx_trig = DMAC_TRIGSRC_SERCOM7_RX, @@ -905,11 +905,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->spilock); + ret = nxmutex_lock(&priv->spilock); } else { - ret = nxsem_post(&priv->spilock); + ret = nxmutex_unlock(&priv->spilock); } return ret; diff --git a/arch/arm/src/samd5e5/sam_tc.c b/arch/arm/src/samd5e5/sam_tc.c index cc36b3082962e..4a9f6881ad511 100644 --- a/arch/arm/src/samd5e5/sam_tc.c +++ b/arch/arm/src/samd5e5/sam_tc.c @@ -236,8 +236,6 @@ static const uint8_t g_regoffset[TC_NREGISTERS] = /* Initialization */ -static void tc_takesem(struct sam_tc_dev_s *priv); -#define tc_givesem(priv) (nxsem_post(&priv->exclsem)) void tc_bridge_enable(int tc); void sam_tc_dumpregs(struct sam_tc_dev_s *priv); void sam_tc_setperiod(struct sam_tc_dev_s *priv); @@ -252,41 +250,6 @@ static uint32_t sam_tc_divfreq_lookup(uint32_t ftcin, int ndx); * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: tc_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wake-ups due to - * the receipt of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void tc_takesem(struct sam_tc_dev_s *priv) -{ - int ret; - - do - { - /* Take the semaphore (perhaps waiting) */ - - ret = nxsem_wait(&priv->exclsem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - /* DEBUGASSERT(ret == OK || ret == -EINTR); */ - } - - while (ret == -EINTR); -} - /**************************************************************************** * Name: tc_enable * @@ -657,7 +620,7 @@ TC_HANDLE sam_tc_allocate(int tc, int frequency) /* Initialize the TC driver structure */ priv->flags = 0; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Enable clocking to the TC module in PCHCTRL */ @@ -701,7 +664,7 @@ TC_HANDLE sam_tc_allocate(int tc, int frequency) /* Get exclusive access to the timer/count data structure */ - tc_takesem(priv); + nxmutex_lock(&priv->lock); /* Is it available? */ @@ -710,7 +673,7 @@ TC_HANDLE sam_tc_allocate(int tc, int frequency) /* No.. return a failure */ tmrerr("ERROR: TC%d is in-use\n", priv->attr->tc); - tc_givesem(priv); + nxmutex_unlock(&priv->lock); leave_critical_section(flags); return NULL; } @@ -724,7 +687,7 @@ TC_HANDLE sam_tc_allocate(int tc, int frequency) priv->initialized = true; leave_critical_section(flags); - tc_givesem(priv); + nxmutex_unlock(&priv->lock); } /* Return an opaque reference to the tc */ diff --git a/arch/arm/src/samd5e5/sam_tc.h b/arch/arm/src/samd5e5/sam_tc.h index 4154c8175c314..0b361dc301377 100644 --- a/arch/arm/src/samd5e5/sam_tc.h +++ b/arch/arm/src/samd5e5/sam_tc.h @@ -30,6 +30,7 @@ #include #include #include +#include #include "sam_config.h" #include "sam_port.h" @@ -101,7 +102,7 @@ struct sam_tc_dev_s uint32_t freq; /* TC freq */ uint32_t duty; /* TC duty cycle */ - sem_t exclsem; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t waitsem; /* Wait for TC */ bool initialized; /* True: Timer data has been initialized */ diff --git a/arch/arm/src/samd5e5/sam_usb.c b/arch/arm/src/samd5e5/sam_usb.c index 7e751a3c412a6..eb0d963b26fbd 100644 --- a/arch/arm/src/samd5e5/sam_usb.c +++ b/arch/arm/src/samd5e5/sam_usb.c @@ -78,6 +78,7 @@ #include #include #include +#include #include #include #include @@ -649,7 +650,7 @@ struct sam_usbhost_s uint8_t hoststate; /* State of the device (see enum sam_hoststate_e) */ uint8_t prevstate; /* Previous state of the device before SUSPEND */ uint16_t epavail; /* Bitset of available endpoints */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ bool connected; /* Connected to device */ bool change; /* Connection change */ bool pscwait; /* True: Thread is waiting for a port event */ @@ -726,13 +727,6 @@ static inline void sam_modifyreg8(uint32_t clrbits, uint32_t setbits, uintptr_t regaddr); -/* Semaphores */ - -static void sam_takesem(sem_t *sem); -#define sam_givesem(s) nxsem_post(s); -#ifdef CONFIG_USBHOST -#endif - /* Clks */ static void sam_enableclks(void); @@ -1175,34 +1169,6 @@ const struct trace_msg_t g_usb_trace_strings_intdecode[] = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static void sam_takesem(sem_t *sem) -{ - int ret; - - do - { - /* Take the semaphore (perhaps waiting) */ - - ret = nxsem_wait(sem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); -} - /**************************************************************************** * Register Operations ****************************************************************************/ @@ -5185,7 +5151,7 @@ static void sam_pipe_wakeup(struct sam_usbhost_s *priv, SAM_VTRACE2_PIPEWAKEUP_OUT, pipe->epno, pipe->result); - sam_givesem(&pipe->waitsem); + nxsem_post(&pipe->waitsem); pipe->waiter = false; } @@ -6717,7 +6683,7 @@ static void sam_gint_connected(struct sam_usbhost_s *priv) priv->smstate = SMSTATE_ATTACHED; if (priv->pscwait) { - sam_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -6767,7 +6733,7 @@ static void sam_gint_disconnected(struct sam_usbhost_s *priv) if (priv->pscwait) { - sam_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -6859,7 +6825,7 @@ static int sam_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - sam_takesem(&priv->pscsem); + nxsem_wait_uninterruptible(&priv->pscsem); } } @@ -7044,7 +7010,7 @@ static int sam_ep0configure(struct usbhost_driver_s *drvr, * hardware and state structures */ - sam_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Configure the EP0 pipe */ @@ -7054,8 +7020,7 @@ static int sam_ep0configure(struct usbhost_driver_s *drvr, pipe->maxpacket = maxpacketsize; sam_pipe_configure(priv, pipe->idx); - sam_givesem(&priv->exclsem); - + nxmutex_unlock(&priv->lock); return OK; } @@ -7102,7 +7067,7 @@ static int sam_epalloc(struct usbhost_driver_s *drvr, * host hardware and state structures */ - sam_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Handler control pipes differently from other endpoint types. This is * because the normal, "transfer" endpoints are unidirectional an require @@ -7119,7 +7084,7 @@ static int sam_epalloc(struct usbhost_driver_s *drvr, ret = sam_xfrep_alloc(priv, epdesc, ep); } - sam_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -7153,13 +7118,13 @@ static int sam_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) * USB host hardware and state structures */ - sam_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Halt the pipe and mark the pipe available */ sam_pipe_free(priv, (intptr_t)ep); - sam_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -7406,7 +7371,7 @@ static int sam_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * host hardware and state structures */ - sam_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Loop, retrying until the retry time expires */ @@ -7446,7 +7411,7 @@ static int sam_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactions exit here */ - sam_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -7465,7 +7430,7 @@ static int sam_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * and timeouts have been exhausted */ - sam_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -7498,7 +7463,7 @@ static int sam_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * USB host hardware and state structures */ - sam_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Loop, retrying until the retry time expires */ @@ -7541,7 +7506,7 @@ static int sam_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactins exit here */ - sam_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -7560,7 +7525,7 @@ static int sam_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, * and timeouts have been exhausted */ - sam_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -7622,7 +7587,7 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, * USB host hardware and state structures */ - sam_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Handle IN and OUT transfer slightly differently */ @@ -7635,7 +7600,7 @@ static ssize_t sam_transfer(struct usbhost_driver_s *drvr, nbytes = sam_out_transfer(priv, pipe, buffer, buflen); } - sam_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nbytes; } @@ -7694,7 +7659,7 @@ static int sam_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, * USB host hardware and state structures */ - sam_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Handle IN and OUT transfer slightly differently */ @@ -7707,7 +7672,7 @@ static int sam_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, ret = sam_out_asynch(priv, pipe, buffer, buflen, callback, arg); } - sam_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -7766,7 +7731,7 @@ static int sam_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake'em up! */ - sam_givesem(&pipe->waitsem); + nxsem_post(&pipe->waitsem); pipe->waiter = false; } @@ -7843,7 +7808,7 @@ static int sam_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - sam_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -8566,10 +8531,10 @@ static inline void sam_sw_initialize(struct sam_usbhost_s *priv) sam_reset_pipes(priv, false); - /* Initialize semaphores */ + /* Initialize semaphore & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/samv7/sam_hsmci.c b/arch/arm/src/samv7/sam_hsmci.c index 2517351fd5f58..0ff66d793a2f1 100644 --- a/arch/arm/src/samv7/sam_hsmci.c +++ b/arch/arm/src/samv7/sam_hsmci.c @@ -393,9 +393,6 @@ struct sam_dev_s /* Low-level helpers ********************************************************/ -static int sam_takesem(struct sam_dev_s *priv); -#define sam_givesem(priv) (nxsem_post(&priv->waitsem)) - #ifdef CONFIG_SAMV7_HSMCI_REGDEBUG static bool sam_checkreg(struct sam_dev_s *priv, bool wr, uint32_t value, uint32_t address); @@ -586,27 +583,6 @@ static struct sam_dev_s g_hsmci0; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int sam_takesem(struct sam_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: sam_checkreg * @@ -1306,7 +1282,7 @@ static void sam_endwait(struct sam_dev_s *priv, sdio_eventset_t wkupevent) /* Wake up the waiting thread */ - sam_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2823,7 +2799,7 @@ static sdio_eventset_t sam_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = sam_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started), diff --git a/arch/arm/src/samv7/sam_mcan.c b/arch/arm/src/samv7/sam_mcan.c index 247e83bb09915..582924ce3b3b6 100644 --- a/arch/arm/src/samv7/sam_mcan.c +++ b/arch/arm/src/samv7/sam_mcan.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -890,7 +891,7 @@ struct sam_mcan_s uint8_t nextalloc; /* Number of allocated extended filters */ #endif uint8_t nstdalloc; /* Number of allocated standard filters */ - sem_t locksem; /* Enforces mutually exclusive access */ + mutex_t lock; /* Enforces mutually exclusive access */ sem_t txfsem; /* Used to wait for TX FIFO availability */ uint32_t btp; /* Current bit timing */ uint32_t fbtp; /* Current fast bit timing */ @@ -925,12 +926,6 @@ static void mcan_dumpregs(struct sam_mcan_s *priv, const char *msg); # define mcan_dumpregs(priv,msg) #endif -/* Semaphore helpers */ - -static int mcan_dev_lock(struct sam_mcan_s *priv); -static int mcan_dev_lock_noncancelable(struct sam_mcan_s *priv); -#define mcan_dev_unlock(priv) nxsem_post(&priv->locksem) - static void mcan_buffer_reserve(struct sam_mcan_s *priv); static void mcan_buffer_release(struct sam_mcan_s *priv); @@ -1388,61 +1383,6 @@ static void mcan_dumpregs(struct sam_mcan_s *priv, const char *msg) } #endif -/**************************************************************************** - * Name: mcan_dev_lock - * - * Description: - * Take the semaphore that enforces mutually exclusive access to device - * structures, handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the MCAN peripheral state - * - * Returned Value: - * Normally success (OK) is returned, but the error -ECANCELED may be - * return in the event that task has been canceled. - * - ****************************************************************************/ - -static int mcan_dev_lock(struct sam_mcan_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->locksem); -} - -/**************************************************************************** - * Name: mcan_dev_lock_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int mcan_dev_lock_noncancelable(struct sam_mcan_s *priv) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(&priv->locksem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: mcan_buffer_reserve * @@ -1810,7 +1750,7 @@ static int mcan_add_extfilter(struct sam_mcan_s *priv, /* Get exclusive excess to the MCAN hardware */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1919,13 +1859,13 @@ static int mcan_add_extfilter(struct sam_mcan_s *priv, mcan_putreg(priv, SAM_MCAN_CCCR_OFFSET, regval); } - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return ndx; } } DEBUGASSERT(priv->nextalloc == priv->config->nextfilters); - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -EAGAIN; } #endif @@ -1970,7 +1910,7 @@ static int mcan_del_extfilter(struct sam_mcan_s *priv, int ndx) /* Get exclusive excess to the MCAN hardware */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1985,7 +1925,7 @@ static int mcan_del_extfilter(struct sam_mcan_s *priv, int ndx) { /* No, error out */ - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -ENOENT; } @@ -2044,7 +1984,7 @@ static int mcan_del_extfilter(struct sam_mcan_s *priv, int ndx) *extfilter++ = 0; *extfilter = 0; - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -2081,7 +2021,7 @@ static int mcan_add_stdfilter(struct sam_mcan_s *priv, /* Get exclusive excess to the MCAN hardware */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2186,13 +2126,13 @@ static int mcan_add_stdfilter(struct sam_mcan_s *priv, mcan_putreg(priv, SAM_MCAN_CCCR_OFFSET, regval); } - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return ndx; } } DEBUGASSERT(priv->nstdalloc == priv->config->nstdfilters); - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -EAGAIN; } @@ -2235,7 +2175,7 @@ static int mcan_del_stdfilter(struct sam_mcan_s *priv, int ndx) /* Get exclusive excess to the MCAN hardware */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2250,7 +2190,7 @@ static int mcan_del_stdfilter(struct sam_mcan_s *priv, int ndx) { /* No, error out */ - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -ENOENT; } @@ -2308,7 +2248,7 @@ static int mcan_del_stdfilter(struct sam_mcan_s *priv, int ndx) stdfilter = config->msgram.stdfilters + ndx; *stdfilter = 0; - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -2351,7 +2291,7 @@ static int mcan_start_busoff_recovery_sequence(struct sam_mcan_s *priv) /* Get exclusive access to the MCAN peripheral */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2362,7 +2302,7 @@ static int mcan_start_busoff_recovery_sequence(struct sam_mcan_s *priv) regval = mcan_getreg(priv, SAM_MCAN_PSR_OFFSET); if (!(regval & MCAN_PSR_BO)) { - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -EPERM; } @@ -2372,7 +2312,7 @@ static int mcan_start_busoff_recovery_sequence(struct sam_mcan_s *priv) regval &= ~MCAN_CCCR_INIT; mcan_putreg(priv, SAM_MCAN_CCCR_OFFSET, regval); - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -2407,7 +2347,7 @@ static void mcan_reset(struct can_dev_s *dev) /* Get exclusive access to the MCAN peripheral */ - mcan_dev_lock_noncancelable(priv); + nxmutex_lock(&priv->lock); /* Disable all interrupts */ @@ -2427,7 +2367,7 @@ static void mcan_reset(struct can_dev_s *dev) sam_disableperiph1(priv->config->pid); priv->state = MCAN_STATE_RESET; - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -2463,7 +2403,7 @@ static int mcan_setup(struct can_dev_s *dev) /* Get exclusive access to the MCAN peripheral */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2512,7 +2452,7 @@ static int mcan_setup(struct can_dev_s *dev) up_enable_irq(config->irq0); up_enable_irq(config->irq1); - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -2546,7 +2486,7 @@ static void mcan_shutdown(struct can_dev_s *dev) /* Get exclusive access to the MCAN peripheral */ - mcan_dev_lock_noncancelable(priv); + nxmutex_lock(&priv->lock); /* Disable MCAN interrupts at the NVIC */ @@ -2566,7 +2506,7 @@ static void mcan_shutdown(struct can_dev_s *dev) /* Disable peripheral clocking to the MCAN controller */ sam_disableperiph1(priv->config->pid); - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -3023,7 +2963,7 @@ static int mcan_send(struct can_dev_s *dev, struct can_msg_s *msg) /* Get exclusive access to the MCAN peripheral */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { mcan_buffer_release(priv); @@ -3125,7 +3065,7 @@ static int mcan_send(struct can_dev_s *dev, struct can_msg_s *msg) /* And request to send the packet */ mcan_putreg(priv, SAM_MCAN_TXBAR_OFFSET, (1 << ndx)); - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); /* Report that the TX transfer is complete to the upper half logic. Of * course, the transfer is not complete, but this early notification @@ -3166,7 +3106,7 @@ static bool mcan_txready(struct can_dev_s *dev) /* Get exclusive access to the MCAN peripheral */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return false; @@ -3191,7 +3131,7 @@ static bool mcan_txready(struct can_dev_s *dev) (sval <= priv->config->ntxfifoq)); #endif - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return notfull; } @@ -3229,7 +3169,7 @@ static bool mcan_txempty(struct can_dev_s *dev) /* Get exclusive access to the MCAN peripheral */ - ret = mcan_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return false; @@ -3245,7 +3185,7 @@ static bool mcan_txempty(struct can_dev_s *dev) regval = mcan_getreg(priv, SAM_MCAN_TXFQS_OFFSET); if (((regval & MCAN_TXFQS_TFQF) != 0)) { - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return false; } @@ -3269,7 +3209,7 @@ static bool mcan_txempty(struct can_dev_s *dev) empty = (tffl >= priv->config->ntxfifoq); #endif - mcan_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return empty; } @@ -4375,9 +4315,9 @@ struct can_dev_s *sam_mcan_initialize(int port) return NULL; } - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->locksem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->txfsem, 0, config->ntxfifoq); dev->cd_ops = &g_mcanops; diff --git a/arch/arm/src/samv7/sam_progmem.c b/arch/arm/src/samv7/sam_progmem.c index e789b07799668..7575c750787bc 100644 --- a/arch/arm/src/samv7/sam_progmem.c +++ b/arch/arm/src/samv7/sam_progmem.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -153,33 +153,12 @@ ****************************************************************************/ static uint32_t g_page_buffer[SAMV7_PAGE_WORDS]; -static sem_t g_page_sem; +static mutex_t g_page_lock; /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: page_buffer_lock - * - * Description: - * Get exclusive access to the global page buffer - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int page_buffer_lock(void) -{ - return nxsem_wait_uninterruptible(&g_page_sem); -} - -#define page_buffer_unlock() nxsem_post(&g_page_sem) - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -212,11 +191,11 @@ void sam_progmem_initialize(void) regval &= ~EEFC_FMR_FRDY; sam_eefc_writefmr(regval); - /* Initialize the semaphore that manages exclusive access to the global + /* Initialize the mutex that manages exclusive access to the global * page buffer. */ - nxsem_init(&g_page_sem, 0, 1); + nxmutex_init(&g_page_lock); } /**************************************************************************** @@ -526,7 +505,7 @@ ssize_t up_progmem_write(size_t address, const void *buffer, size_t buflen) /* Get exclusive access to the global page buffer */ - ret = page_buffer_lock(); + ret = nxmutex_lock(&g_page_lock); if (ret < 0) { return (ssize_t)ret; @@ -610,7 +589,7 @@ ssize_t up_progmem_write(size_t address, const void *buffer, size_t buflen) page++; } - page_buffer_unlock(); + nxmutex_unlock(&g_page_lock); return written; } diff --git a/arch/arm/src/samv7/sam_qspi.c b/arch/arm/src/samv7/sam_qspi.c index 47a00f8f6acc5..6baeec3f60f6a 100644 --- a/arch/arm/src/samv7/sam_qspi.c +++ b/arch/arm/src/samv7/sam_qspi.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -169,7 +170,7 @@ struct sam_qspidev_s uint8_t irq; /* Interrupt number */ #endif bool initialized; /* TRUE: Controller has been initialized */ - sem_t exclsem; /* Assures mutually exclusive access to QSPI */ + mutex_t lock; /* Assures mutually exclusive access to QSPI */ #ifdef CONFIG_SAMV7_QSPI_DMA bool candma; /* DMA is supported */ @@ -1048,11 +1049,11 @@ static int qspi_lock(struct qspi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1755,11 +1756,11 @@ struct qspi_dev_s *sam_qspi_initialize(int intf) { /* No perform one time initialization */ - /* Initialize the QSPI semaphore that enforces mutually exclusive + /* Initialize the QSPI mutex that enforces mutually exclusive * access to the QSPI registers. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_SAMV7_QSPI_DMA /* Pre-allocate DMA channels. */ @@ -1830,7 +1831,7 @@ struct qspi_dev_s *sam_qspi_initialize(int intf) } #endif - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); return NULL; } #endif /* CONFIG_SAMV7_QSPI */ diff --git a/arch/arm/src/samv7/sam_qspi_spi.c b/arch/arm/src/samv7/sam_qspi_spi.c index ba72cd10f8530..dc6086ffab1fa 100644 --- a/arch/arm/src/samv7/sam_qspi_spi.c +++ b/arch/arm/src/samv7/sam_qspi_spi.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -86,7 +86,7 @@ typedef void (*select_t)(uint32_t devid, bool selected); struct sam_spidev_s { uint32_t base; /* SPI controller register base address */ - sem_t spisem; /* Assures mutually exclusive access to SPI */ + mutex_t spilock; /* Assures mutually exclusive access to SPI */ select_t select; /* SPI select call-out */ bool initialized; /* TRUE: Controller has been initialized */ bool escape_lastxfer; /* Don't set LASTXFER-Bit in the next transfer */ @@ -257,11 +257,11 @@ static int qspi_spi_lock(struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&spi->spisem); + ret = nxmutex_lock(&spi->spilock); } else { - ret = nxsem_post(&spi->spisem); + ret = nxmutex_unlock(&spi->spilock); } return ret; @@ -832,11 +832,11 @@ struct spi_dev_s *sam_qspi_spi_initialize(int intf) qspi_getreg(spi, SAM_QSPI_SR_OFFSET); qspi_getreg(spi, SAM_QSPI_RDR_OFFSET); - /* Initialize the SPI semaphore that enforces mutually exclusive + /* Initialize the SPI mutex that enforces mutually exclusive * access to the SPI registers. */ - nxsem_init(&spi->spisem, 0, 1); + nxmutex_init(&spi->spilock); spi->escape_lastxfer = false; spi->initialized = true; } diff --git a/arch/arm/src/samv7/sam_spi.c b/arch/arm/src/samv7/sam_spi.c index 14c32b5235e9b..819a6b95b942c 100644 --- a/arch/arm/src/samv7/sam_spi.c +++ b/arch/arm/src/samv7/sam_spi.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -176,7 +176,7 @@ typedef void (*select_t)(uint32_t devid, bool selected); struct sam_spidev_s { uint32_t base; /* SPI controller register base address */ - sem_t spisem; /* Assures mutually exclusive access to SPI */ + mutex_t spilock; /* Assures mutually exclusive access to SPI */ select_t select; /* SPI select call-out */ bool initialized; /* TRUE: Controller has been initialized */ bool escape_lastxfer; /* Don't set LASTXFER-Bit in the next transfer */ @@ -902,11 +902,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&spi->spisem); + ret = nxmutex_lock(&spi->spilock); } else { - ret = nxsem_post(&spi->spisem); + ret = nxmutex_unlock(&spi->spilock); } return ret; @@ -2156,11 +2156,11 @@ struct spi_dev_s *sam_spibus_initialize(int port) spi_getreg(spi, SAM_SPI_SR_OFFSET); spi_getreg(spi, SAM_SPI_RDR_OFFSET); - /* Initialize the SPI semaphore that enforces mutually exclusive + /* Initialize the SPI mutex that enforces mutually exclusive * access to the SPI registers. */ - nxsem_init(&spi->spisem, 0, 1); + nxmutex_init(&spi->spilock); spi->escape_lastxfer = false; spi->initialized = true; diff --git a/arch/arm/src/samv7/sam_spi_slave.c b/arch/arm/src/samv7/sam_spi_slave.c index c27846c55f909..c4647610bcd9b 100644 --- a/arch/arm/src/samv7/sam_spi_slave.c +++ b/arch/arm/src/samv7/sam_spi_slave.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -80,7 +80,7 @@ struct sam_spidev_s struct spi_slave_dev_s *dev; xcpt_t handler; /* SPI interrupt handler */ uint32_t base; /* SPI controller register base address */ - sem_t spisem; /* Assures mutually exclusive access to SPI */ + mutex_t spilock; /* Assures mutually exclusive access to SPI */ uint16_t outval; /* Default shift-out value */ uint16_t irq; /* SPI IRQ number */ uint8_t mode; /* Mode 0,1,2,3 */ @@ -130,10 +130,6 @@ static void spi_dumpregs(struct sam_spidev_s *priv, const char *msg); # define spi_dumpregs(priv,msg) #endif -static int spi_semtake(struct sam_spidev_s *priv); -static void spi_semtake_noncancelable(struct sam_spidev_s *priv); -#define spi_semgive(priv) (nxsem_post(&(priv)->spisem)) - /* Interrupt Handling */ static int spi_interrupt(int irq, void *context, void *arg); @@ -335,56 +331,6 @@ static void spi_dumpregs(struct sam_spidev_s *priv, const char *msg) } #endif -/**************************************************************************** - * Name: spi_semtake - * - * Description: - * Take the semaphore that enforces mutually exclusive access to SPI - * resources. May return ECANCELED if the calling thread was canceled. - * - * Input Parameters: - * priv - A reference to the MCAN peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int spi_semtake(struct sam_spidev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->spisem); -} - -/**************************************************************************** - * Name: spi_semtake_noncancelable - * - * Description: - * Take the semaphore that enforces mutually exclusive access to SPI - * resources, handling any exceptional conditions. Always successful. - * - * Input Parameters: - * priv - A reference to the MCAN peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void spi_semtake_noncancelable(struct sam_spidev_s *priv) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(&priv->spisem); - - /* ECANCELED is the only error expected here */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: spi_interrupt * @@ -781,14 +727,14 @@ static void spi_bind(struct spi_slave_ctrlr_s *ctrlr, /* Get exclusive access to the SPI device */ - ret = spi_semtake(priv); + ret = nxmutex_lock(&priv->spilock); if (ret < 0) { /* REVISIT: No mechanism to report error. This error should only * occur if the calling task was canceled. */ - spierr("RROR: spi_semtake failed: %d\n", ret); + spierr("RROR: nxmutex_lock failed: %d\n", ret); return; } @@ -863,7 +809,7 @@ static void spi_bind(struct spi_slave_ctrlr_s *ctrlr, spi_putreg(priv, regval, SAM_SPI_IER_OFFSET); - spi_semgive(priv); + nxmutex_unlock(&priv->spilock); } /**************************************************************************** @@ -893,7 +839,7 @@ static void spi_unbind(struct spi_slave_ctrlr_s *ctrlr) /* Get exclusive access to the SPI device */ - spi_semtake_noncancelable(priv); + nxmutex_lock(&priv->spilock); /* Disable SPI interrupts (still enabled at the NVIC) */ @@ -912,7 +858,7 @@ static void spi_unbind(struct spi_slave_ctrlr_s *ctrlr) spi_putreg(priv, SPI_CR_SWRST, SAM_SPI_CR_OFFSET); spi_putreg(priv, SPI_CR_SWRST, SAM_SPI_CR_OFFSET); - spi_semgive(priv); + nxmutex_unlock(&priv->spilock); } /**************************************************************************** @@ -953,7 +899,7 @@ static int spi_enqueue(struct spi_slave_ctrlr_s *ctrlr, /* Get exclusive access to the SPI device */ - ret = spi_semtake(priv); + ret = nxmutex_lock(&priv->spilock); if (ret < 0) { return ret; @@ -998,7 +944,7 @@ static int spi_enqueue(struct spi_slave_ctrlr_s *ctrlr, } leave_critical_section(flags); - spi_semgive(priv); + nxmutex_unlock(&priv->spilock); return ret; } @@ -1029,14 +975,14 @@ static bool spi_qfull(struct spi_slave_ctrlr_s *ctrlr) /* Get exclusive access to the SPI device */ - ret = spi_semtake(priv); + ret = nxmutex_lock(&priv->spilock); if (ret < 0) { /* REVISIT: No mechanism to report error. This error should only * occurr if the calling task was canceled. */ - spierr("RROR: spi_semtake failed: %d\n", ret); + spierr("RROR: nxmutex_lock failed: %d\n", ret); return true; } @@ -1054,7 +1000,7 @@ static bool spi_qfull(struct spi_slave_ctrlr_s *ctrlr) bret = (next == priv->tail); leave_critical_section(flags); - spi_semgive(priv); + nxmutex_unlock(&priv->spilock); return bret; } @@ -1085,7 +1031,7 @@ static void spi_qflush(struct spi_slave_ctrlr_s *ctrlr) /* Get exclusive access to the SPI device */ - spi_semtake_noncancelable(priv); + nxmutex_lock(&priv->spilock); /* Mark the buffer empty, momentarily disabling interrupts */ @@ -1093,7 +1039,7 @@ static void spi_qflush(struct spi_slave_ctrlr_s *ctrlr) priv->head = 0; priv->tail = 0; leave_critical_section(flags); - spi_semgive(priv); + nxmutex_unlock(&priv->spilock); } /**************************************************************************** @@ -1241,11 +1187,11 @@ struct spi_slave_ctrlr_s *sam_spi_slave_initialize(int port) spi_getreg(priv, SAM_SPI_SR_OFFSET); spi_getreg(priv, SAM_SPI_RDR_OFFSET); - /* Initialize the SPI semaphore that enforces mutually exclusive + /* Initialize the SPI mutex that enforces mutually exclusive * access to the SPI registers. */ - nxsem_init(&priv->spisem, 0, 1); + nxmutex_init(&priv->spilock); priv->nss = true; priv->initialized = true; diff --git a/arch/arm/src/samv7/sam_ssc.c b/arch/arm/src/samv7/sam_ssc.c index e063c055bc8e0..55bb38ff6c57e 100644 --- a/arch/arm/src/samv7/sam_ssc.c +++ b/arch/arm/src/samv7/sam_ssc.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include "arm_internal.h" @@ -415,7 +416,7 @@ struct sam_ssc_s { struct i2s_dev_s dev; /* Externally visible I2S interface */ uintptr_t base; /* SSC controller register base address */ - sem_t exclsem; /* Assures mutually exclusive access to SSC */ + mutex_t lock; /* Assures mutually exclusive access to SSC */ uint8_t datalen; /* Data width (8, 16, or 32) */ uint8_t align; /* Log2 of data width (0, 1, or 3) */ uint8_t pid; /* Peripheral ID */ @@ -503,14 +504,6 @@ static void ssc_dump_queues(struct sam_transport_s *xpt, # define ssc_dump_buffer(m,b,s) #endif -/* Semaphore helpers */ - -static int ssc_exclsem_take(struct sam_ssc_s *priv); -#define ssc_exclsem_give(priv) nxsem_post(&priv->exclsem) - -static int ssc_bufsem_take(struct sam_ssc_s *priv); -#define ssc_bufsem_give(priv) nxsem_post(&priv->bufsem) - /* Buffer container helpers */ static struct sam_buffer_s * @@ -835,46 +828,6 @@ static void ssc_dump_queues(struct sam_transport_s *xpt, const char *msg) } #endif -/**************************************************************************** - * Name: ssc_exclsem_take - * - * Description: - * Take the exclusive access semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the SSC peripheral state - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int ssc_exclsem_take(struct sam_ssc_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -/**************************************************************************** - * Name: ssc_bufsem_take - * - * Description: - * Take the buffer semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the SSC peripheral state - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int ssc_bufsem_take(struct sam_ssc_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->bufsem); -} - /**************************************************************************** * Name: ssc_buf_allocate * @@ -905,7 +858,7 @@ static struct sam_buffer_s *ssc_buf_allocate(struct sam_ssc_s *priv) * have at least one free buffer container. */ - ret = ssc_bufsem_take(priv); + ret = nxsem_wait_uninterruptible(&priv->bufsem); if (ret < 0) { return NULL; @@ -956,7 +909,7 @@ static void ssc_buf_free(struct sam_ssc_s *priv, /* Wake up any threads waiting for a buffer container */ - ssc_bufsem_give(priv); + nxsem_post(&priv->bufsem); } /**************************************************************************** @@ -2182,7 +2135,7 @@ static int ssc_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the SSC driver data */ - ret = ssc_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout_with_buf; @@ -2194,7 +2147,7 @@ static int ssc_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: SSC%d has no receiver\n", priv->sscno); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Add a reference to the audio buffer */ @@ -2222,11 +2175,11 @@ static int ssc_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, ret = ssc_rxdma_setup(priv); DEBUGASSERT(ret == OK); leave_critical_section(flags); - ssc_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - ssc_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); errout_with_buf: ssc_buf_free(priv, bfcontainer); @@ -2408,7 +2361,7 @@ static int ssc_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the SSC driver data */ - ret = ssc_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout_with_buf; @@ -2420,7 +2373,7 @@ static int ssc_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: SSC%d has no transmitter\n", priv->sscno); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Add a reference to the audio buffer */ @@ -2448,11 +2401,11 @@ static int ssc_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, ret = ssc_txdma_setup(priv); DEBUGASSERT(ret == OK); leave_critical_section(flags); - ssc_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - ssc_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); errout_with_buf: ssc_buf_free(priv, bfcontainer); @@ -3378,7 +3331,7 @@ struct i2s_dev_s *sam_ssc_initialize(int port) /* Initialize the common parts for the SSC device structure */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->dev.ops = &g_sscops; priv->sscno = port; @@ -3450,7 +3403,7 @@ struct i2s_dev_s *sam_ssc_initialize(int port) ssc_dma_free(priv); errout_with_alloc: - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return NULL; } diff --git a/arch/arm/src/samv7/sam_tc.c b/arch/arm/src/samv7/sam_tc.c index ccfe322939b91..c40713902eb28 100644 --- a/arch/arm/src/samv7/sam_tc.c +++ b/arch/arm/src/samv7/sam_tc.c @@ -127,7 +127,7 @@ struct sam_chan_s struct sam_tc_s { - sem_t exclsem; /* Assures mutually exclusive access to TC */ + mutex_t lock; /* Assures mutually exclusive access to TC */ uintptr_t base; /* Register base address */ uint8_t tc; /* Timer/channel number {0, 1, 2, 3} */ bool initialized; /* True: Timer/counter has been initialized */ @@ -158,11 +158,6 @@ struct mck_divsrc_s * Private Function Prototypes ****************************************************************************/ -/* Low-level helpers ********************************************************/ - -static int sam_takesem(struct sam_tc_s *tc); -#define sam_givesem(tc) (nxsem_post(&tc->exclsem)) - #ifdef CONFIG_SAMV7_TC_REGDEBUG static void sam_regdump(struct sam_chan_s *chan, const char *msg); static bool sam_checkreg(struct sam_tc_s *tc, bool wr, uint32_t regaddr, @@ -572,26 +567,6 @@ static const uint8_t g_regoffset[TC_NREGISTERS] = * Low-level Helpers ****************************************************************************/ -/**************************************************************************** - * Name: sam_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int sam_takesem(struct sam_tc_s *tc) -{ - return nxsem_wait_uninterruptible(&tc->exclsem); -} - /**************************************************************************** * Name: sam_regdump * @@ -1083,7 +1058,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) /* Initialize the timer counter data structure. */ memset(tc, 0, sizeof(struct sam_tc_s)); - nxsem_init(&tc->exclsem, 0, 1); + nxmutex_init(&tc->lock); tc->base = tcconfig->base; tc->tc = tcconfig->tc; @@ -1113,7 +1088,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) /* Get exclusive access to the timer/count data structure */ - ret = sam_takesem(tc); + ret = nxmutex_lock(&tc->lock); if (ret < 0) { leave_critical_section(flags); @@ -1132,7 +1107,7 @@ static inline struct sam_chan_s *sam_tc_initialize(int channel) /* Yes.. return a failure */ tmrerr("ERROR: Channel %d is in-use\n", channel); - sam_givesem(tc); + nxmutex_unlock(&tc->lock); return NULL; } @@ -1246,7 +1221,7 @@ TC_HANDLE sam_tc_allocate(int channel, int mode) sam_chan_putreg(chan, SAM_TC_CMR_OFFSET, mode); sam_regdump(chan, "Allocated"); - sam_givesem(chan->tc); + nxmutex_unlock(&chan->tc->lock); } /* Return an opaque reference to the channel */ diff --git a/arch/arm/src/samv7/sam_trng.c b/arch/arm/src/samv7/sam_trng.c index 6a416ced220e9..71848cfb98a68 100644 --- a/arch/arm/src/samv7/sam_trng.c +++ b/arch/arm/src/samv7/sam_trng.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -61,7 +62,7 @@ static ssize_t sam_read(struct file *filep, char *buffer, size_t); struct trng_dev_s { - sem_t exclsem; /* Enforces exclusive access to the TRNG */ + mutex_t lock; /* Enforces exclusive access to the TRNG */ sem_t waitsem; /* Wait for buffer full */ uint32_t *samples; /* Current buffer being filled */ size_t maxsamples; /* Size of the current buffer (in 32-bit words) */ @@ -236,7 +237,7 @@ static ssize_t sam_read(struct file *filep, char *buffer, size_t buflen) /* Get exclusive access to the TRNG hardware */ - ret = nxsem_wait(&g_trngdev.exclsem); + ret = nxmutex_lock(&g_trngdev.lock); if (ret < 0) { /* This is probably -EINTR meaning that we were awakened by a signal */ @@ -308,7 +309,7 @@ static ssize_t sam_read(struct file *filep, char *buffer, size_t buflen) /* Release our lock on the TRNG hardware */ - nxsem_post(&g_trngdev.exclsem); + nxmutex_unlock(&g_trngdev.lock); finfo("Return %d\n", (int)retval); return retval; @@ -338,9 +339,9 @@ static int sam_rng_initialize(void) memset(&g_trngdev, 0, sizeof(struct trng_dev_s)); - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&g_trngdev.exclsem, 0, 1); + nxmutex_init(&g_trngdev.lock); nxsem_init(&g_trngdev.waitsem, 0, 0); /* The waitsem semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/samv7/sam_twihs.c b/arch/arm/src/samv7/sam_twihs.c index df0bce2d8605d..9165ae1d2f60d 100644 --- a/arch/arm/src/samv7/sam_twihs.c +++ b/arch/arm/src/samv7/sam_twihs.c @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -160,7 +161,7 @@ struct twi_dev_s int refs; /* Reference count */ uint8_t msgc; /* Number of message in the message list */ - sem_t exclsem; /* Only one thread can access at a time */ + mutex_t lock; /* Only one thread can access at a time */ sem_t waitsem; /* Wait for TWIHS transfer completion */ struct wdog_s timeout; /* Watchdog to recover from bus hangs */ volatile int result; /* The result of the transfer */ @@ -182,9 +183,6 @@ struct twi_dev_s /* Low-level helper functions */ -static int twi_takesem(sem_t *sem); -#define twi_givesem(sem) (nxsem_post(sem)) - #ifdef CONFIG_SAMV7_TWIHSHS_REGDEBUG static bool twi_checkreg(struct twi_dev_s *priv, bool wr, uint32_t value, uintptr_t address); @@ -248,7 +246,13 @@ static const struct twi_attr_s g_twi0attr = .base = SAM_TWIHS0_BASE, }; -static struct twi_dev_s g_twi0; +static struct twi_dev_s g_twi0 = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_SAMV7_TWIHS1 @@ -268,7 +272,13 @@ static const struct twi_attr_s g_twi1attr = .base = SAM_TWIHS1_BASE, }; -static struct twi_dev_s g_twi1; +static struct twi_dev_s g_twi1 = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_SAMV7_TWIHS2 @@ -288,7 +298,13 @@ static const struct twi_attr_s g_twi2attr = .base = SAM_TWIHS2_BASE, }; -static struct twi_dev_s g_twi2; +static struct twi_dev_s g_twi2 = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif static const struct i2c_ops_s g_twiops = @@ -303,25 +319,6 @@ static const struct i2c_ops_s g_twiops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: twi_takesem - * - * Description: - * Take the wait semaphore. May be interrupted by a signal. - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int twi_takesem(sem_t *sem) -{ - return nxsem_wait(sem); -} - /**************************************************************************** * Name: twi_checkreg * @@ -494,7 +491,7 @@ static int twi_wait(struct twi_dev_s *priv, unsigned int size) do { i2cinfo("TWIHS%d Waiting...\n", priv->attr->twi); - ret = twi_takesem(&priv->waitsem); + ret = nxsem_wait(&priv->waitsem); i2cinfo("TWIHS%d Awakened with result: %d\n", priv->attr->twi, priv->result); @@ -554,7 +551,7 @@ static void twi_wakeup(struct twi_dev_s *priv, int result) /* Wake up the waiting thread with the result of the transfer */ priv->result = result; - twi_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -918,7 +915,7 @@ static int twi_transfer(struct i2c_master_s *dev, /* Get exclusive access to the device */ - ret = twi_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -980,7 +977,7 @@ static int twi_transfer(struct i2c_master_s *dev, i2cerr("ERROR: Transfer failed: %d\n", ret); } - twi_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1140,7 +1137,7 @@ static int twi_reset(struct i2c_master_s *dev) /* Get exclusive access to the TWIHS device */ - ret = twi_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret >= 0) { /* Do the reset-procedure */ @@ -1149,7 +1146,7 @@ static int twi_reset(struct i2c_master_s *dev) /* Release our lock on the bus */ - twi_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } return ret; @@ -1441,17 +1438,6 @@ struct i2c_master_s *sam_i2cbus_initialize(int bus) priv->dev.ops = &g_twiops; - /* Initialize semaphores */ - - nxsem_init(&priv->exclsem, 0, 1); - nxsem_init(&priv->waitsem, 0, 0); - - /* The waitsem semaphore is used for signaling and, hence, should not - * have priority inheritance enabled. - */ - - nxsem_set_protocol(&priv->waitsem, SEM_PRIO_NONE); - /* Perform repeatable TWIHS hardware initialization */ twi_hw_initialize(priv, frequency); @@ -1498,11 +1484,6 @@ int sam_i2cbus_uninitialize(struct i2c_master_s *dev) { up_disable_irq(priv->attr->irq); - /* Reset data structures */ - - nxsem_destroy(&priv->exclsem); - nxsem_destroy(&priv->waitsem); - /* Cancel the watchdog timer */ wd_cancel(&priv->timeout); diff --git a/arch/arm/src/samv7/sam_usbdevhs.c b/arch/arm/src/samv7/sam_usbdevhs.c index 84828c65b9ff4..438235798c3ad 100644 --- a/arch/arm/src/samv7/sam_usbdevhs.c +++ b/arch/arm/src/samv7/sam_usbdevhs.c @@ -914,7 +914,7 @@ static void sam_dumpep(struct sam_usbdev_s *priv, int epno) * Description: * Allocate a DMA transfer descriptor by removing it from the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ @@ -942,7 +942,7 @@ static struct sam_dtd_s *sam_dtd_alloc(struct sam_usbdev_s *priv) * Description: * Free a DMA transfer descriptor by returning it to the free list * - * Assumption: Caller holds the exclsem + * Assumption: Caller holds the lock * ****************************************************************************/ diff --git a/arch/arm/src/samv7/sam_xdmac.c b/arch/arm/src/samv7/sam_xdmac.c index 18b3cf61ef631..a8dcb5626e3cc 100644 --- a/arch/arm/src/samv7/sam_xdmac.c +++ b/arch/arm/src/samv7/sam_xdmac.c @@ -111,9 +111,9 @@ struct sam_xdmach_s struct sam_xdmac_s { - /* These semaphores protect the DMA channel and descriptor tables */ + /* These mutex protect the DMA channel and descriptor tables */ - sem_t chsem; /* Protects channel table */ + mutex_t chlock; /* Protects channel table */ sem_t dsem; /* Protects descriptor table */ }; @@ -349,24 +349,6 @@ static struct sam_xdmac_s g_xdmac; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: sam_takechsem() and sam_givechsem() - * - * Description: - * Used to get exclusive access to the DMA channel table - * - ****************************************************************************/ - -static int sam_takechsem(struct sam_xdmac_s *xdmac) -{ - return nxsem_wait_uninterruptible(&xdmac->chsem); -} - -static inline void sam_givechsem(struct sam_xdmac_s *xdmac) -{ - nxsem_post(&xdmac->chsem); -} - /**************************************************************************** * Name: sam_takedsem() and sam_givedsem() * @@ -1005,7 +987,7 @@ sam_allocdesc(struct sam_xdmach_s *xdmach, struct chnext_view1_s *prev, * that is an atomic operation. */ - ret = sam_takechsem(xdmac); + ret = nxmutex_lock(&xdmac->chlock); if (ret < 0) { sam_givedsem(xdmac); @@ -1077,7 +1059,7 @@ sam_allocdesc(struct sam_xdmach_s *xdmach, struct chnext_view1_s *prev, * search loop should always be successful. */ - sam_givechsem(xdmac); + nxmutex_unlock(&xdmac->chlock); DEBUGASSERT(descr != NULL); } @@ -1580,9 +1562,9 @@ void sam_dmainitialize(struct sam_xdmac_s *xdmac) sam_putdmac(xdmac, XDMAC_CHAN_ALL, SAM_XDMAC_GD_OFFSET); - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&xdmac->chsem, 0, 1); + nxmutex_init(&xdmac->chlock); nxsem_init(&xdmac->dsem, 0, SAMV7_NDMACHAN); /* The 'dsem' is used for signaling rather than mutual exclusion and, @@ -1657,7 +1639,7 @@ DMA_HANDLE sam_dmachannel(uint8_t dmacno, uint32_t chflags) */ xdmach = NULL; - ret = sam_takechsem(xdmac); + ret = nxmutex_lock(&xdmac->chlock); if (ret < 0) { return NULL; @@ -1690,7 +1672,7 @@ DMA_HANDLE sam_dmachannel(uint8_t dmacno, uint32_t chflags) } } - sam_givechsem(xdmac); + nxmutex_unlock(&xdmac->chlock); /* Show the result of the allocation */ diff --git a/arch/arm/src/stm32/stm32_1wire.c b/arch/arm/src/stm32/stm32_1wire.c index d8697ba496228..4351a9ae2c56d 100644 --- a/arch/arm/src/stm32/stm32_1wire.c +++ b/arch/arm/src/stm32/stm32_1wire.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -115,7 +116,7 @@ struct stm32_1wire_priv_s { const struct stm32_1wire_config_s *config; /* Port configuration */ volatile int refs; /* Reference count */ - sem_t sem_excl; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ sem_t sem_isr; /* Interrupt wait semaphore */ int baud; /* Baud rate */ const struct stm32_1wire_msg_s *msgs; /* Messages data */ @@ -147,11 +148,6 @@ static void stm32_1wire_set_apb_clock(struct stm32_1wire_priv_s *priv, bool on); static int stm32_1wire_init(struct stm32_1wire_priv_s *priv); static int stm32_1wire_deinit(struct stm32_1wire_priv_s *priv); -static inline void stm32_1wire_sem_init(struct stm32_1wire_priv_s *priv); -static inline void stm32_1wire_sem_destroy( - struct stm32_1wire_priv_s *priv); -static inline int stm32_1wire_sem_wait(struct stm32_1wire_priv_s *priv); -static inline void stm32_1wire_sem_post(struct stm32_1wire_priv_s *priv); static int stm32_1wire_process(struct stm32_1wire_priv_s *priv, const struct stm32_1wire_msg_s *msgs, int count); @@ -188,6 +184,8 @@ static struct stm32_1wire_priv_s stm32_1wire1_priv = { .config = &stm32_1wire1_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL }; @@ -207,6 +205,8 @@ static struct stm32_1wire_priv_s stm32_1wire2_priv = { .config = &stm32_1wire2_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL }; @@ -226,6 +226,8 @@ static struct stm32_1wire_priv_s stm32_1wire3_priv = { .config = &stm32_1wire3_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL }; @@ -245,6 +247,8 @@ static struct stm32_1wire_priv_s stm32_1wire4_priv = { .config = &stm32_1wire4_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL }; @@ -264,6 +268,8 @@ static struct stm32_1wire_priv_s stm32_1wire5_priv = { .config = &stm32_1wire5_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL }; @@ -283,6 +289,8 @@ static struct stm32_1wire_priv_s stm32_1wire6_priv = { .config = &stm32_1wire6_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL }; @@ -302,6 +310,8 @@ static struct stm32_1wire_priv_s stm32_1wire7_priv = { .config = &stm32_1wire7_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL }; @@ -321,6 +331,8 @@ static struct stm32_1wire_priv_s stm32_1wire8_priv = { .config = &stm32_1wire8_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL }; @@ -703,67 +715,6 @@ static int stm32_1wire_deinit(struct stm32_1wire_priv_s *priv) return OK; } -/**************************************************************************** - * Name: stm32_1wire_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_1wire_sem_init(struct stm32_1wire_priv_s *priv) -{ - nxsem_init(&priv->sem_excl, 0, 1); - nxsem_init(&priv->sem_isr, 0, 0); - - /* The sem_isr semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ - - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); -} - -/**************************************************************************** - * Name: stm32_1wire_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_1wire_sem_destroy( - struct stm32_1wire_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); - nxsem_destroy(&priv->sem_isr); -} - -/**************************************************************************** - * Name: stm32_1wire_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary - * - ****************************************************************************/ - -static inline int stm32_1wire_sem_wait(struct stm32_1wire_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_1wire_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_1wire_sem_post(struct stm32_1wire_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - /**************************************************************************** * Name: stm32_1wire_exec * @@ -781,7 +732,7 @@ static int stm32_1wire_process(struct stm32_1wire_priv_s *priv, /* Lock out other clients */ - ret = stm32_1wire_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -877,8 +828,7 @@ static int stm32_1wire_process(struct stm32_1wire_priv_s *priv, /* Release the port for re-use by other clients */ - stm32_1wire_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -1275,8 +1225,8 @@ struct onewire_dev_s *stm32_1wireinitialize(int port) /* Initialize instance */ - inst->ops = &stm32_1wire_ops; - inst->priv = priv; + inst->ops = &stm32_1wire_ops; + inst->priv = priv; /* Initialize private data for the first time, increment reference count, * power-up hardware and configure GPIOs. @@ -1286,7 +1236,6 @@ struct onewire_dev_s *stm32_1wireinitialize(int port) if (priv->refs++ == 0) { - stm32_1wire_sem_init(priv); stm32_1wire_init(priv); } @@ -1337,10 +1286,7 @@ int stm32_1wireuninitialize(struct onewire_dev_s *dev) /* Disable power and other HW resource (GPIO's) */ stm32_1wire_deinit(priv); - - /* Release unused resources */ - - stm32_1wire_sem_destroy(priv); + nxmutex_unlock(&priv->lock); /* Free instance */ diff --git a/arch/arm/src/stm32/stm32_adc.c b/arch/arm/src/stm32/stm32_adc.c index d16c605b008df..a7a81db8c6490 100644 --- a/arch/arm/src/stm32/stm32_adc.c +++ b/arch/arm/src/stm32/stm32_adc.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include "arm_internal.h" #include "chip.h" @@ -379,7 +379,7 @@ struct adccmn_data_s { uint8_t refcount; /* How many ADC instances are currently in use */ - sem_t lock; /* Exclusive access to common ADC data */ + mutex_t lock; /* Exclusive access to common ADC data */ }; #endif @@ -497,10 +497,6 @@ static void tim_modifyreg(struct stm32_dev_s *priv, int offset, static void tim_dumpregs(struct stm32_dev_s *priv, const char *msg); #endif -#ifdef HAVE_ADC_CMN_DATA -static int adccmn_lock(struct stm32_dev_s *priv, bool lock); -#endif - static void adc_rccreset(struct stm32_dev_s *priv, bool reset); /* ADC Interrupt Handler */ @@ -1798,28 +1794,6 @@ static void adc_inj_startconv(struct stm32_dev_s *priv, bool enable) #endif /* ADC_HAVE_INJECTED */ -/**************************************************************************** - * Name: adccmn_lock - ****************************************************************************/ - -#ifdef HAVE_ADC_CMN_DATA -static int adccmn_lock(struct stm32_dev_s *priv, bool lock) -{ - int ret; - - if (lock) - { - ret = nxsem_wait_uninterruptible(&priv->cmn->lock); - } - else - { - ret = nxsem_post(&priv->cmn->lock); - } - - return ret; -} -#endif - /**************************************************************************** * Name: adc_rccreset * @@ -2833,7 +2807,7 @@ static void adc_reset(struct adc_dev_s *dev) /* Only if this is the first initialzied ADC instance in the ADC block */ #ifdef HAVE_ADC_CMN_DATA - if (adccmn_lock(priv, true) < 0) + if (nxmutex_lock(&priv->cmn->lock) < 0) { goto out; } @@ -2851,7 +2825,7 @@ static void adc_reset(struct adc_dev_s *dev) } #ifdef HAVE_ADC_CMN_DATA - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); #endif out: @@ -2963,7 +2937,7 @@ static int adc_setup(struct adc_dev_s *dev) #ifdef HAVE_ADC_CMN_DATA /* Increase instances counter */ - ret = adccmn_lock(priv, true); + ret = nxmutex_lock(&priv->cmn->lock); if (ret < 0) { return ret; @@ -2982,7 +2956,7 @@ static int adc_setup(struct adc_dev_s *dev) #ifdef HAVE_ADC_CMN_DATA priv->cmn->refcount += 1; - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); #endif /* The ADC device is ready */ @@ -3032,7 +3006,7 @@ static void adc_shutdown(struct adc_dev_s *dev) #endif #ifdef HAVE_ADC_CMN_DATA - if (adccmn_lock(priv, true) < 0) + if (nxmutex_lock(&priv->cmn->lock) < 0) { return; } @@ -3076,7 +3050,7 @@ static void adc_shutdown(struct adc_dev_s *dev) priv->cmn->refcount -= 1; } - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); #endif } @@ -4805,7 +4779,7 @@ struct adc_dev_s *stm32_adcinitialize(int intf, const uint8_t *chanlist, * the ADC block. */ - nxsem_init(&priv->cmn->lock, 0, 1); + nxmutex_init(&priv->cmn->lock); #endif return dev; diff --git a/arch/arm/src/stm32/stm32_aes.c b/arch/arm/src/stm32/stm32_aes.c index c88d8d8558b17..b322ec928c54f 100644 --- a/arch/arm/src/stm32/stm32_aes.c +++ b/arch/arm/src/stm32/stm32_aes.c @@ -68,8 +68,8 @@ static int stm32aes_setup_cr(int mode, int encrypt); * Private Data ****************************************************************************/ -static sem_t g_stm32aes_lock; -static bool g_stm32aes_initdone = false; +static mutex_t g_stm32aes_lock; +static bool g_stm32aes_initdone = false; /**************************************************************************** * Public Data @@ -231,7 +231,7 @@ int stm32_aesinitialize(void) { uint32_t regval; - nxsem_init(&g_stm32aes_lock, 0, 1); + nxmutex_init(&g_stm32aes_lock); regval = getreg32(STM32_RCC_AHBENR); regval |= RCC_AHBENR_AESEN; @@ -252,8 +252,7 @@ int stm32_aesuninitialize(void) regval &= ~RCC_AHBENR_AESEN; putreg32(regval, STM32_RCC_AHBENR); - nxsem_destroy(&g_stm32aes_lock); - + nxmutex_destroy(&g_stm32aes_lock); return OK; } @@ -286,7 +285,7 @@ int aes_cypher(void *out, const void *in, size_t size, return -EINVAL; } - ret = nxsem_wait(&g_stm32aes_lock); + ret = nxmutex_lock(&g_stm32aes_lock); if (ret < 0) { return ret; @@ -319,6 +318,6 @@ int aes_cypher(void *out, const void *in, size_t size, stm32aes_enable(false); out: - nxsem_post(&g_stm32aes_lock); + nxmutex_unlock(&g_stm32aes_lock); return ret; } diff --git a/arch/arm/src/stm32/stm32_bbsram.c b/arch/arm/src/stm32/stm32_bbsram.c index 0c08326cf52a9..e6940511cc772 100644 --- a/arch/arm/src/stm32/stm32_bbsram.c +++ b/arch/arm/src/stm32/stm32_bbsram.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "stm32_bbsram.h" #include "chip.h" @@ -96,7 +97,7 @@ struct bbsramfh_s struct stm32_bbsram_s { - sem_t exclsem; /* For atomic accesses to this structure */ + mutex_t lock; /* For atomic accesses to this structure */ uint8_t refs; /* Number of references */ struct bbsramfh_s *bbf; /* File in bbram */ }; @@ -181,35 +182,7 @@ static void stm32_bbsram_dump(struct bbsramfh_s *bbf, char *op) #endif /**************************************************************************** - * Name: stm32_bbsram_semgive - ****************************************************************************/ - -static void stm32_bbsram_semgive(struct stm32_bbsram_s *priv) -{ - nxsem_post(&priv->exclsem); -} - -/**************************************************************************** - * Name: stm32_bbsram_semtake - * - * Description: - * Take a semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the CAN peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int stm32_bbsram_semtake(struct stm32_bbsram_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -/**************************************************************************** - * Name: stm32_bbsram_ulock + * Name: stm32_bbsram_unlock * * Description: * Unprotects RTC registers, RTC backup data registers and backup SRAM @@ -286,7 +259,7 @@ static int stm32_bbsram_open(struct file *filep) /* Increment the reference count */ - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -301,7 +274,7 @@ static int stm32_bbsram_open(struct file *filep) bbr->refs++; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return OK; } @@ -339,7 +312,7 @@ static int stm32_bbsram_close(struct file *filep) DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -368,7 +341,7 @@ static int stm32_bbsram_close(struct file *filep) } } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return ret; } @@ -387,7 +360,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (off_t)ret; @@ -413,7 +386,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, /* Return EINVAL if the whence argument is invalid */ - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return -EINVAL; } @@ -439,7 +412,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, ret = -EINVAL; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return ret; } @@ -457,7 +430,7 @@ static ssize_t stm32_bbsram_read(struct file *filep, char *buffer, DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (ssize_t)ret; @@ -472,7 +445,7 @@ static ssize_t stm32_bbsram_read(struct file *filep, char *buffer, memcpy(buffer, &bbr->bbf->data[filep->f_pos], len); filep->f_pos += len; - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return len; } @@ -514,7 +487,7 @@ static ssize_t stm32_bbsram_write(struct file *filep, len = bbr->bbf->len - filep->f_pos; } - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (ssize_t)ret; @@ -528,7 +501,7 @@ static ssize_t stm32_bbsram_write(struct file *filep, stm32_bbsram_lock(); filep->f_pos += len; BBSRAM_DUMP(bbr->bbf, "write done"); - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); } BBSRAM_DEBUG_READ(); @@ -571,7 +544,7 @@ static int stm32_bbsram_ioctl(struct file *filep, int cmd, { struct bbsramd_s *bbrr = (struct bbsramd_s *)((uintptr_t)arg); - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -592,7 +565,7 @@ static int stm32_bbsram_ioctl(struct file *filep, int cmd, ret = OK; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); } return ret; @@ -620,7 +593,7 @@ static int stm32_bbsram_unlink(struct inode *inode) DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -633,8 +606,8 @@ static int stm32_bbsram_unlink(struct inode *inode) bbr->bbf->crc = stm32_bbsram_crc(bbr->bbf); stm32_bbsram_lock(); bbr->refs = 0; - stm32_bbsram_semgive(bbr); - nxsem_destroy(&bbr->exclsem); + nxmutex_unlock(&bbr->lock); + nxmutex_destroy(&bbr->lock); return 0; } @@ -697,7 +670,7 @@ static int stm32_bbsram_probe(int *ent, struct stm32_bbsram_s pdev[]) pdev[i].bbf = pf; pf = (struct bbsramfh_s *)((uint8_t *)pf + alloc); - nxsem_init(&g_bbsram[i].exclsem, 0, 1); + nxmutex_init(&g_bbsram[i].lock); } avail -= alloc; diff --git a/arch/arm/src/stm32/stm32_dma2d.c b/arch/arm/src/stm32/stm32_dma2d.c index 2c9372ed290d4..dbb257c4c7796 100644 --- a/arch/arm/src/stm32/stm32_dma2d.c +++ b/arch/arm/src/stm32/stm32_dma2d.c @@ -37,7 +37,7 @@ #include #include -#include +#include #include #include @@ -111,7 +111,7 @@ struct stm32_dma2d_s uint32_t *clut; /* Color lookup table */ #endif - sem_t *lock; /* Ensure mutually exclusive access */ + mutex_t *lock; /* Ensure mutually exclusive access */ }; /* Interrupt handling */ @@ -248,9 +248,9 @@ static uint32_t g_clut[STM32_DMA2D_NCLUT * / 4]; #endif /* CONFIG_STM32_FB_CMAP */ -/* The DMA2D semaphore that enforces mutually exclusive access */ +/* The DMA2D mutex that enforces mutually exclusive access */ -static sem_t g_lock; +static mutex_t g_lock; /* Semaphore for interrupt handling */ @@ -751,7 +751,7 @@ static int stm32_dma2d_setclut(const struct fb_cmap_s *cmap) lcdinfo("cmap=%p\n", cmap); - nxsem_wait(priv->lock); + nxmutex_lock(priv->lock); for (n = cmap->first; n < cmap->len - 1 && n < STM32_DMA2D_NCLUT; n++) { @@ -785,8 +785,7 @@ static int stm32_dma2d_setclut(const struct fb_cmap_s *cmap) # endif } - nxsem_post(priv->lock); - + nxmutex_unlock(priv->lock); return OK; } #endif /* CONFIG_STM32_FB_CMAP */ @@ -833,7 +832,7 @@ static int stm32_dma2d_fillcolor(struct stm32_dma2d_overlay_s *oinfo, } #endif - nxsem_wait(priv->lock); + nxmutex_lock(priv->lock); /* Set output pfc */ @@ -865,7 +864,7 @@ static int stm32_dma2d_fillcolor(struct stm32_dma2d_overlay_s *oinfo, lcderr("ERROR: Returning ECANCELED\n"); } - nxsem_post(priv->lock); + nxmutex_unlock(priv->lock); return ret; } @@ -906,7 +905,7 @@ static int stm32_dma2d_blit(struct stm32_dma2d_overlay_s *doverlay, ", soverlay=%p, sarea=%p\n", doverlay, destxpos, destypos, soverlay, sarea); - nxsem_wait(priv->lock); + nxmutex_lock(priv->lock); /* Set output pfc */ @@ -956,7 +955,7 @@ static int stm32_dma2d_blit(struct stm32_dma2d_overlay_s *doverlay, lcderr("ERROR: Returning ECANCELED\n"); } - nxsem_post(priv->lock); + nxmutex_unlock(priv->lock); return ret; } @@ -1018,7 +1017,7 @@ static int stm32_dma2d_blend(struct stm32_dma2d_overlay_s *doverlay, } #endif - nxsem_wait(priv->lock); + nxmutex_lock(priv->lock); /* Set output pfc */ @@ -1066,7 +1065,7 @@ static int stm32_dma2d_blend(struct stm32_dma2d_overlay_s *doverlay, lcderr("ERROR: Returning ECANCELED\n"); } - nxsem_post(priv->lock); + nxmutex_unlock(priv->lock); return ret; } @@ -1096,11 +1095,11 @@ int stm32_dma2dinitialize(void) * arch/arm/src/stm32/stm32f40xxx_rcc.c */ - /* Initialize the DMA2D semaphore that enforces mutually exclusive + /* Initialize the DMA2D mutex that enforces mutually exclusive * access to the driver */ - nxsem_init(&g_lock, 0, 1); + nxmutex_init(&g_lock); /* Initialize the semaphore for interrupt handling. This waitsem * semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/stm32/stm32_dma_v1.c b/arch/arm/src/stm32/stm32_dma_v1.c index 9caf78d6b9b1d..e28d7275e4337 100644 --- a/arch/arm/src/stm32/stm32_dma_v1.c +++ b/arch/arm/src/stm32/stm32_dma_v1.c @@ -269,24 +269,6 @@ static inline void dmachan_putreg(struct stm32_dma_s *dmach, putreg32(value, dmach->base + offset); } -/**************************************************************************** - * Name: stm32_dmatake() and stm32_dmagive() - * - * Description: - * Used to get exclusive access to a DMA channel. - * - ****************************************************************************/ - -static int stm32_dmatake(struct stm32_dma_s *dmach) -{ - return nxsem_wait_uninterruptible(&dmach->sem); -} - -static inline void stm32_dmagive(struct stm32_dma_s *dmach) -{ - nxsem_post(&dmach->sem); -} - /**************************************************************************** * Name: stm32_dmachandisable * @@ -493,7 +475,7 @@ DMA_HANDLE stm32_dmachannel(unsigned int chndef) * is available if it is currently being used by another driver */ - ret = stm32_dmatake(dmach); + ret = nxsem_wait_uninterruptible(&dmach->sem); if (ret < 0) { return NULL; @@ -540,7 +522,7 @@ void stm32_dmafree(DMA_HANDLE handle) /* Release the channel */ - stm32_dmagive(dmach); + nxsem_post(&dmach->sem); } /**************************************************************************** diff --git a/arch/arm/src/stm32/stm32_dma_v2.c b/arch/arm/src/stm32/stm32_dma_v2.c index b37c63dd6e67b..f3d383f77e5ef 100644 --- a/arch/arm/src/stm32/stm32_dma_v2.c +++ b/arch/arm/src/stm32/stm32_dma_v2.c @@ -227,24 +227,6 @@ static inline void dmast_putreg(struct stm32_dma_s *dmast, uint32_t offset, putreg32(value, dmast->base + offset); } -/**************************************************************************** - * Name: stm32_dmatake() and stm32_dmagive() - * - * Description: - * Used to get exclusive access to a DMA channel. - * - ****************************************************************************/ - -static int stm32_dmatake(struct stm32_dma_s *dmast) -{ - return nxsem_wait_uninterruptible(&dmast->sem); -} - -static inline void stm32_dmagive(struct stm32_dma_s *dmast) -{ - nxsem_post(&dmast->sem); -} - /**************************************************************************** * Name: stm32_dmastream * @@ -523,7 +505,7 @@ DMA_HANDLE stm32_dmachannel(unsigned int dmamap) * is available if it is currently being used by another driver */ - ret = stm32_dmatake(dmast); + ret = nxsem_wait_uninterruptible(&dmast->sem); if (ret < 0) { return NULL; @@ -565,7 +547,7 @@ void stm32_dmafree(DMA_HANDLE handle) /* Release the channel */ - stm32_dmagive(dmast); + nxsem_post(&dmast->sem); } /**************************************************************************** diff --git a/arch/arm/src/stm32/stm32_foc.c b/arch/arm/src/stm32/stm32_foc.c index 956049fbd5ed8..68ca3fb461619 100644 --- a/arch/arm/src/stm32/stm32_foc.c +++ b/arch/arm/src/stm32/stm32_foc.c @@ -32,6 +32,7 @@ #include #include +#include #include "arm_internal.h" #include "stm32_pwm.h" @@ -705,7 +706,7 @@ struct stm32_foc_dev_s struct stm32_foc_adccmn_s { uint8_t cntr; /* ADC common counter */ - sem_t sem; /* Lock data */ + mutex_t lock; /* Lock data */ }; /* STM32 FOC volatile data */ @@ -1340,7 +1341,7 @@ static int stm32_foc_setup(struct foc_dev_s *dev) #ifdef FOC_ADC_HAVE_CMN /* Lock ADC common data */ - ret = nxsem_wait_uninterruptible(&priv->adc_cmn->sem); + ret = nxmutex_lock(&priv->adc_cmn->lock); if (ret < 0) { goto errout; @@ -1361,7 +1362,7 @@ static int stm32_foc_setup(struct foc_dev_s *dev) /* Unlock ADC common data */ - nxsem_post(&priv->adc_cmn->sem); + nxmutex_unlock(&priv->adc_cmn->lock); #endif /* Setup PWM */ @@ -1474,7 +1475,7 @@ static int stm32_foc_shutdown(struct foc_dev_s *dev) #ifdef FOC_ADC_HAVE_CMN /* Lock ADC common data */ - ret = nxsem_wait_uninterruptible(&priv->adc_cmn->sem); + ret = nxmutex_lock(&priv->adc_cmn->lock); if (ret < 0) { goto errout; @@ -1497,7 +1498,7 @@ static int stm32_foc_shutdown(struct foc_dev_s *dev) #ifdef FOC_ADC_HAVE_CMN /* Unlock ADC common data */ - nxsem_post(&priv->adc_cmn->sem); + nxmutex_unlock(&priv->adc_cmn->lock); #endif /* Call board-specific shutdown */ @@ -2355,9 +2356,9 @@ stm32_foc_initialize(int inst, struct stm32_foc_board_s *board) modifyreg32(FOC_PWM_FZ_REG, 0, pwmfzbit); #ifdef FOC_ADC_HAVE_CMN - /* Initialize ADC common data semaphore */ + /* Initialize ADC common data mutex */ - nxsem_init(&foc_priv->adc_cmn->sem, 0, 1); + nxmutex_init(&foc_priv->adc_cmn->lock); #endif /* Initialize calibration semaphore */ diff --git a/arch/arm/src/stm32/stm32_hrtim.c b/arch/arm/src/stm32/stm32_hrtim.c index b1242d96bbaff..4cb73bf9dce66 100644 --- a/arch/arm/src/stm32/stm32_hrtim.c +++ b/arch/arm/src/stm32/stm32_hrtim.c @@ -6011,25 +6011,13 @@ struct hrtim_dev_s *stm32_hrtiminitialize(void) #ifndef CONFIG_STM32_HRTIM_DISABLE_CHARDRV int hrtim_register(const char *path, struct hrtim_dev_s *dev) { - int ret ; - /* Initialize the HRTIM device structure */ dev->hd_ocount = 0; - /* Initialize semaphores */ - - nxsem_init(&dev->hd_closesem, 0, 1); - /* Register the HRTIM character driver */ - ret = register_driver(path, &hrtim_fops, 0444, dev); - if (ret < 0) - { - nxsem_destroy(&dev->hd_closesem); - } - - return ret; + return register_driver(path, &hrtim_fops, 0444, dev); } #endif /* CONFIG_STM32_HRTIM_DISABLE_CHARDRV */ diff --git a/arch/arm/src/stm32/stm32_hrtim.h b/arch/arm/src/stm32/stm32_hrtim.h index b0bac5bec5524..bb6c0f10f64de 100644 --- a/arch/arm/src/stm32/stm32_hrtim.h +++ b/arch/arm/src/stm32/stm32_hrtim.h @@ -1074,7 +1074,6 @@ struct hrtim_dev_s /* Fields managed by common upper half HRTIM logic */ uint8_t hd_ocount; /* The number of times the device has been opened */ - sem_t hd_closesem; /* Locks out new opens while close is in progress */ #endif /* Fields provided by lower half HRTIM logic */ diff --git a/arch/arm/src/stm32/stm32_i2c.c b/arch/arm/src/stm32/stm32_i2c.c index 78977f5df32a5..83da24ba5878d 100644 --- a/arch/arm/src/stm32/stm32_i2c.c +++ b/arch/arm/src/stm32/stm32_i2c.c @@ -66,6 +66,7 @@ #include #include #include +#include #include #include @@ -232,7 +233,7 @@ struct stm32_i2c_priv_s const struct stm32_i2c_config_s *config; int refs; /* Reference count */ - sem_t sem_excl; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion lock */ #ifndef CONFIG_I2C_POLLED sem_t sem_isr; /* Interrupt wait semaphore */ #endif @@ -277,9 +278,6 @@ static uint32_t stm32_i2c_toticks(int msgc, struct i2c_msg_s *msgs); static inline int stm32_i2c_sem_waitdone(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_post(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_init(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_destroy(struct stm32_i2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void stm32_i2c_tracereset(struct stm32_i2c_priv_s *priv); @@ -304,7 +302,7 @@ uint32_t stm32_i2c_disablefsmc(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_enablefsmc(uint32_t ahbenr); #endif /* I2C1_FSMC_CONFLICT */ -static int stm32_i2c_isr_process(struct stm32_i2c_priv_s * priv); +static int stm32_i2c_isr_process(struct stm32_i2c_priv_s *priv); #ifndef CONFIG_I2C_POLLED static int stm32_i2c_isr(int irq, void *context, void *arg); @@ -372,6 +370,10 @@ static struct stm32_i2c_priv_s stm32_i2c1_priv = .ops = &stm32_i2c_ops, .config = &stm32_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, @@ -401,6 +403,10 @@ static struct stm32_i2c_priv_s stm32_i2c2_priv = .ops = &stm32_i2c_ops, .config = &stm32_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, @@ -430,6 +436,10 @@ static struct stm32_i2c_priv_s stm32_i2c3_priv = .ops = &stm32_i2c_ops, .config = &stm32_i2c3_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, @@ -702,57 +712,6 @@ static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv) i2cinfo("Timeout with CR1: %04" PRIx32 " SR1: %04" PRIx32 "\n", cr1, sr1); } -/**************************************************************************** - * Name: stm32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_post(struct stm32_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_init(struct stm32_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: stm32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_destroy(struct stm32_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - /**************************************************************************** * Name: stm32_i2c_trace* * @@ -1550,7 +1509,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = nxsem_wait(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1740,7 +1699,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, priv->dcnt = 0; priv->ptr = NULL; - stm32_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -1777,7 +1736,7 @@ static int stm32_i2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1877,7 +1836,7 @@ static int stm32_i2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - stm32_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -1939,7 +1898,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - stm32_i2c_sem_init(priv); stm32_i2c_init(priv); } @@ -1983,9 +1941,6 @@ int stm32_i2cbus_uninitialize(struct i2c_master_s *dev) stm32_i2c_deinit(priv); - /* Release unused resources */ - - stm32_i2c_sem_destroy(priv); return OK; } diff --git a/arch/arm/src/stm32/stm32_i2c_alt.c b/arch/arm/src/stm32/stm32_i2c_alt.c index 16e4464a33e46..114a17cfe77ef 100644 --- a/arch/arm/src/stm32/stm32_i2c_alt.c +++ b/arch/arm/src/stm32/stm32_i2c_alt.c @@ -94,6 +94,7 @@ #include #include #include +#include #include #include @@ -281,7 +282,7 @@ struct stm32_i2c_priv_s const struct stm32_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 @@ -328,9 +329,6 @@ static uint32_t stm32_i2c_toticks(int msgc, struct i2c_msg_s *msgs); static inline int stm32_i2c_sem_waitdone(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_post(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_init(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_destroy(struct stm32_i2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void stm32_i2c_tracereset(struct stm32_i2c_priv_s *priv); @@ -355,7 +353,7 @@ uint32_t stm32_i2c_disablefsmc(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_enablefsmc(uint32_t ahbenr); #endif /* I2C1_FSMC_CONFLICT */ -static int stm32_i2c_isr_process(struct stm32_i2c_priv_s * priv); +static int stm32_i2c_isr_process(struct stm32_i2c_priv_s *priv); #ifndef CONFIG_I2C_POLLED static int stm32_i2c_isr(int irq, void *context, void *arg); @@ -402,6 +400,10 @@ static struct stm32_i2c_priv_s stm32_i2c1_priv = .ops = &stm32_i2c_ops, .config = &stm32_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, @@ -431,6 +433,10 @@ static struct stm32_i2c_priv_s stm32_i2c2_priv = .ops = &stm32_i2c_ops, .config = &stm32_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, @@ -460,6 +466,10 @@ static struct stm32_i2c_priv_s stm32_i2c3_priv = .ops = &stm32_i2c_ops, .config = &stm32_i2c3_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, @@ -732,57 +742,6 @@ static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv) i2cinfo("Timeout with CR1: %04" PRIx32 " SR1: %04" PRIx32 "\n", cr1, sr1); } -/**************************************************************************** - * Name: stm32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_post(struct stm32_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_init(struct stm32_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: stm32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_destroy(struct stm32_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - /**************************************************************************** * Name: stm32_i2c_trace* * @@ -2052,7 +2011,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = nxsem_wait(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2255,7 +2214,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, stm32_i2c_enablefsmc(ahbenr); #endif - stm32_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -2292,7 +2251,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2389,7 +2348,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Release the port for re-use by other clients */ - stm32_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2451,7 +2410,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - stm32_i2c_sem_init(priv); stm32_i2c_init(priv); } @@ -2495,9 +2453,6 @@ int stm32_i2cbus_uninitialize(struct i2c_master_s *dev) stm32_i2c_deinit(priv); - /* Release unused resources */ - - stm32_i2c_sem_destroy(priv); return OK; } diff --git a/arch/arm/src/stm32/stm32_i2c_v2.c b/arch/arm/src/stm32/stm32_i2c_v2.c index cdf2f8b56fcf1..33cdf7a62392e 100644 --- a/arch/arm/src/stm32/stm32_i2c_v2.c +++ b/arch/arm/src/stm32/stm32_i2c_v2.c @@ -233,6 +233,7 @@ #include #include +#include #include #include #include @@ -400,7 +401,7 @@ struct stm32_i2c_priv_s const struct stm32_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 @@ -458,9 +459,6 @@ static uint32_t stm32_i2c_toticks(int msgc, struct i2c_msg_s *msgs); #endif /* CONFIG_STM32_I2C_DYNTIMEO */ static inline int stm32_i2c_sem_waitdone(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_post(struct i2c_master_s *dev); -static inline void stm32_i2c_sem_init(struct i2c_master_s *dev); -static inline void stm32_i2c_sem_destroy(struct i2c_master_s *dev); #ifdef CONFIG_I2C_TRACE static void stm32_i2c_tracereset(struct stm32_i2c_priv_s *priv); static void stm32_i2c_tracenew(struct stm32_i2c_priv_s *priv, @@ -516,6 +514,10 @@ static struct stm32_i2c_priv_s stm32_i2c1_priv = { .config = &stm32_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, @@ -548,6 +550,10 @@ static struct stm32_i2c_priv_s stm32_i2c2_priv = { .config = &stm32_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, @@ -580,6 +586,10 @@ static struct stm32_i2c_priv_s stm32_i2c3_priv = { .config = &stm32_i2c3_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, @@ -612,6 +622,10 @@ static struct stm32_i2c_priv_s stm32_i2c4_priv = { .config = &stm32_i2c4_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, @@ -1020,58 +1034,6 @@ static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv) i2cinfo("Timeout with CR: %04" PRIx32 " SR: %04" PRIx32 "\n", cr, sr); } -/**************************************************************************** - * Name: stm32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_post(struct i2c_master_s *dev) -{ - nxsem_post(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_init(struct i2c_master_s *dev) -{ - nxsem_init(&((struct stm32_i2c_inst_s *)dev)->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(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, 0, 0); - nxsem_set_protocol(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, - SEM_PRIO_NONE); -#endif -} - -/**************************************************************************** - * Name: stm32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_destroy(struct i2c_master_s *dev) -{ - nxsem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr); -#endif -} - /**************************************************************************** * Name: stm32_i2c_trace* * @@ -2490,7 +2452,7 @@ static int stm32_i2c_process(struct i2c_master_s *dev, /* Dump the trace result */ stm32_i2c_tracedump(priv); - stm32_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return -errval; } @@ -2510,7 +2472,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = nxsem_wait(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); + ret = nxmutex_lock(&((struct stm32_i2c_inst_s *)dev)->priv->lock); if (ret >= 0) { ret = stm32_i2c_process(dev, msgs, count); @@ -2530,7 +2492,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, #ifdef CONFIG_I2C_RESET static int stm32_i2c_reset(struct i2c_master_s * dev) { - struct stm32_i2c_priv_s * priv; + struct stm32_i2c_priv_s *priv; unsigned int clock_count; unsigned int stretch_count; uint32_t scl_gpio; @@ -2550,7 +2512,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2650,7 +2612,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Release the port for re-use by other clients */ - stm32_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2691,7 +2653,6 @@ static int stm32_i2c_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_i2c_priv_s *priv = (struct stm32_i2c_priv_s *)((char *)cb - offsetof(struct stm32_i2c_priv_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -2706,13 +2667,7 @@ static int stm32_i2c_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for I2C bus is held. */ - if (nxsem_get_value(&priv->sem_excl, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -2808,7 +2763,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - stm32_i2c_sem_init((struct i2c_master_s *)inst); stm32_i2c_init(priv); #ifdef CONFIG_PM @@ -2868,8 +2822,6 @@ int stm32_i2cbus_uninitialize(struct i2c_master_s * dev) /* Release unused resources */ - stm32_i2c_sem_destroy((struct i2c_master_s *)dev); - kmm_free(dev); return OK; } diff --git a/arch/arm/src/stm32/stm32_i2s.c b/arch/arm/src/stm32/stm32_i2s.c index 3b81a90c8bc5d..90a6ff170381f 100644 --- a/arch/arm/src/stm32/stm32_i2s.c +++ b/arch/arm/src/stm32/stm32_i2s.c @@ -56,6 +56,7 @@ #include #include +#include #include #include #include @@ -276,7 +277,7 @@ struct stm32_i2s_s { struct i2s_dev_s dev; /* Externally visible I2S interface */ uintptr_t base; /* I2S controller register base address */ - sem_t exclsem; /* Assures mutually exclusive access to I2S */ + mutex_t lock; /* Assures mutually exclusive access to I2S */ bool initialized; /* Has I2S interface been initialized */ uint8_t datalen; /* Data width (8 or 16) */ uint8_t align; /* Log2 of data width (0 or 1) */ @@ -343,14 +344,6 @@ static void i2s_dump_regs(struct stm32_i2s_s *priv, const char *msg); # define i2s_dump_buffer(m,b,s) #endif -/* Semaphore helpers */ - -static int i2s_exclsem_take(struct stm32_i2s_s *priv); -#define i2s_exclsem_give(priv) nxsem_post(&priv->exclsem) - -static int i2s_bufsem_take(struct stm32_i2s_s *priv); -#define i2s_bufsem_give(priv) nxsem_post(&priv->bufsem) - /* Buffer container helpers */ static struct stm32_buffer_s * @@ -608,46 +601,6 @@ static void i2s_dump_regs(struct stm32_i2s_s *priv, const char *msg) } #endif -/**************************************************************************** - * Name: i2s_exclsem_take - * - * Description: - * Take the exclusive access semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the i2s peripheral state - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int i2s_exclsem_take(struct stm32_i2s_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -/**************************************************************************** - * Name: i2s_bufsem_take - * - * Description: - * Take the buffer semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the i2s peripheral state - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int i2s_bufsem_take(struct stm32_i2s_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->bufsem); -} - /**************************************************************************** * Name: i2s_buf_allocate * @@ -678,7 +631,7 @@ static struct stm32_buffer_s *i2s_buf_allocate(struct stm32_i2s_s *priv) * have at least one free buffer container. */ - ret = i2s_bufsem_take(priv); + ret = nxsem_wait_uninterruptible(&priv->bufsem); if (ret < 0) { return NULL; @@ -729,7 +682,7 @@ static void i2s_buf_free(struct stm32_i2s_s *priv, /* Wake up any threads waiting for a buffer container */ - i2s_bufsem_give(priv); + nxsem_post(&priv->bufsem); } /**************************************************************************** @@ -1888,7 +1841,7 @@ static int stm32_i2s_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the I2S driver data */ - ret = i2s_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout_with_buf; @@ -1900,7 +1853,7 @@ static int stm32_i2s_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: I2S%d has no receiver\n", priv->i2sno); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Add a reference to the audio buffer */ @@ -1927,11 +1880,11 @@ static int stm32_i2s_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, ret = i2s_rxdma_setup(priv); DEBUGASSERT(ret == OK); leave_critical_section(flags); - i2s_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - i2s_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); errout_with_buf: i2s_buf_free(priv, bfcontainer); @@ -2100,7 +2053,7 @@ static int stm32_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the I2S driver data */ - ret = i2s_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout_with_buf; @@ -2112,7 +2065,7 @@ static int stm32_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: I2S%d has no transmitter\n", priv->i2sno); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Add a reference to the audio buffer */ @@ -2139,11 +2092,11 @@ static int stm32_i2s_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, ret = i2s_txdma_setup(priv); DEBUGASSERT(ret == OK); leave_critical_section(flags); - i2s_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - i2s_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); errout_with_buf: i2s_buf_free(priv, bfcontainer); @@ -2593,7 +2546,7 @@ struct i2s_dev_s *stm32_i2sbus_initialize(int port) /* Initialize the common parts for the I2S device structure */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->dev.ops = &g_i2sops; priv->i2sno = port; @@ -2646,7 +2599,7 @@ struct i2s_dev_s *stm32_i2sbus_initialize(int port) errout_with_alloc: leave_critical_section(flags); - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return NULL; } diff --git a/arch/arm/src/stm32/stm32_ltdc.c b/arch/arm/src/stm32/stm32_ltdc.c index 11a7037a96c90..c0f633ed4f6f7 100644 --- a/arch/arm/src/stm32/stm32_ltdc.c +++ b/arch/arm/src/stm32/stm32_ltdc.c @@ -37,7 +37,7 @@ #include #include -#include +#include #include #include @@ -616,7 +616,7 @@ struct stm32_ltdc_s struct stm32_dma2d_overlay_s dma2dinfo; /* Overlay info for DMA2D */ #endif - sem_t *lock; /* Layer exclusive access */ + mutex_t *lock; /* Layer exclusive access */ }; /* This structure provides the overall state of the LTDC layer */ @@ -806,9 +806,9 @@ static uint8_t g_transpclut[STM32_LTDC_NCLUT]; # endif #endif /* CONFIG_STM32_FB_CMAP */ -/* The LTDC semaphore that enforces mutually exclusive access */ +/* The LTDC mutex that enforces mutually exclusive access */ -static sem_t g_lock; +static mutex_t g_lock; /* The semaphore for interrupt handling */ @@ -1608,9 +1608,9 @@ static int stm32_ltdc_reload(uint8_t value, bool waitvblank) static void stm32_ltdc_irqconfig(void) { - /* Initialize the LTDC semaphore that enforces mutually exclusive access */ + /* Initialize the LTDC mutex that enforces mutually exclusive access */ - nxsem_init(&g_lock, 0, 1); + nxmutex_init(&g_lock); /* Initialize the semaphore for interrupt handling. This waitsem * semaphore is used for signaling and, hence, should not have priority @@ -2419,9 +2419,9 @@ static int stm32_getcmap(struct fb_vtable_s *vtable, # else layer = &priv->layer[LTDC_LAYER_L1]; # endif - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); stm32_ltdc_lgetclut(layer, cmap); - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); ret = OK; } @@ -2496,7 +2496,7 @@ static int stm32_putcmap(struct fb_vtable_s *vtable, /* Update the layer clut register */ - nxsem_wait(&g_lock); + nxmutex_lock(&g_lock); for (n = 0; n < LTDC_NLAYERS; n++) { @@ -2509,7 +2509,7 @@ static int stm32_putcmap(struct fb_vtable_s *vtable, priv->dma2d->setclut(cmap); # endif - nxsem_post(&g_lock); + nxmutex_unlock(&g_lock); ret = OK; } @@ -2591,7 +2591,7 @@ static int stm32_settransp(struct fb_vtable_s *vtable, { struct stm32_ltdc_s * layer = &priv->layer[oinfo->overlay]; - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); layer->oinfo.transp.transp = oinfo->transp.transp; layer->oinfo.transp.transp_mode = oinfo->transp.transp_mode; @@ -2614,7 +2614,7 @@ static int stm32_settransp(struct fb_vtable_s *vtable, layer->oinfo.transp.transp_mode); } - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return OK; } @@ -2656,7 +2656,7 @@ static int stm32_setchromakey(struct fb_vtable_s *vtable, } # endif - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); # ifdef CONFIG_STM32_FB_CMAP if (oinfo->chromakey >= g_vtable.cmap.len) { @@ -2675,7 +2675,7 @@ static int stm32_setchromakey(struct fb_vtable_s *vtable, ret = OK; } - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return ret; } # ifdef CONFIG_STM32_DMA2D @@ -2720,11 +2720,11 @@ static int stm32_setcolor(struct fb_vtable_s *vtable, DEBUGASSERT(&layer->oinfo == poverlay); - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); poverlay->color = oinfo->color; ret = priv->dma2d->fillcolor(&layer->dma2dinfo, &poverlay->sarea, poverlay->color); - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return ret; # else @@ -2757,13 +2757,13 @@ static int stm32_setblank(struct fb_vtable_s *vtable, { struct stm32_ltdc_s * layer = &priv->layer[oinfo->overlay]; - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); layer->oinfo.blank = oinfo->blank; /* Enable or disable layer */ stm32_ltdc_lenable(layer, (layer->oinfo.blank == 0)); - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return OK; } @@ -2808,9 +2808,9 @@ static int stm32_setarea(struct fb_vtable_s *vtable, vtable; struct stm32_ltdc_s * layer = &priv->layer[oinfo->overlay]; - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); memcpy(&layer->oinfo.sarea, &oinfo->sarea, sizeof(struct fb_area_s)); - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return OK; } @@ -2863,10 +2863,10 @@ static int stm32_blit(struct fb_vtable_s *vtable, sarea.w = MIN(darea->w, sarea.w); sarea.h = MIN(darea->h, sarea.h); - nxsem_wait(dlayer->lock); + nxmutex_lock(dlayer->lock); ret = priv->dma2d->blit(&dlayer->dma2dinfo, darea->x, darea->y, &slayer->dma2dinfo, &sarea); - nxsem_post(dlayer->lock); + nxmutex_unlock(dlayer->lock); return ret; # else @@ -2932,11 +2932,11 @@ static int stm32_blend(struct fb_vtable_s *vtable, barea.w = MIN(farea->w, barea.w); barea.h = MIN(farea->h, barea.h); - nxsem_wait(dlayer->lock); + nxmutex_lock(dlayer->lock); ret = priv->dma2d->blend(&dlayer->dma2dinfo, darea->x, darea->y, &flayer->dma2dinfo, farea->x, farea->y, &blayer->dma2dinfo, &barea); - nxsem_post(dlayer->lock); + nxmutex_unlock(dlayer->lock); return ret; # else diff --git a/arch/arm/src/stm32/stm32_otgfshost.c b/arch/arm/src/stm32/stm32_otgfshost.c index 128aca680de25..ae3bef24ffdba 100644 --- a/arch/arm/src/stm32/stm32_otgfshost.c +++ b/arch/arm/src/stm32/stm32_otgfshost.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -254,7 +255,7 @@ struct stm32_usbhost_s volatile bool connected; /* Connected to device */ volatile bool change; /* Connection change */ volatile bool pscwait; /* True: Thread is waiting for a port event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for a port event */ struct stm32_ctrlinfo_s ep0; /* Root hub port EP0 description */ @@ -294,12 +295,6 @@ static inline void stm32_modifyreg(uint32_t addr, uint32_t clrbits, # define stm32_pktdump(m,b,n) #endif -/* Semaphores ***************************************************************/ - -static int stm32_takesem(sem_t *sem); -static int stm32_takesem_noncancelable(sem_t *sem); -#define stm32_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t stm32_getle16(const uint8_t *val); @@ -633,54 +628,6 @@ static inline void stm32_modifyreg(uint32_t addr, uint32_t clrbits, stm32_putreg(addr, (((stm32_getreg(addr)) & ~clrbits) | setbits)); } -/**************************************************************************** - * Name: stm32_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int stm32_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: stm32_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int stm32_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: stm32_getle16 * @@ -1138,7 +1085,7 @@ static int stm32_chan_wait(struct stm32_usbhost_s *priv, * wait here. */ - ret = stm32_takesem(&chan->waitsem); + ret = nxsem_wait_uninterruptible(&chan->waitsem); } while (chan->waiter && ret >= 0); @@ -1188,7 +1135,7 @@ static void stm32_chan_wakeup(struct stm32_usbhost_s *priv, OTGFS_VTRACE2_CHANWAKEUP_OUT, chan->epno, chan->result); - stm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -2996,7 +2943,7 @@ static void stm32_gint_connected(struct stm32_usbhost_s *priv) priv->smstate = SMSTATE_ATTACHED; if (priv->pscwait) { - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3044,7 +2991,7 @@ static void stm32_gint_disconnected(struct stm32_usbhost_s *priv) if (priv->pscwait) { - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3949,7 +3896,7 @@ static int stm32_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = stm32_takesem(&priv->pscsem); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -4130,7 +4077,7 @@ static int stm32_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4154,7 +4101,7 @@ static int stm32_ep0configure(struct usbhost_driver_s *drvr, stm32_chan_configure(priv, ep0info->inndx); - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4195,7 +4142,7 @@ static int stm32_epalloc(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4216,7 +4163,7 @@ static int stm32_epalloc(struct usbhost_driver_s *drvr, ret = stm32_xfrep_alloc(priv, epdesc, ep); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4249,7 +4196,7 @@ static int stm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem_noncancelable(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); /* A single channel is represent by an index in the range of 0 to * STM32_MAX_TX_FIFOS. Otherwise, the ep must be a pointer to an allocated @@ -4277,7 +4224,7 @@ static int stm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) kmm_free(ctrlep); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4513,7 +4460,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4557,7 +4504,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactions exit here */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4572,7 +4519,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4600,7 +4547,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4648,7 +4595,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactins exit here */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4664,7 +4611,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4722,7 +4669,7 @@ static ssize_t stm32_transfer(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -4739,7 +4686,7 @@ static ssize_t stm32_transfer(struct usbhost_driver_s *drvr, nbytes = stm32_out_transfer(priv, chidx, buffer, buflen); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nbytes; } @@ -4794,7 +4741,7 @@ static int stm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4811,7 +4758,7 @@ static int stm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, ret = stm32_out_asynch(priv, chidx, buffer, buflen, callback, arg); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4870,7 +4817,7 @@ static int stm32_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake'em up! */ - stm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -4947,7 +4894,7 @@ static int stm32_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -5303,10 +5250,10 @@ static inline void stm32_sw_initialize(struct stm32_usbhost_s *priv) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/stm32/stm32_otghshost.c b/arch/arm/src/stm32/stm32_otghshost.c index ae977bc75b4b0..f4521eb54ae57 100644 --- a/arch/arm/src/stm32/stm32_otghshost.c +++ b/arch/arm/src/stm32/stm32_otghshost.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -259,7 +260,7 @@ struct stm32_usbhost_s volatile bool connected; /* Connected to device */ volatile bool change; /* Connection change */ volatile bool pscwait; /* True: Thread is waiting for a port event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for a port event */ struct stm32_ctrlinfo_s ep0; /* Root hub port EP0 description */ @@ -299,12 +300,6 @@ static inline void stm32_modifyreg(uint32_t addr, uint32_t clrbits, # define stm32_pktdump(m,b,n) #endif -/* Semaphores ***************************************************************/ - -static int stm32_takesem(sem_t *sem); -static int stm32_takesem_noncancelable(sem_t *sem); -#define stm32_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t stm32_getle16(const uint8_t *val); @@ -638,54 +633,6 @@ static inline void stm32_modifyreg(uint32_t addr, uint32_t clrbits, stm32_putreg(addr, (((stm32_getreg(addr)) & ~clrbits) | setbits)); } -/**************************************************************************** - * Name: stm32_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int stm32_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: stm32_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int stm32_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: stm32_getle16 * @@ -1189,7 +1136,7 @@ static void stm32_chan_wakeup(struct stm32_usbhost_s *priv, OTGHS_VTRACE2_CHANWAKEUP_OUT, chan->epno, chan->result); - stm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -2997,7 +2944,7 @@ static void stm32_gint_connected(struct stm32_usbhost_s *priv) priv->smstate = SMSTATE_ATTACHED; if (priv->pscwait) { - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3045,7 +2992,7 @@ static void stm32_gint_disconnected(struct stm32_usbhost_s *priv) if (priv->pscwait) { - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3950,7 +3897,7 @@ static int stm32_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = stm32_takesem(&priv->pscsem); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -4131,7 +4078,7 @@ static int stm32_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4155,7 +4102,7 @@ static int stm32_ep0configure(struct usbhost_driver_s *drvr, stm32_chan_configure(priv, ep0info->inndx); - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4196,7 +4143,7 @@ static int stm32_epalloc(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4217,7 +4164,7 @@ static int stm32_epalloc(struct usbhost_driver_s *drvr, ret = stm32_xfrep_alloc(priv, epdesc, ep); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4250,7 +4197,7 @@ static int stm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem_noncancelable(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); /* A single channel is represent by an index in the range of 0 to * STM32_MAX_TX_FIFOS. Otherwise, the ep must be a pointer to an allocated @@ -4278,7 +4225,7 @@ static int stm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) kmm_free(ctrlep); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4514,7 +4461,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4558,7 +4505,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactions exit here */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4573,7 +4520,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4601,7 +4548,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4649,7 +4596,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactins exit here */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4665,7 +4612,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4723,7 +4670,7 @@ static ssize_t stm32_transfer(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -4740,7 +4687,7 @@ static ssize_t stm32_transfer(struct usbhost_driver_s *drvr, nbytes = stm32_out_transfer(priv, chidx, buffer, buflen); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nbytes; } @@ -4795,7 +4742,7 @@ static int stm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4812,7 +4759,7 @@ static int stm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, ret = stm32_out_asynch(priv, chidx, buffer, buflen, callback, arg); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4871,7 +4818,7 @@ static int stm32_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake'em up! */ - stm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -4948,7 +4895,7 @@ static int stm32_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -5302,10 +5249,10 @@ static inline void stm32_sw_initialize(struct stm32_usbhost_s *priv) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/stm32/stm32_rng.c b/arch/arm/src/stm32/stm32_rng.c index 01fdb4c17f05b..a59128de3cafb 100644 --- a/arch/arm/src/stm32/stm32_rng.c +++ b/arch/arm/src/stm32/stm32_rng.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -57,7 +58,7 @@ static ssize_t stm32_rng_read(struct file *filep, char *buffer, size_t); struct rng_dev_s { - sem_t rd_devsem; /* Threads can only exclusively access the RNG */ + mutex_t rd_devlock; /* Threads can only exclusively access the RNG */ sem_t rd_readsem; /* To block until the buffer is filled */ char *rd_buf; size_t rd_buflen; @@ -101,7 +102,7 @@ static int stm32_rng_initialize(void) memset(&g_rngdev, 0, sizeof(struct rng_dev_s)); - nxsem_init(&g_rngdev.rd_devsem, 0, 1); + nxmutex_init(&g_rngdev.rd_devlock); if (irq_attach(STM32_IRQ_RNG, stm32_rng_interrupt, NULL)) { @@ -232,13 +233,13 @@ static ssize_t stm32_rng_read(struct file *filep, char *buffer, { int ret; - ret = nxsem_wait(&g_rngdev.rd_devsem); + ret = nxmutex_lock(&g_rngdev.rd_devlock); if (ret < 0) { return ret; } - /* We've got the semaphore. */ + /* We've got the mutex. */ /* Initialize the operation semaphore with 0 for blocking until the * buffer is filled from interrupts. The readsem semaphore is used @@ -262,7 +263,7 @@ static ssize_t stm32_rng_read(struct file *filep, char *buffer, /* Free RNG for next use */ - nxsem_post(&g_rngdev.rd_devsem); + nxmutex_unlock(&g_rngdev.rd_devlock); return ret < 0 ? ret : buflen; } diff --git a/arch/arm/src/stm32/stm32_rtc_lowerhalf.c b/arch/arm/src/stm32/stm32_rtc_lowerhalf.c index 72301a5bac991..1f5859cd776b0 100644 --- a/arch/arm/src/stm32/stm32_rtc_lowerhalf.c +++ b/arch/arm/src/stm32/stm32_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -80,7 +81,7 @@ struct stm32_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -411,7 +412,7 @@ static int stm32_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -444,8 +445,7 @@ static int stm32_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; #else @@ -456,7 +456,7 @@ static int stm32_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->id == 0); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -488,8 +488,7 @@ static int stm32_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; #endif } @@ -676,7 +675,7 @@ static int stm32_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid) DEBUGASSERT(alarmid == RTC_ALARMA || alarmid == RTC_ALARMB); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -698,8 +697,7 @@ static int stm32_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid) ret = stm32_rtc_cancelalarm((enum alm_id_e)alarmid); } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; #else @@ -838,18 +836,16 @@ static int stm32_setperiodic(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } memcpy(&priv->periodic, alarminfo, sizeof(struct lower_setperiodic_s)); - ret = stm32_rtc_setperiodic(&alarminfo->period, stm32_periodic_callback); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -881,7 +877,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) DEBUGASSERT(id == 0); - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -889,8 +885,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) ret = stm32_rtc_cancelperiodic(); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -923,8 +918,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) struct rtc_lowerhalf_s *stm32_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/arm/src/stm32/stm32_sdio.c b/arch/arm/src/stm32/stm32_sdio.c index a256b96d77168..f7a1a6f32265e 100644 --- a/arch/arm/src/stm32/stm32_sdio.c +++ b/arch/arm/src/stm32/stm32_sdio.c @@ -382,8 +382,6 @@ struct stm32_sampleregs_s /* Low-level helpers ********************************************************/ -static int stm32_takesem(struct stm32_dev_s *priv); -#define stm32_givesem(priv) (nxsem_post(&priv->waitsem)) static inline void stm32_setclkcr(uint32_t clkcr); static void stm32_configwaitints(struct stm32_dev_s *priv, uint32_t waitmask, sdio_eventset_t waitevents, sdio_eventset_t wkupevents); @@ -564,27 +562,6 @@ static struct stm32_sampleregs_s g_sampleregs[DEBUG_NSAMPLES]; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: stm32_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * dev - Instance of the SDIO device driver state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int stm32_takesem(struct stm32_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: stm32_setclkcr * @@ -1277,7 +1254,7 @@ static void stm32_endwait(struct stm32_dev_s *priv, /* Wake up the waiting thread */ - stm32_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2617,7 +2594,7 @@ static sdio_eventset_t stm32_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = stm32_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/arm/src/stm32/stm32_spi.c b/arch/arm/src/stm32/stm32_spi.c index 64217bc655229..b0c1406429ec4 100644 --- a/arch/arm/src/stm32/stm32_spi.c +++ b/arch/arm/src/stm32/stm32_spi.c @@ -58,7 +58,7 @@ #include #include -#include +#include #include #include @@ -219,7 +219,7 @@ struct stm32_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 through 16) */ @@ -1254,11 +1254,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2088,9 +2088,9 @@ static void spi_bus_initialize(struct stm32_spidev_s *priv) spi_putreg(priv, STM32_SPI_CRCPR_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion. diff --git a/arch/arm/src/stm32/stm32f10xxf30xx_flash.c b/arch/arm/src/stm32/stm32f10xxf30xx_flash.c index 794ac7efb3044..6077564626017 100644 --- a/arch/arm/src/stm32/stm32f10xxf30xx_flash.c +++ b/arch/arm/src/stm32/stm32f10xxf30xx_flash.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include @@ -72,22 +72,12 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions ****************************************************************************/ -static int sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_sem); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void flash_unlock(uintptr_t base) { while ((getreg32(base + STM32_FLASH_SR_OFFSET) & FLASH_SR_BSY) != 0) @@ -117,7 +107,7 @@ int stm32_flash_unlock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -127,7 +117,7 @@ int stm32_flash_unlock(void) #if defined(STM32_FLASH_DUAL_BANK) flash_unlock(STM32_FLASHIF1_BASE); #endif - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -136,7 +126,7 @@ int stm32_flash_lock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -146,7 +136,7 @@ int stm32_flash_lock(void) #if defined(STM32_FLASH_DUAL_BANK) flash_lock(STM32_FLASHIF1_BASE); #endif - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -249,7 +239,7 @@ ssize_t up_progmem_eraseblock(size_t block) base = STM32_FLASHIF_BASE; } - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -257,7 +247,7 @@ ssize_t up_progmem_eraseblock(size_t block) if ((getreg32(STM32_RCC_CR) & RCC_CR_HSION) == 0) { - sem_unlock(); + nxmutex_unlock(&g_lock); return -EPERM; } @@ -280,7 +270,7 @@ ssize_t up_progmem_eraseblock(size_t block) } modifyreg32(base + STM32_FLASH_CR_OFFSET, FLASH_CR_PER, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -333,7 +323,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) return -EFAULT; } - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -341,7 +331,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) if ((getreg32(STM32_RCC_CR) & RCC_CR_HSION) == 0) { - sem_unlock(); + nxmutex_unlock(&g_lock); return -EPERM; } @@ -367,21 +357,21 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) if ((getreg32(base + STM32_FLASH_SR_OFFSET) & FLASH_SR_WRPRT_ERR) != 0) { modifyreg32(base + STM32_FLASH_CR_OFFSET, FLASH_CR_PG, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); return -EROFS; } if (getreg16(addr) != *hword) { modifyreg32(base + STM32_FLASH_CR_OFFSET, FLASH_CR_PG, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); return -EIO; } } modifyreg32(base + STM32_FLASH_CR_OFFSET, FLASH_CR_PG, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); return written; } diff --git a/arch/arm/src/stm32/stm32f20xxf40xx_flash.c b/arch/arm/src/stm32/stm32f20xxf40xx_flash.c index fa781ffc30e23..5bee010acc4e3 100644 --- a/arch/arm/src/stm32/stm32f20xxf40xx_flash.c +++ b/arch/arm/src/stm32/stm32f20xxf40xx_flash.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include @@ -65,22 +65,12 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions ****************************************************************************/ -static int sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_sem); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void flash_unlock(void) { while (getreg32(STM32_FLASH_SR) & FLASH_SR_BSY) @@ -128,14 +118,14 @@ int stm32_flash_unlock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_unlock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -144,14 +134,14 @@ int stm32_flash_lock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -344,7 +334,7 @@ ssize_t up_progmem_eraseblock(size_t block) return -EFAULT; } - sem_lock(); + nxmutex_lock(&g_lock); /* Get flash ready and begin erasing single block */ @@ -360,7 +350,7 @@ ssize_t up_progmem_eraseblock(size_t block) } modifyreg32(STM32_FLASH_CR, FLASH_CR_SER, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -398,7 +388,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) return -EFAULT; } - sem_lock(); + nxmutex_lock(&g_lock); /* Get flash ready and begin flashing */ @@ -430,14 +420,14 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) if (getreg32(STM32_FLASH_SR) & FLASH_CR_SER) { modifyreg32(STM32_FLASH_CR, FLASH_CR_PG, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); return -EROFS; } if (getreg16(addr) != *hword) { modifyreg32(STM32_FLASH_CR, FLASH_CR_PG, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); return -EIO; } } @@ -448,7 +438,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) data_cache_enable(); #endif - sem_unlock(); + nxmutex_unlock(&g_lock); return written; } diff --git a/arch/arm/src/stm32/stm32f40xxx_i2c.c b/arch/arm/src/stm32/stm32f40xxx_i2c.c index b86ccbd362148..b97986083440a 100644 --- a/arch/arm/src/stm32/stm32f40xxx_i2c.c +++ b/arch/arm/src/stm32/stm32f40xxx_i2c.c @@ -65,6 +65,7 @@ #include #include #include +#include #include #include @@ -262,7 +263,7 @@ struct stm32_i2c_priv_s const struct stm32_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 @@ -317,9 +318,6 @@ static uint32_t stm32_i2c_toticks(int msgc, struct i2c_msg_s *msgs); static inline int stm32_i2c_sem_waitdone(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_post(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_init(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_destroy(struct stm32_i2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void stm32_i2c_tracereset(struct stm32_i2c_priv_s *priv); @@ -402,6 +400,10 @@ static struct stm32_i2c_priv_s stm32_i2c1_priv = .ops = &stm32_i2c_ops, .config = &stm32_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, @@ -440,6 +442,10 @@ static struct stm32_i2c_priv_s stm32_i2c2_priv = .ops = &stm32_i2c_ops, .config = &stm32_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, @@ -476,6 +482,10 @@ static struct stm32_i2c_priv_s stm32_i2c3_priv = .ops = &stm32_i2c_ops, .config = &stm32_i2c3_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, @@ -755,57 +765,6 @@ static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv) i2cinfo("Timeout with CR1: %04" PRIx32 " SR1: %04" PRIx32 "\n", cr1, sr1); } -/**************************************************************************** - * Name: stm32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_post(struct stm32_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_init(struct stm32_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: stm32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_destroy(struct stm32_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - /**************************************************************************** * Name: stm32_i2c_trace* * @@ -2269,7 +2228,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = nxsem_wait(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2468,7 +2427,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, priv->dcnt = 0; priv->ptr = NULL; - stm32_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -2505,7 +2464,7 @@ static int stm32_i2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2605,7 +2564,7 @@ static int stm32_i2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - stm32_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2667,7 +2626,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - stm32_i2c_sem_init(priv); stm32_i2c_init(priv); #ifdef CONFIG_STM32_I2C_DMA @@ -2732,9 +2690,6 @@ int stm32_i2cbus_uninitialize(struct i2c_master_s *dev) stm32_dmafree(priv->txdma); #endif - /* Release unused resources */ - - stm32_i2c_sem_destroy(priv); return OK; } diff --git a/arch/arm/src/stm32/stm32l15xx_flash.c b/arch/arm/src/stm32/stm32l15xx_flash.c index 269efb16e649d..011ac8536f4f1 100644 --- a/arch/arm/src/stm32/stm32l15xx_flash.c +++ b/arch/arm/src/stm32/stm32l15xx_flash.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include @@ -75,22 +75,12 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions ****************************************************************************/ -static int sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_sem); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void stm32_eeprom_unlock(void) { while (getreg32(STM32_FLASH_SR) & FLASH_SR_BSY) @@ -272,14 +262,14 @@ int stm32_flash_unlock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_unlock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -288,14 +278,14 @@ int stm32_flash_lock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -320,14 +310,14 @@ ssize_t stm32_eeprom_write(size_t addr, const void *buf, size_t buflen) return -EINVAL; } - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; } outlen = stm32_eeprom_erase_write(addr, buf, buflen); - sem_unlock(); + nxmutex_unlock(&g_lock); return outlen; } @@ -337,14 +327,14 @@ ssize_t stm32_eeprom_erase(size_t addr, size_t eraselen) ssize_t outlen; int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; } outlen = stm32_eeprom_erase_write(addr, NULL, eraselen); - sem_unlock(); + nxmutex_unlock(&g_lock); return outlen; } @@ -437,7 +427,7 @@ ssize_t up_progmem_eraseblock(size_t block) /* Get flash ready and begin erasing single page */ - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -460,7 +450,7 @@ ssize_t up_progmem_eraseblock(size_t block) } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -506,7 +496,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) /* Get flash ready and begin flashing */ - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -554,7 +544,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return (ret == OK) ? written : ret; } diff --git a/arch/arm/src/stm32f0l0g0/stm32_adc.c b/arch/arm/src/stm32f0l0g0/stm32_adc.c index fa392547a83e4..ada6677d89033 100644 --- a/arch/arm/src/stm32f0l0g0/stm32_adc.c +++ b/arch/arm/src/stm32f0l0g0/stm32_adc.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include "arm_internal.h" #include "chip.h" @@ -173,7 +173,7 @@ struct adccmn_data_s { uint8_t initialized; /* How many ADC instances are currently in use */ - sem_t lock; /* Exclusive access to common ADC data */ + mutex_t lock; /* Exclusive access to common ADC data */ }; #endif @@ -272,10 +272,6 @@ static void tim_dumpregs(struct stm32_dev_s *priv, const char *msg); #endif -#ifdef HAVE_ADC_CMN_DATA -static int adccmn_lock(struct stm32_dev_s *priv, bool lock); -#endif - static void adc_rccreset(struct stm32_dev_s *priv, bool reset); /* ADC Interrupt Handler */ @@ -814,28 +810,6 @@ static void adc_reg_startconv(struct stm32_dev_s *priv, bool enable) } } -/**************************************************************************** - * Name: adccmn_lock - ****************************************************************************/ - -#ifdef HAVE_ADC_CMN_DATA -static int adccmn_lock(struct stm32_dev_s *priv, bool lock) -{ - int ret; - - if (lock) - { - ret = nxsem_wait_uninterruptible(&priv->cmn->lock); - } - else - { - ret = nxsem_post(&priv->cmn->lock); - } - - return ret; -} -#endif - /**************************************************************************** * Name: adc_rccreset * @@ -1338,7 +1312,7 @@ static void adc_reset(struct adc_dev_s *dev) /* Only if this is the first initialzied ADC instance in the ADC block */ #ifdef HAVE_ADC_CMN_DATA - if (adccmn_lock(priv, true) < 0) + if (nxmutex_lock(&priv->cmn->lock) < 0) { leave_critical_section(flags); return; @@ -1357,7 +1331,7 @@ static void adc_reset(struct adc_dev_s *dev) } #ifdef HAVE_ADC_CMN_DATA - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); #endif leave_critical_section(flags); @@ -1443,14 +1417,14 @@ static int adc_setup(struct adc_dev_s *dev) #ifdef HAVE_ADC_CMN_DATA /* Increase instances counter */ - ret = adccmn_lock(priv, true); + ret = nxmutex_lock(&priv->cmn->lock); if (ret < 0) { return; } priv->cmn->initialized += 1; - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); #endif return ret; @@ -1485,7 +1459,7 @@ static void adc_shutdown(struct adc_dev_s *dev) #endif #ifdef HAVE_ADC_CMN_DATA - if (adccmn_lock(priv, true) < 0) + if (nxmutex_lock(&priv->cmn->lock) < 0) { return; } @@ -1518,8 +1492,7 @@ static void adc_shutdown(struct adc_dev_s *dev) /* Decrease instances counter */ priv->cmn->initialized -= 1; - - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); #endif } diff --git a/arch/arm/src/stm32f0l0g0/stm32_aes.c b/arch/arm/src/stm32f0l0g0/stm32_aes.c index a711788ba672b..62478a499d06e 100644 --- a/arch/arm/src/stm32f0l0g0/stm32_aes.c +++ b/arch/arm/src/stm32f0l0g0/stm32_aes.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -68,8 +68,8 @@ static int stm32aes_setup_cr(int mode, int encrypt); * Private Data ****************************************************************************/ -static sem_t g_stm32aes_lock; -static bool g_stm32aes_initdone = false; +static mutex_t g_stm32aes_lock; +static bool g_stm32aes_initdone = false; /**************************************************************************** * Public Data @@ -231,7 +231,7 @@ int stm32_aesinitialize(void) { uint32_t regval; - nxsem_init(&g_stm32aes_lock, 0, 1); + nxmutex_init(&g_stm32aes_lock); regval = getreg32(STM32_RCC_AHBENR); regval |= RCC_AHBENR_AESEN; @@ -252,8 +252,7 @@ int stm32_aesuninitialize(void) regval &= ~RCC_AHBENR_AESEN; putreg32(regval, STM32_RCC_AHBENR); - nxsem_destroy(&g_stm32aes_lock); - + nxmutex_destroy(&g_stm32aes_lock); return OK; } @@ -286,7 +285,7 @@ int aes_cypher(void *out, const void *in, size_t size, return -EINVAL; } - ret = nxsem_wait(&g_stm32aes_lock); + ret = nxmutex_lock(&g_stm32aes_lock); if (ret < 0) { return ret; @@ -319,6 +318,6 @@ int aes_cypher(void *out, const void *in, size_t size, stm32aes_enable(false); out: - nxsem_post(&g_stm32aes_lock); + nxmutex_unlock(&g_stm32aes_lock); return ret; } diff --git a/arch/arm/src/stm32f0l0g0/stm32_dma_v1.c b/arch/arm/src/stm32f0l0g0/stm32_dma_v1.c index 3a88a251096dd..fc8b6719f959d 100644 --- a/arch/arm/src/stm32f0l0g0/stm32_dma_v1.c +++ b/arch/arm/src/stm32f0l0g0/stm32_dma_v1.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include @@ -201,24 +201,6 @@ static inline void dmachan_putreg(struct stm32_dma_s *dmach, putreg32(value, dmach->base + offset); } -/**************************************************************************** - * Name: stm32_dmatake() and stm32_dmagive() - * - * Description: - * Used to get exclusive access to a DMA channel. - * - ****************************************************************************/ - -static int stm32_dmatake(struct stm32_dma_s *dmach) -{ - return nxsem_wait_uninterruptible(&dmach->sem); -} - -static inline void stm32_dmagive(struct stm32_dma_s *dmach) -{ - nxsem_post(&dmach->sem); -} - /**************************************************************************** * Name: stm32_dmachandisable * @@ -400,7 +382,7 @@ DMA_HANDLE stm32_dmachannel(unsigned int chndef) * is available if it is currently being used by another driver */ - ret = stm32_dmatake(dmach); + ret = nxsem_wait_uninterruptible(&dmach->sem); if (ret < 0) { return NULL; @@ -447,7 +429,7 @@ void stm32_dmafree(DMA_HANDLE handle) /* Release the channel */ - stm32_dmagive(dmach); + nxsem_post(&dmach->sem); } /**************************************************************************** diff --git a/arch/arm/src/stm32f0l0g0/stm32_i2c.c b/arch/arm/src/stm32f0l0g0/stm32_i2c.c index 33f8f82be2742..cc826af67a696 100644 --- a/arch/arm/src/stm32f0l0g0/stm32_i2c.c +++ b/arch/arm/src/stm32f0l0g0/stm32_i2c.c @@ -234,6 +234,7 @@ #include #include +#include #include #include #include @@ -401,7 +402,7 @@ struct stm32_i2c_priv_s 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 @@ -454,15 +455,11 @@ static inline void stm32_i2c_putreg32(struct stm32_i2c_priv_s *priv, static inline void stm32_i2c_modifyreg32(struct stm32_i2c_priv_s *priv, uint8_t offset, uint32_t clearbits, uint32_t setbits); -static inline int stm32_i2c_sem_wait(struct i2c_master_s *dev); #ifdef CONFIG_STM32F0L0G0_I2C_DYNTIMEO static uint32_t stm32_i2c_toticks(int msgc, struct i2c_msg_s *msgs); #endif /* CONFIG_STM32F0L0G0_I2C_DYNTIMEO */ static inline int stm32_i2c_sem_waitdone(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_post(struct i2c_master_s *dev); -static inline void stm32_i2c_sem_init(struct i2c_master_s *dev); -static inline void stm32_i2c_sem_destroy(struct i2c_master_s *dev); #ifdef CONFIG_I2C_TRACE static void stm32_i2c_tracereset(struct stm32_i2c_priv_s *priv); static void stm32_i2c_tracenew(struct stm32_i2c_priv_s *priv, @@ -517,6 +514,10 @@ static struct stm32_i2c_priv_s stm32_i2c1_priv = { .config = &stm32_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, @@ -548,6 +549,10 @@ static struct stm32_i2c_priv_s stm32_i2c2_priv = { .config = &stm32_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, @@ -579,6 +584,10 @@ static struct stm32_i2c_priv_s stm32_i2c3_priv = { .config = &stm32_i2c3_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, @@ -610,6 +619,10 @@ static struct stm32_i2c_priv_s stm32_i2c4_priv = { .config = &stm32_i2c4_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, @@ -709,20 +722,6 @@ static inline void stm32_i2c_modifyreg32(struct stm32_i2c_priv_s *priv, modifyreg32(priv->config->base + offset, clearbits, setbits); } -/**************************************************************************** - * Name: stm32_i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. May be interrupted by - * a signal. - * - ****************************************************************************/ - -static inline int stm32_i2c_sem_wait(struct i2c_master_s *dev) -{ - return nxsem_wait(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -} - /**************************************************************************** * Name: stm32_i2c_toticks * @@ -1034,58 +1033,6 @@ static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv) cr, sr); } -/**************************************************************************** - * Name: stm32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_post(struct i2c_master_s *dev) -{ - nxsem_post(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_init(struct i2c_master_s *dev) -{ - nxsem_init(&((struct stm32_i2c_inst_s *)dev)->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(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, 0, 0); - nxsem_set_protocol(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, - SEM_PRIO_NONE); -#endif -} - -/**************************************************************************** - * Name: stm32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_destroy(struct i2c_master_s *dev) -{ - nxsem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr); -#endif -} - /**************************************************************************** * Name: stm32_i2c_trace* * @@ -2501,7 +2448,7 @@ static int stm32_i2c_process(struct i2c_master_s *dev, /* Dump the trace result */ stm32_i2c_tracedump(priv); - stm32_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return -errval; } @@ -2521,7 +2468,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = stm32_i2c_sem_wait(dev); + ret = nxmutex_lock(&((struct stm32_i2c_inst_s *)dev)->priv->lock); if (ret >= 0) { ret = stm32_i2c_process(dev, msgs, count); @@ -2541,7 +2488,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, #ifdef CONFIG_I2C_RESET static int stm32_i2c_reset(struct i2c_master_s * dev) { - struct stm32_i2c_priv_s * priv; + struct stm32_i2c_priv_s *priv; unsigned int clock_count; unsigned int stretch_count; uint32_t scl_gpio; @@ -2561,7 +2508,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2661,7 +2608,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Release the port for re-use by other clients */ - stm32_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2702,7 +2649,6 @@ static int stm32_i2c_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_i2c_priv_s *priv = (struct stm32_i2c_priv_s *)((char *)cb - offsetof(struct stm32_i2c_priv_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -2717,13 +2663,7 @@ static int stm32_i2c_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for I2C bus is held. */ - if (nxsem_get_value(&priv->sem_excl, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -2821,7 +2761,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - stm32_i2c_sem_init((struct i2c_master_s *)inst); stm32_i2c_init(priv); #ifdef CONFIG_PM @@ -2879,10 +2818,6 @@ int stm32_i2cbus_uninitialize(struct i2c_master_s * dev) stm32_i2c_deinit(((struct stm32_i2c_inst_s *)dev)->priv); - /* Release unused resources */ - - stm32_i2c_sem_destroy((struct i2c_master_s *)dev); - kmm_free(dev); return OK; } diff --git a/arch/arm/src/stm32f0l0g0/stm32_rng.c b/arch/arm/src/stm32f0l0g0/stm32_rng.c index 7957f8bee2188..9e669d8813a4f 100644 --- a/arch/arm/src/stm32f0l0g0/stm32_rng.c +++ b/arch/arm/src/stm32f0l0g0/stm32_rng.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -57,7 +58,7 @@ static ssize_t stm32_rng_read(struct file *filep, char *buffer, size_t); struct rng_dev_s { - sem_t rd_devsem; /* Threads can only exclusively access the RNG */ + mutex_t rd_devlock; /* Threads can only exclusively access the RNG */ sem_t rd_readsem; /* To block until the buffer is filled */ char *rd_buf; size_t rd_buflen; @@ -100,8 +101,7 @@ static int stm32_rng_initialize(void) _info("Initializing RNG\n"); memset(&g_rngdev, 0, sizeof(struct rng_dev_s)); - - nxsem_init(&g_rngdev.rd_devsem, 0, 1); + nxmutex_init(&g_rngdev.rd_devlock); if (irq_attach(STM32_IRQ_RNG, stm32_rng_interrupt, NULL)) { @@ -232,7 +232,7 @@ static ssize_t stm32_rng_read(struct file *filep, { int ret; - ret = nxsem_wait(&g_rngdev.rd_devsem); + ret = nxmutex_lock(&g_rngdev.rd_devlock); if (ret < 0) { return ret; @@ -262,7 +262,7 @@ static ssize_t stm32_rng_read(struct file *filep, /* Free RNG for next use */ - nxsem_post(&g_rngdev.rd_devsem); + nxmutex_unlock(&g_rngdev.rd_devlock); return ret < 0 ? ret : buflen; } diff --git a/arch/arm/src/stm32f0l0g0/stm32_spi.c b/arch/arm/src/stm32f0l0g0/stm32_spi.c index 3dce4a00f53c7..7992302dd0f28 100644 --- a/arch/arm/src/stm32f0l0g0/stm32_spi.c +++ b/arch/arm/src/stm32f0l0g0/stm32_spi.c @@ -66,7 +66,7 @@ #include #include -#include +#include #include #include @@ -176,7 +176,7 @@ struct stm32_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 through 16) */ @@ -1048,11 +1048,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1794,7 +1794,6 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_spidev_s *priv = (struct stm32_spidev_s *)((char *)cb - offsetof(struct stm32_spidev_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -1807,15 +1806,7 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, case PM_STANDBY: case PM_SLEEP: - /* Check if exclusive lock for SPI bus is held. */ - - if (nxsem_get_value(&priv->exclsem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -1955,9 +1946,9 @@ static void spi_bus_initialize(struct stm32_spidev_s *priv) spi_putreg(priv, STM32_SPI_CRCPR_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32F0L0G0_SPI_DMA if (priv->rxch && priv->txch) diff --git a/arch/arm/src/stm32f7/stm32_adc.c b/arch/arm/src/stm32f7/stm32_adc.c index c452001a46737..495571a77a96b 100644 --- a/arch/arm/src/stm32f7/stm32_adc.c +++ b/arch/arm/src/stm32f7/stm32_adc.c @@ -176,7 +176,7 @@ struct adccmn_data_s { uint8_t refcount; /* How many ADC instances are currently in use */ - sem_t lock; /* Exclusive access to common ADC data */ + mutex_t lock; /* Exclusive access to common ADC data */ }; /* This structure describes the state of one ADC block */ @@ -284,8 +284,6 @@ static void tim_modifyreg(struct stm32_dev_s *priv, int offset, static void tim_dumpregs(struct stm32_dev_s *priv, const char *msg); #endif -static int adccmn_lock(struct stm32_dev_s *priv, bool lock); - static void adc_rccreset(struct stm32_dev_s *priv, bool reset); /* ADC Interrupt Handler */ @@ -1301,26 +1299,6 @@ static void adc_inj_startconv(struct stm32_dev_s *priv, bool enable) #endif /* ADC_HAVE_INJECTED */ -/**************************************************************************** - * Name: adccmn_lock - ****************************************************************************/ - -static int adccmn_lock(struct stm32_dev_s *priv, bool lock) -{ - int ret; - - if (lock) - { - ret = nxsem_wait_uninterruptible(&priv->cmn->lock); - } - else - { - ret = nxsem_post(&priv->cmn->lock); - } - - return ret; -} - /**************************************************************************** * Name: adc_rccreset * @@ -1769,7 +1747,7 @@ static void adc_reset(struct adc_dev_s *dev) /* Only if this is the first initialzied ADC instance in the ADC block */ - if (adccmn_lock(priv, true) < 0) + if (nxmutex_lock(&priv->cmn->lock) < 0) { goto out; } @@ -1785,7 +1763,7 @@ static void adc_reset(struct adc_dev_s *dev) adc_rccreset(priv, false); } - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); out: leave_critical_section(flags); @@ -1866,7 +1844,7 @@ static int adc_setup(struct adc_dev_s *dev) /* Increase instances counter */ - ret = adccmn_lock(priv, true); + ret = nxmutex_lock(&priv->cmn->lock); if (ret < 0) { return ret; @@ -1883,7 +1861,7 @@ static int adc_setup(struct adc_dev_s *dev) } priv->cmn->refcount += 1; - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); /* The ADC device is ready */ @@ -1927,7 +1905,7 @@ static void adc_shutdown(struct adc_dev_s *dev) adc_enable(priv, false); - if (adccmn_lock(priv, true) < 0) + if (nxmutex_lock(&priv->cmn->lock) < 0) { return; } @@ -1969,7 +1947,7 @@ static void adc_shutdown(struct adc_dev_s *dev) priv->cmn->refcount -= 1; } - adccmn_lock(priv, false); + nxmutex_unlock(&priv->cmn->lock); } /**************************************************************************** @@ -3076,8 +3054,7 @@ struct adc_dev_s *stm32_adc_initialize(int intf, * the ADC block. */ - nxsem_init(&priv->cmn->lock, 0, 1); - + nxmutex_init(&priv->cmn->lock); return dev; } diff --git a/arch/arm/src/stm32f7/stm32_bbsram.c b/arch/arm/src/stm32f7/stm32_bbsram.c index 6ff5295bdeb73..c6f912ede5658 100644 --- a/arch/arm/src/stm32f7/stm32_bbsram.c +++ b/arch/arm/src/stm32f7/stm32_bbsram.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -96,7 +97,7 @@ struct bbsramfh_s struct stm32_bbsram_s { - sem_t exclsem; /* For atomic accesses to this structure */ + mutex_t lock; /* For atomic accesses to this structure */ uint8_t refs; /* Number of references */ struct bbsramfh_s *bbf; /* File in bbram */ }; @@ -181,35 +182,7 @@ static void stm32_bbsram_dump(struct bbsramfh_s *bbf, char *op) #endif /**************************************************************************** - * Name: stm32_bbsram_semgive - ****************************************************************************/ - -static void stm32_bbsram_semgive(struct stm32_bbsram_s *priv) -{ - nxsem_post(&priv->exclsem); -} - -/**************************************************************************** - * Name: stm32_bbsram_semtake - * - * Description: - * Take a semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the CAN peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int stm32_bbsram_semtake(struct stm32_bbsram_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -/**************************************************************************** - * Name: stm32_bbsram_ulock + * Name: stm32_bbsram_unlock * * Description: * Unprotects RTC registers, RTC backup data registers and backup SRAM @@ -286,7 +259,7 @@ static int stm32_bbsram_open(struct file *filep) /* Increment the reference count */ - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -301,7 +274,7 @@ static int stm32_bbsram_open(struct file *filep) bbr->refs++; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return OK; } @@ -339,7 +312,7 @@ static int stm32_bbsram_close(struct file *filep) DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -368,7 +341,7 @@ static int stm32_bbsram_close(struct file *filep) } } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return ret; } @@ -387,7 +360,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (off_t)ret; @@ -413,7 +386,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, /* Return EINVAL if the whence argument is invalid */ - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return -EINVAL; } @@ -439,7 +412,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, ret = -EINVAL; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return ret; } @@ -457,7 +430,7 @@ static ssize_t stm32_bbsram_read(struct file *filep, char *buffer, DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (ssize_t)ret; @@ -472,7 +445,7 @@ static ssize_t stm32_bbsram_read(struct file *filep, char *buffer, memcpy(buffer, &bbr->bbf->data[filep->f_pos], len); filep->f_pos += len; - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return len; } @@ -514,7 +487,7 @@ static ssize_t stm32_bbsram_write(struct file *filep, len = bbr->bbf->len - filep->f_pos; } - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (ssize_t)ret; @@ -528,7 +501,7 @@ static ssize_t stm32_bbsram_write(struct file *filep, stm32_bbsram_lock(); filep->f_pos += len; BBSRAM_DUMP(bbr->bbf, "write done"); - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); } BBSRAM_DEBUG_READ(); @@ -571,7 +544,7 @@ static int stm32_bbsram_ioctl(struct file *filep, int cmd, { struct bbsramd_s *bbrr = (struct bbsramd_s *)((uintptr_t)arg); - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -592,7 +565,7 @@ static int stm32_bbsram_ioctl(struct file *filep, int cmd, ret = OK; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); } return ret; @@ -620,7 +593,7 @@ static int stm32_bbsram_unlink(struct inode *inode) DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -633,8 +606,8 @@ static int stm32_bbsram_unlink(struct inode *inode) bbr->bbf->crc = stm32_bbsram_crc(bbr->bbf); stm32_bbsram_lock(); bbr->refs = 0; - stm32_bbsram_semgive(bbr); - nxsem_destroy(&bbr->exclsem); + nxmutex_unlock(&bbr->lock); + nxmutex_destroy(&bbr->lock); return 0; } #endif @@ -697,7 +670,7 @@ static int stm32_bbsram_probe(int *ent, struct stm32_bbsram_s pdev[]) pdev[i].bbf = pf; pf = (struct bbsramfh_s *)((uint8_t *)pf + alloc); - nxsem_init(&g_bbsram[i].exclsem, 0, 1); + nxmutex_init(&g_bbsram[i].lock); } avail -= alloc; diff --git a/arch/arm/src/stm32f7/stm32_dma.c b/arch/arm/src/stm32f7/stm32_dma.c index 7f1ad71469d2b..3ff569ae90cf2 100644 --- a/arch/arm/src/stm32f7/stm32_dma.c +++ b/arch/arm/src/stm32f7/stm32_dma.c @@ -229,24 +229,6 @@ static inline void dmast_putreg(struct stm32_dma_s *dmast, uint32_t offset, putreg32(value, dmast->base + offset); } -/**************************************************************************** - * Name: stm32_dmatake() and stm32_dmagive() - * - * Description: - * Used to get exclusive access to a DMA channel. - * - ****************************************************************************/ - -static int stm32_dmatake(struct stm32_dma_s *dmast) -{ - return nxsem_wait_uninterruptible(&dmast->sem); -} - -static inline void stm32_dmagive(struct stm32_dma_s *dmast) -{ - nxsem_post(&dmast->sem); -} - /**************************************************************************** * Name: stm32_dmastream * @@ -525,7 +507,7 @@ DMA_HANDLE stm32_dmachannel(unsigned int dmamap) * is available if it is currently being used by another driver */ - ret = stm32_dmatake(dmast); + ret = nxsem_wait_uninterruptible(&dmast->sem); if (ret < 0) { return NULL; @@ -567,7 +549,7 @@ void stm32_dmafree(DMA_HANDLE handle) /* Release the channel */ - stm32_dmagive(dmast); + nxsem_post(&dmast->sem); } /**************************************************************************** diff --git a/arch/arm/src/stm32f7/stm32_dma2d.c b/arch/arm/src/stm32f7/stm32_dma2d.c index ff350c5b6af42..b28be08cfff12 100644 --- a/arch/arm/src/stm32f7/stm32_dma2d.c +++ b/arch/arm/src/stm32f7/stm32_dma2d.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include @@ -111,7 +111,7 @@ struct stm32_dma2d_s uint32_t *clut; /* Color lookup table */ #endif - sem_t *lock; /* Ensure mutually exclusive access */ + mutex_t *lock; /* Ensure mutually exclusive access */ }; /* Interrupt handling */ @@ -247,9 +247,9 @@ static uint32_t g_clut[STM32_DMA2D_NCLUT * / 4]; #endif /* CONFIG_STM32F7_FB_CMAP */ -/* The DMA2D semaphore that enforces mutually exclusive access */ +/* The DMA2D mutex that enforces mutually exclusive access */ -static sem_t g_lock; +static mutex_t g_lock; /* Semaphore for interrupt handling */ @@ -749,7 +749,7 @@ static int stm32_dma2d_setclut(const struct fb_cmap_s *cmap) lcdinfo("cmap=%p\n", cmap); - nxsem_wait(priv->lock); + nxmutex_lock(priv->lock); for (n = cmap->first; n < cmap->len - 1 && n < STM32_DMA2D_NCLUT; n++) { @@ -783,8 +783,7 @@ static int stm32_dma2d_setclut(const struct fb_cmap_s *cmap) # endif } - nxsem_post(priv->lock); - + nxmutex_unlock(priv->lock); return OK; } #endif /* CONFIG_STM32F7_FB_CMAP */ @@ -831,7 +830,7 @@ static int stm32_dma2d_fillcolor(struct stm32_dma2d_overlay_s *oinfo, } #endif - nxsem_wait(priv->lock); + nxmutex_lock(priv->lock); /* Set output pfc */ @@ -863,7 +862,7 @@ static int stm32_dma2d_fillcolor(struct stm32_dma2d_overlay_s *oinfo, lcderr("ERROR: Returning ECANCELED\n"); } - nxsem_post(priv->lock); + nxmutex_unlock(priv->lock); return ret; } @@ -903,7 +902,7 @@ static int stm32_dma2d_blit(struct stm32_dma2d_overlay_s *doverlay, lcdinfo("doverlay=%p, destxpos=%d, destypos=%d, soverlay=%p, sarea=%p\n", doverlay, destxpos, destypos, soverlay, sarea); - nxsem_wait(priv->lock); + nxmutex_lock(priv->lock); /* Set output pfc */ @@ -953,7 +952,7 @@ static int stm32_dma2d_blit(struct stm32_dma2d_overlay_s *doverlay, lcderr("ERROR: Returning ECANCELED\n"); } - nxsem_post(priv->lock); + nxmutex_unlock(priv->lock); return ret; } @@ -1015,7 +1014,7 @@ static int stm32_dma2d_blend(struct stm32_dma2d_overlay_s *doverlay, } #endif - nxsem_wait(priv->lock); + nxmutex_lock(priv->lock); /* Set output pfc */ @@ -1063,7 +1062,7 @@ static int stm32_dma2d_blend(struct stm32_dma2d_overlay_s *doverlay, lcderr("ERROR: Returning ECANCELED\n"); } - nxsem_post(priv->lock); + nxmutex_unlock(priv->lock); return ret; } @@ -1093,11 +1092,11 @@ int stm32_dma2dinitialize(void) * arch/arm/src/stm32f7/stm32f7xxxx_rcc.c */ - /* Initialize the DMA2D semaphore that enforces mutually exclusive + /* Initialize the DMA2D mutex that enforces mutually exclusive * access to the driver */ - nxsem_init(&g_lock, 0, 1); + nxmutex_init(&g_lock); /* Initialize the semaphore for interrupt handling. This waitsem * semaphore is used for signaling and, hence, should not have diff --git a/arch/arm/src/stm32f7/stm32_flash.c b/arch/arm/src/stm32f7/stm32_flash.c index 36886d9f03dd3..fbeb101c4d099 100644 --- a/arch/arm/src/stm32f7/stm32_flash.c +++ b/arch/arm/src/stm32f7/stm32_flash.c @@ -52,7 +52,7 @@ #include #include -#include +#include #include #include @@ -77,26 +77,12 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions ****************************************************************************/ -static void stm32_waste(void) -{ -} - -static int sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_sem); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void flash_unlock(void) { while (getreg32(STM32_FLASH_SR) & FLASH_SR_BSY) @@ -126,14 +112,14 @@ int stm32_flash_unlock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_unlock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -142,14 +128,14 @@ int stm32_flash_lock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -339,7 +325,7 @@ ssize_t up_progmem_eraseblock(size_t block) return -EFAULT; } - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -359,7 +345,7 @@ ssize_t up_progmem_eraseblock(size_t block) } modifyreg32(STM32_FLASH_CR, FLASH_CR_SER, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -399,7 +385,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) addr -= flash_base; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -438,21 +424,21 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) if (getreg32(STM32_FLASH_SR) & FLASH_CR_SER) { modifyreg32(STM32_FLASH_CR, FLASH_CR_PG, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); return -EROFS; } if (getreg8(addr) != *byte) { modifyreg32(STM32_FLASH_CR, FLASH_CR_PG, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); return -EIO; } } modifyreg32(STM32_FLASH_CR, FLASH_CR_PG, 0); - sem_unlock(); + nxmutex_unlock(&g_lock); return written; } diff --git a/arch/arm/src/stm32f7/stm32_foc.c b/arch/arm/src/stm32f7/stm32_foc.c index ba17c22ff2f41..be6855c47009c 100644 --- a/arch/arm/src/stm32f7/stm32_foc.c +++ b/arch/arm/src/stm32f7/stm32_foc.c @@ -32,6 +32,7 @@ #include #include +#include #include "arm_internal.h" #include "stm32_gpio.h" @@ -532,7 +533,7 @@ struct stm32_foc_dev_s struct stm32_foc_adccmn_s { uint8_t cntr; /* ADC common counter */ - sem_t sem; /* Lock data */ + sem_t lock; /* Lock data */ }; /* STM32 FOC volatile data */ @@ -1139,7 +1140,7 @@ static int stm32_foc_setup(struct foc_dev_s *dev) /* Lock ADC common data */ - ret = nxsem_wait_uninterruptible(&priv->adc_cmn->sem); + ret = nxmutex_lock(&priv->adc_cmn->lock); if (ret < 0) { goto errout; @@ -1160,7 +1161,7 @@ static int stm32_foc_setup(struct foc_dev_s *dev) /* Unlock ADC common data */ - nxsem_post(&priv->adc_cmn->sem); + nxmutex_unlock(&priv->adc_cmn->lock); /* Setup PWM */ @@ -1271,7 +1272,7 @@ static int stm32_foc_shutdown(struct foc_dev_s *dev) /* Lock ADC common data */ - ret = nxsem_wait_uninterruptible(&priv->adc_cmn->sem); + ret = nxmutex_lock(&priv->adc_cmn->lock); if (ret < 0) { goto errout; @@ -1292,7 +1293,7 @@ static int stm32_foc_shutdown(struct foc_dev_s *dev) /* Unlock ADC common data */ - nxsem_post(&priv->adc_cmn->sem); + nxmutex_unlock(&priv->adc_cmn->lock); /* Call board-specific shutdown */ @@ -2085,9 +2086,9 @@ stm32_foc_initialize(int inst, struct stm32_foc_board_s *board) modifyreg32(FOC_PWM_FZ_REG, 0, pwmfzbit); - /* Initialize ADC common data semaphore */ + /* Initialize ADC common data mutex */ - nxsem_init(&foc_priv->adc_cmn->sem, 0, 1); + nxmutex_init(&foc_priv->adc_cmn->lock); /* Initialize calibration semaphore */ diff --git a/arch/arm/src/stm32f7/stm32_i2c.c b/arch/arm/src/stm32f7/stm32_i2c.c index 07c8dc3b3d692..bca79cad8317a 100644 --- a/arch/arm/src/stm32f7/stm32_i2c.c +++ b/arch/arm/src/stm32f7/stm32_i2c.c @@ -246,6 +246,7 @@ #include #include +#include #include #include #include @@ -437,7 +438,7 @@ struct stm32_i2c_priv_s const struct stm32_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 @@ -495,9 +496,6 @@ static uint32_t stm32_i2c_toticks(int msgc, struct i2c_msg_s *msgs); #endif /* CONFIG_STM32F7_I2C_DYNTIMEO */ static inline int stm32_i2c_sem_waitdone(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_post(struct i2c_master_s *dev); -static inline void stm32_i2c_sem_init(struct i2c_master_s *dev); -static inline void stm32_i2c_sem_destroy(struct i2c_master_s *dev); #ifdef CONFIG_I2C_TRACE static void stm32_i2c_tracereset(struct stm32_i2c_priv_s *priv); static void stm32_i2c_tracenew(struct stm32_i2c_priv_s *priv, @@ -512,7 +510,7 @@ static inline void stm32_i2c_sendstart(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sendstop(struct stm32_i2c_priv_s *priv); static inline uint32_t stm32_i2c_getstatus(struct stm32_i2c_priv_s *priv); -static int stm32_i2c_isr_process(struct stm32_i2c_priv_s * priv); +static int stm32_i2c_isr_process(struct stm32_i2c_priv_s *priv); #ifndef CONFIG_I2C_POLLED static int stm32_i2c_isr(int irq, void *context, void *arg); #endif @@ -553,6 +551,10 @@ static struct stm32_i2c_priv_s stm32_i2c1_priv = { .config = &stm32_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, @@ -585,6 +587,10 @@ static struct stm32_i2c_priv_s stm32_i2c2_priv = { .config = &stm32_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, @@ -617,6 +623,10 @@ static struct stm32_i2c_priv_s stm32_i2c3_priv = { .config = &stm32_i2c3_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, @@ -649,6 +659,10 @@ static struct stm32_i2c_priv_s stm32_i2c4_priv = { .config = &stm32_i2c4_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, @@ -1057,58 +1071,6 @@ static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv) i2cinfo("Timeout with CR: %04" PRIx32 " SR: %04" PRIx32 "\n", cr, sr); } -/**************************************************************************** - * Name: stm32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_post(struct i2c_master_s *dev) -{ - nxsem_post(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_init(struct i2c_master_s *dev) -{ - nxsem_init(&((struct stm32_i2c_inst_s *)dev)->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(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, 0, 0); - nxsem_set_protocol(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, - SEM_PRIO_NONE); -#endif -} - -/**************************************************************************** - * Name: stm32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_destroy(struct i2c_master_s *dev) -{ - nxsem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr); -#endif -} - /**************************************************************************** * Name: stm32_i2c_trace* * @@ -2527,7 +2489,7 @@ static int stm32_i2c_process(struct i2c_master_s *dev, /* Dump the trace result */ stm32_i2c_tracedump(priv); - stm32_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return -errval; } @@ -2547,7 +2509,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = nxsem_wait(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); + ret = nxmutex_lock(&((struct stm32_i2c_inst_s *)dev)->priv->lock); if (ret >= 0) { ret = stm32_i2c_process(dev, msgs, count); @@ -2567,7 +2529,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, #ifdef CONFIG_I2C_RESET static int stm32_i2c_reset(struct i2c_master_s * dev) { - struct stm32_i2c_priv_s * priv; + struct stm32_i2c_priv_s *priv; unsigned int clock_count; unsigned int stretch_count; uint32_t scl_gpio; @@ -2587,7 +2549,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2687,7 +2649,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Release the port for re-use by other clients */ - stm32_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2728,7 +2690,6 @@ static int stm32_i2c_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_i2c_priv_s *priv = (struct stm32_i2c_priv_s *)((char *)cb - offsetof(struct stm32_i2c_priv_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -2743,13 +2704,7 @@ static int stm32_i2c_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for I2C bus is held. */ - if (nxsem_get_value(&priv->sem_excl, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -2845,7 +2800,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - stm32_i2c_sem_init((struct i2c_master_s *)inst); stm32_i2c_init(priv); #ifdef CONFIG_PM @@ -2903,10 +2857,6 @@ int stm32_i2cbus_uninitialize(struct i2c_master_s * dev) stm32_i2c_deinit(((struct stm32_i2c_inst_s *)dev)->priv); - /* Release unused resources */ - - stm32_i2c_sem_destroy((struct i2c_master_s *)dev); - kmm_free(dev); return OK; } diff --git a/arch/arm/src/stm32f7/stm32_ltdc.c b/arch/arm/src/stm32f7/stm32_ltdc.c index 454dd771ba89b..dcebf243f874e 100644 --- a/arch/arm/src/stm32f7/stm32_ltdc.c +++ b/arch/arm/src/stm32f7/stm32_ltdc.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include @@ -617,7 +617,7 @@ struct stm32_ltdc_s struct stm32_dma2d_overlay_s dma2dinfo; /* Overlay info for DMA2D */ #endif - sem_t *lock; /* Layer exclusive access */ + mutex_t *lock; /* Layer exclusive access */ }; /* This structure provides the overall state of the LTDC layer */ @@ -807,9 +807,9 @@ static uint8_t g_transpclut[STM32_LTDC_NCLUT]; # endif #endif /* CONFIG_STM32F7_FB_CMAP */ -/* The LTDC semaphore that enforces mutually exclusive access */ +/* The LTDC mutex that enforces mutually exclusive access */ -static sem_t g_lock; +static mutex_t g_lock; /* The semaphore for interrupt handling */ @@ -1611,9 +1611,9 @@ static int stm32_ltdc_reload(uint8_t value, bool waitvblank) static void stm32_ltdc_irqconfig(void) { - /* Initialize the LTDC semaphore that enforces mutually exclusive access */ + /* Initialize the LTDC mutex that enforces mutually exclusive access */ - nxsem_init(&g_lock, 0, 1); + nxmutex_init(&g_lock); /* Initialize the semaphore for interrupt handling. This waitsem * semaphore is used for signaling and, hence, should not have priority @@ -2421,9 +2421,9 @@ static int stm32_getcmap(struct fb_vtable_s *vtable, # else layer = &priv->layer[LTDC_LAYER_L1]; # endif - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); stm32_ltdc_lgetclut(layer, cmap); - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); ret = OK; } @@ -2498,7 +2498,7 @@ static int stm32_putcmap(struct fb_vtable_s *vtable, /* Update the layer clut register */ - nxsem_wait(&g_lock); + nxmutex_lock(&g_lock); for (n = 0; n < LTDC_NLAYERS; n++) { @@ -2511,7 +2511,7 @@ static int stm32_putcmap(struct fb_vtable_s *vtable, priv->dma2d->setclut(cmap); # endif - nxsem_post(&g_lock); + nxmutex_unlock(&g_lock); ret = OK; } @@ -2593,7 +2593,7 @@ static int stm32_settransp(struct fb_vtable_s *vtable, { struct stm32_ltdc_s * layer = &priv->layer[oinfo->overlay]; - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); layer->oinfo.transp.transp = oinfo->transp.transp; layer->oinfo.transp.transp_mode = oinfo->transp.transp_mode; @@ -2616,7 +2616,7 @@ static int stm32_settransp(struct fb_vtable_s *vtable, layer->oinfo.transp.transp_mode); } - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return OK; } @@ -2658,7 +2658,7 @@ static int stm32_setchromakey(struct fb_vtable_s *vtable, } # endif - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); # ifdef CONFIG_STM32F7_FB_CMAP if (oinfo->chromakey >= g_vtable.cmap.len) { @@ -2676,7 +2676,7 @@ static int stm32_setchromakey(struct fb_vtable_s *vtable, ret = OK; } - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return ret; } # ifdef CONFIG_STM32F7_DMA2D @@ -2720,11 +2720,11 @@ static int stm32_setcolor(struct fb_vtable_s *vtable, DEBUGASSERT(&layer->oinfo == poverlay); - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); poverlay->color = oinfo->color; ret = priv->dma2d->fillcolor(&layer->dma2dinfo, &poverlay->sarea, poverlay->color); - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return ret; # else @@ -2756,13 +2756,13 @@ static int stm32_setblank(struct fb_vtable_s *vtable, { struct stm32_ltdc_s * layer = &priv->layer[oinfo->overlay]; - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); layer->oinfo.blank = oinfo->blank; /* Enable or disable layer */ stm32_ltdc_lenable(layer, (layer->oinfo.blank == 0)); - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return OK; } @@ -2808,9 +2808,9 @@ static int stm32_setarea(struct fb_vtable_s *vtable, struct stm32_ltdc_s * layer = &priv->layer[oinfo->overlay]; - nxsem_wait(layer->lock); + nxmutex_lock(layer->lock); memcpy(&layer->oinfo.sarea, &oinfo->sarea, sizeof(struct fb_area_s)); - nxsem_post(layer->lock); + nxmutex_unlock(layer->lock); return OK; } @@ -2865,10 +2865,10 @@ static int stm32_blit(struct fb_vtable_s *vtable, sarea.w = MIN(darea->w, sarea.w); sarea.h = MIN(darea->h, sarea.h); - nxsem_wait(dlayer->lock); + nxmutex_lock(dlayer->lock); ret = priv->dma2d->blit(&dlayer->dma2dinfo, darea->x, darea->y, &slayer->dma2dinfo, &sarea); - nxsem_post(dlayer->lock); + nxmutex_unlock(dlayer->lock); return ret; # else @@ -2934,11 +2934,11 @@ static int stm32_blend(struct fb_vtable_s *vtable, barea.w = MIN(farea->w, barea.w); barea.h = MIN(farea->h, barea.h); - nxsem_wait(dlayer->lock); + nxmutex_lock(dlayer->lock); ret = priv->dma2d->blend(&dlayer->dma2dinfo, darea->x, darea->y, &flayer->dma2dinfo, farea->x, farea->y, &blayer->dma2dinfo, &barea); - nxsem_post(dlayer->lock); + nxmutex_unlock(dlayer->lock); return ret; # else diff --git a/arch/arm/src/stm32f7/stm32_otghost.c b/arch/arm/src/stm32f7/stm32_otghost.c index fd6f95876661c..7bdd334604e95 100644 --- a/arch/arm/src/stm32f7/stm32_otghost.c +++ b/arch/arm/src/stm32f7/stm32_otghost.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -253,7 +254,7 @@ struct stm32_usbhost_s volatile bool connected; /* Connected to device */ volatile bool change; /* Connection change */ volatile bool pscwait; /* True: Thread is waiting for a port event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for a port event */ struct stm32_ctrlinfo_s ep0; /* Root hub port EP0 description */ @@ -293,12 +294,6 @@ static inline void stm32_modifyreg(uint32_t addr, uint32_t clrbits, # define stm32_pktdump(m,b,n) #endif -/* Semaphores ***************************************************************/ - -static int stm32_takesem(sem_t *sem); -static int stm32_takesem_noncancelable(sem_t *sem); -#define stm32_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t stm32_getle16(const uint8_t *val); @@ -628,54 +623,6 @@ static inline void stm32_modifyreg(uint32_t addr, uint32_t clrbits, stm32_putreg(addr, (((stm32_getreg(addr)) & ~clrbits) | setbits)); } -/**************************************************************************** - * Name: stm32_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int stm32_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: stm32_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int stm32_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: stm32_getle16 * @@ -1175,7 +1122,7 @@ static void stm32_chan_wakeup(struct stm32_usbhost_s *priv, OTG_VTRACE2_CHANWAKEUP_OUT, chan->epno, chan->result); - stm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -2977,7 +2924,7 @@ static void stm32_gint_connected(struct stm32_usbhost_s *priv) priv->smstate = SMSTATE_ATTACHED; if (priv->pscwait) { - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3025,7 +2972,7 @@ static void stm32_gint_disconnected(struct stm32_usbhost_s *priv) if (priv->pscwait) { - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3923,7 +3870,7 @@ static int stm32_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = stm32_takesem(&priv->pscsem); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -4104,7 +4051,7 @@ static int stm32_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4128,7 +4075,7 @@ static int stm32_ep0configure(struct usbhost_driver_s *drvr, stm32_chan_configure(priv, ep0info->inndx); - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4169,7 +4116,7 @@ static int stm32_epalloc(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4190,7 +4137,7 @@ static int stm32_epalloc(struct usbhost_driver_s *drvr, ret = stm32_xfrep_alloc(priv, epdesc, ep); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4223,7 +4170,7 @@ static int stm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem_noncancelable(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); /* A single channel is represent by an index in the range of 0 to * STM32_MAX_TX_FIFOS. Otherwise, the ep must be a pointer to an allocated @@ -4251,7 +4198,7 @@ static int stm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) kmm_free(ctrlep); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4487,7 +4434,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4531,7 +4478,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactions exit here */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4546,7 +4493,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4574,7 +4521,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4622,7 +4569,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactins exit here */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4638,7 +4585,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4696,7 +4643,7 @@ static ssize_t stm32_transfer(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -4713,7 +4660,7 @@ static ssize_t stm32_transfer(struct usbhost_driver_s *drvr, nbytes = stm32_out_transfer(priv, chidx, buffer, buflen); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nbytes; } @@ -4768,7 +4715,7 @@ static int stm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4785,7 +4732,7 @@ static int stm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, ret = stm32_out_asynch(priv, chidx, buffer, buflen, callback, arg); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4844,7 +4791,7 @@ static int stm32_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake'em up! */ - stm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -4921,7 +4868,7 @@ static int stm32_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -5275,10 +5222,10 @@ static inline void stm32_sw_initialize(struct stm32_usbhost_s *priv) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/stm32f7/stm32_qspi.c b/arch/arm/src/stm32f7/stm32_qspi.c index ea83b82aca99d..252a5e6ab74bd 100644 --- a/arch/arm/src/stm32f7/stm32_qspi.c +++ b/arch/arm/src/stm32f7/stm32_qspi.c @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -169,7 +169,7 @@ struct stm32f7_qspidev_s uint8_t nbits; /* Width of word in bits (8 to 32) */ uint8_t intf; /* QSPI controller number (0) */ bool initialized; /* TRUE: Controller has been initialized */ - sem_t exclsem; /* Assures mutually exclusive access to QSPI */ + mutex_t lock; /* Assures mutually exclusive access to QSPI */ bool memmap; /* TRUE: Controller is in memory mapped mode */ #ifdef CONFIG_STM32F7_QSPI_INTERRUPTS @@ -1750,11 +1750,11 @@ static int qspi_lock(struct qspi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2558,11 +2558,11 @@ struct qspi_dev_s *stm32f7_qspi_initialize(int intf) { /* Now perform one time initialization. * - * Initialize the QSPI semaphore that enforces mutually exclusive + * Initialize the QSPI mutex that enforces mutually exclusive * access to the QSPI registers. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32F7_QSPI_DMA /* Pre-allocate DMA channels. */ @@ -2642,7 +2642,7 @@ struct qspi_dev_s *stm32f7_qspi_initialize(int intf) } #endif - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); return NULL; } diff --git a/arch/arm/src/stm32f7/stm32_rng.c b/arch/arm/src/stm32f7/stm32_rng.c index 30ac7dbf49608..7aec1bc6f9bf5 100644 --- a/arch/arm/src/stm32f7/stm32_rng.c +++ b/arch/arm/src/stm32f7/stm32_rng.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include @@ -57,7 +57,7 @@ static ssize_t stm32_rngread(struct file *filep, char *buffer, size_t); struct rng_dev_s { - sem_t rd_devsem; /* Threads can only exclusively access the RNG */ + mutex_t rd_devlock; /* Threads can only exclusively access the RNG */ sem_t rd_readsem; /* To block until the buffer is filled */ char *rd_buf; size_t rd_buflen; @@ -99,7 +99,7 @@ static int stm32_rng_initialize(void) memset(&g_rngdev, 0, sizeof(struct rng_dev_s)); - nxsem_init(&g_rngdev.rd_devsem, 0, 1); + nxmutex_init(&g_rngdev.rd_devlock); if (irq_attach(STM32_IRQ_RNG, stm32_rnginterrupt, NULL)) { @@ -251,7 +251,7 @@ static ssize_t stm32_rngread(struct file *filep, char *buffer, size_t buflen) { int ret; - ret = nxsem_wait(&g_rngdev.rd_devsem); + ret = nxmutex_lock(&g_rngdev.rd_devlock); if (ret < 0) { return ret; @@ -283,9 +283,9 @@ static ssize_t stm32_rngread(struct file *filep, char *buffer, size_t buflen) nxsem_destroy(&g_rngdev.rd_readsem); - /* Free RNG via the device semaphore for next use */ + /* Free RNG via the device mutex for next use */ - nxsem_post(&g_rngdev.rd_devsem); + nxmutex_unlock(&g_rngdev.rd_devlock); return ret < 0 ? ret : buflen; } diff --git a/arch/arm/src/stm32f7/stm32_rtc_lowerhalf.c b/arch/arm/src/stm32f7/stm32_rtc_lowerhalf.c index 794fa3e0e97b5..c9da58ac8aaf8 100644 --- a/arch/arm/src/stm32f7/stm32_rtc_lowerhalf.c +++ b/arch/arm/src/stm32f7/stm32_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -74,7 +75,7 @@ struct stm32_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -233,7 +234,7 @@ static int stm32_rdtime(struct rtc_lowerhalf_s *lower, priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -258,12 +259,12 @@ static int stm32_rdtime(struct rtc_lowerhalf_s *lower, { int errcode = get_errno(); DEBUGASSERT(errcode > 0); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -errcode; } #endif - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -291,7 +292,7 @@ static int stm32_settime(struct rtc_lowerhalf_s *lower, priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -319,7 +320,7 @@ static int stm32_settime(struct rtc_lowerhalf_s *lower, ret = up_rtc_settime(&ts); #endif - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -374,7 +375,7 @@ static int stm32_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -407,8 +408,7 @@ static int stm32_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -514,7 +514,7 @@ static int stm32_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid) DEBUGASSERT(alarmid == RTC_ALARMA || alarmid == RTC_ALARMB); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -536,8 +536,7 @@ static int stm32_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid) ret = stm32_rtc_cancelalarm((enum alm_id_e)alarmid); } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -657,18 +656,16 @@ static int stm32_setperiodic(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } memcpy(&priv->periodic, alarminfo, sizeof(struct lower_setperiodic_s)); - ret = stm32_rtc_setperiodic(&alarminfo->period, stm32_periodic_callback); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -700,7 +697,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) DEBUGASSERT(id == 0); - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -708,8 +705,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) ret = stm32_rtc_cancelperiodic(); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -742,8 +738,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) struct rtc_lowerhalf_s *stm32_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/arm/src/stm32f7/stm32_sai.c b/arch/arm/src/stm32f7/stm32_sai.c index 24bb938818772..37adb33f9cfb0 100644 --- a/arch/arm/src/stm32f7/stm32_sai.c +++ b/arch/arm/src/stm32f7/stm32_sai.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include @@ -188,7 +189,7 @@ struct stm32f7_sai_s stm32_sai_sampleratecb_t sampleratecb; uintptr_t base; /* SAI block register base address */ - sem_t exclsem; /* Assures mutually exclusive access to SAI */ + mutex_t lock; /* Assures mutually exclusive access to SAI */ uint32_t frequency; /* SAI clock frequency */ uint32_t syncen; /* Synchronization setting */ #ifdef CONFIG_STM32F7_SAI_DMA @@ -223,14 +224,6 @@ static void sai_dump_regs(struct stm32f7_sai_s *priv, const char *msg); # define sai_dump_regs(s,m) #endif -/* Semaphore helpers */ - -static void sai_exclsem_take(struct stm32f7_sai_s *priv); -#define sai_exclsem_give(priv) nxsem_post(&priv->exclsem) - -static void sai_bufsem_take(struct stm32f7_sai_s *priv); -#define sai_bufsem_give(priv) nxsem_post(&priv->bufsem) - /* Buffer container helpers */ static struct sai_buffer_s * @@ -685,37 +678,6 @@ static void sai_dump_regs(struct stm32f7_sai_s *priv, const char *msg) } #endif -/**************************************************************************** - * Name: sai_exclsem_take - * - * Description: - * Take the exclusive access semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the SAI peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void sai_exclsem_take(struct stm32f7_sai_s *priv) -{ - int ret; - - /* Wait until we successfully get the semaphore. EINTR is the only - * expected 'failure' (meaning that the wait for the semaphore was - * interrupted by a signal). - */ - - do - { - ret = nxsem_wait(&priv->exclsem); - DEBUGASSERT(ret == 0 || ret == -EINTR); - } - while (ret == -EINTR); -} - /**************************************************************************** * Name: sai_mckdivider * @@ -1258,7 +1220,7 @@ static int sai_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the SAI driver data */ - sai_exclsem_take(priv); + nxmutex_lock(&priv->lock); /* Verify not already TX'ing */ @@ -1266,7 +1228,7 @@ static int sai_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: SAI has no receiver\n"); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } mode = priv->syncen ? SAI_CR1_MODE_SLAVE_RX : SAI_CR1_MODE_MASTER_RX; @@ -1299,11 +1261,11 @@ static int sai_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, #endif DEBUGASSERT(ret == OK); leave_critical_section(flags); - sai_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - sai_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); sai_buf_free(priv, bfcontainer); return ret; } @@ -1358,7 +1320,7 @@ static int sai_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the SAI driver data */ - sai_exclsem_take(priv); + nxmutex_lock(&priv->lock); /* Verify not already RX'ing */ @@ -1366,7 +1328,7 @@ static int sai_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: SAI has no transmitter\n"); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } mode = priv->syncen ? SAI_CR1_MODE_SLAVE_TX : SAI_CR1_MODE_MASTER_TX; @@ -1399,46 +1361,15 @@ static int sai_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, #endif DEBUGASSERT(ret == OK); leave_critical_section(flags); - sai_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - sai_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); sai_buf_free(priv, bfcontainer); return ret; } -/**************************************************************************** - * Name: sai_bufsem_take - * - * Description: - * Take the buffer semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the SAI peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void sai_bufsem_take(struct stm32f7_sai_s *priv) -{ - int ret; - - /* Wait until we successfully get the semaphore. EINTR is the only - * expected 'failure' (meaning that the wait for the semaphore was - * interrupted by a signal). - */ - - do - { - ret = nxsem_wait(&priv->bufsem); - DEBUGASSERT(ret == 0 || ret == -EINTR); - } - while (ret == -EINTR); -} - /**************************************************************************** * Name: sai_buf_allocate * @@ -1468,7 +1399,7 @@ static struct sai_buffer_s *sai_buf_allocate(struct stm32f7_sai_s *priv) * have at least one free buffer container. */ - sai_bufsem_take(priv); + nxsem_wait_uninterruptible(&priv->bufsem); /* Get the buffer from the head of the free list */ @@ -1515,7 +1446,7 @@ static void sai_buf_free(struct stm32f7_sai_s *priv, /* Wake up any threads waiting for a buffer container */ - sai_bufsem_give(priv); + nxsem_post(&priv->bufsem); } /**************************************************************************** @@ -1568,7 +1499,7 @@ static void sai_portinitialize(struct stm32f7_sai_s *priv) { sai_dump_regs(priv, "Before initialization"); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Initialize buffering */ diff --git a/arch/arm/src/stm32f7/stm32_sdmmc.c b/arch/arm/src/stm32f7/stm32_sdmmc.c index 35803eda426af..7da5e9efe6e25 100644 --- a/arch/arm/src/stm32f7/stm32_sdmmc.c +++ b/arch/arm/src/stm32f7/stm32_sdmmc.c @@ -463,8 +463,6 @@ struct stm32_sampleregs_s static inline void sdmmc_putreg32(struct stm32_dev_s *priv, uint32_t value, int offset); static inline uint32_t sdmmc_getreg32(struct stm32_dev_s *priv, int offset); -static int stm32_takesem(struct stm32_dev_s *priv); -#define stm32_givesem(priv) (nxsem_post(&priv->waitsem)) static inline void stm32_setclkcr(struct stm32_dev_s *priv, uint32_t clkcr); static void stm32_configwaitints(struct stm32_dev_s *priv, uint32_t waitmask, sdio_eventset_t waitevents, sdio_eventset_t wkupevents); @@ -758,27 +756,6 @@ static inline void sdmmc_modifyreg32(struct stm32_dev_s *priv, int offset, leave_critical_section(flags); } -/**************************************************************************** - * Name: stm32_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * priv - Instance of the SDMMC private state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int stm32_takesem(struct stm32_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: stm32_setclkcr * @@ -1504,7 +1481,7 @@ static void stm32_endwait(struct stm32_dev_s *priv, /* Wake up the waiting thread */ - stm32_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2874,7 +2851,7 @@ static sdio_eventset_t stm32_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = stm32_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/arm/src/stm32f7/stm32_spi.c b/arch/arm/src/stm32f7/stm32_spi.c index d4c95117acc63..a384bb84d52b7 100644 --- a/arch/arm/src/stm32f7/stm32_spi.c +++ b/arch/arm/src/stm32f7/stm32_spi.c @@ -59,7 +59,7 @@ #include #include -#include +#include #include #include @@ -203,7 +203,7 @@ struct stm32_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ int8_t nbits; /* Width of word in bits */ @@ -1238,11 +1238,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2061,7 +2061,6 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_spidev_s *priv = (struct stm32_spidev_s *)((char *)cb - offsetof(struct stm32_spidev_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -2076,13 +2075,7 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for SPI bus is held. */ - if (nxsem_get_value(&priv->exclsem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper * PM states. @@ -2160,9 +2153,9 @@ static void spi_bus_initialize(struct stm32_spidev_s *priv) spi_putreg(priv, STM32_SPI_CRCPR_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access. */ + /* Initialize the SPI mutex that enforces mutually exclusive access. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32F7_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion. diff --git a/arch/arm/src/stm32h7/stm32_bbsram.c b/arch/arm/src/stm32h7/stm32_bbsram.c index 4c14bcd8f6276..3cb4190ece8ff 100644 --- a/arch/arm/src/stm32h7/stm32_bbsram.c +++ b/arch/arm/src/stm32h7/stm32_bbsram.c @@ -40,6 +40,7 @@ #include #include #include +#include #include "stm32_bbsram.h" #include "chip.h" @@ -117,7 +118,7 @@ struct bbsramfh_s struct stm32_bbsram_s { - sem_t exclsem; /* For atomic accesses to this structure */ + mutex_t lock; /* For atomic accesses to this structure */ uint8_t refs; /* Number of references */ struct bbsramfh_s *bbf; /* File in bbram */ }; @@ -202,35 +203,7 @@ static void stm32_bbsram_dump(struct bbsramfh_s *bbf, char *op) #endif /**************************************************************************** - * Name: stm32_bbsram_semgive - ****************************************************************************/ - -static void stm32_bbsram_semgive(struct stm32_bbsram_s *priv) -{ - nxsem_post(&priv->exclsem); -} - -/**************************************************************************** - * Name: stm32_bbsram_semtake - * - * Description: - * Take a semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the CAN peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int stm32_bbsram_semtake(struct stm32_bbsram_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -/**************************************************************************** - * Name: stm32_bbsram_ulock + * Name: stm32_bbsram_unlock * * Description: * Unprotects RTC registers, RTC backup data registers and backup SRAM @@ -332,7 +305,7 @@ static int stm32_bbsram_open(struct file *filep) /* Increment the reference count */ - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -347,7 +320,7 @@ static int stm32_bbsram_open(struct file *filep) bbr->refs++; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return OK; } @@ -385,7 +358,7 @@ static int stm32_bbsram_close(struct file *filep) DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -414,7 +387,7 @@ static int stm32_bbsram_close(struct file *filep) } } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return ret; } @@ -433,7 +406,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (off_t)ret; @@ -459,7 +432,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, /* Return EINVAL if the whence argument is invalid */ - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return -EINVAL; } @@ -485,7 +458,7 @@ static off_t stm32_bbsram_seek(struct file *filep, off_t offset, ret = -EINVAL; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return ret; } @@ -503,7 +476,7 @@ static ssize_t stm32_bbsram_read(struct file *filep, char *buffer, DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (ssize_t)ret; @@ -518,7 +491,7 @@ static ssize_t stm32_bbsram_read(struct file *filep, char *buffer, memcpy(buffer, &bbr->bbf->data[filep->f_pos], len); filep->f_pos += len; - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return len; } @@ -561,7 +534,7 @@ static ssize_t stm32_bbsram_write(struct file *filep, len = bbr->bbf->len - filep->f_pos; } - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return (ssize_t)ret; @@ -575,7 +548,7 @@ static ssize_t stm32_bbsram_write(struct file *filep, stm32_bbsram_lock(); filep->f_pos += len; BBSRAM_DUMP(bbr->bbf, "write done"); - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); } BBSRAM_DEBUG_READ(); @@ -618,7 +591,7 @@ static int stm32_bbsram_ioctl(struct file *filep, int cmd, { struct bbsramd_s *bbrr = (struct bbsramd_s *)((uintptr_t)arg); - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -639,7 +612,7 @@ static int stm32_bbsram_ioctl(struct file *filep, int cmd, ret = OK; } - stm32_bbsram_semgive(bbr); + nxmutex_unlock(&bbr->lock); } return ret; @@ -667,7 +640,7 @@ static int stm32_bbsram_unlink(struct inode *inode) DEBUGASSERT(inode && inode->i_private); bbr = (struct stm32_bbsram_s *)inode->i_private; - ret = stm32_bbsram_semtake(bbr); + ret = nxmutex_lock(&bbr->lock); if (ret < 0) { return ret; @@ -681,8 +654,8 @@ static int stm32_bbsram_unlink(struct inode *inode) stm32_bbsram_ecc_workaround(bbr->bbf); stm32_bbsram_lock(); bbr->refs = 0; - stm32_bbsram_semgive(bbr); - nxsem_destroy(&bbr->exclsem); + nxmutex_unlock(&bbr->lock); + nxmutex_destroy(&bbr->lock); return 0; } #endif @@ -755,7 +728,7 @@ static int stm32_bbsram_probe(int *ent, struct stm32_bbsram_s pdev[]) pdev[i].bbf = pf; pf = (struct bbsramfh_s *)((uint8_t *)pf + alloc); - nxsem_init(&g_bbsram[i].exclsem, 0, 1); + nxmutex_init(&g_bbsram[i].lock); } avail -= alloc; diff --git a/arch/arm/src/stm32h7/stm32_flash.c b/arch/arm/src/stm32h7/stm32_flash.c index c38d253c1a865..2456e3f576b87 100644 --- a/arch/arm/src/stm32h7/stm32_flash.c +++ b/arch/arm/src/stm32h7/stm32_flash.c @@ -58,7 +58,7 @@ #include #include -#include +#include #include #include @@ -164,7 +164,7 @@ struct stm32h7_flash_priv_s { - sem_t sem; /* Bank exclusive */ + mutex_t lock; /* Bank exclusive */ uint32_t ifbase; /* FLASHIF interface base address */ uint32_t base; /* FLASH base address */ uint32_t stblock; /* The first Block Number */ @@ -177,7 +177,7 @@ struct stm32h7_flash_priv_s static struct stm32h7_flash_priv_s stm32h7_flash_bank1_priv = { - .sem = SEM_INITIALIZER(1), + .lock = NXMUTEX_INITIALIZER, .ifbase = STM32_FLASHIF_BASE + STM32_FLASH_BANK1_OFFSET, .base = STM32_FLASH_BANK1, .stblock = 0, @@ -186,7 +186,7 @@ static struct stm32h7_flash_priv_s stm32h7_flash_bank1_priv = #if STM32_DUAL_BANK static struct stm32h7_flash_priv_s stm32h7_flash_bank2_priv = { - .sem = SEM_INITIALIZER(1), + .lock = NXMUTEX_INITIALIZER, .ifbase = STM32_FLASHIF_BASE + STM32_FLASH_BANK2_OFFSET, .base = STM32_FLASH_BANK2, .stblock = PROGMEM_NBLOCKS / 2, @@ -243,33 +243,6 @@ static inline void stm32h7_flash_modifyreg32(struct stm32h7_flash_priv_s modifyreg32(priv->ifbase + offset, clearbits, setbits); } -/**************************************************************************** - * Name: stm32h7_flash_sem_lock - * - * Description: - * Take the Bank exclusive access semaphore - * - ****************************************************************************/ - -static int stm32h7_flash_sem_lock(struct stm32h7_flash_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem); -} - -/**************************************************************************** - * Name: stm32h7_flash_sem_unlock - * - * Description: - * Release the Bank exclusive access semaphore - * - ****************************************************************************/ - -static inline void stm32h7_flash_sem_unlock(struct stm32h7_flash_priv_s - *priv) -{ - nxsem_post(&priv->sem); -} - /**************************************************************************** * Name: stm32h7_unlock_flash * @@ -555,14 +528,14 @@ int stm32h7_flash_unlock(size_t addr) if (priv) { - ret = stm32h7_flash_sem_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } stm32h7_unlock_flash(priv); - stm32h7_flash_sem_unlock(priv); + nxmutex_unlock(&priv->lock); } return ret; @@ -583,14 +556,14 @@ int stm32h7_flash_lock(size_t addr) if (priv) { - ret = stm32h7_flash_sem_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } stm32h7_lock_flash(priv); - stm32h7_flash_sem_unlock(priv); + nxmutex_unlock(&priv->lock); } return ret; @@ -790,7 +763,7 @@ ssize_t up_progmem_eraseblock(size_t block) priv = stm32h7_flash_bank(block_address); - ret = stm32h7_flash_sem_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -799,7 +772,7 @@ ssize_t up_progmem_eraseblock(size_t block) if (stm32h7_wait_for_last_operation(priv)) { ret = -EIO; - goto exit_with_sem; + goto exit_with_lock; } /* Get flash ready and begin erasing single block */ @@ -817,7 +790,7 @@ ssize_t up_progmem_eraseblock(size_t block) if (stm32h7_wait_for_last_operation(priv)) { ret = -EIO; - goto exit_with_lock_sem; + goto exit_with_unlock; } stm32h7_flash_modifyreg32(priv, STM32_FLASH_CR1_OFFSET, FLASH_CR_SER, 0); @@ -825,11 +798,11 @@ ssize_t up_progmem_eraseblock(size_t block) 0); ret = 0; -exit_with_lock_sem: +exit_with_unlock: stm32h7_lock_flash(priv); -exit_with_sem: - stm32h7_flash_sem_unlock(priv); +exit_with_lock: + nxmutex_unlock(&priv->lock); /* Verify */ @@ -875,7 +848,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) return -EFAULT; } - ret = stm32h7_flash_sem_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -889,7 +862,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) if (stm32h7_wait_for_last_operation(priv)) { written = -EIO; - goto exit_with_sem; + goto exit_with_lock; } /* Get flash ready and begin flashing */ @@ -932,7 +905,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) if (stm32h7_wait_for_last_operation(priv)) { written = -EIO; - goto exit_with_lock_sem; + goto exit_with_unlock; } sr = stm32h7_flash_getreg32(priv, STM32_FLASH_SR1_OFFSET); @@ -945,7 +918,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) stm32h7_flash_modifyreg32(priv, STM32_FLASH_CCR1_OFFSET, 0, ~0); ret = -EIO; - goto exit_with_lock_sem; + goto exit_with_unlock; } } @@ -953,7 +926,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) stm32h7_flash_modifyreg32(priv, STM32_FLASH_CCR1_OFFSET, 0, ~0); -exit_with_lock_sem: +exit_with_unlock: stm32h7_lock_flash(priv); /* Verify */ @@ -993,8 +966,8 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) 0, ~0); } -exit_with_sem: - stm32h7_flash_sem_unlock(priv); +exit_with_lock: + nxmutex_unlock(&priv->lock); return written; } diff --git a/arch/arm/src/stm32h7/stm32_i2c.c b/arch/arm/src/stm32h7/stm32_i2c.c index 89c531e702360..5086a80807a71 100644 --- a/arch/arm/src/stm32h7/stm32_i2c.c +++ b/arch/arm/src/stm32h7/stm32_i2c.c @@ -216,6 +216,7 @@ #include #include +#include #include #include #include @@ -396,7 +397,7 @@ struct stm32_i2c_priv_s const struct stm32_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 @@ -454,9 +455,6 @@ static uint32_t stm32_i2c_toticks(int msgc, struct i2c_msg_s *msgs); #endif /* CONFIG_STM32H7_I2C_DYNTIMEO */ static inline int stm32_i2c_sem_waitdone(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv); -static inline void stm32_i2c_sem_post(struct i2c_master_s *dev); -static inline void stm32_i2c_sem_init(struct i2c_master_s *dev); -static inline void stm32_i2c_sem_destroy(struct i2c_master_s *dev); #ifdef CONFIG_I2C_TRACE static void stm32_i2c_tracereset(struct stm32_i2c_priv_s *priv); static void stm32_i2c_tracenew(struct stm32_i2c_priv_s *priv, @@ -471,7 +469,7 @@ static inline void stm32_i2c_sendstart(struct stm32_i2c_priv_s *priv); static inline void stm32_i2c_sendstop(struct stm32_i2c_priv_s *priv); static inline uint32_t stm32_i2c_getstatus(struct stm32_i2c_priv_s *priv); -static int stm32_i2c_isr_process(struct stm32_i2c_priv_s * priv); +static int stm32_i2c_isr_process(struct stm32_i2c_priv_s *priv); #ifndef CONFIG_I2C_POLLED static int stm32_i2c_isr(int irq, void *context, void *arg); #endif @@ -512,6 +510,10 @@ static struct stm32_i2c_priv_s stm32_i2c1_priv = { .config = &stm32_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, @@ -544,6 +546,10 @@ static struct stm32_i2c_priv_s stm32_i2c2_priv = { .config = &stm32_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, @@ -576,6 +582,10 @@ static struct stm32_i2c_priv_s stm32_i2c3_priv = { .config = &stm32_i2c3_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, @@ -608,6 +618,10 @@ static struct stm32_i2c_priv_s stm32_i2c4_priv = { .config = &stm32_i2c4_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, @@ -1016,58 +1030,6 @@ static inline void stm32_i2c_sem_waitstop(struct stm32_i2c_priv_s *priv) i2cinfo("Timeout with CR: %04" PRIx32 " SR: %04" PRIx32 "\n", cr, sr); } -/**************************************************************************** - * Name: stm32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_post(struct i2c_master_s *dev) -{ - nxsem_post(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_init(struct i2c_master_s *dev) -{ - nxsem_init(&((struct stm32_i2c_inst_s *)dev)->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(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, 0, 0); - nxsem_set_protocol(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr, - SEM_PRIO_NONE); -#endif -} - -/**************************************************************************** - * Name: stm32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_i2c_sem_destroy(struct i2c_master_s *dev) -{ - nxsem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&((struct stm32_i2c_inst_s *)dev)->priv->sem_isr); -#endif -} - /**************************************************************************** * Name: stm32_i2c_trace* * @@ -2491,7 +2453,7 @@ static int stm32_i2c_process(struct i2c_master_s *dev, /* Dump the trace result */ stm32_i2c_tracedump(priv); - stm32_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return -errval; } @@ -2511,7 +2473,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = nxsem_wait(&((struct stm32_i2c_inst_s *)dev)->priv->sem_excl); + ret = nxmutex_lock(&((struct stm32_i2c_inst_s *)dev)->priv->lock); if (ret >= 0) { ret = stm32_i2c_process(dev, msgs, count); @@ -2531,7 +2493,7 @@ static int stm32_i2c_transfer(struct i2c_master_s *dev, #ifdef CONFIG_I2C_RESET static int stm32_i2c_reset(struct i2c_master_s * dev) { - struct stm32_i2c_priv_s * priv; + struct stm32_i2c_priv_s *priv; unsigned int clock_count; unsigned int stretch_count; uint32_t scl_gpio; @@ -2551,7 +2513,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2651,7 +2613,7 @@ static int stm32_i2c_reset(struct i2c_master_s * dev) /* Release the port for re-use by other clients */ - stm32_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2692,7 +2654,6 @@ static int stm32_i2c_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_i2c_priv_s *priv = (struct stm32_i2c_priv_s *)((char *)cb - offsetof(struct stm32_i2c_priv_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -2707,13 +2668,7 @@ static int stm32_i2c_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for I2C bus is held. */ - if (nxsem_get_value(&priv->sem_excl, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -2809,7 +2764,6 @@ struct i2c_master_s *stm32_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - stm32_i2c_sem_init((struct i2c_master_s *)inst); stm32_i2c_init(priv); #ifdef CONFIG_PM @@ -2867,10 +2821,6 @@ int stm32_i2cbus_uninitialize(struct i2c_master_s * dev) stm32_i2c_deinit(((struct stm32_i2c_inst_s *)dev)->priv); - /* Release unused resources */ - - stm32_i2c_sem_destroy((struct i2c_master_s *)dev); - kmm_free(dev); return OK; } diff --git a/arch/arm/src/stm32h7/stm32_otghost.c b/arch/arm/src/stm32h7/stm32_otghost.c index 69130758e7901..807714d339a8e 100644 --- a/arch/arm/src/stm32h7/stm32_otghost.c +++ b/arch/arm/src/stm32h7/stm32_otghost.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -255,7 +256,7 @@ struct stm32_usbhost_s volatile bool connected; /* Connected to device */ volatile bool change; /* Connection change */ volatile bool pscwait; /* True: Thread is waiting for a port event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for a port event */ struct stm32_ctrlinfo_s ep0; /* Root hub port EP0 description */ @@ -295,12 +296,6 @@ static inline void stm32_modifyreg(uint32_t addr, uint32_t clrbits, # define stm32_pktdump(m,b,n) #endif -/* Semaphores ***************************************************************/ - -static int stm32_takesem(sem_t *sem); -static int stm32_takesem_noncancelable(sem_t *sem); -#define stm32_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t stm32_getle16(const uint8_t *val); @@ -630,54 +625,6 @@ static inline void stm32_modifyreg(uint32_t addr, uint32_t clrbits, stm32_putreg(addr, (((stm32_getreg(addr)) & ~clrbits) | setbits)); } -/**************************************************************************** - * Name: stm32_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int stm32_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: stm32_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int stm32_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: stm32_getle16 * @@ -1177,7 +1124,7 @@ static void stm32_chan_wakeup(struct stm32_usbhost_s *priv, OTG_VTRACE2_CHANWAKEUP_OUT, chan->epno, chan->result); - stm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -2978,7 +2925,7 @@ static void stm32_gint_connected(struct stm32_usbhost_s *priv) priv->smstate = SMSTATE_ATTACHED; if (priv->pscwait) { - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3026,7 +2973,7 @@ static void stm32_gint_disconnected(struct stm32_usbhost_s *priv) if (priv->pscwait) { - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3924,7 +3871,7 @@ static int stm32_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = stm32_takesem(&priv->pscsem); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -4106,7 +4053,7 @@ static int stm32_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4130,7 +4077,7 @@ static int stm32_ep0configure(struct usbhost_driver_s *drvr, stm32_chan_configure(priv, ep0info->inndx); - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4173,7 +4120,7 @@ static int stm32_epalloc(struct usbhost_driver_s *drvr, * structures. */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4194,7 +4141,7 @@ static int stm32_epalloc(struct usbhost_driver_s *drvr, ret = stm32_xfrep_alloc(priv, epdesc, ep); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4227,7 +4174,7 @@ static int stm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem_noncancelable(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); /* A single channel is represent by an index in the range of 0 to * STM32_MAX_TX_FIFOS. Otherwise, the ep must be a pointer to an allocated @@ -4255,7 +4202,7 @@ static int stm32_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) kmm_free(ctrlep); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4491,7 +4438,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4535,7 +4482,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactions exit here */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4550,7 +4497,7 @@ static int stm32_ctrlin(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4578,7 +4525,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4626,7 +4573,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, { /* All success transactins exit here */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4642,7 +4589,7 @@ static int stm32_ctrlout(struct usbhost_driver_s *drvr, usbhost_ep_t ep0, /* All failures exit here after all retries and timeouts are exhausted */ - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4700,7 +4647,7 @@ static ssize_t stm32_transfer(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -4717,7 +4664,7 @@ static ssize_t stm32_transfer(struct usbhost_driver_s *drvr, nbytes = stm32_out_transfer(priv, chidx, buffer, buflen); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nbytes; } @@ -4772,7 +4719,7 @@ static int stm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4789,7 +4736,7 @@ static int stm32_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, ret = stm32_out_asynch(priv, chidx, buffer, buflen, callback, arg); } - stm32_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4848,7 +4795,7 @@ static int stm32_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake'em up! */ - stm32_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -4925,7 +4872,7 @@ static int stm32_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - stm32_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -5279,10 +5226,10 @@ static inline void stm32_sw_initialize(struct stm32_usbhost_s *priv) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/stm32h7/stm32_qspi.c b/arch/arm/src/stm32h7/stm32_qspi.c index 7a2bb1a491884..2401ccbd52be6 100644 --- a/arch/arm/src/stm32h7/stm32_qspi.c +++ b/arch/arm/src/stm32h7/stm32_qspi.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -193,7 +194,7 @@ struct stm32h7_qspidev_s uint8_t nbits; /* Width of word in bits (8 to 32) */ uint8_t intf; /* QSPI controller number (0) */ bool initialized; /* TRUE: Controller has been initialized */ - sem_t exclsem; /* Assures mutually exclusive access to QSPI */ + mutex_t lock; /* Assures mutually exclusive access to QSPI */ bool memmap; /* TRUE: Controller is in memory mapped mode */ #ifdef CONFIG_STM32H7_QSPI_INTERRUPTS @@ -1801,23 +1802,13 @@ static int qspi_lock(struct qspi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - /* Take the semaphore (perhaps waiting) */ + /* Take the mutex (perhaps waiting) */ - do - { - ret = nxsem_wait(&priv->exclsem); - - /* The only case that an error should occur here is if the wait - * was awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2625,11 +2616,11 @@ struct qspi_dev_s *stm32h7_qspi_initialize(int intf) { /* Now perform one time initialization. * - * Initialize the QSPI semaphore that enforces mutually exclusive + * Initialize the QSPI mutex that enforces mutually exclusive * access to the QSPI registers. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32H7_QSPI_DMA /* Pre-allocate DMA channels. */ @@ -2709,7 +2700,7 @@ struct qspi_dev_s *stm32h7_qspi_initialize(int intf) } #endif - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); return NULL; } diff --git a/arch/arm/src/stm32h7/stm32_rtc_lowerhalf.c b/arch/arm/src/stm32h7/stm32_rtc_lowerhalf.c index 15181f9c8425d..9addd887be356 100644 --- a/arch/arm/src/stm32h7/stm32_rtc_lowerhalf.c +++ b/arch/arm/src/stm32h7/stm32_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "arm_internal.h" @@ -74,7 +75,7 @@ struct stm32_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -234,7 +235,7 @@ static int stm32_rdtime(struct rtc_lowerhalf_s *lower, priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -259,12 +260,12 @@ static int stm32_rdtime(struct rtc_lowerhalf_s *lower, { int errcode = get_errno(); DEBUGASSERT(errcode > 0); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -errcode; } #endif - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -292,7 +293,7 @@ static int stm32_settime(struct rtc_lowerhalf_s *lower, priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -320,7 +321,7 @@ static int stm32_settime(struct rtc_lowerhalf_s *lower, ret = up_rtc_settime(&ts); #endif - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -375,7 +376,7 @@ static int stm32_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -408,8 +409,7 @@ static int stm32_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -515,7 +515,7 @@ static int stm32_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid) DEBUGASSERT(alarmid == RTC_ALARMA || alarmid == RTC_ALARMB); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -537,8 +537,7 @@ static int stm32_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid) ret = stm32_rtc_cancelalarm((enum alm_id_e)alarmid); } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -658,18 +657,16 @@ static int stm32_setperiodic(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL); priv = (struct stm32_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } memcpy(&priv->periodic, alarminfo, sizeof(struct lower_setperiodic_s)); - ret = stm32_rtc_setperiodic(&alarminfo->period, stm32_periodic_callback); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -701,7 +698,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) DEBUGASSERT(id == 0); - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -709,8 +706,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) ret = stm32_rtc_cancelperiodic(); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -743,8 +739,7 @@ static int stm32_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) struct rtc_lowerhalf_s *stm32_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/arm/src/stm32h7/stm32_sdmmc.c b/arch/arm/src/stm32h7/stm32_sdmmc.c index 92d67a44da533..be4d15b0a0db5 100644 --- a/arch/arm/src/stm32h7/stm32_sdmmc.c +++ b/arch/arm/src/stm32h7/stm32_sdmmc.c @@ -423,8 +423,6 @@ struct stm32_sampleregs_s static inline void sdmmc_putreg32(struct stm32_dev_s *priv, uint32_t value, int offset); static inline uint32_t sdmmc_getreg32(struct stm32_dev_s *priv, int offset); -static int stm32_takesem(struct stm32_dev_s *priv); -#define stm32_givesem(priv) (nxsem_post(&priv->waitsem)) static inline void stm32_setclkcr(struct stm32_dev_s *priv, uint32_t clkcr); static void stm32_configwaitints(struct stm32_dev_s *priv, uint32_t waitmask, sdio_eventset_t waitevents, @@ -702,27 +700,6 @@ static inline void sdmmc_modifyreg32(struct stm32_dev_s *priv, int offset, leave_critical_section(flags); } -/**************************************************************************** - * Name: stm32_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * priv - Instance of the SDMMC private state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int stm32_takesem(struct stm32_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: stm32_setclkcr * @@ -1509,7 +1486,7 @@ static void stm32_endwait(struct stm32_dev_s *priv, /* Wake up the waiting thread */ - stm32_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2976,7 +2953,7 @@ static sdio_eventset_t stm32_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = stm32_takesem(priv); + ret = nxsem_wait_uninterruptible(priv); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/arm/src/stm32h7/stm32_spi.c b/arch/arm/src/stm32h7/stm32_spi.c index 53146257c59c2..15567e4d8db9d 100644 --- a/arch/arm/src/stm32h7/stm32_spi.c +++ b/arch/arm/src/stm32h7/stm32_spi.c @@ -60,7 +60,7 @@ #include #include #include -#include +#include #include #include @@ -256,7 +256,7 @@ struct stm32_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ int8_t nbits; /* Width of word in bits */ @@ -1418,11 +1418,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2378,7 +2378,6 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_spidev_s *priv = (struct stm32_spidev_s *)((char *)cb - offsetof(struct stm32_spidev_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -2393,13 +2392,7 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for SPI bus is held. */ - if (nxsem_get_value(&priv->exclsem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -2504,9 +2497,9 @@ static void spi_bus_initialize(struct stm32_spidev_s *priv) spi_putreg(priv, STM32_SPI_CRCPOLY_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access. */ + /* Initialize the SPI mutex that enforces mutually exclusive access. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32H7_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion. diff --git a/arch/arm/src/stm32h7/stm32_spi_slave.c b/arch/arm/src/stm32h7/stm32_spi_slave.c index 91d96a7deb478..9d0b09b8ebc56 100644 --- a/arch/arm/src/stm32h7/stm32_spi_slave.c +++ b/arch/arm/src/stm32h7/stm32_spi_slave.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include #include @@ -199,7 +199,7 @@ struct stm32_spidev_s bool dmarunning; /* DMA is started */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual + mutex_t lock; /* Held while chip is selected for mutual * exclusion */ int8_t nbits; /* Width of word in bits */ uint8_t mode; /* Mode 0,1,2,3 */ @@ -905,24 +905,13 @@ static int spi_lock(struct spi_slave_ctrlr_s *ctrlr, bool lock) if (lock) { - /* Take the semaphore (perhaps waiting) */ + /* Take the mutex (perhaps waiting) */ - do - { - ret = nxsem_wait(&priv->exclsem); - - /* The only case that an error should occur here is if the wait - * was awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); + ret = nxmutex_lock(&priv->lock); } else { - nxsem_post(&priv->exclsem); - ret = OK; + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1551,7 +1540,6 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_spidev_s *priv = (struct stm32_spidev_s *)((char *)cb - offsetof(struct stm32_spidev_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -1566,15 +1554,9 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for SPI bus is held. */ - if (nxsem_getvalue(&priv->exclsem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - /* If exclusive lock is held, do not allow entry to deeper PM states */ - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { return -EBUSY; } @@ -1666,9 +1648,9 @@ static void spi_slave_initialize(struct stm32_spidev_s *priv) spi_putreg(priv, STM32_SPI_CRCPOLY_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access. */ + /* Initialize the SPI mutex that enforces mutually exclusive access. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32H7_SPI_DMA /* DMA will be started in the interrupt handler, synchronized to the master diff --git a/arch/arm/src/stm32l4/stm32l4_1wire.c b/arch/arm/src/stm32l4/stm32l4_1wire.c index b7a9541f91683..0818cfae55562 100644 --- a/arch/arm/src/stm32l4/stm32l4_1wire.c +++ b/arch/arm/src/stm32l4/stm32l4_1wire.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -108,7 +109,7 @@ struct stm32_1wire_priv_s { const struct stm32_1wire_config_s *config; /* Port configuration */ volatile int refs; /* Reference count */ - sem_t sem_excl; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ sem_t sem_isr; /* Interrupt wait semaphore */ int baud; /* Baud rate */ const struct stm32_1wire_msg_s *msgs; /* Messages data */ @@ -143,11 +144,6 @@ static void stm32_1wire_set_apb_clock(struct stm32_1wire_priv_s *priv, bool on); static int stm32_1wire_init(struct stm32_1wire_priv_s *priv); static int stm32_1wire_deinit(struct stm32_1wire_priv_s *priv); -static inline void stm32_1wire_sem_init(struct stm32_1wire_priv_s *priv); -static inline void stm32_1wire_sem_destroy( - struct stm32_1wire_priv_s *priv); -static inline int stm32_1wire_sem_wait(struct stm32_1wire_priv_s *priv); -static inline void stm32_1wire_sem_post(struct stm32_1wire_priv_s *priv); static int stm32_1wire_process(struct stm32_1wire_priv_s *priv, const struct stm32_1wire_msg_s *msgs, int count); @@ -188,6 +184,8 @@ static struct stm32_1wire_priv_s stm32_1wire1_priv = { .config = &stm32_1wire1_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL, #ifdef CONFIG_PM .pm_cb.prepare = stm32_1wire_pm_prepare, @@ -210,6 +208,8 @@ static struct stm32_1wire_priv_s stm32_1wire2_priv = { .config = &stm32_1wire2_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL, #ifdef CONFIG_PM .pm_cb.prepare = stm32_1wire_pm_prepare, @@ -232,6 +232,8 @@ static struct stm32_1wire_priv_s stm32_1wire3_priv = { .config = &stm32_1wire3_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL, #ifdef CONFIG_PM .pm_cb.prepare = stm32_1wire_pm_prepare, @@ -254,6 +256,8 @@ static struct stm32_1wire_priv_s stm32_1wire4_priv = { .config = &stm32_1wire4_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL, #ifdef CONFIG_PM .pm_cb.prepare = stm32_1wire_pm_prepare, @@ -276,6 +280,8 @@ static struct stm32_1wire_priv_s stm32_1wire5_priv = { .config = &stm32_1wire5_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .msgs = NULL, #ifdef CONFIG_PM .pm_cb.prepare = stm32_1wire_pm_prepare, @@ -623,67 +629,6 @@ static int stm32_1wire_deinit(struct stm32_1wire_priv_s *priv) return OK; } -/**************************************************************************** - * Name: stm32_1wire_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32_1wire_sem_init(struct stm32_1wire_priv_s *priv) -{ - nxsem_init(&priv->sem_excl, 0, 1); - nxsem_init(&priv->sem_isr, 0, 0); - - /* The sem_isr semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ - - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); -} - -/**************************************************************************** - * Name: stm32_1wire_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32_1wire_sem_destroy( - struct stm32_1wire_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); - nxsem_destroy(&priv->sem_isr); -} - -/**************************************************************************** - * Name: stm32_1wire_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary - * - ****************************************************************************/ - -static inline int stm32_1wire_sem_wait(struct stm32_1wire_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32_1wire_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32_1wire_sem_post(struct stm32_1wire_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - /**************************************************************************** * Name: stm32_1wire_exec * @@ -701,7 +646,7 @@ static int stm32_1wire_process(struct stm32_1wire_priv_s *priv, /* Lock out other clients */ - ret = stm32_1wire_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -797,8 +742,7 @@ static int stm32_1wire_process(struct stm32_1wire_priv_s *priv, /* Release the port for re-use by other clients */ - stm32_1wire_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -1138,7 +1082,6 @@ static int stm32_1wire_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_1wire_priv_s *priv = (struct stm32_1wire_priv_s *)((char *)cb - offsetof(struct stm32_1wire_priv_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -1153,13 +1096,7 @@ static int stm32_1wire_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for 1-Wire bus is held. */ - if (nxsem_get_value(&priv->sem_excl, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -1270,7 +1207,6 @@ struct onewire_dev_s *stm32l4_1wireinitialize(int port) if (priv->refs++ == 0) { - stm32_1wire_sem_init(priv); stm32_1wire_init(priv); #ifdef CONFIG_PM @@ -1336,10 +1272,6 @@ int stm32l4_1wireuninitialize(struct onewire_dev_s *dev) stm32_1wire_deinit(priv); - /* Release unused resources */ - - stm32_1wire_sem_destroy(priv); - /* Free instance */ kmm_free(dev); diff --git a/arch/arm/src/stm32l4/stm32l4_flash.c b/arch/arm/src/stm32l4/stm32l4_flash.c index cda4ea41573b7..3344c137d11ec 100644 --- a/arch/arm/src/stm32l4/stm32l4_flash.c +++ b/arch/arm/src/stm32l4/stm32l4_flash.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include @@ -96,23 +96,13 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; static uint32_t g_page_buffer[FLASH_PAGE_WORDS]; /**************************************************************************** * Private Functions ****************************************************************************/ -static inline int sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_sem); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void flash_unlock(void) { while (getreg32(STM32L4_FLASH_SR) & FLASH_SR_BSY) @@ -217,14 +207,14 @@ int stm32l4_flash_unlock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_unlock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -233,14 +223,14 @@ int stm32l4_flash_lock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -275,7 +265,7 @@ uint32_t stm32l4_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) DEBUGASSERT((clrbits & FLASH_OPTCR_RDP_MASK) == 0); DEBUGASSERT((setbits & FLASH_OPTCR_RDP_MASK) == 0); - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return 0; @@ -304,7 +294,7 @@ uint32_t stm32l4_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) } flash_optbytes_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return regval; } @@ -365,7 +355,7 @@ ssize_t up_progmem_eraseblock(size_t block) /* Erase single block */ - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -376,7 +366,7 @@ ssize_t up_progmem_eraseblock(size_t block) flash_erase(block); flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -450,7 +440,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) dest = (uint32_t *)((uint8_t *)addr - offset); written = 0; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -577,7 +567,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return (ret == OK) ? written : ret; } diff --git a/arch/arm/src/stm32l4/stm32l4_i2c.c b/arch/arm/src/stm32l4/stm32l4_i2c.c index 293b6eb517d4a..2a35e80a29a40 100644 --- a/arch/arm/src/stm32l4/stm32l4_i2c.c +++ b/arch/arm/src/stm32l4/stm32l4_i2c.c @@ -274,6 +274,7 @@ #include #include #include +#include #include #include #include @@ -431,7 +432,7 @@ struct stm32l4_i2c_priv_s const struct stm32l4_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 @@ -495,9 +496,6 @@ static inline int stm32l4_i2c_sem_waitdone(struct stm32l4_i2c_priv_s *priv); static inline void stm32l4_i2c_sem_waitstop(struct stm32l4_i2c_priv_s *priv); -static inline void stm32l4_i2c_sem_post(struct i2c_master_s *dev); -static inline void stm32l4_i2c_sem_init(struct i2c_master_s *dev); -static inline void stm32l4_i2c_sem_destroy(struct i2c_master_s *dev); #ifdef CONFIG_I2C_TRACE static void stm32l4_i2c_tracereset(struct stm32l4_i2c_priv_s *priv); static void stm32l4_i2c_tracenew(struct stm32l4_i2c_priv_s *priv, @@ -555,6 +553,10 @@ static struct stm32l4_i2c_priv_s stm32l4_i2c1_priv = { .config = &stm32l4_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, @@ -587,6 +589,10 @@ static struct stm32l4_i2c_priv_s stm32l4_i2c2_priv = { .config = &stm32l4_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, @@ -619,6 +625,10 @@ static struct stm32l4_i2c_priv_s stm32l4_i2c3_priv = { .config = &stm32l4_i2c3_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, @@ -651,6 +661,10 @@ static struct stm32l4_i2c_priv_s stm32l4_i2c4_priv = { .config = &stm32l4_i2c4_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, @@ -1069,58 +1083,6 @@ void stm32l4_i2c_sem_waitstop(struct stm32l4_i2c_priv_s *priv) i2cinfo("Timeout with CR: %04" PRIx32 " SR: %04" PRIx32 "\n", cr, sr); } -/**************************************************************************** - * Name: stm32l4_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void stm32l4_i2c_sem_post(struct i2c_master_s *dev) -{ - nxsem_post(&((struct stm32l4_i2c_inst_s *)dev)->priv->sem_excl); -} - -/**************************************************************************** - * Name: stm32l4_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void stm32l4_i2c_sem_init(struct i2c_master_s *dev) -{ - nxsem_init(&((struct stm32l4_i2c_inst_s *)dev)->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(&((struct stm32l4_i2c_inst_s *)dev)->priv->sem_isr, 0, 0); - nxsem_set_protocol(&((struct stm32l4_i2c_inst_s *)dev)->priv->sem_isr, - SEM_PRIO_NONE); -#endif -} - -/**************************************************************************** - * Name: stm32l4_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void stm32l4_i2c_sem_destroy(struct i2c_master_s *dev) -{ - nxsem_destroy(&((struct stm32l4_i2c_inst_s *)dev)->priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&((struct stm32l4_i2c_inst_s *)dev)->priv->sem_isr); -#endif -} - /**************************************************************************** * Name: stm32l4_i2c_trace* * @@ -2703,7 +2665,7 @@ static int stm32l4_i2c_process(struct i2c_master_s *dev, /* Dump the trace result */ stm32l4_i2c_tracedump(priv); - stm32l4_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return -errval; } @@ -2724,7 +2686,7 @@ static int stm32l4_i2c_transfer(struct i2c_master_s *dev, /* Ensure that address or flags don't change meanwhile */ - ret = nxsem_wait(&((struct stm32l4_i2c_inst_s *)dev)->priv->sem_excl); + ret = nxmutex_lock(&((struct stm32l4_i2c_inst_s *)dev)->priv->lock); if (ret >= 0) { ret = stm32l4_i2c_process(dev, msgs, count); @@ -2744,7 +2706,7 @@ static int stm32l4_i2c_transfer(struct i2c_master_s *dev, #ifdef CONFIG_I2C_RESET static int stm32l4_i2c_reset(struct i2c_master_s * dev) { - struct stm32l4_i2c_priv_s * priv; + struct stm32l4_i2c_priv_s *priv; unsigned int clock_count; unsigned int stretch_count; uint32_t scl_gpio; @@ -2764,7 +2726,7 @@ static int stm32l4_i2c_reset(struct i2c_master_s * dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2861,7 +2823,7 @@ static int stm32l4_i2c_reset(struct i2c_master_s * dev) /* Release the port for re-use by other clients */ - stm32l4_i2c_sem_post(dev); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -2902,7 +2864,6 @@ static int stm32l4_i2c_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32l4_i2c_priv_s *priv = (struct stm32l4_i2c_priv_s *)((char *)cb - offsetof(struct stm32l4_i2c_priv_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -2917,13 +2878,7 @@ static int stm32l4_i2c_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for I2C bus is held. */ - if (nxsem_get_value(&priv->sem_excl, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -3014,7 +2969,6 @@ struct i2c_master_s *stm32l4_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - stm32l4_i2c_sem_init((struct i2c_master_s *)inst); stm32l4_i2c_init(priv); #ifdef CONFIG_PM @@ -3072,10 +3026,6 @@ int stm32l4_i2cbus_uninitialize(struct i2c_master_s * dev) stm32l4_i2c_deinit(((struct stm32l4_i2c_inst_s *)dev)->priv); - /* Release unused resources */ - - stm32l4_i2c_sem_destroy((struct i2c_master_s *)dev); - kmm_free(dev); return OK; } diff --git a/arch/arm/src/stm32l4/stm32l4_otgfshost.c b/arch/arm/src/stm32l4/stm32l4_otgfshost.c index b5baa50ce0c66..523085eed09c4 100644 --- a/arch/arm/src/stm32l4/stm32l4_otgfshost.c +++ b/arch/arm/src/stm32l4/stm32l4_otgfshost.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -252,7 +253,7 @@ struct stm32l4_usbhost_s volatile bool connected; /* Connected to device */ volatile bool change; /* Connection change */ volatile bool pscwait; /* True: Thread is waiting for a port event */ - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ sem_t pscsem; /* Semaphore to wait for a port event */ struct stm32l4_ctrlinfo_s ep0; /* Root hub port EP0 description */ @@ -292,12 +293,6 @@ static inline void stm32l4_modifyreg(uint32_t addr, uint32_t clrbits, # define stm32l4_pktdump(m,b,n) #endif -/* Semaphores ***************************************************************/ - -static int stm32l4_takesem(sem_t *sem); -static int stm32l4_takesem_noncancelable(sem_t *sem); -#define stm32l4_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t stm32l4_getle16(const uint8_t *val); @@ -638,54 +633,6 @@ static inline void stm32l4_modifyreg(uint32_t addr, uint32_t clrbits, stm32l4_putreg(addr, (((stm32l4_getreg(addr)) & ~clrbits) | setbits)); } -/**************************************************************************** - * Name: stm32l4_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int stm32l4_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: stm32l4_takesem_noncancelable - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. This version also - * ignores attempts to cancel the thread. - * - ****************************************************************************/ - -static int stm32l4_takesem_noncancelable(sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error is ECANCELED which would occur if the - * calling thread were canceled. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: stm32l4_getle16 * @@ -1190,7 +1137,7 @@ static void stm32l4_chan_wakeup(struct stm32l4_usbhost_s *priv, OTGFS_VTRACE2_CHANWAKEUP_OUT, chan->epno, chan->result); - stm32l4_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -2999,7 +2946,7 @@ static void stm32l4_gint_connected(struct stm32l4_usbhost_s *priv) priv->smstate = SMSTATE_ATTACHED; if (priv->pscwait) { - stm32l4_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3047,7 +2994,7 @@ static void stm32l4_gint_disconnected(struct stm32l4_usbhost_s *priv) if (priv->pscwait) { - stm32l4_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3952,7 +3899,7 @@ static int stm32l4_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = stm32l4_takesem(&priv->pscsem); + ret = nxsem_wait(&priv->pscsem); if (ret < 0) { return ret; @@ -4134,7 +4081,7 @@ static int stm32l4_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32l4_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4158,7 +4105,7 @@ static int stm32l4_ep0configure(struct usbhost_driver_s *drvr, stm32l4_chan_configure(priv, ep0info->inndx); - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4199,7 +4146,7 @@ static int stm32l4_epalloc(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32l4_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4220,7 +4167,7 @@ static int stm32l4_epalloc(struct usbhost_driver_s *drvr, ret = stm32l4_xfrep_alloc(priv, epdesc, ep); } - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4253,7 +4200,7 @@ static int stm32l4_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32l4_takesem_noncancelable(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); /* A single channel is represent by an index in the range of 0 to * STM32L4_MAX_TX_FIFOS. Otherwise, the ep must be a pointer to an @@ -4281,7 +4228,7 @@ static int stm32l4_epfree(struct usbhost_driver_s *drvr, usbhost_ep_t ep) kmm_free(ctrlep); } - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -4523,7 +4470,7 @@ static int stm32l4_ctrlin(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32l4_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4567,7 +4514,7 @@ static int stm32l4_ctrlin(struct usbhost_driver_s *drvr, { /* All success transactions exit here */ - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4582,7 +4529,7 @@ static int stm32l4_ctrlin(struct usbhost_driver_s *drvr, /* All failures exit here after all retries and timeouts are exhausted */ - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4612,7 +4559,7 @@ static int stm32l4_ctrlout(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32l4_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4659,7 +4606,7 @@ static int stm32l4_ctrlout(struct usbhost_driver_s *drvr, { /* All success transactins exit here */ - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -4675,7 +4622,7 @@ static int stm32l4_ctrlout(struct usbhost_driver_s *drvr, /* All failures exit here after all retries and timeouts are exhausted */ - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4733,7 +4680,7 @@ static ssize_t stm32l4_transfer(struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32l4_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return (ssize_t)ret; @@ -4750,7 +4697,7 @@ static ssize_t stm32l4_transfer(struct usbhost_driver_s *drvr, nbytes = stm32l4_out_transfer(priv, chidx, buffer, buflen); } - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nbytes; } @@ -4805,7 +4752,7 @@ static int stm32l4_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, /* We must have exclusive access to the USB host hardware and structures */ - ret = stm32l4_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4822,7 +4769,7 @@ static int stm32l4_asynch(struct usbhost_driver_s *drvr, usbhost_ep_t ep, ret = stm32l4_out_asynch(priv, chidx, buffer, buflen, callback, arg); } - stm32l4_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4881,7 +4828,7 @@ static int stm32l4_cancel(struct usbhost_driver_s *drvr, usbhost_ep_t ep) /* Wake'em up! */ - stm32l4_givesem(&chan->waitsem); + nxsem_post(&chan->waitsem); chan->waiter = false; } @@ -4958,7 +4905,7 @@ static int stm32l4_connect(struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - stm32l4_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); @@ -5316,10 +5263,10 @@ static inline void stm32l4_sw_initialize(struct stm32l4_usbhost_s *priv) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/arm/src/stm32l4/stm32l4_qspi.c b/arch/arm/src/stm32l4/stm32l4_qspi.c index 4ab650fdc5903..4c34344a9bbd2 100644 --- a/arch/arm/src/stm32l4/stm32l4_qspi.c +++ b/arch/arm/src/stm32l4/stm32l4_qspi.c @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -167,7 +167,7 @@ struct stm32l4_qspidev_s uint8_t nbits; /* Width of word in bits (8 to 32) */ uint8_t intf; /* QSPI controller number (0) */ bool initialized; /* TRUE: Controller has been initialized */ - sem_t exclsem; /* Assures mutually exclusive access to QSPI */ + mutex_t lock; /* Assures mutually exclusive access to QSPI */ bool memmap; /* TRUE: Controller is in memory mapped mode */ #ifdef STM32L4_QSPI_INTERRUPTS @@ -1698,11 +1698,11 @@ static int qspi_lock(struct qspi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2499,11 +2499,11 @@ struct qspi_dev_s *stm32l4_qspi_initialize(int intf) { /* Now perform one time initialization */ - /* Initialize the QSPI semaphore that enforces mutually exclusive + /* Initialize the QSPI mutex that enforces mutually exclusive * access to the QSPI registers. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32L4_QSPI_DMA /* Pre-allocate DMA channels. */ @@ -2583,7 +2583,7 @@ struct qspi_dev_s *stm32l4_qspi_initialize(int intf) } #endif - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); return NULL; } diff --git a/arch/arm/src/stm32l4/stm32l4_rng.c b/arch/arm/src/stm32l4/stm32l4_rng.c index 3f602f802793f..8cb3eecd93ca8 100644 --- a/arch/arm/src/stm32l4/stm32l4_rng.c +++ b/arch/arm/src/stm32l4/stm32l4_rng.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -57,7 +58,7 @@ static ssize_t stm32l4_rngread(struct file *filep, char *buffer, size_t); struct rng_dev_s { - sem_t rd_devsem; /* Threads can only exclusively access the RNG */ + mutex_t rd_devlock; /* Threads can only exclusively access the RNG */ sem_t rd_readsem; /* To block until the buffer is filled */ char *rd_buf; size_t rd_buflen; @@ -95,7 +96,7 @@ static int stm32l4_rng_initialize(void) memset(&g_rngdev, 0, sizeof(struct rng_dev_s)); - nxsem_init(&g_rngdev.rd_devsem, 0, 1); + nxmutex_init(&g_rngdev.rd_devlock); if (irq_attach(STM32L4_IRQ_RNG, stm32l4_rnginterrupt, NULL)) { @@ -236,7 +237,7 @@ static ssize_t stm32l4_rngread(struct file *filep, { int ret; - ret = nxsem_wait(&g_rngdev.rd_devsem); + ret = nxmutex_lock(&g_rngdev.rd_devlock); if (ret < 0) { return ret; @@ -268,9 +269,9 @@ static ssize_t stm32l4_rngread(struct file *filep, nxsem_destroy(&g_rngdev.rd_readsem); - /* Free RNG via the device semaphore for next use */ + /* Free RNG via the device mutex for next use */ - nxsem_post(&g_rngdev.rd_devsem); + nxmutex_unlock(&g_rngdev.rd_devlock); return buflen; } diff --git a/arch/arm/src/stm32l4/stm32l4_rtc_lowerhalf.c b/arch/arm/src/stm32l4/stm32l4_rtc_lowerhalf.c index a8c48c67fbfe4..8337fd0bb9248 100644 --- a/arch/arm/src/stm32l4/stm32l4_rtc_lowerhalf.c +++ b/arch/arm/src/stm32l4/stm32l4_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "chip.h" @@ -73,7 +74,7 @@ struct stm32l4_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -232,7 +233,7 @@ static int stm32l4_rdtime(struct rtc_lowerhalf_s *lower, priv = (struct stm32l4_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -244,8 +245,7 @@ static int stm32l4_rdtime(struct rtc_lowerhalf_s *lower, ret = up_rtc_getdatetime((struct tm *)rtctime); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } @@ -273,7 +273,7 @@ static int stm32l4_settime(struct rtc_lowerhalf_s *lower, priv = (struct stm32l4_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -285,8 +285,7 @@ static int stm32l4_settime(struct rtc_lowerhalf_s *lower, ret = stm32l4_rtc_setdatetime((const struct tm *)rtctime); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } @@ -341,7 +340,7 @@ static int stm32l4_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB); priv = (struct stm32l4_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -374,8 +373,7 @@ static int stm32l4_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -483,7 +481,7 @@ static int stm32l4_cancelalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(alarmid == RTC_ALARMA || alarmid == RTC_ALARMB); priv = (struct stm32l4_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -505,8 +503,7 @@ static int stm32l4_cancelalarm(struct rtc_lowerhalf_s *lower, ret = stm32l4_rtc_cancelalarm((enum alm_id_e)alarmid); } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -627,19 +624,17 @@ stm32l4_setperiodic(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL); priv = (struct stm32l4_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } memcpy(&priv->periodic, alarminfo, sizeof(struct lower_setperiodic_s)); - ret = stm32l4_rtc_setperiodic(&alarminfo->period, stm32l4_periodic_callback); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -671,7 +666,7 @@ static int stm32l4_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) DEBUGASSERT(id == 0); - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -679,8 +674,7 @@ static int stm32l4_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) ret = stm32l4_rtc_cancelperiodic(); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -713,8 +707,7 @@ static int stm32l4_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) struct rtc_lowerhalf_s *stm32l4_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/arm/src/stm32l4/stm32l4_sai.c b/arch/arm/src/stm32l4/stm32l4_sai.c index 7f1b22fa7323a..cdb4cf6ef44d4 100644 --- a/arch/arm/src/stm32l4/stm32l4_sai.c +++ b/arch/arm/src/stm32l4/stm32l4_sai.c @@ -55,6 +55,7 @@ #include #include #include +#include #include #include "stm32l4_dma.h" @@ -144,7 +145,7 @@ struct stm32l4_sai_s { struct i2s_dev_s dev; /* Externally visible I2S interface */ uintptr_t base; /* SAI block register base address */ - sem_t exclsem; /* Assures mutually exclusive access to SAI */ + mutex_t lock; /* Assures mutually exclusive access to SAI */ uint32_t frequency; /* SAI clock frequency */ uint32_t syncen; /* Synchronization setting */ #ifdef CONFIG_STM32L4_SAI_DMA @@ -179,14 +180,6 @@ static void sai_dump_regs(struct stm32l4_sai_s *priv, const char *msg); # define sai_dump_regs(s,m) #endif -/* Semaphore helpers */ - -static int sai_exclsem_take(struct stm32l4_sai_s *priv); -#define sai_exclsem_give(priv) nxsem_post(&priv->exclsem) - -static int sai_bufsem_take(struct stm32l4_sai_s *priv); -#define sai_bufsem_give(priv) nxsem_post(&priv->bufsem) - /* Buffer container helpers */ static struct sai_buffer_s * @@ -441,25 +434,6 @@ static void sai_dump_regs(struct stm32l4_sai_s *priv, const char *msg) } #endif -/**************************************************************************** - * Name: sai_exclsem_take - * - * Description: - * Take the exclusive access semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the SAI peripheral state - * - * Returned Value: - * OK on success; a negated errno value on failure - * - ****************************************************************************/ - -static int sai_exclsem_take(struct stm32l4_sai_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - /**************************************************************************** * Name: sai_mckdivider * @@ -980,7 +954,7 @@ static int sai_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the SAI driver data */ - ret = sai_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { sai_buf_free(priv, bfcontainer); @@ -993,7 +967,7 @@ static int sai_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: SAI has no receiver\n"); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } mode = priv->syncen ? SAI_CR1_MODE_SLAVE_RX : SAI_CR1_MODE_MASTER_RX; @@ -1026,11 +1000,11 @@ static int sai_receive(struct i2s_dev_s *dev, struct ap_buffer_s *apb, #endif DEBUGASSERT(ret == OK); leave_critical_section(flags); - sai_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - sai_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); sai_buf_free(priv, bfcontainer); return ret; } @@ -1085,7 +1059,7 @@ static int sai_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, /* Get exclusive access to the SAI driver data */ - ret = sai_exclsem_take(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { sai_buf_free(priv, bfcontainer); @@ -1098,7 +1072,7 @@ static int sai_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, { i2serr("ERROR: SAI has no transmitter\n"); ret = -EAGAIN; - goto errout_with_exclsem; + goto errout_with_excllock; } mode = priv->syncen ? SAI_CR1_MODE_SLAVE_TX : SAI_CR1_MODE_MASTER_TX; @@ -1131,34 +1105,15 @@ static int sai_send(struct i2s_dev_s *dev, struct ap_buffer_s *apb, #endif DEBUGASSERT(ret == OK); leave_critical_section(flags); - sai_exclsem_give(priv); + nxmutex_unlock(&priv->lock); return OK; -errout_with_exclsem: - sai_exclsem_give(priv); +errout_with_excllock: + nxmutex_unlock(&priv->lock); sai_buf_free(priv, bfcontainer); return ret; } -/**************************************************************************** - * Name: sai_bufsem_take - * - * Description: - * Take the buffer semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the SAI peripheral state - * - * Returned Value: - * OK on success; a negated errno value on failure - * - ****************************************************************************/ - -static int sai_bufsem_take(struct stm32l4_sai_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->bufsem); -} - /**************************************************************************** * Name: sai_buf_allocate * @@ -1189,7 +1144,7 @@ static struct sai_buffer_s *sai_buf_allocate(struct stm32l4_sai_s *priv) * have at least one free buffer container. */ - ret = sai_bufsem_take(priv); + ret = nxsem_wait_uninterruptible(&priv->bufsem); if (ret < 0) { return ret; @@ -1240,7 +1195,7 @@ static void sai_buf_free(struct stm32l4_sai_s *priv, /* Wake up any threads waiting for a buffer container */ - sai_bufsem_give(priv); + nxsem_post(&priv->bufsem); } /**************************************************************************** @@ -1293,7 +1248,7 @@ static void sai_portinitialize(struct stm32l4_sai_s *priv) { sai_dump_regs(priv, "Before initialization"); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Initialize buffering */ diff --git a/arch/arm/src/stm32l4/stm32l4_sdmmc.c b/arch/arm/src/stm32l4/stm32l4_sdmmc.c index 3a906fc513def..7f8336a9d3236 100644 --- a/arch/arm/src/stm32l4/stm32l4_sdmmc.c +++ b/arch/arm/src/stm32l4/stm32l4_sdmmc.c @@ -390,8 +390,6 @@ struct stm32_sampleregs_s static inline void sdmmc_putreg32(struct stm32_dev_s *priv, uint32_t value, int offset); static inline uint32_t sdmmc_getreg32(struct stm32_dev_s *priv, int offset); -static int stm32_takesem(struct stm32_dev_s *priv); -#define stm32_givesem(priv) (nxsem_post(&priv->waitsem)) static inline void stm32_setclkcr(struct stm32_dev_s *priv, uint32_t clkcr); static void stm32_configwaitints(struct stm32_dev_s *priv, uint32_t waitmask, sdio_eventset_t waitevents, sdio_eventset_t wkupevents); @@ -673,27 +671,6 @@ static inline void sdmmc_modifyreg32(struct stm32_dev_s *priv, int offset, leave_critical_section(flags); } -/**************************************************************************** - * Name: stm32_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * priv - Instance of the SDMMC private state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int stm32_takesem(struct stm32_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: stm32_setclkcr * @@ -1361,7 +1338,7 @@ static void stm32_endwait(struct stm32_dev_s *priv, /* Wake up the waiting thread */ - stm32_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2643,7 +2620,7 @@ static sdio_eventset_t stm32_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = stm32_takesem(priv); + ret = nxsem_wait_uninterruptible(priv); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/arm/src/stm32l4/stm32l4_spi.c b/arch/arm/src/stm32l4/stm32l4_spi.c index cf0f2a21b13e6..bae7443b322e4 100644 --- a/arch/arm/src/stm32l4/stm32l4_spi.c +++ b/arch/arm/src/stm32l4/stm32l4_spi.c @@ -65,7 +65,7 @@ #include #include -#include +#include #include #include @@ -155,7 +155,7 @@ struct stm32l4_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 through 16) */ @@ -942,11 +942,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1653,7 +1653,6 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, { struct stm32l4_spidev_s *priv = (struct stm32l4_spidev_s *)((char *)cb - offsetof(struct stm32l4_spidev_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -1668,13 +1667,7 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for SPI bus is held. */ - if (nxsem_get_value(&priv->exclsem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -1752,9 +1745,9 @@ static void spi_bus_initialize(struct stm32l4_spidev_s *priv) spi_putreg(priv, STM32L4_SPI_CRCPR_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32L4_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion */ diff --git a/arch/arm/src/stm32l4/stm32l4x6xx_dma.c b/arch/arm/src/stm32l4/stm32l4x6xx_dma.c index 599c048d70e01..6843de6a78ce3 100644 --- a/arch/arm/src/stm32l4/stm32l4x6xx_dma.c +++ b/arch/arm/src/stm32l4/stm32l4x6xx_dma.c @@ -196,24 +196,6 @@ static inline void dmachan_putreg(struct stm32l4_dma_s *dmach, putreg32(value, dmach->base + offset); } -/**************************************************************************** - * Name: stm32l4_dmatake() and stm32l4_dmagive() - * - * Description: - * Used to get exclusive access to a DMA channel. - * - ****************************************************************************/ - -static int stm32l4_dmatake(struct stm32l4_dma_s *dmach) -{ - return nxsem_wait_uninterruptible(&dmach->sem); -} - -static inline void stm32l4_dmagive(struct stm32l4_dma_s *dmach) -{ - nxsem_post(&dmach->sem); -} - /**************************************************************************** * Name: stm32l4_dmachandisable * @@ -391,7 +373,7 @@ DMA_HANDLE stm32l4_dmachannel(unsigned int chndef) * is available if it is currently being used by another driver */ - ret = stm32l4_dmatake(dmach); + ret = nxsem_wait_uninterruptible(&dmach->sem); if (ret < 0) { return NULL; @@ -436,7 +418,7 @@ void stm32l4_dmafree(DMA_HANDLE handle) /* Release the channel */ - stm32l4_dmagive(dmach); + nxsem_post(&dmach->sem); } /**************************************************************************** diff --git a/arch/arm/src/stm32l5/stm32l5_flash.c b/arch/arm/src/stm32l5/stm32l5_flash.c index bff483c30c215..634c6ad1d6dce 100644 --- a/arch/arm/src/stm32l5/stm32l5_flash.c +++ b/arch/arm/src/stm32l5/stm32l5_flash.c @@ -40,6 +40,7 @@ #include #include #include +#include #include "stm32l5_rcc.h" #include "stm32l5_waste.h" @@ -96,37 +97,13 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; static uint32_t g_page_buffer[FLASH_PAGE_WORDS]; /**************************************************************************** * Private Functions ****************************************************************************/ -static inline void sem_lock(void) -{ - int ret; - - do - { - /* Take the semaphore (perhaps waiting) */ - - ret = nxsem_wait(&g_sem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void flash_unlock(void) { while (getreg32(STM32L5_FLASH_NSSR) & FLASH_SR_BSY) @@ -192,16 +169,16 @@ static inline void flash_erase(size_t page) void stm32l5_flash_unlock(void) { - sem_lock(); + nxmutex_lock(&g_lock); flash_unlock(); - sem_unlock(); + nxmutex_unlock(&g_lock); } void stm32l5_flash_lock(void) { - sem_lock(); + nxmutex_lock(&g_lock); flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); } /**************************************************************************** @@ -233,7 +210,7 @@ uint32_t stm32l5_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) DEBUGASSERT((clrbits & FLASH_OPTR_RDP_MASK) == 0); DEBUGASSERT((setbits & FLASH_OPTR_RDP_MASK) == 0); - sem_lock(); + nxmutex_lock(&g_lock); flash_optbytes_unlock(); /* Modify Option Bytes in register. */ @@ -257,7 +234,7 @@ uint32_t stm32l5_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) } flash_optbytes_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return regval; } @@ -316,13 +293,13 @@ ssize_t up_progmem_eraseblock(size_t block) /* Erase single block */ - sem_lock(); + nxmutex_lock(&g_lock); flash_unlock(); flash_erase(block); flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -395,7 +372,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) dest = (uint32_t *)((uint8_t *)addr - offset); written = 0; - sem_lock(); + nxmutex_lock(&g_lock); /* Get flash ready and begin flashing. */ @@ -502,7 +479,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return (ret == OK) ? written : ret; } diff --git a/arch/arm/src/stm32l5/stm32l5_spi.c b/arch/arm/src/stm32l5/stm32l5_spi.c index b59cb8b5616be..4c2b75a0fdae5 100644 --- a/arch/arm/src/stm32l5/stm32l5_spi.c +++ b/arch/arm/src/stm32l5/stm32l5_spi.c @@ -66,7 +66,7 @@ #include #include -#include +#include #include #include @@ -158,7 +158,7 @@ struct stm32l5_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 through 16) */ @@ -937,23 +937,13 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - /* Take the semaphore (perhaps waiting) */ + /* Take the mutex (perhaps waiting) */ - do - { - ret = nxsem_wait(&priv->exclsem); - - /* The only case that an error should occur here is if the wait - * was awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); + ret = nxmutex_lock(&priv->lock); } else { - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); ret = OK; } @@ -1656,7 +1646,6 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32l5_spidev_s *priv = (struct stm32l5_spidev_s *)((char *)cb - offsetof(struct stm32l5_spidev_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -1671,13 +1660,7 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for SPI bus is held. */ - if (nxsem_getvalue(&priv->exclsem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM states. */ @@ -1754,9 +1737,9 @@ static void spi_bus_initialize(struct stm32l5_spidev_s *priv) spi_putreg(priv, STM32L5_SPI_CRCPR_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32L5_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion */ diff --git a/arch/arm/src/stm32u5/stm32_flash.c b/arch/arm/src/stm32u5/stm32_flash.c index f444176e14c4b..a597e23992e5d 100644 --- a/arch/arm/src/stm32u5/stm32_flash.c +++ b/arch/arm/src/stm32u5/stm32_flash.c @@ -40,6 +40,7 @@ #include #include #include +#include #include "stm32_rcc.h" #include "stm32_waste.h" @@ -96,37 +97,13 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; static uint32_t g_page_buffer[FLASH_PAGE_WORDS]; /**************************************************************************** * Private Functions ****************************************************************************/ -static inline void sem_lock(void) -{ - int ret; - - do - { - /* Take the semaphore (perhaps waiting) */ - - ret = nxsem_wait(&g_sem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void flash_unlock(void) { while (getreg32(STM32_FLASH_NSSR) & FLASH_NSSR_BSY) @@ -192,16 +169,16 @@ static inline void flash_erase(size_t page) void stm32_flash_unlock(void) { - sem_lock(); + nxmutex_lock(&g_lock); flash_unlock(); - sem_unlock(); + nxmutex_unlock(&g_lock); } void stm32_flash_lock(void) { - sem_lock(); + nxmutex_lock(&g_lock); flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); } /**************************************************************************** @@ -233,7 +210,7 @@ uint32_t stm32_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) DEBUGASSERT((clrbits & FLASH_OPTR_RDP_MASK) == 0); DEBUGASSERT((setbits & FLASH_OPTR_RDP_MASK) == 0); - sem_lock(); + nxmutex_lock(&g_lock); flash_optbytes_unlock(); /* Modify Option Bytes in register. */ @@ -257,7 +234,7 @@ uint32_t stm32_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) } flash_optbytes_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return regval; } @@ -316,13 +293,13 @@ ssize_t up_progmem_eraseblock(size_t block) /* Erase single block */ - sem_lock(); + nxmutex_lock(&g_lock); flash_unlock(); flash_erase(block); flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -395,7 +372,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) dest = (uint32_t *)((uint8_t *)addr - offset); written = 0; - sem_lock(); + nxmutex_lock(&g_lock); /* Get flash ready and begin flashing. */ @@ -502,7 +479,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return (ret == OK) ? written : ret; } diff --git a/arch/arm/src/stm32u5/stm32_spi.c b/arch/arm/src/stm32u5/stm32_spi.c index 6510bfd656a84..400305b7d6d78 100644 --- a/arch/arm/src/stm32u5/stm32_spi.c +++ b/arch/arm/src/stm32u5/stm32_spi.c @@ -60,7 +60,7 @@ #include #include #include -#include +#include #include #include @@ -208,7 +208,7 @@ struct stm32_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ int8_t nbits; /* Width of word in bits */ @@ -1160,11 +1160,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2120,7 +2120,6 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, struct stm32_spidev_s *priv = (struct stm32_spidev_s *)((char *)cb - offsetof(struct stm32_spidev_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -2135,13 +2134,7 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for SPI bus is held. */ - if (nxsem_get_value(&priv->exclsem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -2246,9 +2239,9 @@ static void spi_bus_initialize(struct stm32_spidev_s *priv) spi_putreg(priv, STM32_SPI_CRCPOLY_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access. */ + /* Initialize the SPI mutex that enforces mutually exclusive access. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32U5_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion. diff --git a/arch/arm/src/stm32wb/stm32wb_blehci.c b/arch/arm/src/stm32wb/stm32wb_blehci.c index c991554d76db4..74b3483bd0a5f 100644 --- a/arch/arm/src/stm32wb/stm32wb_blehci.c +++ b/arch/arm/src/stm32wb/stm32wb_blehci.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #if defined(CONFIG_UART_BTH4) @@ -127,7 +128,7 @@ static struct bt_driver_s g_blehci_driver = .send = stm32wb_blehci_driversend }; -static sem_t g_excl_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; struct work_s g_drv_init_work; /**************************************************************************** @@ -170,7 +171,7 @@ static int stm32wb_blehci_driversend(struct bt_driver_s *btdev, /* Ensure non-concurrent access */ - ret = nxsem_wait_uninterruptible(&g_excl_sem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -185,7 +186,7 @@ static int stm32wb_blehci_driversend(struct bt_driver_s *btdev, ret = stm32wb_mbox_bleacl(data, len); } - nxsem_post(&g_excl_sem); + nxmutex_unlock(&g_lock); } return ret < 0 ? ret : (int)len; diff --git a/arch/arm/src/stm32wb/stm32wb_flash.c b/arch/arm/src/stm32wb/stm32wb_flash.c index 80f74534e4aff..99cfb0fcc9f69 100644 --- a/arch/arm/src/stm32wb/stm32wb_flash.c +++ b/arch/arm/src/stm32wb/stm32wb_flash.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include @@ -83,23 +83,13 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; static uint32_t g_page_buffer[FLASH_PAGE_WORDS]; /**************************************************************************** * Private Functions ****************************************************************************/ -static inline int sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_sem); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void flash_unlock(void) { while (getreg32(STM32WB_FLASH_SR) & FLASH_SR_BSY) @@ -168,14 +158,14 @@ int stm32wb_flash_unlock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_unlock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -184,14 +174,14 @@ int stm32wb_flash_lock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -226,7 +216,7 @@ uint32_t stm32wb_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) DEBUGASSERT((clrbits & FLASH_OPTR_RDP_MASK) == 0); DEBUGASSERT((setbits & FLASH_OPTR_RDP_MASK) == 0); - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return 0; @@ -255,7 +245,7 @@ uint32_t stm32wb_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) } flash_optbytes_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return regval; } @@ -316,7 +306,7 @@ ssize_t up_progmem_eraseblock(size_t block) /* Erase single block */ - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -327,7 +317,7 @@ ssize_t up_progmem_eraseblock(size_t block) flash_erase(block); flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -400,7 +390,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) dest = (uint32_t *)((uint8_t *)addr - offset); written = 0; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -511,7 +501,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return (ret == OK) ? written : ret; } diff --git a/arch/arm/src/stm32wb/stm32wb_rtc_lowerhalf.c b/arch/arm/src/stm32wb/stm32wb_rtc_lowerhalf.c index e7c5ee3d961db..3f346a5b9b69b 100644 --- a/arch/arm/src/stm32wb/stm32wb_rtc_lowerhalf.c +++ b/arch/arm/src/stm32wb/stm32wb_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "chip.h" @@ -73,7 +74,7 @@ struct stm32wb_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -231,7 +232,7 @@ static int stm32wb_rdtime(struct rtc_lowerhalf_s *lower, priv = (struct stm32wb_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -243,8 +244,7 @@ static int stm32wb_rdtime(struct rtc_lowerhalf_s *lower, ret = up_rtc_getdatetime((struct tm *)rtctime); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } @@ -272,7 +272,7 @@ static int stm32wb_settime(struct rtc_lowerhalf_s *lower, priv = (struct stm32wb_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -284,8 +284,7 @@ static int stm32wb_settime(struct rtc_lowerhalf_s *lower, ret = stm32wb_rtc_setdatetime((const struct tm *)rtctime); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } @@ -340,7 +339,7 @@ static int stm32wb_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(alarminfo->id == RTC_ALARMA || alarminfo->id == RTC_ALARMB); priv = (struct stm32wb_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -373,8 +372,7 @@ static int stm32wb_setalarm(struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -481,7 +479,7 @@ static int stm32wb_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid) DEBUGASSERT(alarmid == RTC_ALARMA || alarmid == RTC_ALARMB); priv = (struct stm32wb_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -503,8 +501,7 @@ static int stm32wb_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid) ret = stm32wb_rtc_cancelalarm((enum alm_id_e)alarmid); } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -625,19 +622,17 @@ stm32wb_setperiodic(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL); priv = (struct stm32wb_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } memcpy(&priv->periodic, alarminfo, sizeof(struct lower_setperiodic_s)); - ret = stm32wb_rtc_setperiodic(&alarminfo->period, stm32wb_periodic_callback); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -669,7 +664,7 @@ static int stm32wb_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) DEBUGASSERT(id == 0); - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -677,8 +672,7 @@ static int stm32wb_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) ret = stm32wb_rtc_cancelperiodic(); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -711,8 +705,7 @@ static int stm32wb_cancelperiodic(struct rtc_lowerhalf_s *lower, int id) struct rtc_lowerhalf_s *stm32wb_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/arm/src/stm32wb/stm32wb_spi.c b/arch/arm/src/stm32wb/stm32wb_spi.c index 6b9c5674597c1..0cbbe39ed8f2a 100644 --- a/arch/arm/src/stm32wb/stm32wb_spi.c +++ b/arch/arm/src/stm32wb/stm32wb_spi.c @@ -65,7 +65,7 @@ #include #include -#include +#include #include #include @@ -154,7 +154,7 @@ struct stm32wb_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 through 16) */ @@ -883,11 +883,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1590,7 +1590,6 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, { struct stm32wb_spidev_s *priv = (struct stm32wb_spidev_s *)((char *)cb - offsetof(struct stm32wb_spidev_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -1605,13 +1604,7 @@ static int spi_pm_prepare(struct pm_callback_s *cb, int domain, /* Check if exclusive lock for SPI bus is held. */ - if (nxsem_get_value(&priv->exclsem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxmutex_is_locked(&priv->lock)) { /* Exclusive lock is held, do not allow entry to deeper PM * states. @@ -1687,9 +1680,9 @@ static void spi_bus_initialize(struct stm32wb_spidev_s *priv) spi_putreg(priv, STM32WB_SPI_CRCPR_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32WB_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion */ diff --git a/arch/arm/src/stm32wl5/stm32wl5_flash.c b/arch/arm/src/stm32wl5/stm32wl5_flash.c index 7fff2d309ce09..f2bc151b00bbc 100644 --- a/arch/arm/src/stm32wl5/stm32wl5_flash.c +++ b/arch/arm/src/stm32wl5/stm32wl5_flash.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include @@ -85,23 +85,13 @@ * Private Data ****************************************************************************/ -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; static uint32_t g_page_buffer[FLASH_PAGE_WORDS]; /**************************************************************************** * Private Functions ****************************************************************************/ -static inline int sem_lock(void) -{ - return nxsem_wait_uninterruptible(&g_sem); -} - -static inline void sem_unlock(void) -{ - nxsem_post(&g_sem); -} - static void flash_unlock(void) { while (getreg32(STM32WL5_FLASH_SR) & FLASH_SR_BSY) @@ -170,14 +160,14 @@ int stm32wl5_flash_unlock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_unlock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -186,14 +176,14 @@ int stm32wl5_flash_lock(void) { int ret; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return ret; } @@ -228,7 +218,7 @@ uint32_t stm32wl5_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) DEBUGASSERT((clrbits & FLASH_OPTR_RDP_MASK) == 0); DEBUGASSERT((setbits & FLASH_OPTR_RDP_MASK) == 0); - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return 0; @@ -257,7 +247,7 @@ uint32_t stm32wl5_flash_user_optbytes(uint32_t clrbits, uint32_t setbits) } flash_optbytes_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return regval; } @@ -318,7 +308,7 @@ ssize_t up_progmem_eraseblock(size_t block) /* Erase single block */ - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -329,7 +319,7 @@ ssize_t up_progmem_eraseblock(size_t block) flash_erase(block); flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); /* Verify */ @@ -403,7 +393,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) dest = (uint32_t *)((uint8_t *)addr - offset); written = 0; - ret = sem_lock(); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -519,7 +509,7 @@ ssize_t up_progmem_write(size_t addr, const void *buf, size_t buflen) } flash_lock(); - sem_unlock(); + nxmutex_unlock(&g_lock); return (ret == OK) ? written : ret; } diff --git a/arch/arm/src/stm32wl5/stm32wl5_spi.c b/arch/arm/src/stm32wl5/stm32wl5_spi.c index a338678d57403..478789db5b524 100644 --- a/arch/arm/src/stm32wl5/stm32wl5_spi.c +++ b/arch/arm/src/stm32wl5/stm32wl5_spi.c @@ -58,7 +58,7 @@ #include #include -#include +#include #include #include @@ -181,7 +181,7 @@ struct stm32wl5_spidev_s uint32_t rxccr; /* DMA control register for RX transfers */ #endif bool initialized; /* Has SPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (4 through 16) */ @@ -935,11 +935,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1728,9 +1728,9 @@ static void spi_bus_initialize(struct stm32wl5_spidev_s *priv) spi_putreg(priv, STM32WL5_SPI_CRCPR_OFFSET, 7); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_STM32WL5_SPI_DMA /* Initialize the SPI semaphores that is used to wait for DMA completion. diff --git a/arch/arm/src/tiva/common/tiva_adclow.c b/arch/arm/src/tiva/common/tiva_adclow.c index 232b697e4c62c..453999ffb8dd9 100644 --- a/arch/arm/src/tiva/common/tiva_adclow.c +++ b/arch/arm/src/tiva/common/tiva_adclow.c @@ -76,7 +76,7 @@ #include #include #include -#include +#include #include @@ -128,8 +128,6 @@ /* ADC support definitions **************************************************/ #define SSE_PROC_TRIG(n) (1 << (n)) -#define SEM_PROCESS_PRIVATE 0 -#define SEM_PROCESS_SHARED 1 /**************************************************************************** * Public Functions @@ -177,7 +175,7 @@ struct tiva_adc_s struct tiva_adc_sse_s { - sem_t exclsem; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ struct work_s work; /* Supports the interrupt handling "bottom half" */ bool cfg; /* Configuration state */ bool ena; /* Sample sequencer operation state */ @@ -814,7 +812,7 @@ static struct tiva_adc_s *tiva_adc_struct_init(struct tiva_adc_cfg_s *cfg) { sse->adc = cfg->adc; sse->num = s; - nxsem_init(&sse->exclsem, SEM_PROCESS_PRIVATE, 1); + nxmutex_init(&sse->lock); sse->ena = false; sse->cfg = true; } @@ -961,7 +959,7 @@ int tiva_adc_lock(struct tiva_adc_s *priv, int sse) struct tiva_adc_sse_s *s = g_sses[SSE_IDX(priv->devno, sse)]; ainfo("Locking...\n"); - return nxsem_wait_uninterruptible(&s->exclsem); + return nxmutex_lock(&s->lock); } /**************************************************************************** @@ -976,7 +974,7 @@ void tiva_adc_unlock(struct tiva_adc_s *priv, int sse) { struct tiva_adc_sse_s *s = g_sses[SSE_IDX(priv->devno, sse)]; ainfo("Unlocking\n"); - nxsem_post(&s->exclsem); + nxmutex_unlock(&s->lock); } #ifdef CONFIG_DEBUG_ANALOG diff --git a/arch/arm/src/tiva/common/tiva_can.c b/arch/arm/src/tiva/common/tiva_can.c index ed09af18e7800..4348142d1be0a 100644 --- a/arch/arm/src/tiva/common/tiva_can.c +++ b/arch/arm/src/tiva/common/tiva_can.c @@ -155,7 +155,7 @@ struct tiva_canmod_s /* Mutex for threads accessing the interface registers */ - mutex_t thd_iface_mtx; + mutex_t thd_iface_lock; /* Interface registers base address for threads threads */ @@ -169,7 +169,7 @@ struct tiva_canmod_s * The TX FIFO should never be resized at runtime. */ - mutex_t fifo_mtx; + mutex_t fifo_lock; /* All RX FIFOs + 1 TX FIFO */ @@ -479,7 +479,7 @@ static int tivacan_setup(struct can_dev_s *dev) tivacan_ioctl(dev, CANIOC_SET_BITTIMING, (unsigned long)&default_timing); - nxmutex_lock(&canmod->thd_iface_mtx); + nxmutex_lock(&canmod->thd_iface_lock); /* Ensure a consistent state */ @@ -542,7 +542,7 @@ static int tivacan_setup(struct can_dev_s *dev) & TIVA_CANIF_CRQ_BUSY); } - nxmutex_unlock(&canmod->thd_iface_mtx); + nxmutex_unlock(&canmod->thd_iface_lock); /* Register the ISR */ @@ -735,7 +735,7 @@ int tivacan_rxhandler(int argc, char** argv) while (true) { nxsem_wait(&canmod->rxsem); - nxmutex_lock(&canmod->thd_iface_mtx); + nxmutex_lock(&canmod->thd_iface_lock); /* Process the received message(s). Since hardware RX FIFOS are used * and new messages are received into the mailbox with the lowest @@ -847,7 +847,7 @@ int tivacan_rxhandler(int argc, char** argv) #endif } - nxmutex_unlock(&canmod->thd_iface_mtx); + nxmutex_unlock(&canmod->thd_iface_lock); #ifdef CONFIG_CAN_ERRORS if (ret == OK) @@ -1199,7 +1199,7 @@ static int tivacan_send(struct can_dev_s *dev, struct can_msg_s *msg) return -EBUSY; } - nxmutex_lock(&canmod->thd_iface_mtx); + nxmutex_lock(&canmod->thd_iface_lock); /* Protect the message object due the minute chance that the mailbox was * previously used for a remote frame and could receive messages, causing @@ -1291,7 +1291,7 @@ static int tivacan_send(struct can_dev_s *dev, struct can_msg_s *msg) while (getreg32(canmod->thd_iface_base + TIVA_CANIF_OFFSET_CRQ) & TIVA_CANIF_CRQ_BUSY); - nxmutex_unlock(&canmod->thd_iface_mtx); + nxmutex_unlock(&canmod->thd_iface_lock); /* Move to the next message in the h/w TX FIFO. * Tell the upper-half the message has been submitted... this recurses @@ -1972,7 +1972,7 @@ int tivacan_alloc_fifo(struct can_dev_s *dev, int depth) int free_fifo_idx = -1; struct tiva_canmod_s *canmod = dev->cd_priv; - nxmutex_lock(&canmod->fifo_mtx); + nxmutex_lock(&canmod->fifo_lock); /* Mailboxes allocated other RX FIFOs or the TX FIFO */ @@ -2007,7 +2007,7 @@ int tivacan_alloc_fifo(struct can_dev_s *dev, int depth) if (numclaimed != depth) { - nxmutex_unlock(&canmod->fifo_mtx); + nxmutex_unlock(&canmod->fifo_lock); return -ENOSPC; } else @@ -2017,7 +2017,7 @@ int tivacan_alloc_fifo(struct can_dev_s *dev, int depth) claimed &= 0xffffffff >> (32 - i); canmod->fifos[free_fifo_idx] = claimed; - nxmutex_unlock(&canmod->fifo_mtx); + nxmutex_unlock(&canmod->fifo_lock); return free_fifo_idx; } } @@ -2044,7 +2044,7 @@ static void tivacan_free_fifo(struct can_dev_s *dev, tiva_can_fifo_t *fifo) { struct tiva_canmod_s * canmod = dev->cd_priv; - nxmutex_lock(&canmod->thd_iface_mtx); + nxmutex_lock(&canmod->thd_iface_lock); for (int i = 0; i < TIVA_CAN_NUM_MBOXES; ++i) { @@ -2056,7 +2056,7 @@ static void tivacan_free_fifo(struct can_dev_s *dev, } } - nxmutex_unlock(&canmod->thd_iface_mtx); + nxmutex_unlock(&canmod->thd_iface_lock); } /**************************************************************************** @@ -2321,8 +2321,7 @@ static int tivacan_initfilter(struct can_dev_s *dev, } } - nxmutex_unlock(&canmod->thd_iface_mtx); - + nxmutex_unlock(&canmod->thd_iface_lock); return OK; } @@ -2380,14 +2379,14 @@ int tiva_can_initialize(char *devpath, int modnum) /* Initialize concurrancy objects for accessing interfaces */ - ret = nxmutex_init(&canmod->thd_iface_mtx); + ret = nxmutex_init(&canmod->thd_iface_lock); if (ret < 0) { canerr("ERROR: failed to initialize mutex: %d\n", ret); return ret; } - ret = nxmutex_init(&canmod->fifo_mtx); + ret = nxmutex_init(&canmod->fifo_lock); if (ret < 0) { canerr("ERROR: failed to initialize mutex: %d\n", ret); diff --git a/arch/arm/src/tiva/common/tiva_i2c.c b/arch/arm/src/tiva/common/tiva_i2c.c index ddbb7a3e8995d..2e719739a64f5 100644 --- a/arch/arm/src/tiva/common/tiva_i2c.c +++ b/arch/arm/src/tiva/common/tiva_i2c.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -194,7 +195,7 @@ struct tiva_i2c_priv_s /* Port configuration */ const struct tiva_i2c_config_s *config; - sem_t exclsem; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ #ifndef CONFIG_I2C_POLLED sem_t waitsem; /* Interrupt wait semaphore */ #endif @@ -256,9 +257,6 @@ static uint32_t tiva_i2c_toticks(int msgc, struct i2c_msg_s *msgv); #endif /* CONFIG_TIVA_I2C_DYNTIMEO */ static inline int tiva_i2c_sem_waitdone(struct tiva_i2c_priv_s *priv); -static inline void tiva_i2c_sem_post(struct tiva_i2c_priv_s *priv); -static inline void tiva_i2c_sem_init(struct tiva_i2c_priv_s *priv); -static inline void tiva_i2c_sem_destroy(struct tiva_i2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void tiva_i2c_tracereset(struct tiva_i2c_priv_s *priv); @@ -270,7 +268,7 @@ static void tiva_i2c_tracedump(struct tiva_i2c_priv_s *priv); static void tiva_i2c_startxfr(struct tiva_i2c_priv_s *priv); static void tiva_i2c_nextxfr(struct tiva_i2c_priv_s *priv, uint32_t cmd); -static int tiva_i2c_process(struct tiva_i2c_priv_s * priv, uint32_t status); +static int tiva_i2c_process(struct tiva_i2c_priv_s *priv, uint32_t status); #ifndef CONFIG_I2C_POLLED static int tiva_i2c_interrupt(int irq, void *context, void *arg); @@ -320,7 +318,13 @@ static const struct tiva_i2c_config_s tiva_i2c0_config = .devno = 0, }; -static struct tiva_i2c_priv_s tiva_i2c0_priv; +static struct tiva_i2c_priv_s tiva_i2c0_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C1 @@ -341,7 +345,13 @@ static const struct tiva_i2c_config_s tiva_i2c1_config = .devno = 1, }; -static struct tiva_i2c_priv_s tiva_i2c1_priv; +static struct tiva_i2c_priv_s tiva_i2c1_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C2 @@ -362,7 +372,13 @@ static const struct tiva_i2c_config_s tiva_i2c2_config = .devno = 2, }; -static struct tiva_i2c_priv_s tiva_i2c2_priv; +static struct tiva_i2c_priv_s tiva_i2c2_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C3 @@ -383,7 +399,13 @@ static const struct tiva_i2c_config_s tiva_i2c3_config = .devno = 3, }; -static struct tiva_i2c_priv_s tiva_i2c3_priv; +static struct tiva_i2c_priv_s tiva_i2c3_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C4 @@ -404,7 +426,13 @@ static const struct tiva_i2c_config_s tiva_i2c4_config = .devno = 4, }; -static struct tiva_i2c_priv_s tiva_i2c4_priv; +static struct tiva_i2c_priv_s tiva_i2c4_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C5 @@ -425,7 +453,13 @@ static const struct tiva_i2c_config_s tiva_i2c5_config = .devno = 5, }; -static struct tiva_i2c_priv_s tiva_i2c5_priv; +static struct tiva_i2c_priv_s tiva_i2c5_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C6 @@ -446,7 +480,13 @@ static const struct tiva_i2c_config_s tiva_i2c6_config = .devno = 6, }; -static struct tiva_i2c_priv_s tiva_i2c6_priv; +static struct tiva_i2c_priv_s tiva_i2c6_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C7 @@ -467,7 +507,13 @@ static const struct tiva_i2c_config_s tiva_i2c7_config = .devno = 7, }; -static struct tiva_i2c_priv_s tiva_i2c7_priv; +static struct tiva_i2c_priv_s tiva_i2c7_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C8 @@ -488,7 +534,13 @@ static const struct tiva_i2c_config_s tiva_i2c8_config = .devno = 8, }; -static struct tiva_i2c_priv_s tiva_i2c8_priv; +static struct tiva_i2c_priv_s tiva_i2c8_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif #ifdef CONFIG_TIVA_I2C9 @@ -509,7 +561,13 @@ static const struct tiva_i2c_config_s tiva_i2c9_config = .devno = 9, }; -static struct tiva_i2c_priv_s tiva_i2c9_priv; +static struct tiva_i2c_priv_s tiva_i2c9_priv = +{ + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .waitsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif +}; #endif /**************************************************************************** @@ -783,57 +841,6 @@ static inline int tiva_i2c_sem_waitdone(struct tiva_i2c_priv_s *priv) } #endif -/**************************************************************************** - * Name: tiva_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void tiva_i2c_sem_post(struct tiva_i2c_priv_s *priv) -{ - nxsem_post(&priv->exclsem); -} - -/**************************************************************************** - * Name: tiva_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void tiva_i2c_sem_init(struct tiva_i2c_priv_s *priv) -{ - nxsem_init(&priv->exclsem, 0, 1); - -#ifndef CONFIG_I2C_POLLED - /* This semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ - - nxsem_init(&priv->waitsem, 0, 0); - nxsem_set_protocol(&priv->waitsem, SEM_PRIO_NONE); -#endif -} - -/**************************************************************************** - * Name: tiva_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void tiva_i2c_sem_destroy(struct tiva_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->exclsem); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->waitsem); -#endif -} - /**************************************************************************** * Name: tiva_i2c_trace * @@ -1589,7 +1596,7 @@ static int tiva_i2c_transfer(struct i2c_master_s *dev, DEBUGASSERT(priv && priv->config && msgv && msgc > 0); i2cinfo("I2C%d: msgc=%d\n", priv->config->devno, msgc); - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1717,7 +1724,7 @@ static int tiva_i2c_transfer(struct i2c_master_s *dev, priv->mcnt = 0; priv->mptr = NULL; - tiva_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -1754,7 +1761,7 @@ static int tiva_i2c_reset(struct i2c_master_s * dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1844,7 +1851,7 @@ static int tiva_i2c_reset(struct i2c_master_s * dev) /* Release the port for re-use by other clients */ - tiva_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_I2C_RESET */ @@ -1964,7 +1971,6 @@ struct i2c_master_s *tiva_i2cbus_initialize(int port) /* Initialize the device structure */ priv->config = config; - tiva_i2c_sem_init(priv); /* Initialize the I2C hardware */ @@ -2004,10 +2010,6 @@ int tiva_i2cbus_uninitialize(struct i2c_master_s *dev) tiva_i2c_uninitialize(priv); priv->refs = 0; - - /* Release unused resources */ - - tiva_i2c_sem_destroy(priv); } else { diff --git a/arch/arm/src/tiva/common/tiva_ssi.c b/arch/arm/src/tiva/common/tiva_ssi.c index bc4346136a02e..dc4e1527f225c 100644 --- a/arch/arm/src/tiva/common/tiva_ssi.c +++ b/arch/arm/src/tiva/common/tiva_ssi.c @@ -33,6 +33,7 @@ #include #include +#include #include #include @@ -201,7 +202,7 @@ struct tiva_ssidev_s * reduce the overhead of constant SPI re-configuration. */ - sem_t exclsem; /* For exclusive access to the SSI bus */ + mutex_t lock; /* For exclusive access to the SSI bus */ uint32_t frequency; /* Current desired SCLK frequency */ uint32_t actual; /* Current actual SCLK frequency */ uint8_t mode; /* Current mode 0,1,2,3 */ @@ -223,11 +224,6 @@ static inline void ssi_putreg(struct tiva_ssidev_s *priv, static uint32_t ssi_disable(struct tiva_ssidev_s *priv); static void ssi_enable(struct tiva_ssidev_s *priv, uint32_t enable); -#ifndef CONFIG_SSI_POLLWAIT -static int ssi_semtake(sem_t *sem); -#define ssi_semgive(s) nxsem_post(s); -#endif - /* SSI data transfer */ static void ssi_txnull(struct tiva_ssidev_s *priv); @@ -471,27 +467,6 @@ static void ssi_enable(struct tiva_ssidev_s *priv, uint32_t enable) spiinfo("CR1: %08" PRIx32 "\n", regval); } -/**************************************************************************** - * Name: ssi_semtake - * - * Description: - * Wait for a semaphore (handling interruption by signals); - * - * Input Parameters: - * priv - Device-specific state data - * enable - The previous operational state - * - * Returned Value: - * - ****************************************************************************/ - -#ifndef CONFIG_SSI_POLLWAIT -static int ssi_semtake(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} -#endif - /**************************************************************************** * Name: ssi_txnull, ssi_txuint16, and ssi_txuint8 * @@ -890,7 +865,7 @@ static int ssi_transfer(struct tiva_ssidev_s *priv, const void *txbuffer, leave_critical_section(flags); do { - ret = ssi_semtake(&priv->xfrsem); + ret = nxsem_wait_uninterruptible(&priv->xfrsem); } while (priv->nrxwords < priv->nwords && ret >= 0); @@ -1038,7 +1013,7 @@ static int ssi_interrupt(int irq, void *context, void *arg) /* Wake up the waiting thread */ spiinfo("Transfer complete\n"); - ssi_semgive(&priv->xfrsem); + nxsem_post(&priv->xfrsem); } return OK; @@ -1073,11 +1048,11 @@ static int ssi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1631,7 +1606,7 @@ struct spi_dev_s *tiva_ssibus_initialize(int port) nxsem_init(&priv->xfrsem, 0, 0); nxsem_set_protocol(&priv->xfrsem, SEM_PRIO_NONE); #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Set all CR1 fields to reset state. This will be master mode. */ diff --git a/arch/arm/src/tlsr82/tlsr82_adc.c b/arch/arm/src/tlsr82/tlsr82_adc.c index e02dbd0cec4c1..6f30b5e939e80 100644 --- a/arch/arm/src/tlsr82/tlsr82_adc.c +++ b/arch/arm/src/tlsr82/tlsr82_adc.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include "tlsr82_adc.h" @@ -215,7 +215,7 @@ static struct adc_dev_s g_adc_chanbat_dev = }; #endif -static sem_t g_sem_excl = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Inline Functions @@ -864,7 +864,7 @@ static void adc_read_work(struct adc_dev_s *dev) int32_t adc; struct adc_chan_s *priv = (struct adc_chan_s *)dev->ad_priv; - ret = nxsem_wait(&g_sem_excl); + ret = nxmutex_lock(&g_lock); if (ret < 0) { aerr("Failed to wait sem ret=%d\n", ret); @@ -913,7 +913,7 @@ static void adc_read_work(struct adc_dev_s *dev) ainfo("channel: %" PRIu8 ", voltage: %" PRIu32 " mV\n", priv->channel, adc); - nxsem_post(&g_sem_excl); + nxmutex_unlock(&g_lock); } /**************************************************************************** diff --git a/arch/arm/src/tlsr82/tlsr82_aes.c b/arch/arm/src/tlsr82/tlsr82_aes.c index 05d7d48183a32..96128cf2513ff 100644 --- a/arch/arm/src/tlsr82/tlsr82_aes.c +++ b/arch/arm/src/tlsr82/tlsr82_aes.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include #include @@ -50,7 +50,7 @@ * Private data ****************************************************************************/ -static sem_t g_aes_excl_sem = SEM_INITIALIZER(1); +static mutex_t g_aes_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Public data @@ -217,7 +217,7 @@ int aes_cypher(void *out, const void *in, size_t size, const void *iv, return -EINVAL; } - ret = nxsem_wait(&g_aes_excl_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -245,7 +245,7 @@ int aes_cypher(void *out, const void *in, size_t size, const void *iv, } } - ret = nxsem_post(&g_aes_excl_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; diff --git a/arch/arm/src/tlsr82/tlsr82_serial.c b/arch/arm/src/tlsr82/tlsr82_serial.c index 649fe50c5c26c..68bcf44760017 100644 --- a/arch/arm/src/tlsr82/tlsr82_serial.c +++ b/arch/arm/src/tlsr82/tlsr82_serial.c @@ -262,7 +262,8 @@ static char g_uart0txbuffer[UART0_TX_BUF_SIZE]; #ifdef CONFIG_TLSR82_UART0_TXDMA static char g_uart0txdmabuf[UART0_TXDMA_BUF_SIZE + DMA_HEAD_LEN] \ aligned_data(4); -static sem_t g_uart0txdmasem = SEM_INITIALIZER(1); +static sem_t g_uart0txdmasem = NXSEM_INITIALIZER(1, + PRIOINHERIT_FLAGS_DISABLE); #endif #ifdef CONFIG_TLSR82_UART0_RXDMA @@ -1548,7 +1549,6 @@ static void tlsr82_uart_dma_txavail(struct uart_dev_s *dev) /* Wait for the previous dma transfer finish */ nxsem_wait(priv->txdmasem); - uart_xmitchars_dma(dev); } #endif diff --git a/arch/arm/src/xmc4/xmc4_spi.c b/arch/arm/src/xmc4/xmc4_spi.c index 5ea91e1e41dbb..62643294b0970 100644 --- a/arch/arm/src/xmc4/xmc4_spi.c +++ b/arch/arm/src/xmc4/xmc4_spi.c @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include "arm_internal.h" @@ -193,7 +193,7 @@ typedef void (*select_t)(struct spi_dev_s *dev, uint32_t devid, struct xmc4_spidev_s { uint32_t base; /* SPI controller register base address */ - sem_t spisem; /* Assures mutually exclusive access to SPI */ + mutex_t spilock; /* Assures mutually exclusive access to SPI */ select_t select; /* SPI select call-out */ bool initialized; /* TRUE: Controller has been initialized */ #ifdef CONFIG_XMC4_SPI_DMA @@ -1050,11 +1050,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&spi->spisem); + ret = nxmutex_lock(&spi->spilock); } else { - ret = nxsem_post(&spi->spisem); + ret = nxmutex_unlock(&spi->spilock); } return ret; @@ -2057,11 +2057,11 @@ struct spi_dev_s *xmc4_spibus_initialize(int channel) spi_putreg(spi, 0, XMC4_USIC_CCR_OFFSET); - /* Initialize the SPI semaphore that enforces mutually exclusive + /* Initialize the SPI mutex that enforces mutually exclusive * access to the SPI registers. */ - nxsem_init(&spi->spisem, 0, 1); + nxmutex_init(&spi->spilock); spi->initialized = true; #ifdef CONFIG_XMC4_SPI_DMA diff --git a/arch/arm64/src/qemu/qemu_serial.c b/arch/arm64/src/qemu/qemu_serial.c index 10b90b4aabaa1..3c582fa587d2d 100644 --- a/arch/arm64/src/qemu/qemu_serial.c +++ b/arch/arm64/src/qemu/qemu_serial.c @@ -799,10 +799,6 @@ void qemu_earlyserialinit(void) #endif } -/* Used to assure mutually exclusive access up_putc() */ - -/* static sem_t g_putc_lock = SEM_INITIALIZER(1); */ - /*************************************************************************** * Name: up_putc * diff --git a/arch/avr/src/avr/up_spi.c b/arch/avr/src/avr/up_spi.c index 7147873459509..2a35b22ae499d 100644 --- a/arch/avr/src/avr/up_spi.c +++ b/arch/avr/src/avr/up_spi.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include @@ -54,7 +54,7 @@ struct avr_spidev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t mode; /* Mode 0,1,2,3 */ @@ -142,11 +142,11 @@ static int spi_lock(FAR struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -481,9 +481,9 @@ FAR struct spi_dev_s *avr_spibus_initialize(int port) spi_setfrequency((FAR struct spi_dev_s *)priv, 400000); - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); leave_critical_section(flags); return &priv->spidev; diff --git a/arch/mips/src/pic32mx/pic32mx_spi.c b/arch/mips/src/pic32mx/pic32mx_spi.c index c937ac14ac90d..133b0e60162c4 100644 --- a/arch/mips/src/pic32mx/pic32mx_spi.c +++ b/arch/mips/src/pic32mx/pic32mx_spi.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include "mips_internal.h" @@ -74,7 +74,7 @@ struct pic32mx_dev_s uint8_t rxirq; /* SPI receive done interrupt number */ uint8_t txirq; /* SPI transfer done interrupt number */ #endif - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t nbits; /* Width of word in bits (8 to 16) */ @@ -427,11 +427,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -963,9 +963,9 @@ struct spi_dev_s *pic32mx_spibus_initialize(int port) priv->nbits = 8; priv->mode = SPIDEV_MODE0; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_PIC32MX_SPI_INTERRUPTS /* Enable interrupts at the SPI controller */ diff --git a/arch/mips/src/pic32mz/pic32mz_dma.c b/arch/mips/src/pic32mz/pic32mz_dma.c index 596cddf761cf7..d9263618d01c6 100644 --- a/arch/mips/src/pic32mz/pic32mz_dma.c +++ b/arch/mips/src/pic32mz/pic32mz_dma.c @@ -32,6 +32,7 @@ #include #include +#include #include "mips_internal.h" #include "sched/sched.h" @@ -72,7 +73,7 @@ struct pic32mz_dmac_s { /* Protects the channels' table */ - sem_t chsem; + mutex_t chlock; /* Describes all DMA channels */ @@ -83,9 +84,6 @@ struct pic32mz_dmac_s * Private Function Prototypes ****************************************************************************/ -static int pic32mz_dma_takesem(struct pic32mz_dmac_s *dmac); -static inline void pic32mz_dma_givesem(struct pic32mz_dmac_s *dmac); - static inline uint32_t pic32mz_dma_getreg(struct pic32mz_dmach_s *dmach, uint8_t offset); static inline void pic32mz_dma_putreg(struct pic32mz_dmach_s *dmach, @@ -184,32 +182,6 @@ static struct pic32mz_dmac_s g_dmac = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: pic32mz_dma_takesem - * - * Description: - * Take the exclusive access, waiting as necessary - * - ****************************************************************************/ - -static int pic32mz_dma_takesem(struct pic32mz_dmac_s *dmac) -{ - return nxsem_wait_uninterruptible(&dmac->chsem); -} - -/**************************************************************************** - * Name: pic32mz_dma_givesem - * - * Description: - * Release the semaphore - * - ****************************************************************************/ - -static inline void pic32mz_dma_givesem(struct pic32mz_dmac_s *dmac) -{ - nxsem_post(&dmac->chsem); -} - /**************************************************************************** * Name: pic32mz_dma_getreg * @@ -764,9 +736,9 @@ void weak_function up_dma_initialize(void) pic32mz_dma_putglobal(PIC32MZ_DMA_CONSET_OFFSET, DMA_CON_ON); - /* Initialize the semaphore. */ + /* Initialize the mutex. */ - nxsem_init(&g_dmac.chsem, 0, 1); + nxmutex_init(&g_dmac.chlock); } /**************************************************************************** @@ -791,7 +763,7 @@ DMA_HANDLE pic32mz_dma_alloc(const struct pic32mz_dma_chcfg_s *cfg) /* Search for an available DMA channel */ - ret = pic32mz_dma_takesem(&g_dmac); + ret = nxmutex_lock(&g_dmac.chlock); if (ret < 0) { return NULL; @@ -828,7 +800,7 @@ DMA_HANDLE pic32mz_dma_alloc(const struct pic32mz_dma_chcfg_s *cfg) } } - pic32mz_dma_givesem(&g_dmac); + nxmutex_unlock(&g_dmac.chlock); /* Show the result of the allocation */ diff --git a/arch/mips/src/pic32mz/pic32mz_i2c.c b/arch/mips/src/pic32mz/pic32mz_i2c.c index 3307442a2abca..9f94d62de72f9 100644 --- a/arch/mips/src/pic32mz/pic32mz_i2c.c +++ b/arch/mips/src/pic32mz/pic32mz_i2c.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -185,7 +186,7 @@ struct pic32mz_i2c_priv_s const struct pic32mz_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 @@ -235,10 +236,6 @@ static inline int pic32mz_i2c_sem_waitdone(struct pic32mz_i2c_priv_s *priv); static inline void pic32mz_i2c_sem_waitidle(struct pic32mz_i2c_priv_s *priv); -static inline void pic32mz_i2c_sem_post(struct pic32mz_i2c_priv_s *priv); -static inline void pic32mz_i2c_sem_init(struct pic32mz_i2c_priv_s *priv); -static inline void -pic32mz_i2c_sem_destroy(struct pic32mz_i2c_priv_s *priv); #ifdef CONFIG_I2C_TRACE static void pic32mz_i2c_tracereset(struct pic32mz_i2c_priv_s *priv); @@ -271,7 +268,7 @@ pic32mz_i2c_getstatus(struct pic32mz_i2c_priv_s *priv); static inline bool pic32mz_i2c_master_inactive(struct pic32mz_i2c_priv_s *priv); -static int pic32mz_i2c_isr_process(struct pic32mz_i2c_priv_s * priv); +static int pic32mz_i2c_isr_process(struct pic32mz_i2c_priv_s *priv); #ifndef CONFIG_I2C_POLLED static int pic32mz_i2c_isr(int irq, void *context, void *arg); @@ -337,6 +334,10 @@ static struct pic32mz_i2c_priv_s pic32mz_i2c1_priv = .ops = &pic32mz_i2c_ops, .config = &pic32mz_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, @@ -365,6 +366,10 @@ static struct pic32mz_i2c_priv_s pic32mz_i2c2_priv = .ops = &pic32mz_i2c_ops, .config = &pic32mz_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, @@ -393,6 +398,10 @@ static struct pic32mz_i2c_priv_s pic32mz_i2c3_priv = .ops = &pic32mz_i2c_ops, .config = &pic32mz_i2c3_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, @@ -421,6 +430,10 @@ static struct pic32mz_i2c_priv_s pic32mz_i2c4_priv = .ops = &pic32mz_i2c_ops, .config = &pic32mz_i2c4_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, @@ -449,6 +462,10 @@ static struct pic32mz_i2c_priv_s pic32mz_i2c5_priv = .ops = &pic32mz_i2c_ops, .config = &pic32mz_i2c5_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, @@ -809,58 +826,6 @@ pic32mz_i2c_sem_waitidle(struct pic32mz_i2c_priv_s *priv) i2cinfo("Timeout with I2CxCON: %04x I2CxSTAT: %04x\n", con, stat); } -/**************************************************************************** - * Name: pic32mz_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void pic32mz_i2c_sem_post(struct pic32mz_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: pic32mz_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void pic32mz_i2c_sem_init(struct pic32mz_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: pic32mz_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static inline void -pic32mz_i2c_sem_destroy(struct pic32mz_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - /**************************************************************************** * Name: pic32mz_i2c_isr_process * @@ -1566,9 +1531,9 @@ static int pic32mz_i2c_transfer(struct i2c_master_s *dev, uint32_t status = 0; int ret; - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1661,8 +1626,7 @@ static int pic32mz_i2c_transfer(struct i2c_master_s *dev, priv->dcnt = 0; priv->ptr = NULL; - pic32mz_i2c_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -1697,7 +1661,7 @@ static int pic32mz_i2c_reset(struct i2c_master_s *dev) /* Lock out other clients */ - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1792,7 +1756,7 @@ static int pic32mz_i2c_reset(struct i2c_master_s *dev) /* Release the port for re-use by other clients */ - pic32mz_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); } #endif /* CONFIG_I2C_RESET */ @@ -1858,7 +1822,6 @@ struct i2c_master_s *pic32mz_i2cbus_initialize(int port) if ((volatile int)priv->refs++ == 0) { - pic32mz_i2c_sem_init(priv); pic32mz_i2c_init(priv); } @@ -1902,9 +1865,6 @@ int pic32mz_i2cbus_uninitialize(struct i2c_master_s *dev) pic32mz_i2c_deinit(priv); - /* Release unused resources */ - - pic32mz_i2c_sem_destroy(priv); return OK; } diff --git a/arch/mips/src/pic32mz/pic32mz_spi.c b/arch/mips/src/pic32mz/pic32mz_spi.c index 22e0f0eea0a5a..49e982bb60735 100644 --- a/arch/mips/src/pic32mz/pic32mz_spi.c +++ b/arch/mips/src/pic32mz/pic32mz_spi.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include "mips_internal.h" @@ -127,7 +127,7 @@ struct pic32mz_dev_s { struct spi_dev_s spidev; /* Externally visible part of the SPI interface */ const struct pic32mz_config_s *config; - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ uint8_t mode; /* Mode 0,1,2,3 */ @@ -1176,11 +1176,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2083,9 +2083,9 @@ struct spi_dev_s *pic32mz_spibus_initialize(int port) priv->nbits = 8; priv->mode = SPIDEV_MODE0; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ + /* Initialize the SPI mutex that enforces mutually exclusive access */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_PIC32MZ_SPI_INTERRUPTS /* Enable interrupts at the SPI controller */ diff --git a/arch/renesas/src/rx65n/rx65n_dtc.c b/arch/renesas/src/rx65n/rx65n_dtc.c index 5165e11f2594c..efb637eefc28e 100644 --- a/arch/renesas/src/rx65n/rx65n_dtc.c +++ b/arch/renesas/src/rx65n/rx65n_dtc.c @@ -33,7 +33,6 @@ #include #include -#include #include "up_internal.h" #include "chip.h" @@ -388,7 +387,6 @@ struct rx65n_dtc_s uint8_t chan; /* DTC channel number */ bool initialized; /* Initialization status */ - sem_t sem; /* Used to wait for DTC channel to become available */ uint32_t base; /* DTC channel register base address */ uint8_t * vectortable; /* Vector table pointer */ @@ -1713,7 +1711,6 @@ void rx65n_dtc_initialize(void) for (chndx = 0; chndx < DTC_NCHANNELS; chndx++) /* RX65N support only one channel */ { dtchandle = &g_dtchandle[chndx]; - nxsem_init(&dtchandle->sem, 0, 1); /* Get DTC Vector table */ diff --git a/arch/renesas/src/rx65n/rx65n_riic.c b/arch/renesas/src/rx65n/rx65n_riic.c index 03362993164c8..aade22ef214f2 100644 --- a/arch/renesas/src/rx65n/rx65n_riic.c +++ b/arch/renesas/src/rx65n/rx65n_riic.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include "rx65n_riic.h" @@ -110,12 +111,12 @@ struct rx65n_i2c_priv_s { const struct i2c_ops_s *ops; const struct rx65n_i2c_dev_s *dev; - int refs; /* Referernce count */ - int bus; /* Bus number */ + int refs; /* Referernce count */ + int bus; /* Bus number */ volatile uint8_t mode; /* See enum rx65n_i2c_mode_e */ volatile uint8_t dev_sts; /* See enum rx65n_i2c_dev_sts_e */ volatile uint8_t event; /* See enum rx65n_i2c_event_e */ - sem_t sem_excl; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ sem_t sem_isr; /* Interrupt wait semaphore */ uint8_t msgc; /* Number of Messages */ struct i2c_msg_s *msgv; /* Message list */ @@ -241,6 +242,10 @@ static struct rx65n_i2c_priv_s rx65n_riic0_priv = .dev = &rx65n_riic0_dev, .refs = 0, .bus = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .mode = RIIC_NONE, .dev_sts = RIIC_STS_NO_INIT, .event = RIIC_EV_NONE, @@ -272,6 +277,10 @@ static struct rx65n_i2c_priv_s rx65n_riic1_priv = .dev = &rx65n_riic1_dev, .refs = 0, .bus = 1, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .mode = RIIC_NONE, .dev_sts = RIIC_STS_NO_INIT, .event = RIIC_EV_NONE, @@ -303,6 +312,10 @@ static struct rx65n_i2c_priv_s rx65n_riic2_priv = .dev = &rx65n_riic2_dev, .refs = 0, .bus = 2, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .mode = RIIC_NONE, .dev_sts = RIIC_STS_NO_INIT, .event = RIIC_EV_NONE, @@ -2799,7 +2812,7 @@ static int rx65n_i2c_transfer(FAR struct i2c_master_s *dev, \ /* Get exclusive access to the I2C bus */ - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); priv->mode = RIIC_READY; priv->dev_sts = RIIC_STS_IDLE; @@ -2889,7 +2902,7 @@ static int rx65n_i2c_transfer(FAR struct i2c_master_s *dev, \ } } - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); while (RIIC_FINISH != priv->mode && RIIC_NONE != priv->mode); @@ -2969,7 +2982,7 @@ static int rx65n_i2c_reset(FAR struct i2c_master_s *dev) DEBUGASSERT(priv->refs > 0); - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); if (priv->bus == 0) { @@ -3006,7 +3019,7 @@ static int rx65n_i2c_reset(FAR struct i2c_master_s *dev) rx65n_putreg(regval, RX65N_RIIC2_ICCR1); } - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return OK; } #endif /* CONFIG_I2C_RESET */ @@ -3065,11 +3078,6 @@ FAR struct i2c_master_s *rx65n_i2cbus_initialize(int channel) if ((volatile int)priv->refs++ == 0) { - /* Initialize semaphores */ - - nxsem_init(&priv->sem_excl, 0, 1); - nxsem_init(&priv->sem_isr, 0, 0); - /* Initialize the RIIC registers */ rx65n_riic_init(priv); @@ -3121,11 +3129,6 @@ int rx65n_i2cbus_uninitialize(FAR struct i2c_master_s *dev) irq_detach(priv->dev->tei_irq); irq_detach(priv->dev->eei_irq); - /* Release unused resources */ - - nxsem_destroy(&priv->sem_excl); - nxsem_destroy(&priv->sem_isr); - return OK; } diff --git a/arch/renesas/src/rx65n/rx65n_rspi.c b/arch/renesas/src/rx65n/rx65n_rspi.c index 0aa6994dec506..cc6463b14a373 100644 --- a/arch/renesas/src/rx65n/rx65n_rspi.c +++ b/arch/renesas/src/rx65n/rx65n_rspi.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include #include @@ -165,7 +165,7 @@ struct rx65n_rspidev_s #endif bool initialized; /* Has RSPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ @@ -1700,11 +1700,11 @@ static int rspi_lock(FAR struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -2256,7 +2256,7 @@ static void rspi_bus_initialize(FAR struct rx65n_rspidev_s *priv) } #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Initialize control register */ diff --git a/arch/renesas/src/rx65n/rx65n_rspi_sw.c b/arch/renesas/src/rx65n/rx65n_rspi_sw.c index 3dc4734abf58c..98fed887ebc78 100644 --- a/arch/renesas/src/rx65n/rx65n_rspi_sw.c +++ b/arch/renesas/src/rx65n/rx65n_rspi_sw.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include #include @@ -154,7 +154,7 @@ struct rx65n_rspidev_s void (*rxword)(struct rx65n_rspidev_s *priv); bool initialized; /* Has RSPI interface been initialized */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ @@ -1335,11 +1335,11 @@ static int rspi_lock(FAR struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1852,7 +1852,7 @@ static void rspi_bus_initialize(FAR struct rx65n_rspidev_s *priv) nxsem_init(&priv->waitsem, 0, 0); nxsem_set_protocol(&priv->waitsem, SEM_PRIO_NONE); #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Initialize control register */ diff --git a/arch/renesas/src/rx65n/rx65n_rtc_lowerhalf.c b/arch/renesas/src/rx65n/rx65n_rtc_lowerhalf.c index 3f3d44604ab94..56a8f8e30cedd 100644 --- a/arch/renesas/src/rx65n/rx65n_rtc_lowerhalf.c +++ b/arch/renesas/src/rx65n/rx65n_rtc_lowerhalf.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "chip.h" #include @@ -79,7 +80,7 @@ struct rx65n_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -360,7 +361,7 @@ static int rx65n_setalarm(FAR struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->id == 0); priv = (FAR struct rx65n_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -391,8 +392,7 @@ static int rx65n_setalarm(FAR struct rtc_lowerhalf_s *lower, } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -646,19 +646,17 @@ static int rx65n_setperiodic(FAR struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL); priv = (FAR struct rx65n_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } memcpy(&priv->periodic, alarminfo, sizeof(struct lower_setperiodic_s)); - ret = rx65n_rtc_setperiodic(&alarminfo->period, (periodiccb_t)rx65n_periodic_callback); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -690,7 +688,7 @@ static int rx65n_cancelperiodic(FAR struct rtc_lowerhalf_s *lower, int id) DEBUGASSERT(id == 0); - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -698,8 +696,7 @@ static int rx65n_cancelperiodic(FAR struct rtc_lowerhalf_s *lower, int id) ret = rx65n_rtc_cancelperiodic(); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -732,8 +729,7 @@ static int rx65n_cancelperiodic(FAR struct rtc_lowerhalf_s *lower, int id) FAR struct rtc_lowerhalf_s *rx65n_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (FAR struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/renesas/src/rx65n/rx65n_sbram.c b/arch/renesas/src/rx65n/rx65n_sbram.c index 546c8ba07ad5d..bb0d6d3e14646 100644 --- a/arch/renesas/src/rx65n/rx65n_sbram.c +++ b/arch/renesas/src/rx65n/rx65n_sbram.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -90,7 +91,7 @@ struct sbramfh_s struct rx65n_sbram_s { - sem_t exclsem; /* For atomic accesses to this structure */ + mutex_t lock; /* For atomic accesses to this structure */ uint8_t refs; /* Number of references */ FAR struct sbramfh_s *bbf; /* File in bbram */ }; @@ -175,46 +176,6 @@ static void rx65n_sbram_dump(FAR struct sbramfh_s *bbf, char *op) } #endif -/**************************************************************************** - * Name: rx65n_sbram_semgive - ****************************************************************************/ - -static void rx65n_sbram_semgive(FAR struct rx65n_sbram_s *priv) -{ - nxsem_post(&priv->exclsem); -} - -/**************************************************************************** - * Name: rx65n_sbram_semtake - * - * Description: - * Take a semaphore handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the CAN peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void rx65n_sbram_semtake(FAR struct rx65n_sbram_s *priv) -{ - int ret; - - /* Wait until we successfully get the semaphore. EINTR is the only - * expected 'failure' (meaning that the wait for the semaphore was - * interrupted by a signal. - */ - - do - { - ret = nxsem_wait(&priv->exclsem); - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); -} - /**************************************************************************** * Name: rx65n_sbram_crc * @@ -252,7 +213,7 @@ static int rx65n_sbram_open(FAR struct file *filep) /* Increment the reference count */ - rx65n_sbram_semtake(bbr); + nxmutex_lock(&bbr->lock); if (bbr->refs == MAX_OPENCNT) { return -EMFILE; @@ -262,7 +223,7 @@ static int rx65n_sbram_open(FAR struct file *filep) bbr->refs++; } - rx65n_sbram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return OK; } @@ -300,7 +261,7 @@ static int rx65n_sbram_close(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); bbr = (FAR struct rx65n_sbram_s *)inode->i_private; - rx65n_sbram_semtake(bbr); + nxmutex_lock(&bbr->lock); SBRAM_DUMP(bbr->bbf, "close"); @@ -323,7 +284,7 @@ static int rx65n_sbram_close(FAR struct file *filep) } } - rx65n_sbram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return ret; } @@ -342,7 +303,7 @@ static off_t rx65n_sbram_seek(FAR struct file *filep, off_t offset, DEBUGASSERT(inode && inode->i_private); bbr = (FAR struct rx65n_sbram_s *)inode->i_private; - rx65n_sbram_semtake(bbr); + nxmutex_lock(&bbr->lock); /* Determine the new, requested file position */ @@ -364,7 +325,7 @@ static off_t rx65n_sbram_seek(FAR struct file *filep, off_t offset, /* Return EINVAL if the whence argument is invalid */ - rx65n_sbram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return -EINVAL; } @@ -394,7 +355,7 @@ static off_t rx65n_sbram_seek(FAR struct file *filep, off_t offset, ret = -EINVAL; } - rx65n_sbram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return ret; } @@ -411,7 +372,7 @@ static ssize_t rx65n_sbram_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); bbr = (FAR struct rx65n_sbram_s *)inode->i_private; - rx65n_sbram_semtake(bbr); + nxmutex_lock(&bbr->lock); /* Trim len if read would go beyond end of device */ @@ -422,7 +383,7 @@ static ssize_t rx65n_sbram_read(FAR struct file *filep, FAR char *buffer, memcpy(buffer, &bbr->bbf->data[filep->f_pos], len); filep->f_pos += len; - rx65n_sbram_semgive(bbr); + nxmutex_unlock(&bbr->lock); return len; } @@ -467,12 +428,12 @@ static ssize_t rx65n_sbram_write(FAR struct file *filep, ret = len; /* save number of bytes written */ - rx65n_sbram_semtake(bbr); + nxmutex_lock(&bbr->lock); SBRAM_DUMP(bbr->bbf, "write"); rx65n_sbram_internal_write(bbr->bbf, buffer, filep->f_pos, len); filep->f_pos += len; SBRAM_DUMP(bbr->bbf, "write done"); - rx65n_sbram_semgive(bbr); + nxmutex_unlock(&bbr->lock); } SBRAM_DEBUG_READ(); @@ -515,7 +476,7 @@ static int rx65n_sbram_ioctl(FAR struct file *filep, int cmd, { FAR struct sbramd_s *bbrr = (FAR struct sbramd_s *)((uintptr_t)arg); - rx65n_sbram_semtake(bbr); + nxmutex_lock(&bbr->lock); if (!bbrr) { ret = -EINVAL; @@ -531,7 +492,7 @@ static int rx65n_sbram_ioctl(FAR struct file *filep, int cmd, ret = OK; } - rx65n_sbram_semgive(bbr); + nxmutex_unlock(&bbr->lock); } return ret; @@ -558,14 +519,14 @@ static int rx65n_sbram_unlink(FAR struct inode *inode) DEBUGASSERT(inode && inode->i_private); bbr = (FAR struct rx65n_sbram_s *)inode->i_private; - rx65n_sbram_semtake(bbr); + nxmutex_lock(&bbr->lock); memset(bbr->bbf->data, 0, bbr->bbf->len); bbr->bbf->lastwrite.tv_nsec = 0; bbr->bbf->lastwrite.tv_sec = 0; bbr->bbf->crc = rx65n_sbram_crc(bbr->bbf); bbr->refs = 0; - rx65n_sbram_semgive(bbr); - nxsem_destroy(&bbr->exclsem); + nxmutex_unlock(&bbr->lock); + nxmutex_destroy(&bbr->lock); return 0; } #endif @@ -626,7 +587,7 @@ static int rx65n_sbram_probe(int *ent, struct rx65n_sbram_s pdev[]) pdev[i].bbf = pf; pf = (struct sbramfh_s *)((uint8_t *)pf + alloc); - nxsem_init(&g_sbram[i].exclsem, 0, 1); + nxmutex_init(&g_sbram[i].lock); } avail -= alloc; diff --git a/arch/renesas/src/rx65n/rx65n_usbdev.c b/arch/renesas/src/rx65n/rx65n_usbdev.c index 626f6290e91fb..b06ab7766de71 100644 --- a/arch/renesas/src/rx65n/rx65n_usbdev.c +++ b/arch/renesas/src/rx65n/rx65n_usbdev.c @@ -298,8 +298,6 @@ uint8_t g_buffer[64] ; # define rx65n_putreg16(val,addr) putreg16(val,addr) # define rx65n_putreg32(val,addr) putreg32(val,addr) -#define rx65n_usbdev_givesem(s) nxsem_post(s); - /* Request queue operations *************************************************/ FAR struct rx65n_req_s *rx65n_rqdequeue(FAR struct rx65n_ep_s diff --git a/arch/renesas/src/rx65n/rx65n_usbhost.c b/arch/renesas/src/rx65n/rx65n_usbhost.c index 5b158f384e5d9..783ef5c80c520 100644 --- a/arch/renesas/src/rx65n/rx65n_usbhost.c +++ b/arch/renesas/src/rx65n/rx65n_usbhost.c @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -136,7 +137,7 @@ struct rx65n_usbhost_s #endif - sem_t exclsem; /* Support mutually exclusive access */ + mutex_t lock; /* Support mutually exclusive access */ /* Semaphore to wait Write-back Done Head event */ @@ -293,9 +294,6 @@ static void rx65n_usbhost_clearbit (volatile short *regadd, /* Semaphores ***************************************************************/ -static int rx65n_usbhost_takesem(sem_t *sem); -#define rx65n_usbhost_givesem(s) nxsem_post(s); - /* Byte stream access helper functions **************************************/ static inline uint16_t rx65n_usbhost_getle16(const uint8_t *val); @@ -2877,7 +2875,7 @@ void usb_hstd_brdy_pipe_process (uint16_t bitsts) #endif } - rx65n_usbhost_givesem(&g_rx65n_edlist[i].wdhsem); + nxsem_post(&g_rx65n_edlist[i].wdhsem); } } } @@ -2959,7 +2957,7 @@ void usb_hstd_bemp_pipe_process (uint16_t bitsts) /* Release the semaphore for this pipe */ - rx65n_usbhost_givesem(&g_rx65n_edlist[i].wdhsem); + nxsem_post(&g_rx65n_edlist[i].wdhsem); } else @@ -3589,7 +3587,7 @@ void usb_hstd_nrdy_endprocess (uint16_t pipe) /* Release the semaphore for this pipe */ - rx65n_usbhost_givesem(&g_rx65n_edlist[pipe].wdhsem); + nxsem_post(&g_rx65n_edlist[pipe].wdhsem); } else { @@ -3611,7 +3609,7 @@ void usb_hstd_nrdy_endprocess (uint16_t pipe) /* Release the semaphore for this pipe */ - rx65n_usbhost_givesem(&g_rx65n_edlist[pipe].wdhsem); + nxsem_post(&g_rx65n_edlist[pipe].wdhsem); } } } @@ -4454,7 +4452,7 @@ void usb_hstd_brdy_pipe (void) usb_hstd_ctrl_end((uint16_t) USB_CTRL_END); - rx65n_usbhost_givesem(&EDCTRL->wdhsem); + nxsem_post(&EDCTRL->wdhsem); return; /* Nothing else to do here... as of now... */ } @@ -4575,7 +4573,7 @@ void usb_hstd_brdy_pipe (void) (EDCTRL->xfrinfo->tdxfercond == USB_READSHRT) || (EDCTRL->xfrinfo->tdxfercond == USB_READOVER)) { - rx65n_usbhost_givesem(&EDCTRL->wdhsem); + nxsem_post(&EDCTRL->wdhsem); } hw_usb_clear_sts_brdy (USB_PIPE0); /* This was missing? */ @@ -4680,7 +4678,7 @@ void usb_hstd_bemp_pipe (void) usb_hstd_ctrl_end((uint16_t) USB_CTRL_END); - rx65n_usbhost_givesem(&EDCTRL->wdhsem); + nxsem_post(&EDCTRL->wdhsem); return; /* As of now, Nothing else to do here... */ } @@ -4957,35 +4955,6 @@ static void rx65n_usbhost_putreg(uint16_t val, uint32_t addr) } #endif -/**************************************************************************** - * Name: rx65n_usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int rx65n_usbhost_takesem(sem_t *sem) -{ - int ret; - - do - { - /* Take the semaphore (perhaps waiting) */ - - ret = nxsem_wait(sem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); - return ret; -} - /**************************************************************************** * Name: rx65n_usbhost_getle16 * @@ -5253,7 +5222,7 @@ static inline int rx65n_usbhost_remctrled(struct rx65n_usbhost_s *priv, if (g_kbdport == g_usbidx) { - rx65n_usbhost_givesem(&g_rx65n_edlist[g_kbdpipe].wdhsem); + nxsem_post(&g_rx65n_edlist[g_kbdpipe].wdhsem); } return OK; @@ -5847,7 +5816,7 @@ static int rx65n_usbhost_ctrltd(struct rx65n_usbhost_s *priv, if (dirpid == GTD_STATUS_DP_SETUP) { - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Set DATA0 bit of DCPCTR */ @@ -5878,17 +5847,17 @@ static int rx65n_usbhost_ctrltd(struct rx65n_usbhost_s *priv, hw_usb_hset_sureq(); /* At this point every thing is done w.r.t hardware to send the - * setup packet... Now release the exclusive access semaphore and + * setup packet... Now release the exclusive access mutex and * wait for wdhsem */ - rx65n_usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* Wait for the Writeback Done Head interrupt */ if (priv->connected) { - rx65n_usbhost_takesem(&ed->wdhsem); + nxsem_wait_uninterruptible(&ed->wdhsem); } /* Disable setup packet status response */ @@ -5899,7 +5868,7 @@ static int rx65n_usbhost_ctrltd(struct rx65n_usbhost_s *priv, else if (dirpid == GTD_STATUS_DP_IN) { - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* BEMP0 Disable */ @@ -5915,27 +5884,27 @@ static int rx65n_usbhost_ctrltd(struct rx65n_usbhost_s *priv, * to receive the setup data... Now wait for interrupt */ - rx65n_usbhost_givesem (&priv->exclsem); + nxmutex_unlock(&priv->lock); if (priv->connected) { - rx65n_usbhost_takesem(&ed->wdhsem); + nxsem_wait_uninterruptible(&ed->wdhsem); } } else if (dirpid == GTD_STATUS_DP_OUT) { - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* process setup packet status phase */ usb_hstd_ctrl_write_start(buffer, buflen); - rx65n_usbhost_givesem (&priv->exclsem); + nxmutex_unlock(&priv->lock); if (priv->connected) { - rx65n_usbhost_takesem(&ed->wdhsem); + nxsem_wait_uninterruptible(&ed->wdhsem); } /* Disable Empty Interrupt */ @@ -6189,20 +6158,20 @@ static void rx65n_usbhost_bottomhalf (void *arg) * is no real option (other than to reschedule and delay). */ - rx65n_usbhost_takesem(&g_usbhost.exclsem); + nxmutex_lock(&g_usbhost.lock); if (bottom_half_processing == USB_PROCESS_SACK_INT) { EDCTRL->xfrinfo->tdstatus = TD_CC_NOERROR; hw_usb_hclear_sts_sack(); - rx65n_usbhost_givesem(&EDCTRL->wdhsem); + nxsem_post(&EDCTRL->wdhsem); } else if (bottom_half_processing == USB_PROCESS_SIGN_INT) { EDCTRL->xfrinfo->tdstatus = TD_CC_PIDCHECKFAILURE; hw_usb_hclear_sts_sign(); - rx65n_usbhost_givesem(&EDCTRL->wdhsem); + nxsem_post(&EDCTRL->wdhsem); } else if (bottom_half_processing == USB_PROCESS_ATTACHED_INT) @@ -6235,7 +6204,7 @@ static void rx65n_usbhost_bottomhalf (void *arg) if (priv->pscwait) { priv->pscwait = false; - rx65n_usbhost_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } } else @@ -6264,13 +6233,13 @@ static void rx65n_usbhost_bottomhalf (void *arg) /* hardware is available */ - rx65n_usbhost_givesem(&g_usbhost.exclsem); + nxmutex_unlock(&g_usbhost.lock); /* Are we bound to a class instance? */ if (g_kbdpipe) { - rx65n_usbhost_givesem(&g_rx65n_edlist[g_kbdpipe].wdhsem); + nxsem_post(&g_rx65n_edlist[g_kbdpipe].wdhsem); } g_kbdpipe = 0; @@ -6286,7 +6255,7 @@ static void rx65n_usbhost_bottomhalf (void *arg) if (priv->pscwait) { - rx65n_usbhost_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } @@ -6329,7 +6298,7 @@ static void rx65n_usbhost_bottomhalf (void *arg) bottom_half_processing); } - rx65n_usbhost_givesem(&g_usbhost.exclsem); + nxmutex_unlock(&g_usbhost.lock); } /**************************************************************************** @@ -6423,7 +6392,7 @@ static int rx65n_usbhost_wait(struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - ret = rx65n_usbhost_takesem(&priv->pscsem); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -6600,7 +6569,7 @@ static int rx65n_usbhost_ep0configure(struct usbhost_driver_s *drvr, /* We must have exclusive access to EP0 and the control list */ - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); usb_cstd_set_nak(USB_PIPE0); /* Make sure, all the DEVADDn registers are set to default state */ @@ -6624,7 +6593,7 @@ static int rx65n_usbhost_ep0configure(struct usbhost_driver_s *drvr, hw_usb_set_curpipe (USB_CUSE, USB_PIPE0); hw_usb_set_bclr (USB_CUSE); - rx65n_usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -6672,7 +6641,7 @@ static int rx65n_usbhost_epalloc(struct usbhost_driver_s *drvr, * periodic list and the interrupt table. */ - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Take the ED descriptor from the list of ED Array - based on pipe num * Also note it down as part of ED structurie itself @@ -6819,7 +6788,7 @@ static int rx65n_usbhost_epalloc(struct usbhost_driver_s *drvr, } } - rx65n_usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -6858,7 +6827,7 @@ static int rx65n_usbhost_epfree(struct usbhost_driver_s *drvr, * the periodic list and the interrupt table. */ - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Remove the ED to the correct list depending on the trasfer type */ @@ -6888,7 +6857,7 @@ static int rx65n_usbhost_epfree(struct usbhost_driver_s *drvr, /* Put the ED back into the free list */ rx65n_usbhost_edfree(ed); - rx65n_usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -6935,7 +6904,7 @@ static int rx65n_usbhost_alloc(struct usbhost_driver_s *drvr, /* We must have exclusive access to the transfer buffer pool */ - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); *buffer = rx65n_usbhost_tballoc(); if (*buffer) @@ -6944,7 +6913,7 @@ static int rx65n_usbhost_alloc(struct usbhost_driver_s *drvr, ret = OK; } - rx65n_usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -6979,9 +6948,9 @@ static int rx65n_usbhost_free(struct usbhost_driver_s *drvr, uint8_t *buffer) /* We must have exclusive access to the transfer buffer pool */ - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); rx65n_usbhost_tbfree(buffer); - rx65n_usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -7173,7 +7142,8 @@ static int rx65n_usbhost_ctrlin(struct usbhost_driver_s *drvr, USB_REQ_RECIPIENT_INTERFACE) && (req_req == USBHID_REQUEST_GETREPORT)) { - rx65n_usbhost_takesem (&g_rx65n_edlist[kbd_interrupt_in_pipe].wdhsem); + nxsem_wait_uninterruptible( + &g_rx65n_edlist[kbd_interrupt_in_pipe].wdhsem); *(local_buf + 0) = kbd_report_data [0]; *(local_buf + 1) = kbd_report_data [1]; @@ -7491,7 +7461,7 @@ static ssize_t rx65n_usbhost_transfer(struct usbhost_driver_s *drvr, * */ - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Allocate a structure to retain the information needed when the * transfer completes. @@ -7505,8 +7475,8 @@ static ssize_t rx65n_usbhost_transfer(struct usbhost_driver_s *drvr, { uerr("ERROR: rx65n_usbhost_alloc_xfrinfo failed\n"); nbytes = -ENOMEM; - rx65n_usbhost_givesem(&priv->exclsem); - goto errout_with_sem; + nxmutex_unlock(&priv->lock); + goto errout_with_lock; } /* Newly added condition */ @@ -7569,7 +7539,7 @@ static ssize_t rx65n_usbhost_transfer(struct usbhost_driver_s *drvr, /* Set up the transfer */ ret = rx65n_usbhost_transfer_common(priv, ed, buffer, buflen); - rx65n_usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (ret < 0) { uerr("ERROR: rx65n_usbhost_transfer_common failed: %d\n", ret); @@ -7581,7 +7551,7 @@ static ssize_t rx65n_usbhost_transfer(struct usbhost_driver_s *drvr, if (priv->connected) { - rx65n_usbhost_takesem(&ed->wdhsem); + nxsem_wait_uninterruptible(&ed->wdhsem); } /* Update the buffer pointer for next buffer operation */ @@ -7650,9 +7620,9 @@ static ssize_t rx65n_usbhost_transfer(struct usbhost_driver_s *drvr, } while (0); -errout_with_sem: +errout_with_lock: - /* rx65n_usbhost_givesem(&priv->exclsem); */ + /* nxmutex_unlock(&priv->lock); */ return nbytes; } @@ -7802,7 +7772,7 @@ static int rx65n_usbhost_asynch(struct usbhost_driver_s *drvr, * buffer pool, the bulk and interrupt lists, and the HCCA interrupt table. */ - rx65n_usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Allocate a structure to retain the information needed when the * asynchronous transfer completes. @@ -7815,7 +7785,7 @@ static int rx65n_usbhost_asynch(struct usbhost_driver_s *drvr, { uerr("ERROR: rx65n_usbhost_alloc_xfrinfo failed\n"); ret = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } /* Initialize the transfer structure */ @@ -7835,7 +7805,7 @@ static int rx65n_usbhost_asynch(struct usbhost_driver_s *drvr, if (ret < 0) { uerr("ERROR: rx65n_usbhost_dma_alloc failed: %d\n", ret); - goto errout_with_sem; + goto errout_with_lock; } /* If a buffer was allocated, then use it instead of the callers buffer */ @@ -7861,7 +7831,7 @@ static int rx65n_usbhost_asynch(struct usbhost_driver_s *drvr, /* Enable Ready Interrupt */ - rx65n_usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; errout_with_asynch: @@ -7877,8 +7847,8 @@ static int rx65n_usbhost_asynch(struct usbhost_driver_s *drvr, rx65n_usbhost_free_xfrinfo(xfrinfo); ed->xfrinfo = NULL; -errout_with_sem: - rx65n_usbhost_givesem(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -7965,7 +7935,7 @@ static int rx65n_usbhost_cancel(FAR struct usbhost_driver_s *drvr, /* Wake up the waiting thread */ - rx65n_usbhost_givesem(&g_rx65n_edlist[USB_PIPE6].wdhsem); + nxsem_post(&g_rx65n_edlist[USB_PIPE6].wdhsem); /* And free the transfer structure */ @@ -8397,10 +8367,10 @@ struct usbhost_connection_s *rx65n_usbhost_initialize(int controller) usbhost_devaddr_initialize(&priv->rhport); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->pscsem, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_lock(&priv->lock); /* The pscsem semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/arch/risc-v/src/bl602/bl602_i2c.c b/arch/risc-v/src/bl602/bl602_i2c.c index 172274d6b69d6..01c197a33a114 100644 --- a/arch/risc-v/src/bl602/bl602_i2c.c +++ b/arch/risc-v/src/bl602/bl602_i2c.c @@ -33,6 +33,7 @@ #include #include +#include #include #include @@ -92,11 +93,11 @@ struct bl602_i2c_priv_s const struct bl602_i2c_config_s *config; - uint8_t subflag; /* Sub address flag */ - uint32_t subaddr; /* Sub address */ - uint8_t sublen; /* Sub address length */ - sem_t sem_excl; /* Mutual exclusion semaphore */ - sem_t sem_isr; /* Interrupt wait semaphore */ + uint8_t subflag; /* Sub address flag */ + uint32_t subaddr; /* Sub address */ + uint8_t sublen; /* Sub address length */ + mutex_t lock; /* Mutual exclusion mutex */ + sem_t sem_isr; /* Interrupt wait semaphore */ /* I2C work state */ @@ -151,6 +152,8 @@ static struct bl602_i2c_priv_s bl602_i2c0_priv = .config = &bl602_i2c0_config, .subaddr = 0, .sublen = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .i2cstate = EV_I2C_END_INT, .msgv = NULL, .msgid = 0, @@ -228,26 +231,6 @@ static void bl602_i2c_recvdata(struct bl602_i2c_priv_s *priv) priv->bytes += count; } -/**************************************************************************** - * Name: bl602_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static void bl602_i2c_sem_init(struct bl602_i2c_priv_s *priv) -{ - nxsem_init(&priv->sem_excl, 0, 1); - - /* This semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ - - nxsem_init(&priv->sem_isr, 0, 1); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); -} - /**************************************************************************** * Name: bl602_i2c_clear_status * @@ -689,10 +672,10 @@ static int bl602_i2c_transfer(struct i2c_master_s *dev, return -1; } - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - i2cerr("take sem_excl error\n"); + i2cerr("lock error\n"); return ret; } @@ -759,8 +742,7 @@ static int bl602_i2c_transfer(struct i2c_master_s *dev, nxsem_post(&priv->sem_isr); } - nxsem_post(&priv->sem_excl); - + nxmutex_unlock(&priv->lock); return ret; } @@ -996,7 +978,6 @@ struct i2c_master_s *bl602_i2cbus_initialize(int port) up_enable_irq(BL602_IRQ_I2C); bl602_i2c_intmask(I2C_INT_ALL, 1); irq_attach(BL602_IRQ_I2C, bl602_i2c_irq, priv); - bl602_i2c_sem_init(priv); leave_critical_section(flags); @@ -1035,9 +1016,6 @@ int bl602_i2cbus_uninitialize(struct i2c_master_s *dev) bl602_swrst_ahb_slave1(AHB_SLAVE1_I2C); - nxsem_destroy(&priv->sem_excl); - nxsem_destroy(&priv->sem_isr); - return OK; } diff --git a/arch/risc-v/src/bl602/bl602_netdev.c b/arch/risc-v/src/bl602/bl602_netdev.c index abb8471d72e8c..09ea73aaf174d 100644 --- a/arch/risc-v/src/bl602/bl602_netdev.c +++ b/arch/risc-v/src/bl602/bl602_netdev.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -201,7 +202,7 @@ static struct tx_buf_ind_s g_tx_buf_indicator = static uint8_t locate_data(".wifi_ram.txbuff") g_tx_buff[BL602_NET_TXBUFF_NUM][BL602_NET_TXBUFF_SIZE]; -static sem_t g_wifi_scan_sem; /* wifi scan complete semaphore */ +static mutex_t g_wifi_scan_lock; /* wifi scan complete mutex */ static sem_t g_wifi_connect_sem; /* Rx Pending List */ @@ -1531,7 +1532,7 @@ bl602_net_ioctl(struct net_driver_s *dev, int cmd, unsigned long arg) para->flags = 0; } - if (nxsem_trywait(&g_wifi_scan_sem) == 0) + if (nxmutex_trylock(&g_wifi_scan_lock) == 0) { if (priv->channel != 0) { @@ -1564,24 +1565,24 @@ bl602_net_ioctl(struct net_driver_s *dev, int cmd, unsigned long arg) { struct iwreq *req = (struct iwreq *)arg; - nxsem_wait(&g_wifi_scan_sem); + nxmutex_lock(&g_wifi_scan_lock); if (g_state.scan_result_status != 0) { wlwarn("scan failed\n"); - nxsem_post(&g_wifi_scan_sem); + nxmutex_unlock(&g_wifi_scan_lock); return -EIO; } if (g_state.scan_result_len == 0) { req->u.data.length = 0; - nxsem_post(&g_wifi_scan_sem); + nxmutex_unlock(&g_wifi_scan_lock); return OK; } ret = format_scan_result_to_wapi(req, g_state.scan_result_len); - nxsem_post(&g_wifi_scan_sem); + nxmutex_unlock(&g_wifi_scan_lock); return ret; } while (0); @@ -2049,7 +2050,7 @@ void bl602_net_event(int evt, int val) do { g_state.scan_result_status = val; - nxsem_post(&g_wifi_scan_sem); + nxmutex_unlock(&g_wifi_scan_lock); } while (0); @@ -2109,9 +2110,9 @@ int bl602_net_initialize(void) int idx; uint8_t mac[6]; - /* Initialize scan semaphore */ + /* Initialize scan mutex & semaphore */ - tmp = sem_init(&g_wifi_scan_sem, 0, 1); + tmp = nxmutex_init(&g_wifi_scan_lock); if (tmp < 0) { return tmp; @@ -2204,7 +2205,7 @@ int bl602_net_initialize(void) tmp = netdev_register(&priv->net_dev, NET_LL_IEEE80211); if (tmp < 0) { - sem_destroy(&g_wifi_scan_sem); + nxmutex_destroy(&g_wifi_scan_lock); return tmp; } } diff --git a/arch/risc-v/src/bl602/bl602_os_hal.c b/arch/risc-v/src/bl602/bl602_os_hal.c index 493a1db59a6dd..6ab0e43c34cb4 100644 --- a/arch/risc-v/src/bl602/bl602_os_hal.c +++ b/arch/risc-v/src/bl602/bl602_os_hal.c @@ -55,6 +55,7 @@ #include #include #include +#include #include #include @@ -1281,26 +1282,26 @@ void bl_os_irq_disable(int32_t n) void *bl_os_mutex_create(void) { int ret; - sem_t *sem; + mutex_t *mutex; int tmp; - tmp = sizeof(sem_t); - sem = (sem_t *)kmm_malloc(tmp); - if (!sem) + tmp = sizeof(mutex_t); + mutex = (mutex_t *)kmm_malloc(tmp); + if (!mutex) { wlerr("ERROR: Failed to alloc %d memory\n", tmp); return NULL; } - ret = nxsem_init(sem, 0, 1); + ret = nxmutex_init(mutex); if (ret) { - wlerr("ERROR: Failed to initialize sem error=%d\n", ret); - kmm_free(sem); + wlerr("ERROR: Failed to initialize mutex error=%d\n", ret); + kmm_free(mutex); return NULL; } - return sem; + return mutex; } /**************************************************************************** @@ -1319,10 +1320,10 @@ void *bl_os_mutex_create(void) void bl_os_mutex_delete(void *mutex_data) { - sem_t *sem = (sem_t *)mutex_data; + mutex_t *mutex = (mutex_t *)mutex_data; - nxsem_destroy(sem); - kmm_free(sem); + nxmutex_destroy(mutex); + kmm_free(mutex); } /**************************************************************************** @@ -1342,12 +1343,12 @@ void bl_os_mutex_delete(void *mutex_data) int32_t bl_os_mutex_lock(void *mutex_data) { int ret; - sem_t *sem = (sem_t *)mutex_data; + mutex_t *mutex = (mutex_t *)mutex_data; - ret = nxsem_wait(sem); + ret = nxmutex_lock(mutex); if (ret) { - wlerr("ERROR: Failed to wait sem\n"); + wlerr("ERROR: Failed to wait mutex\n"); } return bl_os_errno_trans(ret); @@ -1370,12 +1371,12 @@ int32_t bl_os_mutex_lock(void *mutex_data) int32_t bl_os_mutex_unlock(void *mutex_data) { int ret; - sem_t *sem = (sem_t *)mutex_data; + mutex_t *mutex = (mutex_t *)mutex_data; - ret = nxsem_post(sem); + ret = nxmutex_unlock(mutex); if (ret) { - wlerr("ERROR: Failed to post sem error=%d\n", ret); + wlerr("ERROR: Failed to unlock error=%d\n", ret); } return bl_os_errno_trans(ret); diff --git a/arch/risc-v/src/bl602/bl602_rtc_lowerhalf.c b/arch/risc-v/src/bl602/bl602_rtc_lowerhalf.c index f736427b93f37..13169cc15b484 100644 --- a/arch/risc-v/src/bl602/bl602_rtc_lowerhalf.c +++ b/arch/risc-v/src/bl602/bl602_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "riscv_internal.h" @@ -72,7 +73,7 @@ struct bl602_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ struct rtc_time rtc_base; struct rtc_time rtc_alarm; @@ -366,7 +367,7 @@ static int bl602_setalarm(struct rtc_lowerhalf_s *lower, DEBUGASSERT(lower != NULL && alarminfo != NULL && alarminfo->id == 0); priv = (struct bl602_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -397,8 +398,7 @@ static int bl602_setalarm(struct rtc_lowerhalf_s *lower, ret = OK; } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -678,7 +678,7 @@ int up_rtc_initialize(void) struct rtc_lowerhalf_s *bl602_rtc_lowerhalf_initialize(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); + nxmutex_init(&g_rtc_lowerhalf.devlock); #ifdef CONFIG_RTC_PERIODIC g_rtc_lowerhalf.periodic_enable = 0; diff --git a/arch/risc-v/src/bl602/bl602_spi.c b/arch/risc-v/src/bl602/bl602_spi.c index ba4ad59cc799c..9d78f17c2dfde 100644 --- a/arch/risc-v/src/bl602/bl602_spi.c +++ b/arch/risc-v/src/bl602/bl602_spi.c @@ -37,7 +37,7 @@ #include #include -#include +#include #include #include @@ -130,7 +130,7 @@ struct bl602_spi_priv_s /* Held while chip is selected for mutual exclusion */ - sem_t exclsem; + mutex_t lock; /* Interrupt wait semaphore */ @@ -234,10 +234,12 @@ static const struct spi_ops_s bl602_spi_ops = static struct bl602_spi_priv_s bl602_spi_priv = { .spi_dev = - { - .ops = &bl602_spi_ops - }, - .config = &bl602_spi_config, + { + .ops = &bl602_spi_ops + }, + .config = &bl602_spi_config, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .dma_rxchan = -1, .dma_txchan = -1, }; @@ -446,11 +448,11 @@ static int bl602_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1560,12 +1562,6 @@ static void bl602_spi_init(struct spi_dev_s *dev) struct bl602_spi_priv_s *priv = (struct bl602_spi_priv_s *)dev; const struct bl602_spi_config_s *config = priv->config; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ - - nxsem_init(&priv->exclsem, 0, 1); - nxsem_init(&priv->sem_isr_rx, 0, 0); - nxsem_init(&priv->sem_isr_tx, 0, 0); - bl602_configgpio(BOARD_SPI_CS); bl602_configgpio(BOARD_SPI_MOSI); bl602_configgpio(BOARD_SPI_MISO); @@ -1720,11 +1716,6 @@ int bl602_spibus_uninitialize(struct spi_dev_s *dev) leave_critical_section(flags); bl602_spi_deinit(dev); - - nxsem_destroy(&priv->exclsem); - nxsem_destroy(&priv->sem_isr_rx); - nxsem_destroy(&priv->sem_isr_tx); - return OK; } diff --git a/arch/risc-v/src/esp32c3/esp32c3_adc.c b/arch/risc-v/src/esp32c3/esp32c3_adc.c index 58b65f1b51459..959430d0c1eeb 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_adc.c +++ b/arch/risc-v/src/esp32c3/esp32c3_adc.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include "riscv_internal.h" #include "esp32c3.h" @@ -248,7 +248,7 @@ static uint16_t g_cal_digit; static uint32_t g_clk_ref; -static sem_t g_sem_excl = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -573,10 +573,10 @@ static void adc_read_work(struct adc_dev_s *dev) int32_t adc; struct adc_chan_s *priv = (struct adc_chan_s *)dev->ad_priv; - ret = nxsem_wait(&g_sem_excl); + ret = nxmutex_lock(&g_lock); if (ret < 0) { - aerr("Failed to wait sem ret=%d\n", ret); + aerr("Failed to lock ret=%d\n", ret); return ; } @@ -591,7 +591,7 @@ static void adc_read_work(struct adc_dev_s *dev) ainfo("channel: %" PRIu8 ", voltage: %" PRIu32 " mV\n", priv->channel, adc); - nxsem_post(&g_sem_excl); + nxmutex_unlock(&g_lock); } /**************************************************************************** @@ -706,11 +706,11 @@ static int adc_setup(struct adc_dev_s *dev) /* Start calibration only once */ - ret = nxsem_wait(&g_sem_excl); + ret = nxmutex_lock(&g_lock); if (ret < 0) { adc_disable_clk(); - aerr("Failed to wait sem ret=%d\n", ret); + aerr("Failed to lock ret=%d\n", ret); return ret; } @@ -720,7 +720,7 @@ static int adc_setup(struct adc_dev_s *dev) g_calibrated = true; } - nxsem_post(&g_sem_excl); + nxmutex_unlock(&g_lock); /* The ADC device is ready */ diff --git a/arch/risc-v/src/esp32c3/esp32c3_aes.c b/arch/risc-v/src/esp32c3/esp32c3_aes.c index 74558066b26e1..ce9f412552abe 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_aes.c +++ b/arch/risc-v/src/esp32c3/esp32c3_aes.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include "riscv_internal.h" @@ -54,7 +54,7 @@ ****************************************************************************/ static bool g_aes_inited; -static sem_t g_aes_sem = SEM_INITIALIZER(1); +static mutex_t g_aes_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -188,7 +188,7 @@ int esp32c3_aes_ecb_cypher(struct esp32c3_aes_s *aes, bool encrypt, DEBUGASSERT(aes && input && output); DEBUGASSERT(size && ((size % AES_BLK_SIZE) == 0)); - ret = nxsem_wait(&g_aes_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -204,7 +204,7 @@ int esp32c3_aes_ecb_cypher(struct esp32c3_aes_s *aes, bool encrypt, d += AES_BLK_SIZE; } - ret = nxsem_post(&g_aes_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; @@ -246,7 +246,7 @@ int esp32c3_aes_cbc_cypher(struct esp32c3_aes_s *aes, bool encrypt, DEBUGASSERT(aes && input && output && ivptr); DEBUGASSERT(size && ((size % AES_BLK_SIZE) == 0)); - ret = nxsem_wait(&g_aes_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -283,7 +283,7 @@ int esp32c3_aes_cbc_cypher(struct esp32c3_aes_s *aes, bool encrypt, d += AES_BLK_SIZE; } - ret = nxsem_post(&g_aes_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; @@ -328,7 +328,7 @@ int esp32c3_aes_ctr_cypher(struct esp32c3_aes_s *aes, uint32_t *offptr, DEBUGASSERT(aes && offptr && cntptr && cacheptr && input && output); DEBUGASSERT(size); - ret = nxsem_wait(&g_aes_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -359,7 +359,7 @@ int esp32c3_aes_ctr_cypher(struct esp32c3_aes_s *aes, uint32_t *offptr, *offptr = n; - ret = nxsem_post(&g_aes_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; @@ -412,7 +412,7 @@ int esp32c3_aes_xts_cypher(struct esp32c3_aes_xts_s *aes, bool encrypt, DEBUGASSERT((size >= AES_BLK_SIZE) && (size <= ((1 << 20) * AES_BLK_SIZE))); - ret = nxsem_wait(&g_aes_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -476,7 +476,7 @@ int esp32c3_aes_xts_cypher(struct esp32c3_aes_xts_s *aes, bool encrypt, } } - ret = nxsem_post(&g_aes_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; diff --git a/arch/risc-v/src/esp32c3/esp32c3_bignum.c b/arch/risc-v/src/esp32c3/esp32c3_bignum.c index 1faf161f5c41d..7908178f3635d 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_bignum.c +++ b/arch/risc-v/src/esp32c3/esp32c3_bignum.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include "riscv_internal.h" #include "hardware/esp32c3_rsa.h" @@ -75,7 +75,7 @@ * Private Data ****************************************************************************/ -static sem_t g_rsa_sem = SEM_INITIALIZER(1); +static mutex_t g_rsa_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -224,7 +224,7 @@ static void esp32c3_mpi_wait_op_complete(void) static void esp32c3_mpi_enable_hardware_hw_op(void) { - nxsem_wait(&g_rsa_sem); + nxmutex_lock(&g_rsa_lock); /* Enable RSA hardware */ @@ -262,7 +262,7 @@ static void esp32c3_mpi_disable_hardware_hw_op(void) modifyreg32(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_CRYPTO_RSA_CLK_EN, 0); modifyreg32(SYSTEM_PERIP_RST_EN1_REG, 0, SYSTEM_CRYPTO_RSA_RST); - nxsem_post(&g_rsa_sem); + nxmutex_unlock(&g_rsa_lock); } /**************************************************************************** diff --git a/arch/risc-v/src/esp32c3/esp32c3_dma.c b/arch/risc-v/src/esp32c3/esp32c3_dma.c index 4aa2b9dc1b9f5..8309b929344ba 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_dma.c +++ b/arch/risc-v/src/esp32c3/esp32c3_dma.c @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -66,8 +67,8 @@ * Private Data ****************************************************************************/ -static bool g_dma_chan_used[ESP32C3_DMA_CHAN_MAX]; -static sem_t g_dma_exc_sem = SEM_INITIALIZER(1); +static bool g_dma_chan_used[ESP32C3_DMA_CHAN_MAX]; +static mutex_t g_dma_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Public Functions @@ -107,7 +108,7 @@ int32_t esp32c3_dma_request(enum esp32c3_dma_periph_e periph, dmainfo("periph=%" PRIu32 " tx_prio=%" PRIu32 " rx_prio=%" PRIu32 "\n", (uint32_t)periph, tx_prio, rx_prio); - nxsem_wait_uninterruptible(&g_dma_exc_sem); + nxmutex_lock(&g_dma_lock); for (chan = 0; chan < ESP32C3_DMA_CHAN_MAX; chan++) { @@ -122,8 +123,7 @@ int32_t esp32c3_dma_request(enum esp32c3_dma_periph_e periph, { dmaerr("No available GDMA channel for allocation\n"); - nxsem_post(&g_dma_exc_sem); - + nxmutex_unlock(&g_dma_lock); return ERROR; } @@ -170,8 +170,7 @@ int32_t esp32c3_dma_request(enum esp32c3_dma_periph_e periph, SET_REG(DMA_OUT_PRI_CH0_REG, chan, tx_prio); SET_REG(DMA_IN_PRI_CH0_REG, chan, rx_prio); - nxsem_post(&g_dma_exc_sem); - + nxmutex_unlock(&g_dma_lock); return chan; } diff --git a/arch/risc-v/src/esp32c3/esp32c3_i2c.c b/arch/risc-v/src/esp32c3/esp32c3_i2c.c index 64ba8238fb07d..7e73db1513b48 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_i2c.c +++ b/arch/risc-v/src/esp32c3/esp32c3_i2c.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -219,7 +220,7 @@ struct esp32c3_i2c_priv_s const struct esp32c3_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 */ @@ -329,6 +330,10 @@ static struct esp32c3_i2c_priv_s esp32c3_i2c0_priv = .id = 0, .config = &esp32c3_i2c0_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .i2cstate = I2CSTATE_IDLE, .msgv = NULL, .msgid = 0, @@ -898,85 +903,6 @@ static int esp32c3_i2c_polling_waitdone(struct esp32c3_i2c_priv_s *priv) } #endif -/**************************************************************************** - * Name: esp32c3_i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - * Returned Value: - * Zero (OK) is returned on success. A negated errno value is returned on - * failure. - * - ****************************************************************************/ - -static int esp32c3_i2c_sem_wait(struct esp32c3_i2c_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - -/**************************************************************************** - * Name: esp32c3_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void esp32c3_i2c_sem_post(struct esp32c3_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: esp32c3_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void esp32c3_i2c_sem_destroy(struct esp32c3_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - -/**************************************************************************** - * Name: esp32c3_i2c_sem_init - * - * Description: - * Initialize semaphores. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static inline void esp32c3_i2c_sem_init(struct esp32c3_i2c_priv_s *priv) -{ - nxsem_init(&priv->sem_excl, 0, 1); - - /* This semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ -#ifndef CONFIG_I2C_POLLED - nxsem_init(&priv->sem_isr, 0, 0); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); -#endif -} - /**************************************************************************** * Device Driver Operations ****************************************************************************/ @@ -1009,7 +935,7 @@ static int esp32c3_i2c_transfer(struct i2c_master_s *dev, DEBUGASSERT(count > 0); - ret = esp32c3_i2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1116,8 +1042,7 @@ static int esp32c3_i2c_transfer(struct i2c_master_s *dev, /* Dump the trace result */ esp32c3_i2c_tracedump(priv); - - esp32c3_i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -1595,8 +1520,6 @@ struct i2c_master_s *esp32c3_i2cbus_initialize(int port) up_enable_irq(priv->cpuint); #endif - esp32c3_i2c_sem_init(priv); - esp32c3_i2c_init(priv); leave_critical_section(flags); @@ -1652,8 +1575,6 @@ int esp32c3_i2cbus_uninitialize(struct i2c_master_s *dev) esp32c3_i2c_deinit(priv); - esp32c3_i2c_sem_destroy(priv); - return OK; } diff --git a/arch/risc-v/src/esp32c3/esp32c3_rng.c b/arch/risc-v/src/esp32c3/esp32c3_rng.c index 2e04709c24936..e0c0d056e3dae 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_rng.c +++ b/arch/risc-v/src/esp32c3/esp32c3_rng.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include @@ -67,7 +67,7 @@ static ssize_t esp32c3_rng_read(struct file *filep, char *buffer, struct rng_dev_s { uint8_t *rd_buf; - sem_t rd_sem; /* semaphore for read RNG data */ + mutex_t rd_lock; /* mutex for read RNG data */ }; /**************************************************************************** @@ -131,9 +131,7 @@ static int esp32c3_rng_initialize(void) _info("Initializing RNG\n"); memset(&g_rngdev, 0, sizeof(struct rng_dev_s)); - - nxsem_init(&g_rngdev.rd_sem, 0, 1); - nxsem_set_protocol(&g_rngdev.rd_sem, SEM_PRIO_NONE); + nxmutex_init(&g_rngdev.rd_lock); return OK; } @@ -149,7 +147,7 @@ static ssize_t esp32c3_rng_read(struct file *filep, char *buffer, ssize_t read_len; uint8_t *rd_buf = (uint8_t *)buffer; - if (nxsem_wait(&priv->rd_sem) != OK) + if (nxmutex_lock(&priv->rd_lock) != OK) { return -EBUSY; } @@ -168,10 +166,9 @@ static ssize_t esp32c3_rng_read(struct file *filep, char *buffer, buflen -= to_copy; } - /* Release rd_sem for next read */ - - nxsem_post(&priv->rd_sem); + /* Release rd_lock for next read */ + nxmutex_unlock(&priv->rd_lock); return read_len; } diff --git a/arch/risc-v/src/esp32c3/esp32c3_sha.c b/arch/risc-v/src/esp32c3/esp32c3_sha.c index 15c0a3129c0c9..db8ec46d768e2 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_sha.c +++ b/arch/risc-v/src/esp32c3/esp32c3_sha.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include "riscv_internal.h" #include "hardware/esp32c3_sha.h" @@ -105,7 +105,7 @@ ****************************************************************************/ static bool g_sha_inited; -static sem_t g_sha_sem = SEM_INITIALIZER(1); +static mutex_t g_sha_lock = NXMUTEX_INITIALIZER; static const unsigned char esp32c3_sha_padding[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -536,7 +536,7 @@ int esp32c3_sha1_update(struct esp32c3_sha1_context_s *ctx, len = (ilen / 64) * 64; if (len || local_len) { - ret = nxsem_wait(&g_sha_sem); + ret = nxmutex_lock(&g_sha_lock); if (ret < 0) { return ret; @@ -558,7 +558,7 @@ int esp32c3_sha1_update(struct esp32c3_sha1_context_s *ctx, } ret = esp32c3_sha1_block(ctx, input, len, ctx->buffer, local_len); - ret |= nxsem_post(&g_sha_sem); + ret |= nxmutex_unlock(&g_sha_lock); if (ret != 0) { @@ -762,7 +762,7 @@ int esp32c3_sha256_update(struct esp32c3_sha256_context_s *ctx, if (len || local_len) { - ret = nxsem_wait(&g_sha_sem); + ret = nxmutex_lock(&g_sha_lock); if (ret < 0) { return ret; @@ -784,7 +784,7 @@ int esp32c3_sha256_update(struct esp32c3_sha256_context_s *ctx, } ret = esp32c3_sha256_block(ctx, input, len, ctx->buffer, local_len); - ret |= nxsem_post(&g_sha_sem); + ret |= nxmutex_unlock(&g_sha_lock); if (ret != 0) { diff --git a/arch/risc-v/src/esp32c3/esp32c3_spi.c b/arch/risc-v/src/esp32c3/esp32c3_spi.c index 42b2fa3adc046..6056f5ff0a7ab 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_spi.c +++ b/arch/risc-v/src/esp32c3/esp32c3_spi.c @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include @@ -169,7 +169,7 @@ struct esp32c3_spi_priv_s const struct esp32c3_spi_config_s *config; int refs; /* Reference count */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ #ifdef CONFIG_ESP32C3_SPI2_DMA sem_t sem_isr; /* Interrupt wait semaphore */ int cpuint; /* SPI interrupt ID */ @@ -301,14 +301,14 @@ static const struct spi_ops_s esp32c3_spi2_ops = static struct esp32c3_spi_priv_s esp32c3_spi2_priv = { .spi_dev = - { - .ops = &esp32c3_spi2_ops - }, + { + .ops = &esp32c3_spi2_ops + }, .config = &esp32c3_spi2_config, .refs = 0, - .exclsem = SEM_INITIALIZER(0), + .lock = NXMUTEX_INITIALIZER, #ifdef CONFIG_ESP32C3_SPI2_DMA - .sem_isr = SEM_INITIALIZER(0), + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .cpuint = -ENOMEM, .dma_channel = -1, #endif @@ -398,11 +398,11 @@ static int esp32c3_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1215,10 +1215,6 @@ static void esp32c3_spi_init(struct spi_dev_s *dev) const struct esp32c3_spi_config_s *config = priv->config; uint32_t regval; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ - - nxsem_init(&priv->exclsem, 0, 1); - esp32c3_gpiowrite(config->cs_pin, true); esp32c3_gpiowrite(config->mosi_pin, true); esp32c3_gpiowrite(config->miso_pin, true); @@ -1277,9 +1273,6 @@ static void esp32c3_spi_init(struct spi_dev_s *dev) putreg32(VALUE_MASK(0, SPI_CS_HOLD_TIME), SPI_USER1_REG); #ifdef CONFIG_ESP32C3_SPI2_DMA - nxsem_init(&priv->sem_isr, 0, 0); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); - esp32c3_spi_dma_init(dev); #endif @@ -1474,13 +1467,10 @@ int esp32c3_spibus_uninitialize(struct spi_dev_s *dev) up_disable_irq(priv->cpuint); esp32c3_free_cpuint(priv->config->periph); priv->cpuint = -ENOMEM; - - nxsem_destroy(&priv->sem_isr); #endif esp32c3_spi_deinit(dev); - - nxsem_destroy(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } diff --git a/arch/risc-v/src/esp32c3/esp32c3_spiflash_mtd.c b/arch/risc-v/src/esp32c3/esp32c3_spiflash_mtd.c index d10310006564c..7573d38a4497a 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_spiflash_mtd.c +++ b/arch/risc-v/src/esp32c3/esp32c3_spiflash_mtd.c @@ -35,7 +35,7 @@ #include #include -#include +#include #include #include "esp32c3_attr.h" @@ -141,7 +141,7 @@ static const struct esp32c3_mtd_dev_s g_esp32c3_spiflash_encrypt = /* Ensure exclusive access to the driver */ -static sem_t g_exclsem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -182,15 +182,14 @@ static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock, finfo("spi_flash_erase_range(0x%x, %d)\n", offset, nbytes); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_erase_range(offset, nbytes); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -239,17 +238,16 @@ static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset, finfo("spi_flash_read(0x%x, %p, %d)\n", offset, buffer, nbytes); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_read(offset, buffer, nbytes); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -294,15 +292,14 @@ static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock, finfo("spi_flash_read(0x%x, %p, %d)\n", addr, buffer, size); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_read(addr, buffer, size); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -348,17 +345,16 @@ static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev, nbytes); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_read_encrypted(offset, buffer, nbytes); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -405,15 +401,14 @@ static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev, finfo("spi_flash_read_encrypted(0x%x, %p, %d)\n", addr, buffer, size); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_read_encrypted(addr, buffer, size); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -463,17 +458,16 @@ static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset, finfo("spi_flash_write(0x%x, %p, %d)\n", offset, buffer, nbytes); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_write(offset, buffer, nbytes); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -519,15 +513,14 @@ static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock, finfo("spi_flash_write(0x%x, %p, %d)\n", addr, buffer, size); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_write(addr, buffer, size); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -575,15 +568,14 @@ static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev, finfo("spi_flash_write_encrypted(0x%x, %p, %d)\n", addr, buffer, size); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_write_encrypted(addr, buffer, size); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { diff --git a/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.c b/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.c index 1ca6857da5d80..187b385a6d272 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.c +++ b/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include #include #include @@ -417,7 +417,7 @@ static bool g_wifi_irq_bind; static struct work_s g_wifi_evt_work; static sq_queue_t g_wifi_evt_queue; static struct wifi_notify g_wifi_notify[WIFI_ADPT_EVT_MAX]; -static sem_t g_wifiexcl_sem = SEM_INITIALIZER(1); +static mutex_t g_wifiexcl_lock = NXMUTEX_INITIALIZER; /* Wi-Fi adapter reference */ @@ -898,7 +898,7 @@ static int esp_wifi_lock(bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&g_wifiexcl_sem); + ret = nxmutex_lock(&g_wifiexcl_lock); if (ret < 0) { wlinfo("INFO: Failed to lock Wi-Fi ret=%d\n", ret); @@ -906,7 +906,7 @@ static int esp_wifi_lock(bool lock) } else { - ret = nxsem_post(&g_wifiexcl_sem); + ret = nxmutex_unlock(&g_wifiexcl_lock); if (ret < 0) { wlinfo("INFO: Failed to unlock Wi-Fi ret=%d\n", ret); diff --git a/arch/risc-v/src/mpfs/mpfs_emmcsd.c b/arch/risc-v/src/mpfs/mpfs_emmcsd.c index 6b2170bef671c..acf5983ba3551 100644 --- a/arch/risc-v/src/mpfs/mpfs_emmcsd.c +++ b/arch/risc-v/src/mpfs/mpfs_emmcsd.c @@ -352,8 +352,6 @@ struct mpfs_dev_s /* Low-level helper ********************************************************/ -#define mpfs_givesem(priv) (nxsem_post(&priv->waitsem)) - /* Mutual exclusion */ #if defined(CONFIG_SDIO_MUXBUS) @@ -472,27 +470,6 @@ struct mpfs_dev_s g_emmcsd_dev = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: mpfs_takesem - * - * Description: - * Take the wait semaphore (handling false alarm wakeups due to the receipt - * of signals). - * - * Input Parameters: - * priv - Instance of the EMMCSD private state structure. - * - * Returned Value: - * Normally OK, but may return -ECANCELED in the rare event that the task - * has been canceled. - * - ****************************************************************************/ - -static int mpfs_takesem(struct mpfs_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - /**************************************************************************** * Name: mpfs_reset_lines * @@ -931,7 +908,7 @@ static void mpfs_endwait(struct mpfs_dev_s *priv, /* Wake up the waiting thread */ - mpfs_givesem(priv); + nxsem_post(&priv->waitsem); } /**************************************************************************** @@ -2761,7 +2738,7 @@ static sdio_eventset_t mpfs_eventwait(struct sdio_dev_s *dev) * incremented and there will be no wait. */ - ret = mpfs_takesem(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { /* Task canceled. Cancel the wdog (assuming it was started) and diff --git a/arch/risc-v/src/mpfs/mpfs_i2c.c b/arch/risc-v/src/mpfs/mpfs_i2c.c index 8f04a33dee7bc..937fa6b878bc5 100644 --- a/arch/risc-v/src/mpfs/mpfs_i2c.c +++ b/arch/risc-v/src/mpfs/mpfs_i2c.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -134,7 +135,7 @@ struct mpfs_i2c_priv_s uint8_t ser_address; /* Own i2c address */ uint8_t target_addr; /* Target i2c address */ - sem_t sem_excl; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ sem_t sem_isr; /* Interrupt wait semaphore */ int refs; /* Reference count */ @@ -162,6 +163,8 @@ static struct mpfs_i2c_priv_s g_mpfs_i2c0_lo_priv = .frequency = 0, .ser_address = 0x21, .target_addr = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .refs = 0, .tx_size = 0, .tx_idx = 0, @@ -183,6 +186,8 @@ static struct mpfs_i2c_priv_s g_mpfs_i2c1_lo_priv = .frequency = 0, .ser_address = 0x21, .target_addr = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .refs = 0, .tx_size = 0, .tx_idx = 0, @@ -343,82 +348,6 @@ static int mpfs_i2c_sem_waitdone(struct mpfs_i2c_priv_s *priv) return nxsem_tickwait_uninterruptible(&priv->sem_isr, SEC2TICK(10)); } -/**************************************************************************** - * Name: mpfs_i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - * Returned Value: - * Zero (OK) is returned on success. A negated errno value is returned on - * failure. - * - ****************************************************************************/ - -static int mpfs_i2c_sem_wait(struct mpfs_i2c_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - -/**************************************************************************** - * Name: mpfs_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void mpfs_i2c_sem_post(struct mpfs_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: mpfs_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void mpfs_i2c_sem_destroy(struct mpfs_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); - nxsem_destroy(&priv->sem_isr); -} - -/**************************************************************************** - * Name: mpfs_i2c_sem_init - * - * Description: - * Initialize semaphores. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static inline void mpfs_i2c_sem_init(struct mpfs_i2c_priv_s *priv) -{ - nxsem_init(&priv->sem_excl, 0, 1); - - /* 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); -} - /**************************************************************************** * Name: mpfs_i2c_irq * @@ -632,7 +561,7 @@ static int mpfs_i2c_transfer(struct i2c_master_s *dev, i2cinfo("Starting transfer request of %d message(s):\n", count); DEBUGASSERT(count > 0); - ret = mpfs_i2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -716,8 +645,7 @@ static int mpfs_i2c_transfer(struct i2c_master_s *dev, i2cinfo("Message %" PRIu8 " transfer complete.\n", priv->msgid); } - mpfs_i2c_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -891,7 +819,6 @@ struct i2c_master_s *mpfs_i2cbus_initialize(int port) return NULL; } - mpfs_i2c_sem_init(priv); ret = mpfs_i2c_init(priv); if (ret != OK) { @@ -945,7 +872,6 @@ int mpfs_i2cbus_uninitialize(struct i2c_master_s *dev) leave_critical_section(flags); mpfs_i2c_deinit(priv); - mpfs_i2c_sem_destroy(priv); return OK; } diff --git a/arch/risc-v/src/mpfs/mpfs_spi.c b/arch/risc-v/src/mpfs/mpfs_spi.c index 4c3fa75fc0179..b364cd5887334 100644 --- a/arch/risc-v/src/mpfs/mpfs_spi.c +++ b/arch/risc-v/src/mpfs/mpfs_spi.c @@ -35,7 +35,7 @@ #include #include -#include +#include #include #include @@ -119,7 +119,7 @@ struct mpfs_spi_priv_s const int id; /* SPI0 or SPI1 id */ uint32_t devid; /* SPI CS device 0..7 */ - sem_t sem_excl; /* Bus usage semaphore */ + mutex_t lock; /* Bus usage mutex */ sem_t sem_isr; /* Interrupt wait semaphore */ uint32_t frequency; /* Requested clock frequency */ @@ -224,14 +224,16 @@ static const struct spi_ops_s mpfs_spi0_ops = static struct mpfs_spi_priv_s g_mpfs_spi0_priv = { .spi_dev = - { - .ops = &mpfs_spi0_ops - }, + { + .ops = &mpfs_spi0_ops + }, .config = &mpfs_spi_config, .hw_base = MPFS_SPI0_LO_BASE, .plic_irq = MPFS_IRQ_SPI0, .id = 0, - .devid = 0 + .devid = 0, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), }; #endif /* CONFIG_MPFS_SPI0 */ @@ -268,14 +270,16 @@ static const struct spi_ops_s mpfs_spi1_ops = static struct mpfs_spi_priv_s g_mpfs_spi1_priv = { .spi_dev = - { - .ops = &mpfs_spi1_ops - }, + { + .ops = &mpfs_spi1_ops + }, .config = &mpfs_spi_config, .hw_base = MPFS_SPI1_LO_BASE, .plic_irq = MPFS_IRQ_SPI1, .id = 1, - .devid = 1 + .devid = 1, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), }; #endif /* CONFIG_MPFS_SPI1 */ @@ -361,11 +365,11 @@ static int mpfs_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->sem_excl); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->sem_excl); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1383,13 +1387,6 @@ static void mpfs_spi_init(struct spi_dev_s *dev) struct mpfs_spi_priv_s *priv = (struct mpfs_spi_priv_s *)dev; const struct mpfs_spi_config_s *config = priv->config; - /* Initialize the SPI semaphore for mutually exclusive access */ - - nxsem_init(&priv->sem_excl, 0, 1); - - nxsem_init(&priv->sem_isr, 0, 0); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); - up_disable_irq(priv->plic_irq); /* Perform reset and enable clocks */ @@ -1569,9 +1566,6 @@ int mpfs_spibus_uninitialize(struct spi_dev_s *dev) mpfs_spi_deinit(dev); - nxsem_destroy(&priv->sem_excl); - nxsem_destroy(&priv->sem_isr); - return OK; } diff --git a/arch/x86_64/src/intel64/intel64_rng.c b/arch/x86_64/src/intel64/intel64_rng.c index f3113098f468f..5440c0d5de24a 100644 --- a/arch/x86_64/src/intel64/intel64_rng.c +++ b/arch/x86_64/src/intel64/intel64_rng.c @@ -54,7 +54,6 @@ static ssize_t x86_rngread(struct file *filep, char *buffer, size_t); struct rng_dev_s { - sem_t rd_devsem; /* Threads can only exclusively access the RNG */ sem_t rd_readsem; /* To block until the buffer is filled NOT used */ }; @@ -91,9 +90,6 @@ static int x86_rng_initialize(void) _info("Initializing RNG\n"); memset(&g_rngdev, 0, sizeof(struct rng_dev_s)); - - nxsem_init(&g_rngdev.rd_devsem, 0, 1); - return OK; } diff --git a/arch/xtensa/src/common/xtensa_hostfs.c b/arch/xtensa/src/common/xtensa_hostfs.c index 39fce29aa7420..0122970b904b7 100644 --- a/arch/xtensa/src/common/xtensa_hostfs.c +++ b/arch/xtensa/src/common/xtensa_hostfs.c @@ -152,7 +152,7 @@ int host_fstat(int fd, struct stat *buf) * Assumptions: * - host_lseek never fails * - It's ok to change the file offset temporarily as - * hostfs_semtake provides enough serialization. + * hostfs_lock provides enough serialization. */ off_t saved_off = host_lseek(fd, 0, SEEK_CUR); diff --git a/arch/xtensa/src/esp32/esp32_aes.c b/arch/xtensa/src/esp32/esp32_aes.c index 3c644c7e29632..6a2885f35c7d3 100644 --- a/arch/xtensa/src/esp32/esp32_aes.c +++ b/arch/xtensa/src/esp32/esp32_aes.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include "xtensa.h" @@ -48,7 +48,7 @@ ****************************************************************************/ static bool g_aes_inited; -static sem_t g_aes_sem = SEM_INITIALIZER(1); +static mutex_t g_aes_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -182,7 +182,7 @@ int esp32_aes_ecb_cypher(struct esp32_aes_s *aes, bool encrypt, DEBUGASSERT(aes && input && output); DEBUGASSERT(size && ((size % AES_BLK_SIZE) == 0)); - ret = nxsem_wait(&g_aes_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -198,7 +198,7 @@ int esp32_aes_ecb_cypher(struct esp32_aes_s *aes, bool encrypt, d += AES_BLK_SIZE; } - ret = nxsem_post(&g_aes_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; @@ -239,7 +239,7 @@ int esp32_aes_cbc_cypher(struct esp32_aes_s *aes, bool encrypt, void *ivptr, DEBUGASSERT(aes && input && output && ivptr); DEBUGASSERT(size && ((size % AES_BLK_SIZE) == 0)); - ret = nxsem_wait(&g_aes_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -276,7 +276,7 @@ int esp32_aes_cbc_cypher(struct esp32_aes_s *aes, bool encrypt, void *ivptr, d += AES_BLK_SIZE; } - ret = nxsem_post(&g_aes_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; @@ -321,7 +321,7 @@ int esp32_aes_ctr_cypher(struct esp32_aes_s *aes, uint32_t *offptr, DEBUGASSERT(aes && offptr && cntptr && cacheptr && input && output); DEBUGASSERT(size); - ret = nxsem_wait(&g_aes_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -352,7 +352,7 @@ int esp32_aes_ctr_cypher(struct esp32_aes_s *aes, uint32_t *offptr, *offptr = n; - ret = nxsem_post(&g_aes_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; @@ -405,7 +405,7 @@ int esp32_aes_xts_cypher(struct esp32_aes_xts_s *aes, bool encrypt, DEBUGASSERT((size >= AES_BLK_SIZE) && (size <= ((1 << 20) * AES_BLK_SIZE))); - ret = nxsem_wait(&g_aes_sem); + ret = nxmutex_lock(&g_aes_lock); if (ret < 0) { return ret; @@ -469,7 +469,7 @@ int esp32_aes_xts_cypher(struct esp32_aes_xts_s *aes, bool encrypt, } } - ret = nxsem_post(&g_aes_sem); + ret = nxmutex_unlock(&g_aes_lock); if (ret < 0) { return ret; diff --git a/arch/xtensa/src/esp32/esp32_himem.c b/arch/xtensa/src/esp32/esp32_himem.c index c25eeadb9d864..7559444880688 100644 --- a/arch/xtensa/src/esp32/esp32_himem.c +++ b/arch/xtensa/src/esp32/esp32_himem.c @@ -110,13 +110,6 @@ static ssize_t himem_write(struct file *filep, const char *buffer, static int himem_ioctl(struct file *filep, int cmd, unsigned long arg); -/* This structure is used only for access control */ - -struct himem_access_s -{ - sem_t exclsem; /* Supports mutual exclusion */ -}; - /* Metadata for a block of physical RAM */ typedef struct @@ -223,7 +216,6 @@ size_t esp_himem_reserved_area_size(void) int esp_himem_init(void) { - struct himem_access_s *priv; int paddr_start = (4096 * 1024) - (CACHE_BLOCKSIZE * SPIRAM_BANKSWITCH_RESERVE); int paddr_end; @@ -235,17 +227,6 @@ int esp_himem_init(void) return -ENODEV; } - /* Allocate a new himem access instance */ - - priv = (struct himem_access_s *) - kmm_zalloc(sizeof(struct himem_access_s)); - - if (!priv) - { - merr("ERROR: Failed to allocate device structure\n"); - return -ENOMEM; - } - maxram = esp_spiram_get_size(); /* Catch double init */ @@ -285,11 +266,10 @@ int esp_himem_init(void) /* Register the character driver */ - ret = register_driver("/dev/himem", &g_himemfops, 0666, priv); + ret = register_driver("/dev/himem", &g_himemfops, 0666, NULL); if (ret < 0) { merr("ERROR: Failed to register driver: %d\n", ret); - kmm_free(priv); } minfo("Initialized. Using last %d 32KB address blocks for bank \ diff --git a/arch/xtensa/src/esp32/esp32_i2c.c b/arch/xtensa/src/esp32/esp32_i2c.c index 6dea1b053cd3f..50a76555d2aac 100644 --- a/arch/xtensa/src/esp32/esp32_i2c.c +++ b/arch/xtensa/src/esp32/esp32_i2c.c @@ -40,8 +40,8 @@ #include #include #include +#include #include -#include #include #include @@ -200,7 +200,7 @@ struct esp32_i2c_priv_s const struct esp32_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 */ @@ -223,8 +223,6 @@ struct esp32_i2c_priv_s uint32_t clk_freq; /* Current I2C Clock frequency */ - spinlock_t lock; /* Device specific lock */ - /* I2C trace support */ #ifdef CONFIG_I2C_TRACE @@ -308,6 +306,10 @@ static struct esp32_i2c_priv_s esp32_i2c0_priv = .ops = &esp32_i2c_ops, .config = &esp32_i2c0_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .i2cstate = I2CSTATE_IDLE, .msgv = NULL, .msgid = 0, @@ -340,6 +342,10 @@ static struct esp32_i2c_priv_s esp32_i2c1_priv = .ops = &esp32_i2c_ops, .config = &esp32_i2c1_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .i2cstate = I2CSTATE_IDLE, .msgv = NULL, .msgid = 0, @@ -829,70 +835,6 @@ static int esp32_i2c_polling_waitdone(struct esp32_i2c_priv_s *priv) } #endif -/**************************************************************************** - * Name: esp32_i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary - * - ****************************************************************************/ - -static int esp32_i2c_sem_wait(struct esp32_i2c_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - -/**************************************************************************** - * Name: esp32_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static void esp32_i2c_sem_post(struct esp32_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: esp32_i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - ****************************************************************************/ - -static void esp32_i2c_sem_destroy(struct esp32_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - -/**************************************************************************** - * Name: esp32_i2c_sem_init - * - * Description: - * Initialize semaphores - * - ****************************************************************************/ - -static inline void esp32_i2c_sem_init(struct esp32_i2c_priv_s *priv) -{ - nxsem_init(&priv->sem_excl, 0, 1); - - /* This semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ - -#ifndef CONFIG_I2C_POLLED - nxsem_init(&priv->sem_isr, 0, 0); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); -#endif -} - /**************************************************************************** * Device Driver Operations ****************************************************************************/ @@ -915,7 +857,7 @@ static int esp32_i2c_transfer(struct i2c_master_s *dev, DEBUGASSERT(count > 0); - ret = esp32_i2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1017,8 +959,7 @@ static int esp32_i2c_transfer(struct i2c_master_s *dev, esp32_i2c_tracedump(priv); - esp32_i2c_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -1133,19 +1074,15 @@ static int esp32_i2c_clear_bus(struct esp32_i2c_priv_s *priv) #ifdef CONFIG_I2C_RESET static int esp32_i2c_reset(struct i2c_master_s *dev) { - irqstate_t flags; struct esp32_i2c_priv_s *priv = (struct esp32_i2c_priv_s *)dev; DEBUGASSERT(dev); - DEBUGASSERT(priv->refs > 0); - flags = spin_lock_irqsave(&priv->lock); + nxmutex_lock(&priv->lock); esp32_i2c_deinit(priv); - esp32_i2c_clear_bus(priv); - esp32_i2c_init(priv); priv->i2cstate = I2CSTATE_IDLE; @@ -1153,8 +1090,7 @@ static int esp32_i2c_reset(struct i2c_master_s *dev) priv->bytes = 0; priv->ready_read = 0; - spin_unlock_irqrestore(&priv->lock, flags); - + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -1498,7 +1434,6 @@ static inline void esp32_i2c_process(struct esp32_i2c_priv_s *priv, struct i2c_master_s *esp32_i2cbus_initialize(int port) { - irqstate_t flags; struct esp32_i2c_priv_s *priv; #ifndef CONFIG_I2C_POLLED const struct esp32_i2c_config_s *config; @@ -1521,12 +1456,10 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port) return NULL; } - flags = spin_lock_irqsave(&priv->lock); - - if ((volatile int)priv->refs++ != 0) + nxmutex_lock(&priv->lock); + if (priv->refs++ != 0) { - spin_unlock_irqrestore(&priv->lock, flags); - + nxmutex_unlock(&priv->lock); return (struct i2c_master_s *)priv; } @@ -1542,7 +1475,8 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port) { /* Failed to allocate a CPU interrupt of this type */ - spin_unlock_irqrestore(&priv->lock, flags); + priv->refs--; + nxmutex_unlock(&priv->lock); return NULL; } @@ -1551,20 +1485,17 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port) if (ret != OK) { esp32_teardown_irq(priv->cpu, config->periph, priv->cpuint); + priv->refs--; - spin_unlock_irqrestore(&priv->lock, flags); - + nxmutex_unlock(&priv->lock); return NULL; } up_enable_irq(config->irq); #endif - esp32_i2c_sem_init(priv); - esp32_i2c_init(priv); - - spin_unlock_irqrestore(&priv->lock, flags); + nxmutex_unlock(&priv->lock); return (struct i2c_master_s *)priv; } @@ -1579,7 +1510,6 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port) int esp32_i2cbus_uninitialize(struct i2c_master_s *dev) { - irqstate_t flags; struct esp32_i2c_priv_s *priv = (struct esp32_i2c_priv_s *)dev; DEBUGASSERT(dev); @@ -1589,24 +1519,20 @@ int esp32_i2cbus_uninitialize(struct i2c_master_s *dev) return ERROR; } - flags = spin_lock_irqsave(&priv->lock); - + nxmutex_lock(&priv->lock); if (--priv->refs) { - spin_unlock_irqrestore(&priv->lock, flags); + nxmutex_unlock(&priv->lock); return OK; } - spin_unlock_irqrestore(&priv->lock, flags); - #ifndef CONFIG_I2C_POLLED up_disable_irq(priv->config->irq); esp32_teardown_irq(priv->cpu, priv->config->periph, priv->cpuint); #endif esp32_i2c_deinit(priv); - - esp32_i2c_sem_destroy(priv); + nxmutex_unlock(&priv->lock); return OK; } diff --git a/arch/xtensa/src/esp32/esp32_rng.c b/arch/xtensa/src/esp32/esp32_rng.c index 627aec683f00a..48cf1085fc2a9 100644 --- a/arch/xtensa/src/esp32/esp32_rng.c +++ b/arch/xtensa/src/esp32/esp32_rng.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include @@ -68,7 +68,7 @@ static ssize_t esp32_rng_read(struct file *filep, char *buffer, struct rng_dev_s { uint8_t *rd_buf; - sem_t rd_sem; /* semaphore for read RNG data */ + mutex_t rd_lock; /* mutex for read RNG data */ }; /**************************************************************************** @@ -131,9 +131,7 @@ static int esp32_rng_initialize(void) _info("Initializing RNG\n"); memset(&g_rngdev, 0, sizeof(struct rng_dev_s)); - - nxsem_init(&g_rngdev.rd_sem, 0, 1); - nxsem_set_protocol(&g_rngdev.rd_sem, SEM_PRIO_NONE); + nxmutex_init(&g_rngdev.rd_lock); return OK; } @@ -149,7 +147,7 @@ static ssize_t esp32_rng_read(struct file *filep, char *buffer, ssize_t read_len; uint8_t *rd_buf = (uint8_t *)buffer; - if (nxsem_wait(&priv->rd_sem) != OK) + if (nxmutex_lock(&priv->rd_lock) != OK) { return -EBUSY; } @@ -168,10 +166,9 @@ static ssize_t esp32_rng_read(struct file *filep, char *buffer, buflen -= to_copy; } - /* Release rd_sem for next read */ - - nxsem_post(&priv->rd_sem); + /* Release rd_lock for next read */ + nxmutex_unlock(&priv->rd_lock); return read_len; } diff --git a/arch/xtensa/src/esp32/esp32_spi.c b/arch/xtensa/src/esp32/esp32_spi.c index d5518b9913307..e8e5ac368c199 100644 --- a/arch/xtensa/src/esp32/esp32_spi.c +++ b/arch/xtensa/src/esp32/esp32_spi.c @@ -38,8 +38,8 @@ #include #include #include +#include #include -#include #include #include @@ -146,7 +146,7 @@ struct esp32_spi_priv_s /* Held while chip is selected for mutual exclusion */ - sem_t exclsem; + mutex_t lock; /* Interrupt wait semaphore */ @@ -163,8 +163,6 @@ struct esp32_spi_priv_s /* Actual SPI send/receive bits once transmission */ uint8_t nbits; - - spinlock_t lock; /* Device specific lock. */ }; /**************************************************************************** @@ -274,10 +272,12 @@ static const struct spi_ops_s esp32_spi2_ops = static struct esp32_spi_priv_s esp32_spi2_priv = { .spi_dev = - { - .ops = &esp32_spi2_ops - }, - .config = &esp32_spi2_config + { + .ops = &esp32_spi2_ops + }, + .config = &esp32_spi2_config, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), }; #endif /* CONFIG_ESP32_SPI2 */ @@ -348,10 +348,12 @@ static const struct spi_ops_s esp32_spi3_ops = static struct esp32_spi_priv_s esp32_spi3_priv = { .spi_dev = - { - .ops = &esp32_spi3_ops - }, - .config = &esp32_spi3_config + { + .ops = &esp32_spi3_ops + }, + .config = &esp32_spi3_config, + .lock = NXMUTEX_INITIALIZER, + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), }; #endif /* CONFIG_ESP32_SPI3 */ @@ -477,11 +479,11 @@ static int esp32_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1267,10 +1269,6 @@ static void esp32_spi_init(struct spi_dev_s *dev) const struct esp32_spi_config_s *config = priv->config; uint32_t regval; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ - - nxsem_init(&priv->exclsem, 0, 1); - esp32_gpiowrite(config->cs_pin, 1); esp32_gpiowrite(config->mosi_pin, 1); esp32_gpiowrite(config->miso_pin, 1); @@ -1331,9 +1329,6 @@ static void esp32_spi_init(struct spi_dev_s *dev) if (config->use_dma) { - nxsem_init(&priv->sem_isr, 0, 0); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); - modifyreg32(DPORT_PERIP_CLK_EN_REG, 0, config->dma_clk_bit); modifyreg32(DPORT_PERIP_RST_EN_REG, config->dma_rst_bit, 0); @@ -1426,7 +1421,6 @@ struct spi_dev_s *esp32_spibus_initialize(int port) int ret; struct spi_dev_s *spi_dev; struct esp32_spi_priv_s *priv; - irqstate_t flags; switch (port) { @@ -1444,13 +1438,13 @@ struct spi_dev_s *esp32_spibus_initialize(int port) return NULL; } - flags = spin_lock_irqsave(&priv->lock); + nxmutex_lock(&priv->lock); spi_dev = (struct spi_dev_s *)priv; - - if ((volatile int)priv->refs != 0) + if (priv->refs != 0) { - spin_unlock_irqrestore(&priv->lock, flags); + priv->refs++; + nxmutex_unlock(&priv->lock); return spi_dev; } @@ -1464,7 +1458,7 @@ struct spi_dev_s *esp32_spibus_initialize(int port) 1, ESP32_CPUINT_LEVEL); if (priv->cpuint < 0) { - spin_unlock_irqrestore(&priv->lock, flags); + nxmutex_unlock(&priv->lock); return NULL; } @@ -1474,7 +1468,7 @@ struct spi_dev_s *esp32_spibus_initialize(int port) esp32_teardown_irq(priv->cpu, priv->config->periph, priv->cpuint); - spin_unlock_irqrestore(&priv->lock, flags); + nxmutex_unlock(&priv->lock); return NULL; } @@ -1482,11 +1476,9 @@ struct spi_dev_s *esp32_spibus_initialize(int port) } esp32_spi_init(spi_dev); - priv->refs++; - spin_unlock_irqrestore(&priv->lock, flags); - + nxmutex_unlock(&priv->lock); return spi_dev; } @@ -1500,7 +1492,6 @@ struct spi_dev_s *esp32_spibus_initialize(int port) int esp32_spibus_uninitialize(struct spi_dev_s *dev) { - irqstate_t flags; struct esp32_spi_priv_s *priv = (struct esp32_spi_priv_s *)dev; DEBUGASSERT(dev); @@ -1510,29 +1501,23 @@ int esp32_spibus_uninitialize(struct spi_dev_s *dev) return ERROR; } - flags = spin_lock_irqsave(&priv->lock); - + nxmutex_lock(&priv->lock); if (--priv->refs != 0) { - spin_unlock_irqrestore(&priv->lock, flags); + nxmutex_unlock(&priv->lock); return OK; } - spin_unlock_irqrestore(&priv->lock, flags); - if (priv->config->use_dma) { up_disable_irq(priv->config->irq); esp32_teardown_irq(priv->cpu, priv->config->periph, priv->cpuint); - - nxsem_destroy(&priv->sem_isr); } esp32_spi_deinit(dev); - - nxsem_destroy(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } diff --git a/arch/xtensa/src/esp32/esp32_spiflash.c b/arch/xtensa/src/esp32/esp32_spiflash.c index 63fd688fc92f3..769c0f6684708 100644 --- a/arch/xtensa/src/esp32/esp32_spiflash.c +++ b/arch/xtensa/src/esp32/esp32_spiflash.c @@ -36,7 +36,7 @@ #include #include -#include +#include #include #include "xtensa.h" @@ -302,7 +302,7 @@ static struct esp32_spiflash_s g_esp32_spiflash1_encrypt = /* Enxusre exculisve access to the driver */ -static sem_t g_exclsem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -1499,7 +1499,7 @@ static int esp32_erase(struct mtd_dev_s *dev, off_t startblock, finfo("esp32_erase(%p, %d, %d)\n", dev, startblock, nblocks); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1507,8 +1507,7 @@ static int esp32_erase(struct mtd_dev_s *dev, off_t startblock, ret = esp32_erasesector(priv, addr, size); - nxsem_post(&g_exclsem); - + nxmutex_unlock(&g_lock); if (ret == OK) { ret = nblocks; @@ -1548,9 +1547,9 @@ static ssize_t esp32_read(struct mtd_dev_s *dev, off_t offset, finfo("esp32_read(%p, 0x%x, %d, %p)\n", dev, offset, nbytes, buffer); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1559,8 +1558,7 @@ static ssize_t esp32_read(struct mtd_dev_s *dev, off_t offset, esp32_set_read_opt(priv); ret = esp32_readdata(priv, offset, buffer, nbytes); - nxsem_post(&g_exclsem); - + nxmutex_unlock(&g_lock); if (ret == OK) { ret = nbytes; @@ -1648,9 +1646,9 @@ static ssize_t esp32_read_decrypt(struct mtd_dev_s *dev, dev, offset, nbytes, buffer); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1658,8 +1656,7 @@ static ssize_t esp32_read_decrypt(struct mtd_dev_s *dev, ret = esp32_readdata_encrypted(priv, offset, tmpbuff, nbytes); - nxsem_post(&g_exclsem); - + nxmutex_unlock(&g_lock); if (ret == OK) { ret = nbytes; @@ -1751,9 +1748,9 @@ static ssize_t esp32_write(struct mtd_dev_s *dev, off_t offset, finfo("esp32_write(%p, 0x%x, %d, %p)\n", dev, offset, nbytes, buffer); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1761,8 +1758,7 @@ static ssize_t esp32_write(struct mtd_dev_s *dev, off_t offset, ret = esp32_writedata(priv, offset, buffer, nbytes); - nxsem_post(&g_exclsem); - + nxmutex_unlock(&g_lock); if (ret == OK) { ret = nbytes; @@ -1852,7 +1848,7 @@ static ssize_t esp32_bwrite_encrypt(struct mtd_dev_s *dev, dev, startblock, nblocks, buffer); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1860,8 +1856,7 @@ static ssize_t esp32_bwrite_encrypt(struct mtd_dev_s *dev, ret = esp32_writedata_encrypted(priv, addr, buffer, size); - nxsem_post(&g_exclsem); - + nxmutex_unlock(&g_lock); if (ret == OK) { ret = nblocks; diff --git a/arch/xtensa/src/esp32/esp32_wifi_adapter.c b/arch/xtensa/src/esp32/esp32_wifi_adapter.c index bb52f609c44f9..70b49b11179f3 100644 --- a/arch/xtensa/src/esp32/esp32_wifi_adapter.c +++ b/arch/xtensa/src/esp32/esp32_wifi_adapter.c @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include #include #include @@ -382,7 +382,7 @@ void intr_matrix_set(int cpu_no, uint32_t model_num, uint32_t intr_num); static struct work_s g_wifi_evt_work; static sq_queue_t g_wifi_evt_queue; static struct wifi_notify g_wifi_notify[WIFI_ADPT_EVT_MAX]; -static sem_t g_wifiexcl_sem = SEM_INITIALIZER(1); +static mutex_t g_wifiexcl_lock = NXMUTEX_INITIALIZER; /* Callback function to update Wi-Fi MAC time */ @@ -779,7 +779,7 @@ static int esp_wifi_lock(bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&g_wifiexcl_sem); + ret = nxmutex_lock(&g_wifiexcl_lock); if (ret < 0) { wlinfo("Failed to lock Wi-Fi ret=%d\n", ret); @@ -787,7 +787,7 @@ static int esp_wifi_lock(bool lock) } else { - ret = nxsem_post(&g_wifiexcl_sem); + ret = nxmutex_unlock(&g_wifiexcl_lock); if (ret < 0) { wlinfo("Failed to unlock Wi-Fi ret=%d\n", ret); diff --git a/arch/xtensa/src/esp32s2/esp32s2_i2c.c b/arch/xtensa/src/esp32s2/esp32s2_i2c.c index 6fde13a276e6d..4b2b89fe71f37 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_i2c.c +++ b/arch/xtensa/src/esp32s2/esp32s2_i2c.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -210,7 +211,7 @@ struct esp32s2_i2c_priv_s const struct esp32s2_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 */ @@ -256,10 +257,6 @@ static int i2c_transfer(struct i2c_master_s *dev, struct i2c_msg_s *msgs, int count); static inline void i2c_process(struct esp32s2_i2c_priv_s *priv, uint32_t status); -static void i2c_sem_init(struct esp32s2_i2c_priv_s *priv); -static void i2c_sem_destroy(struct esp32s2_i2c_priv_s *priv); -static void i2c_sem_post(struct esp32s2_i2c_priv_s *priv); -static int i2c_sem_wait(struct esp32s2_i2c_priv_s *priv); #ifndef CONFIG_I2C_POLLED static int i2c_sem_waitdone(struct esp32s2_i2c_priv_s *priv); #endif @@ -322,6 +319,10 @@ static struct esp32s2_i2c_priv_s g_esp32s2_i2c0_priv = .id = ESP32S2_I2C0, .config = &g_esp32s2_i2c0_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .i2cstate = I2CSTATE_IDLE, .msgv = NULL, .msgid = 0, @@ -354,6 +355,10 @@ static struct esp32s2_i2c_priv_s g_esp32s2_i2c1_priv = .id = ESP32S2_I2C1, .config = &g_esp32s2_i2c1_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .i2cstate = I2CSTATE_IDLE, .msgv = NULL, .msgid = 0, @@ -911,86 +916,6 @@ static int i2c_polling_waitdone(struct esp32s2_i2c_priv_s *priv) } #endif -/**************************************************************************** - * Name: i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - * Returned Value: - * Zero (OK) is returned on success. A negated errno value is returned on - * failure. - * - ****************************************************************************/ - -static int i2c_sem_wait(struct esp32s2_i2c_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - -/**************************************************************************** - * Name: i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void i2c_sem_post(struct esp32s2_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void i2c_sem_destroy(struct esp32s2_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - -/**************************************************************************** - * Name: i2c_sem_init - * - * Description: - * Initialize semaphores. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void i2c_sem_init(struct esp32s2_i2c_priv_s *priv) -{ - nxsem_init(&priv->sem_excl, 0, 1); - - /* This semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ - -#ifndef CONFIG_I2C_POLLED - nxsem_init(&priv->sem_isr, 0, 0); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); -#endif -} - /**************************************************************************** * Device Driver Operations ****************************************************************************/ @@ -1020,7 +945,7 @@ static int i2c_transfer(struct i2c_master_s *dev, struct i2c_msg_s *msgs, DEBUGASSERT(count > 0); - ret = i2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1147,8 +1072,7 @@ static int i2c_transfer(struct i2c_master_s *dev, struct i2c_msg_s *msgs, i2c_tracedump(priv); - i2c_sem_post(priv); - + nxmutex_unlock(&priv->lock); return ret; } @@ -1640,8 +1564,6 @@ struct i2c_master_s *esp32s2_i2cbus_initialize(int port) up_enable_irq(config->irq); #endif - i2c_sem_init(priv); - i2c_init(priv); leave_critical_section(flags); @@ -1694,8 +1616,6 @@ int esp32s2_i2cbus_uninitialize(struct i2c_master_s *dev) i2c_deinit(priv); - i2c_sem_destroy(priv); - return OK; } diff --git a/arch/xtensa/src/esp32s2/esp32s2_rng.c b/arch/xtensa/src/esp32s2/esp32s2_rng.c index e7c506f0dba85..c9ef15c77d3ca 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_rng.c +++ b/arch/xtensa/src/esp32s2/esp32s2_rng.c @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include @@ -70,7 +70,7 @@ static ssize_t esp32s2_rng_read(struct file *filep, char *buffer, struct rng_dev_s { uint8_t *rd_buf; - sem_t rd_sem; /* semaphore for read RNG data */ + mutex_t rd_lock; /* mutex for read RNG data */ }; /**************************************************************************** @@ -129,9 +129,7 @@ static int esp32s2_rng_initialize(void) _info("Initializing RNG\n"); memset(&g_rngdev, 0, sizeof(struct rng_dev_s)); - - nxsem_init(&g_rngdev.rd_sem, 0, 1); - nxsem_set_protocol(&g_rngdev.rd_sem, SEM_PRIO_NONE); + nxmutex_init(&g_rngdev.rd_lock); return OK; } @@ -147,7 +145,7 @@ static ssize_t esp32s2_rng_read(struct file *filep, char *buffer, ssize_t read_len; uint8_t *rd_buf = (uint8_t *)buffer; - if (nxsem_wait(&priv->rd_sem) != OK) + if (nxmutex_lock(&priv->rd_lock) != OK) { return -EBUSY; } @@ -166,10 +164,9 @@ static ssize_t esp32s2_rng_read(struct file *filep, char *buffer, buflen -= to_copy; } - /* Release rd_sem for next read */ - - nxsem_post(&priv->rd_sem); + /* Release rd_lock for next read */ + nxmutex_unlock(&priv->rd_lock); return read_len; } diff --git a/arch/xtensa/src/esp32s2/esp32s2_spi.c b/arch/xtensa/src/esp32s2/esp32s2_spi.c index d86f418846cdb..1cbc6a0fd60e7 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_spi.c +++ b/arch/xtensa/src/esp32s2/esp32s2_spi.c @@ -38,8 +38,7 @@ #include #include #include -#include -#include +#include #include #include @@ -126,7 +125,7 @@ struct esp32s2_spi_priv_s const struct esp32s2_spi_config_s *config; int refs; /* Reference count */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ enum spi_mode_e mode; /* Actual SPI hardware mode */ @@ -234,11 +233,11 @@ static struct esp32s2_spi_priv_s esp32s2_spi2_priv = { .spi_dev = { - .ops = &esp32s2_spi2_ops + .ops = &esp32s2_spi2_ops }, .config = &esp32s2_spi2_config, .refs = 0, - .exclsem = SEM_INITIALIZER(0), + .lock = NXMUTEX_INITIALIZER, .frequency = 0, .actual = 0, .mode = 0, @@ -303,11 +302,11 @@ static struct esp32s2_spi_priv_s esp32s2_spi3_priv = { .spi_dev = { - .ops = &esp32s2_spi3_ops + .ops = &esp32s2_spi3_ops }, .config = &esp32s2_spi3_config, .refs = 0, - .exclsem = SEM_INITIALIZER(0), + .lock = NXMUTEX_INITIALIZER, .frequency = 0, .actual = 0, .mode = 0, @@ -422,11 +421,11 @@ static int esp32s2_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1023,10 +1022,6 @@ static void esp32s2_spi_init(struct spi_dev_s *dev) const struct esp32s2_spi_config_s *config = priv->config; uint32_t regval; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ - - nxsem_init(&priv->exclsem, 0, 1); - esp32s2_gpiowrite(config->cs_pin, true); esp32s2_gpiowrite(config->mosi_pin, true); esp32s2_gpiowrite(config->miso_pin, true); @@ -1207,8 +1202,6 @@ int esp32s2_spibus_uninitialize(struct spi_dev_s *dev) esp32s2_spi_deinit(dev); - nxsem_destroy(&priv->exclsem); - return OK; } diff --git a/arch/xtensa/src/esp32s3/esp32s3_dma.c b/arch/xtensa/src/esp32s3/esp32s3_dma.c index 826634141bf8e..118d96189010c 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_dma.c +++ b/arch/xtensa/src/esp32s3/esp32s3_dma.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include "xtensa.h" @@ -66,8 +67,8 @@ * Private Data ****************************************************************************/ -static bool g_dma_chan_used[ESP32S3_DMA_CHAN_MAX]; -static sem_t g_dma_exc_sem = SEM_INITIALIZER(1); +static bool g_dma_chan_used[ESP32S3_DMA_CHAN_MAX]; +static mutex_t g_dma_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Public Functions @@ -105,7 +106,7 @@ int32_t esp32s3_dma_request(enum esp32s3_dma_periph_e periph, dmainfo("periph=%" PRIu32 " tx_prio=%" PRIu32 " rx_prio=%" PRIu32 "\n", (uint32_t)periph, tx_prio, rx_prio); - nxsem_wait_uninterruptible(&g_dma_exc_sem); + nxmutex_lock(&g_dma_lock); for (chan = 0; chan < ESP32S3_DMA_CHAN_MAX; chan++) { @@ -120,8 +121,7 @@ int32_t esp32s3_dma_request(enum esp32s3_dma_periph_e periph, { dmaerr("No available GDMA channel for allocation\n"); - nxsem_post(&g_dma_exc_sem); - + nxmutex_unlock(&g_dma_lock); return ERROR; } @@ -168,8 +168,7 @@ int32_t esp32s3_dma_request(enum esp32s3_dma_periph_e periph, SET_REG(DMA_OUT_PRI_CH0_REG, chan, tx_prio); SET_REG(DMA_IN_PRI_CH0_REG, chan, rx_prio); - nxsem_post(&g_dma_exc_sem); - + nxmutex_unlock(&g_dma_lock); return chan; } diff --git a/arch/xtensa/src/esp32s3/esp32s3_i2c.c b/arch/xtensa/src/esp32s3/esp32s3_i2c.c index 20b5c1bccee0a..7c245ca4f5edc 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_i2c.c +++ b/arch/xtensa/src/esp32s3/esp32s3_i2c.c @@ -41,8 +41,8 @@ #include #include #include +#include #include -#include #include @@ -204,7 +204,7 @@ struct esp32s3_i2c_priv_s const struct esp32s3_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 */ @@ -227,8 +227,6 @@ struct esp32s3_i2c_priv_s uint32_t clk_freq; /* Current I2C Clock frequency */ - spinlock_t lock; /* Device specific lock */ - #ifdef CONFIG_I2C_TRACE /* I2C trace support */ @@ -253,10 +251,6 @@ static int i2c_transfer(struct i2c_master_s *dev, struct i2c_msg_s *msgs, int count); static inline void i2c_process(struct esp32s3_i2c_priv_s *priv, uint32_t status); -static void i2c_sem_init(struct esp32s3_i2c_priv_s *priv); -static void i2c_sem_destroy(struct esp32s3_i2c_priv_s *priv); -static void i2c_sem_post(struct esp32s3_i2c_priv_s *priv); -static int i2c_sem_wait(struct esp32s3_i2c_priv_s *priv); #ifndef CONFIG_I2C_POLLED static int i2c_sem_waitdone(struct esp32s3_i2c_priv_s *priv); #endif @@ -319,6 +313,10 @@ static struct esp32s3_i2c_priv_s g_esp32s3_i2c0_priv = .id = ESP32S3_I2C0, .config = &g_esp32s3_i2c0_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .i2cstate = I2CSTATE_IDLE, .msgv = NULL, .msgid = 0, @@ -351,6 +349,10 @@ static struct esp32s3_i2c_priv_s g_esp32s3_i2c1_priv = .id = ESP32S3_I2C1, .config = &g_esp32s3_i2c1_config, .refs = 0, + .lock = NXMUTEX_INITIALIZER, +#ifndef CONFIG_I2C_POLLED + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), +#endif .i2cstate = I2CSTATE_IDLE, .msgv = NULL, .msgid = 0, @@ -945,86 +947,6 @@ static int i2c_polling_waitdone(struct esp32s3_i2c_priv_s *priv) } #endif -/**************************************************************************** - * Name: i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - * Returned Value: - * Zero (OK) is returned on success. A negated errno value is returned on - * failure. - * - ****************************************************************************/ - -static int i2c_sem_wait(struct esp32s3_i2c_priv_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->sem_excl); -} - -/**************************************************************************** - * Name: i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void i2c_sem_post(struct esp32s3_i2c_priv_s *priv) -{ - nxsem_post(&priv->sem_excl); -} - -/**************************************************************************** - * Name: i2c_sem_destroy - * - * Description: - * Destroy semaphores. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void i2c_sem_destroy(struct esp32s3_i2c_priv_s *priv) -{ - nxsem_destroy(&priv->sem_excl); -#ifndef CONFIG_I2C_POLLED - nxsem_destroy(&priv->sem_isr); -#endif -} - -/**************************************************************************** - * Name: i2c_sem_init - * - * Description: - * Initialize semaphores. - * - * Parameters: - * priv - Pointer to the internal driver state structure. - * - ****************************************************************************/ - -static void i2c_sem_init(struct esp32s3_i2c_priv_s *priv) -{ - nxsem_init(&priv->sem_excl, 0, 1); - - /* This semaphore is used for signaling and, hence, should not have - * priority inheritance enabled. - */ - -#ifndef CONFIG_I2C_POLLED - nxsem_init(&priv->sem_isr, 0, 0); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); -#endif -} - /**************************************************************************** * Device Driver Operations ****************************************************************************/ @@ -1054,7 +976,7 @@ static int i2c_transfer(struct i2c_master_s *dev, struct i2c_msg_s *msgs, DEBUGASSERT(count > 0); - ret = i2c_sem_wait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1180,8 +1102,7 @@ static int i2c_transfer(struct i2c_master_s *dev, struct i2c_msg_s *msgs, /* Dump the trace result */ i2c_tracedump(priv); - - i2c_sem_post(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -1226,13 +1147,12 @@ static void i2c_clear_bus(struct esp32s3_i2c_priv_s *priv) #ifdef CONFIG_I2C_RESET static int i2c_reset(struct i2c_master_s *dev) { - irqstate_t flags; struct esp32s3_i2c_priv_s *priv = (struct esp32s3_i2c_priv_s *)dev; DEBUGASSERT(dev != NULL); DEBUGASSERT(priv->refs > 0); - flags = spin_lock_irqsave(&priv->lock); + nxmutex_lock(&priv->lock); i2c_reset_fsmc(priv); @@ -1245,8 +1165,7 @@ static int i2c_reset(struct i2c_master_s *dev) priv->bytes = 0; priv->ready_read = false; - spin_unlock_irqrestore(&priv->lock, flags); - + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -1616,7 +1535,6 @@ static void i2c_process(struct esp32s3_i2c_priv_s *priv, uint32_t status) struct i2c_master_s *esp32s3_i2cbus_initialize(int port) { - irqstate_t flags; struct esp32s3_i2c_priv_s *priv; #ifndef CONFIG_I2C_POLLED const struct esp32s3_i2c_config_s *config; @@ -1639,12 +1557,10 @@ struct i2c_master_s *esp32s3_i2cbus_initialize(int port) return NULL; } - flags = spin_lock_irqsave(&priv->lock); - + nxmutex_lock(&priv->lock); if (priv->refs++ != 0) { - spin_unlock_irqrestore(&priv->lock, flags); - + nxmutex_unlock(&priv->lock); return (struct i2c_master_s *)priv; } @@ -1660,7 +1576,8 @@ struct i2c_master_s *esp32s3_i2cbus_initialize(int port) { /* Failed to allocate a CPU interrupt of this type */ - spin_unlock_irqrestore(&priv->lock, flags); + priv->refs--; + nxmutex_unlock(&priv->lock); return NULL; } @@ -1669,20 +1586,17 @@ struct i2c_master_s *esp32s3_i2cbus_initialize(int port) if (ret != OK) { esp32s3_teardown_irq(priv->cpu, config->periph, priv->cpuint); + priv->refs--; - spin_unlock_irqrestore(&priv->lock, flags); - + nxmutex_unlock(&priv->lock); return NULL; } up_enable_irq(config->irq); #endif - i2c_sem_init(priv); - i2c_init(priv); - - spin_unlock_irqrestore(&priv->lock, flags); + nxmutex_unlock(&priv->lock); return (struct i2c_master_s *)priv; } @@ -1705,7 +1619,6 @@ struct i2c_master_s *esp32s3_i2cbus_initialize(int port) int esp32s3_i2cbus_uninitialize(struct i2c_master_s *dev) { - irqstate_t flags; struct esp32s3_i2c_priv_s *priv = (struct esp32s3_i2c_priv_s *)dev; DEBUGASSERT(dev != NULL); @@ -1715,24 +1628,20 @@ int esp32s3_i2cbus_uninitialize(struct i2c_master_s *dev) return ERROR; } - flags = spin_lock_irqsave(&priv->lock); - + nxmutex_lock(&priv->lock); if (--priv->refs) { - spin_unlock_irqrestore(&priv->lock, flags); + nxmutex_unlock(&priv->lock); return OK; } - spin_unlock_irqrestore(&priv->lock, flags); - #ifndef CONFIG_I2C_POLLED up_disable_irq(priv->config->irq); esp32s3_teardown_irq(priv->cpu, priv->config->periph, priv->cpuint); #endif i2c_deinit(priv); - - i2c_sem_destroy(priv); + nxmutex_unlock(&priv->lock); return OK; } diff --git a/arch/xtensa/src/esp32s3/esp32s3_spi.c b/arch/xtensa/src/esp32s3/esp32s3_spi.c index a3251bacb1b45..d9e2d4d26052f 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_spi.c +++ b/arch/xtensa/src/esp32s3/esp32s3_spi.c @@ -38,8 +38,8 @@ #include #include #include +#include #include -#include #include #include @@ -157,7 +157,7 @@ struct esp32s3_spi_priv_s const struct esp32s3_spi_config_s *config; int refs; /* Reference count */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ #ifdef CONFIG_ESP32S3_SPI2_DMA sem_t sem_isr; /* Interrupt wait semaphore */ int cpu; /* CPU ID */ @@ -168,7 +168,6 @@ struct esp32s3_spi_priv_s uint32_t actual; /* Actual clock frequency */ enum spi_mode_e mode; /* Actual SPI hardware mode */ uint8_t nbits; /* Actual SPI send/receive bits once transmission */ - spinlock_t lock; /* Device specific lock. */ }; /**************************************************************************** @@ -292,14 +291,14 @@ static const struct spi_ops_s esp32s3_spi2_ops = static struct esp32s3_spi_priv_s esp32s3_spi2_priv = { .spi_dev = - { - .ops = &esp32s3_spi2_ops - }, + { + .ops = &esp32s3_spi2_ops + }, .config = &esp32s3_spi2_config, .refs = 0, - .exclsem = SEM_INITIALIZER(0), + .lock = NXMUTEX_INITIALIZER, #ifdef CONFIG_ESP32S3_SPI2_DMA - .sem_isr = SEM_INITIALIZER(0), + .sem_isr = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), .cpuint = -ENOMEM, .dma_channel = -1, #endif @@ -367,12 +366,12 @@ static const struct spi_ops_s esp32s3_spi3_ops = static struct esp32s3_spi_priv_s esp32s3_spi3_priv = { .spi_dev = - { - .ops = &esp32s3_spi3_ops - }, + { + .ops = &esp32s3_spi3_ops + }, .config = &esp32s3_spi3_config, .refs = 0, - .exclsem = SEM_INITIALIZER(0), + .lock = NXMUTEX_INITIALIZER, .frequency = 0, .actual = 0, .mode = 0, @@ -496,11 +495,11 @@ static int esp32s3_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -1330,10 +1329,6 @@ static void esp32s3_spi_init(struct spi_dev_s *dev) const struct esp32s3_spi_config_s *config = priv->config; uint32_t regval; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ - - nxsem_init(&priv->exclsem, 0, 1); - esp32s3_gpiowrite(config->cs_pin, true); esp32s3_gpiowrite(config->mosi_pin, true); esp32s3_gpiowrite(config->miso_pin, true); @@ -1399,9 +1394,6 @@ static void esp32s3_spi_init(struct spi_dev_s *dev) SPI_USER1_REG(priv->config->id)); #ifdef CONFIG_ESP32S3_SPI2_DMA - nxsem_init(&priv->sem_isr, 0, 0); - nxsem_set_protocol(&priv->sem_isr, SEM_PRIO_NONE); - esp32s3_spi_dma_init(dev); #endif @@ -1619,13 +1611,10 @@ int esp32s3_spibus_uninitialize(struct spi_dev_s *dev) priv->cpuint = -ENOMEM; - nxsem_destroy(&priv->sem_isr); #endif esp32s3_spi_deinit(dev); - nxsem_destroy(&priv->exclsem); - return OK; } diff --git a/arch/xtensa/src/esp32s3/esp32s3_spiflash_mtd.c b/arch/xtensa/src/esp32s3/esp32s3_spiflash_mtd.c index 2f9e7272f7646..ab7cc6790e928 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_spiflash_mtd.c +++ b/arch/xtensa/src/esp32s3/esp32s3_spiflash_mtd.c @@ -35,7 +35,7 @@ #include #include -#include +#include #include #include "hardware/esp32s3_soc.h" @@ -145,7 +145,7 @@ static const struct esp32s3_mtd_dev_s g_esp32s3_spiflash_encrypt = /* Ensure exclusive access to the driver */ -static sem_t g_exclsem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -186,15 +186,14 @@ static int esp32s3_erase(struct mtd_dev_s *dev, off_t startblock, finfo("spi_flash_erase_range(0x%x, %d)\n", offset, nbytes); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_erase_range(offset, nbytes); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -243,17 +242,16 @@ static ssize_t esp32s3_read(struct mtd_dev_s *dev, off_t offset, finfo("spi_flash_read(0x%x, %p, %d)\n", offset, buffer, nbytes); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_read(offset, buffer, nbytes); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -298,15 +296,14 @@ static ssize_t esp32s3_bread(struct mtd_dev_s *dev, off_t startblock, finfo("spi_flash_read(0x%x, %p, %d)\n", addr, buffer, size); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_read(addr, buffer, size); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -352,17 +349,16 @@ static ssize_t esp32s3_read_decrypt(struct mtd_dev_s *dev, nbytes); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_read_encrypted(offset, buffer, nbytes); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -409,15 +405,14 @@ static ssize_t esp32s3_bread_decrypt(struct mtd_dev_s *dev, finfo("spi_flash_read_encrypted(0x%x, %p, %d)\n", addr, buffer, size); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_read_encrypted(addr, buffer, size); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -466,17 +461,16 @@ static ssize_t esp32s3_write(struct mtd_dev_s *dev, off_t offset, finfo("spi_flash_write(0x%x, %p, %d)\n", offset, buffer, nbytes); #endif - /* Acquire the semaphore. */ + /* Acquire the mutex. */ - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_write(offset, buffer, nbytes); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -523,15 +517,14 @@ static ssize_t esp32s3_bwrite_encrypt(struct mtd_dev_s *dev, finfo("spi_flash_write_encrypted(0x%x, %p, %d)\n", addr, buffer, size); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_write_encrypted(addr, buffer, size); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { @@ -576,15 +569,14 @@ static ssize_t esp32s3_bwrite(struct mtd_dev_s *dev, off_t startblock, finfo("spi_flash_write(0x%x, %p, %d)\n", addr, buffer, size); #endif - ret = nxsem_wait(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; } ret = spi_flash_write(addr, buffer, size); - - nxsem_post(&g_exclsem); + nxmutex_unlock(&g_lock); if (ret == OK) { diff --git a/arch/z16/src/z16f/z16f_espi.c b/arch/z16/src/z16f/z16f_espi.c index e8a74c5eb2f67..b12cb77e8b7a5 100644 --- a/arch/z16/src/z16f/z16f_espi.c +++ b/arch/z16/src/z16f/z16f_espi.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include "chip.h" @@ -52,7 +52,7 @@ struct z16f_spi_s bool initialized; /* TRUE: Controller has been initialized */ uint8_t nbits; /* Width of word in bits (1-8) */ uint8_t mode; /* Mode 0,1,2,3 */ - sem_t exclsem; /* Assures mutually exclusive access to SPI */ + mutex_t lock; /* Assures mutually exclusive access to SPI */ uint32_t frequency; /* Requested clock frequency */ uint32_t actual; /* Actual clock frequency */ @@ -360,11 +360,11 @@ static int spi_lock(FAR struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -793,7 +793,7 @@ FAR struct spi_dev_s *z16_spibus_initialize(int port) flags = enter_critical_section(); priv->spi.ops = &g_epsiops; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Set up the SPI pin configuration (board-specific logic is required * to configure and manage all chip selects). diff --git a/arch/z80/src/ez80/ez80_i2c.c b/arch/z80/src/ez80/ez80_i2c.c index d4d440e6d8469..8b80c2d0891fb 100644 --- a/arch/z80/src/ez80/ez80_i2c.c +++ b/arch/z80/src/ez80/ez80_i2c.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include #include @@ -89,8 +89,8 @@ static int ez80_i2c_transfer(FAR struct i2c_master_s *dev, * Private Data ****************************************************************************/ -static bool g_initialized; /* true:I2C has been initialized */ -static sem_t g_i2csem; /* Serialize I2C transfers */ +static bool g_initialized; /* true:I2C has been initialized */ +static mutex_t g_i2clock; /* Serialize I2C transfers */ const struct i2c_ops_s g_ops = { @@ -101,25 +101,6 @@ const struct i2c_ops_s g_ops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: ez80_i2c_semtake/ez80_i2c_semgive - * - * Description: - * Take/Give the I2C semaphore. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int ez80_i2c_semtake(void) -{ - return nxsem_wait(&g_i2csem); -} - /**************************************************************************** * Name: ez80_i2c_setccr * @@ -817,7 +798,7 @@ static int ez80_i2c_transfer(FAR struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - ret = nxsem_wait(&g_i2csem); + ret = nxmutex_lock(&g_i2clock); if (ret < 0) { return ret; @@ -889,7 +870,7 @@ static int ez80_i2c_transfer(FAR struct i2c_master_s *dev, flags = (nostop) ? EZ80_NOSTART : 0; } - nxsem_post(&g_i2csem); + nxmutex_unlock(&g_i2clock); return ret; } @@ -931,9 +912,9 @@ FAR struct i2c_master_s *ez80_i2cbus_initialize(int port) * SCL/SDA are not multiplexed */ - /* This semaphore enforces serialized access for I2C transfers */ + /* This mutex enforces serialized access for I2C transfers */ - nxsem_init(&g_i2csem, 0, 1); + nxmutex_init(&g_i2clock); /* Enable I2C -- but not interrupts */ diff --git a/arch/z80/src/ez80/ez80_rtc_lowerhalf.c b/arch/z80/src/ez80/ez80_rtc_lowerhalf.c index 08528b73eeea8..63f590e4d48d9 100644 --- a/arch/z80/src/ez80/ez80_rtc_lowerhalf.c +++ b/arch/z80/src/ez80/ez80_rtc_lowerhalf.c @@ -31,6 +31,7 @@ #include #include +#include #include #include "chip.h" @@ -70,7 +71,7 @@ struct ez80_lowerhalf_s * this file. */ - sem_t devsem; /* Threads can only exclusively access the RTC */ + mutex_t devlock; /* Threads can only exclusively access the RTC */ #ifdef CONFIG_RTC_ALARM /* Alarm callback information */ @@ -356,7 +357,7 @@ static int ez80_setalarm(FAR struct rtc_lowerhalf_s *lower, priv = (FAR struct ez80_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -383,8 +384,7 @@ static int ez80_setalarm(FAR struct rtc_lowerhalf_s *lower, cbinfo->priv = NULL; } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -483,7 +483,7 @@ static int ez80_cancelalarm(FAR struct rtc_lowerhalf_s *lower, int alarmid) priv = (FAR struct ez80_lowerhalf_s *)lower; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -498,7 +498,7 @@ static int ez80_cancelalarm(FAR struct rtc_lowerhalf_s *lower, int alarmid) /* Then cancel the alarm */ ret = ez80_rtc_cancelalarm(); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -569,8 +569,7 @@ static int ez80_rdalarm(FAR struct rtc_lowerhalf_s *lower, FAR struct rtc_lowerhalf_s *ez80_rtc_lowerhalf(void) { - nxsem_init(&g_rtc_lowerhalf.devsem, 0, 1); - + nxmutex_init(&g_rtc_lowerhalf.devlock); return (FAR struct rtc_lowerhalf_s *)&g_rtc_lowerhalf; } diff --git a/arch/z80/src/ez80/ez80_spi.c b/arch/z80/src/ez80/ez80_spi.c index de2fc5e766931..1e63a6548817d 100644 --- a/arch/z80/src/ez80/ez80_spi.c +++ b/arch/z80/src/ez80/ez80_spi.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include @@ -129,9 +129,7 @@ static struct spi_dev_s g_spidev = &g_spiops }; -/* Semaphore supports mutually exclusive access */ - -static sem_t g_exclsem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; /* These are used to perform reconfigurations only when necessary. */ @@ -170,11 +168,11 @@ static int spi_lock(FAR struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&g_exclsem); + ret = nxmutex_lock(&g_lock); } else { - ret = nxsem_post(&g_exclsem); + ret = nxmutex_unlock(&g_lock); } return ret; diff --git a/arch/z80/src/z8/z8_i2c.c b/arch/z80/src/z8/z8_i2c.c index 4d2688a11b1fa..abaaafa3a167e 100644 --- a/arch/z80/src/z8/z8_i2c.c +++ b/arch/z80/src/z8/z8_i2c.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include #include @@ -95,8 +95,8 @@ extern uint32_t get_freq(void); * Private Data ****************************************************************************/ -static bool g_initialized; /* true:I2C has been initialized */ -static sem_t g_i2csem; /* Serialize I2C transfers */ +static bool g_initialized; /* true:I2C has been initialized */ +static mutex_t g_i2clock; /* Serialize I2C transfers */ const struct i2c_ops_s g_ops = { @@ -497,7 +497,7 @@ static int z8_i2c_transfer(FAR struct i2c_master_s *dev, /* Get exclusive access to the I2C bus */ - ret = nxsem_wait(&g_i2csem); + ret = nxmutex_lock(&g_i2clock); if (ret < 0) { return ret; @@ -567,7 +567,7 @@ static int z8_i2c_transfer(FAR struct i2c_master_s *dev, flags = (nostop) ? Z8_NOSTART : 0; } - nxsem_post(&g_i2csem); + nxmutex_unlock(&g_i2clock); return ret; } @@ -631,9 +631,9 @@ FAR struct i2c_master_s *z8_i2cbus_initialize(int port) PAADDR = 0x02; PACTL |= 0xc0; - /* This semaphore enforces serialized access for I2C transfers */ + /* This mutex enforces serialized access for I2C transfers */ - nxsem_init(&g_i2csem, 0, 1); + nxmutex_init(&g_i2clock); /* Enable I2C -- no interrupts */ diff --git a/audio/audio.c b/audio/audio.c index 4d7e05105e715..3dd3169f87199 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include @@ -73,7 +73,7 @@ struct audio_upperhalf_s { uint8_t crefs; /* The number of times the device has been opened */ volatile bool started; /* True: playback is active */ - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ FAR struct audio_lowerhalf_s *dev; /* lower-half state */ struct file *usermq; /* User mode app's message queue */ }; @@ -150,7 +150,7 @@ static int audio_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -167,7 +167,7 @@ static int audio_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Save the new open count on success */ @@ -175,8 +175,8 @@ static int audio_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&upper->lock); errout: return ret; @@ -200,7 +200,7 @@ static int audio_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -232,8 +232,7 @@ static int audio_close(FAR struct file *filep) } ret = OK; - - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); errout: return ret; @@ -365,7 +364,7 @@ static int audio_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -664,7 +663,7 @@ static int audio_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -947,7 +946,7 @@ int audio_register(FAR const char *name, FAR struct audio_lowerhalf_s *dev) * (it was already zeroed by kmm_zalloc()) */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); upper->dev = dev; #ifdef CONFIG_AUDIO_CUSTOM_DEV_PATH diff --git a/boards/arm/cxd56xx/common/src/cxd56_imageproc.c b/boards/arm/cxd56xx/common/src/cxd56_imageproc.c index a8d505fccff21..ae9de5aa740bc 100644 --- a/boards/arm/cxd56xx/common/src/cxd56_imageproc.c +++ b/boards/arm/cxd56xx/common/src/cxd56_imageproc.c @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -205,9 +206,9 @@ struct aligned_data(16) ge2d_abcmd_s ****************************************************************************/ static sem_t g_rotwait; -static sem_t g_rotexc; -static sem_t g_geexc; -static sem_t g_abexc; +static mutex_t g_rotlock; +static mutex_t g_gelock; +static mutex_t g_ablock; static struct file g_gfile; static char g_gcmdbuf[256] aligned_data(16); @@ -216,24 +217,13 @@ static char g_gcmdbuf[256] aligned_data(16); * Private Functions ****************************************************************************/ -static int ip_semtake(sem_t * id) -{ - return nxsem_wait_uninterruptible(id); -} - -static void ip_semgive(sem_t * id) -{ - nxsem_post(id); -} - static int intr_handler_rot(int irq, void *context, void *arg) { putreg32(1, ROT_INTR_CLEAR); putreg32(0, ROT_INTR_ENABLE); putreg32(1, ROT_INTR_DISABLE); - ip_semgive(&g_rotwait); - + nxsem_post(&g_rotwait); return 0; } @@ -415,7 +405,7 @@ static void imageproc_convert_(int is_yuv2rgb, return; } - ret = ip_semtake(&g_rotexc); + ret = nxmutex_lock(&g_rotlock); if (ret) { return; @@ -445,9 +435,8 @@ static void imageproc_convert_(int is_yuv2rgb, putreg32(0, ROT_RGB_ALIGNMENT); putreg32(1, ROT_COMMAND); - ip_semtake(&g_rotwait); - - ip_semgive(&g_rotexc); + nxsem_wait_uninterruptible(&g_rotwait); + nxmutex_unlock(&g_rotlock); } static void get_rect_info(imageproc_imginfo_t *imginfo, @@ -527,10 +516,10 @@ static void *get_blendarea(imageproc_imginfo_t *imginfo, int offset) void imageproc_initialize(void) { - nxsem_init(&g_rotexc, 0, 1); + nxmutex_init(&g_rotlock); nxsem_init(&g_rotwait, 0, 0); - nxsem_init(&g_geexc, 0, 1); - nxsem_init(&g_abexc, 0, 1); + nxmutex_init(&g_gelock); + nxmutex_init(&g_ablock); nxsem_set_protocol(&g_rotwait, SEM_PRIO_NONE); cxd56_ge2dinitialize(GEDEVNAME); @@ -558,9 +547,9 @@ void imageproc_finalize(void) cxd56_ge2duninitialize(GEDEVNAME); nxsem_destroy(&g_rotwait); - nxsem_destroy(&g_rotexc); - nxsem_destroy(&g_geexc); - nxsem_destroy(&g_abexc); + nxmutex_destroy(&g_rotlock); + nxmutex_destroy(&g_gelock); + nxmutex_destroy(&g_ablock); } void imageproc_convert_yuv2rgb(uint8_t * ibuf, @@ -629,7 +618,7 @@ int imageproc_resize(uint8_t * ibuf, return -EINVAL; } - ret = ip_semtake(&g_geexc); + ret = nxmutex_lock(&g_gelock); if (ret) { return ret; @@ -652,7 +641,7 @@ int imageproc_resize(uint8_t * ibuf, 0x0080); if (cmd == NULL) { - ip_semgive(&g_geexc); + nxmutex_unlock(&g_gelock); return -EINVAL; } @@ -666,12 +655,11 @@ int imageproc_resize(uint8_t * ibuf, ret = file_write(&g_gfile, g_gcmdbuf, len); if (ret < 0) { - ip_semgive(&g_geexc); + nxmutex_unlock(&g_gelock); return -EFAULT; } - ip_semgive(&g_geexc); - + nxmutex_unlock(&g_gelock); return 0; } @@ -747,7 +735,7 @@ int imageproc_clip_and_resize(uint8_t * ibuf, clip_height = ivsize; } - ret = ip_semtake(&g_geexc); + ret = nxmutex_lock(&g_gelock); if (ret) { return ret; @@ -771,7 +759,7 @@ int imageproc_clip_and_resize(uint8_t * ibuf, if (cmd == NULL) { - ip_semgive(&g_geexc); + nxmutex_unlock(&g_gelock); return -EINVAL; } @@ -785,12 +773,11 @@ int imageproc_clip_and_resize(uint8_t * ibuf, ret = file_write(&g_gfile, g_gcmdbuf, len); if (ret < 0) { - ip_semgive(&g_geexc); + nxmutex_unlock(&g_gelock); return -EFAULT; } - ip_semgive(&g_geexc); - + nxmutex_unlock(&g_gelock); return 0; } @@ -939,7 +926,7 @@ int imageproc_alpha_blend(imageproc_imginfo_t *dst, src_addr = get_blendarea(src, src_offset); a_addr = get_blendarea(alpha, a_offset); - ret = ip_semtake(&g_abexc); + ret = nxmutex_lock(&g_ablock); if (ret) { return ret; /* -EINTR */ @@ -962,7 +949,7 @@ int imageproc_alpha_blend(imageproc_imginfo_t *dst, if (cmd == NULL) { - ip_semgive(&g_abexc); + nxmutex_unlock(&g_ablock); return -EINVAL; } @@ -976,11 +963,11 @@ int imageproc_alpha_blend(imageproc_imginfo_t *dst, ret = file_write(&g_gfile, g_gcmdbuf, len); if (ret < 0) { - ip_semgive(&g_abexc); + nxmutex_unlock(&g_ablock); return -EFAULT; } - ip_semgive(&g_abexc); + nxmutex_unlock(&g_ablock); return 0; } diff --git a/boards/arm/cxd56xx/spresense/src/cxd56_power.c b/boards/arm/cxd56xx/spresense/src/cxd56_power.c index 62bfd8f589998..32dafde225f62 100644 --- a/boards/arm/cxd56xx/spresense/src/cxd56_power.c +++ b/boards/arm/cxd56xx/spresense/src/cxd56_power.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include "chip.h" #include "arm_internal.h" @@ -54,7 +54,7 @@ * Private Data ****************************************************************************/ -static sem_t g_ltsem = SEM_INITIALIZER(1); +static mutex_t g_ltlock = NXMUTEX_INITIALIZER; static bool g_used_lna = false; static bool g_used_tcxo = true; #ifdef CONFIG_BOARDCTL_RESET @@ -395,7 +395,7 @@ int board_xtal_power_control(bool en) /* Get exclusive access to the lna / tcxo power control */ - nxsem_wait_uninterruptible(&g_ltsem); + nxmutex_lock(&g_ltlock); if (en) { @@ -421,8 +421,7 @@ int board_xtal_power_control(bool en) g_used_tcxo = false; } - nxsem_post(&g_ltsem); - + nxmutex_unlock(&g_ltlock); return ret; } @@ -453,7 +452,7 @@ int board_lna_power_control(bool en) /* Get exclusive access to the lna / tcxo power control */ - nxsem_wait_uninterruptible(&g_ltsem); + nxmutex_lock(&g_ltlock); if (en) { @@ -479,8 +478,7 @@ int board_lna_power_control(bool en) g_used_lna = false; } - nxsem_post(&g_ltsem); - + nxmutex_unlock(&g_ltlock); return ret; } diff --git a/boards/arm/imxrt/imxrt1064-evk/scripts/user-space.ld b/boards/arm/imxrt/imxrt1064-evk/scripts/user-space.ld index f0f40a0a72e6e..54349cb7f1382 100644 --- a/boards/arm/imxrt/imxrt1064-evk/scripts/user-space.ld +++ b/boards/arm/imxrt/imxrt1064-evk/scripts/user-space.ld @@ -29,8 +29,6 @@ EXTERN(umm_initialize) EXTERN(umm_addregion) -EXTERN(umm_trysemaphore) -EXTERN(umm_givesemaphore) EXTERN(malloc) EXTERN(realloc) diff --git a/boards/arm/imxrt/teensy-4.x/scripts/user-space.ld b/boards/arm/imxrt/teensy-4.x/scripts/user-space.ld index 65695c02a0b11..a7d36d9adff39 100644 --- a/boards/arm/imxrt/teensy-4.x/scripts/user-space.ld +++ b/boards/arm/imxrt/teensy-4.x/scripts/user-space.ld @@ -29,8 +29,6 @@ EXTERN(umm_initialize) EXTERN(umm_addregion) -EXTERN(umm_trysemaphore) -EXTERN(umm_givesemaphore) EXTERN(malloc) EXTERN(realloc) diff --git a/boards/arm/lpc214x/mcu123-lpc214x/src/lpc2148_spi1.c b/boards/arm/lpc214x/mcu123-lpc214x/src/lpc2148_spi1.c index b463b2b5efc2a..68a2363044e86 100644 --- a/boards/arm/lpc214x/mcu123-lpc214x/src/lpc2148_spi1.c +++ b/boards/arm/lpc214x/mcu123-lpc214x/src/lpc2148_spi1.c @@ -57,7 +57,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -130,7 +130,7 @@ static struct spi_dev_s g_spidev = &g_spiops }; -static sem_t g_exclsem = SEM_INITIALIZER(1); /* For mutually exclusive access */ +static mutex_t g_lock = NXMUTEX_INITIALIZER; /* For mutually exclusive access */ /**************************************************************************** * Public Data @@ -167,11 +167,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&g_exclsem); + ret = nxmutex_lock(&g_lock); } else { - ret = nxsem_post(&g_exclsem); + ret = nxmutex_unlock(&g_lock); } return ret; diff --git a/boards/arm/lpc214x/zp214xpa/src/lpc2148_spi1.c b/boards/arm/lpc214x/zp214xpa/src/lpc2148_spi1.c index 30f9565623ed1..fd34f276e9caa 100644 --- a/boards/arm/lpc214x/zp214xpa/src/lpc2148_spi1.c +++ b/boards/arm/lpc214x/zp214xpa/src/lpc2148_spi1.c @@ -58,7 +58,7 @@ #include #include -#include +#include #include #include "arm_internal.h" @@ -133,7 +133,7 @@ static struct spi_dev_s g_spidev = &g_spiops }; -static sem_t g_exclsem = SEM_INITIALIZER(1); /* For mutually exclusive access */ +static mutex_t g_lock = NXMUTEX_INITIALIZER; /* For mutually exclusive access */ /**************************************************************************** * Public Data @@ -170,11 +170,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&g_exclsem); + ret = nxmutex_lock(&g_lock); } else { - ret = nxsem_post(&g_exclsem); + ret = nxmutex_unlock(&g_lock); } return ret; diff --git a/boards/arm/stm32/mikroe-stm32f4/src/stm32_touchscreen.c b/boards/arm/stm32/mikroe-stm32f4/src/stm32_touchscreen.c index c09c122634301..faa8c85227135 100644 --- a/boards/arm/stm32/mikroe-stm32f4/src/stm32_touchscreen.c +++ b/boards/arm/stm32/mikroe-stm32f4/src/stm32_touchscreen.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -214,7 +215,7 @@ struct tc_dev_s uint16_t newy; /* New, un-thresholded Y value */ uint8_t sampcount; /* Count of samples for average so far */ uint8_t resamplecount; /* Countdown to PENUP */ - sem_t devsem; /* Manages exclusive access to this structure */ + mutex_t devlock; /* Manages exclusive access to this structure */ sem_t waitsem; /* Used to wait for the availability of data */ struct tc_sample_s sample; /* Last sampled touch point data */ struct work_s work; /* Supports the state machine delayed processing */ @@ -673,12 +674,12 @@ static int tc_waitsample(struct tc_dev_s *priv, sched_lock(); - /* Now release the semaphore that manages mutually exclusive access to + /* Now release the mutex that manages mutually exclusive access to * the device structure. This may cause other tasks to become ready to * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -703,7 +704,7 @@ static int tc_waitsample(struct tc_dev_s *priv, * sample. Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); errout: /* Restore pre-emption. We might get suspended here but that is okay @@ -1103,7 +1104,7 @@ static int tc_open(struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1117,7 +1118,7 @@ static int tc_open(struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* When the reference increments to 1, this is the first open event @@ -1128,8 +1129,8 @@ static int tc_open(struct file *filep) priv->crefs = tmp; -errout_with_sem: - nxsem_post(&priv->devsem); +errout_with_lock: + nxmutex_unlock(&priv->devlock); return ret; #else return OK; @@ -1155,7 +1156,7 @@ static int tc_close(struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1171,7 +1172,7 @@ static int tc_close(struct file *filep) priv->crefs--; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); #endif return OK; } @@ -1209,7 +1210,7 @@ static ssize_t tc_read(struct file *filep, char *buffer, size_t len) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1293,7 +1294,7 @@ static ssize_t tc_read(struct file *filep, char *buffer, size_t len) ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1320,7 +1321,7 @@ static int tc_ioctl(struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1337,7 +1338,7 @@ static int tc_ioctl(struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; #endif } @@ -1363,7 +1364,7 @@ static int tc_poll(struct file *filep, struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1428,7 +1429,7 @@ static int tc_poll(struct file *filep, struct pollfd *fds, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1502,7 +1503,7 @@ int stm32_tsc_setup(int minor) /* Initialize the touchscreen device driver instance */ memset(priv, 0, sizeof(struct tc_dev_s)); - nxsem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ + nxmutex_init(&priv->devlock); /* Initialize device structure mutex */ nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ /* Register the device as an input device */ @@ -1538,7 +1539,7 @@ int stm32_tsc_setup(int minor) return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); #ifdef CONFIG_TOUCHSCREEN_MULTIPLE kmm_free(priv); #endif diff --git a/boards/arm/str71x/olimex-strp711/src/str71_spi.c b/boards/arm/str71x/olimex-strp711/src/str71_spi.c index 5b09af72fb29c..44cfbbbd6356e 100644 --- a/boards/arm/str71x/olimex-strp711/src/str71_spi.c +++ b/boards/arm/str71x/olimex-strp711/src/str71_spi.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include @@ -368,7 +368,7 @@ struct str71x_spidev_s bool initialized; /* Initialize port only once! */ uint32_t spibase; /* BSPIn base address */ uint16_t csbit; /* BSPIn SS bit int GPIO0 */ - sem_t exclsem; /* Supports mutually exclusive access */ + mutex_t lock; /* Supports mutually exclusive access */ }; /**************************************************************************** @@ -429,7 +429,7 @@ static struct str71x_spidev_s g_spidev0 = }, .spibase = STR71X_BSPI0_BASE, .csbit = ENC_GPIO0_CS, - .exclsem = SEM_INITIALIZER(1) + .lock = NXMUTEX_INITIALIZER }; #endif @@ -442,7 +442,7 @@ static struct str71x_spidev_s g_spidev1 = }, .spibase = STR71X_BSPI1_BASE, .csbit = MMCSD_GPIO0_CS, - .exclsem = SEM_INITIALIZER(1) + .lock = NXMUTEX_INITIALIZER }; #endif @@ -580,11 +580,11 @@ static int spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; diff --git a/boards/mips/pic32mx/pic32mx7mmb/src/pic32_touchscreen.c b/boards/mips/pic32mx/pic32mx7mmb/src/pic32_touchscreen.c index 8d5c8fcb96e8f..18a142eecb9ac 100644 --- a/boards/mips/pic32mx/pic32mx7mmb/src/pic32_touchscreen.c +++ b/boards/mips/pic32mx/pic32mx7mmb/src/pic32_touchscreen.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -194,7 +195,7 @@ struct tc_dev_s volatile bool penchange; /* An unreported event is buffered */ uint16_t value; /* Partial sample value (Y+ or X-) */ uint16_t newy; /* New, un-thresholded Y value */ - sem_t devsem; /* Manages exclusive access to this structure */ + mutex_t devlock; /* Manages exclusive access to this structure */ sem_t waitsem; /* Used to wait for the availability of data */ struct tc_sample_s sample; /* Last sampled touch point data */ struct work_s work; /* Supports the state machine delayed processing */ @@ -562,12 +563,12 @@ static int tc_waitsample(struct tc_dev_s *priv, sched_lock(); - /* Now release the semaphore that manages mutually exclusive access to + /* Now release the mutex that manages mutually exclusive access to * the device structure. This may cause other tasks to become ready to * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -587,12 +588,12 @@ static int tc_waitsample(struct tc_dev_s *priv, } } - /* Re-acquire the semaphore that manages mutually exclusive access to + /* Re-acquire the mutex that manages mutually exclusive access to * the device structure. We may have to wait here. But we have our sample. * Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); errout: /* Restore pre-emption. We might get suspended here but that is okay @@ -980,7 +981,7 @@ static int tc_open(struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -994,7 +995,7 @@ static int tc_open(struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* When the reference increments to 1, this is the first open event @@ -1005,8 +1006,8 @@ static int tc_open(struct file *filep) priv->crefs = tmp; -errout_with_sem: - nxsem_post(&priv->devsem); +errout_with_lock: + nxmutex_unlock(&priv->devlock); return ret; #else return OK; @@ -1032,7 +1033,7 @@ static int tc_close(struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1048,7 +1049,7 @@ static int tc_close(struct file *filep) priv->crefs--; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); #endif return OK; } @@ -1086,7 +1087,7 @@ static ssize_t tc_read(struct file *filep, char *buffer, size_t len) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1170,7 +1171,7 @@ static ssize_t tc_read(struct file *filep, char *buffer, size_t len) ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1197,7 +1198,7 @@ static int tc_ioctl(struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1214,7 +1215,7 @@ static int tc_ioctl(struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; #endif } @@ -1240,7 +1241,7 @@ static int tc_poll(struct file *filep, struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1305,7 +1306,7 @@ static int tc_poll(struct file *filep, struct pollfd *fds, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1366,7 +1367,7 @@ int pic32mx_tsc_setup(int minor) /* Initialize the touchscreen device driver instance */ memset(priv, 0, sizeof(struct tc_dev_s)); - nxsem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ + nxmutex_init(&priv->devlock); /* Initialize device structure mutex */ nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ /* Register the device as an input device */ @@ -1398,7 +1399,7 @@ int pic32mx_tsc_setup(int minor) return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); #ifdef CONFIG_TOUCHSCREEN_MULTIPLE kmm_free(priv); #endif diff --git a/crypto/random_pool.c b/crypto/random_pool.c index 005c3558612ed..6bb346f878a5e 100644 --- a/crypto/random_pool.c +++ b/crypto/random_pool.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include /**************************************************************************** @@ -66,7 +66,7 @@ struct blake2xs_rng_s struct rng_s { - sem_t rd_sem; /* Threads can only exclusively access the RNG */ + mutex_t rd_lock; /* Threads can only exclusively access the RNG */ volatile uint32_t rd_addptr; volatile uint32_t rd_newentr; volatile uint8_t rd_rotate; @@ -362,7 +362,7 @@ static void rng_init(void) cryptinfo("Initializing RNG\n"); memset(&g_rng, 0, sizeof(struct rng_s)); - nxsem_init(&g_rng.rd_sem, 0, 1); + nxmutex_init(&g_rng.rd_lock); /* We do not initialize output here because this is called * quite early in boot and there may not be enough entropy. @@ -495,7 +495,7 @@ void up_rngreseed(void) { int ret; - ret = nxsem_wait_uninterruptible(&g_rng.rd_sem); + ret = nxmutex_lock(&g_rng.rd_lock); if (ret >= 0) { if (g_rng.rd_newentr >= MIN_SEED_NEW_ENTROPY_WORDS) @@ -503,7 +503,7 @@ void up_rngreseed(void) rng_reseed(); } - nxsem_post(&g_rng.rd_sem); + nxmutex_unlock(&g_rng.rd_lock); } } @@ -546,21 +546,7 @@ void up_randompool_initialize(void) void arc4random_buf(FAR void *bytes, size_t nbytes) { - int ret; - - do - { - ret = nxsem_wait_uninterruptible(&g_rng.rd_sem); - - /* The only possible error should be if we were awakened by - * thread cancellation. At this point, we must continue to acquire - * the semaphore anyway. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); - + nxmutex_lock(&g_rng.rd_lock); rng_buf_internal(bytes, nbytes); - nxsem_post(&g_rng.rd_sem); + nxmutex_unlock(&g_rng.rd_lock); } diff --git a/drivers/1wire/1wire.c b/drivers/1wire/1wire.c index ae1fbe4948ea1..ec96506b625a3 100644 --- a/drivers/1wire/1wire.c +++ b/drivers/1wire/1wire.c @@ -138,7 +138,6 @@ static int onewire_pm_prepare(FAR struct pm_callback_s *cb, int domain, struct onewire_master_s *master = (struct onewire_master_s *)((char *)cb - offsetof(struct onewire_master_s, pm_cb)); - int sval; /* Logic to prepare for a reduced power state goes here. */ @@ -153,13 +152,7 @@ static int onewire_pm_prepare(FAR struct pm_callback_s *cb, int domain, /* Check if exclusive lock for the bus master is held. */ - if (nxsem_get_value(&master->devsem.sem, &sval) < 0) - { - DEBUGPANIC(); - return -EINVAL; - } - - if (sval <= 0) + if (nxrmutex_is_locked(&master->devlock) { /* Exclusive lock is held, do not allow entry to deeper PM * states. diff --git a/drivers/1wire/ds28e17.c b/drivers/1wire/ds28e17.c index a93a29f10431d..c31d717b50e70 100644 --- a/drivers/1wire/ds28e17.c +++ b/drivers/1wire/ds28e17.c @@ -142,38 +142,6 @@ static const struct i2c_ops_s ds_i2c_ops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: ds_i2c_sem_wait - * - * Description: - * Take the exclusive access, waiting as necessary - * - ****************************************************************************/ - -static inline int ds_i2c_sem_wait(FAR struct i2c_master_s *i2cdev) -{ - FAR struct ds_i2c_inst_s *inst = (FAR struct ds_i2c_inst_s *)i2cdev; - FAR struct onewire_master_s *master = inst->master; - - return nxrmutex_lock(&master->devlock); -} - -/**************************************************************************** - * Name: ds_i2c_sem_post - * - * Description: - * Release the mutual exclusion semaphore - * - ****************************************************************************/ - -static inline void ds_i2c_sem_post(FAR struct i2c_master_s *i2cdev) -{ - FAR struct ds_i2c_inst_s *inst = (FAR struct ds_i2c_inst_s *)i2cdev; - FAR struct onewire_master_s *master = inst->master; - - nxrmutex_unlock(&master->devlock); -} - static int ds_error(uint8_t buf[]) { /* Warnings */ @@ -732,14 +700,14 @@ static int ds_i2c_reset(FAR struct i2c_master_s *i2cdev) FAR struct onewire_master_s *master = inst->master; int ret; - ret = ds_i2c_sem_wait(i2cdev); + ret = nxrmutex_lock(&master->devlock); if (ret < 0) { return ret; } ret = ONEWIRE_RESET(master->dev); - ds_i2c_sem_post(i2cdev); + nxrmutex_unlock(&master->devlock); return ret; } @@ -757,16 +725,18 @@ static int ds_i2c_transfer(FAR struct i2c_master_s *i2cdev, FAR struct i2c_msg_s *msgs, int count) { + FAR struct ds_i2c_inst_s *inst = (FAR struct ds_i2c_inst_s *)i2cdev; + FAR struct onewire_master_s *master = inst->master; int ret; - ret = ds_i2c_sem_wait(i2cdev); + ret = nxrmutex_lock(&master->devlock); if (ret < 0) { return ret; } ret = ds_i2c_process(i2cdev, msgs, count); - ds_i2c_sem_post(i2cdev); + nxrmutex_unlock(&master->devlock); return ret; } diff --git a/drivers/analog/adc.c b/drivers/analog/adc.c index b4fefdfb453da..1fa456f345e06 100644 --- a/drivers/analog/adc.c +++ b/drivers/analog/adc.c @@ -107,7 +107,7 @@ static int adc_open(FAR struct file *filep) * finished. */ - ret = nxsem_wait(&dev->ad_closesem); + ret = nxmutex_lock(&dev->ad_closelock); if (ret >= 0) { /* Increment the count of references to the device. If this is the @@ -158,7 +158,7 @@ static int adc_open(FAR struct file *filep) dev->ad_ocount = tmp; } - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } return ret; @@ -180,7 +180,7 @@ static int adc_close(FAR struct file *filep) irqstate_t flags; int ret; - ret = nxsem_wait(&dev->ad_closesem); + ret = nxmutex_lock(&dev->ad_closelock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -190,7 +190,7 @@ static int adc_close(FAR struct file *filep) if (dev->ad_ocount > 1) { dev->ad_ocount--; - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } else { @@ -204,7 +204,7 @@ static int adc_close(FAR struct file *filep) dev->ad_ops->ao_shutdown(dev); /* Disable the ADC */ leave_critical_section(flags); - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } } @@ -673,10 +673,10 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s *dev) dev->ad_ocount = 0; - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&dev->ad_recv.af_sem, 0, 0); - nxsem_init(&dev->ad_closesem, 0, 1); + nxmutex_init(&dev->ad_closelock); /* The receive semaphore is used for signaling and, hence, should not have * priority inheritance enabled. @@ -695,7 +695,7 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s *dev) if (ret < 0) { nxsem_destroy(&dev->ad_recv.af_sem); - nxsem_destroy(&dev->ad_closesem); + nxmutex_destroy(&dev->ad_closelock); } return ret; diff --git a/drivers/analog/comp.c b/drivers/analog/comp.c index 833fe5d2bc7ec..45ecc8d41a9d0 100644 --- a/drivers/analog/comp.c +++ b/drivers/analog/comp.c @@ -80,15 +80,6 @@ static const struct comp_callback_s g_comp_callback = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: comp_semtake - ****************************************************************************/ - -static int comp_semtake(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /**************************************************************************** * Name: comp_poll ****************************************************************************/ @@ -105,7 +96,7 @@ static int comp_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = comp_semtake(&dev->ad_sem); + ret = nxmutex_lock(&dev->ad_lock); if (ret < 0) { return ret; @@ -159,7 +150,7 @@ static int comp_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&dev->ad_sem); + nxmutex_unlock(&dev->ad_lock); return ret; } @@ -203,7 +194,7 @@ static int comp_open(FAR struct file *filep) * finished. */ - ret = nxsem_wait(&dev->ad_sem); + ret = nxmutex_lock(&dev->ad_lock); if (ret >= 0) { /* Increment the count of references to the device. If this is the @@ -241,7 +232,7 @@ static int comp_open(FAR struct file *filep) } } - nxsem_post(&dev->ad_sem); + nxmutex_unlock(&dev->ad_lock); } return ret; @@ -263,7 +254,7 @@ static int comp_close(FAR struct file *filep) irqstate_t flags; int ret; - ret = nxsem_wait(&dev->ad_sem); + ret = nxmutex_lock(&dev->ad_lock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -273,7 +264,7 @@ static int comp_close(FAR struct file *filep) if (dev->ad_ocount > 1) { dev->ad_ocount--; - nxsem_post(&dev->ad_sem); + nxmutex_unlock(&dev->ad_lock); } else { @@ -287,7 +278,7 @@ static int comp_close(FAR struct file *filep) dev->ad_ops->ao_shutdown(dev); /* Disable the COMP */ leave_critical_section(flags); - nxsem_post(&dev->ad_sem); + nxmutex_unlock(&dev->ad_lock); } } @@ -356,10 +347,9 @@ int comp_register(FAR const char *path, FAR struct comp_dev_s *dev) dev->ad_ocount = 0; - /* Initialize semaphores */ + /* Initialize mutex */ - nxsem_init(&dev->ad_sem, 0, 1); - nxsem_set_protocol(&dev->ad_sem, SEM_PRIO_NONE); + nxmutex_init(&dev->ad_lock); nxsem_init(&dev->ad_readsem, 0, 0); nxsem_set_protocol(&dev->ad_readsem, SEM_PRIO_NONE); @@ -383,7 +373,7 @@ int comp_register(FAR const char *path, FAR struct comp_dev_s *dev) ret = register_driver(path, &comp_fops, 0444, dev); if (ret < 0) { - nxsem_destroy(&dev->ad_sem); + nxmutex_destroy(&dev->ad_lock); } return ret; diff --git a/drivers/analog/dac.c b/drivers/analog/dac.c index f6c849b3500ce..da9bb4c1962f1 100644 --- a/drivers/analog/dac.c +++ b/drivers/analog/dac.c @@ -98,7 +98,7 @@ static int dac_open(FAR struct file *filep) * finished. */ - ret = nxsem_wait(&dev->ad_closesem); + ret = nxmutex_lock(&dev->ad_closelock); if (ret >= 0) { /* Increment the count of references to the device. If this is the @@ -141,7 +141,7 @@ static int dac_open(FAR struct file *filep) } } - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } return ret; @@ -163,7 +163,7 @@ static int dac_close(FAR struct file *filep) irqstate_t flags; int ret; - ret = nxsem_wait(&dev->ad_closesem); + ret = nxmutex_lock(&dev->ad_closelock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -173,7 +173,7 @@ static int dac_close(FAR struct file *filep) if (dev->ad_ocount > 1) { dev->ad_ocount--; - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } else { @@ -194,7 +194,7 @@ static int dac_close(FAR struct file *filep) dev->ad_ops->ao_shutdown(dev); /* Disable the DAC */ leave_critical_section(flags); - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } } @@ -498,10 +498,10 @@ int dac_register(FAR const char *path, FAR struct dac_dev_s *dev) dev->ad_ocount = 0; - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&dev->ad_xmit.af_sem, 0, 0); - nxsem_init(&dev->ad_closesem, 0, 1); + nxmutex_init(&dev->ad_closelock); /* The transmit semaphore is used for signaling and, hence, should not have * priority inheritance enabled. diff --git a/drivers/analog/ltc1867l.c b/drivers/analog/ltc1867l.c index efeade5729f5e..6c355382fc913 100644 --- a/drivers/analog/ltc1867l.c +++ b/drivers/analog/ltc1867l.c @@ -38,7 +38,7 @@ #include #include #include -#include +#include #if defined(CONFIG_ADC_LTC1867L) @@ -61,7 +61,7 @@ struct ltc1867l_dev_s unsigned int devno; FAR struct ltc1867l_channel_config_s *channel_config; int channel_config_count; - sem_t sem; + mutex_t lock; }; /**************************************************************************** @@ -222,7 +222,7 @@ static int adc_ioctl(FAR struct adc_dev_s *dev, int cmd, unsigned long arg) if (cmd == ANIOC_TRIGGER) { - while (nxsem_wait(&priv->sem) < 0); + while (nxmutex_lock(&priv->lock) < 0); adc_lock(spi); @@ -267,7 +267,7 @@ static int adc_ioctl(FAR struct adc_dev_s *dev, int cmd, unsigned long arg) } adc_unlock(spi); - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); } else { @@ -331,7 +331,7 @@ int ltc1867l_register(FAR const char *devpath, FAR struct spi_dev_s *spi, adcpriv->channel_config = channel_config; adcpriv->channel_config_count = channel_config_count; - ret = nxsem_init(&adcpriv->sem, 1, 1); + ret = nxmutex_init(&adcpriv->lock); if (ret < 0) { kmm_free(adcpriv); @@ -342,7 +342,7 @@ int ltc1867l_register(FAR const char *devpath, FAR struct spi_dev_s *spi, if (adcdev == NULL) { aerr("ERROR: Failed to allocate adc_dev_s instance\n"); - nxsem_destroy(&adcpriv->sem); + nxmutex_destroy(&adcpriv->lock); kmm_free(adcpriv); return -ENOMEM; } @@ -358,7 +358,7 @@ int ltc1867l_register(FAR const char *devpath, FAR struct spi_dev_s *spi, { aerr("ERROR: Failed to register adc driver: %d\n", ret); kmm_free(adcdev); - nxsem_destroy(&adcpriv->sem); + nxmutex_destroy(&adcpriv->lock); kmm_free(adcpriv); } diff --git a/drivers/analog/opamp.c b/drivers/analog/opamp.c index 8cc9116dd510a..53211fa083eb6 100644 --- a/drivers/analog/opamp.c +++ b/drivers/analog/opamp.c @@ -86,7 +86,7 @@ static int opamp_open(FAR struct file *filep) * finished. */ - ret = nxsem_wait(&dev->ad_closesem); + ret = nxmutex_lock(&dev->ad_closelock); if (ret >= 0) { /* Increment the count of references to the device. If this is the @@ -124,7 +124,7 @@ static int opamp_open(FAR struct file *filep) } } - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } return ret; @@ -146,7 +146,7 @@ static int opamp_close(FAR struct file *filep) irqstate_t flags; int ret; - ret = nxsem_wait(&dev->ad_closesem); + ret = nxmutex_lock(&dev->ad_closelock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -156,7 +156,7 @@ static int opamp_close(FAR struct file *filep) if (dev->ad_ocount > 1) { dev->ad_ocount--; - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } else { @@ -170,7 +170,7 @@ static int opamp_close(FAR struct file *filep) dev->ad_ops->ao_shutdown(dev); /* Disable the OPAMP */ leave_critical_section(flags); - nxsem_post(&dev->ad_closesem); + nxmutex_unlock(&dev->ad_closelock); } } @@ -207,16 +207,16 @@ int opamp_register(FAR const char *path, FAR struct opamp_dev_s *dev) dev->ad_ocount = 0; - /* Initialize semaphores */ + /* Initialize mutex */ - nxsem_init(&dev->ad_closesem, 0, 1); + nxmutex_init(&dev->ad_closelock); /* Register the OPAMP character driver */ ret = register_driver(path, &opamp_fops, 0444, dev); if (ret < 0) { - nxsem_destroy(&dev->ad_closesem); + nxmutex_destroy(&dev->ad_closelock); } return ret; diff --git a/drivers/audio/cs4344.c b/drivers/audio/cs4344.c index a29080d9ef078..ae956ef2222dd 100644 --- a/drivers/audio/cs4344.c +++ b/drivers/audio/cs4344.c @@ -54,10 +54,6 @@ * Private Function Prototypes ****************************************************************************/ -static int cs4344_takesem(FAR sem_t *sem); -static int cs4344_forcetake(FAR sem_t *sem); -#define cs4344_givesem(s) nxsem_post(s) - static void cs4344_setdatawidth(FAR struct cs4344_dev_s *priv); static void cs4344_setbitrate(FAR struct cs4344_dev_s *priv); @@ -162,57 +158,6 @@ static const struct audio_ops_s g_audioops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: cs4344_takesem - * - * Description: - * Take a semaphore count, handling the nasty EINTR return if we are - * interrupted by a signal. - * - ****************************************************************************/ - -static int cs4344_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: cs4344_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static int cs4344_forcetake(FAR sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - /* Remember the first failure */ - - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: cs4344_setdatawidth * @@ -689,7 +634,7 @@ static int cs4344_sendbuffer(FAR struct cs4344_dev_s *priv) * only while accessing 'inflight'. */ - ret = cs4344_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -748,7 +693,7 @@ static int cs4344_sendbuffer(FAR struct cs4344_dev_s *priv) } } - cs4344_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -940,7 +885,7 @@ static int cs4344_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, audinfo("Enqueueing: apb=%p curbyte=%d nbytes=%d flags=%04x\n", apb, apb->curbyte, apb->nbytes, apb->flags); - ret = cs4344_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -954,7 +899,7 @@ static int cs4344_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED; dq_addlast(&apb->dq_entry, &priv->pendq); - cs4344_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Send a message to the worker thread indicating that a new buffer has * been enqueued. If mq is NULL, then the playing has not yet started. @@ -1064,9 +1009,9 @@ static int cs4344_reserve(FAR struct audio_lowerhalf_s *dev) FAR struct cs4344_dev_s *priv = (FAR struct cs4344_dev_s *)dev; int ret = OK; - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = cs4344_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1092,8 +1037,7 @@ static int cs4344_reserve(FAR struct audio_lowerhalf_s *dev) priv->reserved = true; } - cs4344_givesem(&priv->pendsem); - + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1124,14 +1068,14 @@ static int cs4344_release(FAR struct audio_lowerhalf_s *dev) priv->threadid = 0; } - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = cs4344_forcetake(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); /* Really we should free any queued buffers here */ priv->reserved = false; - cs4344_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1252,7 +1196,7 @@ static void *cs4344_workerthread(pthread_addr_t pvarg) /* Return any pending buffers in our pending queue */ - cs4344_forcetake(&priv->pendsem); + nxmutex_lock(&priv->pendlock); while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq)) != NULL) { /* Release our reference to the buffer */ @@ -1268,7 +1212,7 @@ static void *cs4344_workerthread(pthread_addr_t pvarg) #endif } - cs4344_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Return any pending buffers in our done queue */ @@ -1357,7 +1301,7 @@ FAR struct audio_lowerhalf_s *cs4344_initialize(FAR struct i2s_dev_s *i2s) priv->dev.ops = &g_audioops; priv->i2s = i2s; - nxsem_init(&priv->pendsem, 0, 1); + nxmutex_init(&priv->pendlock); dq_init(&priv->pendq); dq_init(&priv->doneq); @@ -1367,7 +1311,7 @@ FAR struct audio_lowerhalf_s *cs4344_initialize(FAR struct i2s_dev_s *i2s) return &priv->dev; } - nxsem_destroy(&priv->pendsem); + nxmutex_destroy(&priv->pendlock); kmm_free(priv); return NULL; } diff --git a/drivers/audio/cs4344.h b/drivers/audio/cs4344.h index 18377ae0db8af..0244372b392b5 100644 --- a/drivers/audio/cs4344.h +++ b/drivers/audio/cs4344.h @@ -32,6 +32,7 @@ #include #include +#include #include #ifdef CONFIG_AUDIO @@ -71,7 +72,7 @@ struct cs4344_dev_s char mqname[16]; /* Our message queue name */ pthread_t threadid; /* ID of our thread */ uint32_t bitrate; /* Actual programmed bit rate */ - sem_t pendsem; /* Protect pendq */ + mutex_t pendlock; /* Protect pendq */ uint16_t samprate; /* Configured samprate (samples/sec) */ uint8_t nchannels; /* Number of channels (1 or 2) */ uint8_t bpsamp; /* Bits per sample (8 or 16) */ diff --git a/drivers/audio/cs43l22.c b/drivers/audio/cs43l22.c index d458c6fb3b123..ec763e3f31d1c 100644 --- a/drivers/audio/cs43l22.c +++ b/drivers/audio/cs43l22.c @@ -61,9 +61,6 @@ static uint8_t cs43l22_readreg(FAR struct cs43l22_dev_s *priv, uint8_t regaddr); static void cs43l22_writereg(FAR struct cs43l22_dev_s *priv, uint8_t regaddr, uint8_t regval); -static int cs43l22_takesem(FAR sem_t *sem); -static int cs43l22_forcetake(FAR sem_t *sem); -#define cs43l22_givesem(s) nxsem_post(s) #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME static inline uint16_t cs43l22_scalevolume(uint16_t volume, b16_t scale); @@ -346,57 +343,6 @@ cs43l22_writereg(FAR struct cs43l22_dev_s *priv, uint8_t regaddr, } } -/**************************************************************************** - * Name: cs43l22_takesem - * - * Description: - * Take a semaphore count, handling the nasty EINTR return if we are - * interrupted by a signal. - * - ****************************************************************************/ - -static int cs43l22_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: cs43l22_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static int cs43l22_forcetake(FAR sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - /* Remember the first failure */ - - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: cs43l22_scalevolume * @@ -1106,7 +1052,7 @@ static int cs43l22_sendbuffer(FAR struct cs43l22_dev_s *priv) * only while accessing 'inflight'. */ - ret = cs43l22_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1165,7 +1111,7 @@ static int cs43l22_sendbuffer(FAR struct cs43l22_dev_s *priv) } } - cs43l22_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1371,7 +1317,7 @@ static int cs43l22_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, audinfo("Enqueueing: apb=%p curbyte=%d nbytes=%d flags=%04x\n", apb, apb->curbyte, apb->nbytes, apb->flags); - ret = cs43l22_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1385,7 +1331,7 @@ static int cs43l22_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED; dq_addlast(&apb->dq_entry, &priv->pendq); - cs43l22_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Send a message to the worker thread indicating that a new buffer has * been enqueued. If mq is NULL, then the playing has not yet started. @@ -1500,9 +1446,9 @@ static int cs43l22_reserve(FAR struct audio_lowerhalf_s *dev) FAR struct cs43l22_dev_s *priv = (FAR struct cs43l22_dev_s *)dev; int ret = OK; - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = cs43l22_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1528,8 +1474,7 @@ static int cs43l22_reserve(FAR struct audio_lowerhalf_s *dev) priv->reserved = true; } - cs43l22_givesem(&priv->pendsem); - + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1560,14 +1505,14 @@ static int cs43l22_release(FAR struct audio_lowerhalf_s *dev) priv->threadid = 0; } - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = cs43l22_forcetake(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); /* Really we should free any queued buffers here */ priv->reserved = false; - cs43l22_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1731,7 +1676,7 @@ static void *cs43l22_workerthread(pthread_addr_t pvarg) /* Return any pending buffers in our pending queue */ - cs43l22_forcetake(&priv->pendsem); + nxmutex_lock(&priv->pendlock); while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq)) != NULL) { /* Release our reference to the buffer */ @@ -1747,7 +1692,7 @@ static void *cs43l22_workerthread(pthread_addr_t pvarg) #endif } - cs43l22_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Return any pending buffers in our done queue */ @@ -1989,7 +1934,7 @@ FAR struct audio_lowerhalf_s * priv->i2c = i2c; priv->i2s = i2s; - nxsem_init(&priv->pendsem, 0, 1); + nxmutex_init(&priv->pendlock); dq_init(&priv->pendq); dq_init(&priv->doneq); @@ -2024,7 +1969,7 @@ FAR struct audio_lowerhalf_s * return NULL; errout_with_dev: - nxsem_destroy(&priv->pendsem); + nxmutex_destroy(&priv->pendlock); kmm_free(priv); return NULL; } diff --git a/drivers/audio/cs43l22.h b/drivers/audio/cs43l22.h index eb82c95ca9c3c..a697fd9d94ccc 100644 --- a/drivers/audio/cs43l22.h +++ b/drivers/audio/cs43l22.h @@ -330,7 +330,7 @@ struct cs43l22_dev_s char mqname[16]; /* Our message queue name */ pthread_t threadid; /* ID of our thread */ uint32_t bitrate; /* Actual programmed bit rate */ - sem_t pendsem; /* Protect pendq */ + mutex_t pendlock; /* Protect pendq */ #ifdef CS43L22_USE_FFLOCK_INT struct work_s work; /* Interrupt work */ #endif diff --git a/drivers/audio/cxd56.c b/drivers/audio/cxd56.c index ddffd9fe57588..2f27645a4e1be 100644 --- a/drivers/audio/cxd56.c +++ b/drivers/audio/cxd56.c @@ -3727,7 +3727,6 @@ struct audio_lowerhalf_s *cxd56_initialize( priv->lower = lower; priv->state = CXD56_DEV_STATE_OFF; - nxsem_init(&priv->pendsem, 0, 1); dq_init(&priv->up_pendq); dq_init(&priv->up_runq); #ifdef CONFIG_AUDIO_CXD56_SRC diff --git a/drivers/audio/cxd56.h b/drivers/audio/cxd56.h index cedb1f1918aad..869bfdb681e33 100644 --- a/drivers/audio/cxd56.h +++ b/drivers/audio/cxd56.h @@ -269,7 +269,6 @@ struct cxd56_dev_s struct file mq; /* Message queue for receiving messages */ char mqname[16]; /* Our message queue name */ pthread_t threadid; /* ID of our thread */ - sem_t pendsem; /* Protect pendq */ struct dq_queue_s up_pendq; /* Pending buffers from app to process */ struct dq_queue_s up_runq; /* Buffers from app being played */ diff --git a/drivers/audio/cxd56_src.c b/drivers/audio/cxd56_src.c index 6a8a652daa9fe..6f32a2ad7ee34 100644 --- a/drivers/audio/cxd56_src.c +++ b/drivers/audio/cxd56_src.c @@ -78,7 +78,6 @@ struct cxd56_srcdata_s char mqname[32]; struct file mq; - sem_t pendsem; pthread_t threadid; uint8_t bytewidth; diff --git a/drivers/audio/tone.c b/drivers/audio/tone.c index b3bfeb4aaaaef..3e397fa4aada6 100644 --- a/drivers/audio/tone.c +++ b/drivers/audio/tone.c @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -85,7 +86,7 @@ struct tone_upperhalf_s uint8_t channel; /* Output channel that drives the tone. */ #endif volatile bool started; /* True: pulsed output is being generated */ - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ struct pwm_info_s tone; /* Pulsed output for Audio Tone */ struct pwm_lowerhalf_s *devtone; struct oneshot_lowerhalf_s *oneshot; @@ -772,7 +773,7 @@ static int tone_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -789,7 +790,7 @@ static int tone_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Save the new open count on success */ @@ -797,8 +798,8 @@ static int tone_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&upper->lock); errout: return ret; @@ -822,7 +823,7 @@ static int tone_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -838,7 +839,7 @@ static int tone_close(FAR struct file *filep) upper->crefs--; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); ret = OK; errout: @@ -958,7 +959,7 @@ int tone_register(FAR const char *path, FAR struct pwm_lowerhalf_s *tone, * kmm_zalloc()). */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); upper->devtone = tone; upper->oneshot = oneshot; #ifdef CONFIG_PWM_MULTICHAN diff --git a/drivers/audio/vs1053.c b/drivers/audio/vs1053.c index d722453fbcf7c..4da14b29c6299 100644 --- a/drivers/audio/vs1053.c +++ b/drivers/audio/vs1053.c @@ -106,7 +106,7 @@ struct vs1053_struct_s struct file mq; /* Message queue for receiving messages */ char mqname[16]; /* Our message queue name */ pthread_t threadid; /* ID of our thread */ - sem_t apbq_sem; /* Audio Pipeline Buffer Queue sem access */ + mutex_t apbq_lock; /* Audio Pipeline Buffer Queue mutex access */ #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME int16_t volume; /* Current volume level */ #ifndef CONFIG_AUDIO_EXCLUDE_BALANCE @@ -1194,7 +1194,7 @@ static void vs1053_feeddata(FAR struct vs1053_struct_s *dev) /* Lock the buffer queue to pop the next buffer */ - if ((ret = nxsem_wait(&dev->apbq_sem)) < 0) + if ((ret = nxmutex_lock(&dev->apbq_lock)) < 0) { #ifdef CONFIG_AUDIO_MULTI_SESSION dev->lower.upper(dev->lower.priv, @@ -1219,13 +1219,13 @@ static void vs1053_feeddata(FAR struct vs1053_struct_s *dev) if (apb == NULL) { - nxsem_post(&dev->apbq_sem); + nxmutex_unlock(&dev->apbq_lock); break; } samp = &apb->samp[apb->curbyte]; apb_reference(apb); /* Add our buffer reference */ - nxsem_post(&dev->apbq_sem); + nxmutex_unlock(&dev->apbq_lock); } } } @@ -1388,7 +1388,7 @@ static void *vs1053_workerthread(pthread_addr_t pvarg) /* Cancel any leftover buffer in our queue */ - if (nxsem_wait(&dev->apbq_sem) == OK) + if (nxmutex_lock(&dev->apbq_lock) == OK) { /* Get the next buffer from the queue */ @@ -1397,7 +1397,7 @@ static void *vs1053_workerthread(pthread_addr_t pvarg) ; } - nxsem_post(&dev->apbq_sem); + nxmutex_unlock(&dev->apbq_lock); /* Free the active buffer */ @@ -1487,11 +1487,11 @@ static int vs1053_start(FAR struct audio_lowerhalf_s *lower) /* Pop the first enqueued buffer */ - if ((ret = nxsem_wait(&dev->apbq_sem)) == OK) + if ((ret = nxmutex_lock(&dev->apbq_lock)) == OK) { dev->apb = (FAR struct ap_buffer_s *) dq_remfirst(&dev->apbq); apb_reference(dev->apb); /* Add our buffer reference */ - nxsem_post(&dev->apbq_sem); + nxmutex_unlock(&dev->apbq_lock); } else { @@ -1655,14 +1655,14 @@ static int vs1053_enqueuebuffer(FAR struct audio_lowerhalf_s *lower, /* Lock access to the apbq */ - if ((ret = nxsem_wait(&dev->apbq_sem)) == OK) + if ((ret = nxmutex_lock(&dev->apbq_lock)) == OK) { /* We can now safely add the buffer to the queue */ apb->curbyte = 0; apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED; dq_addlast(&apb->dq_entry, &dev->apbq); - nxsem_post(&dev->apbq_sem); + nxmutex_unlock(&dev->apbq_lock); /* Send a message indicating a new buffer enqueued */ @@ -1754,9 +1754,9 @@ static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower) FAR struct vs1053_struct_s *dev = (struct vs1053_struct_s *) lower; int ret; - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = nxsem_wait(&dev->apbq_sem); + ret = nxmutex_lock(&dev->apbq_lock); if (ret < 0) { return ret; @@ -1778,8 +1778,7 @@ static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower) dev->paused = false; } - nxsem_post(&dev->apbq_sem); - + nxmutex_unlock(&dev->apbq_lock); return ret; } @@ -1809,9 +1808,9 @@ static int vs1053_release(FAR struct audio_lowerhalf_s *lower) dev->threadid = 0; } - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = nxsem_wait(&dev->apbq_sem); + ret = nxmutex_lock(&dev->apbq_lock); if (ret < 0) { return ret; @@ -1820,7 +1819,7 @@ static int vs1053_release(FAR struct audio_lowerhalf_s *lower) /* Really we should free any queued buffers here */ dev->busy = false; - nxsem_post(&dev->apbq_sem); + nxmutex_unlock(&dev->apbq_lock); return OK; } @@ -1875,7 +1874,7 @@ struct audio_lowerhalf_s *vs1053_initialize(FAR struct spi_dev_s *spi, #endif #endif - nxsem_init(&dev->apbq_sem, 0, 1); + nxmutex_init(&dev->apbq_lock); dq_init(&dev->apbq); /* Reset the VS1053 chip */ diff --git a/drivers/audio/wm8776.c b/drivers/audio/wm8776.c index 2608fe0015bf5..ee11f2e95fd77 100644 --- a/drivers/audio/wm8776.c +++ b/drivers/audio/wm8776.c @@ -58,11 +58,6 @@ static void wm8776_writereg(FAR struct wm8776_dev_s *priv, uint8_t regaddr, uint16_t regval); - -static int wm8776_takesem(FAR sem_t *sem); -static int wm8776_forcetake(FAR sem_t *sem); -#define wm8776_givesem(s) nxsem_post(s) - static int wm8776_getcaps(FAR struct audio_lowerhalf_s *dev, int type, FAR struct audio_caps_s *caps); #ifdef CONFIG_AUDIO_MULTI_SESSION @@ -194,57 +189,6 @@ static void wm8776_writereg(FAR struct wm8776_dev_s *priv, } } -/**************************************************************************** - * Name: wm8776_takesem - * - * Description: - * Take a semaphore count, handling the nasty EINTR return if we are - * interrupted by a signal. - * - ****************************************************************************/ - -static int wm8776_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: wm8776_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static int wm8776_forcetake(FAR sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - /* Remember the first failure */ - - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: wm8776_setvolume * @@ -679,7 +623,7 @@ static int wm8776_sendbuffer(FAR struct wm8776_dev_s *priv) * only while accessing 'inflight'. */ - ret = wm8776_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -716,7 +660,7 @@ static int wm8776_sendbuffer(FAR struct wm8776_dev_s *priv) } } - wm8776_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -914,7 +858,7 @@ static int wm8776_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, /* Add the new buffer to the tail of pending audio buffers */ - ret = wm8776_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -922,7 +866,7 @@ static int wm8776_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED; dq_addlast(&apb->dq_entry, &priv->pendq); - wm8776_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Send a message to the worker thread indicating that a new buffer has * been enqueued. If mq is NULL, then the playing has not yet started. @@ -1034,9 +978,9 @@ static int wm8776_reserve(FAR struct audio_lowerhalf_s *dev) FAR struct wm8776_dev_s *priv = (FAR struct wm8776_dev_s *) dev; int ret = OK; - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = wm8776_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1062,8 +1006,7 @@ static int wm8776_reserve(FAR struct audio_lowerhalf_s *dev) priv->reserved = true; } - wm8776_givesem(&priv->pendsem); - + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1093,14 +1036,14 @@ static int wm8776_release(FAR struct audio_lowerhalf_s *dev) priv->threadid = 0; } - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = wm8776_forcetake(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); /* Really we should free any queued buffers here */ priv->reserved = false; - wm8776_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1294,7 +1237,7 @@ static void *wm8776_workerthread(pthread_addr_t pvarg) /* Return any pending buffers in our pending queue */ - wm8776_forcetake(&priv->pendsem); + nxmutex_lock(&priv->pendlock); while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq)) != NULL) { /* Release our reference to the buffer */ @@ -1310,7 +1253,7 @@ static void *wm8776_workerthread(pthread_addr_t pvarg) #endif } - wm8776_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Return any pending buffers in our done queue */ @@ -1376,7 +1319,7 @@ FAR struct audio_lowerhalf_s * priv->i2c = i2c; priv->i2s = i2s; - nxsem_init(&priv->pendsem, 0, 1); + nxmutex_init(&priv->pendlock); dq_init(&priv->pendq); dq_init(&priv->doneq); diff --git a/drivers/audio/wm8776.h b/drivers/audio/wm8776.h index 83b325bc3d339..9b304f0e11be9 100644 --- a/drivers/audio/wm8776.h +++ b/drivers/audio/wm8776.h @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -85,7 +86,7 @@ struct wm8776_dev_s char mqname[16]; /* Our message queue name */ pthread_t threadid; /* ID of our thread */ uint32_t bitrate; /* Actual programmed bit rate */ - sem_t pendsem; /* Protect pendq */ + mutex_t pendlock; /* Protect pendq */ uint16_t samprate; /* Configured samprate (samples/sec) */ #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME #ifndef CONFIG_AUDIO_EXCLUDE_BALANCE diff --git a/drivers/audio/wm8904.c b/drivers/audio/wm8904.c index 301a6124fab1c..29a5ee5b319f8 100644 --- a/drivers/audio/wm8904.c +++ b/drivers/audio/wm8904.c @@ -77,9 +77,6 @@ static uint8_t regaddr); static void wm8904_writereg(FAR struct wm8904_dev_s *priv, uint8_t regaddr, uint16_t regval); -static int wm8904_takesem(FAR sem_t *sem); -static int wm8904_forcetake(FAR sem_t *sem); -#define wm8904_givesem(s) nxsem_post(s) #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME static inline uint16_t wm8904_scalevolume(uint16_t volume, b16_t scale); @@ -391,57 +388,6 @@ static void wm8904_writereg(FAR struct wm8904_dev_s *priv, uint8_t regaddr, } } -/**************************************************************************** - * Name: wm8904_takesem - * - * Description: - * Take a semaphore count, handling the nasty EINTR return if we are - * interrupted by a signal. - * - ****************************************************************************/ - -static int wm8904_takesem(sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: wm8904_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static int wm8904_forcetake(FAR sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - /* Remember the first failure */ - - ret = result; - } - } - while (result < 0); - - return ret; -} - /**************************************************************************** * Name: wm8904_scalevolume * @@ -1529,7 +1475,7 @@ static int wm8904_sendbuffer(FAR struct wm8904_dev_s *priv) * only while accessing 'inflight'. */ - ret = wm8904_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1588,7 +1534,7 @@ static int wm8904_sendbuffer(FAR struct wm8904_dev_s *priv) } } - wm8904_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1795,7 +1741,7 @@ static int wm8904_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, /* Add the new buffer to the tail of pending audio buffers */ - ret = wm8904_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1803,7 +1749,7 @@ static int wm8904_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED; dq_addlast(&apb->dq_entry, &priv->pendq); - wm8904_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Send a message to the worker thread indicating that a new buffer has * been enqueued. If mq is NULL, then the playing has not yet started. @@ -1915,9 +1861,9 @@ static int wm8904_reserve(FAR struct audio_lowerhalf_s *dev) FAR struct wm8904_dev_s *priv = (FAR struct wm8904_dev_s *) dev; int ret; - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = wm8904_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1943,8 +1889,7 @@ static int wm8904_reserve(FAR struct audio_lowerhalf_s *dev) priv->reserved = true; } - wm8904_givesem(&priv->pendsem); - + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1974,14 +1919,14 @@ static int wm8904_release(FAR struct audio_lowerhalf_s *dev) priv->threadid = 0; } - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - ret = wm8904_forcetake(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); /* Really we should free any queued buffers here */ priv->reserved = false; - wm8904_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -2200,7 +2145,7 @@ static void *wm8904_workerthread(pthread_addr_t pvarg) /* Return any pending buffers in our pending queue */ - wm8904_forcetake(&priv->pendsem); + nxmutex_lock(&priv->pendlock); while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq)) != NULL) { /* Release our reference to the buffer */ @@ -2216,7 +2161,7 @@ static void *wm8904_workerthread(pthread_addr_t pvarg) #endif } - wm8904_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Return any pending buffers in our done queue */ @@ -2601,7 +2546,7 @@ FAR struct audio_lowerhalf_s * priv->i2c = i2c; priv->i2s = i2s; - nxsem_init(&priv->pendsem, 0, 1); + nxmutex_init(&priv->pendlock); dq_init(&priv->pendq); dq_init(&priv->doneq); @@ -2630,7 +2575,7 @@ FAR struct audio_lowerhalf_s * return NULL; errout_with_dev: - nxsem_destroy(&priv->pendsem); + nxmutex_destroy(&priv->pendlock); kmm_free(priv); return NULL; } diff --git a/drivers/audio/wm8904.h b/drivers/audio/wm8904.h index 4acfb4a7e8b87..d92840eb6594c 100644 --- a/drivers/audio/wm8904.h +++ b/drivers/audio/wm8904.h @@ -37,6 +37,7 @@ #include #include +#include #include #ifdef CONFIG_AUDIO @@ -1091,7 +1092,7 @@ struct wm8904_dev_s char mqname[16]; /* Our message queue name */ pthread_t threadid; /* ID of our thread */ uint32_t bitrate; /* Actual programmed bit rate */ - sem_t pendsem; /* Protect pendq */ + mutex_t pendlock; /* Protect pendq */ #ifdef WM8904_USE_FFLOCK_INT struct work_s work; /* Interrupt work */ #endif diff --git a/drivers/audio/wm8994.c b/drivers/audio/wm8994.c index d1215a1d6de27..5f77f2e2b6571 100644 --- a/drivers/audio/wm8994.c +++ b/drivers/audio/wm8994.c @@ -84,8 +84,6 @@ static uint16_t regaddr); static void wm8994_writereg(FAR struct wm8994_dev_s *priv, uint16_t regaddr, uint16_t regval); -static int wm8994_takesem(FAR sem_t *sem); -#define wm8994_givesem(s) nxsem_post(s) #ifndef CONFIG_AUDIO_EXCLUDE_VOLUME static inline uint16_t wm8994_scalevolume(uint16_t volume, b16_t scale); @@ -443,19 +441,6 @@ static void wm8994_setsamplefreq(FAR struct wm8994_dev_s *priv) wm8994_writereg(priv, WM8994_AIF1_RATE, regval); } -/* Name: wm8994_takesem - * - * Description: - * Take a semaphore count, handling the nasty EINTR return if we are - * interrupted by a signal. - * - */ - -static int wm8994_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /* Name: wm8994_scalevolume * * Description: @@ -1186,7 +1171,7 @@ static int wm8994_sendbuffer(FAR struct wm8994_dev_s *priv) * only while accessing 'inflight'. */ - ret = wm8994_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1245,7 +1230,7 @@ static int wm8994_sendbuffer(FAR struct wm8994_dev_s *priv) } } - wm8994_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1444,7 +1429,7 @@ static int wm8994_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, /* Add the new buffer to the tail of pending audio buffers */ - ret = wm8994_takesem(&priv->pendsem); + ret = nxmutex_lock(&priv->pendlock); if (ret < 0) { return ret; @@ -1452,7 +1437,7 @@ static int wm8994_enqueuebuffer(FAR struct audio_lowerhalf_s *dev, apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED; dq_addlast(&apb->dq_entry, &priv->pendq); - wm8994_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Send a message to the worker thread indicating that a new buffer has * been enqueued. If mq is NULL, then the playing has not yet started. @@ -1561,9 +1546,9 @@ static int wm8994_reserve(FAR struct audio_lowerhalf_s *dev) FAR struct wm8994_dev_s *priv = (FAR struct wm8994_dev_s *) dev; int ret = OK; - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - wm8994_takesem(&priv->pendsem); + nxmutex_lock(&priv->pendlock); if (priv->reserved) { ret = -EBUSY; @@ -1584,8 +1569,7 @@ static int wm8994_reserve(FAR struct audio_lowerhalf_s *dev) priv->reserved = true; } - wm8994_givesem(&priv->pendsem); - + nxmutex_unlock(&priv->pendlock); return ret; } @@ -1613,14 +1597,14 @@ static int wm8994_release(FAR struct audio_lowerhalf_s *dev) priv->threadid = 0; } - /* Borrow the APBQ semaphore for thread sync */ + /* Borrow the APBQ mutex for thread sync */ - wm8994_takesem(&priv->pendsem); + nxmutex_lock(&priv->pendlock); /* Really we should free any queued buffers here */ priv->reserved = false; - wm8994_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); return OK; } @@ -1837,7 +1821,7 @@ static void *wm8994_workerthread(pthread_addr_t pvarg) /* Return any pending buffers in our pending queue */ - wm8994_takesem(&priv->pendsem); + nxmutex_lock(&priv->pendlock); while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&priv->pendq)) != NULL) { /* Release our reference to the buffer */ @@ -1853,7 +1837,7 @@ static void *wm8994_workerthread(pthread_addr_t pvarg) #endif } - wm8994_givesem(&priv->pendsem); + nxmutex_unlock(&priv->pendlock); /* Return any pending buffers in our done queue */ @@ -2814,7 +2798,7 @@ FAR struct audio_lowerhalf_s * priv->i2c = i2c; priv->i2s = i2s; - nxsem_init(&priv->pendsem, 0, 1); + nxmutex_init(&priv->pendlock); dq_init(&priv->pendq); dq_init(&priv->doneq); @@ -2849,7 +2833,7 @@ FAR struct audio_lowerhalf_s * return NULL; errout_with_dev: - nxsem_destroy(&priv->pendsem); + nxmutex_destroy(&priv->pendlock); kmm_free(priv); return NULL; } diff --git a/drivers/audio/wm8994.h b/drivers/audio/wm8994.h index 0a2ecffc555d3..5289753692667 100644 --- a/drivers/audio/wm8994.h +++ b/drivers/audio/wm8994.h @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -1822,7 +1823,7 @@ struct wm8994_dev_s char mqname[16]; /* Our message queue name */ pthread_t threadid; /* ID of our thread */ uint32_t bitrate; /* Actual programmed bit rate */ - sem_t pendsem; /* Protect pendq */ + mutex_t pendlock; /* Protect pendq */ #ifdef WM8994_USE_FFLOCK_INT struct work_s work; /* Interrupt work */ #endif diff --git a/drivers/bch/Make.defs b/drivers/bch/Make.defs index 24e430e221759..42993e2d6936c 100644 --- a/drivers/bch/Make.defs +++ b/drivers/bch/Make.defs @@ -23,7 +23,7 @@ ifeq ($(CONFIG_BCH),y) # Include BCH driver CSRCS += bchlib_setup.c bchlib_teardown.c bchlib_read.c bchlib_write.c -CSRCS += bchlib_cache.c bchlib_sem.c bchdev_register.c bchdev_unregister.c +CSRCS += bchlib_cache.c bchdev_register.c bchdev_unregister.c CSRCS += bchdev_driver.c # Include BCH driver build support diff --git a/drivers/bch/bch.h b/drivers/bch/bch.h index cf4171a602a54..c766a89093357 100644 --- a/drivers/bch/bch.h +++ b/drivers/bch/bch.h @@ -31,14 +31,13 @@ #include #include -#include +#include #include /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define bchlib_semgive(d) nxsem_post(&(d)->sem) /* To match bchlib_semtake */ #define MAX_OPENCNT (255) /* Limit of uint8_t */ /**************************************************************************** @@ -51,7 +50,7 @@ struct bchlib_s uint32_t sectsize; /* The size of one sector on the device */ size_t nsectors; /* Number of sectors supported by the device */ size_t sector; /* The current sector in the buffer */ - sem_t sem; /* For atomic accesses to this structure */ + mutex_t lock; /* For atomic accesses to this structure */ uint8_t refs; /* Number of references */ bool dirty; /* true: Data has been written to the buffer */ bool readonly; /* true: Only read operations are supported */ @@ -82,7 +81,6 @@ EXTERN const struct file_operations bch_fops; * Public Function Prototypes ****************************************************************************/ -EXTERN int bchlib_semtake(FAR struct bchlib_s *bch); EXTERN int bchlib_flushsector(FAR struct bchlib_s *bch); EXTERN int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector); diff --git a/drivers/bch/bchdev_driver.c b/drivers/bch/bchdev_driver.c index 6de05af454696..d807822cce68c 100644 --- a/drivers/bch/bchdev_driver.c +++ b/drivers/bch/bchdev_driver.c @@ -121,7 +121,7 @@ static int bch_open(FAR struct file *filep) /* Increment the reference count */ - ret = bchlib_semtake(bch); + ret = nxmutex_lock(&bch->lock); if (ret < 0) { return ret; @@ -136,7 +136,7 @@ static int bch_open(FAR struct file *filep) bch->refs++; } - bchlib_semgive(bch); + nxmutex_unlock(&bch->lock); return ret; } @@ -158,7 +158,7 @@ static int bch_close(FAR struct file *filep) /* Get exclusive access */ - ret = bchlib_semtake(bch); + ret = nxmutex_lock(&bch->lock); if (ret < 0) { return ret; @@ -206,7 +206,7 @@ static int bch_close(FAR struct file *filep) } } - bchlib_semgive(bch); + nxmutex_unlock(&bch->lock); return ret; } @@ -224,7 +224,7 @@ static off_t bch_seek(FAR struct file *filep, off_t offset, int whence) DEBUGASSERT(inode && inode->i_private); bch = (FAR struct bchlib_s *)inode->i_private; - ret = bchlib_semtake(bch); + ret = nxmutex_lock(&bch->lock); if (ret < 0) { return ret; @@ -250,7 +250,7 @@ static off_t bch_seek(FAR struct file *filep, off_t offset, int whence) /* Return EINVAL if the whence argument is invalid */ - bchlib_semgive(bch); + nxmutex_unlock(&bch->lock); return -EINVAL; } @@ -278,7 +278,7 @@ static off_t bch_seek(FAR struct file *filep, off_t offset, int whence) ret = -EINVAL; } - bchlib_semgive(bch); + nxmutex_unlock(&bch->lock); return ret; } @@ -295,7 +295,7 @@ static ssize_t bch_read(FAR struct file *filep, FAR char *buffer, size_t len) DEBUGASSERT(inode && inode->i_private); bch = (FAR struct bchlib_s *)inode->i_private; - ret = bchlib_semtake(bch); + ret = nxmutex_lock(&bch->lock); if (ret < 0) { return ret; @@ -307,7 +307,7 @@ static ssize_t bch_read(FAR struct file *filep, FAR char *buffer, size_t len) filep->f_pos += ret; } - bchlib_semgive(bch); + nxmutex_unlock(&bch->lock); return ret; } @@ -327,7 +327,7 @@ static ssize_t bch_write(FAR struct file *filep, FAR const char *buffer, if (!bch->readonly) { - ret = bchlib_semtake(bch); + ret = nxmutex_lock(&bch->lock); if (ret < 0) { return ret; @@ -339,7 +339,7 @@ static ssize_t bch_write(FAR struct file *filep, FAR const char *buffer, filep->f_pos += ret; } - bchlib_semgive(bch); + nxmutex_unlock(&bch->lock); } return ret; @@ -373,7 +373,7 @@ static int bch_ioctl(FAR struct file *filep, int cmd, unsigned long arg) FAR struct bchlib_s **bchr = (FAR struct bchlib_s **)((uintptr_t)arg); - ret = bchlib_semtake(bch); + ret = nxmutex_lock(&bch->lock); if (ret < 0) { return ret; @@ -390,7 +390,7 @@ static int bch_ioctl(FAR struct file *filep, int cmd, unsigned long arg) ret = OK; } - bchlib_semgive(bch); + nxmutex_unlock(&bch->lock); } break; @@ -476,7 +476,7 @@ static int bch_unlink(FAR struct inode *inode) /* Get exclusive access to the BCH device */ - ret = bchlib_semtake(bch); + ret = nxmutex_lock(&bch->lock); if (ret < 0) { return ret; @@ -510,7 +510,7 @@ static int bch_unlink(FAR struct inode *inode) } } - bchlib_semgive(bch); + nxmutex_unlock(&bch->lock); return ret; } #endif diff --git a/drivers/bch/bchlib_sem.c b/drivers/bch/bchlib_sem.c deleted file mode 100644 index bb12c8868ab6c..0000000000000 --- a/drivers/bch/bchlib_sem.c +++ /dev/null @@ -1,44 +0,0 @@ -/**************************************************************************** - * drivers/bch/bchlib_sem.c - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include - -#include "bch.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: bch_semtake - ****************************************************************************/ - -int bchlib_semtake(FAR struct bchlib_s *bch) -{ - return nxsem_wait(&bch->sem); -} diff --git a/drivers/bch/bchlib_setup.c b/drivers/bch/bchlib_setup.c index 9c57c2021fc1a..2d37f06607c2a 100644 --- a/drivers/bch/bchlib_setup.c +++ b/drivers/bch/bchlib_setup.c @@ -104,7 +104,7 @@ int bchlib_setup(const char *blkdev, bool readonly, FAR void **handle) /* Save the geometry info and complete initialization of the structure */ - nxsem_init(&bch->sem, 0, 1); + nxmutex_init(&bch->lock); bch->nsectors = geo.geo_nsectors; bch->sectsize = geo.geo_sectorsize; bch->sector = (size_t)-1; diff --git a/drivers/bch/bchlib_teardown.c b/drivers/bch/bchlib_teardown.c index 17b06b247ddc5..4048cd3fa5e58 100644 --- a/drivers/bch/bchlib_teardown.c +++ b/drivers/bch/bchlib_teardown.c @@ -75,7 +75,7 @@ int bchlib_teardown(FAR void *handle) kmm_free(bch->buffer); } - nxsem_destroy(&bch->sem); + nxmutex_destroy(&bch->lock); kmm_free(bch); return OK; } diff --git a/drivers/can/can.c b/drivers/can/can.c index 052caa5fc2689..d541a57428cf8 100644 --- a/drivers/can/can.c +++ b/drivers/can/can.c @@ -96,10 +96,6 @@ * Private Function Prototypes ****************************************************************************/ -/* Semaphore helpers */ - -static int can_takesem(FAR sem_t *sem); - /* CAN helpers */ static uint8_t can_dlc2bytes(uint8_t dlc); @@ -149,21 +145,6 @@ static const struct file_operations g_canops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: can_takesem - ****************************************************************************/ - -static int can_takesem(FAR sem_t *sem) -{ - return nxsem_wait(sem); -} - -/**************************************************************************** - * Name: can_givesem - ****************************************************************************/ - -#define can_givesem(sem) nxsem_post(sem) - /**************************************************************************** * Name: can_dlc2bytes * @@ -326,7 +307,7 @@ static void can_txready_work(FAR void *arg) { /* Yes.. Inform them that new xmit space is available */ - can_givesem(&dev->cd_xmit.tx_sem); + nxsem_post(&dev->cd_xmit.tx_sem); } } } @@ -367,7 +348,7 @@ static int can_open(FAR struct file *filep) /* If the port is the middle of closing, wait until the close is finished */ - ret = can_takesem(&dev->cd_closesem); + ret = nxmutex_lock(&dev->cd_closelock); if (ret < 0) { return ret; @@ -424,7 +405,7 @@ static int can_open(FAR struct file *filep) } errout: - can_givesem(&dev->cd_closesem); + nxmutex_unlock(&dev->cd_closelock); return ret; } @@ -450,7 +431,7 @@ static int can_close(FAR struct file *filep) caninfo("ocount: %u\n", dev->cd_crefs); #endif - ret = can_takesem(&dev->cd_closesem); + ret = nxmutex_lock(&dev->cd_closelock); if (ret < 0) { return ret; @@ -502,7 +483,7 @@ static int can_close(FAR struct file *filep) leave_critical_section(flags); errout: - can_givesem(&dev->cd_closesem); + nxmutex_unlock(&dev->cd_closelock); return ret; } @@ -597,7 +578,7 @@ static ssize_t can_read(FAR struct file *filep, FAR char *buffer, } else { - ret = can_takesem(&fifo->rx_sem); + ret = nxsem_wait(&fifo->rx_sem); } if (ret < 0) @@ -650,7 +631,7 @@ static ssize_t can_read(FAR struct file *filep, FAR char *buffer, * don't block. */ - can_givesem(&fifo->rx_sem); + nxsem_post(&fifo->rx_sem); } /* Return the number of bytes that were read. */ @@ -830,7 +811,7 @@ static ssize_t can_write(FAR struct file *filep, FAR const char *buffer, DEBUGASSERT(dev->cd_ntxwaiters < 255); dev->cd_ntxwaiters++; - ret = can_takesem(&fifo->tx_sem); + ret = nxsem_wait(&fifo->tx_sem); dev->cd_ntxwaiters--; if (ret < 0) { @@ -1059,7 +1040,7 @@ static int can_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Get exclusive access to the poll structures */ - ret = can_takesem(&dev->cd_pollsem); + ret = nxmutex_lock(&dev->cd_polllock); if (ret < 0) { /* A signal received while waiting for access to the poll data @@ -1112,7 +1093,7 @@ static int can_poll(FAR struct file *filep, FAR struct pollfd *fds, dev->cd_ntxwaiters++; do { - ret = can_takesem(&dev->cd_xmit.tx_sem); + ret = nxsem_wait(&dev->cd_xmit.tx_sem); } while (ret < 0); dev->cd_ntxwaiters--; @@ -1128,7 +1109,7 @@ static int can_poll(FAR struct file *filep, FAR struct pollfd *fds, eventset |= POLLOUT; } - can_givesem(&dev->cd_xmit.tx_sem); + nxsem_post(&dev->cd_xmit.tx_sem); /* Check whether there are messages in the RX FIFO. */ @@ -1176,7 +1157,7 @@ static int can_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - can_givesem(&dev->cd_pollsem); + nxmutex_unlock(&dev->cd_polllock); return_with_irqdisabled: leave_critical_section(flags); @@ -1213,8 +1194,8 @@ int can_register(FAR const char *path, FAR struct can_dev_s *dev) nxsem_init(&dev->cd_xmit.tx_sem, 0, 1); nxsem_set_protocol(&dev->cd_xmit.tx_sem, SEM_PRIO_NONE); - nxsem_init(&dev->cd_closesem, 0, 1); - nxsem_init(&dev->cd_pollsem, 0, 1); + nxmutex_init(&dev->cd_closelock); + nxmutex_init(&dev->cd_polllock); for (i = 0; i < CONFIG_CAN_NPENDINGRTR; i++) { @@ -1323,7 +1304,7 @@ int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr, /* Restart the waiting thread and mark the entry unused */ - can_givesem(&wait->cr_sem); + nxsem_post(&wait->cr_sem); } } } @@ -1390,7 +1371,7 @@ int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr, if (sval <= 0) { - can_givesem(&fifo->rx_sem); + nxsem_post(&fifo->rx_sem); } errcode = OK; @@ -1522,7 +1503,7 @@ int can_txdone(FAR struct can_dev_s *dev) { /* Yes.. Inform them that new xmit space is available */ - ret = can_givesem(&dev->cd_xmit.tx_sem); + ret = nxsem_post(&dev->cd_xmit.tx_sem); } else { diff --git a/drivers/can/mcp2515.c b/drivers/can/mcp2515.c index b8cfc70595a38..38216483310dd 100644 --- a/drivers/can/mcp2515.c +++ b/drivers/can/mcp2515.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -198,7 +199,7 @@ struct mcp2515_can_s struct mcp2515_config_s *config; /* The constant configuration */ uint8_t state; /* See enum can_state_s */ uint8_t nalloc; /* Number of allocated filters */ - sem_t locksem; /* Enforces mutually exclusive access */ + mutex_t lock; /* Enforces mutually exclusive access */ sem_t txfsem; /* Used to wait for TX FIFO availability */ uint32_t btp; /* Current bit timing */ uint8_t rxints; /* Configured RX interrupts */ @@ -238,11 +239,6 @@ static void mcp2515_dumpregs(FAR struct mcp2515_can_s *priv, # define mcp2515_dumpregs(priv,msg) #endif -/* Semaphore helpers */ - -static int mcp2515_dev_lock(FAR struct mcp2515_can_s *priv); -#define mcp2515_dev_unlock(priv) nxsem_post(&priv->locksem) - /* MCP2515 helpers */ #ifdef CONFIG_CAN_EXTID @@ -515,26 +511,6 @@ static void mcp2515_dumpregs(FAR struct mcp2515_can_s *priv, } #endif -/**************************************************************************** - * Name: mcp2515_dev_lock - * - * Description: - * Take the semaphore that enforces mutually exclusive access to device - * structures, handling any exceptional conditions - * - * Input Parameters: - * priv - A reference to the MCP2515 peripheral state - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int mcp2515_dev_lock(FAR struct mcp2515_can_s *priv) -{ - return nxsem_wait(&priv->locksem); -} - /**************************************************************************** * Name: mcp2515_add_extfilter * @@ -567,7 +543,7 @@ static int mcp2515_add_extfilter(FAR struct mcp2515_can_s *priv, /* Get exclusive excess to the MCP2515 hardware */ - ret = mcp2515_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -764,12 +740,12 @@ static int mcp2515_add_extfilter(FAR struct mcp2515_can_s *priv, regval = (regval & ~CANCTRL_REQOP_MASK) | (CANCTRL_REQOP_NORMAL); mcp2515_writeregs(priv, MCP2515_CANCTRL, ®val, 1); - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return ndx; } } - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -EAGAIN; } #endif @@ -815,7 +791,7 @@ static int mcp2515_del_extfilter(FAR struct mcp2515_can_s *priv, int ndx) /* Get exclusive excess to the MCP2515 hardware */ - ret = mcp2515_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -827,7 +803,7 @@ static int mcp2515_del_extfilter(FAR struct mcp2515_can_s *priv, int ndx) { /* No, error out */ - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -ENOENT; } @@ -874,7 +850,7 @@ static int mcp2515_del_extfilter(FAR struct mcp2515_can_s *priv, int ndx) regval = (regval & ~CANCTRL_REQOP_MASK) | (CANCTRL_REQOP_NORMAL); mcp2515_writeregs(priv, MCP2515_CANCTRL, ®val, 1); - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -910,7 +886,7 @@ static int mcp2515_add_stdfilter(FAR struct mcp2515_can_s *priv, /* Get exclusive excess to the MCP2515 hardware */ - ret = mcp2515_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1051,12 +1027,12 @@ static int mcp2515_add_stdfilter(FAR struct mcp2515_can_s *priv, regval = (regval & ~CANCTRL_REQOP_MASK) | (CANCTRL_REQOP_NORMAL); mcp2515_writeregs(priv, MCP2515_CANCTRL, ®val, 1); - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return ndx; } } - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -EAGAIN; } @@ -1100,7 +1076,7 @@ static int mcp2515_del_stdfilter(FAR struct mcp2515_can_s *priv, int ndx) /* Get exclusive excess to the MCP2515 hardware */ - ret = mcp2515_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1112,7 +1088,7 @@ static int mcp2515_del_stdfilter(FAR struct mcp2515_can_s *priv, int ndx) { /* No, error out */ - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return -ENOENT; } @@ -1157,7 +1133,7 @@ static int mcp2515_del_stdfilter(FAR struct mcp2515_can_s *priv, int ndx) regval = (regval & ~CANCTRL_REQOP_MASK) | (CANCTRL_REQOP_NORMAL); mcp2515_writeregs(priv, MCP2515_CANCTRL, ®val, 1); - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -1189,7 +1165,7 @@ static void mcp2515_reset_lowlevel(FAR struct mcp2515_can_s *priv) /* Get exclusive access to the MCP2515 peripheral */ - ret = mcp2515_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return; @@ -1219,7 +1195,7 @@ static void mcp2515_reset_lowlevel(FAR struct mcp2515_can_s *priv) /* Define the current state and unlock */ priv->state = MCP2515_STATE_RESET; - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -1281,7 +1257,7 @@ static int mcp2515_setup(FAR struct can_dev_s *dev) /* Get exclusive access to the MCP2515 peripheral */ - ret = mcp2515_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1312,7 +1288,7 @@ static int mcp2515_setup(FAR struct can_dev_s *dev) priv->state = MCP2515_STATE_SETUP; mcp2515_rxint(dev, true); - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -1752,7 +1728,7 @@ static int mcp2515_send(FAR struct can_dev_s *dev, FAR struct can_msg_s *msg) /* Get exclusive access to the MCP2515 peripheral */ - ret = mcp2515_dev_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1763,7 +1739,7 @@ static int mcp2515_send(FAR struct can_dev_s *dev, FAR struct can_msg_s *msg) ret = nxsem_wait(&priv->txfsem); if (ret < 0) { - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -1850,7 +1826,7 @@ static int mcp2515_send(FAR struct can_dev_s *dev, FAR struct can_msg_s *msg) priv->spi_txbuf[0] = MCP2515_RTS(txbuf); mcp2515_transfer(priv, 1); - mcp2515_dev_unlock(priv); + nxmutex_unlock(&priv->lock); /* Report that the TX transfer is complete to the upper half logic. Of * course, the transfer is not complete, but this early notification @@ -2545,9 +2521,9 @@ FAR struct mcp2515_can_s * * due to IOCTL command processing. */ - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->locksem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->txfsem, 0, MCP2515_NUM_TX_BUFFERS); /* Initialize bitmask */ diff --git a/drivers/eeprom/i2c_xx24xx.c b/drivers/eeprom/i2c_xx24xx.c index 0a726cc1b82b2..a8f3b2e3e1650 100644 --- a/drivers/eeprom/i2c_xx24xx.c +++ b/drivers/eeprom/i2c_xx24xx.c @@ -84,6 +84,7 @@ #include #include +#include #include #include @@ -125,7 +126,7 @@ struct ee24xx_dev_s /* Driver management */ - sem_t sem; /* file write access serialization */ + mutex_t lock; /* file write access serialization */ uint8_t refs; /* Nr of times the device has been opened */ bool readonly; /* Flags */ @@ -337,32 +338,6 @@ static int ee24xx_writepage(FAR struct ee24xx_dev_s *eedev, uint32_t memaddr, return I2C_TRANSFER(eedev->i2c, msgs, 2); } -/**************************************************************************** - * Name: ee24xx_semtake - * - * Acquire a resource to access the device. - * The purpose of the semaphore is to block tasks that try to access the - * EEPROM while another task is actively using it. - * - ****************************************************************************/ - -static int ee24xx_semtake(FAR struct ee24xx_dev_s *eedev) -{ - return nxsem_wait_uninterruptible(&eedev->sem); -} - -/**************************************************************************** - * Name: ee24xx_semgive - * - * Release a resource to access the device. - * - ****************************************************************************/ - -static inline void ee24xx_semgive(FAR struct ee24xx_dev_s *eedev) -{ - nxsem_post(&eedev->sem); -} - /**************************************************************************** * Driver Functions ****************************************************************************/ @@ -383,7 +358,7 @@ static int ee24xx_open(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee24xx_dev_s *)inode->i_private; - ret = ee24xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -400,7 +375,7 @@ static int ee24xx_open(FAR struct file *filep) eedev->refs += 1; } - ee24xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } @@ -420,7 +395,7 @@ static int ee24xx_close(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee24xx_dev_s *)inode->i_private; - ret = ee24xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -439,7 +414,7 @@ static int ee24xx_close(FAR struct file *filep) eedev->refs -= 1; } - ee24xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } @@ -460,7 +435,7 @@ static off_t ee24xx_seek(FAR struct file *filep, off_t offset, int whence) DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee24xx_dev_s *)inode->i_private; - ret = ee24xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -486,7 +461,7 @@ static off_t ee24xx_seek(FAR struct file *filep, off_t offset, int whence) /* Return EINVAL if the whence argument is invalid */ - ee24xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return -EINVAL; } @@ -515,7 +490,7 @@ static off_t ee24xx_seek(FAR struct file *filep, off_t offset, int whence) ret = -EINVAL; } - ee24xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } @@ -536,7 +511,7 @@ static ssize_t ee24xx_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee24xx_dev_s *)inode->i_private; - ret = ee24xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -594,7 +569,7 @@ static ssize_t ee24xx_read(FAR struct file *filep, FAR char *buffer, filep->f_pos += len; done: - ee24xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } @@ -615,7 +590,7 @@ static ssize_t at24cs_read_uuid(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee24xx_dev_s *)inode->i_private; - ret = ee24xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -669,7 +644,7 @@ static ssize_t at24cs_read_uuid(FAR struct file *filep, FAR char *buffer, filep->f_pos += len; done: - ee24xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } #endif @@ -715,7 +690,7 @@ static ssize_t ee24xx_write(FAR struct file *filep, FAR const char *buffer, savelen = len; /* save number of bytes written */ - ret = ee24xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -795,7 +770,7 @@ static ssize_t ee24xx_write(FAR struct file *filep, FAR const char *buffer, ret = savelen; done: - ee24xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } @@ -863,7 +838,7 @@ int ee24xx_initialize(FAR struct i2c_master_s *bus, uint8_t devaddr, return -ENOMEM; } - nxsem_init(&eedev->sem, 0, 1); + nxmutex_init(&eedev->lock); eedev->freq = CONFIG_EE24XX_FREQUENCY; eedev->i2c = bus; diff --git a/drivers/eeprom/spi_xx25xx.c b/drivers/eeprom/spi_xx25xx.c index 29229d2871366..69426920e5b66 100644 --- a/drivers/eeprom/spi_xx25xx.c +++ b/drivers/eeprom/spi_xx25xx.c @@ -108,6 +108,7 @@ #include #include +#include #include #include @@ -174,7 +175,7 @@ struct ee25xx_dev_s uint32_t size; /* in bytes, expanded from geometry */ uint16_t pgsize; /* write block size, in bytes, expanded from geometry */ uint16_t addrlen; /* number of BITS in data addresses */ - sem_t sem; /* file access serialization */ + mutex_t lock; /* file access serialization */ uint8_t refs; /* The number of times the device has been opened */ uint8_t readonly; /* Flags */ }; @@ -453,32 +454,6 @@ static void ee25xx_writepage(FAR struct ee25xx_dev_s *eedev, ee25xx_unlock(eedev->spi); } -/**************************************************************************** - * Name: ee25xx_semtake - * - * Acquire a resource to access the device. - * The purpose of the semaphore is to block tasks that try to access the - * EEPROM while another task is actively using it. - * - ****************************************************************************/ - -static int ee25xx_semtake(FAR struct ee25xx_dev_s *eedev) -{ - return nxsem_wait_uninterruptible(&eedev->sem); -} - -/**************************************************************************** - * Name: ee25xx_semgive - * - * Release a resource to access the device. - * - ****************************************************************************/ - -static inline void ee25xx_semgive(FAR struct ee25xx_dev_s *eedev) -{ - nxsem_post(&eedev->sem); -} - /**************************************************************************** * Driver Functions ****************************************************************************/ @@ -499,7 +474,7 @@ static int ee25xx_open(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee25xx_dev_s *)inode->i_private; - ret = ee25xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -516,7 +491,7 @@ static int ee25xx_open(FAR struct file *filep) eedev->refs += 1; } - ee25xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } @@ -536,7 +511,7 @@ static int ee25xx_close(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee25xx_dev_s *)inode->i_private; - ret = ee25xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -555,7 +530,7 @@ static int ee25xx_close(FAR struct file *filep) eedev->refs -= 1; } - ee25xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } @@ -576,7 +551,7 @@ static off_t ee25xx_seek(FAR struct file *filep, off_t offset, int whence) DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee25xx_dev_s *)inode->i_private; - ret = ee25xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -602,7 +577,7 @@ static off_t ee25xx_seek(FAR struct file *filep, off_t offset, int whence) /* Return EINVAL if the whence argument is invalid */ - ee25xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return -EINVAL; } @@ -630,7 +605,7 @@ static off_t ee25xx_seek(FAR struct file *filep, off_t offset, int whence) ret = -EINVAL; } - ee25xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return ret; } @@ -648,7 +623,7 @@ static ssize_t ee25xx_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); eedev = (FAR struct ee25xx_dev_s *)inode->i_private; - ret = ee25xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -680,7 +655,7 @@ static ssize_t ee25xx_read(FAR struct file *filep, FAR char *buffer, /* Update the file position */ filep->f_pos += len; - ee25xx_semgive(eedev); + nxmutex_unlock(&eedev->lock); return len; } @@ -719,7 +694,7 @@ static ssize_t ee25xx_write(FAR struct file *filep, FAR const char *buffer, len = eedev->size - filep->f_pos; } - ret = ee25xx_semtake(eedev); + ret = nxmutex_lock(&eedev->lock); if (ret < 0) { return ret; @@ -776,8 +751,7 @@ static ssize_t ee25xx_write(FAR struct file *filep, FAR const char *buffer, filep->f_pos += cnt; } - ee25xx_semgive(eedev); - + nxmutex_unlock(&eedev->lock); return ret; } @@ -841,7 +815,7 @@ int ee25xx_initialize(FAR struct spi_dev_s *dev, FAR char *devname, return -ENOMEM; } - nxsem_init(&eedev->sem, 0, 1); + nxmutex_init(&eedev->lock); eedev->spi = dev; eedev->size = 128 << g_ee25xx_devices[devtype].bytes; diff --git a/drivers/efuse/efuse.c b/drivers/efuse/efuse.c index 2f58fa975a21a..26583a0dbba2d 100644 --- a/drivers/efuse/efuse.c +++ b/drivers/efuse/efuse.c @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #ifdef CONFIG_EFUSE @@ -53,7 +53,7 @@ struct efuse_upperhalf_s { - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ FAR char *path; /* Registration path */ /* The contained lower-half driver */ @@ -148,7 +148,7 @@ static int efuse_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -238,7 +238,7 @@ static int efuse_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -291,7 +291,7 @@ FAR void *efuse_register(FAR const char *path, * by kmm_zalloc()). */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); upper->lower = lower; /* Copy the registration path */ @@ -318,7 +318,7 @@ FAR void *efuse_register(FAR const char *path, kmm_free(upper->path); errout_with_upper: - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); errout: @@ -361,7 +361,7 @@ void efuse_unregister(FAR void *handle) /* Then free all of the driver resources */ kmm_free(upper->path); - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); } diff --git a/drivers/i2c/i2c_driver.c b/drivers/i2c/i2c_driver.c index 06f4c09ce4b01..245558364ddf1 100644 --- a/drivers/i2c/i2c_driver.c +++ b/drivers/i2c/i2c_driver.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #ifdef CONFIG_I2C_DRIVER @@ -58,7 +58,7 @@ struct i2c_driver_s { FAR struct i2c_master_s *i2c; /* Contained I2C lower half driver */ #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - sem_t exclsem; /* Mutual exclusion */ + mutex_t lock; /* Mutual exclusion */ int16_t crefs; /* Number of open references */ bool unlinked; /* True, driver has been unlinked */ #endif @@ -130,7 +130,7 @@ static int i2cdrvr_open(FAR struct file *filep) /* Get exclusive access to the I2C driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -141,7 +141,7 @@ static int i2cdrvr_open(FAR struct file *filep) priv->crefs++; DEBUGASSERT(priv->crefs > 0); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -167,7 +167,7 @@ static int i2cdrvr_close(FAR struct file *filep) /* Get exclusive access to the I2C driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -184,12 +184,12 @@ static int i2cdrvr_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return OK; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -238,7 +238,7 @@ static int i2cdrvr_ioctl(FAR struct file *filep, int cmd, unsigned long arg) #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS /* Get exclusive access to the I2C driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -289,7 +289,7 @@ static int i2cdrvr_ioctl(FAR struct file *filep, int cmd, unsigned long arg) } #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); #endif return ret; } @@ -311,7 +311,7 @@ static int i2cdrvr_unlink(FAR struct inode *inode) /* Get exclusive access to the I2C driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -321,7 +321,7 @@ static int i2cdrvr_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return OK; } @@ -331,7 +331,7 @@ static int i2cdrvr_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -381,7 +381,7 @@ int i2c_register(FAR struct i2c_master_s *i2c, int bus) priv->i2c = i2c; #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #endif /* Create the character device name */ diff --git a/drivers/i2s/i2schar.c b/drivers/i2s/i2schar.c index c02df019e1451..dbd86e374d5b0 100644 --- a/drivers/i2s/i2schar.c +++ b/drivers/i2s/i2schar.c @@ -43,6 +43,7 @@ #include #include +#include #include #include #include @@ -73,7 +74,7 @@ struct i2schar_dev_s { FAR struct i2s_dev_s *i2s; /* The lower half i2s driver */ - sem_t exclsem; /* Assures mutually exclusive access */ + mutex_t lock; /* Assures mutually exclusive access */ }; /**************************************************************************** @@ -242,7 +243,7 @@ static ssize_t i2schar_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to i2c character driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { i2serr("ERROR: nxsem_wait returned: %d\n", ret); @@ -263,12 +264,12 @@ static ssize_t i2schar_read(FAR struct file *filep, FAR char *buffer, * received */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return sizeof(struct ap_buffer_s) + nbytes; errout_with_reference: apb_free(apb); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -315,7 +316,7 @@ static ssize_t i2schar_write(FAR struct file *filep, FAR const char *buffer, /* Get exclusive access to i2c character driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { i2serr("ERROR: nxsem_wait returned: %d\n", ret); @@ -336,12 +337,12 @@ static ssize_t i2schar_write(FAR struct file *filep, FAR const char *buffer, * sent. */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return sizeof(struct ap_buffer_s) + nbytes; errout_with_reference: apb_free(apb); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -423,7 +424,7 @@ int i2schar_register(FAR struct i2s_dev_s *i2s, int minor) /* Initialize the I2S character device structure */ priv->i2s = i2s; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Create the character device name */ diff --git a/drivers/input/ads7843e.c b/drivers/input/ads7843e.c index 0f94ce0a89d71..b773797cfa335 100644 --- a/drivers/input/ads7843e.c +++ b/drivers/input/ads7843e.c @@ -381,7 +381,7 @@ static int ads7843e_waitsample(FAR struct ads7843e_dev_s *priv, * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -410,7 +410,7 @@ static int ads7843e_waitsample(FAR struct ads7843e_dev_s *priv, * sample. Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); errout: /* Then re-enable interrupts. We might get interrupt here and there @@ -498,7 +498,6 @@ static void ads7843e_worker(FAR void *arg) uint16_t xdiff; uint16_t ydiff; bool pendown; - int ret; DEBUGASSERT(priv != NULL); @@ -521,17 +520,7 @@ static void ads7843e_worker(FAR void *arg) /* Get exclusive access to the driver data structure */ - do - { - ret = nxsem_wait_uninterruptible(&priv->devsem); - - /* This would only fail if something canceled the worker thread? - * That is not expected. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); + nxmutex_lock(&priv->devlock); /* Check for pen up or down by reading the PENIRQ GPIO. */ @@ -677,7 +666,7 @@ static void ads7843e_worker(FAR void *arg) /* Release our lock on the state structure and unlock the SPI bus */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); ads7843e_unlock(priv->spi); } @@ -742,7 +731,7 @@ static int ads7843e_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -756,7 +745,7 @@ static int ads7843e_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* When the reference increments to 1, this is the first open event @@ -767,8 +756,8 @@ static int ads7843e_open(FAR struct file *filep) priv->crefs = tmp; -errout_with_sem: - nxsem_post(&priv->devsem); +errout_with_lock: + nxmutex_unlock(&priv->devlock); return ret; #else iinfo("Opening\n"); @@ -796,7 +785,7 @@ static int ads7843e_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -812,7 +801,7 @@ static int ads7843e_close(FAR struct file *filep) priv->crefs--; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); #endif iinfo("Closing\n"); return OK; @@ -854,7 +843,7 @@ static ssize_t ads7843e_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { ierr("ERROR: nxsem_wait: %d\n", ret); @@ -943,7 +932,7 @@ static ssize_t ads7843e_read(FAR struct file *filep, FAR char *buffer, ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); iinfo("Returning: %d\n", ret); return ret; } @@ -967,7 +956,7 @@ static int ads7843e_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -998,7 +987,7 @@ static int ads7843e_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1023,7 +1012,7 @@ static int ads7843e_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1085,7 +1074,7 @@ static int ads7843e_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1150,10 +1139,10 @@ int ads7843e_register(FAR struct spi_dev_s *spi, priv->threshx = INVALID_THRESHOLD; /* Initialize thresholding logic */ priv->threshy = INVALID_THRESHOLD; /* Initialize thresholding logic */ - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ - nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ + nxmutex_init(&priv->devlock); /* Initialize device structure mutex */ + nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ /* The pen event semaphore is used for signaling and, hence, should not * have priority inheritance enabled. @@ -1230,7 +1219,7 @@ int ads7843e_register(FAR struct spi_dev_s *spi, return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); #ifdef CONFIG_ADS7843E_MULTIPLE kmm_free(priv); #endif diff --git a/drivers/input/ads7843e.h b/drivers/input/ads7843e.h index 2c12b55b84394..6a7909662856c 100644 --- a/drivers/input/ads7843e.h +++ b/drivers/input/ads7843e.h @@ -44,6 +44,7 @@ #include #include +#include #include #include #include @@ -134,7 +135,7 @@ struct ads7843e_dev_s volatile bool penchange; /* An unreported event is buffered */ uint16_t threshx; /* Thresholding X value */ uint16_t threshy; /* Thresholding Y value */ - sem_t devsem; /* Manages exclusive access to this structure */ + mutex_t devlock; /* Manages exclusive access to this structure */ sem_t waitsem; /* Used to wait for the availability of data */ FAR struct ads7843e_config_s *config; /* Board configuration data */ diff --git a/drivers/input/cypress_mbr3108.c b/drivers/input/cypress_mbr3108.c index 8e803cb0343cf..9f05cf2d1bff4 100644 --- a/drivers/input/cypress_mbr3108.c +++ b/drivers/input/cypress_mbr3108.c @@ -191,7 +191,7 @@ struct mbr3108_dev_s struct mbr3108_board_s *board; const struct mbr3108_sensor_conf_s *sensor_conf; - sem_t devsem; + mutex_t devlock; uint8_t cref; struct mbr3108_debug_conf_s debug_conf; bool int_pending; @@ -743,7 +743,7 @@ static ssize_t mbr3108_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); priv = inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -772,7 +772,7 @@ static ssize_t mbr3108_read(FAR struct file *filep, FAR char *buffer, priv->int_pending = false; leave_critical_section(flags); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret < 0 ? ret : outlen; } @@ -795,7 +795,7 @@ static ssize_t mbr3108_write(FAR struct file *filep, FAR const char *buffer, return -EINVAL; } - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -853,7 +853,7 @@ static ssize_t mbr3108_write(FAR struct file *filep, FAR const char *buffer, } out: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret < 0 ? ret : buflen; } @@ -871,7 +871,7 @@ static int mbr3108_open(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); priv = inode->i_private; - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -885,7 +885,7 @@ static int mbr3108_open(FAR struct file *filep) ret = priv->board->set_power(priv->board, true); if (ret < 0) { - goto out_sem; + goto out_lock; } /* Let chip to power up before probing */ @@ -900,7 +900,7 @@ static int mbr3108_open(FAR struct file *filep) /* No such device. Power off the switch. */ priv->board->set_power(priv->board, false); - goto out_sem; + goto out_lock; } if (priv->sensor_conf) @@ -913,7 +913,7 @@ static int mbr3108_open(FAR struct file *filep) /* Configuration failed. Power off the switch. */ priv->board->set_power(priv->board, false); - goto out_sem; + goto out_lock; } } @@ -927,8 +927,8 @@ static int mbr3108_open(FAR struct file *filep) ret = 0; } -out_sem: - nxsem_post(&priv->devsem); +out_lock: + nxmutex_unlock(&priv->devlock); return ret; } @@ -945,7 +945,7 @@ static int mbr3108_close(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); priv = inode->i_private; - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -976,8 +976,7 @@ static int mbr3108_close(FAR struct file *filep) priv->cref = use_count; } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return 0; } @@ -996,7 +995,7 @@ static int mbr3108_poll(FAR struct file *filep, FAR struct pollfd *fds, DEBUGASSERT(inode && inode->i_private); priv = (FAR struct mbr3108_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1060,7 +1059,7 @@ static int mbr3108_poll(FAR struct file *filep, FAR struct pollfd *fds, } out: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1108,7 +1107,7 @@ int cypress_mbr3108_register(FAR const char *devpath, priv->board = board_config; priv->sensor_conf = sensor_conf; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); ret = register_driver(devpath, &g_mbr3108_fileops, 0666, priv); if (ret < 0) diff --git a/drivers/input/ft5x06.c b/drivers/input/ft5x06.c index b1c10ce3cde4a..400c3706fe635 100644 --- a/drivers/input/ft5x06.c +++ b/drivers/input/ft5x06.c @@ -55,6 +55,7 @@ #include #include #include +#include #include #include #include @@ -106,7 +107,7 @@ struct ft5x06_dev_s int16_t lastx; /* Last reported X position */ int16_t lasty; /* Last reported Y position */ #endif - sem_t devsem; /* Manages exclusive access to + mutex_t devlock; /* Manages exclusive access to * this structure */ sem_t waitsem; /* Used to wait for the * availability of data */ @@ -245,17 +246,7 @@ static void ft5x06_data_worker(FAR void *arg) * corrupt any read operation that is in place. */ - do - { - ret = nxsem_wait_uninterruptible(&priv->devsem); - - /* This would only fail if something canceled the worker thread? - * That is not expected. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); + nxmutex_lock(&priv->devlock); /* Read touch data */ @@ -340,7 +331,7 @@ static void ft5x06_data_worker(FAR void *arg) config->enable(config, true); #endif - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); } /**************************************************************************** @@ -627,7 +618,7 @@ static ssize_t ft5x06_waitsample(FAR struct ft5x06_dev_s *priv, * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -656,7 +647,7 @@ static ssize_t ft5x06_waitsample(FAR struct ft5x06_dev_s *priv, * sample. Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret >= 0) { /* Now sample the data. @@ -763,10 +754,10 @@ static int ft5x06_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -778,7 +769,7 @@ static int ft5x06_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* When the reference increments to 1, this is the first open event @@ -791,7 +782,7 @@ static int ft5x06_open(FAR struct file *filep) if (ret < 0) { ierr("ERROR: ft5x06_bringup failed: %d\n", ret); - goto errout_with_sem; + goto errout_with_lock; } } @@ -799,8 +790,8 @@ static int ft5x06_open(FAR struct file *filep) priv->crefs = tmp; -errout_with_sem: - nxsem_post(&priv->devsem); +errout_with_lock: + nxmutex_unlock(&priv->devlock); return ret; } @@ -822,10 +813,10 @@ static int ft5x06_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -847,7 +838,7 @@ static int ft5x06_close(FAR struct file *filep) ft5x06_shutdown(priv); } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } @@ -883,10 +874,10 @@ static ssize_t ft5x06_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -920,7 +911,7 @@ static ssize_t ft5x06_read(FAR struct file *filep, FAR char *buffer, ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -943,10 +934,10 @@ static int ft5x06_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -975,7 +966,7 @@ static int ft5x06_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1000,10 +991,10 @@ static int ft5x06_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -1066,7 +1057,7 @@ static int ft5x06_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1132,7 +1123,7 @@ int ft5x06_register(FAR struct i2c_master_s *i2c, priv->config = config; /* Save the board configuration */ priv->frequency = config->frequency; /* Set the current I2C frequency */ - nxsem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ + nxmutex_init(&priv->devlock); /* Initialize device structure mutex */ nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ /* The event wait semaphore is used for signaling and, hence, should not @@ -1190,7 +1181,7 @@ int ft5x06_register(FAR struct i2c_master_s *i2c, return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return ret; } diff --git a/drivers/input/keyboard_upper.c b/drivers/input/keyboard_upper.c index 762d028de9475..6d950f7d9e600 100644 --- a/drivers/input/keyboard_upper.c +++ b/drivers/input/keyboard_upper.c @@ -33,7 +33,7 @@ #include #include #include -#include +#include /**************************************************************************** * Pre-processor Definitions @@ -46,7 +46,7 @@ struct keyboard_opriv_s { sem_t waitsem; - sem_t locksem; + mutex_t lock; struct circbuf_s circ; struct list_node node; FAR struct pollfd *fds; @@ -56,11 +56,11 @@ struct keyboard_opriv_s struct keyboard_upperhalf_s { - sem_t exclsem; /* Manages exclusive access to this structure */ - struct list_node head; /* Head of list */ + mutex_t lock; /* Manages exclusive access to this structure */ + struct list_node head; /* Head of list */ FAR struct keyboard_lowerhalf_s - *lower; /* A pointer of lower half instance */ - uint8_t nums; /* Number of buffer */ + *lower; /* A pointer of lower half instance */ + uint8_t nums; /* Number of buffer */ }; /**************************************************************************** @@ -109,7 +109,7 @@ static int keyboard_open(FAR struct file *filep) FAR struct keyboard_upperhalf_s *upper = inode->i_private; int ret; - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -118,7 +118,7 @@ static int keyboard_open(FAR struct file *filep) opriv = kmm_zalloc(sizeof(FAR struct keyboard_opriv_s)); if (opriv == NULL) { - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return -ENOMEM; } @@ -128,7 +128,7 @@ static int keyboard_open(FAR struct file *filep) if (ret < 0) { kmm_free(opriv); - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -140,18 +140,18 @@ static int keyboard_open(FAR struct file *filep) if (ret < 0) { kmm_free(opriv); - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } } nxsem_init(&opriv->waitsem, 0, 0); - nxsem_init(&opriv->locksem, 0, 1); + nxmutex_init(&opriv->lock); nxsem_set_protocol(&opriv->waitsem, SEM_PRIO_NONE); list_add_tail(&upper->head, &opriv->node); filep->f_priv = opriv; - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -166,7 +166,7 @@ static int keyboard_close(FAR struct file *filep) FAR struct keyboard_upperhalf_s *upper = inode->i_private; int ret; - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -186,11 +186,11 @@ static int keyboard_close(FAR struct file *filep) list_delete(&opriv->node); circbuf_uninit(&opriv->circ); nxsem_destroy(&opriv->waitsem); - nxsem_destroy(&opriv->locksem); + nxmutex_destroy(&opriv->lock); kmm_free(opriv); out: - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -206,7 +206,7 @@ static ssize_t keyboard_read(FAR struct file *filep, /* Make sure that we have exclusive access to the private data structure */ - ret = nxsem_wait(&opriv->locksem); + ret = nxmutex_lock(&opriv->lock); if (ret < 0) { return ret; @@ -223,14 +223,14 @@ static ssize_t keyboard_read(FAR struct file *filep, } else { - nxsem_post(&opriv->locksem); + nxmutex_unlock(&opriv->lock); ret = nxsem_wait_uninterruptible(&opriv->waitsem); if (ret < 0) { return ret; } - ret = nxsem_wait(&opriv->locksem); + ret = nxmutex_lock(&opriv->lock); if (ret < 0) { return ret; @@ -241,7 +241,7 @@ static ssize_t keyboard_read(FAR struct file *filep, ret = circbuf_read(&opriv->circ, buff, len); out: - nxsem_post(&opriv->locksem); + nxmutex_unlock(&opriv->lock); return ret; } @@ -255,7 +255,7 @@ static int keyboard_poll(FAR struct file *filep, FAR struct keyboard_opriv_s *opriv = filep->f_priv; int ret; - ret = nxsem_wait(&opriv->locksem); + ret = nxmutex_lock(&opriv->lock); if (ret < 0) { return ret; @@ -286,7 +286,7 @@ static int keyboard_poll(FAR struct file *filep, } errout: - nxsem_post(&opriv->locksem); + nxmutex_unlock(&opriv->lock); return ret; } @@ -341,12 +341,12 @@ int keyboard_register(FAR struct keyboard_lowerhalf_s *lower, upper->nums = nums; lower->priv = upper; list_initialize(&upper->head); - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); ret = register_driver(path, &g_keyboard_fops, 0666, upper); if (ret < 0) { - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); return ret; } @@ -376,7 +376,7 @@ int keyboard_unregister(FAR struct keyboard_lowerhalf_s *lower, return ret; } - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); return 0; } @@ -393,7 +393,7 @@ void keyboard_event(FAR struct keyboard_lowerhalf_s *lower, uint32_t keycode, struct keyboard_event_s key; int semcount; - if (nxsem_wait(&upper->exclsem) < 0) + if (nxmutex_lock(&upper->lock) < 0) { return; } @@ -402,7 +402,7 @@ void keyboard_event(FAR struct keyboard_lowerhalf_s *lower, uint32_t keycode, key.type = type; list_for_every_entry(&upper->head, opriv, struct keyboard_opriv_s, node) { - if (nxsem_wait(&opriv->locksem) == 0) + if (nxmutex_lock(&opriv->lock) == 0) { circbuf_overwrite(&opriv->circ, &key, sizeof(struct keyboard_event_s)); @@ -413,9 +413,9 @@ void keyboard_event(FAR struct keyboard_lowerhalf_s *lower, uint32_t keycode, } poll_notify(&opriv->fds, 1, POLLIN); - nxsem_post(&opriv->locksem); + nxmutex_unlock(&opriv->lock); } } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); } diff --git a/drivers/input/max11802.c b/drivers/input/max11802.c index 1d40432d8c2c9..4b47fc77c5daa 100644 --- a/drivers/input/max11802.c +++ b/drivers/input/max11802.c @@ -338,7 +338,7 @@ static int max11802_waitsample(FAR struct max11802_dev_s *priv, * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -367,7 +367,7 @@ static int max11802_waitsample(FAR struct max11802_dev_s *priv, * sample. Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); errout: /* Then re-enable interrupts. We might get interrupt here and there @@ -483,17 +483,7 @@ static void max11802_worker(FAR void *arg) /* Get exclusive access to the driver data structure */ - do - { - ret = nxsem_wait_uninterruptible(&priv->devsem); - - /* This would only fail if something canceled the worker thread? - * That is not expected. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); + nxmutex_lock(&priv->devlock); /* Check for pen up or down by reading the PENIRQ GPIO. */ @@ -669,7 +659,7 @@ static void max11802_worker(FAR void *arg) /* Release our lock on the state structure and unlock the SPI bus */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); max11802_unlock(priv->spi); } @@ -734,7 +724,7 @@ static int max11802_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -748,7 +738,7 @@ static int max11802_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* When the reference increments to 1, this is the first open event @@ -759,8 +749,8 @@ static int max11802_open(FAR struct file *filep) priv->crefs = tmp; -errout_with_sem: - nxsem_post(&priv->devsem); +errout_with_lock: + nxmutex_unlock(&priv->devlock); return ret; #else iinfo("Opening\n"); @@ -788,7 +778,7 @@ static int max11802_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -804,7 +794,7 @@ static int max11802_close(FAR struct file *filep) priv->crefs--; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); #endif iinfo("Closing\n"); return OK; @@ -846,7 +836,7 @@ static ssize_t max11802_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { ierr("ERROR: nxsem_wait: %d\n", ret); @@ -935,7 +925,7 @@ static ssize_t max11802_read(FAR struct file *filep, FAR char *buffer, ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); iinfo("Returning: %d\n", ret); return ret; } @@ -959,7 +949,7 @@ static int max11802_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -990,7 +980,7 @@ static int max11802_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1015,7 +1005,7 @@ static int max11802_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1077,7 +1067,7 @@ static int max11802_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1142,10 +1132,10 @@ int max11802_register(FAR struct spi_dev_s *spi, priv->threshx = INVALID_THRESHOLD; /* Initialize thresholding logic */ priv->threshy = INVALID_THRESHOLD; /* Initialize thresholding logic */ - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ - nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ + nxmutex_init(&priv->devlock); /* Initialize device structure mutex */ + nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ /* The pen event semaphore is used for signaling and, hence, should not * have priority inheritance enabled. @@ -1263,7 +1253,7 @@ int max11802_register(FAR struct spi_dev_s *spi, return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); #ifdef CONFIG_MAX11802_MULTIPLE kmm_free(priv); #endif diff --git a/drivers/input/max11802.h b/drivers/input/max11802.h index 6fe9228a9efcc..48613c22dfd65 100644 --- a/drivers/input/max11802.h +++ b/drivers/input/max11802.h @@ -38,6 +38,7 @@ #include #include +#include #include #include #include @@ -120,7 +121,7 @@ struct max11802_dev_s volatile bool penchange; /* An unreported event is buffered */ uint16_t threshx; /* Thresholding X value */ uint16_t threshy; /* Thresholding Y value */ - sem_t devsem; /* Manages exclusive access to this structure */ + mutex_t devlock; /* Manages exclusive access to this structure */ sem_t waitsem; /* Used to wait for the availability of data */ FAR struct max11802_config_s *config; /* Board configuration data */ diff --git a/drivers/input/mxt.c b/drivers/input/mxt.c index 76d2629a90c7a..f5277b03d9d9d 100644 --- a/drivers/input/mxt.c +++ b/drivers/input/mxt.c @@ -44,6 +44,7 @@ #include #include +#include #include #include #include @@ -188,7 +189,7 @@ struct mxt_dev_s #endif volatile bool event; /* True: An unreported event is buffered */ - sem_t devsem; /* Manages exclusive access to this structure */ + mutex_t devlock; /* Manages exclusive access to this structure */ sem_t waitsem; /* Used to wait for the availability of data */ uint32_t frequency; /* Current I2C frequency */ @@ -669,7 +670,7 @@ static inline int mxt_waitsample(FAR struct mxt_dev_s *priv) * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -694,7 +695,7 @@ static inline int mxt_waitsample(FAR struct mxt_dev_s *priv) * sample. Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); errout: /* Then re-enable interrupts. We might get interrupt here and there @@ -969,17 +970,7 @@ static void mxt_worker(FAR void *arg) /* Get exclusive access to the MXT driver data structure */ - do - { - ret = nxsem_wait_uninterruptible(&priv->devsem); - - /* This would only fail if something canceled the worker thread? - * That is not expected. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); + nxmutex_lock(&priv->devlock); /* Loop, processing each message from the maXTouch */ @@ -992,7 +983,7 @@ static void mxt_worker(FAR void *arg) if (ret < 0) { ierr("ERROR: mxt_getmessage failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } id = msg.id; @@ -1054,11 +1045,11 @@ static void mxt_worker(FAR void *arg) } while (id != 0xff && retries < 16); -errout_with_semaphore: +errout_with_lock: /* Release our lock on the MXT device */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); /* Acknowledge and re-enable maXTouch interrupts */ @@ -1122,7 +1113,7 @@ static int mxt_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1137,7 +1128,7 @@ static int mxt_open(FAR struct file *filep) ierr("ERROR: Too many opens: %d\n", priv->crefs); ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* When the reference increments to 1, this is the first open event @@ -1152,7 +1143,7 @@ static int mxt_open(FAR struct file *filep) if (ret < 0) { ierr("ERROR: Failed to enable touch: %d\n", ret); - goto errout_with_sem; + goto errout_with_lock; } /* Clear any pending messages by reading all messages. This will @@ -1165,7 +1156,7 @@ static int mxt_open(FAR struct file *filep) { ierr("ERROR: mxt_flushmsgs failed: %d\n", ret); mxt_putobject(priv, MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0); - goto errout_with_sem; + goto errout_with_lock; } /* Enable touch interrupts */ @@ -1177,8 +1168,8 @@ static int mxt_open(FAR struct file *filep) priv->crefs = tmp; -errout_with_sem: - nxsem_post(&priv->devsem); +errout_with_lock: + nxmutex_unlock(&priv->devlock); return ret; } @@ -1200,7 +1191,7 @@ static int mxt_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1229,7 +1220,7 @@ static int mxt_close(FAR struct file *filep) } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } @@ -1268,7 +1259,7 @@ static ssize_t mxt_read(FAR struct file *filep, FAR char *buffer, size_t len) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1466,7 +1457,7 @@ static ssize_t mxt_read(FAR struct file *filep, FAR char *buffer, size_t len) errout: sched_unlock(); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1489,7 +1480,7 @@ static int mxt_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1521,7 +1512,7 @@ static int mxt_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1546,7 +1537,7 @@ static int mxt_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1611,7 +1602,7 @@ static int mxt_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1868,9 +1859,9 @@ int mxt_register(FAR struct i2c_master_s *i2c, priv->i2c = i2c; /* Save the SPI device handle */ priv->lower = lower; /* Save the board configuration */ - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->devsem, 0, 1); /* Initialize device semaphore */ + nxmutex_init(&priv->devlock); /* Initialize device mutex */ nxsem_init(&priv->waitsem, 0, 0); /* Initialize event wait semaphore */ /* The event wait semaphore is used for signaling and, hence, should not @@ -1928,7 +1919,7 @@ int mxt_register(FAR struct i2c_master_s *i2c, errout_with_irq: MXT_DETACH(lower); errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); nxsem_destroy(&priv->waitsem); kmm_free(priv); return ret; diff --git a/drivers/input/nunchuck.c b/drivers/input/nunchuck.c index e9d23741b23da..08e9ef438079b 100644 --- a/drivers/input/nunchuck.c +++ b/drivers/input/nunchuck.c @@ -43,6 +43,7 @@ #include #include +#include #include #include #include @@ -61,7 +62,7 @@ struct nunchuck_dev_s { FAR struct i2c_master_s *i2c_dev; /* I2C interface connected to Nunchuck */ nunchuck_buttonset_t nck_sample; /* Last sampled button states */ - sem_t nck_exclsem; /* Supports exclusive access to the device */ + mutex_t nck_lock; /* Supports exclusive access to the device */ /* The following is a singly linked list of open references to the * joystick device. @@ -87,11 +88,6 @@ struct nunchuck_open_s * Private Function Prototypes ****************************************************************************/ -/* Semaphore helpers */ - -static inline int nunchuck_takesem(sem_t *sem); -#define nunchuck_givesem(s) nxsem_post(s); - /* Character driver methods */ static int nunchuck_open(FAR struct file *filep); @@ -272,15 +268,6 @@ static int nunchuck_sample(FAR struct nunchuck_dev_s *priv, return OK; } -/**************************************************************************** - * Name: nunchuck_takesem - ****************************************************************************/ - -static inline int nunchuck_takesem(sem_t *sem) -{ - return nxsem_wait(sem); -} - /**************************************************************************** * Name: nunchuck_open ****************************************************************************/ @@ -299,10 +286,10 @@ static int nunchuck_open(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = nunchuck_takesem(&priv->nck_exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nunchuck_takesem failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -314,7 +301,7 @@ static int nunchuck_open(FAR struct file *filep) { ierr("ERROR: Failed to allocate open structure\n"); ret = -ENOMEM; - goto errout_with_sem; + goto errout_with_excllock; } /* Attach the open structure to the device */ @@ -327,8 +314,8 @@ static int nunchuck_open(FAR struct file *filep) filep->f_priv = (FAR void *)opriv; ret = OK; -errout_with_sem: - nunchuck_givesem(&priv->nck_exclsem); +errout_with_excllock: + nxmutex_unlock(&priv->lock); return ret; } @@ -377,10 +364,10 @@ static int nunchuck_close(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = nunchuck_takesem(&priv->nck_exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nunchuck_takesem failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -395,7 +382,7 @@ static int nunchuck_close(FAR struct file *filep) { ierr("ERROR: Failed to find open entry\n"); ret = -ENOENT; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Remove the structure from the device */ @@ -415,8 +402,8 @@ static int nunchuck_close(FAR struct file *filep) ret = OK; -errout_with_exclsem: - nunchuck_givesem(&priv->nck_exclsem); +errout_with_excllock: + nxmutex_unlock(&priv->lock); return ret; } @@ -450,10 +437,10 @@ static ssize_t nunchuck_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to the driver structure */ - ret = nunchuck_takesem(&priv->nck_exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nunchuck_takesem failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -465,7 +452,7 @@ static ssize_t nunchuck_read(FAR struct file *filep, FAR char *buffer, ret = sizeof(struct nunchuck_sample_s); } - nunchuck_givesem(&priv->nck_exclsem); + nxmutex_unlock(&priv->lock); return (ssize_t)ret; } @@ -486,10 +473,10 @@ static int nunchuck_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver structure */ - ret = nunchuck_takesem(&priv->nck_exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nunchuck_takesem failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -525,7 +512,7 @@ static int nunchuck_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nunchuck_givesem(&priv->nck_exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -577,7 +564,7 @@ int nunchuck_register(FAR const char *devname, FAR struct i2c_master_s *i2c) /* Initialize the new nunchuck driver instance */ - nxsem_init(&priv->nck_exclsem, 0, 1); + nxmutex_init(&priv->nck_lock); /* And register the nunchuck driver */ @@ -591,7 +578,7 @@ int nunchuck_register(FAR const char *devname, FAR struct i2c_master_s *i2c) return OK; errout_with_priv: - nxsem_destroy(&priv->nck_exclsem); + nxmutex_destroy(&priv->nck_lock); kmm_free(priv); return ret; } diff --git a/drivers/input/spq10kbd.c b/drivers/input/spq10kbd.c index e491c50de8fe8..e190e32b6b208 100644 --- a/drivers/input/spq10kbd.c +++ b/drivers/input/spq10kbd.c @@ -37,6 +37,7 @@ #include #include #include +#include #include /**************************************************************************** @@ -181,10 +182,10 @@ struct spq10kbd_dev_s djoy_buttonset_t djoystate; /* Joystick button state */ #endif /* CONFIG_SPQ10KBD_DJOY */ - sem_t exclsem; /* Exclusive access to dev */ - sem_t waitsem; /* Signal waiting thread */ - bool waiting; /* Waiting for keyboard data */ - struct work_s work; /* Supports the interrupt handling "bottom half" */ + mutex_t lock; /* Exclusive access to dev */ + sem_t waitsem; /* Signal waiting thread */ + bool waiting; /* Waiting for keyboard data */ + struct work_s work; /* Supports the interrupt handling "bottom half" */ /* The following is a list if poll structures of threads waiting for * driver events. The 'struct pollfd' reference for each open is also @@ -331,7 +332,7 @@ static void spq10kbd_worker(FAR void *arg) uint8_t state; int ret; - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return; @@ -410,7 +411,7 @@ static void spq10kbd_worker(FAR void *arg) /* Clear interrupt status register */ spq10kbd_putreg8(priv, SPQ10KBD_INT, 0); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -513,7 +514,7 @@ static ssize_t spq10kbd_read(FAR struct file *filep, FAR char *buffer, /* Read data from our internal buffer of received characters */ - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -533,14 +534,14 @@ static ssize_t spq10kbd_read(FAR struct file *filep, FAR char *buffer, else { priv->waiting = true; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { return ret; } - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -571,7 +572,7 @@ static ssize_t spq10kbd_read(FAR struct file *filep, FAR char *buffer, priv->tailndx = tail; errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -614,7 +615,7 @@ static int spq10kbd_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv); - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -670,7 +671,7 @@ static int spq10kbd_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1001,7 +1002,7 @@ int spq10kbd_register(FAR struct i2c_master_s *i2c, priv->djoystate = 0; #endif /* CONFIG_SPQ10KBD_DJOY */ - nxsem_init(&priv->exclsem, 0, 1); /* Initialize device semaphore */ + nxmutex_init(&priv->lock); /* Initialize device mutex */ nxsem_init(&priv->waitsem, 0, 0); /* The waitsem semaphore is used for signaling and, hence, should @@ -1049,7 +1050,7 @@ int spq10kbd_register(FAR struct i2c_master_s *i2c, return OK; errout_with_priv: - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return ret; } diff --git a/drivers/input/stmpe811.h b/drivers/input/stmpe811.h index 5a519849fe849..4cfa4ce8847f1 100644 --- a/drivers/input/stmpe811.h +++ b/drivers/input/stmpe811.h @@ -32,6 +32,7 @@ #include +#include #include #include #include @@ -119,7 +120,7 @@ struct stmpe811_dev_s /* Common fields */ FAR struct stmpe811_config_s *config; /* Board configuration data */ - sem_t exclsem; /* Manages exclusive access to this structure */ + mutex_t lock; /* Manages exclusive access to this structure */ #ifdef CONFIG_STMPE811_SPI FAR struct spi_dev_s *spi; /* Saved SPI driver instance */ #else diff --git a/drivers/input/stmpe811_adc.c b/drivers/input/stmpe811_adc.c index 2f47575fb2644..2f0a5562b9725 100644 --- a/drivers/input/stmpe811_adc.c +++ b/drivers/input/stmpe811_adc.c @@ -85,10 +85,10 @@ int stmpe811_adcinitialize(STMPE811_HANDLE handle) /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -113,7 +113,7 @@ int stmpe811_adcinitialize(STMPE811_HANDLE handle) /* Mark ADC initialized */ priv->flags |= STMPE811_FLAGS_ADC_INITIALIZED; - nxsem_post(&priv->exclsem); + nxmutex_unock(&priv->lock); return OK; } @@ -144,10 +144,10 @@ int stmpe811_adcconfig(STMPE811_HANDLE handle, int pin) /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -156,7 +156,7 @@ int stmpe811_adcconfig(STMPE811_HANDLE handle, int pin) if ((priv->inuse & pinmask) != 0) { ierr("ERROR: PIN%d is already in-use\n", pin); - nxsem_post(&priv->exclsem); + nxmutex_unock(&priv->lock); return -EBUSY; } @@ -172,7 +172,7 @@ int stmpe811_adcconfig(STMPE811_HANDLE handle, int pin) /* Mark the pin as 'in use' */ priv->inuse |= pinmask; - nxsem_post(&priv->exclsem); + nxmutex_unock(&priv->lock); return OK; } @@ -203,10 +203,10 @@ uint16_t stmpe811_adcread(STMPE811_HANDLE handle, int pin) /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } diff --git a/drivers/input/stmpe811_base.c b/drivers/input/stmpe811_base.c index bc4f5d19f69e8..6618414c4dd75 100644 --- a/drivers/input/stmpe811_base.c +++ b/drivers/input/stmpe811_base.c @@ -288,7 +288,7 @@ STMPE811_HANDLE stmpe811_instantiate(FAR struct i2c_master_s *dev, /* Initialize the device state structure */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->config = config; #ifdef CONFIG_STMPE811_SPI diff --git a/drivers/input/stmpe811_gpio.c b/drivers/input/stmpe811_gpio.c index 8dccac0213b3c..6d70653291af8 100644 --- a/drivers/input/stmpe811_gpio.c +++ b/drivers/input/stmpe811_gpio.c @@ -126,10 +126,10 @@ int stmpe811_gpioconfig(STMPE811_HANDLE handle, uint8_t pinconfig) /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -138,7 +138,7 @@ int stmpe811_gpioconfig(STMPE811_HANDLE handle, uint8_t pinconfig) if ((priv->inuse & pinmask) != 0) { ierr("ERROR: PIN%d is already in-use\n", pin); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -EBUSY; } @@ -223,7 +223,7 @@ int stmpe811_gpioconfig(STMPE811_HANDLE handle, uint8_t pinconfig) /* Mark the pin as 'in use' */ priv->inuse |= pinmask; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -254,10 +254,10 @@ void stmpe811_gpiowrite(STMPE811_HANDLE handle, uint8_t pinconfig, /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return; } @@ -276,7 +276,7 @@ void stmpe811_gpiowrite(STMPE811_HANDLE handle, uint8_t pinconfig, stmpe811_putreg8(priv, STMPE811_GPIO_CLRPIN, (1 << pin)); } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -307,16 +307,16 @@ int stmpe811_gpioread(STMPE811_HANDLE handle, uint8_t pinconfig, bool *value) /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } regval = stmpe811_getreg8(priv, STMPE811_GPIO_MPSTA); *value = ((regval & STMPE811_GPIO_PIN(pin)) != 0); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -355,10 +355,10 @@ int stmpe811_gpioattach(STMPE811_HANDLE handle, uint8_t pinconfig, /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -388,7 +388,7 @@ int stmpe811_gpioattach(STMPE811_HANDLE handle, uint8_t pinconfig, stmpe811_putreg8(priv, STMPE811_GPIO_EN, regval); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } #endif diff --git a/drivers/input/stmpe811_tsc.c b/drivers/input/stmpe811_tsc.c index 3baaa162121f0..89851b6070cd4 100644 --- a/drivers/input/stmpe811_tsc.c +++ b/drivers/input/stmpe811_tsc.c @@ -252,7 +252,7 @@ static inline int stmpe811_waitsample(FAR struct stmpe811_dev_s *priv, * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -280,7 +280,7 @@ static inline int stmpe811_waitsample(FAR struct stmpe811_dev_s *priv, * Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); errout: /* Restore pre-emption. We might get suspended here but that is okay @@ -317,7 +317,7 @@ static int stmpe811_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -332,7 +332,7 @@ static int stmpe811_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* When the reference increments to 1, this is the first open event @@ -343,8 +343,8 @@ static int stmpe811_open(FAR struct file *filep) priv->crefs = tmp; -errout_with_sem: - nxsem_post(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); return ret; #else return OK; @@ -374,7 +374,7 @@ static int stmpe811_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -391,7 +391,7 @@ static int stmpe811_close(FAR struct file *filep) priv->crefs--; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); #endif return OK; } @@ -435,7 +435,7 @@ static ssize_t stmpe811_read(FAR struct file *filep, /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -520,7 +520,7 @@ static ssize_t stmpe811_read(FAR struct file *filep, ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -547,7 +547,7 @@ static int stmpe811_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -579,7 +579,7 @@ static int stmpe811_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -608,7 +608,7 @@ static int stmpe811_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ @@ -676,7 +676,7 @@ static int stmpe811_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -850,7 +850,7 @@ int stmpe811_register(STMPE811_HANDLE handle, int minor) /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -862,7 +862,7 @@ int stmpe811_register(STMPE811_HANDLE handle, int minor) if ((priv->inuse & TSC_PIN_SET) != 0) { ierr("ERROR: TSC pins is already in-use: %02x\n", priv->inuse); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -EBUSY; } @@ -880,7 +880,7 @@ int stmpe811_register(STMPE811_HANDLE handle, int minor) if (ret < 0) { ierr("ERROR: Failed to register driver %s: %d\n", devname, ret); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -892,7 +892,7 @@ int stmpe811_register(STMPE811_HANDLE handle, int minor) priv->inuse |= TSC_PIN_SET; /* Pins 4-7 are now in-use */ priv->flags |= STMPE811_FLAGS_TSC_INITIALIZED; /* TSC function is initialized */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } diff --git a/drivers/input/touchscreen_upper.c b/drivers/input/touchscreen_upper.c index ae1ed01c9530d..a1d9ace19f032 100644 --- a/drivers/input/touchscreen_upper.c +++ b/drivers/input/touchscreen_upper.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -49,7 +50,7 @@ struct touch_openpriv_s struct list_node node; /* Opened file buffer linked list node */ FAR struct pollfd *fds; /* Polling structure of waiting thread */ sem_t waitsem; /* Used to wait for the availability of data */ - sem_t locksem; /* Manages exclusive access to this structure */ + mutex_t lock; /* Manages exclusive access to this structure */ }; /* This structure is for touchscreen upper half driver */ @@ -57,7 +58,7 @@ struct touch_openpriv_s struct touch_upperhalf_s { uint8_t nums; /* Number of touch point structure */ - sem_t exclsem; /* Manages exclusive access to this structure */ + mutex_t lock; /* Manages exclusive access to this structure */ struct list_node head; /* Opened file buffer chain header node */ FAR struct touch_lowerhalf_s *lower; /* A pointer of lower half instance */ }; @@ -125,7 +126,7 @@ static int touch_open(FAR struct file *filep) return ret; } - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { circbuf_uninit(&openpriv->circbuf); @@ -134,7 +135,7 @@ static int touch_open(FAR struct file *filep) } nxsem_init(&openpriv->waitsem, 0, 0); - nxsem_init(&openpriv->locksem, 0, 1); + nxmutex_init(&openpriv->lock); nxsem_set_protocol(&openpriv->waitsem, SEM_PRIO_NONE); list_add_tail(&upper->head, &openpriv->node); @@ -143,7 +144,7 @@ static int touch_open(FAR struct file *filep) */ filep->f_priv = openpriv; - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -158,7 +159,7 @@ static int touch_close(FAR struct file *filep) FAR struct touch_upperhalf_s *upper = inode->i_private; int ret; - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -167,10 +168,10 @@ static int touch_close(FAR struct file *filep) list_delete(&openpriv->node); circbuf_uninit(&openpriv->circbuf); nxsem_destroy(&openpriv->waitsem); - nxsem_destroy(&openpriv->locksem); + nxmutex_destroy(&openpriv->lock); kmm_free(openpriv); - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -208,7 +209,7 @@ static ssize_t touch_read(FAR struct file *filep, FAR char *buffer, return -EINVAL; } - ret = nxsem_wait(&openpriv->locksem); + ret = nxmutex_lock(&openpriv->lock); if (ret < 0) { return ret; @@ -223,14 +224,14 @@ static ssize_t touch_read(FAR struct file *filep, FAR char *buffer, } else { - nxsem_post(&openpriv->locksem); + nxmutex_unlock(&openpriv->lock); ret = nxsem_wait_uninterruptible(&openpriv->waitsem); if (ret < 0) { return ret; } - ret = nxsem_wait(&openpriv->locksem); + ret = nxmutex_lock(&openpriv->lock); if (ret < 0) { return ret; @@ -241,7 +242,7 @@ static ssize_t touch_read(FAR struct file *filep, FAR char *buffer, ret = circbuf_read(&openpriv->circbuf, buffer, len); out: - nxsem_post(&openpriv->locksem); + nxmutex_unlock(&openpriv->lock); return ret; } @@ -252,7 +253,7 @@ static int touch_ioctl(FAR struct file *filep, int cmd, unsigned long arg) FAR struct touch_lowerhalf_s *lower = upper->lower; int ret; - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -267,7 +268,7 @@ static int touch_ioctl(FAR struct file *filep, int cmd, unsigned long arg) ret = -ENOTTY; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -277,7 +278,7 @@ static int touch_poll(FAR struct file *filep, struct pollfd *fds, bool setup) pollevent_t eventset = 0; int ret; - ret = nxsem_wait(&openpriv->locksem); + ret = nxmutex_lock(&openpriv->lock); if (ret < 0) { return ret; @@ -310,7 +311,7 @@ static int touch_poll(FAR struct file *filep, struct pollfd *fds, bool setup) } errout: - nxsem_post(&openpriv->locksem); + nxmutex_unlock(&openpriv->lock); return ret; } @@ -324,7 +325,7 @@ void touch_event(FAR void *priv, FAR const struct touch_sample_s *sample) FAR struct touch_openpriv_s *openpriv; int semcount; - if (nxsem_wait(&upper->exclsem) < 0) + if (nxmutex_lock(&upper->lock) < 0) { return; } @@ -343,7 +344,7 @@ void touch_event(FAR void *priv, FAR const struct touch_sample_s *sample) poll_notify(&openpriv->fds, 1, POLLIN); } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); } int touch_register(FAR struct touch_lowerhalf_s *lower, @@ -371,12 +372,12 @@ int touch_register(FAR struct touch_lowerhalf_s *lower, upper->lower = lower; upper->nums = nums; list_initialize(&upper->head); - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); ret = register_driver(path, &g_touch_fops, 0666, upper); if (ret < 0) { - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); return ret; } @@ -396,6 +397,6 @@ void touch_unregister(FAR struct touch_lowerhalf_s *lower, iinfo("UnRegistering %s\n", path); unregister_driver(path); - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); } diff --git a/drivers/input/tsc2007.c b/drivers/input/tsc2007.c index eefccda8e5582..bd6cbc057e871 100644 --- a/drivers/input/tsc2007.c +++ b/drivers/input/tsc2007.c @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -152,7 +153,7 @@ struct tsc2007_dev_s uint8_t nwaiters; /* Number of threads waiting for TSC2007 data */ uint8_t id; /* Current touch point ID */ volatile bool penchange; /* An unreported event is buffered */ - sem_t devsem; /* Manages exclusive access to this structure */ + mutex_t devlock; /* Manages exclusive access to this structure */ sem_t waitsem; /* Used to wait for the availability of data */ FAR struct tsc2007_config_s *config; /* Board configuration data */ @@ -339,7 +340,7 @@ static int tsc2007_waitsample(FAR struct tsc2007_dev_s *priv, * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -365,7 +366,7 @@ static int tsc2007_waitsample(FAR struct tsc2007_dev_s *priv, * sample. Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); errout: /* Then re-enable interrupts. We might get interrupt here and there @@ -788,7 +789,7 @@ static int tsc2007_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -803,7 +804,7 @@ static int tsc2007_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* When the reference increments to 1, this is the first open event @@ -814,8 +815,8 @@ static int tsc2007_open(FAR struct file *filep) priv->crefs = tmp; -errout_with_sem: - nxsem_post(&priv->devsem); +errout_with_lock: + nxmutex_unlock(&priv->devlock); return ret; #else return OK; @@ -841,7 +842,7 @@ static int tsc2007_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -858,7 +859,7 @@ static int tsc2007_close(FAR struct file *filep) priv->crefs--; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); #endif return OK; } @@ -897,7 +898,7 @@ static ssize_t tsc2007_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -992,7 +993,7 @@ static ssize_t tsc2007_read(FAR struct file *filep, FAR char *buffer, ret = SIZEOF_TOUCH_SAMPLE_S(1); errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1015,7 +1016,7 @@ static int tsc2007_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -1063,7 +1064,7 @@ static int tsc2007_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1088,7 +1089,7 @@ static int tsc2007_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { ierr("ERROR: nxsem_wait failed: %d\n", ret); @@ -1154,7 +1155,7 @@ static int tsc2007_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1220,7 +1221,7 @@ int tsc2007_register(FAR struct i2c_master_s *dev, priv->i2c = dev; /* Save the I2C device handle */ priv->config = config; /* Save the board configuration */ - nxsem_init(&priv->devsem, 0, 1); /* Initialize device structure semaphore */ + nxmutex_init(&priv->devlock); /* Initialize device structure mutex */ nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */ /* The event wait semaphore is used for signaling and, hence, should not @@ -1294,7 +1295,7 @@ int tsc2007_register(FAR struct i2c_master_s *dev, return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); #ifdef CONFIG_TSC2007_MULTIPLE kmm_free(priv); #endif diff --git a/drivers/ioexpander/mcp23x17.c b/drivers/ioexpander/mcp23x17.c index 961669248f80c..34b864ff6dc4e 100644 --- a/drivers/ioexpander/mcp23x17.c +++ b/drivers/ioexpander/mcp23x17.c @@ -128,21 +128,6 @@ static const struct ioexpander_ops_s g_mcp23x17_ops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: mcp23x17_lock - * - * Description: - * Get exclusive access to the MCP23X17 - * - ****************************************************************************/ - -static int mcp23x17_lock(FAR struct mcp23x17_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -#define mcp23x17_unlock(p) nxsem_post(&(p)->exclsem) - /**************************************************************************** * Name: mcp23x17_write * @@ -334,7 +319,7 @@ static int mcp23x17_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -342,7 +327,7 @@ static int mcp23x17_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = mcp23x17_setbit(priv, MCP23X17_IODIRA, pin, (direction == IOEXPANDER_DIRECTION_IN)); - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -375,7 +360,7 @@ static int mcp23x17_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, { /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -383,7 +368,7 @@ static int mcp23x17_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = mcp23x17_setbit(priv, MCP23X17_IPOLA, pin, ((uintptr_t)value == IOEXPANDER_VAL_INVERT)); - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); } return ret; @@ -414,14 +399,14 @@ static int mcp23x17_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } ret = mcp23x17_setbit(priv, MCP23X17_GPIOA, pin, value); - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -452,7 +437,7 @@ static int mcp23x17_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -460,7 +445,7 @@ static int mcp23x17_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = mcp23x17_getbit(priv, MCP23X17_GPIOA, pin, value); - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -489,14 +474,14 @@ static int mcp23x17_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } ret = mcp23x17_getbit(priv, MCP23X17_GPIOA, pin, value); - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -588,7 +573,7 @@ static int mcp23x17_multiwritepin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -603,7 +588,7 @@ static int mcp23x17_multiwritepin(FAR struct ioexpander_dev_s *dev, ret = mcp23x17_writeread(priv, &addr, 1, &buf[1], 2); if (ret < 0) { - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #else @@ -621,7 +606,7 @@ static int mcp23x17_multiwritepin(FAR struct ioexpander_dev_s *dev, pin = pins[i]; if (pin > 15) { - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return -ENXIO; } else if (pin > 7) @@ -651,7 +636,7 @@ static int mcp23x17_multiwritepin(FAR struct ioexpander_dev_s *dev, #endif ret = mcp23x17_write(priv, buf, 3); - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -681,7 +666,7 @@ static int mcp23x17_multireadpin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -689,7 +674,7 @@ static int mcp23x17_multireadpin(FAR struct ioexpander_dev_s *dev, ret = mcp23x17_getmultibits(priv, MCP23X17_GPIOA, pins, values, count); - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -719,7 +704,7 @@ static int mcp23x17_multireadbuf(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -727,7 +712,7 @@ static int mcp23x17_multireadbuf(FAR struct ioexpander_dev_s *dev, ret = mcp23x17_getmultibits(priv, MCP23X17_GPIOA, pins, values, count); - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -766,7 +751,7 @@ static FAR void *mcp23x17_attach(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the MCP23X17 */ - ret = mcp23x17_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -792,7 +777,7 @@ static FAR void *mcp23x17_attach(FAR struct ioexpander_dev_s *dev, /* Add this callback to the table */ - mcp23x17_unlock(priv); + nxmutex_unlock(&priv->lock); return handle; } @@ -986,7 +971,7 @@ FAR struct ioexpander_dev_s *mcp23x17_initialize( priv->config->enable(priv->config, TRUE); #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); return &priv->dev; } diff --git a/drivers/ioexpander/mcp23x17.h b/drivers/ioexpander/mcp23x17.h index 9a58efcde45e1..ee87278af867f 100644 --- a/drivers/ioexpander/mcp23x17.h +++ b/drivers/ioexpander/mcp23x17.h @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include #include @@ -154,7 +154,7 @@ struct mcp23x17_dev_s */ FAR struct mcp23x17_config_s *config; /* Board configuration data */ FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */ - sem_t exclsem; /* Mutual exclusion */ + mutex_t lock; /* Mutual exclusion */ #ifdef CONFIG_IOEXPANDER_INT_ENABLE #ifdef CONFIG_MCP23X17_INT_POLL diff --git a/drivers/ioexpander/pca9538.c b/drivers/ioexpander/pca9538.c index cd7c1a7b79cef..24ac1d9160a34 100644 --- a/drivers/ioexpander/pca9538.c +++ b/drivers/ioexpander/pca9538.c @@ -121,21 +121,6 @@ static const struct ioexpander_ops_s g_pca9538_ops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: pca9538_lock - * - * Description: - * Get exclusive access to the PCA9538 - * - ****************************************************************************/ - -static int pca9538_lock(FAR struct pca9538_dev_s *pca) -{ - return nxsem_wait_uninterruptible(&pca->exclsem); -} - -#define pca9538_unlock(p) nxsem_post(&(p)->exclsem) - /**************************************************************************** * Name: pca9538_write * @@ -317,7 +302,7 @@ static int pca9538_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -325,7 +310,7 @@ static int pca9538_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = pca9538_setbit(pca, PCA9538_REG_CONFIG, pin, (direction == IOEXPANDER_DIRECTION_IN)); - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -358,7 +343,7 @@ static int pca9538_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, { /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -366,7 +351,7 @@ static int pca9538_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = pca9538_setbit(pca, PCA9538_REG_POLINV, pin, ((uintptr_t)value == IOEXPANDER_VAL_INVERT)); - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); } return ret; @@ -397,14 +382,14 @@ static int pca9538_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; } ret = pca9538_setbit(pca, PCA9538_REG_OUTPUT, pin, value); - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -435,14 +420,14 @@ static int pca9538_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; } ret = pca9538_getbit(pca, PCA9538_REG_INPUT, pin, value); - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -471,14 +456,14 @@ static int pca9538_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; } ret = pca9538_getbit(pca, PCA9538_REG_OUTPUT, pin, value); - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -563,7 +548,7 @@ static int pca9538_multiwritepin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -578,7 +563,7 @@ static int pca9538_multiwritepin(FAR struct ioexpander_dev_s *dev, ret = pca9538_writeread(pca, &addr, 1, &buf[1], 2); if (ret < 0) { - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } #else @@ -596,7 +581,7 @@ static int pca9538_multiwritepin(FAR struct ioexpander_dev_s *dev, pin = pins[i]; if (pin >= PCA9538_GPIO_NPINS) { - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return -ENXIO; } @@ -621,7 +606,7 @@ static int pca9538_multiwritepin(FAR struct ioexpander_dev_s *dev, #endif ret = pca9538_write(pca, buf, 3); - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -651,7 +636,7 @@ static int pca9538_multireadpin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -659,7 +644,7 @@ static int pca9538_multireadpin(FAR struct ioexpander_dev_s *dev, ret = pca9538_getmultibits(pca, PCA9538_REG_INPUT, pins, values, count); - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -689,7 +674,7 @@ static int pca9538_multireadbuf(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -697,7 +682,7 @@ static int pca9538_multireadbuf(FAR struct ioexpander_dev_s *dev, ret = pca9538_getmultibits(pca, PCA9538_REG_OUTPUT, pins, values, count); - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -735,7 +720,7 @@ static FAR void *pca9538_attach(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the PCA555 */ - ret = pca9538_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -761,7 +746,7 @@ static FAR void *pca9538_attach(FAR struct ioexpander_dev_s *dev, /* Add this callback to the table */ - pca9538_unlock(pca); + nxmutex_unlock(&pca->lock); return handle; } @@ -957,7 +942,7 @@ FAR struct ioexpander_dev_s *pca9538_initialize pcadev->config->enable(pcadev->config, TRUE); #endif - nxsem_init(&pcadev->exclsem, 0, 1); + nxmutex_init(&pcadev->lock); return &pcadev->dev; } diff --git a/drivers/ioexpander/pca9538.h b/drivers/ioexpander/pca9538.h index 1c7f55f49965c..7faa5b55e1012 100644 --- a/drivers/ioexpander/pca9538.h +++ b/drivers/ioexpander/pca9538.h @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include #include @@ -114,20 +114,20 @@ struct pca9538_callback_s struct pca9538_dev_s { - struct ioexpander_dev_s dev; /* Nested structure to allow casting - * as public gpio expander. */ + struct ioexpander_dev_s dev; /* Nested structure to allow casting + * as public gpio expander. */ #ifdef CONFIG_PCA9538_SHADOW_MODE - uint8_t sreg[8]; /* Shadowed registers of the PCA9538 */ + uint8_t sreg[8]; /* Shadowed registers of the PCA9538 */ #endif #ifdef CONFIG_PCA9538_MULTIPLE - FAR struct pca9538_dev_s *flink; /* Supports a singly linked list of drivers */ + FAR struct pca9538_dev_s *flink; /* Supports a singly linked list of drivers */ #endif - FAR struct pca9538_config_s *config; /* Board configuration data */ - FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */ - sem_t exclsem; /* Mutual exclusion */ + FAR struct pca9538_config_s *config; /* Board configuration data */ + FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */ + mutex_t lock; /* Mutual exclusion */ #ifdef CONFIG_IOEXPANDER_INT_ENABLE - struct work_s work; /* Supports the interrupt handling "bottom half" */ + struct work_s work; /* Supports the interrupt handling "bottom half" */ /* Saved callback information for each I/O expander client */ diff --git a/drivers/ioexpander/pca9555.c b/drivers/ioexpander/pca9555.c index e2175e9e184d6..052b5f9a0f7bf 100644 --- a/drivers/ioexpander/pca9555.c +++ b/drivers/ioexpander/pca9555.c @@ -125,21 +125,6 @@ static const struct ioexpander_ops_s g_pca9555_ops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: pca9555_lock - * - * Description: - * Get exclusive access to the PCA9555 - * - ****************************************************************************/ - -static int pca9555_lock(FAR struct pca9555_dev_s *pca) -{ - return nxsem_wait_uninterruptible(&pca->exclsem); -} - -#define pca9555_unlock(p) nxsem_post(&(p)->exclsem) - /**************************************************************************** * Name: pca9555_write * @@ -331,7 +316,7 @@ static int pca9555_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -339,7 +324,7 @@ static int pca9555_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = pca9555_setbit(pca, PCA9555_REG_CONFIG, pin, (direction == IOEXPANDER_DIRECTION_IN)); - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -372,7 +357,7 @@ static int pca9555_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, { /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -380,7 +365,7 @@ static int pca9555_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = pca9555_setbit(pca, PCA9555_REG_POLINV, pin, ((uintptr_t)value == IOEXPANDER_VAL_INVERT)); - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); } return ret; @@ -411,14 +396,14 @@ static int pca9555_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; } ret = pca9555_setbit(pca, PCA9555_REG_OUTPUT, pin, value); - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -449,14 +434,14 @@ static int pca9555_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; } ret = pca9555_getbit(pca, PCA9555_REG_INPUT, pin, value); - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -485,14 +470,14 @@ static int pca9555_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; } ret = pca9555_getbit(pca, PCA9555_REG_OUTPUT, pin, value); - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -582,7 +567,7 @@ static int pca9555_multiwritepin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -597,7 +582,7 @@ static int pca9555_multiwritepin(FAR struct ioexpander_dev_s *dev, ret = pca9555_writeread(pca, &addr, 1, &buf[1], 2); if (ret < 0) { - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } #else @@ -615,7 +600,7 @@ static int pca9555_multiwritepin(FAR struct ioexpander_dev_s *dev, pin = pins[i]; if (pin > 15) { - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return -ENXIO; } else if (pin > 7) @@ -645,7 +630,7 @@ static int pca9555_multiwritepin(FAR struct ioexpander_dev_s *dev, #endif ret = pca9555_write(pca, buf, 3); - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -675,7 +660,7 @@ static int pca9555_multireadpin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -683,7 +668,7 @@ static int pca9555_multireadpin(FAR struct ioexpander_dev_s *dev, ret = pca9555_getmultibits(pca, PCA9555_REG_INPUT, pins, values, count); - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -713,7 +698,7 @@ static int pca9555_multireadbuf(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -721,7 +706,7 @@ static int pca9555_multireadbuf(FAR struct ioexpander_dev_s *dev, ret = pca9555_getmultibits(pca, PCA9555_REG_OUTPUT, pins, values, count); - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return ret; } @@ -759,7 +744,7 @@ static FAR void *pca9555_attach(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the PCA555 */ - ret = pca9555_lock(pca); + ret = nxmutex_lock(&pca->lock); if (ret < 0) { return ret; @@ -785,7 +770,7 @@ static FAR void *pca9555_attach(FAR struct ioexpander_dev_s *dev, /* Add this callback to the table */ - pca9555_unlock(pca); + nxmutex_unlock(&pca->lock); return handle; } @@ -978,7 +963,7 @@ FAR struct ioexpander_dev_s *pca9555_initialize( pcadev->config->enable(pcadev->config, TRUE); #endif - nxsem_init(&pcadev->exclsem, 0, 1); + nxmutex_init(&pcadev->lock); return &pcadev->dev; } diff --git a/drivers/ioexpander/pca9555.h b/drivers/ioexpander/pca9555.h index 0c0308a49723d..5981713f7a05f 100644 --- a/drivers/ioexpander/pca9555.h +++ b/drivers/ioexpander/pca9555.h @@ -34,7 +34,7 @@ #include #include -#include +#include #include #include #include @@ -128,20 +128,20 @@ struct pca9555_callback_s struct pca9555_dev_s { - struct ioexpander_dev_s dev; /* Nested structure to allow casting as public gpio - * expander. */ + struct ioexpander_dev_s dev; /* Nested structure to allow casting as public gpio + * expander. */ #ifdef CONFIG_PCA9555_SHADOW_MODE - uint8_t sreg[8]; /* Shadowed registers of the PCA9555 */ + uint8_t sreg[8]; /* Shadowed registers of the PCA9555 */ #endif #ifdef CONFIG_PCA9555_MULTIPLE - FAR struct pca9555_dev_s *flink; /* Supports a singly linked list of drivers */ + FAR struct pca9555_dev_s *flink; /* Supports a singly linked list of drivers */ #endif - FAR struct pca9555_config_s *config; /* Board configuration data */ - FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */ - sem_t exclsem; /* Mutual exclusion */ + FAR struct pca9555_config_s *config; /* Board configuration data */ + FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */ + mutex_t lock; /* Mutual exclusion */ #ifdef CONFIG_IOEXPANDER_INT_ENABLE - struct work_s work; /* Supports the interrupt handling "bottom half" */ + struct work_s work; /* Supports the interrupt handling "bottom half" */ /* Saved callback information for each I/O expander client */ diff --git a/drivers/ioexpander/pcf8574.c b/drivers/ioexpander/pcf8574.c index 105abd9fafa70..cf1a469b5c9f6 100644 --- a/drivers/ioexpander/pcf8574.c +++ b/drivers/ioexpander/pcf8574.c @@ -129,21 +129,6 @@ static const struct ioexpander_ops_s g_pcf8574_ops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: pcf8574_lock - * - * Description: - * Get exclusive access to the I/O Expander - * - ****************************************************************************/ - -static int pcf8574_lock(FAR struct pcf8574_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -#define pcf8574_unlock(p) nxsem_post(&(p)->exclsem) - /**************************************************************************** * Name: pcf8574_read * @@ -255,7 +240,7 @@ static int pcf8574_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = pcf8574_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -282,7 +267,7 @@ static int pcf8574_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = pcf8574_write(priv, priv->inpins | priv->outstate); - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -325,7 +310,7 @@ static int pcf8574_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, ioe_pinset_t bit = ((ioe_pinset_t)1 << pin); ret = OK; - ret = pcf8574_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -370,7 +355,7 @@ static int pcf8574_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = -EINVAL; } - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); } #endif @@ -407,7 +392,7 @@ static int pcf8574_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = pcf8574_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -418,7 +403,7 @@ static int pcf8574_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, if ((priv->inpins & (1 << pin)) != 0) { gpioerr("ERROR: pin%u is an input\n", pin); - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); return -EINVAL; } @@ -439,7 +424,7 @@ static int pcf8574_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = pcf8574_write(priv, priv->inpins | priv->outstate); - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -483,7 +468,7 @@ static int pcf8574_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = pcf8574_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -498,7 +483,7 @@ static int pcf8574_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, */ *value = ((priv->outstate & (1 << pin)) != 0); - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); return OK; } @@ -529,7 +514,7 @@ static int pcf8574_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = OK; errout_with_lock: - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -567,7 +552,7 @@ static int pcf8574_multiwritepin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = pcf8574_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -608,7 +593,7 @@ static int pcf8574_multiwritepin(FAR struct ioexpander_dev_s *dev, ret = pcf8574_write(priv, priv->inpins | priv->outstate); - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -648,7 +633,7 @@ static int pcf8574_multireadpin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = pcf8574_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -704,7 +689,7 @@ static int pcf8574_multireadpin(FAR struct ioexpander_dev_s *dev, ret = OK; errout_with_lock: - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -740,7 +725,7 @@ static FAR void *pcf8574_attach(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = pcf8574_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -764,7 +749,7 @@ static FAR void *pcf8574_attach(FAR struct ioexpander_dev_s *dev, } } - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); return handle; } #endif @@ -929,7 +914,7 @@ static void pcf8574_irqworker(void *arg) /* Check for pending interrupts */ - ret = pcf8574_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -941,7 +926,7 @@ static void pcf8574_irqworker(void *arg) pinset = priv->intstat; priv->intstat = 0; - pcf8574_unlock(priv); + nxmutex_unlock(&priv->lock); /* Perform pin interrupt callbacks */ @@ -1155,7 +1140,7 @@ FAR struct ioexpander_dev_s *pcf8574_initialize(FAR struct i2c_master_s *i2c, priv->config->enable(config, true); #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); return &priv->dev; } diff --git a/drivers/ioexpander/pcf8574.h b/drivers/ioexpander/pcf8574.h index af6bc024fce9f..f1b3b8658edc8 100644 --- a/drivers/ioexpander/pcf8574.h +++ b/drivers/ioexpander/pcf8574.h @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include #include @@ -129,7 +129,7 @@ struct pcf8574_dev_s * expander. */ FAR struct pcf8574_config_s *config; /* Board configuration data */ FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */ - sem_t exclsem; /* Mutual exclusion */ + mutex_t lock; /* Mutual exclusion */ uint8_t inpins; /* Set of input pins */ uint8_t outstate; /* State of all output pins */ diff --git a/drivers/ioexpander/skeleton.c b/drivers/ioexpander/skeleton.c index 8e929e9a07ef4..51974ca32d771 100644 --- a/drivers/ioexpander/skeleton.c +++ b/drivers/ioexpander/skeleton.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include @@ -63,7 +63,7 @@ struct skel_dev_s #ifdef CONFIG_skeleton_MULTIPLE FAR struct skel_dev_s *flink; /* Supports a singly linked list of drivers */ #endif - sem_t exclsem; /* Mutual exclusion */ + mutex_t lock; /* Mutual exclusion */ #ifdef CONFIG_IOEXPANDER_INT_ENABLE struct work_s work; /* Supports the interrupt handling "bottom half" */ @@ -145,21 +145,6 @@ static const struct ioexpander_ops_s g_skel_ops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: skel_lock - * - * Description: - * Get exclusive access to the I/O Expander - * - ****************************************************************************/ - -static int skel_lock(FAR struct skel_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -#define skel_unlock(p) nxsem_post(&(p)->exclsem) - /**************************************************************************** * Name: skel_direction * @@ -195,7 +180,7 @@ static int skel_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -204,7 +189,7 @@ static int skel_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Set the pin direction in the I/O Expander */ #warning Missing logic - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -243,7 +228,7 @@ static int skel_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, { /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -252,7 +237,7 @@ static int skel_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Set the pin option */ #warning Missing logic - skel_unlock(priv); + nxmutex_unlock(&priv->lock); } return ret; @@ -287,7 +272,7 @@ static int skel_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -296,7 +281,7 @@ static int skel_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Write the pin value */ #warning Missing logic - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -332,7 +317,7 @@ static int skel_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -344,7 +329,7 @@ static int skel_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Return the pin value via the value pointer */ #warning Missing logic - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -373,7 +358,7 @@ static int skel_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -382,7 +367,7 @@ static int skel_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Read the buffered pin level */ #warning Missing logic - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -452,7 +437,7 @@ static int skel_multiwritepin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -468,7 +453,7 @@ static int skel_multiwritepin(FAR struct ioexpander_dev_s *dev, pin = pins[i]; if (pin >= CONFIG_IOEXPANDER_NPINS) { - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return -ENXIO; } @@ -485,7 +470,7 @@ static int skel_multiwritepin(FAR struct ioexpander_dev_s *dev, /* Now write back the new pins states */ #warning Missing logic - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -520,14 +505,14 @@ static int skel_multireadpin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } ret = skel_getmultibits(priv, pins, values, count); - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -562,14 +547,14 @@ static int skel_multireadbuf(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; } ret = skel_getmultibits(priv, pins, values, count); - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -601,7 +586,7 @@ static int skel_attach(FAR struct ioexpander_dev_s *dev, ioe_pinset_t pinset, /* Get exclusive access to the I/O Expander */ - ret = skel_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -626,7 +611,7 @@ static int skel_attach(FAR struct ioexpander_dev_s *dev, ioe_pinset_t pinset, /* Add this callback to the table */ - skel_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -820,7 +805,7 @@ FAR struct ioexpander_dev_s *skel_initialize(void) #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); return &priv->dev; } diff --git a/drivers/ioexpander/tca64xx.c b/drivers/ioexpander/tca64xx.c index 769c95887bd84..40347c929db6c 100644 --- a/drivers/ioexpander/tca64xx.c +++ b/drivers/ioexpander/tca64xx.c @@ -55,7 +55,6 @@ /* TCA64xx Helpers */ -static int tca64_lock(FAR struct tca64_dev_s *priv); static FAR const struct tca64_part_s *tca64_getpart( FAR struct tca64_dev_s *priv); static uint8_t tca64_ngpios(FAR struct tca64_dev_s *priv); @@ -167,21 +166,6 @@ static const struct tca64_part_s g_tca64_parts[TCA64_NPARTS] = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: tca64_lock - * - * Description: - * Get exclusive access to the I/O Expander - * - ****************************************************************************/ - -static int tca64_lock(FAR struct tca64_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->exclsem); -} - -#define tca64_unlock(p) nxsem_post(&(p)->exclsem) - /**************************************************************************** * Name: tca64_getpart * @@ -415,7 +399,7 @@ static int tca64_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -467,7 +451,7 @@ static int tca64_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin, } errout_with_lock: - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -516,7 +500,7 @@ static int tca64_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -530,7 +514,7 @@ static int tca64_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, { gpioerr("ERROR: Failed to read polarity register at %u: %d\n", regaddr, ret); - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -554,7 +538,7 @@ static int tca64_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, regaddr, ret); } - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); } #ifdef CONFIG_TCA64XX_INT_ENABLE @@ -565,7 +549,7 @@ static int tca64_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, unsigned int ival = (unsigned int)((uintptr_t)value); ioe_pinset_t bit = ((ioe_pinset_t)1 << pin); - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -610,7 +594,7 @@ static int tca64_option(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = -EINVAL; } - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); } #endif @@ -650,7 +634,7 @@ static int tca64_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -691,7 +675,7 @@ static int tca64_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin, } errout_with_lock: - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -729,7 +713,7 @@ static int tca64_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, /* Get exclusive access to the I/O Expander */ - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -764,7 +748,7 @@ static int tca64_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin, ret = OK; errout_with_lock: - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -801,7 +785,7 @@ static int tca64_multiwritepin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -851,7 +835,7 @@ static int tca64_multiwritepin(FAR struct ioexpander_dev_s *dev, } errout_with_lock: - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -894,7 +878,7 @@ static int tca64_multireadpin(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -937,7 +921,7 @@ static int tca64_multireadpin(FAR struct ioexpander_dev_s *dev, #endif errout_with_lock: - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -973,7 +957,7 @@ static FAR void *tca64_attach(FAR struct ioexpander_dev_s *dev, /* Get exclusive access to the I/O Expander */ - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -997,7 +981,7 @@ static FAR void *tca64_attach(FAR struct ioexpander_dev_s *dev, } } - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); return handle; } @@ -1173,7 +1157,7 @@ static void tca64_irqworker(void *arg) /* Get exclusive access to read inputs and assess pending interrupts. */ - ret = tca64_lock(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1196,7 +1180,7 @@ static void tca64_irqworker(void *arg) { gpioerr("ERROR: Failed to read input %u registers at %u: %d\n", nregs, regaddr, ret); - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); goto errout_with_restart; } @@ -1208,7 +1192,7 @@ static void tca64_irqworker(void *arg) pinset = priv->intstat; priv->intstat = 0; - tca64_unlock(priv); + nxmutex_unlock(&priv->lock); /* Perform pin interrupt callbacks */ @@ -1422,7 +1406,7 @@ FAR struct ioexpander_dev_s *tca64_initialize(FAR struct i2c_master_s *i2c, priv->config->enable(config, true); #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); return &priv->dev; } diff --git a/drivers/ioexpander/tca64xx.h b/drivers/ioexpander/tca64xx.h index e3391f37fa970..bcec8af971a9f 100644 --- a/drivers/ioexpander/tca64xx.h +++ b/drivers/ioexpander/tca64xx.h @@ -34,7 +34,7 @@ #include #include -#include +#include #include #include #include @@ -204,7 +204,7 @@ struct tca64_dev_s FAR struct tca64_config_s *config; /* Board configuration data */ FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */ uint8_t part; /* TCA64xx part ID (see enum tca64xx_part_e) */ - sem_t exclsem; /* Mutual exclusion */ + mutex_t lock; /* Mutual exclusion */ #ifdef CONFIG_IOEXPANDER_INT_ENABLE #ifdef CONFIG_TCA64XX_INT_POLL diff --git a/drivers/ipcc/ipcc_close.c b/drivers/ipcc/ipcc_close.c index 616e4c72fbd79..1f1f7457bb9a5 100644 --- a/drivers/ipcc/ipcc_close.c +++ b/drivers/ipcc/ipcc_close.c @@ -68,7 +68,7 @@ int ipcc_close(FAR struct file *filep) /* Get exclusive access to the IPCC driver state structure */ - if ((ret = nxsem_wait(&priv->exclsem)) < 0) + if ((ret = nxmutex_lock(&priv->lock)) < 0) { return ret; } @@ -89,6 +89,6 @@ int ipcc_close(FAR struct file *filep) return OK; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } diff --git a/drivers/ipcc/ipcc_open.c b/drivers/ipcc/ipcc_open.c index 38c1b68bcb095..f763eabfd22fb 100644 --- a/drivers/ipcc/ipcc_open.c +++ b/drivers/ipcc/ipcc_open.c @@ -65,7 +65,7 @@ int ipcc_open(FAR struct file *filep) /* Get exclusive access to the IPCC driver state structure */ - if ((ret = nxsem_wait(&priv->exclsem)) < 0) + if ((ret = nxmutex_lock(&priv->lock)) < 0) { return ret; } @@ -75,6 +75,6 @@ int ipcc_open(FAR struct file *filep) priv->crefs++; DEBUGASSERT(priv->crefs > 0); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } diff --git a/drivers/ipcc/ipcc_poll.c b/drivers/ipcc/ipcc_poll.c index aa986121dedc0..075778d1653a8 100644 --- a/drivers/ipcc/ipcc_poll.c +++ b/drivers/ipcc/ipcc_poll.c @@ -77,9 +77,9 @@ int ipcc_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Get exclusive access to driver */ - if ((ret = nxsem_wait(&priv->exclsem)) < 0) + if ((ret = nxmutex_lock(&priv->lock)) < 0) { - /* nxsem_wait() will return on signal, we did not start + /* nxmutex_lock() will return on signal, we did not start * any transfer yet, so we can safely return with error */ @@ -98,7 +98,7 @@ int ipcc_poll(FAR struct file *filep, FAR struct pollfd *fds, *slot = NULL; fds->priv = NULL; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -125,7 +125,7 @@ int ipcc_poll(FAR struct file *filep, FAR struct pollfd *fds, /* No free poll slot found */ fds->priv = NULL; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -EBUSY; } @@ -147,6 +147,6 @@ int ipcc_poll(FAR struct file *filep, FAR struct pollfd *fds, poll_notify(priv->fds, CONFIG_IPCC_NPOLLWAITERS, eventset); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } diff --git a/drivers/ipcc/ipcc_priv.h b/drivers/ipcc/ipcc_priv.h index 398c6446e5e90..6d64e681839f1 100644 --- a/drivers/ipcc/ipcc_priv.h +++ b/drivers/ipcc/ipcc_priv.h @@ -24,6 +24,7 @@ #include #include +#include #include /**************************************************************************** @@ -38,7 +39,7 @@ struct ipcc_driver_s FAR struct ipcc_lower_s *ipcc; /* Lower half driver state */ int crefs; /* Count number of open references */ int unlinked; /* 1 - driver has been unlinked */ - sem_t exclsem; /* Mutual exclusion for driver */ + mutex_t lock; /* Mutual exclusion for driver */ sem_t rxsem; /* Data ready to read semaphore */ sem_t txsem; /* Data ready to send semaphore */ }; diff --git a/drivers/ipcc/ipcc_read.c b/drivers/ipcc/ipcc_read.c index cb7c2f15b56ea..2c5c4bc58e7ee 100644 --- a/drivers/ipcc/ipcc_read.c +++ b/drivers/ipcc/ipcc_read.c @@ -131,7 +131,7 @@ ssize_t ipcc_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to driver */ - if ((ret = nxsem_wait(&priv->exclsem))) + if ((ret = nxmutex_lock(&priv->lock))) { /* nxsem_wait() will return on signal, we did not start * any transfer yet, so we can safely return with error @@ -197,7 +197,7 @@ ssize_t ipcc_read(FAR struct file *filep, FAR char *buffer, /* return number of bytes read to the caller */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nread; } #else /* CONFIG_IPCC_BUFFERED */ @@ -212,7 +212,7 @@ ssize_t ipcc_read(FAR struct file *filep, FAR char *buffer, */ leave_critical_section(flags); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nread; } #endif /* CONFIG_IPCC_BUFFERED */ @@ -226,14 +226,14 @@ ssize_t ipcc_read(FAR struct file *filep, FAR char *buffer, */ leave_critical_section(flags); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -EAGAIN; } /* We are in blocking mode, so we have to wait for data to arrive. */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if ((ret = nxsem_wait(&priv->rxsem))) { leave_critical_section(flags); @@ -254,7 +254,7 @@ ssize_t ipcc_read(FAR struct file *filep, FAR char *buffer, * waiting for data, so now let's retake it. */ - nxsem_wait(&priv->exclsem); + nxmutex_lock(&priv->lock); } leave_critical_section(flags); diff --git a/drivers/ipcc/ipcc_register.c b/drivers/ipcc/ipcc_register.c index 22d0736b88187..208014536a54a 100644 --- a/drivers/ipcc/ipcc_register.c +++ b/drivers/ipcc/ipcc_register.c @@ -95,7 +95,7 @@ void ipcc_cleanup(FAR struct ipcc_driver_s *priv) nxsem_destroy(&priv->rxsem); nxsem_destroy(&priv->txsem); priv->ipcc->ops.cleanup(priv->ipcc); - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); } @@ -176,7 +176,7 @@ int ipcc_register(FAR struct ipcc_lower_s *ipcc) /* nxsem_init can't really fail us if we provide it with valid params */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->rxsem, 0, 0); nxsem_init(&priv->txsem, 0, 1); @@ -184,6 +184,5 @@ int ipcc_register(FAR struct ipcc_lower_s *ipcc) error: ipcc_cleanup(priv); - return ret; } diff --git a/drivers/ipcc/ipcc_unlink.c b/drivers/ipcc/ipcc_unlink.c index 099263175540e..a2eefa5026045 100644 --- a/drivers/ipcc/ipcc_unlink.c +++ b/drivers/ipcc/ipcc_unlink.c @@ -70,7 +70,7 @@ int ipcc_unlink(FAR struct inode *inode) /* Get exclusive access to the IPCC driver state structure */ - if ((ret = nxsem_wait(&priv->exclsem)) < 0) + if ((ret = nxmutex_lock(&priv->lock)) < 0) { return ret; } @@ -91,6 +91,6 @@ int ipcc_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } diff --git a/drivers/ipcc/ipcc_write.c b/drivers/ipcc/ipcc_write.c index c3de8fa80deea..e627fea5aed01 100644 --- a/drivers/ipcc/ipcc_write.c +++ b/drivers/ipcc/ipcc_write.c @@ -133,7 +133,7 @@ ssize_t ipcc_write(FAR struct file *filep, FAR const char *buffer, /* Get exclusive access to driver */ - if ((ret = nxsem_wait(&priv->exclsem))) + if ((ret = nxmutex_lock(&priv->lock))) { /* nxsem_wait() will return on signal, we did not start * any transfer yet, so we can safely return with error @@ -171,7 +171,7 @@ ssize_t ipcc_write(FAR struct file *filep, FAR const char *buffer, * be number of bytes written or negated errno. */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); leave_critical_section(flags); return nwritten; } @@ -194,7 +194,7 @@ ssize_t ipcc_write(FAR struct file *filep, FAR const char *buffer, { /* All outstanding data has been copied to txbuffer, we're done */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); leave_critical_section(flags); return nwritten; } @@ -215,7 +215,7 @@ ssize_t ipcc_write(FAR struct file *filep, FAR const char *buffer, * be number of bytes written or negated errno. */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); leave_critical_section(flags); return nwritten; } @@ -230,7 +230,7 @@ ssize_t ipcc_write(FAR struct file *filep, FAR const char *buffer, * -EAGAIN when we did not write anything */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); leave_critical_section(flags); return nwritten ? nwritten : -EAGAIN; } @@ -239,7 +239,7 @@ ssize_t ipcc_write(FAR struct file *filep, FAR const char *buffer, * to write data */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if ((ret = nxsem_wait(&priv->txsem))) { @@ -261,7 +261,7 @@ ssize_t ipcc_write(FAR struct file *filep, FAR const char *buffer, * waiting for data, so now let's retake it. */ - nxsem_wait(&priv->exclsem); + nxmutex_lock(&priv->lock); } leave_critical_section(flags); diff --git a/drivers/lcd/ft80x.c b/drivers/lcd/ft80x.c index 40978f18f6ca2..de957c284a567 100644 --- a/drivers/lcd/ft80x.c +++ b/drivers/lcd/ft80x.c @@ -144,35 +144,6 @@ static const struct file_operations g_ft80x_fops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: ft80x_forcetake - * - * Description: - * This is a wrapper around but nxsem_wait_uninterruptible(). The wrapper - * continues to wait even if the thread is canceled. This must be done in - * certain conditions where were must continue in order to clean-up - * resources. - * - ****************************************************************************/ - -static void ft80x_forcetake(FAR sem_t *sem) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: ft80x_fade * @@ -314,7 +285,7 @@ static void ft80x_interrupt_work(FAR void *arg) /* Get exclusive access to the device structures */ - ft80x_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Get the set of pending interrupts. Note that simply reading this * register is sufficient to clear all pending interrupts. @@ -401,7 +372,7 @@ static void ft80x_interrupt_work(FAR void *arg) DEBUGASSERT(priv->lower != NULL && priv->lower->enable != NULL); priv->lower->enable(priv->lower, true); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -457,7 +428,7 @@ static void ft80x_destroy(FAR struct ft80x_dev_s *priv) /* Then free our container */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); } #endif @@ -487,7 +458,7 @@ static int ft80x_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout; @@ -501,7 +472,7 @@ static int ft80x_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Save the new open count */ @@ -509,8 +480,8 @@ static int ft80x_open(FAR struct file *filep) priv->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); errout: return ret; @@ -543,7 +514,7 @@ static int ft80x_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout; @@ -572,7 +543,7 @@ static int ft80x_close(FAR struct file *filep) } ret = OK; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); errout: return ret; @@ -622,7 +593,7 @@ static ssize_t ft80x_write(FAR struct file *filep, FAR const char *buffer, /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -640,7 +611,7 @@ static ssize_t ft80x_write(FAR struct file *filep, FAR const char *buffer, ft80x_write_memory(priv, FT80X_RAM_DL + filep->f_pos, buffer, len); filep->f_pos += len; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return len; } @@ -667,7 +638,7 @@ static int ft80x_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1168,7 +1139,7 @@ static int ft80x_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1563,16 +1534,16 @@ int ft80x_register(FAR struct i2c_master_s *i2c, priv->i2c = i2c; #endif - /* Initialize the mutual exclusion semaphore */ + /* Initialize the mutual exclusion mutex */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Initialize the FT80x */ ret = ft80x_initialize(priv); if (ret < 0) { - goto errout_with_sem; + goto errout_with_lock; } /* Attach our interrupt handler */ @@ -1581,7 +1552,7 @@ int ft80x_register(FAR struct i2c_master_s *i2c, ret = lower->attach(lower, ft80x_interrupt, priv); if (ret < 0) { - goto errout_with_sem; + goto errout_with_lock; } /* Disable all interrupt sources, but enable interrupts both in the lower @@ -1607,8 +1578,8 @@ int ft80x_register(FAR struct i2c_master_s *i2c, ft80x_write_word(priv, FT80X_REG_INT_EN, FT80X_INT_DISABLE); lower->attach(lower, NULL, NULL); -errout_with_sem: - nxsem_destroy(&priv->exclsem); +errout_with_lock: + nxmutex_destroy(&priv->lock); return ret; } diff --git a/drivers/lcd/ft80x.h b/drivers/lcd/ft80x.h index 5c5ff979740cb..6b611851d90fc 100644 --- a/drivers/lcd/ft80x.h +++ b/drivers/lcd/ft80x.h @@ -42,6 +42,7 @@ #include #include #include +#include /**************************************************************************** * Public Types @@ -177,7 +178,7 @@ struct ft80x_dev_s struct work_s intwork; /* Support back end interrupt processing */ uint32_t frequency; /* Effective frequency */ - sem_t exclsem; /* Mutual exclusion semaphore */ + mutex_t lock; /* Mutual exclusion mutex */ #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS uint8_t crefs; /* Number of open references */ bool unlinked; /* True if the driver has been unlinked */ diff --git a/drivers/lcd/ht16k33_14seg.c b/drivers/lcd/ht16k33_14seg.c index 1a562214a4aab..24ad7bcbc7845 100644 --- a/drivers/lcd/ht16k33_14seg.c +++ b/drivers/lcd/ht16k33_14seg.c @@ -60,6 +60,7 @@ #include #include +#include #include #include #include @@ -107,7 +108,7 @@ struct ht16k33_dev_s uint8_t col; /* Current col position to write on display */ uint8_t buffer[HT16K33_MAX_COL]; bool pendscroll; - sem_t sem_excl; + mutex_t lock; }; struct lcd_instream_s @@ -804,7 +805,7 @@ static ssize_t ht16k33_write(FAR struct file *filep, FAR const char *buffer, uint8_t ch; uint8_t count; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); /* Initialize the stream for use with the SLCD CODEC */ @@ -909,7 +910,7 @@ static ssize_t ht16k33_write(FAR struct file *filep, FAR const char *buffer, lcd_curpos_to_fpos(priv, priv->row, priv->col, &filep->f_pos); - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return buflen; } @@ -933,7 +934,7 @@ static off_t ht16k33_seek(FAR struct file *filep, off_t offset, int whence) off_t maxpos; off_t pos; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); maxpos = HT16K33_MAX_ROW * HT16K33_MAX_COL + (HT16K33_MAX_ROW - 1); pos = filep->f_pos; @@ -990,7 +991,7 @@ static off_t ht16k33_seek(FAR struct file *filep, off_t offset, int whence) break; } - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return pos; } @@ -1046,9 +1047,9 @@ static int ht16k33_ioctl(FAR struct file *filep, int cmd, FAR struct ht16k33_dev_s *priv = (FAR struct ht16k33_dev_s *)inode->i_private; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); *(FAR int *)((uintptr_t)arg) = 1; /* Hardcoded */ - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); } break; @@ -1058,11 +1059,9 @@ static int ht16k33_ioctl(FAR struct file *filep, int cmd, FAR struct ht16k33_dev_s *priv = (FAR struct ht16k33_dev_s *)inode->i_private; - nxsem_wait(&priv->sem_excl); - + nxmutex_lock(&priv->lock); ht16k33_setcontrast(priv, 0, (uint8_t)arg); - - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); } break; @@ -1119,7 +1118,7 @@ int ht16k33_register(int devno, FAR struct i2c_master_s *i2c) priv->row = 0; priv->pendscroll = false; - nxsem_init(&priv->sem_excl, 0, 1); + nxmutex_init(&priv->lock); /* Initialize the display */ diff --git a/drivers/lcd/pcf8574_lcd_backpack.c b/drivers/lcd/pcf8574_lcd_backpack.c index 346195ce7c627..358995111c07e 100644 --- a/drivers/lcd/pcf8574_lcd_backpack.c +++ b/drivers/lcd/pcf8574_lcd_backpack.c @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -79,7 +80,7 @@ struct pcf8574_lcd_dev_s uint8_t refs; /* Number of references */ uint8_t unlinked; /* We are unlinked, so teardown * on last close */ - sem_t sem_excl; /* mutex */ + mutex_t lock; /* mutex */ }; struct lcd_instream_s @@ -1060,7 +1061,7 @@ static int pcf8574_lcd_open(FAR struct file *filep) /* Increment the reference count */ - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); if (priv->refs == MAX_OPENCNT) { return -EMFILE; @@ -1070,7 +1071,7 @@ static int pcf8574_lcd_open(FAR struct file *filep) priv->refs++; } - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return OK; } @@ -1091,7 +1092,7 @@ static int pcf8574_lcd_close(FAR struct file *filep) /* Decrement the reference count */ - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); if (priv->refs == 0) { @@ -1113,7 +1114,7 @@ static int pcf8574_lcd_close(FAR struct file *filep) ret = OK; } - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return ret; } @@ -1139,7 +1140,7 @@ static ssize_t pcf8574_lcd_read(FAR struct file *filep, FAR char *buffer, uint8_t col; bool onlf; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); /* Get current cursor position so we can restore it */ @@ -1197,7 +1198,7 @@ static ssize_t pcf8574_lcd_read(FAR struct file *filep, FAR char *buffer, lcd_putcmd(priv, CMD_SET_DDADDR | addr); /* Restore DDRAM address */ - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return nidx; } @@ -1223,7 +1224,7 @@ static ssize_t pcf8574_lcd_write(FAR struct file *filep, uint8_t ch; uint8_t count; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); /* Initialize the stream for use with the SLCD CODEC */ @@ -1353,7 +1354,7 @@ static ssize_t pcf8574_lcd_write(FAR struct file *filep, lcd_curpos_to_fpos(priv, row, col, &filep->f_pos); - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return buflen; } @@ -1378,7 +1379,7 @@ static off_t pcf8574_lcd_seek(FAR struct file *filep, off_t offset, off_t pos; int maxpos; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); maxpos = priv->cfg.rows * priv->cfg.cols + (priv->cfg.rows - 1); pos = filep->f_pos; @@ -1435,7 +1436,7 @@ static off_t pcf8574_lcd_seek(FAR struct file *filep, off_t offset, break; } - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return pos; } @@ -1486,13 +1487,13 @@ static int pcf8574_lcd_ioctl(FAR struct file *filep, int cmd, uint8_t row; uint8_t col; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); lcd_get_curpos(priv, &row, &col); attr->row = row; attr->column = col; - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); } break; @@ -1515,9 +1516,9 @@ static int pcf8574_lcd_ioctl(FAR struct file *filep, int cmd, FAR struct pcf8574_lcd_dev_s *priv = (FAR struct pcf8574_lcd_dev_s *)inode->i_private; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); lcd_backlight(priv, arg ? true : false); - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); } break; @@ -1529,9 +1530,9 @@ static int pcf8574_lcd_ioctl(FAR struct file *filep, int cmd, FAR struct slcd_createchar_s *attr = (FAR struct slcd_createchar_s *)((uintptr_t) arg); - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); lcd_create_char(priv, attr->idx, attr->bmp); - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); } break; @@ -1575,7 +1576,7 @@ static int pcf8574_lcd_unlink(FAR struct inode *inode) (FAR struct pcf8574_lcd_dev_s *)inode->i_private; int ret = OK; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); priv->unlinked = true; @@ -1588,7 +1589,7 @@ static int pcf8574_lcd_unlink(FAR struct inode *inode) ret = OK; } - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -1645,7 +1646,7 @@ int pcf8574_lcd_backpack_register(FAR const char *devpath, priv->bl_bit = priv->cfg.bl_active_high ? 0 : (1 << priv->cfg.bl); priv->refs = 0; priv->unlinked = false; - nxsem_init(&priv->sem_excl, 0, 1); + nxmutex_init(&priv->lock); /* Initialize */ diff --git a/drivers/lcd/st7032.c b/drivers/lcd/st7032.c index 6d547d57a229d..5278952006b30 100644 --- a/drivers/lcd/st7032.c +++ b/drivers/lcd/st7032.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -67,7 +68,7 @@ struct st7032_dev_s uint8_t col; /* Current col position to write on display */ uint8_t buffer[ST7032_MAX_ROW * ST7032_MAX_COL]; bool pendscroll; - sem_t sem_excl; + mutex_t lock; }; struct lcd_instream_s @@ -726,7 +727,7 @@ static ssize_t st7032_write(FAR struct file *filep, FAR const char *buffer, uint8_t ch; uint8_t count; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); /* Initialize the stream for use with the SLCD CODEC */ @@ -838,7 +839,7 @@ static ssize_t st7032_write(FAR struct file *filep, FAR const char *buffer, lcd_curpos_to_fpos(priv, priv->row, priv->col, &filep->f_pos); - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return buflen; } @@ -862,7 +863,7 @@ static off_t st7032_seek(FAR struct file *filep, off_t offset, int whence) off_t maxpos; off_t pos; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); maxpos = ST7032_MAX_ROW * ST7032_MAX_COL + (ST7032_MAX_ROW - 1); pos = filep->f_pos; @@ -919,7 +920,7 @@ static off_t st7032_seek(FAR struct file *filep, off_t offset, int whence) break; } - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); return pos; } @@ -976,9 +977,9 @@ static int st7032_ioctl(FAR struct file *filep, int cmd, FAR struct st7032_dev_s *priv = (FAR struct st7032_dev_s *)inode->i_private; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); *(FAR int *)((uintptr_t)arg) = 1; /* Hardcoded */ - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); } break; @@ -988,11 +989,11 @@ static int st7032_ioctl(FAR struct file *filep, int cmd, FAR struct st7032_dev_s *priv = (FAR struct st7032_dev_s *)inode->i_private; - nxsem_wait(&priv->sem_excl); + nxmutex_lock(&priv->lock); /* TODO: set display contrast */ - nxsem_post(&priv->sem_excl); + nxmutex_unlock(&priv->lock); } break; @@ -1047,7 +1048,7 @@ int st7032_register(FAR const char *devpath, FAR struct i2c_master_s *i2c) priv->row = 0; priv->pendscroll = false; - nxsem_init(&priv->sem_excl, 0, 1); + nxmutex_init(&priv->lock); /* Initialize the display */ diff --git a/drivers/lcd/tda19988.c b/drivers/lcd/tda19988.c index 462305f373ca0..f07c662a09cf8 100644 --- a/drivers/lcd/tda19988.c +++ b/drivers/lcd/tda19988.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include #include @@ -69,7 +69,7 @@ struct tda1988_dev_s /* Upper half driver state */ - sem_t exclsem; /* Assures exclusive access to the driver */ + mutex_t lock; /* Assures exclusive access to the driver */ uint8_t page; /* Currently selected page */ uint8_t crefs; /* Number of open references */ #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS @@ -829,7 +829,7 @@ static int tda19988_open(FAR struct file *filep) /* Get exclusive access to the driver instance */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -840,7 +840,7 @@ static int tda19988_open(FAR struct file *filep) DEBUGASSERT(priv->crefs != UINT8_MAX); priv->crefs++; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -872,7 +872,7 @@ static int tda19988_close(FAR struct file *filep) /* Get exclusive access to the driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -895,7 +895,7 @@ static int tda19988_close(FAR struct file *filep) } #endif - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -ENOSYS; } @@ -929,7 +929,7 @@ static ssize_t tda19988_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to the driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -944,7 +944,7 @@ static ssize_t tda19988_read(FAR struct file *filep, FAR char *buffer, filep->f_pos += nread; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nread; } @@ -999,7 +999,7 @@ static off_t tda19988_seek(FAR struct file *filep, off_t offset, int whence) /* Get exclusive access to the driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1061,7 +1061,7 @@ static off_t tda19988_seek(FAR struct file *filep, off_t offset, int whence) break; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return pos; } @@ -1093,7 +1093,7 @@ static int tda19988_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1139,7 +1139,7 @@ static int tda19988_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1172,7 +1172,7 @@ static int tda19988_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Get exclusive access to the driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1183,7 +1183,7 @@ static int tda19988_poll(FAR struct file *filep, FAR struct pollfd *fds, poll_notify(&fds, 1, POLLIN | POLLOUT); } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -1212,7 +1212,7 @@ static int tda19988_unlink(FAR struct inode *inode) /* Get exclusive access to the driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1231,7 +1231,7 @@ static int tda19988_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -1599,7 +1599,7 @@ static void tda19988_shutdown(FAR struct tda1988_dev_s *priv) /* Release resources */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); /* Free memory */ @@ -1668,7 +1668,7 @@ TDA19988_HANDLE tda19988_register(FAR const char *devpath, priv->lower = lower; priv->page = HDMI_NO_PAGE; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Initialize the TDA19988 */ @@ -1726,7 +1726,7 @@ int tda19988_videomode(TDA19988_HANDLE handle, /* Get exclusive access to the driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1740,7 +1740,7 @@ int tda19988_videomode(TDA19988_HANDLE handle, lcderr("ERROR: tda19988_videomode_internal failed: %d\n", ret); } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1778,7 +1778,7 @@ ssize_t tda19988_read_edid(TDA19988_HANDLE handle, off_t offset, /* Get exclusive access to the driver */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1793,6 +1793,6 @@ ssize_t tda19988_read_edid(TDA19988_HANDLE handle, off_t offset, (int)nread); } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return nread; } diff --git a/drivers/leds/rgbled.c b/drivers/leds/rgbled.c index fde6bb3379aba..2bf8b58379dc2 100644 --- a/drivers/leds/rgbled.c +++ b/drivers/leds/rgbled.c @@ -41,7 +41,7 @@ #include #include #include -#include +#include #include @@ -57,7 +57,7 @@ struct rgbled_upperhalf_s { uint8_t crefs; /* The number of times the device has been opened */ volatile bool started; /* True: pulsed output is being generated */ - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ FAR struct pwm_lowerhalf_s *devledr; FAR struct pwm_lowerhalf_s *devledg; FAR struct pwm_lowerhalf_s *devledb; @@ -120,7 +120,7 @@ static int rgbled_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { lcderr("ERROR: nxsem_wait failed: %d\n", ret); @@ -138,7 +138,7 @@ static int rgbled_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Save the new open count on success */ @@ -146,8 +146,8 @@ static int rgbled_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&upper->lock); errout: return ret; @@ -171,7 +171,7 @@ static int rgbled_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { lcderr("ERROR: nxsem_wait failed: %d\n", ret); @@ -187,7 +187,7 @@ static int rgbled_close(FAR struct file *filep) upper->crefs--; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); ret = OK; errout: @@ -509,7 +509,7 @@ int rgbled_register(FAR const char *path, FAR struct pwm_lowerhalf_s *ledr, * kmm_zalloc()) */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); upper->devledr = ledr; upper->devledg = ledg; upper->devledb = ledb; diff --git a/drivers/leds/userled_upper.c b/drivers/leds/userled_upper.c index 499f3ea6382bd..4dface6f612bf 100644 --- a/drivers/leds/userled_upper.c +++ b/drivers/leds/userled_upper.c @@ -40,6 +40,7 @@ #include #include +#include #include #include @@ -57,7 +58,7 @@ struct userled_upperhalf_s userled_set_t lu_supported; /* The set of supported LEDs */ userled_set_t lu_ledset; /* Current state of LEDs */ - sem_t lu_exclsem; /* Supports exclusive access to the device */ + mutex_t lu_lock; /* Supports exclusive access to the device */ /* The following is a singly linked list of open references to the * LED device. @@ -83,11 +84,6 @@ struct userled_open_s * Private Function Prototypes ****************************************************************************/ -/* Semaphore helpers */ - -static inline int userled_takesem(sem_t *sem); -#define userled_givesem(s) nxsem_post(s); - /* Character driver methods */ static int userled_open(FAR struct file *filep); @@ -119,15 +115,6 @@ static const struct file_operations userled_fops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: userled_takesem - ****************************************************************************/ - -static inline int userled_takesem(sem_t *sem) -{ - return nxsem_wait(sem); -} - /**************************************************************************** * Name: userled_open ****************************************************************************/ @@ -146,10 +133,10 @@ static int userled_open(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = userled_takesem(&priv->lu_exclsem); + ret = nxmutex_lock(&priv->lu_lock); if (ret < 0) { - lederr("ERROR: userled_takesem failed: %d\n", ret); + lederr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -161,7 +148,7 @@ static int userled_open(FAR struct file *filep) { lederr("ERROR: Failed to allocate open structure\n"); ret = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } /* Attach the open structure to the device */ @@ -174,8 +161,8 @@ static int userled_open(FAR struct file *filep) filep->f_priv = (FAR void *)opriv; ret = OK; -errout_with_sem: - userled_givesem(&priv->lu_exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lu_lock); return ret; } @@ -224,10 +211,10 @@ static int userled_close(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = userled_takesem(&priv->lu_exclsem); + ret = nxmutex_lock(&priv->lu_lock); if (ret < 0) { - lederr("ERROR: userled_takesem failed: %d\n", ret); + lederr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -242,7 +229,7 @@ static int userled_close(FAR struct file *filep) { lederr("ERROR: Failed to find open entry\n"); ret = -ENOENT; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Remove the structure from the device */ @@ -261,8 +248,8 @@ static int userled_close(FAR struct file *filep) kmm_free(opriv); ret = OK; -errout_with_exclsem: - userled_givesem(&priv->lu_exclsem); +errout_with_excllock: + nxmutex_unlock(&priv->lu_lock); return ret; } @@ -306,10 +293,10 @@ static ssize_t userled_write(FAR struct file *filep, FAR const char *buffer, /* Get exclusive access to the driver structure */ - ret = userled_takesem(&priv->lu_exclsem); + ret = nxmutex_lock(&priv->lu_lock); if (ret < 0) { - lederr("ERROR: userled_takesem failed: %d\n", ret); + lederr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -319,7 +306,7 @@ static ssize_t userled_write(FAR struct file *filep, FAR const char *buffer, DEBUGASSERT(lower && lower->ll_setall); lower->ll_setall(lower, ledset); - userled_givesem(&priv->lu_exclsem); + nxmutex_unlock(&priv->lu_lock); return (ssize_t)sizeof(userled_set_t); } @@ -342,10 +329,10 @@ static int userled_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver structure */ - ret = userled_takesem(&priv->lu_exclsem); + ret = nxmutex_lock(&priv->lu_lock); if (ret < 0) { - lederr("ERROR: userled_takesem failed: %d\n", ret); + lederr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -487,7 +474,7 @@ static int userled_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - userled_givesem(&priv->lu_exclsem); + nxmutex_unlock(&priv->lu_lock); return ret; } @@ -537,7 +524,7 @@ int userled_register(FAR const char *devname, /* Initialize the new LED driver instance */ priv->lu_lower = lower; - nxsem_init(&priv->lu_exclsem, 0, 1); + nxmutex_init(&priv->lu_lock); DEBUGASSERT(lower && lower->ll_supported); priv->lu_supported = lower->ll_supported(lower); @@ -558,7 +545,7 @@ int userled_register(FAR const char *devname, return OK; errout_with_priv: - nxsem_destroy(&priv->lu_exclsem); + nxmutex_destroy(&priv->lu_lock); kmm_free(priv); return ret; } diff --git a/drivers/leds/ws2812.c b/drivers/leds/ws2812.c index f47fd2e61b504..a73aad728a64f 100644 --- a/drivers/leds/ws2812.c +++ b/drivers/leds/ws2812.c @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -123,7 +124,7 @@ struct ws2812_dev_s FAR struct spi_dev_s *spi; /* SPI interface */ uint16_t nleds; /* Number of addressable LEDs */ uint8_t *tx_buf; /* Buffer for write transaction and state */ - sem_t exclsem; /* Assures exclusive access to the driver */ + mutex_t lock; /* Assures exclusive access to the driver */ }; #endif /* CONFIG_WS2812_NON_SPI_DRIVER */ @@ -488,7 +489,7 @@ static ssize_t ws2812_write(FAR struct file *filep, FAR const char *buffer, return -EINVAL; } - nxsem_wait(&priv->exclsem); + nxmutex_lock(&priv->lock); start_led = filep->f_pos / WS2812_RW_PIXEL_SIZE; tx_pixel = priv->tx_buf + WS2812_RST_CYCLES + \ @@ -520,8 +521,7 @@ static ssize_t ws2812_write(FAR struct file *filep, FAR const char *buffer, filep->f_pos -= WS2812_RW_PIXEL_SIZE; } - nxsem_post(&priv->exclsem); - + nxmutex_unlock(&priv->lock); return written; } @@ -550,7 +550,7 @@ static off_t ws2812_seek(FAR struct file *filep, off_t offset, int whence) return (off_t)-EINVAL; } - nxsem_wait(&priv->exclsem); + nxmutex_lock(&priv->lock); maxpos = (priv->nleds - 1) * WS2812_RW_PIXEL_SIZE; pos = filep->f_pos; @@ -574,8 +574,7 @@ static off_t ws2812_seek(FAR struct file *filep, off_t offset, int whence) /* Return EINVAL if the whence argument is invalid */ - nxsem_post(&priv->exclsem); - + nxmutex_unlock(&priv->lock); return (off_t)-EINVAL; } @@ -590,8 +589,7 @@ static off_t ws2812_seek(FAR struct file *filep, off_t offset, int whence) filep->f_pos = pos; - nxsem_post(&priv->exclsem); - + nxmutex_unlock(&priv->lock); return pos; } @@ -688,7 +686,7 @@ int ws2812_leds_register(FAR const char *devpath, FAR struct spi_dev_s *spi, priv->spi = spi; ws2812_configspi(priv->spi); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); SPI_SNDBLOCK(priv->spi, priv->tx_buf, TXBUFF_SIZE(priv->nleds)); diff --git a/drivers/loop/losetup.c b/drivers/loop/losetup.c index ca5c4c5f02af8..cdc7032915467 100644 --- a/drivers/loop/losetup.c +++ b/drivers/loop/losetup.c @@ -44,13 +44,12 @@ #include #include #include -#include +#include /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define loop_semgive(d) nxsem_post(&(d)->sem) /* To match loop_semtake */ #define MAX_OPENCNT (255) /* Limit of uint8_t */ /**************************************************************************** @@ -59,7 +58,7 @@ struct loop_struct_s { - sem_t sem; /* For safe read-modify-write operations */ + mutex_t lock; /* For safe read-modify-write operations */ uint32_t nsectors; /* Number of sectors on device */ off_t offset; /* Offset (in bytes) to the first sector */ uint16_t sectsize; /* The size of one sector */ @@ -72,7 +71,6 @@ struct loop_struct_s * Private Function Prototypes ****************************************************************************/ -static int loop_semtake(FAR struct loop_struct_s *dev); static int loop_open(FAR struct inode *inode); static int loop_close(FAR struct inode *inode); static ssize_t loop_read(FAR struct inode *inode, FAR unsigned char *buffer, @@ -104,15 +102,6 @@ static const struct block_operations g_bops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: loop_semtake - ****************************************************************************/ - -static int loop_semtake(FAR struct loop_struct_s *dev) -{ - return nxsem_wait(&dev->sem); -} - /**************************************************************************** * Name: loop_open * @@ -130,7 +119,7 @@ static int loop_open(FAR struct inode *inode) /* Make sure we have exclusive access to the state structure */ - ret = loop_semtake(dev); + ret = nxmutex_lock(&dev->lock); if (ret == OK) { if (dev->opencnt == MAX_OPENCNT) @@ -144,7 +133,7 @@ static int loop_open(FAR struct inode *inode) dev->opencnt++; } - loop_semgive(dev); + nxmutex_unlock(&dev->lock); } return ret; @@ -167,7 +156,7 @@ static int loop_close(FAR struct inode *inode) /* Make sure we have exclusive access to the state structure */ - ret = loop_semtake(dev); + ret = nxmutex_lock(&dev->lock); if (ret == OK) { if (dev->opencnt == 0) @@ -181,7 +170,7 @@ static int loop_close(FAR struct inode *inode) dev->opencnt--; } - loop_semgive(dev); + nxmutex_unlock(&dev->lock); } return ret; @@ -371,7 +360,7 @@ int losetup(FAR const char *devname, FAR const char *filename, /* Initialize the loop device structure. */ - nxsem_init(&dev->sem, 0, 1); + nxmutex_init(&dev->lock); dev->nsectors = (sb.st_size - offset) / sectsize; dev->sectsize = sectsize; dev->offset = offset; diff --git a/drivers/misc/rwbuffer.c b/drivers/misc/rwbuffer.c index 3b60632711e39..7d57a9a7ddf11 100644 --- a/drivers/misc/rwbuffer.c +++ b/drivers/misc/rwbuffer.c @@ -68,58 +68,26 @@ static ssize_t rwb_read_(FAR struct rwbuffer_s *rwb, off_t startblock, ****************************************************************************/ /**************************************************************************** - * Name: rwb_semtake + * Name: rwb_lock ****************************************************************************/ #if defined(CONFIG_DRVR_WRITEBUFFER) -static int rwb_semtake(FAR sem_t *sem) +static int rwb_lock(FAR mutex_t *lock) { - return nxsem_wait_uninterruptible(sem); + return nxmutex_lock(lock); } #else -# define rwb_semtake(s) OK +# define rwb_lock(s) OK #endif /**************************************************************************** - * Name: rwb_forcetake + * Name: rwb_unlock ****************************************************************************/ #if defined(CONFIG_DRVR_WRITEBUFFER) -static int rwb_forcetake(FAR sem_t *sem) -{ - int result; - int ret = OK; - - do - { - result = rwb_semtake(sem); - - /* The only expected failure is if the thread is canceled */ - - DEBUGASSERT(result == OK || result == -ECANCELED); - if (ret == OK && result < 0) - { - /* Remember the first error */ - - ret = result; - } - } - while (result < 0); - - return ret; -} -#else -# define rwb_forcetake(s) OK -#endif - -/**************************************************************************** - * Name: rwb_semgive - ****************************************************************************/ - -#if defined(CONFIG_DRVR_WRITEBUFFER) -# define rwb_semgive(s) nxsem_post(s) +# define rwb_unlock(l) nxmutex_unlock(l) #else -# define rwb_semgive(s) +# define rwb_unlock(l) #endif /**************************************************************************** @@ -152,7 +120,7 @@ static inline bool rwb_overlap(off_t blockstart1, size_t nblocks1, #ifdef CONFIG_DRVR_WRITEBUFFER static inline void rwb_resetwrbuffer(FAR struct rwbuffer_s *rwb) { - /* We assume that the caller holds the wrsem */ + /* We assume that the caller holds the wrlock */ rwb->wrnblocks = 0; rwb->wrblockstart = -1; @@ -163,7 +131,7 @@ static inline void rwb_resetwrbuffer(FAR struct rwbuffer_s *rwb) * Name: rwb_wrflush * * Assumptions: - * The caller holds the wrsem semaphore. + * The caller holds the wrlock mutex. * ****************************************************************************/ @@ -224,9 +192,9 @@ static void rwb_wrtimeout(FAR void *arg) * worker thread. */ - rwb_forcetake(&rwb->wrsem); + rwb_lock(&rwb->wrlock); rwb_wrflush(rwb); - rwb_semgive(&rwb->wrsem); + rwb_unlock(&rwb->wrlock); } #endif @@ -552,7 +520,7 @@ int rwb_invalidate_writebuffer(FAR struct rwbuffer_s *rwb, finfo("startblock=%" PRIdOFF " blockcount=%zu\n", startblock, blockcount); - ret = rwb_semtake(&rwb->wrsem); + ret = rwb_lock(&rwb->wrlock); if (ret < 0) { return ret; @@ -665,7 +633,7 @@ int rwb_invalidate_writebuffer(FAR struct rwbuffer_s *rwb, ret = OK; } - rwb_semgive(&rwb->wrsem); + rwb_unlock(&rwb->wrlock); } return ret; @@ -694,7 +662,7 @@ int rwb_invalidate_readahead(FAR struct rwbuffer_s *rwb, finfo("startblock=%" PRIdOFF " blockcount=%zu\n", startblock, blockcount); - ret = rwb_semtake(&rwb->rhsem); + ret = rwb_lock(&rwb->rhlock); if (ret < 0) { return ret; @@ -784,7 +752,7 @@ int rwb_invalidate_readahead(FAR struct rwbuffer_s *rwb, rwb->rhnblocks = nkeep; } - rwb_semgive(&rwb->rhsem); + rwb_unlock(&rwb->rhlock); } return ret; @@ -834,9 +802,9 @@ int rwb_initialize(FAR struct rwbuffer_s *rwb) DEBUGASSERT(rwb->wralignblocks <= rwb->wrmaxblocks && rwb->wrmaxblocks % rwb->wralignblocks == 0); - /* Initialize the write buffer access semaphore */ + /* Initialize the write buffer access mutex */ - nxsem_init(&rwb->wrsem, 0, 1); + nxmutex_init(&rwb->wrlock); /* Initialize write buffer parameters */ @@ -861,9 +829,9 @@ int rwb_initialize(FAR struct rwbuffer_s *rwb) { finfo("Initialize the read-ahead buffer\n"); - /* Initialize the read-ahead buffer access semaphore */ + /* Initialize the read-ahead buffer access mutex */ - nxsem_init(&rwb->rhsem, 0, 1); + nxmutex_init(&rwb->rhlock); /* Initialize read-ahead buffer parameters */ @@ -898,7 +866,7 @@ void rwb_uninitialize(FAR struct rwbuffer_s *rwb) { rwb_wrcanceltimeout(rwb); rwb_wrflush(rwb); - nxsem_destroy(&rwb->wrsem); + nxmutex_destroy(&rwb->wrlock); if (rwb->wrbuffer) { kmm_free(rwb->wrbuffer); @@ -909,7 +877,7 @@ void rwb_uninitialize(FAR struct rwbuffer_s *rwb) #ifdef CONFIG_DRVR_READAHEAD if (rwb->rhmaxblocks > 0) { - nxsem_destroy(&rwb->rhsem); + nxmutex_destroy(&rwb->rhlock); if (rwb->rhbuffer) { kmm_free(rwb->rhbuffer); @@ -932,7 +900,7 @@ static ssize_t rwb_read_(FAR struct rwbuffer_s *rwb, off_t startblock, { size_t remaining; - ret = nxsem_wait(&rwb->rhsem); + ret = nxmutex_lock(&rwb->rhlock); if (ret < 0) { return ret; @@ -979,7 +947,7 @@ static ssize_t rwb_read_(FAR struct rwbuffer_s *rwb, off_t startblock, ferr("ERROR: Failed to fill the read-ahead buffer: %d\n", ret); - rwb_semgive(&rwb->rhsem); + rwb_unlock(&rwb->rhlock); return ret; } } @@ -990,7 +958,7 @@ static ssize_t rwb_read_(FAR struct rwbuffer_s *rwb, off_t startblock, * driver read method */ - rwb_semgive(&rwb->rhsem); + rwb_unlock(&rwb->rhlock); ret = nblocks; } else @@ -1026,7 +994,7 @@ ssize_t rwb_read(FAR struct rwbuffer_s *rwb, off_t startblock, if (rwb->wrmaxblocks > 0) { - ret = nxsem_wait(&rwb->wrsem); + ret = nxmutex_lock(&rwb->wrlock); if (ret < 0) { return ret; @@ -1046,7 +1014,7 @@ ssize_t rwb_read(FAR struct rwbuffer_s *rwb, off_t startblock, ret = rwb_read_(rwb, startblock, rdblocks, rdbuffer); if (ret < 0) { - rwb_semgive(&rwb->wrsem); + rwb_unlock(&rwb->wrlock); return ret; } @@ -1072,7 +1040,7 @@ ssize_t rwb_read(FAR struct rwbuffer_s *rwb, off_t startblock, readblocks += rdblocks; } - rwb_semgive(&rwb->wrsem); + rwb_unlock(&rwb->wrlock); } #endif @@ -1103,7 +1071,7 @@ ssize_t rwb_write(FAR struct rwbuffer_s *rwb, off_t startblock, * streaming applications. */ - ret = nxsem_wait(&rwb->rhsem); + ret = nxmutex_lock(&rwb->rhlock); if (ret < 0) { return ret; @@ -1119,7 +1087,7 @@ ssize_t rwb_write(FAR struct rwbuffer_s *rwb, off_t startblock, if (ret < 0) { ferr("ERROR: rwb_invalidate_readahead failed: %d\n", ret); - rwb_semgive(&rwb->rhsem); + rwb_unlock(&rwb->rhlock); return ret; } #else @@ -1127,7 +1095,7 @@ ssize_t rwb_write(FAR struct rwbuffer_s *rwb, off_t startblock, #endif } - rwb_semgive(&rwb->rhsem); + rwb_unlock(&rwb->rhlock); } #endif @@ -1136,14 +1104,14 @@ ssize_t rwb_write(FAR struct rwbuffer_s *rwb, off_t startblock, { finfo("startblock=%" PRIdOFF " wrbuffer=%p\n", startblock, wrbuffer); - ret = nxsem_wait(&rwb->wrsem); + ret = nxmutex_lock(&rwb->wrlock); if (ret < 0) { return ret; } ret = rwb_writebuffer(rwb, startblock, nblocks, wrbuffer); - rwb_semgive(&rwb->wrsem); + rwb_unlock(&rwb->wrlock); /* On success, return the number of blocks that we were requested to * write. This is for compatibility with the normal return of a block @@ -1206,28 +1174,28 @@ int rwb_mediaremoved(FAR struct rwbuffer_s *rwb) #ifdef CONFIG_DRVR_WRITEBUFFER if (rwb->wrmaxblocks > 0) { - ret = rwb_semtake(&rwb->wrsem); + ret = rwb_lock(&rwb->wrlock); if (ret < 0) { return ret; } rwb_resetwrbuffer(rwb); - rwb_semgive(&rwb->wrsem); + rwb_unlock(&rwb->wrlock); } #endif #ifdef CONFIG_DRVR_READAHEAD if (rwb->rhmaxblocks > 0) { - ret = rwb_semtake(&rwb->rhsem); + ret = rwb_lock(&rwb->rhlock); if (ret < 0) { return ret; } rwb_resetrhbuffer(rwb); - rwb_semgive(&rwb->rhsem); + rwb_unlock(&rwb->rhlock); } #endif @@ -1284,10 +1252,10 @@ int rwb_flush(FAR struct rwbuffer_s *rwb) { int ret; - ret = rwb_forcetake(&rwb->wrsem); + ret = rwb_lock(&rwb->wrlock); rwb_wrcanceltimeout(rwb); rwb_wrflush(rwb); - rwb_semgive(&rwb->wrsem); + rwb_unlock(&rwb->wrlock); return ret; } diff --git a/drivers/mmcsd/mmcsd_sdio.c b/drivers/mmcsd/mmcsd_sdio.c index 3004bd962c0b6..48c5ab8d34d6e 100644 --- a/drivers/mmcsd/mmcsd_sdio.c +++ b/drivers/mmcsd/mmcsd_sdio.c @@ -50,7 +50,7 @@ #include #include #include -#include +#include #include "mmcsd.h" #include "mmcsd_sdio.h" @@ -105,7 +105,7 @@ struct mmcsd_state_s { FAR struct sdio_dev_s *dev; /* The SDIO device bound to this instance */ uint8_t crefs; /* Open references on the driver */ - sem_t sem; /* Assures mutually exclusive access to the slot */ + mutex_t lock; /* Assures mutually exclusive access to the slot */ /* Status flags */ @@ -140,8 +140,8 @@ struct mmcsd_state_s /* Misc Helpers *************************************************************/ -static int mmcsd_takesem(FAR struct mmcsd_state_s *priv); -static void mmcsd_givesem(FAR struct mmcsd_state_s *priv); +static int mmcsd_lock(FAR struct mmcsd_state_s *priv); +static void mmcsd_unlock(FAR struct mmcsd_state_s *priv); /* Command/response helpers *************************************************/ @@ -244,17 +244,17 @@ static const struct block_operations g_bops = * Misc Helpers ****************************************************************************/ -static int mmcsd_takesem(FAR struct mmcsd_state_s *priv) +static int mmcsd_lock(FAR struct mmcsd_state_s *priv) { int ret; - /* Take the semaphore, giving exclusive access to the driver (perhaps + /* Take the lock, giving exclusive access to the driver (perhaps * waiting) */ if (!up_interrupt_context() && !sched_idletask()) { - ret = nxsem_wait_uninterruptible(&priv->sem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -276,11 +276,11 @@ static int mmcsd_takesem(FAR struct mmcsd_state_s *priv) return ret; } -static void mmcsd_givesem(FAR struct mmcsd_state_s *priv) +static void mmcsd_unlock(FAR struct mmcsd_state_s *priv) { if (!up_interrupt_context() && !sched_idletask()) { - /* Release the SDIO bus lock, then the MMC/SD driver semaphore in the + /* Release the SDIO bus lock, then the MMC/SD driver mutex in the * opposite order that they were taken to assure that no deadlock * conditions will arise. */ @@ -288,7 +288,7 @@ static void mmcsd_givesem(FAR struct mmcsd_state_s *priv) #ifdef CONFIG_SDIO_MUXBUS SDIO_LOCK(priv->dev, FALSE); #endif - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); } } @@ -2028,14 +2028,14 @@ static int mmcsd_open(FAR struct inode *inode) DEBUGASSERT(priv->crefs < MAX_CREFS); - ret = mmcsd_takesem(priv); + ret = mmcsd_lock(priv); if (ret < 0) { return ret; } priv->crefs++; - mmcsd_givesem(priv); + mmcsd_unlock(priv); return OK; } @@ -2058,14 +2058,14 @@ static int mmcsd_close(FAR struct inode *inode) /* Decrement the reference count on the block driver */ DEBUGASSERT(priv->crefs > 0); - ret = mmcsd_takesem(priv); + ret = mmcsd_lock(priv); if (ret < 0) { return ret; } priv->crefs--; - mmcsd_givesem(priv); + mmcsd_unlock(priv); return OK; } @@ -2094,7 +2094,7 @@ static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer, if (nsectors > 0) { - ret = mmcsd_takesem(priv); + ret = mmcsd_lock(priv); if (ret < 0) { return ret; @@ -2138,7 +2138,7 @@ static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer, buffer += nread * priv->blocksize; } - mmcsd_givesem(priv); + mmcsd_unlock(priv); } /* On success, return the number of blocks read */ @@ -2172,7 +2172,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, if (nsectors > 0) { - ret = mmcsd_takesem(priv); + ret = mmcsd_lock(priv); if (ret < 0) { return ret; @@ -2216,7 +2216,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, buffer += nwrite * priv->blocksize; } - mmcsd_givesem(priv); + mmcsd_unlock(priv); } /* On success, return the number of blocks written */ @@ -2244,7 +2244,7 @@ static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry) /* Is there a (supported) card inserted in the slot? */ priv = (FAR struct mmcsd_state_s *)inode->i_private; - ret = mmcsd_takesem(priv); + ret = mmcsd_lock(priv); if (ret < 0) { return ret; @@ -2278,7 +2278,7 @@ static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry) ret = OK; } - mmcsd_givesem(priv); + mmcsd_unlock(priv); } return ret; @@ -2302,7 +2302,7 @@ static int mmcsd_ioctl(FAR struct inode *inode, int cmd, unsigned long arg) /* Process the IOCTL by command */ - ret = mmcsd_takesem(priv); + ret = mmcsd_lock(priv); if (ret < 0) { return ret; @@ -2347,7 +2347,7 @@ static int mmcsd_ioctl(FAR struct inode *inode, int cmd, unsigned long arg) break; } - mmcsd_givesem(priv); + mmcsd_unlock(priv); return ret; } @@ -2379,7 +2379,7 @@ static void mmcsd_mediachange(FAR void *arg) /* Is there a card present in the slot? */ - ret = mmcsd_takesem(priv); + ret = mmcsd_lock(priv); if (ret < 0) { return; @@ -2410,7 +2410,7 @@ static void mmcsd_mediachange(FAR void *arg) SDIO_CALLBACKENABLE(priv->dev, SDIOMEDIA_INSERTED); } - mmcsd_givesem(priv); + mmcsd_unlock(priv); } /**************************************************************************** @@ -3448,7 +3448,7 @@ static int mmcsd_hwinitialize(FAR struct mmcsd_state_s *priv) { int ret; - ret = mmcsd_takesem(priv); + ret = mmcsd_lock(priv); if (ret < 0) { return ret; @@ -3464,7 +3464,7 @@ static int mmcsd_hwinitialize(FAR struct mmcsd_state_s *priv) if (SDIO_ATTACH(priv->dev)) { ferr("ERROR: Unable to attach MMC/SD interrupts\n"); - mmcsd_givesem(priv); + mmcsd_unlock(priv); return -EBUSY; } @@ -3525,7 +3525,7 @@ static int mmcsd_hwinitialize(FAR struct mmcsd_state_s *priv) * the slot was successfully configured. */ - mmcsd_givesem(priv); + mmcsd_unlock(priv); return ret; } @@ -3591,7 +3591,7 @@ int mmcsd_slotinitialize(int minor, FAR struct sdio_dev_s *dev) /* Initialize the MMC/SD state structure */ memset(priv, 0, sizeof(struct mmcsd_state_s)); - nxsem_init(&priv->sem, 0, 1); + nxmutex_init(&priv->lock); /* Bind the MMCSD driver to the MMCSD state structure */ @@ -3648,7 +3648,7 @@ int mmcsd_slotinitialize(int minor, FAR struct sdio_dev_s *dev) errout_with_hwinit: mmcsd_hwuninitialize(priv); errout_with_alloc: - nxsem_destroy(&priv->sem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return ret; } diff --git a/drivers/mmcsd/mmcsd_spi.c b/drivers/mmcsd/mmcsd_spi.c index 095bd9a6a636f..ab0ff19f6ce2c 100644 --- a/drivers/mmcsd/mmcsd_spi.c +++ b/drivers/mmcsd/mmcsd_spi.c @@ -41,6 +41,7 @@ #include #include +#include #include #include #include @@ -120,7 +121,7 @@ struct mmcsd_slot_s { FAR struct spi_dev_s *spi; /* SPI port bound to this slot */ - sem_t sem; /* Assures mutually exclusive access to card and SPI */ + mutex_t lock; /* Assures mutually exclusive access to card and SPI */ uint8_t state; /* State of the slot (see MMCSD_SLOTSTATUS_* definitions) */ uint8_t type; /* Disk type */ uint8_t csd[16]; /* Copy of card CSD */ @@ -148,8 +149,8 @@ struct mmcsd_cmdinfo_s /* Misc *********************************************************************/ -static int mmcsd_semtake(FAR struct mmcsd_slot_s *slot); -static void mmcsd_semgive(FAR struct mmcsd_slot_s *slot); +static int mmcsd_lock(FAR struct mmcsd_slot_s *slot); +static void mmcsd_unlock(FAR struct mmcsd_slot_s *slot); /* Card SPI interface *******************************************************/ @@ -368,18 +369,18 @@ static const struct mmcsd_cmdinfo_s g_acmd41 = ****************************************************************************/ /**************************************************************************** - * Name: mmcsd_semtake + * Name: mmcsd_lock ****************************************************************************/ -static int mmcsd_semtake(FAR struct mmcsd_slot_s *slot) +static int mmcsd_lock(FAR struct mmcsd_slot_s *slot) { int ret; /* Get exclusive access to the MMC/SD device (possibly unnecessary if - * SPI_LOCK is also implemented as a semaphore). + * SPI_LOCK is also implemented as a mutex). */ - ret = nxsem_wait_uninterruptible(&slot->sem); + ret = nxmutex_lock(&slot->lock); if (ret < 0) { return ret; @@ -402,10 +403,10 @@ static int mmcsd_semtake(FAR struct mmcsd_slot_s *slot) } /**************************************************************************** - * Name: mmcsd_semgive + * Name: mmcsd_unlock ****************************************************************************/ -static void mmcsd_semgive(FAR struct mmcsd_slot_s *slot) +static void mmcsd_unlock(FAR struct mmcsd_slot_s *slot) { /* Relinquish the lock on the SPI bus */ @@ -421,7 +422,7 @@ static void mmcsd_semgive(FAR struct mmcsd_slot_s *slot) /* Relinquish the lock on the MMC/SD device */ - nxsem_post(&slot->sem); + nxmutex_unlock(&slot->lock); } /**************************************************************************** @@ -1098,7 +1099,7 @@ static int mmcsd_open(FAR struct inode *inode) } #endif - ret = mmcsd_semtake(slot); + ret = mmcsd_lock(slot); if (ret < 0) { return ret; @@ -1120,7 +1121,7 @@ static int mmcsd_open(FAR struct inode *inode) if (ret < 0) { finfo("Failed to initialize card\n"); - goto errout_with_sem; + goto errout_with_lock; } } @@ -1131,8 +1132,8 @@ static int mmcsd_open(FAR struct inode *inode) SPI_SELECT(spi, SPIDEV_MMCSD(0), false); } -errout_with_sem: - mmcsd_semgive(slot); +errout_with_lock: + mmcsd_unlock(slot); return ret; } @@ -1231,7 +1232,7 @@ static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer, /* Select the slave */ - ret = mmcsd_semtake(slot); + ret = mmcsd_lock(slot); if (ret < 0) { return (ssize_t)ret; @@ -1302,7 +1303,7 @@ static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer, SPI_SELECT(spi, SPIDEV_MMCSD(0), false); SPI_SEND(spi, 0xff); - mmcsd_semgive(slot); + mmcsd_unlock(slot); finfo("Read %zu bytes:\n", nbytes); mmcsd_dumpbuffer("Read buffer", buffer, nbytes); @@ -1325,7 +1326,7 @@ static ssize_t mmcsd_read(FAR struct inode *inode, unsigned char *buffer, } } - mmcsd_semgive(slot); + mmcsd_unlock(slot); return -EIO; } @@ -1424,7 +1425,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, /* Select the slave */ - ret = mmcsd_semtake(slot); + ret = mmcsd_lock(slot); if (ret < 0) { return (ssize_t)ret; @@ -1443,7 +1444,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, if (response != MMCSD_SPIR1_OK) { ferr("ERROR: CMD24 failed: R1=%02x\n", response); - goto errout_with_sem; + goto errout_with_lock; } /* Then transfer the sector */ @@ -1451,7 +1452,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, if (mmcsd_xmitblock(slot, buffer, SECTORSIZE(slot), 0xfe) != 0) { ferr("ERROR: Block transfer failed\n"); - goto errout_with_sem; + goto errout_with_lock; } } else @@ -1464,14 +1465,14 @@ static ssize_t mmcsd_write(FAR struct inode *inode, if (response != MMCSD_SPIR1_OK) { ferr("ERROR: CMD55 failed: R1=%02x\n", response); - goto errout_with_sem; + goto errout_with_lock; } response = mmcsd_sendcmd(slot, &g_acmd23, nsectors); if (response != MMCSD_SPIR1_OK) { ferr("ERROR: ACMD23 failed: R1=%02x\n", response); - goto errout_with_sem; + goto errout_with_lock; } } @@ -1483,7 +1484,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, if (response != MMCSD_SPIR1_OK) { ferr("ERROR: CMD25 failed: R1=%02x\n", response); - goto errout_with_sem; + goto errout_with_lock; } /* Transmit each block */ @@ -1493,7 +1494,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, if (mmcsd_xmitblock(slot, buffer, SECTORSIZE(slot), 0xfc) != 0) { ferr("ERROR: Failed: to receive the block\n"); - goto errout_with_sem; + goto errout_with_lock; } buffer += SECTORSIZE(slot); @@ -1501,7 +1502,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, if (mmcsd_waitready(slot) != OK) { ferr("ERROR: Failed: card is busy\n"); - goto errout_with_sem; + goto errout_with_lock; } } @@ -1515,13 +1516,13 @@ static ssize_t mmcsd_write(FAR struct inode *inode, mmcsd_waitready(slot); SPI_SELECT(spi, SPIDEV_MMCSD(0), false); SPI_SEND(spi, 0xff); - mmcsd_semgive(slot); + mmcsd_unlock(slot); /* The success return value is the number of sectors written */ return nsectors; -errout_with_sem: +errout_with_lock: SPI_SELECT(spi, SPIDEV_MMCSD(0), false); if (retry_count++ < CONFIG_MMCSD_SPIRETRY_COUNT) { @@ -1538,7 +1539,7 @@ static ssize_t mmcsd_write(FAR struct inode *inode, } } - mmcsd_semgive(slot); + mmcsd_unlock(slot); return -EIO; } #endif @@ -1587,7 +1588,7 @@ static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry) /* Re-sample the CSD */ - ret = mmcsd_semtake(slot); + ret = mmcsd_lock(slot); if (ret < 0) { return ret; @@ -1599,7 +1600,7 @@ static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry) if (ret < 0) { - mmcsd_semgive(slot); + mmcsd_unlock(slot); ferr("ERROR: mmcsd_getcsd returned %d\n", ret); return ret; } @@ -1629,7 +1630,7 @@ static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry) */ slot->state &= ~MMCSD_SLOTSTATUS_MEDIACHGD; - mmcsd_semgive(slot); + mmcsd_unlock(slot); finfo("geo_available: %d\n", geometry->geo_available); finfo("geo_mediachanged: %d\n", geometry->geo_mediachanged); @@ -1647,7 +1648,7 @@ static int mmcsd_geometry(FAR struct inode *inode, struct geometry *geometry) * Detect media and initialize * * Precondition: - * Semaphore has been taken. + * Mutex has been taken. ****************************************************************************/ static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot) @@ -1731,7 +1732,7 @@ static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot) { ferr("ERROR: Send CMD0 failed: R1=%02" PRIx32 "\n", result); SPI_SELECT(spi, SPIDEV_MMCSD(0), false); - mmcsd_semgive(slot); + mmcsd_unlock(slot); return -EIO; } @@ -1862,7 +1863,7 @@ static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot) { ferr("ERROR: Failed to exit IDLE state\n"); SPI_SELECT(spi, SPIDEV_MMCSD(0), false); - mmcsd_semgive(slot); + mmcsd_unlock(slot); return -EIO; } } @@ -1871,7 +1872,7 @@ static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot) { ferr("ERROR: Failed to identify card\n"); SPI_SELECT(spi, SPIDEV_MMCSD(0), false); - mmcsd_semgive(slot); + mmcsd_unlock(slot); return -EIO; } @@ -1883,7 +1884,7 @@ static int mmcsd_mediainitialize(FAR struct mmcsd_slot_s *slot) { ferr("ERROR: mmcsd_getcsd(CMD9) failed: %" PRId32 "\n", result); SPI_SELECT(spi, SPIDEV_MMCSD(0), false); - mmcsd_semgive(slot); + mmcsd_unlock(slot); return -EIO; } @@ -1957,7 +1958,7 @@ static void mmcsd_mediachanged(void *arg) /* Save the current slot state and reassess the new state */ - ret = mmcsd_semtake(slot); + ret = mmcsd_lock(slot); if (ret < 0) { return; @@ -2002,7 +2003,7 @@ static void mmcsd_mediachanged(void *arg) } } - mmcsd_semgive(slot); + mmcsd_unlock(slot); } /**************************************************************************** @@ -2046,7 +2047,7 @@ int mmcsd_spislotinitialize(int minor, int slotno, FAR struct spi_dev_s *spi) slot = &g_mmcsdslot[slotno]; memset(slot, 0, sizeof(struct mmcsd_slot_s)); - nxsem_init(&slot->sem, 0, 1); + nxmutex_init(&slot->lock); #ifdef CONFIG_DEBUG_FEATURES if (slot->spi) @@ -2065,7 +2066,7 @@ int mmcsd_spislotinitialize(int minor, int slotno, FAR struct spi_dev_s *spi) * configured for the MMC/SD card */ - ret = mmcsd_semtake(slot); + ret = mmcsd_lock(slot); if (ret < 0) { return ret; @@ -2074,7 +2075,7 @@ int mmcsd_spislotinitialize(int minor, int slotno, FAR struct spi_dev_s *spi) /* Initialize for the media in the slot (if any) */ ret = mmcsd_mediainitialize(slot); - mmcsd_semgive(slot); + mmcsd_unlock(slot); if (ret == 0) { finfo("mmcsd_mediainitialize returned OK\n"); diff --git a/drivers/modem/altair/altmdm_sys.c b/drivers/modem/altair/altmdm_sys.c index 47c0eb73a61bf..48e469c4a1d5d 100644 --- a/drivers/modem/altair/altmdm_sys.c +++ b/drivers/modem/altair/altmdm_sys.c @@ -63,7 +63,7 @@ int altmdm_sys_initlock(FAR struct altmdm_sys_lock_s *handle) return ERROR; } - ret = nxsem_init(&handle->sem, 0, 1); + ret = nxmutex_init(&handle->lock); #ifdef CONFIG_MODEM_ALTMDM_DEBUG if (ret < 0) @@ -94,7 +94,7 @@ int altmdm_sys_deletelock(FAR struct altmdm_sys_lock_s *handle) return ERROR; } - ret = nxsem_destroy(&handle->sem); + ret = nxmutex_destroy(&handle->lock); #ifdef CONFIG_MODEM_ALTMDM_DEBUG if (ret < 0) @@ -125,7 +125,7 @@ int altmdm_sys_lock(FAR struct altmdm_sys_lock_s *handle) return ERROR; } - ret = nxsem_wait_uninterruptible(&handle->sem); + ret = nxmutex_lock(&handle->lock); if (ret < 0) { m_err("nxsem_wait_uninterruptible() failed:%d\n", ret); @@ -153,7 +153,7 @@ int altmdm_sys_unlock(FAR struct altmdm_sys_lock_s *handle) return ERROR; } - ret = nxsem_post(&handle->sem); + ret = nxmutex_unlock(&handle->lock); #ifdef CONFIG_MODEM_ALTMDM_DEBUG if (ret < 0) diff --git a/drivers/modem/altair/altmdm_sys.h b/drivers/modem/altair/altmdm_sys.h index aea624faa5faa..773ea545bfe1c 100644 --- a/drivers/modem/altair/altmdm_sys.h +++ b/drivers/modem/altair/altmdm_sys.h @@ -28,6 +28,7 @@ ****************************************************************************/ #include +#include #include /**************************************************************************** @@ -44,7 +45,7 @@ struct altmdm_sys_lock_s { - sem_t sem; + mutex_t lock; }; struct altmdm_sys_csem_s diff --git a/drivers/motor/foc/foc_dev.c b/drivers/motor/foc/foc_dev.c index 970314f7a8719..585d63e2468b0 100644 --- a/drivers/motor/foc/foc_dev.c +++ b/drivers/motor/foc/foc_dev.c @@ -120,7 +120,7 @@ static int foc_open(FAR struct file *filep) /* If the port is the middle of closing, wait until the close is finished */ - ret = nxsem_wait(&dev->closesem); + ret = nxmutex_lock(&dev->closelock); if (ret >= 0) { /* Increment the count of references to the device. If this the first @@ -158,7 +158,7 @@ static int foc_open(FAR struct file *filep) } } - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } errout: @@ -179,7 +179,7 @@ static int foc_close(FAR struct file *filep) FAR struct foc_dev_s *dev = inode->i_private; int ret = 0; - ret = nxsem_wait(&dev->closesem); + ret = nxmutex_lock(&dev->closelock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -189,7 +189,7 @@ static int foc_close(FAR struct file *filep) if (dev->ocount > 1) { dev->ocount--; - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } else { @@ -200,7 +200,7 @@ static int foc_close(FAR struct file *filep) /* Shutdown the device */ ret = foc_shutdown(dev); - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } } @@ -837,9 +837,9 @@ int foc_register(FAR const char *path, FAR struct foc_dev_s *dev) goto errout; } - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&dev->closesem, 0, 1); + nxmutex_init(&dev->closelock); nxsem_init(&dev->statesem, 0, 0); nxsem_set_protocol(&dev->statesem, SEM_PRIO_NONE); @@ -848,7 +848,7 @@ int foc_register(FAR const char *path, FAR struct foc_dev_s *dev) ret = register_driver(path, &g_foc_fops, 0666, dev); if (ret < 0) { - nxsem_destroy(&dev->closesem); + nxmutex_destroy(&dev->closelock); nxsem_destroy(&dev->statesem); goto errout; } diff --git a/drivers/motor/motor.c b/drivers/motor/motor.c index d00061bcaec5f..7c9d440667ab4 100644 --- a/drivers/motor/motor.c +++ b/drivers/motor/motor.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include @@ -52,7 +52,7 @@ struct motor_upperhalf_s { FAR struct motor_lowerhalf_s *lower; /* the handle of lower half driver */ uint8_t ocount; /* The number of times the device has been opened */ - sem_t closesem; /* Locks out new opens while close is in progress */ + mutex_t closelock; /* Locks out new opens while close is in progress */ }; /**************************************************************************** @@ -108,7 +108,7 @@ static int motor_open(FAR struct file *filep) /* If the port is the middle of closing, wait until the close is finished */ - ret = nxsem_wait(&upper->closesem); + ret = nxmutex_lock(&upper->closelock); if (ret >= 0) { /* Increment the count of references to the device. If this the first @@ -143,7 +143,7 @@ static int motor_open(FAR struct file *filep) } } - nxsem_post(&upper->closesem); + nxmutex_unlock(&upper->closelock); } return OK; @@ -164,7 +164,7 @@ static int motor_close(FAR struct file *filep) FAR struct motor_lowerhalf_s *lower = upper->lower; int ret; - ret = nxsem_wait(&upper->closesem); + ret = nxmutex_lock(&upper->closelock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -174,7 +174,7 @@ static int motor_close(FAR struct file *filep) if (upper->ocount > 1) { upper->ocount--; - nxsem_post(&upper->closesem); + nxmutex_unlock(&upper->closelock); } else { @@ -185,7 +185,7 @@ static int motor_close(FAR struct file *filep) /* Free the IRQ and disable the motor device */ lower->ops->shutdown(lower); /* Disable the motor */ - nxsem_post(&upper->closesem); + nxmutex_unlock(&upper->closelock); } } @@ -579,9 +579,9 @@ int motor_register(FAR const char *path, return -ENOMEM; } - /* Initialize semaphores */ + /* Initialize mutex */ - nxsem_init(&upper->closesem, 0, 1); + nxmutex_init(&upper->closelock); /* Connect motor driver with lower level interface */ @@ -592,7 +592,7 @@ int motor_register(FAR const char *path, ret = register_driver(path, &motor_fops, 0666, upper); if (ret < 0) { - nxsem_destroy(&upper->closesem); + nxmutex_destroy(&upper->closelock); kmm_free(upper); } diff --git a/drivers/mtd/mtd_config.c b/drivers/mtd/mtd_config.c index f4d02abc51961..f63521f705ec0 100644 --- a/drivers/mtd/mtd_config.c +++ b/drivers/mtd/mtd_config.c @@ -1807,7 +1807,7 @@ int mtdconfig_unregister(void) inode = file.f_inode; dev = (FAR struct mtdconfig_struct_s *)inode->i_private; - nxmutex_destroy(&dev->exclsem); + nxsem_destroy(&dev->exclsem); kmm_free(dev); file_close(&file); diff --git a/drivers/mtd/mtd_nand.c b/drivers/mtd/mtd_nand.c index 673bd9a8a4b1e..ab856e385c663 100644 --- a/drivers/mtd/mtd_nand.c +++ b/drivers/mtd/mtd_nand.c @@ -77,11 +77,6 @@ * Private Function Prototypes ****************************************************************************/ -/* NAND locking */ - -static int nand_lock(FAR struct nand_dev_s *nand); -#define nand_unlock(n) nxsem_post(&(n)->exclsem) - /* Bad block checking */ #ifdef CONFIG_MTD_NAND_BLOCKCHECK @@ -120,26 +115,6 @@ static int nand_ioctl(struct mtd_dev_s *dev, int cmd, * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: nand_lock - * - * Description: - * Get exclusive access to the nand. - * - * Input Parameters: - * nand - Pointer to a struct nand_dev_s instance. - * block - Number of block to check. - * - * Returned Value: - * OK on success; a negated errno value on failure. - * - ****************************************************************************/ - -static int nand_lock(FAR struct nand_dev_s *nand) -{ - return nxsem_wait(&nand->exclsem); -} - /**************************************************************************** * Name: nand_checkblock * @@ -548,7 +523,7 @@ static int nand_erase(struct mtd_dev_s *dev, off_t startblock, /* Lock access to the NAND until we complete the erase */ - nand_lock(nand); + nxmutex_lock(&nand->lock); while (blocksleft-- > 0) { /* Erase each sector */ @@ -558,14 +533,14 @@ static int nand_erase(struct mtd_dev_s *dev, off_t startblock, { ferr("ERROR: nand_eraseblock failed on block %ld: %d\n", (long)startblock, ret); - nand_unlock(nand); + nxmutex_unlock(&nand->lock); return ret; } startblock++; } - nand_unlock(nand); + nxmutex_unlock(&nand->lock); return (int)nblocks; } @@ -614,7 +589,7 @@ static ssize_t nand_bread(struct mtd_dev_s *dev, off_t startpage, /* Lock access to the NAND until we complete the read */ - nand_lock(nand); + nxmutex_lock(&nand->lock); /* Then read every page from NAND */ @@ -657,11 +632,11 @@ static ssize_t nand_bread(struct mtd_dev_s *dev, off_t startpage, buffer += pagesize; } - nand_unlock(nand); + nxmutex_unlock(&nand->lock); return npages; errout_with_lock: - nand_unlock(nand); + nxmutex_unlock(&nand->lock); return ret; } @@ -710,7 +685,7 @@ static ssize_t nand_bwrite(struct mtd_dev_s *dev, off_t startpage, /* Lock access to the NAND until we complete the write */ - nand_lock(nand); + nxmutex_lock(&nand->lock); /* Then write every page into NAND */ @@ -753,11 +728,11 @@ static ssize_t nand_bwrite(struct mtd_dev_s *dev, off_t startpage, buffer += pagesize; } - nand_unlock(nand); + nxmutex_unlock(&nand->lock); return npages; errout_with_lock: - nand_unlock(nand); + nxmutex_unlock(&nand->lock); return ret; } @@ -967,7 +942,7 @@ FAR struct mtd_dev_s *nand_initialize(FAR struct nand_raw_s *raw) nand->mtd.ioctl = nand_ioctl; nand->raw = raw; - nxsem_init(&nand->exclsem, 0, 1); + nxmutex_init(&nand->lock); #if defined(CONFIG_MTD_NAND_BLOCKCHECK) && defined(CONFIG_DEBUG_INFO) && \ defined(CONFIG_DEBUG_FS) diff --git a/drivers/mtd/rpmsgmtd.c b/drivers/mtd/rpmsgmtd.c index 6873ece02be1b..83b44b7396ea7 100644 --- a/drivers/mtd/rpmsgmtd.c +++ b/drivers/mtd/rpmsgmtd.c @@ -53,7 +53,7 @@ struct rpmsgmtd_s * opreation until the connection * between two cpu established. */ - mutex_t geoexcl; /* Get mtd geometry operation mutex */ + mutex_t geolock; /* Get mtd geometry operation mutex */ struct mtd_geometry_s geo; /* MTD geomerty */ }; @@ -195,7 +195,7 @@ static int rpmsgmtd_get_geometry(FAR struct rpmsgmtd_s *dev) { int ret; - ret = nxmutex_lock(&dev->geoexcl); + ret = nxmutex_lock(&dev->geolock); if (ret < 0) { return ret; @@ -209,7 +209,7 @@ static int rpmsgmtd_get_geometry(FAR struct rpmsgmtd_s *dev) (unsigned long)&dev->geo); } - nxmutex_unlock(&dev->geoexcl); + nxmutex_unlock(&dev->geolock); return ret; } @@ -1073,7 +1073,7 @@ int rpmsgmtd_register(FAR const char *remotecpu, FAR const char *remotepath, nxsem_init(&dev->wait, 0, 0); nxsem_set_protocol(&dev->wait, SEM_PRIO_NONE); - nxmutex_init(&dev->geoexcl); + nxmutex_init(&dev->geolock); /* Register the rpmsg callback */ diff --git a/drivers/net/phy_notify.c b/drivers/net/phy_notify.c index 6f503cfdef6fd..f7538a75f518a 100644 --- a/drivers/net/phy_notify.c +++ b/drivers/net/phy_notify.c @@ -43,7 +43,7 @@ #include #include -#include +#include #include #include @@ -102,7 +102,6 @@ struct phy_notify_s * Private Function Prototypes ****************************************************************************/ -static int phy_semtake(void); static FAR struct phy_notify_s *phy_find_unassigned(void); static FAR struct phy_notify_s *phy_find_assigned(FAR const char *intf, pid_t pid); @@ -114,7 +113,7 @@ static int phy_handler(int irq, FAR void *context, FAR void *arg); /* Serializes access to the g_notify_clients array */ -static sem_t g_notify_clients_sem = SEM_INITIALIZER(1); +static mutex_t g_notify_clients_lock = NXMUTEX_INITIALIZER; /* This is a array the hold information for each PHY notification client */ @@ -125,17 +124,6 @@ static struct phy_notify_s * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: phy_semtake - ****************************************************************************/ - -static int phy_semtake(void) -{ - return nxsem_wait_uninterruptible(&g_notify_clients_sem); -} - -#define phy_semgive() nxsem_post(&g_notify_clients_sem); - /**************************************************************************** * Name: phy_find_unassigned ****************************************************************************/ @@ -146,10 +134,10 @@ static FAR struct phy_notify_s *phy_find_unassigned(void) int ret; int i; - ret = phy_semtake(); + ret = nxmutex_lock(&g_notify_clients_lock); if (ret < 0) { - phyerr("ERROR: phy_semtake failed: %d\n", ret); + phyerr("ERROR: nxmutex_lock failed: %d\n", ret); return NULL; } @@ -167,7 +155,7 @@ static FAR struct phy_notify_s *phy_find_unassigned(void) /* Return the client entry assigned to the caller */ - phy_semgive(); + nxmutex_unlock(&g_notify_clients_lock); phyinfo("Returning client %d\n", i); return client; } @@ -176,7 +164,7 @@ static FAR struct phy_notify_s *phy_find_unassigned(void) /* Ooops... too many */ phyerr("ERROR: No free client entries\n"); - phy_semgive(); + nxmutex_unlock(&g_notify_clients_lock); return NULL; } @@ -191,10 +179,10 @@ static FAR struct phy_notify_s *phy_find_assigned(FAR const char *intf, int ret; int i; - ret = phy_semtake(); + ret = nxmutex_lock(&g_notify_clients_lock); if (ret < 0) { - phyerr("ERROR: phy_semtake failed: %d\n", ret); + phyerr("ERROR: nxmutex_lock failed: %d\n", ret); return NULL; } @@ -206,7 +194,7 @@ static FAR struct phy_notify_s *phy_find_assigned(FAR const char *intf, { /* Return the matching client entry to the caller */ - phy_semgive(); + nxmutex_unlock(&g_notify_clients_lock); phyinfo("Returning client %d\n", i); return client; } @@ -214,7 +202,7 @@ static FAR struct phy_notify_s *phy_find_assigned(FAR const char *intf, /* Ooops... not found */ - phy_semgive(); + nxmutex_unlock(&g_notify_clients_lock); return NULL; } @@ -375,7 +363,7 @@ int phy_notify_unsubscribe(FAR const char *intf, pid_t pid) /* Detach and disable the PHY interrupt */ - ret = phy_semtake(); + ret = nxmutex_lock(&g_notify_clients_lock); if (ret >= 0) { arch_phy_irq(intf, NULL, NULL, NULL); @@ -390,7 +378,7 @@ int phy_notify_unsubscribe(FAR const char *intf, pid_t pid) client->intf[0] = '\0'; client->pid = INVALID_PROCESS_ID; - phy_semgive(); + nxmutex_unlock(&g_notify_clients_lock); } return OK; diff --git a/drivers/net/slip.c b/drivers/net/slip.c index 160f1cca014fc..3ef93196e899e 100644 --- a/drivers/net/slip.c +++ b/drivers/net/slip.c @@ -153,9 +153,6 @@ static struct slip_driver_s g_slip[CONFIG_NET_SLIP_NINTERFACES]; * Private Function Prototypes ****************************************************************************/ -static int slip_semtake(FAR struct slip_driver_s *priv); -static void slip_forcetake(FAR struct slip_driver_s *priv); - /* Common TX logic */ static void slip_write(FAR struct slip_driver_s *priv, @@ -185,45 +182,6 @@ static int slip_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac); * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: slip_semtake - ****************************************************************************/ - -static int slip_semtake(FAR struct slip_driver_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->waitsem); -} - -#define slip_semgive(p) nxsem_post(&(p)->waitsem); - -/**************************************************************************** - * Name: slip_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static void slip_forcetake(FAR struct slip_driver_s *priv) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(&priv->waitsem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: slip_write * @@ -449,7 +407,7 @@ static int slip_txtask(int argc, FAR char *argv[]) */ priv = &g_slip[index]; - slip_semgive(priv); + nxsem_post(&priv->waitsem); /* Loop forever */ @@ -457,7 +415,7 @@ static int slip_txtask(int argc, FAR char *argv[]) { /* Wait for the timeout to expire (or until we are signaled by */ - ret = slip_semtake(priv); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { DEBUGASSERT(ret == -ECANCELED); @@ -466,13 +424,13 @@ static int slip_txtask(int argc, FAR char *argv[]) if (!priv->txnodelay) { - slip_semgive(priv); + nxsem_post(&priv->waitsem); nxsig_usleep(SLIP_WDDELAY); } else { priv->txnodelay = false; - slip_semgive(priv); + nxsem_post(&priv->waitsem); } /* Is the interface up? */ @@ -656,7 +614,7 @@ static int slip_rxtask(int argc, FAR char *argv[]) */ priv = &g_slip[index]; - slip_semgive(priv); + nxsem_post(&priv->waitsem); /* Loop forever */ @@ -1008,7 +966,7 @@ int slip_initialize(int intf, FAR const char *devname) /* Wait and make sure that the receive task is started. */ - slip_forcetake(priv); + nxsem_wait_uninterruptible(&priv->waitsem); /* Start the SLIP transmitter kernel thread */ @@ -1025,11 +983,11 @@ int slip_initialize(int intf, FAR const char *devname) /* Wait and make sure that the transmit task is started. */ - slip_forcetake(priv); + nxsem_wait_uninterruptible(&priv->waitsem); /* Bump the semaphore count so that it can now be used as a mutex */ - slip_semgive(priv); + nxsem_post(&priv->waitsem); /* Register the device with the OS so that socket IOCTLs can be performed */ diff --git a/drivers/net/telnet.c b/drivers/net/telnet.c index a8045e958059f..32182bedc2918 100644 --- a/drivers/net/telnet.c +++ b/drivers/net/telnet.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include #include @@ -216,7 +216,7 @@ static const struct file_operations g_factory_fops = */ static struct telnet_dev_s *g_telnet_clients[CONFIG_TELNET_MAXLCLIENTS]; -static sem_t g_clients_sem = SEM_INITIALIZER(1); +static mutex_t g_clients_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -630,10 +630,10 @@ static int telnet_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&g_clients_sem); + ret = nxmutex_lock(&g_clients_lock); if (ret < 0) { - nerr("ERROR: nxsem_wait failed: %d\n", ret); + nerr("ERROR: nxmutex_lock failed: %d\n", ret); goto errout; } @@ -648,7 +648,7 @@ static int telnet_open(FAR struct file *filep) /* More than 255 opens; uint8_t would overflow to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Save the new open count on success */ @@ -656,8 +656,8 @@ static int telnet_open(FAR struct file *filep) priv->td_crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&g_clients_sem); +errout_with_lock: + nxmutex_unlock(&g_clients_lock); errout: return ret; @@ -679,10 +679,10 @@ static int telnet_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&g_clients_sem); + ret = nxmutex_lock(&g_clients_lock); if (ret < 0) { - nerr("ERROR: nxsem_wait failed: %d\n", ret); + nerr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -692,7 +692,7 @@ static int telnet_close(FAR struct file *filep) if (priv->td_crefs > 1) { - /* Just decrement the reference count and release the semaphore */ + /* Just decrement the reference count */ priv->td_crefs--; } @@ -748,7 +748,7 @@ static int telnet_close(FAR struct file *filep) kmm_free(priv); } - nxsem_post(&g_clients_sem); + nxmutex_unlock(&g_clients_lock); return ret; } @@ -961,10 +961,10 @@ static int telnet_session(FAR struct telnet_session_s *session) * Get exclusive access to the minor counter. */ - ret = nxsem_wait_uninterruptible(&g_clients_sem); + ret = nxmutex_lock(&g_clients_lock); if (ret < 0) { - nerr("ERROR: nxsem_wait failed: %d\n", ret); + nerr("ERROR: nxmutex_lock failed: %d\n", ret); goto errout_with_clone; } @@ -986,7 +986,7 @@ static int telnet_session(FAR struct telnet_session_s *session) { nerr("ERROR: Too many sessions\n"); ret = -ENFILE; - goto errout_with_semaphore; + goto errout_with_lock; } /* Register the driver */ @@ -996,7 +996,7 @@ static int telnet_session(FAR struct telnet_session_s *session) { nerr("ERROR: Failed to register the driver %s: %d\n", session->ts_devpath, ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Close the original psock (keeping the clone) */ @@ -1015,12 +1015,12 @@ static int telnet_session(FAR struct telnet_session_s *session) /* Save ourself in the list of Telnet client threads */ g_telnet_clients[priv->td_minor] = priv; - nxsem_post(&g_clients_sem); + nxmutex_unlock(&g_clients_lock); return OK; -errout_with_semaphore: - nxsem_post(&g_clients_sem); +errout_with_lock: + nxmutex_unlock(&g_clients_lock); errout_with_clone: psock_close(&priv->td_psock); diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 967305e26d7ba..00ff4da5faa0a 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -59,6 +59,7 @@ #include #include +#include #include #include #include @@ -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; diff --git a/drivers/pipes/pipe.c b/drivers/pipes/pipe.c index d0cac631338c2..43bc3be2d6f36 100644 --- a/drivers/pipes/pipe.c +++ b/drivers/pipes/pipe.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include "pipe_common.h" @@ -64,8 +64,8 @@ static const struct file_operations g_pipe_fops = pipecommon_poll /* poll */ }; -static sem_t g_pipesem = SEM_INITIALIZER(1); -static int g_pipeno; +static mutex_t g_pipelock = NXMUTEX_INITIALIZER; +static int g_pipeno; /**************************************************************************** * Private Functions @@ -79,7 +79,7 @@ static inline int pipe_allocate(void) { int ret; - ret = nxsem_wait(&g_pipesem); + ret = nxmutex_lock(&g_pipelock); if (ret < 0) { return ret; @@ -91,7 +91,7 @@ static inline int pipe_allocate(void) g_pipeno = 0; } - nxsem_post(&g_pipesem); + nxmutex_unlock(&g_pipelock); return ret; } diff --git a/drivers/pipes/pipe_common.c b/drivers/pipes/pipe_common.c index 24350af1fa783..b311576e24ab2 100644 --- a/drivers/pipes/pipe_common.c +++ b/drivers/pipes/pipe_common.c @@ -60,19 +60,6 @@ # define pipe_dumpbuffer(m,a,n) #endif -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: pipecommon_semtake - ****************************************************************************/ - -static int pipecommon_semtake(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -95,7 +82,7 @@ FAR struct pipe_dev_s *pipecommon_allocdev(size_t bufsize) /* Initialize the private structure */ memset(dev, 0, sizeof(struct pipe_dev_s)); - nxsem_init(&dev->d_bfsem, 0, 1); + nxmutex_init(&dev->d_bflock); nxsem_init(&dev->d_rdsem, 0, 0); nxsem_init(&dev->d_wrsem, 0, 0); @@ -118,7 +105,7 @@ FAR struct pipe_dev_s *pipecommon_allocdev(size_t bufsize) void pipecommon_freedev(FAR struct pipe_dev_s *dev) { - nxsem_destroy(&dev->d_bfsem); + nxmutex_destroy(&dev->d_bflock); nxsem_destroy(&dev->d_rdsem); nxsem_destroy(&dev->d_wrsem); kmm_free(dev); @@ -138,14 +125,14 @@ int pipecommon_open(FAR struct file *filep) DEBUGASSERT(dev != NULL); /* Make sure that we have exclusive access to the device structure. The - * nxsem_wait() call should fail if we are awakened by a signal or if the + * nxmutex_lock() call should fail if we are awakened by a signal or if the * thread was canceled. */ - ret = nxsem_wait(&dev->d_bfsem); + ret = nxmutex_lock(&dev->d_bflock); if (ret < 0) { - ferr("ERROR: nxsem_wait failed: %d\n", ret); + ferr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -159,7 +146,7 @@ int pipecommon_open(FAR struct file *filep) dev->d_buffer = (FAR uint8_t *)kmm_malloc(dev->d_bufsize); if (!dev->d_buffer) { - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return -ENOMEM; } } @@ -204,7 +191,7 @@ int pipecommon_open(FAR struct file *filep) * data to be read (policy == 1). */ - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); /* NOTE: d_rdsem is normally used when the read logic waits for more * data to be written. But until the first writer has opened the @@ -216,13 +203,13 @@ int pipecommon_open(FAR struct file *filep) */ ret = nxsem_wait(&dev->d_rdsem); - if (ret < 0 || (ret = nxsem_wait(&dev->d_bfsem)) < 0) + if (ret < 0 || (ret = nxmutex_lock(&dev->d_bflock)) < 0) { - /* The nxsem_wait() call should fail if we are awakened by a + /* The nxmutex_lock() call should fail if we are awakened by a * signal or if the task is canceled. */ - ferr("ERROR: nxsem_wait failed: %d\n", ret); + ferr("ERROR: nxmutex_lock failed: %d\n", ret); /* Immediately close the pipe that we just opened */ @@ -231,7 +218,7 @@ int pipecommon_open(FAR struct file *filep) } } - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return ret; } @@ -253,7 +240,7 @@ int pipecommon_close(FAR struct file *filep) * I've never seen anyone check that. */ - ret = pipecommon_semtake(&dev->d_bfsem); + ret = nxmutex_lock(&dev->d_bflock); if (ret < 0) { /* The close will not be performed if the task was canceled */ @@ -349,7 +336,7 @@ int pipecommon_close(FAR struct file *filep) #endif } - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return OK; } @@ -377,7 +364,7 @@ ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len) /* Make sure that we have exclusive access to the device structure */ - ret = nxsem_wait(&dev->d_bfsem); + ret = nxmutex_lock(&dev->d_bflock); if (ret < 0) { /* May fail because a signal was received or if the task was @@ -395,7 +382,7 @@ ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len) if (dev->d_nwriters <= 0) { - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return 0; } @@ -403,16 +390,16 @@ ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len) if (filep->f_oflags & O_NONBLOCK) { - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return -EAGAIN; } /* Otherwise, wait for something to be written to the pipe */ - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); ret = nxsem_wait(&dev->d_rdsem); - if (ret < 0 || (ret = nxsem_wait(&dev->d_bfsem)) < 0) + if (ret < 0 || (ret = nxmutex_lock(&dev->d_bflock)) < 0) { /* May fail because a signal was received or if the task was * canceled. @@ -451,7 +438,7 @@ ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len) nxsem_post(&dev->d_wrsem); } - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); pipe_dumpbuffer("From PIPE:", start, nread); return nread; } @@ -482,11 +469,11 @@ ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, } /* At present, this method cannot be called from interrupt handlers. That - * is because it calls nxsem_wait() and nxsem_wait() cannot be called from - * interrupt level. This actually happens fairly commonly IF [a-z]err() - * is called from interrupt handlers and stdout is being redirected via a - * pipe. In that case, the debug output will try to go out the pipe - * (interrupt handlers should use the _err() APIs). + * is because it calls nxmutex_lock() and nxmutex_lock() cannot be called + * form interrupt level. This actually happens fairly commonly + * IF [a-z]err() is called from interrupt handlers and stdout is being + * redirected via a pipe. In that case, the debug output will try to go + * out the pipe (interrupt handlers should use the _err() APIs). * * On the other hand, it would be very valuable to be able to feed the pipe * from an interrupt handler! TODO: Consider disabling interrupts instead @@ -498,7 +485,7 @@ ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, /* Make sure that we have exclusive access to the device structure */ - ret = nxsem_wait(&dev->d_bfsem); + ret = nxmutex_lock(&dev->d_bflock); if (ret < 0) { /* May fail because a signal was received or if the task was @@ -521,7 +508,7 @@ ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, if (dev->d_nreaders <= 0) { - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return nwritten == 0 ? -EPIPE : nwritten; } @@ -564,7 +551,7 @@ ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, /* Return the number of bytes written */ - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return len; } } @@ -605,7 +592,7 @@ ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, nwritten = -EAGAIN; } - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return nwritten; } @@ -613,9 +600,9 @@ ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, * the pipe */ - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); ret = nxsem_wait(&dev->d_wrsem); - if (ret < 0 || (ret = nxsem_wait(&dev->d_bfsem)) < 0) + if (ret < 0 || (ret = nxmutex_lock(&dev->d_bflock)) < 0) { /* Either call nxsem_wait may fail because a signal was * received or if the task was canceled. @@ -645,7 +632,7 @@ int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = pipecommon_semtake(&dev->d_bfsem); + ret = nxmutex_lock(&dev->d_bflock); if (ret < 0) { return ret; @@ -747,7 +734,7 @@ int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return ret; } @@ -770,7 +757,7 @@ int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg) } #endif - ret = pipecommon_semtake(&dev->d_bfsem); + ret = nxmutex_lock(&dev->d_bflock); if (ret < 0) { return ret; @@ -851,7 +838,7 @@ int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&dev->d_bfsem); + nxmutex_unlock(&dev->d_bflock); return ret; } diff --git a/drivers/pipes/pipe_common.h b/drivers/pipes/pipe_common.h index 380a2bc0d4c13..ff579e81f6a99 100644 --- a/drivers/pipes/pipe_common.h +++ b/drivers/pipes/pipe_common.h @@ -26,6 +26,7 @@ ****************************************************************************/ #include +#include #include #include @@ -113,7 +114,7 @@ typedef uint8_t pipe_ndx_t; /* 8-bit index */ struct pipe_dev_s { - sem_t d_bfsem; /* Used to serialize access to d_buffer and indices */ + mutex_t d_bflock; /* Used to serialize access to d_buffer and indices */ sem_t d_rdsem; /* Empty buffer - Reader waits for data write */ sem_t d_wrsem; /* Full buffer - Writer waits for data read */ pipe_ndx_t d_wrndx; /* Index in d_buffer to save next byte written */ diff --git a/drivers/power/battery/battery_charger.c b/drivers/power/battery/battery_charger.c index 4e4c8769f1cb6..858c278abf80d 100644 --- a/drivers/power/battery/battery_charger.c +++ b/drivers/power/battery/battery_charger.c @@ -56,7 +56,7 @@ struct battery_charger_priv_s { struct list_node node; - sem_t lock; + mutex_t lock; sem_t wait; uint32_t mask; FAR struct pollfd *fds; @@ -113,7 +113,7 @@ static int battery_charger_notify(FAR struct battery_charger_priv_s *priv, return OK; } - ret = nxsem_wait_uninterruptible(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -131,8 +131,7 @@ static int battery_charger_notify(FAR struct battery_charger_priv_s *priv, } } - nxsem_post(&priv->lock); - + nxmutex_unlock(&priv->lock); return OK; } @@ -156,19 +155,19 @@ static int bat_charger_open(FAR struct file *filep) return -ENOMEM; } - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { kmm_free(priv); return ret; } - nxsem_init(&priv->lock, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->wait, 0, 0); nxsem_set_protocol(&priv->wait, SEM_PRIO_NONE); priv->mask = dev->mask; list_add_tail(&dev->flist, &priv->node); - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); filep->f_priv = priv; return ret; @@ -188,15 +187,15 @@ static int bat_charger_close(FAR struct file *filep) FAR struct battery_charger_dev_s *dev = filep->f_inode->i_private; int ret; - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; } list_delete(&priv->node); - nxsem_post(&dev->batsem); - nxsem_destroy(&priv->lock); + nxmutex_unlock(&dev->batlock); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->wait); kmm_free(priv); @@ -218,7 +217,7 @@ static ssize_t bat_charger_read(FAR struct file *filep, FAR char *buffer, return -EINVAL; } - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -226,7 +225,7 @@ static ssize_t bat_charger_read(FAR struct file *filep, FAR char *buffer, while (priv->mask == 0) { - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); if (filep->f_oflags & O_NONBLOCK) { return -EAGAIN; @@ -238,7 +237,7 @@ static ssize_t bat_charger_read(FAR struct file *filep, FAR char *buffer, return ret; } - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -248,7 +247,7 @@ static ssize_t bat_charger_read(FAR struct file *filep, FAR char *buffer, memcpy(buffer, &priv->mask, sizeof(priv->mask)); priv->mask = 0; - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); return sizeof(priv->mask); } @@ -277,7 +276,7 @@ static int bat_charger_ioctl(FAR struct file *filep, int cmd, /* Enforce mutually exclusive access to the battery driver */ - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; /* Probably -EINTR */ @@ -400,7 +399,7 @@ static int bat_charger_ioctl(FAR struct file *filep, int cmd, break; } - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); return ret; } @@ -414,7 +413,7 @@ static ssize_t bat_charger_poll(FAR struct file *filep, FAR struct battery_charger_priv_s *priv = filep->f_priv; int ret; - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -438,7 +437,7 @@ static ssize_t bat_charger_poll(FAR struct file *filep, fds->priv = NULL; } - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); if (setup) { @@ -472,7 +471,7 @@ int battery_charger_changed(FAR struct battery_charger_dev_s *dev, return 0; } - ret = nxsem_wait_uninterruptible(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; @@ -485,7 +484,7 @@ int battery_charger_changed(FAR struct battery_charger_dev_s *dev, battery_charger_notify(priv, mask); } - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); return OK; } @@ -511,9 +510,9 @@ int battery_charger_register(FAR const char *devpath, { int ret; - /* Initialize the semaphore and the list */ + /* Initialize the mutex and the list */ - nxsem_init(&dev->batsem, 0, 1); + nxmutex_init(&dev->batlock); list_initialize(&dev->flist); /* Register the character driver */ diff --git a/drivers/power/battery/battery_gauge.c b/drivers/power/battery/battery_gauge.c index 17d79f5704c2f..b636d26691e25 100644 --- a/drivers/power/battery/battery_gauge.c +++ b/drivers/power/battery/battery_gauge.c @@ -56,7 +56,7 @@ struct battery_gauge_priv_s { struct list_node node; - sem_t lock; + mutex_t lock; sem_t wait; uint32_t mask; FAR struct pollfd *fds; @@ -115,7 +115,7 @@ static int battery_gauge_notify(FAR struct battery_gauge_priv_s *priv, return OK; } - ret = nxsem_wait_uninterruptible(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -133,8 +133,7 @@ static int battery_gauge_notify(FAR struct battery_gauge_priv_s *priv, } } - nxsem_post(&priv->lock); - + nxmutex_unlock(&priv->lock); return OK; } @@ -158,19 +157,19 @@ static int bat_gauge_open(FAR struct file *filep) return -ENOMEM; } - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { kmm_free(priv); return ret; } - nxsem_init(&priv->lock, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->wait, 0, 0); nxsem_set_protocol(&priv->wait, SEM_PRIO_NONE); priv->mask = dev->mask; list_add_tail(&dev->flist, &priv->node); - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); filep->f_priv = priv; return ret; @@ -190,15 +189,15 @@ static int bat_gauge_close(FAR struct file *filep) FAR struct battery_gauge_dev_s *dev = filep->f_inode->i_private; int ret; - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; } list_delete(&priv->node); - nxsem_post(&dev->batsem); - nxsem_destroy(&priv->lock); + nxmutex_unlock(&dev->batlock); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->wait); kmm_free(priv); @@ -220,7 +219,7 @@ static ssize_t bat_gauge_read(FAR struct file *filep, FAR char *buffer, return -EINVAL; } - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -228,7 +227,7 @@ static ssize_t bat_gauge_read(FAR struct file *filep, FAR char *buffer, while (priv->mask == 0) { - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); if (filep->f_oflags & O_NONBLOCK) { return -EAGAIN; @@ -240,7 +239,7 @@ static ssize_t bat_gauge_read(FAR struct file *filep, FAR char *buffer, return ret; } - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -250,7 +249,7 @@ static ssize_t bat_gauge_read(FAR struct file *filep, FAR char *buffer, memcpy(buffer, &priv->mask, sizeof(priv->mask)); priv->mask = 0; - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); return sizeof(priv->mask); } @@ -281,7 +280,7 @@ static int bat_gauge_ioctl(FAR struct file *filep, /* Enforce mutually exclusive access to the battery driver */ - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; /* Probably -EINTR */ @@ -368,7 +367,7 @@ static int bat_gauge_ioctl(FAR struct file *filep, break; } - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); return ret; } @@ -382,7 +381,7 @@ static ssize_t bat_gauge_poll(FAR struct file *filep, FAR struct battery_gauge_priv_s *priv = filep->f_priv; int ret; - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -406,7 +405,7 @@ static ssize_t bat_gauge_poll(FAR struct file *filep, fds->priv = NULL; } - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); if (setup) { @@ -440,7 +439,7 @@ int battery_gauge_changed(FAR struct battery_gauge_dev_s *dev, return 0; } - ret = nxsem_wait_uninterruptible(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; @@ -453,7 +452,7 @@ int battery_gauge_changed(FAR struct battery_gauge_dev_s *dev, battery_gauge_notify(priv, mask); } - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); return OK; } @@ -479,9 +478,9 @@ int battery_gauge_register(FAR const char *devpath, { int ret; - /* Initialize the semaphore and list */ + /* Initialize the mutex and list */ - nxsem_init(&dev->batsem, 0, 1); + nxmutex_init(&dev->batlock); list_initialize(&dev->flist); /* Register the character driver */ diff --git a/drivers/power/battery/battery_monitor.c b/drivers/power/battery/battery_monitor.c index 235faec335a24..870a4f3ba406c 100644 --- a/drivers/power/battery/battery_monitor.c +++ b/drivers/power/battery/battery_monitor.c @@ -57,7 +57,7 @@ struct battery_monitor_priv_s { struct list_node node; - sem_t lock; + mutex_t lock; sem_t wait; uint32_t mask; FAR struct pollfd *fds; @@ -114,7 +114,7 @@ static int battery_monitor_notify(FAR struct battery_monitor_priv_s *priv, return OK; } - ret = nxsem_wait_uninterruptible(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -132,8 +132,7 @@ static int battery_monitor_notify(FAR struct battery_monitor_priv_s *priv, } } - nxsem_post(&priv->lock); - + nxmutex_unlock(&priv->lock); return OK; } @@ -157,19 +156,19 @@ static int bat_monitor_open(FAR struct file *filep) return -ENOMEM; } - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { kmm_free(priv); return ret; } - nxsem_init(&priv->lock, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->wait, 0, 0); nxsem_set_protocol(&priv->wait, SEM_PRIO_NONE); priv->mask = dev->mask; list_add_tail(&dev->flist, &priv->node); - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); filep->f_priv = priv; return ret; @@ -189,15 +188,15 @@ static int bat_monitor_close(FAR struct file *filep) FAR struct battery_monitor_dev_s *dev = filep->f_inode->i_private; int ret; - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; } list_delete(&priv->node); - nxsem_post(&dev->batsem); - nxsem_destroy(&priv->lock); + nxmutex_unlock(&dev->batlock); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->wait); kmm_free(priv); @@ -219,7 +218,7 @@ static ssize_t bat_monitor_read(FAR struct file *filep, FAR char *buffer, return -EINVAL; } - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -227,7 +226,7 @@ static ssize_t bat_monitor_read(FAR struct file *filep, FAR char *buffer, while (priv->mask == 0) { - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); if (filep->f_oflags & O_NONBLOCK) { return -EAGAIN; @@ -239,7 +238,7 @@ static ssize_t bat_monitor_read(FAR struct file *filep, FAR char *buffer, return ret; } - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -249,7 +248,7 @@ static ssize_t bat_monitor_read(FAR struct file *filep, FAR char *buffer, memcpy(buffer, &priv->mask, sizeof(priv->mask)); priv->mask = 0; - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); return sizeof(priv->mask); } @@ -278,7 +277,7 @@ static int bat_monitor_ioctl(FAR struct file *filep, int cmd, /* Enforce mutually exclusive access to the battery driver */ - ret = nxsem_wait(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; /* Probably -EINTR */ @@ -447,7 +446,7 @@ static int bat_monitor_ioctl(FAR struct file *filep, int cmd, break; } - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); return ret; } @@ -461,7 +460,7 @@ static ssize_t bat_monitor_poll(FAR struct file *filep, FAR struct battery_monitor_priv_s *priv = filep->f_priv; int ret; - ret = nxsem_wait(&priv->lock); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -485,7 +484,7 @@ static ssize_t bat_monitor_poll(FAR struct file *filep, fds->priv = NULL; } - nxsem_post(&priv->lock); + nxmutex_unlock(&priv->lock); if (setup) { @@ -515,7 +514,7 @@ int battery_monitor_changed(FAR struct battery_monitor_dev_s *dev, return 0; } - ret = nxsem_wait_uninterruptible(&dev->batsem); + ret = nxmutex_lock(&dev->batlock); if (ret < 0) { return ret; @@ -528,7 +527,7 @@ int battery_monitor_changed(FAR struct battery_monitor_dev_s *dev, battery_monitor_notify(priv, mask); } - nxsem_post(&dev->batsem); + nxmutex_unlock(&dev->batlock); return OK; } @@ -554,9 +553,9 @@ int battery_monitor_register(FAR const char *devpath, { int ret; - /* Initialize the semaphore and the list */ + /* Initialize the mutex and the list */ - nxsem_init(&dev->batsem, 0, 1); + nxmutex_init(&dev->batlock); list_initialize(&dev->flist); /* Register the character driver */ diff --git a/drivers/power/pm/pm.h b/drivers/power/pm/pm.h index 17654ca93c007..6e91211ede874 100644 --- a/drivers/power/pm/pm.h +++ b/drivers/power/pm/pm.h @@ -87,11 +87,11 @@ struct pm_domain_s struct pm_global_s { - /* This semaphore manages mutually exclusive access to the power management + /* This mutex manages mutually exclusive access to the power management * registry. It must be initialized to the value 1. */ - sem_t regsem; + mutex_t reglock; /* registry is a doubly-linked list of registered power management * callback structures. To ensure mutually exclusive access, this list diff --git a/drivers/power/pm/pm_initialize.c b/drivers/power/pm/pm_initialize.c index 32939cd10e686..c8c5f87f2cc0c 100644 --- a/drivers/power/pm/pm_initialize.c +++ b/drivers/power/pm/pm_initialize.c @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include "pm.h" @@ -46,7 +46,7 @@ struct pm_global_s g_pmglobals = { - SEM_INITIALIZER(1) + NXMUTEX_INITIALIZER }; /**************************************************************************** diff --git a/drivers/power/pm/pm_register.c b/drivers/power/pm/pm_register.c index 61470c779b71c..6e7d80157adfa 100644 --- a/drivers/power/pm/pm_register.c +++ b/drivers/power/pm/pm_register.c @@ -60,9 +60,9 @@ int pm_register(FAR struct pm_callback_s *callbacks) /* Add the new entry to the end of the list of registered callbacks */ - nxsem_wait(&g_pmglobals.regsem); + nxmutex_lock(&g_pmglobals.reglock); dq_addlast(&callbacks->entry, &g_pmglobals.registry); - nxsem_post(&g_pmglobals.regsem); + nxmutex_unlock(&g_pmglobals.reglock); return 0; } diff --git a/drivers/power/pm/pm_unregister.c b/drivers/power/pm/pm_unregister.c index 0aee6948fcc3e..efd9a8117bb48 100644 --- a/drivers/power/pm/pm_unregister.c +++ b/drivers/power/pm/pm_unregister.c @@ -59,9 +59,9 @@ int pm_unregister(FAR struct pm_callback_s *callbacks) /* Remove entry from the list of registered callbacks. */ - nxsem_wait(&g_pmglobals.regsem); + nxmutex_lock(&g_pmglobals.reglock); dq_rem(&callbacks->entry, &g_pmglobals.registry); - nxsem_post(&g_pmglobals.regsem); + nxmutex_unlock(&g_pmglobals.reglock); return 0; } diff --git a/drivers/power/supply/powerled.c b/drivers/power/supply/powerled.c index dc074366adb14..1fc0d7df69468 100644 --- a/drivers/power/supply/powerled.c +++ b/drivers/power/supply/powerled.c @@ -88,7 +88,7 @@ static int powerled_open(FAR struct file *filep) /* If the port is the middle of closing, wait until the close is finished */ - ret = nxsem_wait(&dev->closesem); + ret = nxmutex_lock(&dev->closelock); if (ret >= 0) { /* Increment the count of references to the device. If this the first @@ -126,7 +126,7 @@ static int powerled_open(FAR struct file *filep) } } - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } return OK; @@ -148,7 +148,7 @@ static int powerled_close(FAR struct file *filep) irqstate_t flags; int ret; - ret = nxsem_wait(&dev->closesem); + ret = nxmutex_lock(&dev->closelock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -158,7 +158,7 @@ static int powerled_close(FAR struct file *filep) if (dev->ocount > 1) { dev->ocount--; - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } else { @@ -172,7 +172,7 @@ static int powerled_close(FAR struct file *filep) dev->ops->shutdown(dev); /* Disable the POWERLED */ leave_critical_section(flags); - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } } @@ -406,9 +406,9 @@ int powerled_register(FAR const char *path, FAR struct powerled_dev_s *dev, dev->ocount = 0; - /* Initialize semaphores */ + /* Initialize mutex */ - nxsem_init(&dev->closesem, 0, 1); + nxmutex_init(&dev->closelock); /* Connect POWERLED driver with lower level interface */ @@ -419,7 +419,7 @@ int powerled_register(FAR const char *path, FAR struct powerled_dev_s *dev, ret = register_driver(path, &powerled_fops, 0666, dev); if (ret < 0) { - nxsem_destroy(&dev->closesem); + nxmutex_destroy(&dev->closelock); } return ret; diff --git a/drivers/power/supply/regulator.c b/drivers/power/supply/regulator.c index 16f5bd7269e6c..4fbef8a974f42 100644 --- a/drivers/power/supply/regulator.c +++ b/drivers/power/supply/regulator.c @@ -58,7 +58,7 @@ static int _regulator_set_voltage_unlocked(FAR struct regulator_s *regulator, ****************************************************************************/ static struct list_node g_reg_list = LIST_INITIAL_VALUE(g_reg_list); -static sem_t g_reg_sem = SEM_INITIALIZER(1); +static mutex_t g_reg_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -150,7 +150,7 @@ static FAR struct regulator_dev_s *regulator_dev_lookup(const char *supply) FAR struct regulator_dev_s *rdev; FAR struct regulator_dev_s *rdev_found = NULL; - nxsem_wait_uninterruptible(&g_reg_sem); + nxmutex_lock(&g_reg_lock); list_for_every_entry(&g_reg_list, rdev, struct regulator_dev_s, list) { if (rdev->desc->name && strcmp(rdev->desc->name, supply) == 0) @@ -160,7 +160,7 @@ static FAR struct regulator_dev_s *regulator_dev_lookup(const char *supply) } } - nxsem_post(&g_reg_sem); + nxmutex_unlock(&g_reg_lock); #if defined(CONFIG_REGULATOR_RPMSG) if (rdev_found == NULL) @@ -416,10 +416,10 @@ FAR struct regulator_s *regulator_get(FAR const char *id) regulator->rdev = rdev; list_initialize(®ulator->list); - nxsem_wait_uninterruptible(&rdev->regulator_sem); + nxmutex_lock(&rdev->regulator_lock); rdev->open_count++; list_add_tail(&rdev->consumer_list, ®ulator->list); - nxsem_post(&rdev->regulator_sem); + nxmutex_unlock(&rdev->regulator_lock); return regulator; } @@ -448,14 +448,14 @@ void regulator_put(FAR struct regulator_s *regulator) rdev = regulator->rdev; - nxsem_wait_uninterruptible(&g_reg_sem); + nxmutex_lock(&g_reg_lock); - nxsem_wait_uninterruptible(&rdev->regulator_sem); + nxmutex_lock(&rdev->regulator_lock); list_delete(®ulator->list); rdev->open_count--; - nxsem_post(&rdev->regulator_sem); + nxmutex_unlock(&rdev->regulator_lock); - nxsem_post(&g_reg_sem); + nxmutex_unlock(&g_reg_lock); kmm_free(regulator); } @@ -487,9 +487,9 @@ int regulator_is_enabled(FAR struct regulator_s *regulator) rdev = regulator->rdev; - nxsem_wait_uninterruptible(&rdev->regulator_sem); + nxmutex_lock(&rdev->regulator_lock); ret = _regulator_is_enabled(rdev); - nxsem_post(&rdev->regulator_sem); + nxmutex_unlock(&rdev->regulator_lock); return ret; } @@ -521,7 +521,7 @@ int regulator_enable(FAR struct regulator_s *regulator) rdev = regulator->rdev; - nxsem_wait_uninterruptible(&rdev->regulator_sem); + nxmutex_lock(&rdev->regulator_lock); if (rdev->use_count == 0) { ret = _regulator_do_enable(rdev); @@ -534,8 +534,7 @@ int regulator_enable(FAR struct regulator_s *regulator) rdev->use_count++; err: - nxsem_post(&rdev->regulator_sem); - + nxmutex_unlock(&rdev->regulator_lock); return ret; } @@ -594,7 +593,7 @@ int regulator_disable(FAR struct regulator_s *regulator) rdev = regulator->rdev; - nxsem_wait_uninterruptible(&rdev->regulator_sem); + nxmutex_lock(&rdev->regulator_lock); if (rdev->use_count <= 0) { ret = -EIO; @@ -613,7 +612,7 @@ int regulator_disable(FAR struct regulator_s *regulator) rdev->use_count--; err: - nxsem_post(&rdev->regulator_sem); + nxmutex_unlock(&rdev->regulator_lock); return ret; } @@ -673,9 +672,9 @@ int regulator_set_voltage(FAR struct regulator_s *regulator, rdev = regulator->rdev; - nxsem_wait_uninterruptible(&rdev->regulator_sem); + nxmutex_lock(&rdev->regulator_lock); ret = _regulator_set_voltage_unlocked(regulator, min_uv, max_uv); - nxsem_post(&rdev->regulator_sem); + nxmutex_unlock(&rdev->regulator_lock); return ret; } @@ -707,9 +706,9 @@ int regulator_get_voltage(FAR struct regulator_s *regulator) rdev = regulator->rdev; - nxsem_wait_uninterruptible(&rdev->regulator_sem); + nxmutex_lock(&rdev->regulator_lock); ret = _regulator_get_voltage(rdev); - nxsem_post(&rdev->regulator_sem); + nxmutex_unlock(&rdev->regulator_lock); return ret; } @@ -776,7 +775,7 @@ regulator_register(FAR const struct regulator_desc_s *regulator_desc, rdev->desc = regulator_desc; rdev->ops = regulator_ops; rdev->priv = priv; - nxsem_init(&rdev->regulator_sem, 0, 1); + nxmutex_init(&rdev->regulator_lock); list_initialize(&rdev->consumer_list); list_initialize(&rdev->list); @@ -795,9 +794,9 @@ regulator_register(FAR const struct regulator_desc_s *regulator_desc, rdev->desc->apply_uv); } - nxsem_wait_uninterruptible(&g_reg_sem); + nxmutex_lock(&g_reg_lock); list_add_tail(&g_reg_list, &rdev->list); - nxsem_post(&g_reg_sem); + nxmutex_unlock(&g_reg_lock); return rdev; } @@ -818,16 +817,16 @@ void regulator_unregister(FAR struct regulator_dev_s *rdev) return; } - nxsem_wait_uninterruptible(&g_reg_sem); + nxmutex_lock(&g_reg_lock); if (rdev->open_count) { pwrerr("unregister, open %" PRIu32 "\n", rdev->open_count); - nxsem_post(&g_reg_sem); + nxmutex_unlock(&g_reg_lock); return; } list_delete(&rdev->list); - nxsem_post(&g_reg_sem); + nxmutex_unlock(&g_reg_lock); kmm_free(rdev); } diff --git a/drivers/power/supply/smps.c b/drivers/power/supply/smps.c index 69868f4ef9e3c..7d12b6735e87c 100644 --- a/drivers/power/supply/smps.c +++ b/drivers/power/supply/smps.c @@ -92,7 +92,7 @@ static int smps_open(FAR struct file *filep) /* If the port is the middle of closing, wait until the close is finished */ - ret = nxsem_wait(&dev->closesem); + ret = nxmutex_lock(&dev->closelock); if (ret >= 0) { /* Increment the count of references to the device. If this the first @@ -130,7 +130,7 @@ static int smps_open(FAR struct file *filep) } } - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } return OK; @@ -152,7 +152,7 @@ static int smps_close(FAR struct file *filep) irqstate_t flags; int ret; - ret = nxsem_wait(&dev->closesem); + ret = nxmutex_lock(&dev->closelock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -162,7 +162,7 @@ static int smps_close(FAR struct file *filep) if (dev->ocount > 1) { dev->ocount--; - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } else { @@ -176,7 +176,7 @@ static int smps_close(FAR struct file *filep) dev->ops->shutdown(dev); /* Disable the SMPS */ leave_critical_section(flags); - nxsem_post(&dev->closesem); + nxmutex_unlock(&dev->closelock); } } @@ -503,9 +503,9 @@ int smps_register(FAR const char *path, FAR struct smps_dev_s *dev, dev->ocount = 0; - /* Initialize semaphores */ + /* Initialize mutex */ - nxsem_init(&dev->closesem, 0, 1); + nxmutex_init(&dev->closelock); /* Connect SMPS driver with lower level interface */ @@ -516,7 +516,7 @@ int smps_register(FAR const char *path, FAR struct smps_dev_s *dev, ret = register_driver(path, &smps_fops, 0666, dev); if (ret < 0) { - nxsem_destroy(&dev->closesem); + nxmutex_destroy(&dev->closelock); } return ret; diff --git a/drivers/rc/lirc_dev.c b/drivers/rc/lirc_dev.c index 793b8f204843e..593b802145ec0 100644 --- a/drivers/rc/lirc_dev.c +++ b/drivers/rc/lirc_dev.c @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -53,7 +54,7 @@ struct lirc_upperhalf_s { struct list_node fh; /* list of struct lirc_fh_s object */ FAR struct lirc_lowerhalf_s *lower; /* the handle of lower half driver */ - sem_t exclsem; /* Manages exclusive access to lowerhalf */ + mutex_t lock; /* Manages exclusive access to lowerhalf */ bool gap; /* true if we're in a gap */ uint64_t gap_start; /* time when gap starts */ uint64_t gap_duration; /* duration of initial gap */ @@ -260,7 +261,7 @@ static int lirc_ioctl(FAR struct file *filep, int cmd, unsigned long arg) FAR unsigned int *val = (unsigned int *)(uintptr_t)arg; int ret; - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -575,7 +576,7 @@ static int lirc_ioctl(FAR struct file *filep, int cmd, unsigned long arg) ret = -ENOTTY; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -631,7 +632,7 @@ static ssize_t lirc_write(FAR struct file *filep, FAR const char *buffer, FAR struct lirc_fh_s *fh = filep->f_priv; ssize_t ret; - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -646,7 +647,7 @@ static ssize_t lirc_write(FAR struct file *filep, FAR const char *buffer, ret = lirc_write_pulse(filep, buffer, buflen); } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -788,7 +789,7 @@ int lirc_register(FAR struct lirc_lowerhalf_s *lower, int devno) upper->lower = lower; list_initialize(&upper->fh); - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); lower->priv = upper; /* Register remote control character device */ @@ -803,7 +804,7 @@ int lirc_register(FAR struct lirc_lowerhalf_s *lower, int devno) return ret; drv_err: - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); return ret; } @@ -825,7 +826,7 @@ void lirc_unregister(FAR struct lirc_lowerhalf_s *lower, int devno) FAR struct lirc_upperhalf_s *upper = lower->priv; char path[DEVNAME_MAX]; - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); snprintf(path, DEVNAME_MAX, DEVNAME_FMT, devno); rcinfo("UnRegistering %s\n", path); unregister_driver(path); diff --git a/drivers/sensors/adxl345.h b/drivers/sensors/adxl345.h index 1895b5961f788..4534c1a2a833f 100644 --- a/drivers/sensors/adxl345.h +++ b/drivers/sensors/adxl345.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -100,7 +101,7 @@ struct adxl345_dev_s /* Common fields */ FAR struct adxl345_config_s *config; /* Board configuration data */ - sem_t exclsem; /* Manages exclusive access to this structure */ + mutex_t lock; /* Manages exclusive access to this structure */ #ifdef CONFIG_ADXL345_SPI FAR struct spi_dev_s *spi; /* Saved SPI driver instance */ #else diff --git a/drivers/sensors/adxl345_base.c b/drivers/sensors/adxl345_base.c index 8c499073a0310..0ec8c9b922813 100644 --- a/drivers/sensors/adxl345_base.c +++ b/drivers/sensors/adxl345_base.c @@ -110,7 +110,7 @@ static ssize_t adxl345_read(FAR struct file *filep, /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -135,7 +135,7 @@ static ssize_t adxl345_read(FAR struct file *filep, buffer = (FAR char *) &sample; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return sizeof(struct adxl345_sample_s); } @@ -167,7 +167,7 @@ int adxl345_register(ADXL345_HANDLE handle, int minor) /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { snerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -187,14 +187,14 @@ int adxl345_register(ADXL345_HANDLE handle, int minor) if (ret < 0) { snerr("ERROR: Failed to register driver %s: %d\n", devname, ret); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } /* Indicate that the accelerometer was successfully initialized */ priv->status |= ADXL345_STAT_INITIALIZED; /* Accelerometer is initialized */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -373,7 +373,7 @@ ADXL345_HANDLE adxl345_instantiate(FAR struct i2c_master_s *dev, /* Initialize the device state structure */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->config = config; #ifdef CONFIG_ADXL345_SPI diff --git a/drivers/sensors/adxl372.c b/drivers/sensors/adxl372.c index be7d9fe05e13a..3d7d85acd71db 100644 --- a/drivers/sensors/adxl372.c +++ b/drivers/sensors/adxl372.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include /**************************************************************************** @@ -61,10 +61,8 @@ struct adxl372_dev_s FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */ FAR struct adxl372_config_s *config; /* Pointer to the configuration of the * ADXL372 sensor */ - sem_t devicesem; /* Manages exclusive access to this + mutex_t devicelock; /* Manages exclusive access to this * device */ - sem_t datasem; /* Manages exclusive access to this - * structure */ struct sensor_data_s data; /* The data as measured by the sensor */ uint8_t seek_address; /* Current device address. */ uint8_t readonly; /* 0 = writing to the device in enabled */ @@ -459,7 +457,7 @@ static int adxl372_dvr_open(FAR void *instance, int32_t arg) DEBUGASSERT(priv != NULL); UNUSED(arg); - ret = nxsem_trywait(&priv->devicesem); + ret = nxmutex_trylock(&priv->devicelock); if (ret < 0) { sninfo("INFO: ADXL372 Accelerometer is already opened.\n"); @@ -547,7 +545,7 @@ static int adxl372_dvr_close(FAR void *instance, int32_t arg) /* Release the sensor */ - nxsem_post(&priv->devicesem); + nxmutex_unlock(&priv->devicelock); return OK; } @@ -860,10 +858,9 @@ int adxl372_register(FAR const char *devpath, config->leaf_handle = NULL; config->sc_ops = NULL; - /* Initialize sensor and sensor data access semaphore */ + /* Initialize sensor and sensor data access mutex */ - nxsem_init(&priv->devicesem, 0, 1); - nxsem_init(&priv->datasem, 0, 1); + nxmutex_init(&priv->devicelock); /* Register the character driver */ @@ -872,7 +869,7 @@ int adxl372_register(FAR const char *devpath, { snerr("ERROR: Failed to register accelerometer driver: %d\n", ret); - nxsem_destroy(&priv->datasem); + nxmutex_destroy(&priv->devicelock); kmm_free(priv); return ret; } diff --git a/drivers/sensors/aht10.c b/drivers/sensors/aht10.c index 66b11badd8eab..ac0ed3bc01a96 100644 --- a/drivers/sensors/aht10.c +++ b/drivers/sensors/aht10.c @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -68,7 +69,7 @@ struct aht10_dev_s #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS int16_t crefs; /* Number of open references */ #endif - sem_t devsem; + mutex_t devlock; }; /**************************************************************************** @@ -338,28 +339,17 @@ static int aht10_open(FAR struct file *filep) { FAR struct inode *inode = filep->f_inode; FAR struct aht10_dev_s *priv = inode->i_private; - int ret; /* Get exclusive access */ - do - { - ret = nxsem_wait(&priv->devsem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); + nxmutex_lock(&priv->devlock); /* Increment the count of open references on the driver */ priv->crefs++; DEBUGASSERT(priv->crefs > 0); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -377,21 +367,10 @@ static int aht10_close(FAR struct file *filep) { FAR struct inode *inode = filep->f_inode; FAR struct aht10_dev_s *priv = inode->i_private; - int ret; /* Get exclusive access */ - do - { - ret = nxsem_wait(&priv->devsem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); + nxmutex_lock(&priv->devlock); /* Decrement the count of open references on the driver */ @@ -404,12 +383,12 @@ static int aht10_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -430,17 +409,7 @@ static ssize_t aht10_read(FAR struct file *filep, /* Get exclusive access */ - do - { - ret = nxsem_wait(&priv->devsem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); + nxmutex_lock(&priv->devlock); #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS if (priv->unlinked) @@ -449,7 +418,7 @@ static ssize_t aht10_read(FAR struct file *filep, * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -470,7 +439,7 @@ static ssize_t aht10_read(FAR struct file *filep, } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return length; } @@ -496,17 +465,7 @@ static int aht10_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - do - { - ret = nxsem_wait(&priv->devsem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); + nxmutex_lock(&priv->devlock); #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS if (priv->unlinked) @@ -515,7 +474,7 @@ static int aht10_ioctl(FAR struct file *filep, int cmd, unsigned long arg) * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -559,7 +518,7 @@ static int aht10_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -578,23 +537,13 @@ static int aht10_unlink(FAR struct inode *inode) /* Get exclusive access */ - do - { - ret = nxsem_wait(&priv->devsem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); + nxmutex_lock(&priv->devlock); /* Are there open references to the driver data structure? */ if (priv->crefs <= 0) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } @@ -604,7 +553,7 @@ static int aht10_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -651,7 +600,7 @@ int aht10_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->i2c = i2c; priv->addr = addr; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); ret = aht10_initialize(priv); if (ret < 0) diff --git a/drivers/sensors/bmg160.c b/drivers/sensors/bmg160.c index 4abfe2adb8b9f..59af4e863f7ae 100644 --- a/drivers/sensors/bmg160.c +++ b/drivers/sensors/bmg160.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include @@ -56,7 +56,7 @@ struct bmg160_dev_s FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */ FAR struct bmg160_config_s *config; /* Pointer to the configuration of the * BMG160 sensor */ - sem_t datasem; /* Manages exclusive access to this + mutex_t datalock; /* Manages exclusive access to this * structure */ struct bmg160_sensor_data_s data; /* The data as measured by the sensor */ struct work_s work; /* The work queue is responsible for @@ -212,12 +212,12 @@ static void bmg160_read_measurement_data(FAR struct bmg160_dev_s *dev) bmg160_read_gyroscope_data(dev, &x_gyr, &y_gyr, &z_gyr); - /* Acquire the semaphore before the data is copied */ + /* Acquire the mutex before the data is copied */ - ret = nxsem_wait(&dev->datasem); + ret = nxmutex_lock(&dev->datalock); if (ret < 0) { - snerr("ERROR: Could not acquire dev->datasem: %d\n", ret); + snerr("ERROR: Could not acquire dev->datalock: %d\n", ret); return; } @@ -227,9 +227,9 @@ static void bmg160_read_measurement_data(FAR struct bmg160_dev_s *dev) dev->data.y_gyr = (int16_t) (y_gyr); dev->data.z_gyr = (int16_t) (z_gyr); - /* Give back the semaphore */ + /* Give back the mutex */ - nxsem_post(&dev->datasem); + nxmutex_unlock(&dev->datalock); /* Feed sensor data to entropy pool */ @@ -446,12 +446,12 @@ static ssize_t bmg160_read(FAR struct file *filep, FAR char *buffer, return -ENOSYS; } - /* Acquire the semaphore before the data is copied */ + /* Acquire the mutex before the data is copied */ - ret = nxsem_wait(&priv->datasem); + ret = nxmutex_lock(&priv->datalock); if (ret < 0) { - snerr("ERROR: Could not acquire priv->datasem: %d\n", ret); + snerr("ERROR: Could not acquire priv->datalock: %d\n", ret); return ret; } @@ -464,9 +464,9 @@ static ssize_t bmg160_read(FAR struct file *filep, FAR char *buffer, data->y_gyr = priv->data.y_gyr; data->z_gyr = priv->data.z_gyr; - /* Give back the semaphore */ + /* Give back the mutex */ - nxsem_post(&priv->datasem); + nxmutex_unlock(&priv->datalock); return sizeof(FAR struct bmg160_sensor_data_s); } @@ -527,9 +527,9 @@ int bmg160_register(FAR const char *devpath, FAR struct spi_dev_s *spi, priv->config = config; priv->work.worker = NULL; - /* Initialize sensor data access semaphore */ + /* Initialize sensor data access mutex */ - nxsem_init(&priv->datasem, 0, 1); + nxmutex_init(&priv->datalock); /* Setup SPI frequency and mode */ @@ -552,7 +552,7 @@ int bmg160_register(FAR const char *devpath, FAR struct spi_dev_s *spi, { snerr("ERROR: Failed to register driver: %d\n", ret); kmm_free(priv); - nxsem_destroy(&priv->datasem); + nxmutex_destroy(&priv->datalock); return ret; } diff --git a/drivers/sensors/dhtxx.c b/drivers/sensors/dhtxx.c index b65e3e0f16c32..adf087d464deb 100644 --- a/drivers/sensors/dhtxx.c +++ b/drivers/sensors/dhtxx.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include /**************************************************************************** @@ -76,7 +76,7 @@ struct dhtxx_dev_s { FAR struct dhtxx_config_s *config; - sem_t devsem; + mutex_t devlock; uint8_t raw_data[5]; }; @@ -412,11 +412,11 @@ static int dhtxx_open(FAR struct file *filep) FAR struct dhtxx_dev_s *priv = inode->i_private; int ret; - /* Acquire the semaphore, wait the sampling time before sending anything to + /* Acquire the mutex, wait the sampling time before sending anything to * pass unstable state. */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -428,7 +428,7 @@ static int dhtxx_open(FAR struct file *filep) /* Sensor ready. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } @@ -459,7 +459,7 @@ static ssize_t dhtxx_read(FAR struct file *filep, FAR char *buffer, memset(priv->raw_data, 0u, sizeof(priv->raw_data)); - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return (ssize_t)ret; @@ -512,7 +512,7 @@ static ssize_t dhtxx_read(FAR struct file *filep, FAR char *buffer, /* Sensor ready for new reading */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -561,6 +561,8 @@ int dhtxx_register(FAR const char *devpath, priv->config = config; + nxmutex_init(&priv->devlock); + /* Register the character driver */ ret = register_driver(devpath, &g_dhtxxfops, 0666, priv); diff --git a/drivers/sensors/fakesensor.c b/drivers/sensors/fakesensor.c index b9301039c52b1..acda1bf3c3a2f 100644 --- a/drivers/sensors/fakesensor.c +++ b/drivers/sensors/fakesensor.c @@ -183,6 +183,7 @@ static inline void fakesensor_read_gps(FAR struct fakesensor_s *sensor) float hoop; float altitude; char raw[150]; + memset(&gps, 0, sizeof(struct sensor_gps)); read: diff --git a/drivers/sensors/hall3ph.c b/drivers/sensors/hall3ph.c index 48ccc9c8e1223..20bb5c5b6c7ac 100644 --- a/drivers/sensors/hall3ph.c +++ b/drivers/sensors/hall3ph.c @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include @@ -52,7 +52,7 @@ struct hall3_upperhalf_s { uint8_t crefs; - sem_t exclsem; + mutex_t lock; FAR struct hall3_lowerhalf_s *lower; }; @@ -109,7 +109,7 @@ static int hall3_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -126,7 +126,7 @@ static int hall3_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Check if this is the first time that the driver has been opened. */ @@ -143,7 +143,7 @@ static int hall3_open(FAR struct file *filep) ret = lower->ops->setup(lower); if (ret < 0) { - goto errout_with_sem; + goto errout_with_lock; } } @@ -152,8 +152,8 @@ static int hall3_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&upper->lock); errout: return ret; @@ -177,7 +177,7 @@ static int hall3_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -207,7 +207,7 @@ static int hall3_close(FAR struct file *filep) lower->ops->shutdown(lower); } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); ret = OK; errout: @@ -272,7 +272,7 @@ static int hall3_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -301,7 +301,7 @@ static int hall3_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -349,7 +349,7 @@ int hall3_register(FAR const char *devpath, * (it was already zeroed by kmm_zalloc()) */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); upper->lower = lower; /* Register the Hall effect sensor device */ diff --git a/drivers/sensors/hc_sr04.c b/drivers/sensors/hc_sr04.c index 413adf18f3ba5..86835db10b3ff 100644 --- a/drivers/sensors/hc_sr04.c +++ b/drivers/sensors/hc_sr04.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -69,7 +70,7 @@ static int hcsr04_poll(FAR struct file *filep, FAR struct pollfd *fds, struct hcsr04_dev_s { FAR struct hcsr04_config_s *config; - sem_t devsem; + mutex_t devlock; sem_t conv_donesem; int time_start_pulse; int time_finish_pulse; @@ -138,13 +139,13 @@ static int hcsr04_open(FAR struct file *filep) FAR struct hcsr04_dev_s *priv = inode->i_private; int ret; - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); hcsr04_dbg("OPENED\n"); return OK; } @@ -155,13 +156,13 @@ static int hcsr04_close(FAR struct file *filep) FAR struct hcsr04_dev_s *priv = inode->i_private; int ret; - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); hcsr04_dbg("CLOSED\n"); return OK; } @@ -177,7 +178,7 @@ static ssize_t hcsr04_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return (ssize_t)ret; @@ -213,7 +214,7 @@ static ssize_t hcsr04_read(FAR struct file *filep, FAR char *buffer, } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return length; } @@ -232,7 +233,7 @@ static int hcsr04_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -258,7 +259,7 @@ static int hcsr04_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -288,7 +289,7 @@ static int hcsr04_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -351,7 +352,7 @@ static int hcsr04_poll(FAR struct file *filep, FAR struct pollfd *fds, } out: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -415,7 +416,7 @@ int hcsr04_register(FAR const char *devpath, } priv->config = config; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); nxsem_init(&priv->conv_donesem, 0, 0); ret = register_driver(devpath, &g_hcsr04ops, 0666, priv); diff --git a/drivers/sensors/hdc1008.c b/drivers/sensors/hdc1008.c index 64727e97d13ee..0a995cb09ab5a 100644 --- a/drivers/sensors/hdc1008.c +++ b/drivers/sensors/hdc1008.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -111,7 +112,7 @@ struct hdc1008_dev_s #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS int16_t crefs; /* Number of open references */ #endif - sem_t devsem; + mutex_t devlock; }; /**************************************************************************** @@ -518,7 +519,7 @@ static int hdc1008_open(FAR struct file *filep) (FAR struct hdc1008_dev_s *)inode->i_private; int ret; - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -529,8 +530,7 @@ static int hdc1008_open(FAR struct file *filep) ++priv->crefs; DEBUGASSERT(priv->crefs > 0); - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return 0; } #endif @@ -551,7 +551,7 @@ static int hdc1008_close(FAR struct file *filep) (FAR struct hdc1008_dev_s *)inode->i_private; int ret; - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -568,12 +568,12 @@ static int hdc1008_close(FAR struct file *filep) if ((priv->crefs <= 0) && priv->unlinked) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -607,7 +607,7 @@ static ssize_t hdc1008_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return (ssize_t)ret; @@ -618,7 +618,7 @@ static ssize_t hdc1008_read(FAR struct file *filep, FAR char *buffer, { /* The driver is unliked, access is not allowed */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -626,7 +626,7 @@ static ssize_t hdc1008_read(FAR struct file *filep, FAR char *buffer, ret = hdc1008_measure_current_mode(priv, &data); if (ret < 0) { - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -648,12 +648,11 @@ static ssize_t hdc1008_read(FAR struct file *filep, FAR char *buffer, } else { - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -EINVAL; } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return len; } @@ -745,7 +744,7 @@ static int hdc1008_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return (ssize_t)ret; @@ -756,7 +755,7 @@ static int hdc1008_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { /* The driver is unliked, access is not allowed */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -829,8 +828,7 @@ static int hdc1008_ioctl(FAR struct file *filep, int cmd, unsigned long arg) } } - nxsem_post(&priv->devsem); - + nxmutex_unlock(&priv->devlock); return ret; } @@ -851,7 +849,7 @@ static int hdc1008_unlink(FAR struct inode *inode) DEBUGASSERT((inode != NULL) && (inode->i_private != NULL)); priv = (FAR struct hdc1008_dev_s *)inode->i_private; - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -861,7 +859,7 @@ static int hdc1008_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } @@ -871,7 +869,7 @@ static int hdc1008_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -961,7 +959,7 @@ int hdc1008_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->i2c = i2c; priv->addr = addr; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); /* Register the driver */ @@ -969,7 +967,7 @@ int hdc1008_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, if (ret < 0) { snerr("ERROR: Failed to register driver: %d\n", ret); - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); } @@ -987,7 +985,7 @@ int hdc1008_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, if (ret < 0) { snerr("ERROR: failed to read default configuration\n"); - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return ret; } @@ -999,7 +997,7 @@ int hdc1008_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, if (ret < 0) { snerr("ERROR: failed to set default configuration: %d\n", ret); - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return ret; } diff --git a/drivers/sensors/hts221.c b/drivers/sensors/hts221.c index 7547ab3ba1d31..6e6ca68d34155 100644 --- a/drivers/sensors/hts221.c +++ b/drivers/sensors/hts221.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -128,7 +129,7 @@ struct hts221_dev_s struct i2c_master_s *i2c; uint8_t addr; hts221_config_t *config; - sem_t devsem; + mutex_t devlock; volatile bool int_pending; struct pollfd *fds[CONFIG_HTS221_NPOLLWAITERS]; struct @@ -899,7 +900,7 @@ static int hts221_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -908,7 +909,7 @@ static int hts221_open(FAR struct file *filep) priv->config->set_power(priv->config, true); priv->config->irq_enable(priv->config, true); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); hts221_dbg("Sensor is powered on\n"); return OK; } @@ -921,7 +922,7 @@ static int hts221_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -931,7 +932,7 @@ static int hts221_close(FAR struct file *filep) ret = hts221_power_on_off(priv, false); priv->config->set_power(priv->config, false); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); hts221_dbg("CLOSED\n"); return ret; } @@ -947,7 +948,7 @@ static ssize_t hts221_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return (ssize_t)ret; @@ -970,7 +971,7 @@ static ssize_t hts221_read(FAR struct file *filep, FAR char *buffer, } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return length; } @@ -989,7 +990,7 @@ static int hts221_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1032,7 +1033,7 @@ static int hts221_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1071,7 +1072,7 @@ static int hts221_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1134,7 +1135,7 @@ static int hts221_poll(FAR struct file *filep, FAR struct pollfd *fds, } out: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -1168,7 +1169,7 @@ int hts221_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->addr = addr; priv->i2c = i2c; priv->config = config; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); priv->config->set_power(priv->config, true); diff --git a/drivers/sensors/hyt271.c b/drivers/sensors/hyt271.c index 197c20e8e1c7b..ef92ca5aa7e0d 100644 --- a/drivers/sensors/hyt271.c +++ b/drivers/sensors/hyt271.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include #include @@ -93,7 +93,7 @@ struct hyt271_dev_s struct hyt271_sensor_s sensor[HYT271_SENSOR_MAX]; /* Sensor types */ FAR struct i2c_master_s *i2c; /* I2C interface */ FAR struct hyt271_bus_s *bus; /* Bus power interface */ - sem_t lock_measure_cycle; /* Locks measure cycle */ + mutex_t lock_measure_cycle; /* Locks measure cycle */ uint32_t freq; /* I2C Frequency */ #ifdef CONFIG_SENSORS_HYT271_POLL unsigned long interval; /* Polling interval */ @@ -416,7 +416,7 @@ static int hyt271_change_addr(FAR struct hyt271_dev_s *dev, uint8_t addr) * change operation. */ - ret = nxsem_wait(&dev->lock_measure_cycle); + ret = nxmutex_lock(&dev->lock_measure_cycle); if (ret < 0) { return ret; @@ -517,11 +517,11 @@ static int hyt271_change_addr(FAR struct hyt271_dev_s *dev, uint8_t addr) dev->addr = addr; - nxsem_post(&dev->lock_measure_cycle); + nxmutex_unlock(&dev->lock_measure_cycle); return OK; err_unlock: - nxsem_post(&dev->lock_measure_cycle); + nxmutex_unlock(&dev->lock_measure_cycle); return ret; } @@ -551,7 +551,7 @@ static int hyt271_measure_read(FAR struct hyt271_dev_s *dev, * read operation. */ - ret = nxsem_wait(&dev->lock_measure_cycle); + ret = nxmutex_lock(&dev->lock_measure_cycle); if (ret < 0) { return ret; @@ -588,12 +588,12 @@ static int hyt271_measure_read(FAR struct hyt271_dev_s *dev, buffer[2] << 8 | buffer[3]; data->timestamp = hyt271_curtime(); - nxsem_post(&dev->lock_measure_cycle); + nxmutex_unlock(&dev->lock_measure_cycle); return OK; err_unlock: - nxsem_post(&dev->lock_measure_cycle); + nxmutex_unlock(&dev->lock_measure_cycle); return ret; } @@ -911,7 +911,7 @@ int hyt271_register(int devno, FAR struct i2c_master_s *i2c, uint8_t addr, priv->initial_read = false; #endif - nxsem_init(&priv->lock_measure_cycle, 0, 1); + nxmutex_init(&priv->lock_measure_cycle); #ifdef CONFIG_SENSORS_HYT271_POLL nxsem_init(&priv->run, 0, 0); nxsem_set_protocol(&priv->run, SEM_PRIO_NONE); @@ -973,7 +973,7 @@ int hyt271_register(int devno, FAR struct i2c_master_s *i2c, uint8_t addr, temp_err: sensor_unregister(&priv->sensor[HYT271_SENSOR_TEMP].lower, devno); - nxsem_destroy(&priv->lock_measure_cycle); + nxmutex_destroy(&priv->lock_measure_cycle); kmm_free(priv); return ret; } diff --git a/drivers/sensors/kxtj9.c b/drivers/sensors/kxtj9.c index ac000293646a7..0f8e130295262 100644 --- a/drivers/sensors/kxtj9.c +++ b/drivers/sensors/kxtj9.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include @@ -114,7 +114,7 @@ struct kxtj9_dev_s { FAR struct i2c_master_s *i2c; - sem_t exclsem; + mutex_t lock; bool enable; bool power_enabled; uint8_t address; @@ -325,7 +325,7 @@ static int kxtj9_configure(FAR struct kxtj9_dev_s *priv, uint8_t odr) uint8_t wbuf[0]; int ret; - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -360,7 +360,7 @@ static int kxtj9_configure(FAR struct kxtj9_dev_s *priv, uint8_t odr) priv->int_ctrl = KXTJ9_IEN | KXTJ9_IEA | KXTJ9_IEL; kxtj9_reg_write(priv, INT_CTRL1, priv->int_ctrl); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return 0; } @@ -378,7 +378,7 @@ static int kxtj9_enable(FAR struct kxtj9_dev_s *priv, bool on) uint8_t wbuf[1]; int ret; - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -406,7 +406,7 @@ static int kxtj9_enable(FAR struct kxtj9_dev_s *priv, bool on) sninfo("KXTJ9 in operating mode\n"); } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -425,7 +425,7 @@ static int kxtj9_read_sensor_data(FAR struct kxtj9_dev_s *priv, uint8_t data; int ret; - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -442,7 +442,7 @@ static int kxtj9_read_sensor_data(FAR struct kxtj9_dev_s *priv, /* Read INT_REL to clear interrupt status */ kxtj9_reg_read(priv, INT_REL, &data, 1); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* Feed sensor data to entropy pool */ @@ -628,7 +628,7 @@ int kxtj9_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->i2c = i2c; priv->address = address; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Register the character driver */ diff --git a/drivers/sensors/lis2dh.c b/drivers/sensors/lis2dh.c index 4147e805d32fd..7869480abab85 100644 --- a/drivers/sensors/lis2dh.c +++ b/drivers/sensors/lis2dh.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -88,7 +89,7 @@ struct lis2dh_dev_s struct lis2dh_setup *setup; /* User defined device operation mode setup */ struct lis2dh_vector_s vector_data; /* Latest read data read from lis2dh */ int scale; /* Full scale in milliG */ - sem_t devsem; /* Manages exclusive access to this structure */ + mutex_t devlock; /* Manages exclusive access to this structure */ bool fifo_used; /* LIS2DH configured to use FIFO */ bool fifo_stopped; /* FIFO got full and has stopped. */ #ifdef LIS2DH_COUNT_INTS @@ -313,7 +314,7 @@ static ssize_t lis2dh_read(FAR struct file *filep, FAR char *buffer, return -EINVAL; } - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -500,7 +501,7 @@ static ssize_t lis2dh_read(FAR struct file *filep, FAR char *buffer, ptr->header.int1_source = int1_src; ptr->header.int2_source = int2_src; - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); /* 'ret' was just for debugging, we do return partial reads here. */ @@ -544,7 +545,7 @@ static int lis2dh_ioctl(FAR struct file *filep, int cmd, unsigned long arg) DEBUGASSERT(inode && inode->i_private); priv = (FAR struct lis2dh_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -645,7 +646,7 @@ static int lis2dh_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -671,7 +672,7 @@ static int lis2dh_poll(FAR struct file *filep, FAR struct pollfd *fds, DEBUGASSERT(inode && inode->i_private); priv = (FAR struct lis2dh_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -731,7 +732,7 @@ static int lis2dh_poll(FAR struct file *filep, FAR struct pollfd *fds, } out: - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -2016,7 +2017,7 @@ int lis2dh_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, return -ENOMEM; } - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); priv->fifo_used = false; #ifdef LIS2DH_COUNT_INTS @@ -2046,7 +2047,7 @@ int lis2dh_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return ret; diff --git a/drivers/sensors/lis3dh.c b/drivers/sensors/lis3dh.c index c8eeff960c20d..c7bf2641cba1d 100644 --- a/drivers/sensors/lis3dh.c +++ b/drivers/sensors/lis3dh.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -75,7 +76,7 @@ struct lis3dh_dev_s sem_t readsem; /* Read notification semaphore */ uint8_t fifobuf[LIS3DH_FIFOBUF_SIZE]; /* Raw FIFO buffer */ struct lis3dh_sensor_data_s queue[LIS3DH_QUEUE_MAX]; - sem_t queuesem; /* Queue exclusive lock */ + mutex_t queuelock; /* Queue exclusive lock */ uint8_t queue_rpos; /* Queue read position */ uint8_t queue_wpos; /* Queue write position */ uint8_t queue_count; /* Number of elements in the queue */ @@ -265,45 +266,6 @@ static int lis3dh_ident(FAR struct lis3dh_dev_s *dev) return -ENODEV; } -/**************************************************************************** - * Name: lis3dh_queue_lock - * - * Description: - * Locks exclusive access to the ring buffer queue - * - * Input Parameters: - * dev - Pointer to device driver instance - * - ****************************************************************************/ - -static void lis3dh_queue_lock(FAR struct lis3dh_dev_s *dev) -{ - int ret; - - ret = nxsem_wait(&dev->queuesem); - if (ret < 0) - { - snerr("ERROR: queuesem wait error: %d\n", ret); - return; - } -} - -/**************************************************************************** - * Name: lis3dh_queue_unlock - * - * Description: - * Unlocks exclusive access to the ring buffer queue - * - * Input Parameters: - * dev - Pointer to device driver instance - * - ****************************************************************************/ - -static void lis3dh_queue_unlock(FAR struct lis3dh_dev_s *dev) -{ - nxsem_post(&dev->queuesem); -} - /**************************************************************************** * Name: lis3dh_queue_push * @@ -318,10 +280,10 @@ static void lis3dh_queue_unlock(FAR struct lis3dh_dev_s *dev) static int lis3dh_queue_push(FAR struct lis3dh_dev_s *dev, struct lis3dh_sensor_data_s *data) { - lis3dh_queue_lock(dev); + nxmutex_lock(&dev->queuelock); if (dev->queue_count >= LIS3DH_QUEUE_MAX) { - lis3dh_queue_unlock(dev); + nxmutex_unlock(&dev->queuelock); return -ENOMEM; } @@ -329,7 +291,7 @@ static int lis3dh_queue_push(FAR struct lis3dh_dev_s *dev, dev->queue[dev->queue_wpos % LIS3DH_QUEUE_MAX] = *data; dev->queue_count++; - lis3dh_queue_unlock(dev); + nxmutex_unlock(&dev->queuelock); return OK; } @@ -352,10 +314,10 @@ static int lis3dh_queue_push(FAR struct lis3dh_dev_s *dev, static int lis3dh_queue_pop(FAR struct lis3dh_dev_s *dev, struct lis3dh_sensor_data_s *data) { - lis3dh_queue_lock(dev); + nxmutex_lock(&dev->queuelock); if (dev->queue_count == 0) { - lis3dh_queue_unlock(dev); + nxmutex_unlock(&dev->queuelock); return -EAGAIN; } @@ -364,8 +326,7 @@ static int lis3dh_queue_pop(FAR struct lis3dh_dev_s *dev, dev->queue_count--; - lis3dh_queue_unlock(dev); - + nxmutex_unlock(&dev->queuelock); return OK; } @@ -1003,9 +964,9 @@ int lis3dh_register(FAR const char *devpath, FAR struct spi_dev_s *spi, priv->work.worker = NULL; priv->queue_count = 0; - /* Initialize queue semaphore */ + /* Initialize queue mutex */ - nxsem_init(&priv->queuesem, 0, 1); + nxmutex_init(&priv->queuelock); /* Initialize read notification semaphore */ @@ -1024,7 +985,7 @@ int lis3dh_register(FAR const char *devpath, FAR struct spi_dev_s *spi, { snerr("ERROR: Failed to register driver: %d\n", ret); kmm_free(priv); - nxsem_destroy(&priv->queuesem); + nxmutex_destroy(&priv->queuelock); nxsem_destroy(&priv->readsem); return ret; } diff --git a/drivers/sensors/lis3dsh.c b/drivers/sensors/lis3dsh.c index c5a13bf29cf56..6c0dbe1d0d6cd 100644 --- a/drivers/sensors/lis3dsh.c +++ b/drivers/sensors/lis3dsh.c @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #if defined(CONFIG_SPI) && defined(CONFIG_LIS3DSH) @@ -56,7 +56,7 @@ struct lis3dsh_dev_s FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */ FAR struct lis3dsh_config_s *config; /* Pointer to the configuration * of the LIS3DSH sensor */ - sem_t datasem; /* Manages exclusive access to this + mutex_t datalock; /* Manages exclusive access to this * structure */ struct lis3dsh_sensor_data_s data; /* The data as measured by the sensor */ struct work_s work; /* The work queue is responsible for @@ -219,12 +219,12 @@ static void lis3dsh_read_measurement_data(FAR struct lis3dsh_dev_s *dev) lis3dsh_read_acclerometer_data(dev, &x_acc, &y_acc, &z_acc); - /* Acquire the semaphore before the data is copied */ + /* Acquire the mutex before the data is copied */ - ret = nxsem_wait(&dev->datasem); + ret = nxmutex_lock(&dev->datalock); if (ret < 0) { - snerr("ERROR: Could not acquire dev->datasem: %d\n", ret); + snerr("ERROR: Could not acquire dev->datalock: %d\n", ret); return; } @@ -234,9 +234,9 @@ static void lis3dsh_read_measurement_data(FAR struct lis3dsh_dev_s *dev) dev->data.y_acc = (int16_t)y_acc; dev->data.z_acc = (int16_t)z_acc; - /* Give back the semaphore */ + /* Give back the mutex */ - nxsem_post(&dev->datasem); + nxmutex_unlock(&dev->datalock); /* Feed sensor data to entropy pool */ @@ -459,12 +459,12 @@ static ssize_t lis3dsh_read(FAR struct file *filep, FAR char *buffer, return -ENOSYS; } - /* Acquire the semaphore before the data is copied */ + /* Acquire the mutex before the data is copied */ - ret = nxsem_wait(&priv->datasem); + ret = nxmutex_lock(&priv->datalock); if (ret < 0) { - snerr("ERROR: Could not acquire priv->datasem: %d\n", ret); + snerr("ERROR: Could not acquire priv->datalock: %d\n", ret); return ret; } @@ -477,10 +477,9 @@ static ssize_t lis3dsh_read(FAR struct file *filep, FAR char *buffer, data->y_acc = priv->data.y_acc; data->z_acc = priv->data.z_acc; - /* Give back the semaphore */ - - nxsem_post(&priv->datasem); + /* Give back the mutex */ + nxmutex_unlock(&priv->datalock); return sizeof(FAR struct lis3dsh_sensor_data_s); } @@ -540,8 +539,7 @@ int lis3dsh_register(FAR const char *devpath, FAR struct spi_dev_s *spi, priv->config = config; priv->work.worker = NULL; - nxsem_init(&priv->datasem, 0, 1); /* Initialize sensor data access - * semaphore */ + nxmutex_init(&priv->datalock); /* Initialize sensor data access mutex */ /* Setup SPI frequency and mode */ @@ -555,7 +553,7 @@ int lis3dsh_register(FAR const char *devpath, FAR struct spi_dev_s *spi, { snerr("ERROR: Failed to register driver: %d\n", ret); kmm_free(priv); - nxsem_destroy(&priv->datasem); + nxmutex_destroy(&priv->datalock); return ret; } diff --git a/drivers/sensors/lis3mdl.c b/drivers/sensors/lis3mdl.c index e172b8f7605aa..2acaa4c99ecd5 100644 --- a/drivers/sensors/lis3mdl.c +++ b/drivers/sensors/lis3mdl.c @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #if defined(CONFIG_SPI) && defined(CONFIG_SENSORS_LIS3MDL) @@ -57,7 +57,7 @@ struct lis3mdl_dev_s FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */ FAR struct lis3mdl_config_s *config; /* Pointer to the configuration * of the LIS3MDL sensor */ - sem_t datasem; /* Manages exclusive access to this + mutex_t datalock; /* Manages exclusive access to this * structure */ struct lis3mdl_sensor_data_s data; /* The data as measured by the sensor */ struct work_s work; /* The work queue is responsible for @@ -224,12 +224,12 @@ static void lis3mdl_read_measurement_data(FAR struct lis3mdl_dev_s *dev) lis3mdl_read_temperature(dev, &temperature); - /* Acquire the semaphore before the data is copied */ + /* Acquire the mutex before the data is copied */ - int ret = nxsem_wait(&dev->datasem); + int ret = nxmutex_lock(&dev->datalock); if (ret != OK) { - snerr("ERROR: Could not acquire dev->datasem: %d\n", ret); + snerr("ERROR: Could not acquire dev->datalock: %d\n", ret); return; } @@ -240,9 +240,9 @@ static void lis3mdl_read_measurement_data(FAR struct lis3mdl_dev_s *dev) dev->data.z_mag = (int16_t) (z_mag); dev->data.temperature = (int16_t) (temperature); - /* Give back the semaphore */ + /* Give back the mutex */ - nxsem_post(&dev->datasem); + nxmutex_unlock(&dev->datalock); /* Feed sensor data to entropy pool */ @@ -502,12 +502,12 @@ static ssize_t lis3mdl_read(FAR struct file *filep, FAR char *buffer, return -ENOSYS; } - /* Acquire the semaphore before the data is copied */ + /* Acquire the mutex before the data is copied */ - ret = nxsem_wait(&priv->datasem); + ret = nxmutex_lock(&priv->datalock); if (ret < 0) { - snerr("ERROR: Could not acquire priv->datasem: %d\n", ret); + snerr("ERROR: Could not acquire priv->datalock: %d\n", ret); return ret; } @@ -521,10 +521,9 @@ static ssize_t lis3mdl_read(FAR struct file *filep, FAR char *buffer, data->z_mag = priv->data.z_mag; data->temperature = priv->data.temperature; - /* Give back the semaphore */ - - nxsem_post(&priv->datasem); + /* Give back the mutex */ + nxmutex_unlock(&priv->datalock); return sizeof(FAR struct lis3mdl_sensor_data_s); } @@ -586,8 +585,7 @@ int lis3mdl_register(FAR const char *devpath, FAR struct spi_dev_s *spi, priv->work.worker = NULL; - nxsem_init(&priv->datasem, 0, 1); /* Initialize sensor data access - * semaphore */ + nxmutex_init(&priv->datalock); /* Initialize sensor data access mutex */ /* Setup SPI frequency and mode */ @@ -610,7 +608,7 @@ int lis3mdl_register(FAR const char *devpath, FAR struct spi_dev_s *spi, { snerr("ERROR: Failed to register driver: %d\n", ret); kmm_free(priv); - nxsem_destroy(&priv->datasem); + nxmutex_destroy(&priv->datalock); return -ENODEV; } diff --git a/drivers/sensors/lps25h.c b/drivers/sensors/lps25h.c index feb4e97a9a3db..1cabb33f87f87 100644 --- a/drivers/sensors/lps25h.c +++ b/drivers/sensors/lps25h.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -118,7 +119,7 @@ struct lps25h_dev_s uint8_t addr; bool irqenabled; volatile bool int_pending; - sem_t devsem; + mutex_t devlock; sem_t waitsem; lps25h_config_t *config; }; @@ -330,7 +331,7 @@ static int lps25h_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -351,7 +352,7 @@ static int lps25h_open(FAR struct file *filep) dev->irqenabled = true; out: - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -363,7 +364,7 @@ static int lps25h_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -375,7 +376,7 @@ static int lps25h_close(FAR struct file *filep) dev->config->set_power(dev->config, false); lps25h_dbg("CLOSED\n"); - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -390,7 +391,7 @@ static ssize_t lps25h_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return (ssize_t)ret; @@ -420,7 +421,7 @@ static ssize_t lps25h_read(FAR struct file *filep, FAR char *buffer, } out: - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return length; } @@ -695,7 +696,7 @@ static int lps25h_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -732,7 +733,7 @@ static int lps25h_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -749,7 +750,7 @@ int lps25h_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, return -ENOMEM; } - nxsem_init(&dev->devsem, 0, 1); + nxmutex_init(&dev->devlock); nxsem_init(&dev->waitsem, 0, 0); dev->addr = addr; diff --git a/drivers/sensors/lsm330_spi.c b/drivers/sensors/lsm330_spi.c index a257b78308660..d79c6ea757195 100644 --- a/drivers/sensors/lsm330_spi.c +++ b/drivers/sensors/lsm330_spi.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include /**************************************************************************** @@ -62,10 +62,8 @@ struct lsm330_dev_s FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */ FAR struct lsm330_config_s *config; /* Pointer to the configuration of the * LSM330 sensor */ - sem_t devicesem; /* Manages exclusive access to this + mutex_t devicelock; /* Manages exclusive access to this * device */ - sem_t datasem; /* Manages exclusive access to this - * structure */ struct sensor_data_s data; /* The data as measured by the sensor */ uint8_t seek_address; /* Current device address. */ uint8_t readonly; /* 0 = writing to the device in enabled */ @@ -675,7 +673,7 @@ static int lsm330acl_dvr_open(FAR void *instance_handle, int32_t arg) DEBUGASSERT(priv != NULL); UNUSED(arg); - ret = nxsem_trywait(&priv->devicesem); + ret = nxmutex_trylock(&priv->devicelock); if (ret < 0) { sninfo("INFO: LSM330 Accelerometer is already open.\n"); @@ -768,7 +766,7 @@ static int lsm330gyro_dvr_open(FAR void *instance_handle, int32_t arg) DEBUGASSERT(priv != NULL); UNUSED(arg); - ret = nxsem_trywait(&priv->devicesem); + ret = nxmutex_trylock(&priv->devicelock); if (ret < 0) { sninfo("INFO: LSM330 Gyroscope is already open.\n"); @@ -860,7 +858,7 @@ static int lsm330acl_dvr_close(FAR void *instance_handle, int32_t arg) /* Release the sensor */ - nxsem_post(&priv->devicesem); + nxmutex_unlock(&priv->devicelock); return OK; } @@ -883,7 +881,7 @@ static int lsm330gyro_dvr_close(FAR void *instance_handle, int32_t arg) /* Release the sensor */ - nxsem_post(&priv->devicesem); + nxmutex_unlock(&priv->devicelock); return OK; } @@ -1337,13 +1335,12 @@ int lsm330_register(FAR const char *devpath_acl, return -ENOMEM; } - priv->spi = spi; - priv->config = config_acl; + priv->spi = spi; + priv->config = config_acl; - /* Initialize sensor and sensor data access semaphore */ + /* Initialize sensor and sensor data access mutex */ - nxsem_init(&priv->devicesem, 0, 1); - nxsem_init(&priv->datasem, 0, 1); + nxmutex_init(&priv->devicelock); /* Setup SPI frequency and mode */ @@ -1357,7 +1354,7 @@ int lsm330_register(FAR const char *devpath_acl, { snerr("ERROR: Failed to register accelerometer driver: %d\n", ret); - nxsem_destroy(&priv->datasem); + nxmutex_destroy(&priv->devicelock); kmm_free(priv); return ret; } @@ -1370,7 +1367,7 @@ int lsm330_register(FAR const char *devpath_acl, priv->flink = g_lsm330a_list; g_lsm330a_list = priv; priva = priv; - config_acl->leaf_handle = (void *) priv; + config_acl->leaf_handle = (void *)priv; /* Initialize the LSM330 gyroscope device structure. */ @@ -1385,10 +1382,9 @@ int lsm330_register(FAR const char *devpath_acl, priv->spi = spi; priv->config = config_gyro; - /* Initialize sensor and sensor data access semaphore */ + /* Initialize sensor and sensor data access mutex */ - nxsem_init(&priv->devicesem, 0, 1); - nxsem_init(&priv->datasem, 0, 1); + nxmutex_init(&priv->devicelock); /* Register the character driver */ @@ -1397,7 +1393,7 @@ int lsm330_register(FAR const char *devpath_acl, { snerr("ERROR: Failed to register gyroscope driver: %d\n", ret); - nxsem_destroy(&priv->datasem); + nxmutex_destroy(&priv->devicelock); kmm_free(priv); goto err_exit; } @@ -1409,7 +1405,7 @@ int lsm330_register(FAR const char *devpath_acl, priv->flink = g_lsm330g_list; g_lsm330g_list = priv; - config_gyro->leaf_handle = (void *) priv; + config_gyro->leaf_handle = (void *)priv; config_acl->sc_ops = &g_lsm330acl_dops; config_gyro->sc_ops = &g_lsm330gyro_dops; @@ -1425,7 +1421,7 @@ int lsm330_register(FAR const char *devpath_acl, * accelerometer instance. */ - nxsem_destroy(&priva->datasem); + nxmutex_destroy(&priva->devicelock); kmm_free(priva); return ret; } diff --git a/drivers/sensors/max44009.c b/drivers/sensors/max44009.c index 823d73b3e2b38..194216dfac339 100644 --- a/drivers/sensors/max44009.c +++ b/drivers/sensors/max44009.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -73,7 +74,7 @@ struct max44009_dev_s { FAR struct max44009_config_s *config; - sem_t dev_sem; + mutex_t dev_lock; FAR struct i2c_master_s *i2c; uint8_t addr; uint8_t cref; @@ -221,7 +222,7 @@ static int max44009_open(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); priv = (FAR struct max44009_dev_s *)inode->i_private; - ret = nxsem_wait_uninterruptible(&priv->dev_sem); + ret = nxmutex_lock(&priv->dev_lock); if (ret < 0) { return ret; @@ -236,7 +237,7 @@ static int max44009_open(FAR struct file *filep) if (ret < 0) { max44009_dbg("Cannot power on sensor: %d\n", ret); - goto out_sem; + goto out_lock; } priv->config->irq_enable(priv->config, true); @@ -252,8 +253,8 @@ static int max44009_open(FAR struct file *filep) max44009_dbg("Sensor is powered on\n"); -out_sem: - nxsem_post(&priv->dev_sem); +out_lock: + nxmutex_unlock(&priv->dev_lock); return ret; } @@ -270,7 +271,7 @@ static int max44009_close(FAR struct file *filep) DEBUGASSERT(inode && inode->i_private); priv = (FAR struct max44009_dev_s *)inode->i_private; - ret = nxsem_wait_uninterruptible(&priv->dev_sem); + ret = nxmutex_lock(&priv->dev_lock); if (ret < 0) { return ret; @@ -294,7 +295,7 @@ static int max44009_close(FAR struct file *filep) } max44009_dbg("CLOSED\n"); - nxsem_post(&priv->dev_sem); + nxmutex_unlock(&priv->dev_lock); return OK; } @@ -313,7 +314,7 @@ static ssize_t max44009_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); priv = (FAR struct max44009_dev_s *)inode->i_private; - ret = nxsem_wait_uninterruptible(&priv->dev_sem); + ret = nxmutex_lock(&priv->dev_lock); if (ret < 0) { return (ssize_t)ret; @@ -335,7 +336,7 @@ static ssize_t max44009_read(FAR struct file *filep, FAR char *buffer, } } - nxsem_post(&priv->dev_sem); + nxmutex_unlock(&priv->dev_lock); return length; } @@ -717,7 +718,7 @@ static int max44009_ioctl(FAR struct file *filep, int cmd, unsigned long arg) DEBUGASSERT(inode && inode->i_private); priv = (FAR struct max44009_dev_s *)inode->i_private; - ret = nxsem_wait_uninterruptible(&priv->dev_sem); + ret = nxmutex_lock(&priv->dev_lock); if (ret < 0) { return ret; @@ -748,7 +749,7 @@ static int max44009_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->dev_sem); + nxmutex_unlock(&priv->dev_lock); return ret; } @@ -789,7 +790,7 @@ static int max44009_poll(FAR struct file *filep, FAR struct pollfd *fds, DEBUGASSERT(inode && inode->i_private); priv = (FAR struct max44009_dev_s *)inode->i_private; - ret = nxsem_wait_uninterruptible(&priv->dev_sem); + ret = nxmutex_lock(&priv->dev_lock); if (ret < 0) { return ret; @@ -849,7 +850,7 @@ static int max44009_poll(FAR struct file *filep, FAR struct pollfd *fds, } out: - nxsem_post(&priv->dev_sem); + nxmutex_unlock(&priv->dev_lock); return ret; } @@ -889,7 +890,7 @@ int max44009_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->addr = addr; priv->i2c = i2c; priv->config = config; - nxsem_init(&priv->dev_sem, 0, 1); + nxmutex_init(&priv->dev_lock); ret = register_driver(devpath, &g_alsops, 0666, priv); max44009_dbg("Registered with %d\n", ret); diff --git a/drivers/sensors/mlx90393.c b/drivers/sensors/mlx90393.c index 0eac05a9377e2..6742ef8f7399b 100644 --- a/drivers/sensors/mlx90393.c +++ b/drivers/sensors/mlx90393.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include @@ -58,7 +58,7 @@ struct mlx90393_dev_s FAR struct mlx90393_config_s *config; /* Pointer to the configuration of * the MLX90393 sensor */ - sem_t datasem; /* Manages exclusive access to + mutex_t datalock; /* Manages exclusive access to * this structure */ struct mlx90393_sensor_data_s data; /* The data as measured by the * sensor */ @@ -201,12 +201,12 @@ static void mlx90393_read_measurement_data(FAR struct mlx90393_dev_s *dev) SPI_LOCK(dev->spi, false); - /* Acquire the semaphore before the data is copied */ + /* Acquire the mutex before the data is copied */ - ret = nxsem_wait(&dev->datasem); + ret = nxmutex_lock(&dev->datalock); if (ret != OK) { - snerr("ERROR: Could not acquire dev->datasem: %d\n", ret); + snerr("ERROR: Could not acquire dev->datalock: %d\n", ret); return; } @@ -217,9 +217,9 @@ static void mlx90393_read_measurement_data(FAR struct mlx90393_dev_s *dev) dev->data.y_mag = (int16_t) (y_mag); dev->data.z_mag = (int16_t) (z_mag); - /* Give back the semaphore */ + /* Give back the mutex */ - nxsem_post(&dev->datasem); + nxmutex_unlock(&dev->datalock); /* Feed sensor data to entropy pool */ @@ -482,12 +482,12 @@ static ssize_t mlx90393_read(FAR struct file *filep, FAR char *buffer, /* Copy the sensor data into the buffer */ - /* Acquire the semaphore before the data is copied */ + /* Acquire the mutex before the data is copied */ - ret = nxsem_wait(&priv->datasem); + ret = nxmutex_lock(&priv->datalock); if (ret < 0) { - snerr("ERROR: Could not acquire priv->datasem: %d\n", ret); + snerr("ERROR: Could not acquire priv->datalock: %d\n", ret); return ret; } @@ -499,9 +499,9 @@ static ssize_t mlx90393_read(FAR struct file *filep, FAR char *buffer, data->z_mag = priv->data.z_mag; data->temperature = priv->data.temperature; - /* Give back the semaphore */ + /* Give back the mutex */ - nxsem_post(&priv->datasem); + nxmutex_unlock(&priv->datalock); return sizeof(FAR struct mlx90393_sensor_data_s); } @@ -563,8 +563,7 @@ int mlx90393_register(FAR const char *devpath, FAR struct spi_dev_s *spi, priv->work.worker = NULL; - nxsem_init(&priv->datasem, 0, 1); /* Initialize sensor data access - * semaphore */ + nxmutex_init(&priv->datalock); /* Initialize sensor data access mutex */ /* Setup SPI frequency and mode */ @@ -587,7 +586,7 @@ int mlx90393_register(FAR const char *devpath, FAR struct spi_dev_s *spi, { snerr("ERROR: Failed to register driver: %d\n", ret); kmm_free(priv); - nxsem_destroy(&priv->datasem); + nxmutex_destroy(&priv->datalock); return -ENODEV; } diff --git a/drivers/sensors/mpu60x0.c b/drivers/sensors/mpu60x0.c index 4ecd684b77c47..703995b8a7727 100644 --- a/drivers/sensors/mpu60x0.c +++ b/drivers/sensors/mpu60x0.c @@ -973,7 +973,6 @@ int mpu60x0_register(FAR const char *path, FAR struct mpu_config_s *config) snerr("ERROR: Failed to configure mpu60x0: %d\n", ret); nxmutex_destroy(&priv->lock); - kmm_free(priv); return ret; } @@ -986,7 +985,6 @@ int mpu60x0_register(FAR const char *path, FAR struct mpu_config_s *config) snerr("ERROR: Failed to register mpu60x0 interface: %d\n", ret); nxmutex_destroy(&priv->lock); - kmm_free(priv); return ret; } diff --git a/drivers/sensors/ms5611.c b/drivers/sensors/ms5611.c index 506d05eaca6c3..fad5e681701d8 100644 --- a/drivers/sensors/ms5611.c +++ b/drivers/sensors/ms5611.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -97,7 +98,7 @@ struct ms5611_dev_s unsigned long interval; /* Polling interval */ bool enabled; /* Enable/Disable MS5611 */ sem_t run; /* Locks measure cycle */ - sem_t exclsem; /* Manages exclusive to device */ + mutex_t lock; /* Manages exclusive to device */ }; /**************************************************************************** @@ -261,7 +262,7 @@ static inline void baro_measure_read(FAR struct ms5611_dev_s *priv, /* Enforce exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return; @@ -341,9 +342,9 @@ static inline void baro_measure_read(FAR struct ms5611_dev_s *priv, (uint32_t) buffer[1] << 8 | (uint32_t) buffer[2]; - /* Release the semaphore */ + /* Release the mutex */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* Compensate the temp/press with calibration data */ @@ -657,7 +658,7 @@ int ms5611_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr) priv->interval = 1000000; /* Default interval 1s */ nxsem_init(&priv->run, 0, 0); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->sensor_lower.ops = &g_sensor_ops; priv->sensor_lower.type = SENSOR_TYPE_BAROMETER; diff --git a/drivers/sensors/qencoder.c b/drivers/sensors/qencoder.c index 34acbebecf2b6..3dbe1a2077c64 100644 --- a/drivers/sensors/qencoder.c +++ b/drivers/sensors/qencoder.c @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include @@ -60,7 +60,7 @@ struct qe_upperhalf_s { uint8_t crefs; /* The number of times the device has been opened */ - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ FAR struct qe_lowerhalf_s *lower; /* lower-half state */ }; @@ -117,7 +117,7 @@ static int qe_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -134,7 +134,7 @@ static int qe_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Check if this is the first time that the driver has been opened. */ @@ -151,7 +151,7 @@ static int qe_open(FAR struct file *filep) ret = lower->ops->setup(lower); if (ret < 0) { - goto errout_with_sem; + goto errout_with_lock; } } @@ -160,8 +160,8 @@ static int qe_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&upper->lock); errout: return ret; @@ -185,7 +185,7 @@ static int qe_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -215,7 +215,7 @@ static int qe_close(FAR struct file *filep) lower->ops->shutdown(lower); } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); ret = OK; errout: @@ -280,7 +280,7 @@ static int qe_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -362,7 +362,7 @@ static int qe_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -409,7 +409,7 @@ int qe_register(FAR const char *devpath, FAR struct qe_lowerhalf_s *lower) * (it was already zeroed by kmm_zalloc()) */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); upper->lower = lower; /* Register the QEncoder device */ diff --git a/drivers/sensors/scd30.c b/drivers/sensors/scd30.c index 6c273a9643c58..146c0048d52dd 100644 --- a/drivers/sensors/scd30.c +++ b/drivers/sensors/scd30.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -98,7 +99,7 @@ struct scd30_dev_s #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS int16_t crefs; /* Number of open references */ #endif - sem_t devsem; + mutex_t devlock; uint16_t pressure_comp; /* Pressure compensation in mbar (non-zero * value overrides altitude compensation). */ uint16_t altitude_comp; /* Altitude compensation in meters */ @@ -601,7 +602,7 @@ static int scd30_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -622,7 +623,7 @@ static int scd30_open(FAR struct file *filep) priv->crefs--; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -644,7 +645,7 @@ static int scd30_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -661,12 +662,12 @@ static int scd30_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -691,7 +692,7 @@ static ssize_t scd30_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return (ssize_t)ret; @@ -704,7 +705,7 @@ static ssize_t scd30_read(FAR struct file *filep, FAR char *buffer, * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -738,7 +739,7 @@ static ssize_t scd30_read(FAR struct file *filep, FAR char *buffer, } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return length; } @@ -765,7 +766,7 @@ static int scd30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -778,7 +779,7 @@ static int scd30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -965,7 +966,7 @@ static int scd30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -984,7 +985,7 @@ static int scd30_unlink(FAR struct inode *inode) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -994,7 +995,7 @@ static int scd30_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } @@ -1004,7 +1005,7 @@ static int scd30_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -1059,7 +1060,7 @@ int scd30_register_i2c(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->altitude_comp = SCD30_DEFAULT_ALTITUDE_COMPENSATION; priv->temperature_offset = SCD30_DEFAULT_TEMPERATURE_OFFSET; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); /* Register the character driver */ diff --git a/drivers/sensors/scd41.c b/drivers/sensors/scd41.c index 1009a765a2c3c..0fd7d1185e9b5 100644 --- a/drivers/sensors/scd41.c +++ b/drivers/sensors/scd41.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -108,7 +109,7 @@ struct scd41_dev_s #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS int16_t crefs; /* Number of open references */ #endif - sem_t devsem; + mutex_t devlock; uint16_t pressure_comp; /* Pressure compensation in mbar (non-zero * value overrides altitude compensation). */ uint16_t altitude_comp; /* Altitude compensation in meters */ @@ -581,7 +582,7 @@ static int scd41_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -602,7 +603,7 @@ static int scd41_open(FAR struct file *filep) priv->crefs--; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -624,7 +625,7 @@ static int scd41_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -641,12 +642,12 @@ static int scd41_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -671,7 +672,7 @@ static ssize_t scd41_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return (ssize_t)ret; @@ -684,7 +685,7 @@ static ssize_t scd41_read(FAR struct file *filep, FAR char *buffer, * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -718,7 +719,7 @@ static ssize_t scd41_read(FAR struct file *filep, FAR char *buffer, } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return length; } @@ -745,7 +746,7 @@ static int scd41_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -758,7 +759,7 @@ static int scd41_ioctl(FAR struct file *filep, int cmd, unsigned long arg) * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -922,7 +923,7 @@ static int scd41_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -941,7 +942,7 @@ static int scd41_unlink(FAR struct inode *inode) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -951,7 +952,7 @@ static int scd41_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } @@ -961,7 +962,7 @@ static int scd41_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -1012,7 +1013,7 @@ int scd41_register_i2c(FAR const char *devpath, FAR struct i2c_master_s *i2c) priv->altitude_comp = SCD41_DEFAULT_ALTITUDE_COMPENSATION; priv->temperature_offset = SCD41_DEFAULT_TEMPERATURE_OFFSET; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); /* Register the character driver */ diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c index c0c55e626085b..a27ab86adf1d2 100644 --- a/drivers/sensors/sensor.c +++ b/drivers/sensors/sensor.c @@ -520,7 +520,7 @@ static int sensor_open(FAR struct file *filep) if (user == NULL) { ret = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } if (lower->ops->open) @@ -577,7 +577,7 @@ static int sensor_open(FAR struct file *filep) sensor_pollnotify(upper, POLLPRI); filep->f_priv = user; - goto errout_with_sem; + goto errout_with_lock; errout_with_open: if (lower->ops->close) @@ -587,7 +587,7 @@ static int sensor_open(FAR struct file *filep) errout_with_user: kmm_free(user); -errout_with_sem: +errout_with_lock: nxrmutex_unlock(&upper->lock); return ret; } diff --git a/drivers/sensors/sgp30.c b/drivers/sensors/sgp30.c index ab6cf1fb82856..a13d4564f49dd 100644 --- a/drivers/sensors/sgp30.c +++ b/drivers/sensors/sgp30.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_SGP30) @@ -75,7 +76,7 @@ struct sgp30_dev_s int16_t crefs; /* Number of open references */ #endif struct timespec last_update; - sem_t devsem; + mutex_t devlock; }; struct sgp30_word_s @@ -558,7 +559,7 @@ static int sgp30_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -651,7 +652,7 @@ static int sgp30_open(FAR struct file *filep) } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } #endif @@ -673,7 +674,7 @@ static int sgp30_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -690,12 +691,12 @@ static int sgp30_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -717,7 +718,7 @@ static ssize_t sgp30_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -730,7 +731,7 @@ static ssize_t sgp30_read(FAR struct file *filep, FAR char *buffer, * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -745,7 +746,7 @@ static ssize_t sgp30_read(FAR struct file *filep, FAR char *buffer, { if (filep->f_oflags & O_NONBLOCK) { - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -EAGAIN; } @@ -762,7 +763,7 @@ static ssize_t sgp30_read(FAR struct file *filep, FAR char *buffer, ret = nxsig_nanosleep(&ts_sleep, NULL); if (ret == -EINTR) { - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -EINTR; } } @@ -789,7 +790,7 @@ static ssize_t sgp30_read(FAR struct file *filep, FAR char *buffer, priv->last_update = ts; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return length; } @@ -816,7 +817,7 @@ static int sgp30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -829,7 +830,7 @@ static int sgp30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -972,7 +973,7 @@ static int sgp30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -991,7 +992,7 @@ static int sgp30_unlink(FAR struct inode *inode) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -1001,7 +1002,7 @@ static int sgp30_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } @@ -1011,7 +1012,7 @@ static int sgp30_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -1060,7 +1061,7 @@ int sgp30_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->i2c = i2c; priv->addr = addr; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); /* Register the character driver */ diff --git a/drivers/sensors/sht21.c b/drivers/sensors/sht21.c index 75f010cd84c5e..025278a1749ae 100644 --- a/drivers/sensors/sht21.c +++ b/drivers/sensors/sht21.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -78,7 +79,7 @@ struct sht21_dev_s #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS int16_t crefs; /* Number of open references */ #endif - sem_t devsem; + mutex_t devlock; }; /**************************************************************************** @@ -371,7 +372,7 @@ static int sht21_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -382,7 +383,7 @@ static int sht21_open(FAR struct file *filep) priv->crefs++; DEBUGASSERT(priv->crefs > 0); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -404,7 +405,7 @@ static int sht21_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -421,12 +422,12 @@ static int sht21_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -447,7 +448,7 @@ static ssize_t sht21_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return (ssize_t)ret; @@ -460,7 +461,7 @@ static ssize_t sht21_read(FAR struct file *filep, FAR char *buffer, * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -481,7 +482,7 @@ static ssize_t sht21_read(FAR struct file *filep, FAR char *buffer, } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return length; } @@ -507,7 +508,7 @@ static int sht21_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -520,7 +521,7 @@ static int sht21_ioctl(FAR struct file *filep, int cmd, unsigned long arg) * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -574,7 +575,7 @@ static int sht21_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -593,7 +594,7 @@ static int sht21_unlink(FAR struct inode *inode) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -603,7 +604,7 @@ static int sht21_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } @@ -613,7 +614,7 @@ static int sht21_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -661,7 +662,7 @@ int sht21_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->i2c = i2c; priv->addr = addr; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); /* Register the character driver */ diff --git a/drivers/sensors/sht3x.c b/drivers/sensors/sht3x.c index 9432b82f7bf62..f56d9db3151e4 100644 --- a/drivers/sensors/sht3x.c +++ b/drivers/sensors/sht3x.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -111,7 +112,7 @@ struct sht3x_dev_s #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS int16_t crefs; /* Number of open references */ #endif - sem_t devsem; + mutex_t devlock; }; struct sht3x_word_s @@ -439,7 +440,7 @@ static int sht3x_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -450,7 +451,7 @@ static int sht3x_open(FAR struct file *filep) priv->crefs++; DEBUGASSERT(priv->crefs > 0); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -472,7 +473,7 @@ static int sht3x_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -489,12 +490,12 @@ static int sht3x_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -533,7 +534,7 @@ static int sht3x_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -546,7 +547,7 @@ static int sht3x_ioctl(FAR struct file *filep, int cmd, unsigned long arg) * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return -ENODEV; } #endif @@ -593,7 +594,7 @@ static int sht3x_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -612,7 +613,7 @@ static int sht3x_unlink(FAR struct inode *inode) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -622,7 +623,7 @@ static int sht3x_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } @@ -632,7 +633,7 @@ static int sht3x_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } #endif @@ -678,7 +679,7 @@ int sht3x_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->i2c = i2c; priv->addr = addr; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); /* Register the character driver */ diff --git a/drivers/sensors/sps30.c b/drivers/sensors/sps30.c index 2d870537e11c1..d9588a9647983 100644 --- a/drivers/sensors/sps30.c +++ b/drivers/sensors/sps30.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -92,7 +93,7 @@ struct sps30_dev_s #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS int16_t crefs; /* Number of open references */ #endif - sem_t devsem; + mutex_t devlock; /* Cached sensor values */ @@ -663,7 +664,7 @@ static int sps30_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -712,7 +713,7 @@ static int sps30_open(FAR struct file *filep) priv->crefs--; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return ret; } #endif @@ -734,7 +735,7 @@ static int sps30_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -751,12 +752,12 @@ static int sps30_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return OK; } #endif @@ -777,7 +778,7 @@ static ssize_t sps30_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -790,7 +791,7 @@ static ssize_t sps30_read(FAR struct file *filep, FAR char *buffer, * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return -ENODEV; } #endif @@ -839,7 +840,7 @@ static ssize_t sps30_read(FAR struct file *filep, FAR char *buffer, } } - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return length; } @@ -866,7 +867,7 @@ static int sps30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -879,7 +880,7 @@ static int sps30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) * sensor use on hot swappable I2C bus. */ - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return -ENODEV; } #endif @@ -983,7 +984,7 @@ static int sps30_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -1002,7 +1003,7 @@ static int sps30_unlink(FAR struct inode *inode) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -1012,7 +1013,7 @@ static int sps30_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); return OK; } @@ -1022,7 +1023,7 @@ static int sps30_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->devsem); + nxmutex_unlock(&dev->devlock); return OK; } #endif @@ -1073,7 +1074,7 @@ int sps30_register_i2c(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->addr = addr; priv->started = false; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); /* Register the character driver */ diff --git a/drivers/sensors/t67xx.c b/drivers/sensors/t67xx.c index 6535d597f65b8..50395c0112057 100644 --- a/drivers/sensors/t67xx.c +++ b/drivers/sensors/t67xx.c @@ -109,7 +109,7 @@ struct t67xx_dev_s FAR struct i2c_master_s *i2c; /* I2C interface */ uint8_t addr; /* I2C address */ struct timespec boot_time; /* When sensor was booted */ - sem_t devsem; + mutex_t devlock; }; /**************************************************************************** @@ -574,7 +574,7 @@ static ssize_t t67xx_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -599,7 +599,7 @@ static ssize_t t67xx_read(FAR struct file *filep, FAR char *buffer, if (ret < 0) { snerr("ERROR: t67xx_read_gas_ppm failed: %d\n", ret); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return (ssize_t)ret; } @@ -608,7 +608,7 @@ static ssize_t t67xx_read(FAR struct file *filep, FAR char *buffer, *ptr++ = gas_ppm; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return nsamples * sizeof(struct t67xx_value_s); } @@ -634,7 +634,7 @@ static int t67xx_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait_uninterruptible(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -675,7 +675,7 @@ static int t67xx_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -721,7 +721,7 @@ int t67xx_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, priv->i2c = i2c; priv->addr = addr; - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); clock_systime_timespec(&priv->boot_time); diff --git a/drivers/sensors/usensor.c b/drivers/sensors/usensor.c index a011b9a7f2c99..3974abd31faa3 100644 --- a/drivers/sensors/usensor.c +++ b/drivers/sensors/usensor.c @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include /**************************************************************************** @@ -56,7 +56,7 @@ static int usensor_ioctl(FAR struct file *filep, int cmd, struct usensor_context_s { - sem_t exclsem; /* Manages exclusive access to file operations */ + mutex_t lock; /* Manages exclusive access to file operations */ struct list_node list; /* List of node registered */ }; @@ -114,14 +114,14 @@ static int usensor_register(FAR struct usensor_context_s *usensor, goto errout_with_lower; } - ret = nxsem_wait(&usensor->exclsem); + ret = nxmutex_lock(&usensor->lock); if (ret < 0) { goto errout_with_register; } list_add_tail(&usensor->list, &lower->node); - nxsem_post(&usensor->exclsem); + nxmutex_unlock(&usensor->lock); return ret; @@ -138,7 +138,7 @@ static int usensor_unregister(FAR struct usensor_context_s *usensor, FAR struct usensor_lowerhalf_s *lower; int ret; - ret = nxsem_wait(&usensor->exclsem); + ret = nxmutex_lock(&usensor->lock); if (ret < 0) { return ret; @@ -150,14 +150,14 @@ static int usensor_unregister(FAR struct usensor_context_s *usensor, if (strcmp(path, lower->path) == 0) { list_delete(&lower->node); - nxsem_post(&usensor->exclsem); + nxmutex_unlock(&usensor->lock); sensor_custom_unregister(&lower->driver, path); kmm_free(lower); return 0; } } - nxsem_post(&usensor->exclsem); + nxmutex_unlock(&usensor->lock); return -ENOENT; } @@ -232,19 +232,19 @@ int usensor_initialize(void) return -ENOMEM; } - nxsem_init(&usensor->exclsem, 0, 1); + nxmutex_init(&usensor->lock); list_initialize(&usensor->list); ret = register_driver(USENSOR_PATH, &g_usensor_fops, 0666, usensor); if (ret < 0) { - goto errout_with_sem; + goto errout_with_lock; } return ret; -errout_with_sem: - nxsem_destroy(&usensor->exclsem); +errout_with_lock: + nxmutex_destroy(&usensor->lock); kmm_free(usensor); return ret; } diff --git a/drivers/sensors/xen1210.c b/drivers/sensors/xen1210.c index 8ac5873a705bc..a05e82dff1f4d 100644 --- a/drivers/sensors/xen1210.c +++ b/drivers/sensors/xen1210.c @@ -130,7 +130,7 @@ static ssize_t xen1210_read(FAR struct file *filep, FAR char *buffer, /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { /* This should only happen if the wait was canceled by an signal */ @@ -147,7 +147,7 @@ static ssize_t xen1210_read(FAR struct file *filep, FAR char *buffer, buffer = (FAR char *) &priv->sample; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return sizeof(struct xen1210_sample_s); } @@ -258,7 +258,7 @@ XEN1210_HANDLE xen1210_instantiate(FAR struct spi_dev_s *dev, /* Initialize the device state structure */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); priv->config = config; priv->spi = dev; @@ -332,7 +332,7 @@ int xen1210_register(XEN1210_HANDLE handle, int minor) /* Get exclusive access to the device structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { snerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -346,14 +346,14 @@ int xen1210_register(XEN1210_HANDLE handle, int minor) if (ret < 0) { snerr("ERROR: Failed to register driver %s: %d\n", devname, ret); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } /* Indicate that the accelerometer was successfully initialized */ priv->status |= XEN1210_STAT_INITIALIZED; /* Accelerometer is initialized */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } diff --git a/drivers/sensors/xen1210.h b/drivers/sensors/xen1210.h index be71f231793e0..fcf5063fb4234 100644 --- a/drivers/sensors/xen1210.h +++ b/drivers/sensors/xen1210.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -72,7 +73,7 @@ struct xen1210_dev_s /* Common fields */ FAR struct xen1210_config_s *config; /* Board configuration data */ - sem_t exclsem; /* Manages exclusive access to this structure */ + mutex_t lock; /* Manages exclusive access to this structure */ FAR struct spi_dev_s *spi; /* Saved SPI driver instance */ uint8_t status; /* See XEN1210_STAT_* definitions */ diff --git a/drivers/sensors/zerocross.c b/drivers/sensors/zerocross.c index 5b733f7d3ab61..6a43bfebbe778 100644 --- a/drivers/sensors/zerocross.c +++ b/drivers/sensors/zerocross.c @@ -39,7 +39,7 @@ #include #include #include -#include +#include #include #include @@ -55,7 +55,7 @@ struct zc_upperhalf_s { FAR struct zc_lowerhalf_s *lower; /* lower-half state */ - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ /* The following is a singly linked list of open references to the * zero cross device. @@ -211,7 +211,7 @@ static int zc_open(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { snerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -225,7 +225,7 @@ static int zc_open(FAR struct file *filep) { snerr("ERROR: Failed to allocate open structure\n"); ret = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } /* Attach the open structure to the device */ @@ -238,8 +238,8 @@ static int zc_open(FAR struct file *filep) filep->f_priv = (FAR void *)opriv; ret = OK; -errout_with_sem: - nxsem_post(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); return ret; } @@ -292,7 +292,7 @@ static int zc_close(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { snerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -310,7 +310,7 @@ static int zc_close(FAR struct file *filep) { snerr("ERROR: Failed to find open entry\n"); ret = -ENOENT; - goto errout_with_exclsem; + goto errout_with_lock; } /* Remove the structure from the device */ @@ -337,8 +337,8 @@ static int zc_close(FAR struct file *filep) zerocross_enable(priv); ret = OK; -errout_with_exclsem: - nxsem_post(&priv->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); return ret; } @@ -398,7 +398,7 @@ static int zc_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -446,7 +446,7 @@ static int zc_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -501,7 +501,7 @@ int zc_register(FAR const char *devname, FAR struct zc_lowerhalf_s *lower) /* Initialize the new zero cross driver instance */ priv->lower = lower; - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* And register the zero cross driver */ @@ -509,7 +509,7 @@ int zc_register(FAR const char *devname, FAR struct zc_lowerhalf_s *lower) if (ret < 0) { snerr("ERROR: register_driver failed: %d\n", ret); - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); } diff --git a/drivers/serial/ptmx.c b/drivers/serial/ptmx.c index 05548df46c506..e8a245168b983 100644 --- a/drivers/serial/ptmx.c +++ b/drivers/serial/ptmx.c @@ -35,6 +35,7 @@ #include #include +#include #include #include "pty.h" @@ -63,7 +64,7 @@ struct ptmx_dev_s { uint8_t px_next; /* Next minor number to allocate */ - sem_t px_exclsem; /* Supports mutual exclusion */ + mutex_t px_lock; /* Supports mutual exclusion */ uint32_t px_alloctab[INDEX_MAX]; /* Set of allocated PTYs */ }; @@ -108,7 +109,7 @@ static struct ptmx_dev_s g_ptmx; * Allocate a new unique PTY minor number. * * Assumptions: - * Caller hold the px_exclsem + * Caller hold the px_lock * ****************************************************************************/ @@ -173,7 +174,7 @@ static int ptmx_open(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait(&g_ptmx.px_exclsem); + ret = nxmutex_lock(&g_ptmx.px_lock); if (ret < 0) { return ret; @@ -185,7 +186,7 @@ static int ptmx_open(FAR struct file *filep) if (minor < 0) { ret = minor; - goto errout_with_sem; + goto errout_with_lock; } /* Create the master slave pair. This should create: @@ -222,14 +223,14 @@ static int ptmx_open(FAR struct file *filep) ret = unregister_driver(devname); DEBUGASSERT(ret >= 0 || ret == -EBUSY); /* unregister_driver() should never fail */ - nxsem_post(&g_ptmx.px_exclsem); + nxmutex_unlock(&g_ptmx.px_lock); return OK; errout_with_minor: ptmx_minor_free(minor); -errout_with_sem: - nxsem_post(&g_ptmx.px_exclsem); +errout_with_lock: + nxmutex_unlock(&g_ptmx.px_lock); return ret; } @@ -276,7 +277,7 @@ int ptmx_register(void) { /* Initialize driver state */ - nxsem_init(&g_ptmx.px_exclsem, 0, 1); + nxmutex_init(&g_ptmx.px_lock); /* Register the PTMX driver */ @@ -290,7 +291,7 @@ int ptmx_register(void) * De-allocate a PTY minor number. * * Assumptions: - * Caller hold the px_exclsem + * Caller hold the px_lock * ****************************************************************************/ @@ -299,7 +300,7 @@ void ptmx_minor_free(uint8_t minor) int index; int bitno; - nxsem_wait_uninterruptible(&g_ptmx.px_exclsem); + nxmutex_lock(&g_ptmx.px_lock); /* Free the address by clearing the associated bit in the px_alloctab[]; */ @@ -316,5 +317,5 @@ void ptmx_minor_free(uint8_t minor) g_ptmx.px_next = minor; } - nxsem_post(&g_ptmx.px_exclsem); + nxmutex_unlock(&g_ptmx.px_lock); } diff --git a/drivers/serial/pty.c b/drivers/serial/pty.c index 1634be606352f..fc95b80813e61 100644 --- a/drivers/serial/pty.c +++ b/drivers/serial/pty.c @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -92,14 +93,13 @@ struct pty_devpair_s uint8_t pp_minor; /* Minor device number */ uint16_t pp_nopen; /* Open file count */ sem_t pp_slavesem; /* Slave lock semaphore */ - sem_t pp_exclsem; /* Mutual exclusion */ + mutex_t pp_lock; /* Mutual exclusion */ }; /**************************************************************************** * Private Function Prototypes ****************************************************************************/ -static int pty_semtake(FAR struct pty_devpair_s *devpair); static void pty_destroy(FAR struct pty_devpair_s *devpair); static int pty_pipe(FAR struct pty_devpair_s *devpair); static int pty_open(FAR struct file *filep); @@ -137,21 +137,6 @@ static const struct file_operations g_pty_fops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: pty_semtake - ****************************************************************************/ - -static int pty_semtake(FAR struct pty_devpair_s *devpair) -{ - return nxsem_wait_uninterruptible(&devpair->pp_exclsem); -} - -/**************************************************************************** - * Name: pty_semgive - ****************************************************************************/ - -#define pty_semgive(c) nxsem_post(&(c)->pp_exclsem) - /**************************************************************************** * Name: pty_destroy ****************************************************************************/ @@ -188,7 +173,7 @@ static void pty_destroy(FAR struct pty_devpair_s *devpair) /* And free the device structure */ - nxsem_destroy(&devpair->pp_exclsem); + nxmutex_destroy(&devpair->pp_lock); nxsem_destroy(&devpair->pp_slavesem); kmm_free(devpair); } @@ -250,7 +235,7 @@ static int pty_open(FAR struct file *filep) /* Get exclusive access to the device structure */ - ret = pty_semtake(devpair); + ret = nxmutex_lock(&devpair->pp_lock); if (ret < 0) { return ret; @@ -268,7 +253,7 @@ static int pty_open(FAR struct file *filep) { /* Release the exclusive access before wait */ - pty_semgive(devpair); + nxmutex_unlock(&devpair->pp_lock); /* Wait until unlocked. * We will also most certainly suspend here. @@ -288,7 +273,7 @@ static int pty_open(FAR struct file *filep) * cause suspension. */ - ret = pty_semtake(devpair); + ret = nxmutex_lock(&devpair->pp_lock); if (ret < 0) { return ret; @@ -324,7 +309,7 @@ static int pty_open(FAR struct file *filep) } } - pty_semgive(devpair); + nxmutex_unlock(&devpair->pp_lock); return ret; } @@ -347,7 +332,7 @@ static int pty_close(FAR struct file *filep) /* Get exclusive access */ - ret = pty_semtake(devpair); + ret = nxmutex_lock(&devpair->pp_lock); if (ret < 0) { return ret; @@ -394,7 +379,7 @@ static int pty_close(FAR struct file *filep) devpair->pp_nopen--; } - pty_semgive(devpair); + nxmutex_unlock(&devpair->pp_lock); return OK; } @@ -624,7 +609,7 @@ static int pty_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = pty_semtake(devpair); + ret = nxmutex_lock(&devpair->pp_lock); if (ret < 0) { return ret; @@ -788,7 +773,7 @@ static int pty_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - pty_semgive(devpair); + nxmutex_unlock(&devpair->pp_lock); return ret; } @@ -811,7 +796,7 @@ static int pty_poll(FAR struct file *filep, FAR struct pollfd *fds, dev = inode->i_private; devpair = dev->pd_devpair; - ret = pty_semtake(devpair); + ret = nxmutex_lock(&devpair->pp_lock); if (ret < 0) { return ret; @@ -880,7 +865,7 @@ static int pty_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - pty_semgive(devpair); + nxmutex_unlock(&devpair->pp_lock); return ret; } @@ -902,7 +887,7 @@ static int pty_unlink(FAR struct inode *inode) /* Get exclusive access */ - ret = pty_semtake(devpair); + ret = nxmutex_lock(&devpair->pp_lock); if (ret < 0) { return ret; @@ -922,7 +907,7 @@ static int pty_unlink(FAR struct inode *inode) return OK; } - pty_semgive(devpair); + nxmutex_unlock(&devpair->pp_lock); return OK; } #endif @@ -963,10 +948,10 @@ int pty_register2(int minor, bool susv1) return -ENOMEM; } - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&devpair->pp_slavesem, 0, 0); - nxsem_init(&devpair->pp_exclsem, 0, 1); + nxmutex_init(&devpair->pp_lock); /* The pp_slavesem semaphore is used for signaling and, hence, should not * have priority inheritance enabled. @@ -1028,7 +1013,7 @@ int pty_register2(int minor, bool susv1) unregister_driver(devname); errout_with_devpair: - nxsem_destroy(&devpair->pp_exclsem); + nxmutex_destroy(&devpair->pp_lock); nxsem_destroy(&devpair->pp_slavesem); kmm_free(devpair); return ret; diff --git a/drivers/serial/pty.h b/drivers/serial/pty.h index f4ffaefb3ca60..8526eb6772f3c 100644 --- a/drivers/serial/pty.h +++ b/drivers/serial/pty.h @@ -47,7 +47,7 @@ extern "C" * De-allocate a PTY minor number. * * Assumptions: - * Caller hold the px_exclsem + * Caller hold the px_excllock * ****************************************************************************/ diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c index b61e4b09b8029..61d7674c328b4 100644 --- a/drivers/serial/serial.c +++ b/drivers/serial/serial.c @@ -80,8 +80,6 @@ * Private Function Prototypes ****************************************************************************/ -static int uart_takesem(FAR sem_t *sem, bool errout); - /* Write support */ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, @@ -142,28 +140,6 @@ static struct work_s g_serial_work; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: uart_takesem - ****************************************************************************/ - -static int uart_takesem(FAR sem_t *sem, bool errout) -{ - if (errout) - { - return nxsem_wait(sem); - } - else - { - return nxsem_wait_uninterruptible(sem); - } -} - -/**************************************************************************** - * Name: uart_givesem - ****************************************************************************/ - -#define uart_givesem(sem) nxsem_post(sem) - /**************************************************************************** * Name: uart_putxmitchar ****************************************************************************/ @@ -258,7 +234,7 @@ static int uart_putxmitchar(FAR uart_dev_t *dev, int ch, bool oktoblock) uart_dmatxavail(dev); #endif uart_enabletxint(dev); - ret = uart_takesem(&dev->xmitsem, true); + ret = nxsem_wait(&dev->xmitsem); uart_disabletxint(dev); } @@ -406,7 +382,7 @@ static int uart_tcdrain(FAR uart_dev_t *dev, * the operation with EINTR. */ - ret = (ssize_t)uart_takesem(&dev->xmit.sem, true); + ret = nxmutex_lock(&dev->xmit.lock); if (ret >= 0) { irqstate_t flags; @@ -459,7 +435,7 @@ static int uart_tcdrain(FAR uart_dev_t *dev, uart_dmatxavail(dev); #endif uart_enabletxint(dev); - ret = uart_takesem(&dev->xmitsem, true); + ret = nxsem_wait(&dev->xmitsem); uart_disabletxint(dev); } } @@ -498,7 +474,7 @@ static int uart_tcdrain(FAR uart_dev_t *dev, } } - uart_givesem(&dev->xmit.sem); + nxmutex_unlock(&dev->xmit.lock); } if (cancelable) @@ -528,7 +504,7 @@ static int uart_open(FAR struct file *filep) * If a signal is received while we are waiting, then return EINTR. */ - ret = uart_takesem(&dev->closesem, true); + ret = nxmutex_lock(&dev->closelock); if (ret < 0) { /* A signal received while waiting for the last close operation. */ @@ -545,7 +521,7 @@ static int uart_open(FAR struct file *filep) if (dev->disconnected) { ret = -ENOTCONN; - goto errout_with_sem; + goto errout_with_lock; } #endif @@ -559,7 +535,7 @@ static int uart_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Check if this is the first time that the driver has been opened. */ @@ -580,7 +556,7 @@ static int uart_open(FAR struct file *filep) if (ret < 0) { leave_critical_section(flags); - goto errout_with_sem; + goto errout_with_lock; } } @@ -595,7 +571,7 @@ static int uart_open(FAR struct file *filep) { uart_shutdown(dev); leave_critical_section(flags); - goto errout_with_sem; + goto errout_with_lock; } #ifdef CONFIG_SERIAL_RXDMA @@ -614,8 +590,8 @@ static int uart_open(FAR struct file *filep) dev->open_count = tmp; -errout_with_sem: - uart_givesem(&dev->closesem); +errout_with_lock: + nxmutex_unlock(&dev->closelock); return ret; } @@ -642,11 +618,11 @@ static int uart_close(FAR struct file *filep) * this case. */ - uart_takesem(&dev->closesem, false); + nxmutex_lock(&dev->closelock); if (dev->open_count > 1) { dev->open_count--; - uart_givesem(&dev->closesem); + nxmutex_unlock(&dev->closelock); return OK; } @@ -688,7 +664,7 @@ static int uart_close(FAR struct file *filep) */ uart_reset_sem(dev); - uart_givesem(&dev->closesem); + nxmutex_unlock(&dev->closelock); return OK; } @@ -714,7 +690,7 @@ static ssize_t uart_read(FAR struct file *filep, /* Only one user can access rxbuf->tail at a time */ - ret = uart_takesem(&rxbuf->sem, true); + ret = nxmutex_lock(&dev->recv.lock); if (ret < 0) { /* A signal received while waiting for access to the recv.tail will @@ -945,7 +921,7 @@ static ssize_t uart_read(FAR struct file *filep, */ dev->recvwaiting = true; - ret = uart_takesem(&dev->recvsem, true); + ret = nxsem_wait(&dev->recvsem); } leave_critical_section(flags); @@ -1049,7 +1025,7 @@ static ssize_t uart_read(FAR struct file *filep, #endif #endif - uart_givesem(&dev->recv.sem); + nxmutex_unlock(&dev->recv.lock); return recvd; } @@ -1096,7 +1072,7 @@ static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, /* Only one user can access dev->xmit.head at a time */ - ret = (ssize_t)uart_takesem(&dev->xmit.sem, true); + ret = nxmutex_lock(&dev->xmit.lock); if (ret < 0) { /* A signal received while waiting for access to the xmit.head will @@ -1109,14 +1085,14 @@ static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, #ifdef CONFIG_SERIAL_REMOVABLE /* If the removable device is no longer connected, refuse to write to the - * device. This check occurs after taking the xmit.sem because the + * device. This check occurs after taking the xmit.lock because the * disconnection event might have occurred while we were waiting for * access to the transmit buffers. */ if (dev->disconnected) { - uart_givesem(&dev->xmit.sem); + nxmutex_unlock(&dev->xmit.lock); return -ENOTCONN; } #endif @@ -1228,7 +1204,7 @@ static ssize_t uart_write(FAR struct file *filep, FAR const char *buffer, uart_enabletxint(dev); } - uart_givesem(&dev->xmit.sem); + nxmutex_unlock(&dev->xmit.lock); return nwritten; } @@ -1479,7 +1455,7 @@ static int uart_poll(FAR struct file *filep, /* Are we setting up the poll? Or tearing it down? */ - ret = uart_takesem(&dev->pollsem, true); + ret = nxmutex_lock(&dev->polllock); if (ret < 0) { /* A signal received while waiting for access to the poll data @@ -1525,7 +1501,7 @@ static int uart_poll(FAR struct file *filep, */ eventset = 0; - uart_takesem(&dev->xmit.sem, false); + nxmutex_lock(&dev->xmit.lock); ndx = dev->xmit.head + 1; if (ndx >= dev->xmit.size) @@ -1538,7 +1514,7 @@ static int uart_poll(FAR struct file *filep, eventset |= POLLOUT; } - uart_givesem(&dev->xmit.sem); + nxmutex_unlock(&dev->xmit.lock); /* Check if the receive buffer is empty. * @@ -1547,13 +1523,13 @@ static int uart_poll(FAR struct file *filep, * (we probably should, but that would be a little awkward). */ - uart_takesem(&dev->recv.sem, false); + nxmutex_lock(&dev->recv.lock); if (dev->recv.head != dev->recv.tail) { eventset |= POLLIN; } - uart_givesem(&dev->recv.sem); + nxmutex_unlock(&dev->recv.lock); #ifdef CONFIG_SERIAL_REMOVABLE /* Check if a removable device has been disconnected. */ @@ -1587,7 +1563,7 @@ static int uart_poll(FAR struct file *filep, } errout: - uart_givesem(&dev->pollsem); + nxmutex_unlock(&dev->polllock); return ret; } @@ -1683,14 +1659,14 @@ int uart_register(FAR const char *path, FAR uart_dev_t *dev) } #endif - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&dev->xmit.sem, 0, 1); - nxsem_init(&dev->recv.sem, 0, 1); - nxsem_init(&dev->closesem, 0, 1); + nxmutex_init(&dev->xmit.lock); + nxmutex_init(&dev->recv.lock); + nxmutex_init(&dev->closelock); nxsem_init(&dev->xmitsem, 0, 0); nxsem_init(&dev->recvsem, 0, 0); - nxsem_init(&dev->pollsem, 0, 1); + nxmutex_init(&dev->polllock); /* The recvsem and xmitsem are used for signaling and, hence, should * not have priority inheritance enabled. @@ -1852,9 +1828,9 @@ void uart_reset_sem(FAR uart_dev_t *dev) { nxsem_reset(&dev->xmitsem, 0); nxsem_reset(&dev->recvsem, 0); - nxsem_reset(&dev->xmit.sem, 1); - nxsem_reset(&dev->recv.sem, 1); - nxsem_reset(&dev->pollsem, 1); + nxmutex_reset(&dev->xmit.lock); + nxmutex_reset(&dev->recv.lock); + nxmutex_reset(&dev->polllock); } /**************************************************************************** diff --git a/drivers/serial/uart_bth4.c b/drivers/serial/uart_bth4.c index 017a294908375..d0f28393e5468 100644 --- a/drivers/serial/uart_bth4.c +++ b/drivers/serial/uart_bth4.c @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -57,7 +58,7 @@ struct uart_bth4_s uint8_t sendbuf[CONFIG_UART_BTH4_TXBUFSIZE]; size_t sendlen; - sem_t sendlock; + mutex_t sendlock; FAR struct pollfd *fds[CONFIG_UART_BTH4_NPOLLWAITERS]; }; @@ -239,7 +240,7 @@ static ssize_t uart_bth4_write(FAR struct file *filep, size_t hdrlen; int ret; - ret = nxsem_wait_uninterruptible(&dev->sendlock); + ret = nxmutex_lock(&dev->sendlock); if (ret < 0) { return ret; @@ -329,7 +330,7 @@ static ssize_t uart_bth4_write(FAR struct file *filep, err: dev->sendlen = 0; out: - nxsem_post(&dev->sendlock); + nxmutex_unlock(&dev->sendlock); return ret < 0 ? ret : buflen; } @@ -435,7 +436,7 @@ int uart_bth4_register(FAR const char *path, FAR struct bt_driver_s *drv) drv->receive = uart_bth4_receive; drv->priv = dev; - nxsem_init(&dev->sendlock, 0, 1); + nxmutex_init(&dev->sendlock); nxsem_init(&dev->recvsem, 0, 0); nxsem_set_protocol(&dev->recvsem, SEM_PRIO_NONE); @@ -443,7 +444,7 @@ int uart_bth4_register(FAR const char *path, FAR struct bt_driver_s *drv) ret = register_driver(path, &g_uart_bth4_ops, 0666, dev); if (ret < 0) { - nxsem_destroy(&dev->sendlock); + nxmutex_destroy(&dev->sendlock); nxsem_destroy(&dev->recvsem); circbuf_uninit(&dev->circbuf); kmm_free(dev); diff --git a/drivers/serial/uart_rpmsg.c b/drivers/serial/uart_rpmsg.c index ad8d4bef87403..566cd119f889a 100644 --- a/drivers/serial/uart_rpmsg.c +++ b/drivers/serial/uart_rpmsg.c @@ -74,7 +74,7 @@ begin_packed_struct struct uart_rpmsg_wakeup_s struct uart_rpmsg_priv_s { struct rpmsg_endpoint ept; - mutex_t mutex; + mutex_t lock; FAR const char *devname; FAR const char *cpuname; FAR void *recv_data; @@ -260,14 +260,14 @@ static void uart_rpmsg_dmatxavail(FAR struct uart_dev_s *dev) { FAR struct uart_rpmsg_priv_s *priv = dev->priv; - nxmutex_lock(&priv->mutex); + nxmutex_lock(&priv->lock); if (is_rpmsg_ept_ready(&priv->ept) && dev->dmatx.length == 0) { uart_xmitchars_dma(dev); } - nxmutex_unlock(&priv->mutex); + nxmutex_unlock(&priv->lock); } static void uart_rpmsg_send(FAR struct uart_dev_s *dev, int ch) @@ -354,7 +354,7 @@ static int uart_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept, FAR void *data, { /* Get write-cmd response, this tell how many data have sent */ - nxmutex_lock(&priv->mutex); + nxmutex_lock(&priv->lock); dev->dmatx.nbytes = header->result; if (header->result < 0) @@ -364,7 +364,7 @@ static int uart_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept, FAR void *data, uart_xmitchars_done(dev); - nxmutex_unlock(&priv->mutex); + nxmutex_unlock(&priv->lock); /* If have sent some data succeed, then continue send */ @@ -450,7 +450,7 @@ int uart_rpmsg_init(FAR const char *cpuname, FAR const char *devname, goto fail; } - nxmutex_init(&priv->mutex); + nxmutex_init(&priv->lock); sprintf(dev_name, "%s%s", UART_RPMSG_DEV_PREFIX, devname); uart_register(dev_name, dev); diff --git a/drivers/spi/spi_bitbang.c b/drivers/spi/spi_bitbang.c index 48f102b8a4ae9..80bf1af74b7d9 100644 --- a/drivers/spi/spi_bitbang.c +++ b/drivers/spi/spi_bitbang.c @@ -33,7 +33,7 @@ #include #include -#include +#include #ifdef CONFIG_SPI_BITBANG @@ -161,11 +161,11 @@ static int spi_lock(FAR struct spi_dev_s *dev, bool lock) spiinfo("lock=%d\n", lock); if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -539,7 +539,7 @@ FAR struct spi_dev_s *spi_create_bitbang(FAR const struct priv->nbits = 8; #endif - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Select an initial state of mode 0, 8-bits, and 400KHz */ diff --git a/drivers/spi/spi_driver.c b/drivers/spi/spi_driver.c index f2b2dff8ffaff..46b3fd6d769e1 100644 --- a/drivers/spi/spi_driver.c +++ b/drivers/spi/spi_driver.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include #ifdef CONFIG_SPI_DRIVER @@ -58,7 +58,7 @@ struct spi_driver_s { FAR struct spi_dev_s *spi; /* Contained SPI lower half driver */ #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - sem_t exclsem; /* Mutual exclusion */ + mutex_t lock; /* Mutual exclusion */ int16_t crefs; /* Number of open references */ bool unlinked; /* True, driver has been unlinked */ #endif @@ -130,7 +130,7 @@ static int spidrvr_open(FAR struct file *filep) /* Get exclusive access to the SPI driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -141,7 +141,7 @@ static int spidrvr_open(FAR struct file *filep) priv->crefs++; DEBUGASSERT(priv->crefs > 0); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -167,7 +167,7 @@ static int spidrvr_close(FAR struct file *filep) /* Get exclusive access to the SPI driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -184,12 +184,12 @@ static int spidrvr_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return OK; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -238,7 +238,7 @@ static int spidrvr_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the SPI driver state structure */ #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -274,7 +274,7 @@ static int spidrvr_ioctl(FAR struct file *filep, int cmd, unsigned long arg) } #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); #endif return ret; } @@ -296,7 +296,7 @@ static int spidrvr_unlink(FAR struct inode *inode) /* Get exclusive access to the SPI driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -306,7 +306,7 @@ static int spidrvr_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); return OK; } @@ -316,7 +316,7 @@ static int spidrvr_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -366,7 +366,7 @@ int spi_register(FAR struct spi_dev_s *spi, int bus) priv->spi = spi; #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #endif /* Create the character device name */ diff --git a/drivers/spi/spi_slave_driver.c b/drivers/spi/spi_slave_driver.c index 914991ff21204..7d9501add8c47 100644 --- a/drivers/spi/spi_slave_driver.c +++ b/drivers/spi/spi_slave_driver.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -78,7 +79,7 @@ struct spi_slave_driver_s uint8_t tx_buffer[CONFIG_SPI_SLAVE_DRIVER_BUFFER_SIZE]; uint32_t tx_length; /* Location of next TX value */ #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - sem_t exclsem; /* Mutual exclusion */ + mutex_t lock; /* Mutual exclusion */ int16_t crefs; /* Number of open references */ bool unlinked; /* Indicates if the driver has been unlinked */ #endif @@ -179,7 +180,7 @@ static int spi_slave_open(FAR struct file *filep) /* Get exclusive access to the SPI Slave driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { spierr("Failed to get exclusive access to the driver: %d\n", ret); @@ -191,7 +192,7 @@ static int spi_slave_open(FAR struct file *filep) priv->crefs++; DEBUGASSERT(priv->crefs > 0); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -231,7 +232,7 @@ static int spi_slave_close(FAR struct file *filep) /* Get exclusive access to the SPI Slave driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { spierr("Failed to get exclusive access to the driver: %d\n", ret); @@ -249,13 +250,13 @@ static int spi_slave_close(FAR struct file *filep) if (priv->crefs <= 0 && priv->unlinked) { - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); inode->i_private = NULL; return OK; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } #endif @@ -391,7 +392,7 @@ static int spi_slave_unlink(FAR struct inode *inode) /* Get exclusive access to the SPI Slave driver state structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { spierr("Failed to get exclusive access to the driver: %d\n", ret); @@ -402,7 +403,7 @@ static int spi_slave_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); kmm_free(priv); inode->i_private = NULL; return OK; @@ -413,7 +414,7 @@ static int spi_slave_unlink(FAR struct inode *inode) */ priv->unlinked = true; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif @@ -604,7 +605,7 @@ int spi_slave_register(FAR struct spi_slave_ctrlr_s *ctrlr, int bus) #endif #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #endif /* Create the character device name */ diff --git a/drivers/syslog/ramlog.c b/drivers/syslog/ramlog.c index e1efd4277018a..0a5f3083db475 100644 --- a/drivers/syslog/ramlog.c +++ b/drivers/syslog/ramlog.c @@ -41,6 +41,7 @@ #include #include +#include #include #include #include @@ -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); diff --git a/drivers/syslog/syslog_device.c b/drivers/syslog/syslog_device.c index d52ae7bb31f8c..f2617b43f9967 100644 --- a/drivers/syslog/syslog_device.c +++ b/drivers/syslog/syslog_device.c @@ -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. */ diff --git a/drivers/timers/capture.c b/drivers/timers/capture.c index 379a5eaed9f07..43e0c0c8e2b35 100644 --- a/drivers/timers/capture.c +++ b/drivers/timers/capture.c @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include @@ -60,7 +60,7 @@ struct cap_upperhalf_s { uint8_t crefs; /* The number of times the device has been opened */ - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ FAR struct cap_lowerhalf_s *lower; /* lower-half state */ }; @@ -115,7 +115,7 @@ static int cap_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout; @@ -132,7 +132,7 @@ static int cap_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Check if this is the first time that the driver has been opened. */ @@ -148,7 +148,7 @@ static int cap_open(FAR struct file *filep) ret = lower->ops->start(lower); if (ret < 0) { - goto errout_with_sem; + goto errout_with_lock; } } @@ -157,8 +157,8 @@ static int cap_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); errout: return ret; @@ -180,7 +180,7 @@ static int cap_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto errout; @@ -209,7 +209,7 @@ static int cap_close(FAR struct file *filep) lower->ops->stop(lower); } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&priv->lock); ret = OK; errout: @@ -274,7 +274,7 @@ static int cap_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -319,7 +319,7 @@ static int cap_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -365,7 +365,7 @@ int cap_register(FAR const char *devpath, FAR struct cap_lowerhalf_s *lower) * (it was already zeroed by kmm_zalloc()) */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); upper->lower = lower; /* Register the PWM Capture device */ diff --git a/drivers/timers/oneshot.c b/drivers/timers/oneshot.c index 6e33c611918a4..c4b20ab2446b9 100644 --- a/drivers/timers/oneshot.c +++ b/drivers/timers/oneshot.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #ifdef CONFIG_ONESHOT @@ -48,7 +48,7 @@ struct oneshot_dev_s { FAR struct oneshot_lowerhalf_s *od_lower; /* Lower-half driver state */ - sem_t od_exclsem; /* Supports mutual exclusion */ + mutex_t od_lock; /* Supports mutual exclusion */ /* Oneshot timer expiration notification information */ @@ -169,7 +169,7 @@ static int oneshot_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->od_exclsem); + ret = nxmutex_lock(&priv->od_lock); if (ret < 0) { return ret; @@ -264,7 +264,7 @@ static int oneshot_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->od_exclsem); + nxmutex_unlock(&priv->od_lock); return ret; } @@ -316,7 +316,7 @@ int oneshot_register(FAR const char *devname, /* Initialize the new oneshot timer driver instance */ priv->od_lower = lower; - nxsem_init(&priv->od_exclsem, 0, 1); + nxmutex_init(&priv->od_lock); /* And register the oneshot timer driver */ @@ -324,7 +324,7 @@ int oneshot_register(FAR const char *devname, if (ret < 0) { tmrerr("ERROR: register_driver failed: %d\n", ret); - nxsem_destroy(&priv->od_exclsem); + nxmutex_destroy(&priv->od_lock); kmm_free(priv); } diff --git a/drivers/timers/pwm.c b/drivers/timers/pwm.c index 0efcfc53f6566..0bc942983395f 100644 --- a/drivers/timers/pwm.c +++ b/drivers/timers/pwm.c @@ -38,6 +38,7 @@ #include #include +#include #include #include #include @@ -62,7 +63,7 @@ struct pwm_upperhalf_s volatile bool waiting; /* True: Caller is waiting for the pulse * count to expire */ #endif - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ #ifdef CONFIG_PWM_PULSECOUNT sem_t waitsem; /* Used to wait for the pulse count to * expire */ @@ -159,7 +160,7 @@ static int pwm_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -176,7 +177,7 @@ static int pwm_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Check if this is the first time that the driver has been opened. */ @@ -193,7 +194,7 @@ static int pwm_open(FAR struct file *filep) ret = lower->ops->setup(lower); if (ret < 0) { - goto errout_with_sem; + goto errout_with_lock; } } @@ -202,8 +203,8 @@ static int pwm_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&upper->lock); errout: return ret; @@ -227,7 +228,7 @@ static int pwm_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -258,7 +259,7 @@ static int pwm_close(FAR struct file *filep) } ret = OK; - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); errout: return ret; @@ -429,7 +430,7 @@ static int pwm_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -551,7 +552,7 @@ static int pwm_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -602,7 +603,7 @@ int pwm_register(FAR const char *path, FAR struct pwm_lowerhalf_s *dev) * kmm_zalloc()). */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); #ifdef CONFIG_PWM_PULSECOUNT nxsem_init(&upper->waitsem, 0, 0); diff --git a/drivers/timers/rpmsg_rtc.c b/drivers/timers/rpmsg_rtc.c index bc37db5d6c483..f69c302ae0784 100644 --- a/drivers/timers/rpmsg_rtc.c +++ b/drivers/timers/rpmsg_rtc.c @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include @@ -119,7 +119,7 @@ struct rpmsg_rtc_server_s FAR struct rtc_ops_s *ops; FAR struct rtc_lowerhalf_s *lower; struct list_node list; - sem_t exclsem; + mutex_t lock; }; struct rpmsg_rtc_client_s @@ -473,7 +473,7 @@ static int rpmsg_rtc_server_settime(FAR struct rtc_lowerhalf_s *lower, ret = server->lower->ops->settime(server->lower, rtctime); if (ret >= 0) { - nxsem_wait_uninterruptible(&server->exclsem); + nxmutex_lock(&server->lock); msg.sec = timegm((FAR struct tm *)rtctime); msg.nsec = rtctime->tm_nsec; msg.header.command = RPMSG_RTC_SYNC; @@ -483,7 +483,7 @@ static int rpmsg_rtc_server_settime(FAR struct rtc_lowerhalf_s *lower, rpmsg_send(&client->ept, &msg, sizeof(msg)); } - nxsem_post(&server->exclsem); + nxmutex_unlock(&server->lock); } return ret; @@ -600,9 +600,9 @@ static void rpmsg_rtc_server_ns_unbind(FAR struct rpmsg_endpoint *ept) struct rpmsg_rtc_client_s, ept); FAR struct rpmsg_rtc_server_s *server = ept->priv; - nxsem_wait_uninterruptible(&server->exclsem); + nxmutex_lock(&server->lock); list_delete(&client->node); - nxsem_post(&server->exclsem); + nxmutex_unlock(&server->lock); rpmsg_destroy_ept(&client->ept); kmm_free(client); } @@ -738,9 +738,9 @@ static void rpmsg_rtc_server_ns_bind(FAR struct rpmsg_device *rdev, rpmsg_send(&client->ept, &msg, sizeof(msg)); } - nxsem_wait_uninterruptible(&server->exclsem); + nxmutex_lock(&server->lock); list_add_tail(&server->list, &client->node); - nxsem_post(&server->exclsem); + nxmutex_unlock(&server->lock); } #endif @@ -802,12 +802,12 @@ FAR struct rtc_lowerhalf_s *rpmsg_rtc_server_initialize( server->ops = &g_rpmsg_rtc_server_ops; server->lower = lower; list_initialize(&server->list); - nxsem_init(&server->exclsem, 0, 1); + nxmutex_init(&server->lock); if (rpmsg_register_callback(server, NULL, NULL, rpmsg_rtc_server_ns_match, rpmsg_rtc_server_ns_bind) < 0) { - nxsem_destroy(&server->exclsem); + nxmutex_destroy(&server->lock); kmm_free(server); return NULL; } diff --git a/drivers/timers/rtc.c b/drivers/timers/rtc.c index 4103f27f66770..04507b917bc24 100644 --- a/drivers/timers/rtc.c +++ b/drivers/timers/rtc.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include /**************************************************************************** @@ -55,7 +55,7 @@ struct rtc_alarminfo_s struct rtc_upperhalf_s { FAR struct rtc_lowerhalf_s *lower; /* Contained lower half driver */ - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS uint8_t crefs; /* Number of open references */ @@ -158,7 +158,7 @@ static void rtc_destroy(FAR struct rtc_upperhalf_s *upper) /* And free our container */ - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); } #endif @@ -245,7 +245,7 @@ static int rtc_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -255,7 +255,7 @@ static int rtc_open(FAR struct file *filep) upper->crefs++; DEBUGASSERT(upper->crefs > 0); - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return OK; } #endif @@ -282,7 +282,7 @@ static int rtc_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -292,7 +292,7 @@ static int rtc_close(FAR struct file *filep) DEBUGASSERT(upper->crefs > 0); upper->crefs--; - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); /* If the count has decremented to zero and the driver has been unlinked, * then commit Hara-Kiri now. @@ -349,7 +349,7 @@ static int rtc_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -746,7 +746,7 @@ static int rtc_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -769,7 +769,7 @@ static int rtc_unlink(FAR struct inode *inode) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -778,7 +778,7 @@ static int rtc_unlink(FAR struct inode *inode) /* Indicate that the driver has been unlinked */ upper->unlinked = true; - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); /* If there are no further open references to the driver, then commit * Hara-Kiri now. @@ -835,7 +835,7 @@ int rtc_initialize(int minor, FAR struct rtc_lowerhalf_s *lower) /* Initialize the upper half container */ upper->lower = lower; /* Contain lower half driver */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS upper->crefs = 0; /* No open references */ @@ -853,7 +853,7 @@ int rtc_initialize(int minor, FAR struct rtc_lowerhalf_s *lower) ret = register_driver(devpath, &rtc_fops, 0666, upper); if (ret < 0) { - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); return ret; } diff --git a/drivers/timers/timer.c b/drivers/timers/timer.c index b6054003b83fa..0abcbe0cbc0e8 100644 --- a/drivers/timers/timer.c +++ b/drivers/timers/timer.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #ifdef CONFIG_TIMER @@ -50,7 +50,7 @@ struct timer_upperhalf_s { - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ uint8_t crefs; /* The number of times the device has been opened */ FAR char *path; /* Registration path */ @@ -143,7 +143,7 @@ static int timer_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -160,7 +160,7 @@ static int timer_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Save the new open count */ @@ -168,8 +168,8 @@ static int timer_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&upper->lock); errout: return ret; @@ -193,7 +193,7 @@ static int timer_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -208,7 +208,7 @@ static int timer_close(FAR struct file *filep) upper->crefs--; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return OK; } @@ -266,7 +266,7 @@ static int timer_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -432,7 +432,7 @@ static int timer_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -490,7 +490,7 @@ FAR void *timer_register(FAR const char *path, */ upper->lower = lower; - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); /* Copy the registration path */ @@ -516,7 +516,7 @@ FAR void *timer_register(FAR const char *path, kmm_free(upper->path); errout_with_upper: - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); errout: @@ -563,7 +563,7 @@ void timer_unregister(FAR void *handle) /* Then free all of the driver resources */ - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper->path); kmm_free(upper); } diff --git a/drivers/timers/watchdog.c b/drivers/timers/watchdog.c index 7d03da9896a17..ac936583312c5 100644 --- a/drivers/timers/watchdog.c +++ b/drivers/timers/watchdog.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include #include @@ -91,7 +91,7 @@ struct watchdog_upperhalf_s #endif uint8_t crefs; /* The number of times the device has been opened */ - sem_t exclsem; /* Supports mutual exclusion */ + mutex_t lock; /* Supports mutual exclusion */ FAR char *path; /* Registration path */ /* The contained lower-half driver */ @@ -319,7 +319,7 @@ static int wdog_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -336,7 +336,7 @@ static int wdog_open(FAR struct file *filep) /* More than 255 opens; uint8_t overflows to zero */ ret = -EMFILE; - goto errout_with_sem; + goto errout_with_lock; } /* Save the new open count */ @@ -344,8 +344,8 @@ static int wdog_open(FAR struct file *filep) upper->crefs = tmp; ret = OK; -errout_with_sem: - nxsem_post(&upper->exclsem); +errout_with_lock: + nxmutex_unlock(&upper->lock); errout: return ret; @@ -369,7 +369,7 @@ static int wdog_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { goto errout; @@ -384,7 +384,7 @@ static int wdog_close(FAR struct file *filep) upper->crefs--; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); ret = OK; errout: @@ -445,7 +445,7 @@ static int wdog_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&upper->exclsem); + ret = nxmutex_lock(&upper->lock); if (ret < 0) { return ret; @@ -622,7 +622,7 @@ static int wdog_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&upper->exclsem); + nxmutex_unlock(&upper->lock); return ret; } @@ -690,7 +690,7 @@ FAR void *watchdog_register(FAR const char *path, * by kmm_zalloc()). */ - nxsem_init(&upper->exclsem, 0, 1); + nxmutex_init(&upper->lock); upper->lower = lower; /* Copy the registration path */ @@ -725,7 +725,7 @@ FAR void *watchdog_register(FAR const char *path, kmm_free(upper->path); errout_with_upper: - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); errout: @@ -777,6 +777,6 @@ void watchdog_unregister(FAR void *handle) /* Then free all of the driver resources */ kmm_free(upper->path); - nxsem_destroy(&upper->exclsem); + nxmutex_destroy(&upper->lock); kmm_free(upper); } diff --git a/drivers/usbdev/adb.c b/drivers/usbdev/adb.c index 5aa45f8ce392b..ffe1e3e709573 100644 --- a/drivers/usbdev/adb.c +++ b/drivers/usbdev/adb.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -165,10 +166,10 @@ struct usbdev_adb_s /* Char device driver */ - sem_t exclsem; /* Enforces device exclusive access */ - adb_char_waiter_sem_t *rdsems; /* List of blocking readers */ - adb_char_waiter_sem_t *wrsems; /* List of blocking writers */ - uint8_t crefs; /* Count of opened instances */ + mutex_t lock; /* Enforces device exclusive access */ + adb_char_waiter_sem_t *rdsems; /* List of blocking readers */ + adb_char_waiter_sem_t *wrsems; /* List of blocking writers */ + uint8_t crefs; /* Count of opened instances */ FAR struct pollfd *fds[CONFIG_USBADB_NPOLLWAITERS]; }; @@ -1462,7 +1463,7 @@ static int usbclass_classobject(int minor, /* Initialize the char device structure */ - nxsem_init(&alloc->dev.exclsem, 0, 1); + nxmutex_init(&alloc->dev.lock); alloc->dev.crefs = 0; /* Register char device driver */ @@ -1554,7 +1555,7 @@ static int adb_char_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1566,7 +1567,7 @@ static int adb_char_open(FAR struct file *filep) assert(priv->crefs != 0); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1586,7 +1587,7 @@ static int adb_char_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1598,7 +1599,7 @@ static int adb_char_close(FAR struct file *filep) assert(priv->crefs >= 0); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -1635,7 +1636,7 @@ static int adb_char_blocking_io(FAR struct usbdev_adb_s *priv, leave_critical_section(flags); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* Wait for USB device to notify */ @@ -1644,10 +1645,10 @@ static int adb_char_blocking_io(FAR struct usbdev_adb_s *priv, if (ret < 0) { /* Interrupted wait, unregister semaphore - * TODO ensure that exclsem wait does not fail (ECANCELED) + * TODO ensure that lock wait does not fail (ECANCELED) */ - nxsem_wait_uninterruptible(&priv->exclsem); + nxmutex_lock(&priv->lock); flags = enter_critical_section(); @@ -1670,11 +1671,11 @@ static int adb_char_blocking_io(FAR struct usbdev_adb_s *priv, } leave_critical_section(flags); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } - return nxsem_wait(&priv->exclsem); + return nxmutex_lock(&priv->lock); } /**************************************************************************** @@ -1703,7 +1704,7 @@ static ssize_t adb_char_read(FAR struct file *filep, FAR char *buffer, return -EPIPE; } - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1715,7 +1716,7 @@ static ssize_t adb_char_read(FAR struct file *filep, FAR char *buffer, { if (filep->f_oflags & O_NONBLOCK) { - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return -EAGAIN; } @@ -1737,7 +1738,7 @@ static ssize_t adb_char_read(FAR struct file *filep, FAR char *buffer, } while (sq_empty(&priv->rxpending)); - /* RX queue not empty and exclsem locked so we are the only reader */ + /* RX queue not empty and lock locked so we are the only reader */ nxsem_destroy(&sem.sem); } @@ -1798,7 +1799,7 @@ static ssize_t adb_char_read(FAR struct file *filep, FAR char *buffer, } } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return retlen; } @@ -1829,7 +1830,7 @@ static ssize_t adb_char_write(FAR struct file *filep, return -EPIPE; } - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1925,7 +1926,7 @@ static ssize_t adb_char_write(FAR struct file *filep, ret = wlen; errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1939,7 +1940,7 @@ static int adb_char_poll(FAR struct file *filep, FAR struct pollfd *fds, pollevent_t eventset; irqstate_t flags; - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2010,7 +2011,7 @@ static int adb_char_poll(FAR struct file *filep, FAR struct pollfd *fds, exit_leave_critical: leave_critical_section(flags); errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } diff --git a/drivers/usbdev/usbmsc.c b/drivers/usbdev/usbmsc.c index 22b525f4b8808..2700366e1e008 100644 --- a/drivers/usbdev/usbmsc.c +++ b/drivers/usbdev/usbmsc.c @@ -1326,10 +1326,10 @@ int usbmsc_configure(unsigned int nluns, void **handle) priv = &alloc->dev; memset(priv, 0, sizeof(struct usbmsc_dev_s)); - /* Initialize semaphores */ + /* Initialize semaphores & mutex */ nxsem_init(&priv->thsynch, 0, 0); - nxsem_init(&priv->thlock, 0, 1); + nxmutex_init(&priv->thlock); nxsem_init(&priv->thwaitsem, 0, 0); /* The thsynch and thwaitsem semaphores are used for signaling and, hence, @@ -1614,7 +1614,7 @@ int usbmsc_unbindlun(FAR void *handle, unsigned int lunno) #endif lun = &priv->luntab[lunno]; - ret = usbmsc_scsi_lock(priv); + ret = nxmutex_lock(&priv->thlock); if (ret < 0) { return ret; @@ -1635,7 +1635,7 @@ int usbmsc_unbindlun(FAR void *handle, unsigned int lunno) ret = OK; } - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); return ret; } @@ -1686,7 +1686,7 @@ int usbmsc_exportluns(FAR void *handle) * some protection against re-entrant usage. */ - ret = usbmsc_scsi_lock(priv); + ret = nxmutex_lock(&priv->thlock); if (ret < 0) { return ret; @@ -1742,7 +1742,7 @@ int usbmsc_exportluns(FAR void *handle) leave_critical_section(flags); errout_with_lock: - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); return ret; } @@ -1847,9 +1847,9 @@ void usbmsc_uninitialize(FAR void *handle) do { - ret = usbmsc_scsi_lock(priv); + ret = nxmutex_lock(&priv->thlock); - /* usbmsc_scsi_lock() will fail with ECANCELED, only + /* nxmutex_lock() will fail with ECANCELED, only * if this thread is canceled. At this point, we * have no option but to continue with the teardown. */ @@ -1870,7 +1870,7 @@ void usbmsc_uninitialize(FAR void *handle) leave_critical_section(flags); } - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); /* Wait for the thread to exit */ @@ -1915,7 +1915,7 @@ void usbmsc_uninitialize(FAR void *handle) /* Uninitialize and release the driver structure */ nxsem_destroy(&priv->thsynch); - nxsem_destroy(&priv->thlock); + nxmutex_destroy(&priv->thlock); nxsem_destroy(&priv->thwaitsem); #ifndef CONFIG_USBMSC_COMPOSITE diff --git a/drivers/usbdev/usbmsc.h b/drivers/usbdev/usbmsc.h index 434a9ddff938b..ce12644cedff4 100644 --- a/drivers/usbdev/usbmsc.h +++ b/drivers/usbdev/usbmsc.h @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -388,7 +389,7 @@ struct usbmsc_dev_s pid_t thpid; /* The worker thread task ID */ sem_t thsynch; /* Used to synchronizer terminal events */ - sem_t thlock; /* Used to get exclusive access to the state data */ + mutex_t thlock; /* Used to get exclusive access to the state data */ sem_t thwaitsem; /* Used to signal worker thread */ volatile bool thwaiting; /* True: worker thread is waiting for an event */ volatile uint8_t thstate; /* State of the worker thread */ @@ -498,26 +499,6 @@ EXTERN FAR struct usbmsc_dev_s *g_usbmsc_handoff; * Public Function Prototypes ****************************************************************************/ -/**************************************************************************** - * Name: usbmsc_scsi_lock - * - * Description: - * Get exclusive access to SCSI state data. - * - ****************************************************************************/ - -int usbmsc_scsi_lock(FAR struct usbmsc_dev_s *priv); - -/**************************************************************************** - * Name: usbmsc_scsi_unlock - * - * Description: - * Relinquish exclusive access to SCSI state data. - * - ****************************************************************************/ - -#define usbmsc_scsi_unlock(priv) nxsem_post(&priv->thlock) - /**************************************************************************** * Name: usbmsc_scsi_signal * diff --git a/drivers/usbdev/usbmsc_scsi.c b/drivers/usbdev/usbmsc_scsi.c index 218d61bd33a79..63444e8675bd8 100644 --- a/drivers/usbdev/usbmsc_scsi.c +++ b/drivers/usbdev/usbmsc_scsi.c @@ -364,7 +364,7 @@ static int usbmsc_scsi_wait(FAR struct usbmsc_dev_s *priv) /* We must hold the SCSI lock to call this function */ - DEBUGASSERT(priv->thlock.semcount < 1); + DEBUGASSERT(nxmutex_is_locked(&priv->thlock)); /* A flag is used to prevent driving up the semaphore count. This function * is called (primarily) from the SCSI work thread so we must disable @@ -378,7 +378,7 @@ static int usbmsc_scsi_wait(FAR struct usbmsc_dev_s *priv) /* Relinquish our lock on the SCSI state data */ - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); /* Now wait for a SCSI event to be signaled */ @@ -390,7 +390,7 @@ static int usbmsc_scsi_wait(FAR struct usbmsc_dev_s *priv) /* Re-acquire our lock on the SCSI state data */ - ret2 = usbmsc_scsi_lock(priv); + ret2 = nxmutex_lock(&priv->thlock); leave_critical_section(flags); return ret >= 0 ? ret2 : ret; } @@ -1925,7 +1925,7 @@ static int usbmsc_cmdparsestate(FAR struct usbmsc_dev_s *priv) /* Get exclusive access to the block driver */ - ret = usbmsc_scsi_lock(priv); + ret = nxmutex_lock(&priv->thlock); if (ret < 0) { return ret; @@ -2132,7 +2132,7 @@ static int usbmsc_cmdparsestate(FAR struct usbmsc_dev_s *priv) break; } - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); /* Is a response required? (Not for read6/10/12 and write6/10/12). */ @@ -2845,7 +2845,7 @@ int usbmsc_scsi_main(int argc, char *argv[]) /* Get exclusive access to SCSI state data */ - ret = usbmsc_scsi_lock(priv); + ret = nxmutex_lock(&priv->thlock); if (ret < 0) { return EXIT_FAILURE; @@ -2867,7 +2867,7 @@ int usbmsc_scsi_main(int argc, char *argv[]) { /* The thread has been canceled */ - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); return EXIT_FAILURE; } } @@ -2879,7 +2879,7 @@ int usbmsc_scsi_main(int argc, char *argv[]) priv->thstate = USBMSC_STATE_IDLE; eventset = priv->theventset; priv->theventset = USBMSC_EVENT_NOEVENTS; - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); /* Then loop until we are asked to terminate */ @@ -2891,7 +2891,7 @@ int usbmsc_scsi_main(int argc, char *argv[]) * handling. */ - ret = usbmsc_scsi_lock(priv); + ret = nxmutex_lock(&priv->thlock); if (ret < 0) { return EXIT_FAILURE; @@ -2906,7 +2906,7 @@ int usbmsc_scsi_main(int argc, char *argv[]) /* The thread has been canceled */ leave_critical_section(flags); - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); return EXIT_FAILURE; } } @@ -2918,7 +2918,7 @@ int usbmsc_scsi_main(int argc, char *argv[]) eventset = priv->theventset; priv->theventset = USBMSC_EVENT_NOEVENTS; - usbmsc_scsi_unlock(priv); + nxmutex_unlock(&priv->thlock); /* Were we awakened by some event that requires immediate action? * @@ -3063,16 +3063,3 @@ void usbmsc_scsi_signal(FAR struct usbmsc_dev_s *priv) leave_critical_section(flags); } - -/**************************************************************************** - * Name: usbmsc_scsi_lock - * - * Description: - * Get exclusive access to SCSI state data. - * - ****************************************************************************/ - -int usbmsc_scsi_lock(FAR struct usbmsc_dev_s *priv) -{ - return nxsem_wait_uninterruptible(&priv->thlock); -} diff --git a/drivers/usbhost/usbhost_cdcacm.c b/drivers/usbhost/usbhost_cdcacm.c index fc45e346b202e..eb1d695ed176d 100644 --- a/drivers/usbhost/usbhost_cdcacm.c +++ b/drivers/usbhost/usbhost_cdcacm.c @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include @@ -252,7 +252,7 @@ struct usbhost_cdcacm_s uint16_t rxndx; /* Index to the next byte in the RX packet buffer */ int16_t crefs; /* Reference count on the driver instance */ int16_t nbytes; /* The number of bytes actually transferred */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ + mutex_t lock; /* Used to maintain mutual exclusive access */ struct work_s ntwork; /* For asynchronous notification work */ struct work_s rxwork; /* For RX packet work */ struct work_s txwork; /* For TX packet work */ @@ -285,12 +285,6 @@ struct usbhost_freestate_s * Private Function Prototypes ****************************************************************************/ -/* Semaphores */ - -static int usbhost_takesem(FAR sem_t *sem); -static void usbhost_forcetake(FAR sem_t *sem); -#define usbhost_givesem(s) nxsem_post(s); - /* Memory allocation services */ static FAR struct usbhost_cdcacm_s *usbhost_allocclass(void); @@ -465,48 +459,6 @@ static uint32_t g_devinuse; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int usbhost_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: usbhost_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static void usbhost_forcetake(FAR sem_t *sem) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: usbhost_allocclass * @@ -1300,9 +1252,9 @@ static void usbhost_destroy(FAR void *arg) usbhost_free_buffers(priv); - /* Destroy the semaphores */ + /* Destroy the mutex */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); /* Disconnect the USB host device */ @@ -1948,11 +1900,11 @@ usbhost_create(FAR struct usbhost_hubport_s *hport, priv->crefs = 1; - /* Initialize semaphores + /* Initialize mutex * (this works okay in the interrupt context) */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Set up the serial lower-half interface */ @@ -2048,7 +2000,7 @@ static int usbhost_connect(FAR struct usbhost_class_s *usbclass, /* Get exclusive access to the device structure */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2139,7 +2091,7 @@ static int usbhost_connect(FAR struct usbhost_class_s *usbclass, * ready to handle it! */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2278,7 +2230,7 @@ static int usbhost_setup(FAR struct uart_dev_s *uartdev) /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2308,7 +2260,7 @@ static int usbhost_setup(FAR struct uart_dev_s *uartdev) } leave_critical_section(flags); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2333,7 +2285,7 @@ static void usbhost_shutdown(FAR struct uart_dev_s *uartdev) /* Decrement the reference count on the block driver */ DEBUGASSERT(priv->crefs > 1); - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); priv->crefs--; /* Release the semaphore. The following operations when crefs == 1 are @@ -2341,7 +2293,7 @@ static void usbhost_shutdown(FAR struct uart_dev_s *uartdev) * the block driver. */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* We need to disable interrupts momentarily to assure that there are * no asynchronous disconnect events. @@ -2440,7 +2392,7 @@ static int usbhost_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { /* Process the IOCTL by command */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2608,7 +2560,7 @@ static int usbhost_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } return ret; diff --git a/drivers/usbhost/usbhost_cdcmbim.c b/drivers/usbhost/usbhost_cdcmbim.c index 7d257186f3502..49e5195b26d61 100644 --- a/drivers/usbhost/usbhost_cdcmbim.c +++ b/drivers/usbhost/usbhost_cdcmbim.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -195,7 +196,7 @@ struct usbhost_cdcmbim_s uint16_t ctrlif; /* Control interface number */ uint16_t dataif; /* Data interface number */ int16_t crefs; /* Reference count on the driver instance */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ + mutex_t lock; /* Used to maintain mutual exclusive access */ struct work_s ntwork; /* Notification work */ struct work_s comm_rxwork; /* Communication interface RX work */ struct work_s bulk_rxwork; @@ -236,11 +237,6 @@ struct usbhost_cdcmbim_s * Private Function Prototypes ****************************************************************************/ -/* Semaphores */ - -static void usbhost_takesem(sem_t *sem); -#define usbhost_givesem(s) nxsem_post(s); - /* Memory allocation services */ static inline FAR struct usbhost_cdcmbim_s *usbhost_allocclass(void); @@ -435,7 +431,7 @@ static ssize_t cdcwdm_read(FAR struct file *filep, FAR char *buffer, inode = filep->f_inode; priv = inode->i_private; - usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); if (priv->disconnected) { @@ -476,7 +472,7 @@ static ssize_t cdcwdm_read(FAR struct file *filep, FAR char *buffer, } errout: - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -505,7 +501,7 @@ static ssize_t cdcwdm_write(FAR struct file *filep, FAR const char *buffer, * open and actively trying to interact with the class driver. */ - usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); ret = usbhost_ctrl_cmd(priv, USB_REQ_DIR_OUT | USB_REQ_TYPE_CLASS | @@ -513,7 +509,7 @@ static ssize_t cdcwdm_write(FAR struct file *filep, FAR const char *buffer, USB_CDC_SEND_ENCAPSULATED_COMMAND, 0, priv->ctrlif, (uint8_t *)buffer, buflen); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (ret) { @@ -548,7 +544,7 @@ static int cdcwdm_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv); - usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); if (priv->disconnected) { @@ -605,38 +601,10 @@ static int cdcwdm_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } -/**************************************************************************** - * Name: usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static void usbhost_takesem(sem_t *sem) -{ - int ret; - - do - { - /* Take the semaphore (perhaps waiting) */ - - ret = nxsem_wait(sem); - - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(ret == OK || ret == -EINTR); - } - while (ret == -EINTR); -} - /**************************************************************************** * Name: usbhost_allocclass * @@ -793,7 +761,7 @@ static void usbhost_bulkin_work(FAR void *arg) return; } - usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); if (priv->bulkinbytes < (int16_t)(sizeof(struct usb_cdc_ncm_nth16_s) + sizeof(struct usb_cdc_ncm_ndp16_s))) @@ -855,7 +823,7 @@ static void usbhost_bulkin_work(FAR void *arg) DRVR_ASYNCH(hport->drvr, priv->bulkin, (uint8_t *)priv->rxnetbuf, CDCMBIM_NETBUF_SIZE, usbhost_bulkin_callback, priv); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -900,7 +868,7 @@ static void usbhost_rxdata_work(FAR void *arg) return; } - usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); ret = usbhost_ctrl_cmd(priv, USB_REQ_DIR_IN | USB_REQ_TYPE_CLASS | @@ -933,7 +901,7 @@ static void usbhost_rxdata_work(FAR void *arg) poll_notify(priv->fds, CONFIG_USBHOST_CDCMBIM_NPOLLWAITERS, POLLIN); errout: - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -1645,7 +1613,7 @@ static inline int usbhost_devinit(FAR struct usbhost_cdcmbim_s *priv) if (ret >= 0) { - usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); DEBUGASSERT(priv->crefs >= 2); /* Handle a corner case where (1) open() has been called so the @@ -1668,7 +1636,7 @@ static inline int usbhost_devinit(FAR struct usbhost_cdcmbim_s *priv) uinfo("Successfully initialized\n"); priv->crefs--; - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } } @@ -1954,9 +1922,9 @@ static FAR struct usbhost_class_s priv->crefs = 1; - /* Initialize semaphores (this works in the interrupt context) */ + /* Initialize mutex (this works in the interrupt context) */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Return the instance of the USB class driver */ @@ -2307,7 +2275,7 @@ static int cdcmbim_txpoll(struct net_driver_s *dev) DEBUGASSERT(priv->netdev.d_buf == (FAR uint8_t *)priv->txpktbuf); - usbhost_takesem(&priv->exclsem); + nxmutex_lock(&priv->lock); if (priv->netdev.d_len > 0) { @@ -2319,7 +2287,7 @@ static int cdcmbim_txpoll(struct net_driver_s *dev) } } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return 0; } diff --git a/drivers/usbhost/usbhost_devaddr.c b/drivers/usbhost/usbhost_devaddr.c index 37cc23080372c..e677c4e40d1c9 100644 --- a/drivers/usbhost/usbhost_devaddr.c +++ b/drivers/usbhost/usbhost_devaddr.c @@ -43,22 +43,6 @@ * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usbhost_takesem and usbhost_givesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int usbhost_takesem(FAR struct usbhost_devaddr_s *devgen) -{ - return nxsem_wait_uninterruptible(&devgen->exclsem); -} - -#define usbhost_givesem(devgen) nxsem_post(&devgen->exclsem) - /**************************************************************************** * Name: usbhost_devaddr_allocate * @@ -66,7 +50,7 @@ static int usbhost_takesem(FAR struct usbhost_devaddr_s *devgen) * Allocate a new unique device address. * * Assumptions: - * Caller hold the exclsem + * Caller hold the lock * ****************************************************************************/ @@ -125,7 +109,7 @@ static int usbhost_devaddr_allocate(FAR struct usbhost_devaddr_s *devgen) * De-allocate a device address. * * Assumptions: - * Caller hold the exclsem + * Caller hold the lock * ****************************************************************************/ @@ -232,7 +216,7 @@ void usbhost_devaddr_initialize(FAR struct usbhost_roothubport_s *rhport) devgen = &rhport->devgen; memset(devgen, 0, sizeof(struct usbhost_devaddr_s)); - nxsem_init(&devgen->exclsem, 0, 1); + nxmutex_init(&devgen->lock); devgen->next = 1; } @@ -266,7 +250,7 @@ int usbhost_devaddr_create(FAR struct usbhost_hubport_s *hport) /* Get exclusive access to the root hub port device address data */ - ret = usbhost_takesem(devgen); + ret = nxmutex_lock(&devgen->lock); if (ret < 0) { return ret; @@ -275,7 +259,7 @@ int usbhost_devaddr_create(FAR struct usbhost_hubport_s *hport) /* Allocate a device address */ devaddr = usbhost_devaddr_allocate(devgen); - usbhost_givesem(devgen); + nxmutex_unlock(&devgen->lock); if (devaddr < 0) { @@ -322,7 +306,7 @@ void usbhost_devaddr_destroy(FAR struct usbhost_hubport_s *hport, do { - ret = usbhost_takesem(devgen); + ret = nxmutex_lock(&devgen->lock); /* The only expected error would -ECANCELED meaning that the parent * thread has been canceled. We have to continue and free the @@ -336,6 +320,6 @@ void usbhost_devaddr_destroy(FAR struct usbhost_hubport_s *hport, /* Free the device address */ usbhost_devaddr_free(devgen, devaddr); - usbhost_givesem(devgen); + nxmutex_unlock(&devgen->lock); } } diff --git a/drivers/usbhost/usbhost_ft232r.c b/drivers/usbhost/usbhost_ft232r.c index 936e4776fac25..3bededd1a4d1a 100644 --- a/drivers/usbhost/usbhost_ft232r.c +++ b/drivers/usbhost/usbhost_ft232r.c @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include @@ -238,7 +238,7 @@ struct usbhost_ft232r_s uint16_t rxndx; /* Index to the next byte in the RX packet buffer */ int16_t crefs; /* Reference count on the driver instance */ int16_t nbytes; /* The number of bytes actually transferred */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ + mutex_t lock; /* Used to maintain mutual exclusive access */ struct work_s ntwork; /* For asynchronous notification work */ struct work_s rxwork; /* For RX packet work */ struct work_s txwork; /* For TX packet work */ @@ -266,12 +266,6 @@ struct usbhost_freestate_s * Private Function Prototypes ****************************************************************************/ -/* Semaphores */ - -static int usbhost_takesem(FAR sem_t *sem); -static void usbhost_forcetake(FAR sem_t *sem); -#define usbhost_givesem(s) nxsem_post(s); - /* Memory allocation services */ static FAR struct usbhost_ft232r_s *usbhost_allocclass(void); @@ -431,48 +425,6 @@ static uint32_t g_devinuse; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int usbhost_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: usbhost_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static void usbhost_forcetake(FAR sem_t *sem) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: usbhost_allocclass * @@ -1405,9 +1357,9 @@ static void usbhost_destroy(FAR void *arg) usbhost_free_buffers(priv); - /* Destroy the semaphores */ + /* Destroy the mutex */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); /* Disconnect the USB host device */ @@ -1914,11 +1866,11 @@ usbhost_create(FAR struct usbhost_hubport_s *hport, priv->crefs = 1; - /* Initialize semaphores + /* Initialize mutex * (this works okay in the interrupt context) */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Set up the serial lower-half interface */ @@ -2006,7 +1958,7 @@ static int usbhost_connect(FAR struct usbhost_class_s *usbclass, /* Get exclusive access to the device structure */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2106,7 +2058,7 @@ static int usbhost_connect(FAR struct usbhost_class_s *usbclass, * ready to handle it! */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2215,7 +2167,7 @@ static int usbhost_setup(FAR struct uart_dev_s *uartdev) /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2245,7 +2197,7 @@ static int usbhost_setup(FAR struct uart_dev_s *uartdev) } leave_critical_section(flags); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2270,7 +2222,7 @@ static void usbhost_shutdown(FAR struct uart_dev_s *uartdev) /* Decrement the reference count on the block driver */ DEBUGASSERT(priv->crefs > 1); - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); priv->crefs--; /* Release the semaphore. The following operations when crefs == 1 are @@ -2278,7 +2230,7 @@ static void usbhost_shutdown(FAR struct uart_dev_s *uartdev) * the block driver. */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* We need to disable interrupts momentarily to assure that there are * no asynchronous disconnect events. @@ -2377,7 +2329,7 @@ static int usbhost_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { /* Process the IOCTL by command */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2527,7 +2479,7 @@ static int usbhost_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } return ret; diff --git a/drivers/usbhost/usbhost_hidkbd.c b/drivers/usbhost/usbhost_hidkbd.c index 091d1e9968d4f..6257dbd973e02 100644 --- a/drivers/usbhost/usbhost_hidkbd.c +++ b/drivers/usbhost/usbhost_hidkbd.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -187,7 +188,7 @@ struct usbhost_state_s volatile bool waiting; /* TRUE: waiting for keyboard data */ uint8_t ifno; /* Interface number */ int16_t crefs; /* Reference count on the driver instance */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ + mutex_t lock; /* Used to maintain mutual exclusive access */ sem_t waitsem; /* Used to wait for keyboard data */ FAR uint8_t *tbuffer; /* The allocated transfer buffer */ size_t tbuflen; /* Size of the allocated transfer buffer */ @@ -237,12 +238,6 @@ struct usbhost_outstream_s * Private Function Prototypes ****************************************************************************/ -/* Semaphores */ - -static int usbhost_takesem(FAR sem_t *sem); -static void usbhost_forcetake(FAR sem_t *sem); -#define usbhost_givesem(s) nxsem_post(s); - /* Memory allocation services */ static inline FAR struct usbhost_state_s *usbhost_allocclass(void); @@ -362,9 +357,9 @@ static uint32_t g_devinuse; /* The following are used to managed the class creation operation */ -static sem_t g_exclsem; /* For mutually exclusive thread creation */ -static sem_t g_syncsem; /* Thread data passing interlock */ -static struct usbhost_state_s *g_priv; /* Data passed to thread */ +static mutex_t g_lock; /* For mutually exclusive thread creation */ +static sem_t g_syncsem; /* Thread data passing interlock */ +static struct usbhost_state_s *g_priv; /* Data passed to thread */ /* The following tables map keyboard scan codes to printable ASIC * characters. There is no support here for function keys or cursor @@ -634,48 +629,6 @@ static const uint8_t lcmap[USBHID_NUMSCANCODES] = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int usbhost_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: usbhost_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static void usbhost_forcetake(FAR sem_t *sem) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: usbhost_allocclass * @@ -833,9 +786,9 @@ static void usbhost_destroy(FAR void *arg) usbhost_tdfree(priv); - /* Destroy the semaphores */ + /* Destroy the mutex & semaphores */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->waitsem); /* Disconnect the USB host device */ @@ -1082,7 +1035,7 @@ static int usbhost_kbdpoll(int argc, char *argv[]) hport = priv->usbclass.hport; priv->polling = true; - usbhost_givesem(&g_syncsem); + nxsem_post(&g_syncsem); nxsig_sleep(1); /* Loop here until the device is disconnected */ @@ -1096,7 +1049,7 @@ static int usbhost_kbdpoll(int argc, char *argv[]) * open and actively trying to interact with the class driver. */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1124,7 +1077,7 @@ static int usbhost_kbdpoll(int argc, char *argv[]) /* Send HID report request */ ret = DRVR_CTRLIN(hport->drvr, hport->ep0, ctrlreq, priv->tbuffer); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* Check for errors -- Bail if an excessive number of consecutive * errors are encountered. @@ -1160,7 +1113,7 @@ static int usbhost_kbdpoll(int argc, char *argv[]) /* Add the newly received keystrokes to our internal buffer */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1276,13 +1229,13 @@ static int usbhost_kbdpoll(int argc, char *argv[]) { /* Yes.. wake it up */ - usbhost_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); priv->waiting = false; } } empty = newstate; - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /* If USB debug is on, then provide some periodic indication that @@ -1326,7 +1279,7 @@ static int usbhost_kbdpoll(int argc, char *argv[]) * trying to interact with the class driver. */ - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Indicate that we are no longer running and decrement the reference * count held by this thread. If there are no other users of the class, @@ -1368,7 +1321,7 @@ static int usbhost_kbdpoll(int argc, char *argv[]) * reference is closed */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } leave_critical_section(flags); @@ -1702,7 +1655,7 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) * likelihood of this being used? About zero, but we protect it anyway). */ - ret = usbhost_takesem(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { usbhost_tdfree(priv); @@ -1720,7 +1673,7 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) * probably due to memory resources */ - usbhost_givesem(&g_exclsem); + nxmutex_unlock(&g_lock); goto errout; } @@ -1728,8 +1681,8 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) /* Now wait for the poll task to get properly initialized */ - ret = usbhost_takesem(&g_syncsem); - usbhost_givesem(&g_exclsem); + ret = nxsem_wait_uninterruptible(&g_syncsem); + nxmutex_unlock(&g_lock); if (ret < 0) { @@ -1747,9 +1700,9 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) */ errout: - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); priv->crefs--; - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1963,9 +1916,9 @@ static FAR struct usbhost_class_s * priv->crefs = 1; - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->waitsem, 0, 0); /* The waitsem semaphore is used for signaling and, hence, should @@ -2103,7 +2056,7 @@ static int usbhost_disconnected(struct usbhost_class_s *usbclass) { /* Yes.. wake it up */ - usbhost_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); priv->waiting = false; } @@ -2163,7 +2116,7 @@ static int usbhost_open(FAR struct file *filep) /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2195,7 +2148,7 @@ static int usbhost_open(FAR struct file *filep) leave_critical_section(flags); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2222,7 +2175,7 @@ static int usbhost_close(FAR struct file *filep) /* Decrement the reference count on the driver */ DEBUGASSERT(priv->crefs >= 1); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2291,7 +2244,7 @@ static int usbhost_close(FAR struct file *filep) } } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); leave_critical_section(flags); return OK; } @@ -2321,7 +2274,7 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2361,14 +2314,14 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, uinfo("Waiting...\n"); priv->waiting = true; - usbhost_givesem(&priv->exclsem); - ret = usbhost_takesem(&priv->waitsem); + nxmutex_unlock(&priv->lock); + ret = nxsem_wait_uninterruptible(&priv->waitsem); if (ret < 0) { return ret; } - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2409,7 +2362,7 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, } errout: - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (ssize_t)ret; } @@ -2453,7 +2406,7 @@ static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2522,7 +2475,7 @@ static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2551,7 +2504,7 @@ int usbhost_kbdinit(void) { /* Perform any one-time initialization of the class implementation */ - nxsem_init(&g_exclsem, 0, 1); + nxmutex_init(&g_lock); nxsem_init(&g_syncsem, 0, 0); /* The g_syncsem semaphore is used for signaling and, hence, should not diff --git a/drivers/usbhost/usbhost_hidmouse.c b/drivers/usbhost/usbhost_hidmouse.c index e3017d424f6ff..b7d51de5d5336 100644 --- a/drivers/usbhost/usbhost_hidmouse.c +++ b/drivers/usbhost/usbhost_hidmouse.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -257,7 +258,7 @@ struct usbhost_state_s uint8_t buttons; /* Current state of the mouse buttons */ #endif int16_t crefs; /* Reference count on the driver instance */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ + mutex_t lock; /* Used to maintain mutual exclusive access */ sem_t waitsem; /* Used to wait for mouse data */ FAR uint8_t *tbuffer; /* The allocated transfer buffer */ b16_t xaccum; /* Current integrated X position */ @@ -286,12 +287,6 @@ struct usbhost_state_s * Private Function Prototypes ****************************************************************************/ -/* Semaphores */ - -static int usbhost_takesem(FAR sem_t *sem); -static void usbhost_forcetake(FAR sem_t *sem); -#define usbhost_givesem(s) nxsem_post(s); - /* Memory allocation services */ static inline FAR struct usbhost_state_s *usbhost_allocclass(void); @@ -413,56 +408,14 @@ static uint32_t g_devinuse; /* The following are used to managed the class creation operation */ -static sem_t g_exclsem; /* For mutually exclusive thread creation */ -static sem_t g_syncsem; /* Thread data passing interlock */ -static struct usbhost_state_s *g_priv; /* Data passed to thread */ +static mutex_t g_lock; /* For mutually exclusive thread creation */ +static sem_t g_syncsem; /* Thread data passing interlock */ +static struct usbhost_state_s *g_priv; /* Data passed to thread */ /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int usbhost_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: usbhost_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static void usbhost_forcetake(FAR sem_t *sem) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: usbhost_allocclass * @@ -615,9 +568,9 @@ static void usbhost_destroy(FAR void *arg) usbhost_tdfree(priv); - /* Destroy the semaphores */ + /* Destroy the mutex & semaphores */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->waitsem); /* Disconnect the USB host device */ @@ -1025,7 +978,7 @@ static int usbhost_mouse_poll(int argc, char *argv[]) hport = priv->usbclass.hport; priv->polling = true; - usbhost_givesem(&g_syncsem); + nxsem_post(&g_syncsem); nxsig_sleep(1); /* Loop here until the device is disconnected */ @@ -1081,7 +1034,7 @@ static int usbhost_mouse_poll(int argc, char *argv[]) { /* Get exclusive access to the mouse state data */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { /* Break out and disconnect if the thread is canceled. */ @@ -1160,7 +1113,7 @@ static int usbhost_mouse_poll(int argc, char *argv[]) /* Release our lock on the state structure */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } } @@ -1185,7 +1138,7 @@ static int usbhost_mouse_poll(int argc, char *argv[]) * trying to interact with the class driver. */ - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Indicate that we are no longer running and decrement the reference * count held by this thread. If there are no other users of the class, @@ -1227,7 +1180,7 @@ static int usbhost_mouse_poll(int argc, char *argv[]) * reference is closed */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } leave_critical_section(flags); @@ -1329,7 +1282,7 @@ static int usbhost_waitsample(FAR struct usbhost_state_s *priv, * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -1366,7 +1319,7 @@ static int usbhost_waitsample(FAR struct usbhost_state_s *priv, * sample. Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); errout: /* Then re-enable interrupts. We might get interrupt here and there @@ -1656,7 +1609,7 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) * likelihood of this being used? About zero, but we protect it anyway). */ - ret = usbhost_takesem(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { usbhost_tdfree(priv); @@ -1674,7 +1627,7 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) * probably due to memory resources */ - usbhost_givesem(&g_exclsem); + nxmutex_unlock(&g_lock); goto errout; } @@ -1682,8 +1635,8 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) /* Now wait for the poll task to get properly initialized */ - ret = usbhost_takesem(&g_syncsem); - usbhost_givesem(&g_exclsem); + ret = nxsem_wait_uninterruptible(&g_syncsem); + nxmutex_unlock(&g_lock); if (ret < 0) { @@ -1701,9 +1654,9 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) */ errout: - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); priv->crefs--; - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1916,9 +1869,9 @@ static FAR struct usbhost_class_s * priv->crefs = 1; - /* Initialize semaphores */ + /* Initialize mutex & semaphores */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->waitsem, 0, 0); /* The waitsem semaphore is used for signaling and, hence, should @@ -2061,7 +2014,7 @@ static int usbhost_disconnected(struct usbhost_class_s *usbclass) { /* Yes.. wake them up */ - usbhost_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); } /* Possibilities: @@ -2125,7 +2078,7 @@ static int usbhost_open(FAR struct file *filep) /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2178,7 +2131,7 @@ static int usbhost_open(FAR struct file *filep) leave_critical_section(flags); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2205,7 +2158,7 @@ static int usbhost_close(FAR struct file *filep) /* Decrement the reference count on the driver */ DEBUGASSERT(priv->crefs >= 1); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2272,7 +2225,7 @@ static int usbhost_close(FAR struct file *filep) } } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); leave_critical_section(flags); return OK; } @@ -2306,7 +2259,7 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2423,7 +2376,7 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, #endif errout: - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); iinfo("Returning: %d\n", ret); return (ssize_t)ret; } @@ -2468,7 +2421,7 @@ static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2537,7 +2490,7 @@ static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2566,7 +2519,7 @@ int usbhost_mouse_init(void) { /* Perform any one-time initialization of the class implementation */ - nxsem_init(&g_exclsem, 0, 1); + nxmutex_init(&g_lock); nxsem_init(&g_syncsem, 0, 0); /* The g_syncsem semaphore is used for signaling and, hence, should not diff --git a/drivers/usbhost/usbhost_hub.c b/drivers/usbhost/usbhost_hub.c index d0eeb4fbef17b..d2b0972d85322 100644 --- a/drivers/usbhost/usbhost_hub.c +++ b/drivers/usbhost/usbhost_hub.c @@ -37,7 +37,6 @@ #include #include #include -#include #include #include @@ -116,7 +115,6 @@ struct usbhost_hubpriv_s bool compounddev; /* Hub is part of compound device */ bool indicator; /* Port indicator */ uint16_t pwrondelay; /* Power on wait time in ms */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ struct work_s work; /* Used for deferred callback work */ usbhost_ep_t intin; /* Interrupt IN endpoint */ @@ -1105,10 +1103,6 @@ static void usbhost_disconnect_event(FAR void *arg) usbhost_hport_deactivate(hport); - /* Destroy the semaphores */ - - nxsem_destroy(&priv->exclsem); - /* Disconnect the USB host device */ DRVR_DISCONNECT(hport->drvr, hport); @@ -1319,10 +1313,6 @@ static FAR struct usbhost_class_s * goto errout_with_ctrlreq; } - /* Initialize semaphores (this works okay in the interrupt context) */ - - nxsem_init(&priv->exclsem, 0, 1); - /* Initialize per-port data */ for (port = 0; port < USBHUB_MAX_PORTS; port++) diff --git a/drivers/usbhost/usbhost_max3421e.c b/drivers/usbhost/usbhost_max3421e.c index b3ef780b9cb4d..8bedd314c5080 100644 --- a/drivers/usbhost/usbhost_max3421e.c +++ b/drivers/usbhost/usbhost_max3421e.c @@ -131,11 +131,6 @@ #define TRENTRY(id,fmt1,string) {string} -/* Lock *********************************************************************/ - -#define max3421e_take_exclsem(s) nxrmutex_lock(&(s)->lock) -#define max3421e_give_exclsem(s) nxrmutex_unlock(&(s)->lock); - /**************************************************************************** * Private Types ****************************************************************************/ @@ -419,13 +414,6 @@ static void max3421e_sndblock(FAR struct max3421e_usbhost_s *priv, # define max3421e_pktdump(m,b,n) #endif -/* Semaphores ***************************************************************/ - -static int max3421e_takesem(FAR sem_t *sem); -#define max3421e_givesem(s) nxsem_post(s); -static int max3421e_take_exclsem(FAR struct max3421e_usbhost_s *priv); -static void max3421e_give_exclsem(FAR struct max3421e_usbhost_s *priv); - /* Byte stream access helper functions **************************************/ static inline uint16_t max3421e_getle16(const uint8_t *val); @@ -1116,20 +1104,6 @@ static void max3421e_sndblock(FAR struct max3421e_usbhost_s *priv, SPI_SELECT(spi, SPIDEV_USBHOST(lower->devid), false); } -/**************************************************************************** - * Name: max3421e_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int max3421e_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /**************************************************************************** * Name: max3421e_getle16 * @@ -1370,7 +1344,7 @@ static void max3421e_chan_wakeup(FAR struct max3421e_usbhost_s *priv, MAX3421E_VTRACE2_CHANWAKEUP_OUT, chan->chidx, priv->result); - max3421e_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); priv->waiter = NULL; } @@ -2932,7 +2906,7 @@ static void max3421e_connect_event(FAR struct max3421e_usbhost_s *priv) priv->smstate = SMSTATE_ATTACHED; if (priv->pscwait) { - max3421e_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -2979,7 +2953,7 @@ static void max3421e_disconnect_event(FAR struct max3421e_usbhost_s *priv) if (priv->pscwait) { - max3421e_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); priv->pscwait = false; } } @@ -3343,7 +3317,7 @@ static int max3421e_wait(FAR struct usbhost_connection_s *conn, { /* We must have exclusive access to USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -3368,7 +3342,7 @@ static int max3421e_wait(FAR struct usbhost_connection_s *conn, usbhost_vtrace1(MAX3421E_VTRACE1_CONNECTED2, connport->connected); - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return OK; } @@ -3387,7 +3361,7 @@ static int max3421e_wait(FAR struct usbhost_connection_s *conn, usbhost_vtrace1(MAX3421E_VTRACE1_HUB_CONNECTED, connport->connected); - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return OK; } #endif @@ -3395,8 +3369,8 @@ static int max3421e_wait(FAR struct usbhost_connection_s *conn, /* Wait for the next connection event */ priv->pscwait = true; - max3421e_give_exclsem(priv); - ret = max3421e_takesem(&priv->pscsem); + nxrmutex_unlock(&priv->lock); + ret = nxsem_wait_uninterruptible(&priv->pscsem); if (ret < 0) { return ret; @@ -3461,7 +3435,7 @@ static int max3421e_getspeed(FAR struct max3421e_usbhost_s *priv, /* No, return an error */ usbhost_trace1(MAX3421E_TRACE1_DEVDISCONN6, 0); - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return -ENODEV; } @@ -3522,7 +3496,7 @@ static int max3421e_enumerate(FAR struct usbhost_connection_s *conn, /* We must have exclusive access to the USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -3544,7 +3518,7 @@ static int max3421e_enumerate(FAR struct usbhost_connection_s *conn, ret = max3421e_getspeed(priv, conn, hport); if (ret < 0) { - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return ret; } } @@ -3585,7 +3559,7 @@ static int max3421e_enumerate(FAR struct usbhost_connection_s *conn, USBHOST_HCTL_RCVTOG0 | USBHOST_HCTL_SNDTOG0); max3421e_unlock(priv); - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return ret; } @@ -3630,7 +3604,7 @@ static int max3421e_ep0configure(FAR struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -3644,7 +3618,7 @@ static int max3421e_ep0configure(FAR struct usbhost_driver_s *drvr, chan->maxpacket = maxpacketsize; chan->toggles = USBHOST_HCTL_RCVTOG0 | USBHOST_HCTL_SNDTOG0; - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return OK; } @@ -3691,7 +3665,7 @@ static int max3421e_epalloc(FAR struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -3703,7 +3677,7 @@ static int max3421e_epalloc(FAR struct usbhost_driver_s *drvr, if (chidx < 0) { usbhost_trace1(MAX3421E_TRACE1_CHANALLOC_FAIL, -chidx); - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return chidx; } @@ -3726,7 +3700,7 @@ static int max3421e_epalloc(FAR struct usbhost_driver_s *drvr, /* Return the endpoint number as the endpoint "handle" */ *ep = (usbhost_ep_t)chidx; - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return OK; } @@ -3761,13 +3735,13 @@ static int max3421e_epfree(FAR struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret >= 0) { /* Halt the channel and mark the channel available */ max3421e_chan_free(priv, (intptr_t)ep); - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); } return ret; @@ -4012,7 +3986,7 @@ static int max3421e_ctrlin(FAR struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4061,7 +4035,7 @@ static int max3421e_ctrlin(FAR struct usbhost_driver_s *drvr, { /* All success transactions exit here */ - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return OK; } @@ -4077,7 +4051,7 @@ static int max3421e_ctrlin(FAR struct usbhost_driver_s *drvr, /* All failures exit here after all retries and timeouts are exhausted */ - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4112,7 +4086,7 @@ static int max3421e_ctrlout(FAR struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4163,7 +4137,7 @@ static int max3421e_ctrlout(FAR struct usbhost_driver_s *drvr, { /* All success transactions exit here */ - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return OK; } @@ -4179,7 +4153,7 @@ static int max3421e_ctrlout(FAR struct usbhost_driver_s *drvr, /* All failures exit here after all retries and timeouts are exhausted */ - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return -ETIMEDOUT; } @@ -4240,7 +4214,7 @@ static ssize_t max3421e_transfer(FAR struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4257,7 +4231,7 @@ static ssize_t max3421e_transfer(FAR struct usbhost_driver_s *drvr, nbytes = max3421e_out_transfer(priv, chan, buffer, buflen); } - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return nbytes; } @@ -4316,7 +4290,7 @@ static int max3421e_asynch(FAR struct usbhost_driver_s *drvr, /* We must have exclusive access to the USB host hardware and structures */ - ret = max3421e_take_exclsem(priv); + ret = nxrmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -4333,7 +4307,7 @@ static int max3421e_asynch(FAR struct usbhost_driver_s *drvr, ret = max3421e_out_asynch(priv, chan, buffer, buflen, callback, arg); } - max3421e_give_exclsem(priv); + nxrmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_USBHOST_ASYNCH */ @@ -4395,7 +4369,7 @@ static int max3421e_cancel(FAR struct usbhost_driver_s *drvr, /* Wake'em up! */ - max3421e_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); priv->waiter = NULL; } @@ -4472,7 +4446,7 @@ static int max3421e_connect(FAR struct usbhost_driver_s *drvr, if (priv->pscwait) { priv->pscwait = false; - max3421e_givesem(&priv->pscsem); + nxsem_post(&priv->pscsem); } leave_critical_section(flags); diff --git a/drivers/usbhost/usbhost_skeleton.c b/drivers/usbhost/usbhost_skeleton.c index 495cb9b1afb95..7d4271a5b0a52 100644 --- a/drivers/usbhost/usbhost_skeleton.c +++ b/drivers/usbhost/usbhost_skeleton.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include @@ -89,7 +89,7 @@ struct usbhost_state_s volatile bool disconnected; /* TRUE: Device has been disconnected */ uint8_t ifno; /* Interface number */ int16_t crefs; /* Reference count on the driver instance */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ + mutex_t lock; /* Used to maintain mutual exclusive access */ struct work_s work; /* For interacting with the worker thread */ FAR uint8_t *tbuffer; /* The allocated transfer buffer */ size_t tbuflen; /* Size of the allocated transfer buffer */ @@ -101,12 +101,6 @@ struct usbhost_state_s * Private Function Prototypes ****************************************************************************/ -/* Semaphores */ - -static int usbhost_takesem(FAR sem_t *sem); -static void usbhost_forcetake(FAR sem_t *sem); -#define usbhost_givesem(s) nxsem_post(s); - /* Memory allocation services */ static inline FAR struct usbhost_state_s *usbhost_allocclass(void); @@ -193,48 +187,6 @@ static uint32_t g_devinuse; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int usbhost_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: usbhost_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static void usbhost_forcetake(FAR sem_t *sem) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: usbhost_allocclass * @@ -680,7 +632,7 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) if (ret >= 0) { - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ert; @@ -708,8 +660,9 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) uinfo("Successfully initialized\n"); priv->crefs--; - usbhost_givesem(&priv->exclsem); } + + nxmutex_unlock(&priv->lock); } return ret; @@ -921,11 +874,11 @@ static FAR struct usbhost_class_s * priv->crefs = 1; - /* Initialize semaphores + /* Initialize mutex * (this works okay in the interrupt context) */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Return the instance of the USB class driver */ diff --git a/drivers/usbhost/usbhost_storage.c b/drivers/usbhost/usbhost_storage.c index 55048330def0c..073c36f03876f 100644 --- a/drivers/usbhost/usbhost_storage.c +++ b/drivers/usbhost/usbhost_storage.c @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include @@ -118,7 +118,7 @@ struct usbhost_state_s int16_t crefs; /* Reference count on the driver instance */ uint16_t blocksize; /* Block size of USB mass storage device */ uint32_t nblocks; /* Number of blocks on the USB mass storage device */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ + mutex_t lock; /* Used to maintain mutual exclusive access */ struct work_s work; /* For interacting with the worker thread */ FAR uint8_t *tbuffer; /* The allocated transfer buffer */ size_t tbuflen; /* Size of the allocated transfer buffer */ @@ -137,12 +137,6 @@ struct usbhost_freestate_s * Private Function Prototypes ****************************************************************************/ -/* Semaphores */ - -static int usbhost_takesem(FAR sem_t *sem); -static void usbhost_forcetake(FAR sem_t *sem); -#define usbhost_givesem(s) nxsem_post(s); - /* Memory allocation services */ static inline FAR struct usbhost_state_s *usbhost_allocclass(void); @@ -304,48 +298,6 @@ static uint32_t g_devinuse; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int usbhost_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: usbhost_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static void usbhost_forcetake(FAR sem_t *sem) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: usbhost_allocclass * @@ -962,9 +914,9 @@ static void usbhost_destroy(FAR void *arg) usbhost_tfree(priv); - /* Destroy the semaphores */ + /* Destroy the mutex */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); /* Disconnect the USB host device */ @@ -1379,7 +1331,7 @@ static inline int usbhost_initvolume(FAR struct usbhost_state_s *priv) * driver has been registered. */ - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); DEBUGASSERT(priv->crefs >= 2); /* Decrement the reference count */ @@ -1417,8 +1369,7 @@ static inline int usbhost_initvolume(FAR struct usbhost_state_s *priv) * ready to handle it! */ - usbhost_givesem(&priv->exclsem); - + nxmutex_unlock(&priv->lock); return ret; } @@ -1741,11 +1692,11 @@ static FAR struct usbhost_class_s * priv->crefs = 1; - /* Initialize semaphores + /* Initialize mutex * (this works okay in the interrupt context) */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* NOTE: We do not yet know the geometry of the USB mass storage * device. @@ -1926,7 +1877,7 @@ static int usbhost_open(FAR struct inode *inode) /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1957,7 +1908,7 @@ static int usbhost_open(FAR struct inode *inode) leave_critical_section(flags); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1981,7 +1932,7 @@ static int usbhost_close(FAR struct inode *inode) DEBUGASSERT(priv->crefs > 1); - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); priv->crefs--; /* Release the semaphore. The following operations when crefs == 1 are @@ -1989,7 +1940,7 @@ static int usbhost_close(FAR struct inode *inode) * the block driver. */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* We need to disable interrupts momentarily to assure that there are * no asynchronous disconnect events. @@ -2056,7 +2007,7 @@ static ssize_t usbhost_read(FAR struct inode *inode, unsigned char *buffer, { FAR struct usbmsc_cbw_s *cbw; - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2119,7 +2070,7 @@ static ssize_t usbhost_read(FAR struct inode *inode, unsigned char *buffer, while (nbytes == -EAGAIN); } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /* On success, return the number of blocks read */ @@ -2168,7 +2119,7 @@ static ssize_t usbhost_write(FAR struct inode *inode, { FAR struct usbmsc_cbw_s *cbw; - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2222,7 +2173,7 @@ static ssize_t usbhost_write(FAR struct inode *inode, } } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /* On success, return the number of blocks written */ @@ -2262,7 +2213,7 @@ static int usbhost_geometry(FAR struct inode *inode, { /* Return the geometry of the USB mass storage device */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret >= 0) { geometry->geo_available = true; @@ -2270,7 +2221,7 @@ static int usbhost_geometry(FAR struct inode *inode, geometry->geo_writeenabled = true; geometry->geo_nsectors = priv->nblocks; geometry->geo_sectorsize = priv->blocksize; - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); uinfo("nsectors: %" PRIdOFF " sectorsize: %" PRIi16 "\n", geometry->geo_nsectors, geometry->geo_sectorsize); @@ -2311,7 +2262,7 @@ static int usbhost_ioctl(FAR struct inode *inode, int cmd, unsigned long arg) { /* Process the IOCTL by command */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret >= 0) { switch (cmd) @@ -2323,7 +2274,7 @@ static int usbhost_ioctl(FAR struct inode *inode, int cmd, unsigned long arg) break; } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } } diff --git a/drivers/usbhost/usbhost_xboxcontroller.c b/drivers/usbhost/usbhost_xboxcontroller.c index 5d6453fa848a9..6525b46058722 100644 --- a/drivers/usbhost/usbhost_xboxcontroller.c +++ b/drivers/usbhost/usbhost_xboxcontroller.c @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include @@ -160,7 +160,7 @@ struct usbhost_state_s uint8_t nwaiters; /* Number of threads waiting for controller data */ sem_t waitsem; /* Used to wait for controller data */ int16_t crefs; /* Reference count on the driver instance */ - sem_t exclsem; /* Used to maintain mutual exclusive access */ + mutex_t lock; /* Used to maintain mutual exclusive access */ struct work_s work; /* For interacting with the worker thread */ FAR uint8_t *tbuffer; /* The allocated transfer buffer */ FAR uint8_t obuffer[20]; /* The fixed output transfer buffer */ @@ -183,12 +183,6 @@ struct usbhost_state_s * Private Function Prototypes ****************************************************************************/ -/* Semaphores */ - -static int usbhost_takesem(FAR sem_t *sem); -static void usbhost_forcetake(FAR sem_t *sem); -#define usbhost_givesem(s) nxsem_post(s); - /* Memory allocation services */ static inline FAR struct usbhost_state_s *usbhost_allocclass(void); @@ -318,56 +312,14 @@ static uint32_t g_devinuse; /* The following are used to managed the class creation operation */ -static sem_t g_exclsem; /* For mutually exclusive thread creation */ -static sem_t g_syncsem; /* Thread data passing interlock */ -static struct usbhost_state_s *g_priv; /* Data passed to thread */ +static mutex_t g_lock; /* For mutually exclusive thread creation */ +static sem_t g_syncsem; /* Thread data passing interlock */ +static struct usbhost_state_s *g_priv; /* Data passed to thread */ /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usbhost_takesem - * - * Description: - * This is just a wrapper to handle the annoying behavior of semaphore - * waits that return due to the receipt of a signal. - * - ****************************************************************************/ - -static int usbhost_takesem(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -/**************************************************************************** - * Name: usbhost_forcetake - * - * Description: - * This is just another wrapper but this one continues even if the thread - * is canceled. This must be done in certain conditions where were must - * continue in order to clean-up resources. - * - ****************************************************************************/ - -static void usbhost_forcetake(FAR sem_t *sem) -{ - int ret; - - do - { - ret = nxsem_wait_uninterruptible(sem); - - /* The only expected error would -ECANCELED meaning that the - * parent thread has been canceled. We have to continue and - * terminate the poll in this case. - */ - - DEBUGASSERT(ret == OK || ret == -ECANCELED); - } - while (ret < 0); -} - /**************************************************************************** * Name: usbhost_allocclass * @@ -526,9 +478,9 @@ static void usbhost_destroy(FAR void *arg) usbhost_tfree(priv); - /* Destroy the semaphores */ + /* Destroy the semaphores & mutex */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); nxsem_destroy(&priv->waitsem); /* Disconnect the USB host device */ @@ -620,7 +572,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) hport = priv->usbclass.hport; priv->polling = true; - usbhost_givesem(&g_syncsem); + nxsem_post(&g_syncsem); nxsig_sleep(1); /* Loop here until the device is disconnected */ @@ -683,7 +635,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) { /* Get exclusive access to the controller state data */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto exitloop; @@ -700,7 +652,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) /* Release our lock on the state structure */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } break; @@ -709,7 +661,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) /* Get exclusive access to the controller state data */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto exitloop; @@ -761,7 +713,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) /* Release our lock on the state structure */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); break; @@ -775,7 +727,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) { /* Get exclusive access to the controller state data */ - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { goto exitloop; @@ -867,7 +819,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) /* Release our lock on the state structure */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } break; @@ -899,7 +851,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) * trying to interact with the class driver. */ - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); /* Indicate that we are no longer running and decrement the reference * count held by this thread. If there are no other users of the class, @@ -941,7 +893,7 @@ static int usbhost_xboxcontroller_poll(int argc, char *argv[]) * reference is closed */ - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); } leave_critical_section(flags); @@ -1026,7 +978,7 @@ static int usbhost_waitsample(FAR struct usbhost_state_s *priv, * run, but they cannot run yet because pre-emption is disabled. */ - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); /* Try to get the a sample... if we cannot, then wait on the semaphore * that is posted when new sample data is available. @@ -1063,7 +1015,7 @@ static int usbhost_waitsample(FAR struct usbhost_state_s *priv, * sample. Interrupts and pre-emption will be re-enabled while we wait. */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); errout: /* Then re-enable interrupts. We might get interrupt here and there @@ -1382,7 +1334,7 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) * likelihood of this being used? About zero, but we protect it anyway). */ - ret = usbhost_takesem(&g_exclsem); + ret = nxmutex_lock(&g_lock); if (ret < 0) { usbhost_tfree(priv); @@ -1402,7 +1354,7 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) * resources. */ - usbhost_givesem(&g_exclsem); + nxmutex_unlock(&g_lock); goto errout; } @@ -1410,8 +1362,8 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) /* Now wait for the poll task to get properly initialized */ - usbhost_forcetake(&g_syncsem); - usbhost_givesem(&g_exclsem); + nxsem_wait_uninterruptible(&g_syncsem); + nxmutex_unlock(&g_lock); /* Configure the device */ @@ -1427,9 +1379,9 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv) */ errout: - usbhost_forcetake(&priv->exclsem); + nxmutex_lock(&priv->lock); priv->crefs--; - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1641,11 +1593,11 @@ static FAR struct usbhost_class_s * priv->crefs = 1; - /* Initialize semaphores (this works okay in the interrupt + /* Initialize mutex & semaphores (this works okay in the interrupt * context). */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); nxsem_init(&priv->waitsem, 0, 0); /* The waitsem semaphore is used for signaling and, hence, should @@ -1780,7 +1732,7 @@ static int usbhost_disconnected(struct usbhost_class_s *usbclass) { /* Yes.. wake them up */ - usbhost_givesem(&priv->waitsem); + nxsem_post(&priv->waitsem); } /* Possibilities: @@ -1844,7 +1796,7 @@ static int usbhost_open(FAR struct file *filep) /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1898,7 +1850,7 @@ static int usbhost_open(FAR struct file *filep) } leave_critical_section(flags); - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -1925,7 +1877,7 @@ static int usbhost_close(FAR struct file *filep) /* Decrement the reference count on the driver */ DEBUGASSERT(priv->crefs >= 1); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1992,7 +1944,7 @@ static int usbhost_close(FAR struct file *filep) } } - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); leave_critical_section(flags); return OK; } @@ -2020,7 +1972,7 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2080,7 +2032,7 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, ret = sizeof(struct xbox_controller_buttonstate_s); errout: - usbhost_givesem(&priv->exclsem); + nxmutex_unlock(&priv->lock); iinfo("Returning: %d\n", ret); return (ssize_t)ret; } @@ -2207,7 +2159,7 @@ static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Make sure that we have exclusive access to the private data structure */ DEBUGASSERT(priv); - ret = usbhost_takesem(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2277,7 +2229,7 @@ static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -2306,7 +2258,7 @@ int usbhost_xboxcontroller_init(void) { /* Perform any one-time initialization of the class implementation */ - nxsem_init(&g_exclsem, 0, 1); + nxmutex_init(&g_lock); nxsem_init(&g_syncsem, 0, 0); /* The g_syncsem semaphore is used for signaling and, hence, should not diff --git a/drivers/usbmisc/fusb301.c b/drivers/usbmisc/fusb301.c index d0654d805d478..48b86e76d9b95 100644 --- a/drivers/usbmisc/fusb301.c +++ b/drivers/usbmisc/fusb301.c @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -63,7 +64,7 @@ struct fusb301_dev_s FAR struct i2c_master_s *i2c; /* I2C interface */ uint8_t addr; /* I2C address */ volatile bool int_pending; /* Interrupt received but handled */ - sem_t devsem; /* Manages exclusive access */ + mutex_t devlock; /* Manages exclusive access */ FAR struct fusb301_config_s *config; /* Platform specific configuration */ FAR struct pollfd *fds[CONFIG_FUSB301_NPOLLWAITERS]; }; @@ -529,7 +530,7 @@ static ssize_t fusb301_read(FAR struct file *filep, ptr = (struct fusb301_result_s *)buffer; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -544,7 +545,7 @@ static ssize_t fusb301_read(FAR struct file *filep, ptr->status = fusb301_getreg(priv, FUSB301_STATUS_REG); ptr->dev_type = fusb301_getreg(priv, FUSB301_TYPE_REG); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return sizeof(struct fusb301_result_s); } @@ -575,7 +576,7 @@ static int fusb301_ioctl(FAR struct file *filep, int cmd, unsigned long arg) FAR struct fusb301_dev_s *priv = inode->i_private; int ret; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -635,7 +636,7 @@ static int fusb301_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -661,7 +662,7 @@ static int fusb301_poll(FAR struct file *filep, DEBUGASSERT(inode && inode->i_private); priv = (FAR struct fusb301_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -724,7 +725,7 @@ static int fusb301_poll(FAR struct file *filep, } out: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -774,9 +775,9 @@ int fusb301_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, return -ENOMEM; } - /* Initialize device structure semaphore */ + /* Initialize device structure mutex */ - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); priv->int_pending = false; priv->i2c = i2c; @@ -805,8 +806,7 @@ int fusb301_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); - return ret; } diff --git a/drivers/usbmisc/fusb303.c b/drivers/usbmisc/fusb303.c index cc52ae9d7414f..9d94840c76cef 100644 --- a/drivers/usbmisc/fusb303.c +++ b/drivers/usbmisc/fusb303.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -93,7 +94,7 @@ struct fusb303_dev_s FAR struct i2c_master_s *i2c; /* I2C interface */ uint8_t addr; /* I2C address */ volatile bool int_pending; /* Interrupt received but handled */ - sem_t devsem; /* Manages exclusive access */ + mutex_t devlock; /* Manages exclusive access */ FAR struct fusb303_config_s *config; /* Platform specific configuration */ FAR struct pollfd *fds[CONFIG_FUSB303_NPOLLWAITERS]; }; @@ -630,7 +631,7 @@ static int fusb303_open(FAR struct file *filep) uint8_t dev_type; int ret; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -653,7 +654,7 @@ static int fusb303_open(FAR struct file *filep) priv->config->irq_enable(priv->config, true); } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -671,7 +672,7 @@ static int fusb303_close(FAR struct file *filep) FAR struct fusb303_dev_s *priv = inode->i_private; int ret; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -679,7 +680,7 @@ static int fusb303_close(FAR struct file *filep) priv->config->irq_enable(priv->config, false); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return OK; } @@ -707,7 +708,7 @@ static ssize_t fusb303_read(FAR struct file *filep, FAR char *buffer, ptr = (struct fusb303_result_s *)buffer; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -727,7 +728,7 @@ static ssize_t fusb303_read(FAR struct file *filep, FAR char *buffer, fusb303_clear_interrupts(priv); - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return sizeof(struct fusb303_result_s); } @@ -762,7 +763,7 @@ static int fusb303_ioctl(FAR struct file *filep, int cmd, unsigned long arg) FAR struct fusb303_dev_s *priv = inode->i_private; int ret; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -822,7 +823,7 @@ static int fusb303_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -849,7 +850,7 @@ static int fusb303_poll(FAR struct file *filep, FAR struct pollfd *fds, DEBUGASSERT(inode && inode->i_private); priv = (FAR struct fusb303_dev_s *)inode->i_private; - ret = nxsem_wait(&priv->devsem); + ret = nxmutex_lock(&priv->devlock); if (ret < 0) { return ret; @@ -912,7 +913,7 @@ static int fusb303_poll(FAR struct file *filep, FAR struct pollfd *fds, } out: - nxsem_post(&priv->devsem); + nxmutex_unlock(&priv->devlock); return ret; } @@ -962,9 +963,9 @@ int fusb303_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, return -ENOMEM; } - /* Initialize device structure semaphore */ + /* Initialize device structure mutex */ - nxsem_init(&priv->devsem, 0, 1); + nxmutex_init(&priv->devlock); priv->int_pending = false; priv->i2c = i2c; @@ -993,8 +994,7 @@ int fusb303_register(FAR const char *devpath, FAR struct i2c_master_s *i2c, return OK; errout_with_priv: - nxsem_destroy(&priv->devsem); + nxmutex_destroy(&priv->devlock); kmm_free(priv); - return ret; } diff --git a/drivers/usrsock/usrsock_dev.c b/drivers/usrsock/usrsock_dev.c index 4b6a54a5af0c0..34a8ad3cca049 100644 --- a/drivers/usrsock/usrsock_dev.c +++ b/drivers/usrsock/usrsock_dev.c @@ -40,7 +40,7 @@ #include #include -#include +#include #include #include @@ -58,8 +58,8 @@ struct usrsockdev_s { - sem_t devsem; /* Lock for device node */ - uint8_t ocount; /* The number of times the device has been opened */ + mutex_t devlock; /* Lock for device node */ + uint8_t ocount; /* The number of times the device has been opened */ struct { FAR const struct iovec *iov; /* Pending request buffers */ @@ -111,31 +111,13 @@ static const struct file_operations g_usrsockdevops = static struct usrsockdev_s g_usrsockdev = { - NXSEM_INITIALIZER(1, PRIOINHERIT_FLAGS_DISABLE) + NXMUTEX_INITIALIZER }; /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: usrsockdev_semtake() and usrsockdev_semgive() - * - * Description: - * Take/give semaphore - * - ****************************************************************************/ - -static int usrsockdev_semtake(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - -static void usrsockdev_semgive(FAR sem_t *sem) -{ - nxsem_post(sem); -} - /**************************************************************************** * Name: usrsockdev_is_opened ****************************************************************************/ @@ -179,7 +161,7 @@ static ssize_t usrsockdev_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(dev); - ret = usrsockdev_semtake(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -212,7 +194,7 @@ static ssize_t usrsockdev_read(FAR struct file *filep, FAR char *buffer, len = 0; } - usrsockdev_semgive(&dev->devsem); + nxmutex_unlock(&dev->devlock); return len; } @@ -239,7 +221,7 @@ static off_t usrsockdev_seek(FAR struct file *filep, off_t offset, DEBUGASSERT(dev); - ret = usrsockdev_semtake(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -280,7 +262,7 @@ static off_t usrsockdev_seek(FAR struct file *filep, off_t offset, pos = 0; } - usrsockdev_semgive(&dev->devsem); + nxmutex_unlock(&dev->devlock); return pos; } @@ -312,7 +294,7 @@ static ssize_t usrsockdev_write(FAR struct file *filep, DEBUGASSERT(dev); - ret = usrsockdev_semtake(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -326,7 +308,7 @@ static ssize_t usrsockdev_write(FAR struct file *filep, dev->req.iovcnt = 0; } - usrsockdev_semgive(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -347,7 +329,7 @@ static int usrsockdev_open(FAR struct file *filep) DEBUGASSERT(dev); - ret = usrsockdev_semtake(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -372,7 +354,7 @@ static int usrsockdev_open(FAR struct file *filep) ret = OK; } - usrsockdev_semgive(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -392,7 +374,7 @@ static int usrsockdev_close(FAR struct file *filep) DEBUGASSERT(dev); - ret = usrsockdev_semtake(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -409,7 +391,7 @@ static int usrsockdev_close(FAR struct file *filep) dev->req.iovcnt = 0; dev->req.pos = 0; - usrsockdev_semgive(&dev->devsem); + nxmutex_unlock(&dev->devlock); usrsock_abort(); return ret; @@ -443,7 +425,7 @@ static int usrsockdev_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Are we setting up the poll? Or tearing it down? */ - ret = usrsockdev_semtake(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -510,7 +492,7 @@ static int usrsockdev_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - usrsockdev_semgive(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -529,7 +511,7 @@ int usrsock_request(FAR struct iovec *iov, unsigned int iovcnt) /* Set outstanding request for daemon to handle. */ - net_lockedwait_uninterruptible(&dev->devsem); + net_lockedwait_uninterruptible(&dev->devlock); if (usrsockdev_is_opened(dev)) { @@ -548,7 +530,7 @@ int usrsock_request(FAR struct iovec *iov, unsigned int iovcnt) ret = -ENETDOWN; } - usrsockdev_semgive(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } diff --git a/drivers/video/isx012.c b/drivers/video/isx012.c index c7ac8d3908d01..f05f28e1fd097 100644 --- a/drivers/video/isx012.c +++ b/drivers/video/isx012.c @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include #include @@ -169,7 +169,7 @@ typedef struct isx012_reg_s isx012_reg_t; struct isx012_dev_s { - sem_t i2c_lock; + mutex_t i2c_lock; FAR struct i2c_master_s *i2c; /* I2C interface */ uint8_t i2c_addr; /* I2C address */ int i2c_freq; /* Frequency */ @@ -637,16 +637,6 @@ static struct imgsensor_ops_s g_isx012_ops = * Private Functions ****************************************************************************/ -static void i2c_lock(void) -{ - nxsem_wait_uninterruptible(&g_isx012_private.i2c_lock); -} - -static void i2c_unlock(void) -{ - nxsem_post(&g_isx012_private.i2c_lock); -} - static uint16_t isx012_getreg(FAR isx012_dev_t *priv, uint16_t regaddr, uint16_t regsize) { @@ -663,7 +653,7 @@ static uint16_t isx012_getreg(FAR isx012_dev_t *priv, buffer[0] = regaddr >> 8; buffer[1] = regaddr & 0xff; - i2c_lock(); + nxmutex_lock(&g_isx012_private.i2c_lock); /* Write the register address */ @@ -683,7 +673,7 @@ static uint16_t isx012_getreg(FAR isx012_dev_t *priv, } } - i2c_unlock(); + nxmutex_unlock(&g_isx012_private.i2c_lock); if (ret >= 0) { @@ -713,7 +703,7 @@ static int isx012_putreg(isx012_dev_t *priv, memcpy((FAR uint8_t *)&buffer[2], (FAR uint8_t *)®val, regsize); - i2c_lock(); + nxmutex_lock(&g_isx012_private.i2c_lock); /* And do it */ @@ -724,8 +714,7 @@ static int isx012_putreg(isx012_dev_t *priv, verr("i2c_write failed: %d\n", ret); } - i2c_unlock(); - + nxmutex_unlock(&g_isx012_private.i2c_lock); return ret; } @@ -2965,8 +2954,7 @@ int isx012_initialize(void) priv->state = STATE_ISX012_POWEROFF; - nxsem_init(&priv->i2c_lock, 0, 1); - + nxmutex_init(&priv->i2c_lock); return OK; } diff --git a/drivers/video/isx019.c b/drivers/video/isx019.c index 8611f2596ddd5..f4d4c53bd2fac 100644 --- a/drivers/video/isx019.c +++ b/drivers/video/isx019.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include "isx019_reg.h" #include "isx019_range.h" @@ -192,8 +192,8 @@ typedef struct isx019_rect_s isx019_rect_t; struct isx019_dev_s { - sem_t fpga_lock; - sem_t i2c_lock; + mutex_t fpga_lock; + mutex_t i2c_lock; FAR struct i2c_master_s *i2c; float clock_ratio; isx019_default_value_t default_value; @@ -761,26 +761,6 @@ int32_t g_isx019_wbmode[] = * Private Functions ****************************************************************************/ -static void i2c_lock(void) -{ - nxsem_wait_uninterruptible(&g_isx019_private.i2c_lock); -} - -static void i2c_unlock(void) -{ - nxsem_post(&g_isx019_private.i2c_lock); -} - -static void fpga_lock(void) -{ - nxsem_wait_uninterruptible(&g_isx019_private.fpga_lock); -} - -static void fpga_unlock(void) -{ - nxsem_post(&g_isx019_private.fpga_lock); -} - int fpga_i2c_write(uint8_t addr, FAR uint8_t *data, uint8_t size) { struct i2c_config_s config; @@ -793,7 +773,7 @@ int fpga_i2c_write(uint8_t addr, FAR uint8_t *data, uint8_t size) config.address = ISX019_I2C_SLVADDR; config.addrlen = ISX019_I2C_SLVADDR_LEN; - i2c_lock(); + nxmutex_lock(&g_isx019_private.i2c_lock); /* ISX019 requires that send read command to ISX019 before FPGA access. */ @@ -809,7 +789,7 @@ int fpga_i2c_write(uint8_t addr, FAR uint8_t *data, uint8_t size) &config, buf, size + FPGA_I2C_REGADDR_LEN); - i2c_unlock(); + nxmutex_unlock(&g_isx019_private.i2c_lock); return ret; } @@ -825,7 +805,7 @@ static int fpga_i2c_read(uint8_t addr, FAR uint8_t *data, uint8_t size) config.address = ISX019_I2C_SLVADDR; config.addrlen = ISX019_I2C_SLVADDR_LEN; - i2c_lock(); + nxmutex_lock(&g_isx019_private.i2c_lock); /* ISX019 requires that send read command to ISX019 before FPGA access. */ @@ -844,8 +824,7 @@ static int fpga_i2c_read(uint8_t addr, FAR uint8_t *data, uint8_t size) ret = i2c_read(g_isx019_private.i2c, &config, data, size); } - i2c_unlock(); - + nxmutex_unlock(&g_isx019_private.i2c_lock); return ret; } @@ -991,7 +970,7 @@ static int isx019_i2c_write(uint8_t cat, config.address = ISX019_I2C_SLVADDR; config.addrlen = ISX019_I2C_SLVADDR_LEN; - i2c_lock(); + nxmutex_lock(&g_isx019_private.i2c_lock); ret = send_write_cmd(&config, cat, addr, data, size); if (ret == OK) @@ -999,7 +978,7 @@ static int isx019_i2c_write(uint8_t cat, ret = recv_write_response(&config); } - i2c_unlock(); + nxmutex_unlock(&g_isx019_private.i2c_lock); return ret; } @@ -1046,7 +1025,7 @@ static int isx019_i2c_read(uint8_t cat, config.address = ISX019_I2C_SLVADDR; config.addrlen = ISX019_I2C_SLVADDR_LEN; - i2c_lock(); + nxmutex_lock(&g_isx019_private.i2c_lock); ret = send_read_cmd(&config, cat, addr, size); if (ret == OK) @@ -1054,8 +1033,7 @@ static int isx019_i2c_read(uint8_t cat, ret = recv_read_response(&config, data, size); } - i2c_unlock(); - + nxmutex_unlock(&g_isx019_private.i2c_lock); return ret; } @@ -1493,7 +1471,7 @@ static int isx019_start_capture(imgsensor_stream_type_t type, return ret; } - fpga_lock(); + nxmutex_lock(&g_isx019_private.fpga_lock); /* Update FORMAT_AND_SCALE register of FPGA */ @@ -1611,7 +1589,7 @@ static int isx019_start_capture(imgsensor_stream_type_t type, fpga_i2c_write(FPGA_DATA_OUTPUT, ®val, 1); fpga_activate_setting(); - fpga_unlock(); + nxmutex_unlock(&g_isx019_private.fpga_lock); g_isx019_private.stream = type; return OK; @@ -1622,10 +1600,10 @@ static int isx019_stop_capture(imgsensor_stream_type_t type) uint8_t regval; regval = FPGA_DATA_OUTPUT_STOP; - fpga_lock(); + nxmutex_lock(&g_isx019_private.fpga_lock); fpga_i2c_write(FPGA_DATA_OUTPUT, ®val, 1); fpga_activate_setting(); - fpga_unlock(); + nxmutex_unlock(&g_isx019_private.fpga_lock); return OK; } @@ -2453,7 +2431,7 @@ static int set_jpg_quality(imgsensor_value_t val) return -EINVAL; } - fpga_lock(); + nxmutex_lock(&g_isx019_private.fpga_lock); /* Update DQT data and activate them. */ @@ -2470,7 +2448,7 @@ static int set_jpg_quality(imgsensor_value_t val) set_dqt(FPGA_DQT_LUMA, FPGA_DQT_CALC_DATA, y_calc); set_dqt(FPGA_DQT_CHROMA, FPGA_DQT_CALC_DATA, c_calc); - fpga_unlock(); + nxmutex_unlock(&g_isx019_private.fpga_lock); g_isx019_private.jpg_quality = val.value32; return OK; @@ -3215,15 +3193,15 @@ static int isx019_set_value(uint32_t id, int isx019_initialize(void) { imgsensor_register(&g_isx019_ops); - nxsem_init(&g_isx019_private.i2c_lock, 0, 1); - nxsem_init(&g_isx019_private.fpga_lock, 0, 1); + nxmutex_init(&g_isx019_private.i2c_lock); + nxmutex_init(&g_isx019_private.fpga_lock); return OK; } int isx019_uninitialize(void) { - nxsem_destroy(&g_isx019_private.i2c_lock); - nxsem_destroy(&g_isx019_private.fpga_lock); + nxmutex_destroy(&g_isx019_private.i2c_lock); + nxmutex_destroy(&g_isx019_private.fpga_lock); return OK; } diff --git a/drivers/video/max7456.c b/drivers/video/max7456.c index 9c842d1c16c57..6a8ae7fd2afdf 100644 --- a/drivers/video/max7456.c +++ b/drivers/video/max7456.c @@ -888,41 +888,6 @@ static inline void __mx7_read_nvm(FAR struct mx7_dev_s *dev) while (stat & STAT__CHARUNAVAIL); } -/**************************************************************************** - * Name: __lock - * - * Description: - * Locks the @dev data structure (mutex) to protect it against concurrent - * access. This is necessary, because @dev has some state information in it - * that has to be kept consistent with the chip. This lock also protects - * operations that must not be interrupted by other access to the chip. - * - * Use this function before calling one of the lock-dependent helper - * functions defined above (there are some defined below here, too). - * - ****************************************************************************/ - -static void inline __lock(FAR struct mx7_dev_s *dev) -{ - nxmutex_lock(&dev->lock); -} - -/**************************************************************************** - * Name: __unlock - * - * Description: - * Unlocks the @dev data structure (mutex). - * - * Use this function after calling one of the lock-dependent helper - * functions defined above (there are some defined below here, too). - * - ****************************************************************************/ - -static void inline __unlock(FAR struct mx7_dev_s *dev) -{ - nxmutex_unlock(&dev->lock); -} - /**************************************************************************** * Name: mx7_reset * @@ -935,7 +900,7 @@ static void inline __unlock(FAR struct mx7_dev_s *dev) static void mx7_reset(FAR struct mx7_dev_s *dev) { - __lock(dev); + nxmutex_lock(&dev->lock); /* Issue the reset command. */ @@ -947,7 +912,7 @@ static void mx7_reset(FAR struct mx7_dev_s *dev) /* All done. */ - __unlock(dev); + nxmutex_unlock(&dev->lock); } /**************************************************************************** @@ -1296,9 +1261,9 @@ static ssize_t mx7_read_cm(FAR struct file *filep, FAR char *buf, size_t len) FAR struct mx7_dev_s *dev = inode->i_private; ssize_t ret; - __lock(dev); + nxmutex_lock(&dev->lock); ret = __read_cm(dev, filep->f_pos, (FAR uint8_t *) buf, len); - __unlock(dev); + nxmutex_unlock(&dev->lock); return ret; } @@ -1380,9 +1345,9 @@ static ssize_t mx7_write_fb(FAR struct file *filep, FAR const char *buf, FAR struct mx7_dev_s *dev = inode->i_private; ssize_t ret; - __lock(dev); + nxmutex_lock(&dev->lock); ret = __write_fb(dev, (FAR uint8_t *) buf, len, dev->ca, filep->f_pos); - __unlock(dev); + nxmutex_unlock(&dev->lock); return ret; } @@ -1567,9 +1532,9 @@ static ssize_t mx7_debug_read(FAR struct file *filep, /* Read the register. */ - __lock(dev); + nxmutex_lock(&dev->lock); ret = __mx7_read_reg(dev, addr, &val, 1); - __unlock(dev); + nxmutex_unlock(&dev->lock); if (ret != 1) { @@ -1622,9 +1587,9 @@ static ssize_t mx7_debug_write(FAR struct file *filep, FAR const char *buf, /* Write the register value. */ - __lock(dev); + nxmutex_lock(&dev->lock); __mx7_write_reg(dev, addr, &val, 1); - __unlock(dev); + nxmutex_unlock(&dev->lock); return len; } @@ -1741,7 +1706,7 @@ int max7456_register(FAR const char *path, FAR struct mx7_config_s *config) * I'm doing it anyway for consistency. */ - __lock(dev); + nxmutex_lock(&dev->lock); /* Thus sayeth the datasheet (pp. 38): * @@ -1816,7 +1781,7 @@ int max7456_register(FAR const char *path, FAR struct mx7_config_s *config) /* Release the device to the world. */ - __unlock(dev); + nxmutex_unlock(&dev->lock); return 0; } diff --git a/drivers/video/video.c b/drivers/video/video.c index 35ddd9fdbe97b..4f4af326b6786 100644 --- a/drivers/video/video.c +++ b/drivers/video/video.c @@ -37,6 +37,7 @@ #include #include #include +#include #include @@ -113,7 +114,7 @@ typedef struct video_format_s video_format_t; struct video_type_inf_s { - sem_t lock_state; + mutex_t lock_state; enum video_state_e state; int32_t remaining_capnum; video_wait_capture_t wait_capture; @@ -129,7 +130,7 @@ typedef struct video_type_inf_s video_type_inf_t; struct video_mng_s { FAR char *devpath; /* parameter of video_initialize() */ - sem_t lock_open_num; + mutex_t lock_open_num; uint8_t open_num; video_type_inf_t video_inf; video_type_inf_t still_inf; @@ -199,8 +200,6 @@ static int video_ioctl(FAR struct file *filep, int cmd, unsigned long arg); /* Common function */ -static int video_lock(FAR sem_t *sem); -static int video_unlock(FAR sem_t *sem); static FAR video_type_inf_t *get_video_type_inf (FAR video_mng_t *vmng, uint8_t type); static enum video_state_e estimate_next_video_state @@ -412,26 +411,6 @@ static FAR const struct imgdata_ops_s *g_video_data_ops; * Private Functions ****************************************************************************/ -static int video_lock(FAR sem_t *sem) -{ - if (sem == NULL) - { - return -EINVAL; - } - - return nxsem_wait_uninterruptible(sem); -} - -static int video_unlock(FAR sem_t *sem) -{ - if (sem == NULL) - { - return -EINVAL; - } - - return nxsem_post(sem); -} - static FAR video_type_inf_t *get_video_type_inf (FAR video_mng_t *vmng, uint8_t type) { @@ -758,7 +737,7 @@ static void initialize_streamresources(FAR video_type_inf_t *type_inf) { memset(type_inf, 0, sizeof(video_type_inf_t)); type_inf->remaining_capnum = VIDEO_REMAINING_CAPNUM_INFINITY; - nxsem_init(&type_inf->lock_state, 0, 1); + nxmutex_init(&type_inf->lock_state); nxsem_init(&type_inf->wait_capture.dqbuf_wait_flg, 0, 0); initialize_frame_setting(&type_inf->nr_fmt, type_inf->fmt, @@ -936,7 +915,7 @@ static void cleanup_streamresources(FAR video_type_inf_t *type_inf) { video_framebuff_uninit(&type_inf->bufinf); nxsem_destroy(&type_inf->wait_capture.dqbuf_wait_flg); - nxsem_destroy(&type_inf->lock_state); + nxmutex_destroy(&type_inf->lock_state); memset(type_inf, 0, sizeof(video_type_inf_t)); type_inf->remaining_capnum = VIDEO_REMAINING_CAPNUM_INFINITY; @@ -1023,7 +1002,7 @@ static int video_open(FAR struct file *filep) FAR video_mng_t *priv = (FAR video_mng_t *)inode->i_private; int ret = OK; - video_lock(&priv->lock_open_num); + nxmutex_lock(&priv->lock_open_num); if (priv->open_num == 0) { /* Only in first execution, open device */ @@ -1054,8 +1033,7 @@ static int video_open(FAR struct file *filep) priv->open_num++; } - video_unlock(&priv->lock_open_num); - + nxmutex_unlock(&priv->lock_open_num); return ret; } @@ -1065,7 +1043,7 @@ static int video_close(FAR struct file *filep) FAR video_mng_t *priv = (FAR video_mng_t *)inode->i_private; int ret = ERROR; - video_lock(&priv->lock_open_num); + nxmutex_lock(&priv->lock_open_num); if (priv->open_num == 0) { return OK; @@ -1080,8 +1058,7 @@ static int video_close(FAR struct file *filep) g_video_data_ops->uninit(); } - video_unlock(&priv->lock_open_num); - + nxmutex_unlock(&priv->lock_open_num); return ret; } @@ -1184,7 +1161,7 @@ static int video_qbuf(FAR struct video_mng_s *vmng, memcpy(&container->buf, buf, sizeof(struct v4l2_buffer)); video_framebuff_queue_container(&type_inf->bufinf, container); - video_lock(&type_inf->lock_state); + nxmutex_lock(&type_inf->lock_state); flags = enter_critical_section(); if (type_inf->state == VIDEO_STATE_STREAMON) { @@ -1192,11 +1169,11 @@ static int video_qbuf(FAR struct video_mng_s *vmng, if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { - video_lock(&vmng->still_inf.lock_state); + nxmutex_lock(&vmng->still_inf.lock_state); next_video_state = estimate_next_video_state (vmng, CAUSE_VIDEO_START); change_video_state(vmng, next_video_state); - video_unlock(&vmng->still_inf.lock_state); + nxmutex_unlock(&vmng->still_inf.lock_state); } else { @@ -1220,8 +1197,7 @@ static int video_qbuf(FAR struct video_mng_s *vmng, leave_critical_section(flags); } - video_unlock(&type_inf->lock_state); - + nxmutex_unlock(&type_inf->lock_state); return OK; } @@ -1679,7 +1655,7 @@ static int video_streamon(FAR struct video_mng_s *vmng, return OK; } - video_lock(&type_inf->lock_state); + nxmutex_lock(&type_inf->lock_state); if (type_inf->state != VIDEO_STATE_STREAMOFF) { @@ -1692,8 +1668,7 @@ static int video_streamon(FAR struct video_mng_s *vmng, change_video_state(vmng, next_video_state); } - video_unlock(&type_inf->lock_state); - + nxmutex_unlock(&type_inf->lock_state); return ret; } @@ -1780,7 +1755,7 @@ static int video_takepict_start(FAR struct video_mng_s *vmng, return -EINVAL; } - video_lock(&vmng->still_inf.lock_state); + nxmutex_lock(&vmng->still_inf.lock_state); if (vmng->still_inf.state != VIDEO_STATE_STREAMOFF) { @@ -1829,8 +1804,7 @@ static int video_takepict_start(FAR struct video_mng_s *vmng, } } - video_unlock(&vmng->still_inf.lock_state); - + nxmutex_unlock(&vmng->still_inf.lock_state); return ret; } @@ -1845,7 +1819,7 @@ static int video_takepict_stop(FAR struct video_mng_s *vmng, bool halfpush) return -EINVAL; } - video_lock(&vmng->still_inf.lock_state); + nxmutex_lock(&vmng->still_inf.lock_state); if ((vmng->still_inf.state == VIDEO_STATE_STREAMOFF) && (vmng->still_inf.remaining_capnum == VIDEO_REMAINING_CAPNUM_INFINITY)) @@ -1867,15 +1841,14 @@ static int video_takepict_stop(FAR struct video_mng_s *vmng, bool halfpush) /* Control video stream */ - video_lock(&vmng->video_inf.lock_state); + nxmutex_lock(&vmng->video_inf.lock_state); next_video_state = estimate_next_video_state(vmng, CAUSE_STILL_STOP); change_video_state(vmng, next_video_state); - video_unlock(&vmng->video_inf.lock_state); + nxmutex_unlock(&vmng->video_inf.lock_state); } - video_unlock(&vmng->still_inf.lock_state); - + nxmutex_unlock(&vmng->still_inf.lock_state); return ret; } @@ -3094,9 +3067,9 @@ static FAR void *video_register(FAR const char *devpath) memcpy(priv->devpath, devpath, allocsize); priv->devpath[allocsize] = '\0'; - /* Initialize semaphore */ + /* Initialize mutex */ - nxsem_init(&priv->lock_open_num, 0, 1); + nxmutex_init(&priv->lock_open_num); /* Register the character driver */ @@ -3122,8 +3095,7 @@ static int video_unregister(FAR video_mng_t *v_mgr) } else { - nxsem_destroy(&v_mgr->lock_open_num); - + nxmutex_destroy(&v_mgr->lock_open_num); unregister_driver((const char *)v_mgr->devpath); kmm_free(v_mgr->devpath); diff --git a/drivers/video/video_framebuff.c b/drivers/video/video_framebuff.c index d4e93d1d21013..cb892c1d0da49 100644 --- a/drivers/video/video_framebuff.c +++ b/drivers/video/video_framebuff.c @@ -103,13 +103,13 @@ void video_framebuff_init(video_framebuff_t *fbuf) fbuf->vbuf_tail = NULL; fbuf->vbuf_next = NULL; - nxsem_init(&fbuf->lock_empty, 0, 1); + nxmutex_init(&fbuf->lock_empty); } void video_framebuff_uninit(video_framebuff_t *fbuf) { video_framebuff_realloc_container(fbuf, 0); - nxsem_destroy(&fbuf->lock_empty); + nxmutex_destroy(&fbuf->lock_empty); } int video_framebuff_realloc_container(video_framebuff_t *fbuf, int sz) @@ -154,7 +154,7 @@ vbuf_container_t *video_framebuff_get_container(video_framebuff_t *fbuf) { vbuf_container_t *ret; - nxsem_wait_uninterruptible(&fbuf->lock_empty); + nxmutex_lock(&fbuf->lock_empty); ret = fbuf->vbuf_empty; if (ret) { @@ -162,18 +162,17 @@ vbuf_container_t *video_framebuff_get_container(video_framebuff_t *fbuf) ret->next = NULL; } - nxsem_post(&fbuf->lock_empty); - + nxmutex_unlock(&fbuf->lock_empty); return ret; } void video_framebuff_free_container(video_framebuff_t *fbuf, vbuf_container_t *cnt) { - nxsem_wait_uninterruptible(&fbuf->lock_empty); + nxmutex_lock(&fbuf->lock_empty); cnt->next = fbuf->vbuf_empty; fbuf->vbuf_empty = cnt; - nxsem_post(&fbuf->lock_empty); + nxmutex_unlock(&fbuf->lock_empty); } void video_framebuff_queue_container(video_framebuff_t *fbuf, diff --git a/drivers/video/video_framebuff.h b/drivers/video/video_framebuff.h index f1623f47a61b2..c0d5a2b23888c 100644 --- a/drivers/video/video_framebuff.h +++ b/drivers/video/video_framebuff.h @@ -26,7 +26,7 @@ ****************************************************************************/ #include -#include +#include /**************************************************************************** * Public Types @@ -43,7 +43,7 @@ typedef struct vbuf_container_s vbuf_container_t; struct video_framebuff_s { enum v4l2_buf_mode mode; - sem_t lock_empty; + mutex_t lock_empty; int container_size; vbuf_container_t *vbuf_alloced; vbuf_container_t *vbuf_empty; diff --git a/drivers/video/vnc/vnc_updater.c b/drivers/video/vnc/vnc_updater.c index 38329ec39c0cd..b0ade582fc768 100644 --- a/drivers/video/vnc/vnc_updater.c +++ b/drivers/video/vnc/vnc_updater.c @@ -48,6 +48,9 @@ #include #include +#ifdef VNCSERVER_SEM_DEBUG +# include +#endif #include "vnc_server.h" @@ -63,7 +66,7 @@ ****************************************************************************/ #ifdef VNCSERVER_SEM_DEBUG -static sem_t g_errsem = SEM_INITIALIZER(1); +static mutex_t g_errlock = NXMUTEX_INITIALIZER; #endif /* A rectangle represent the entire local framebuffer */ @@ -107,7 +110,7 @@ static void vnc_sem_debug(FAR struct vnc_session_s *session, int queuewaiting; int ret; - ret = nxsem_wait_uninterruptible(&g_errsem); + ret = nxmutex_lock(&g_errlock); if (ret < 0) { /* Should happen only on task cancellation */ @@ -153,7 +156,7 @@ static void vnc_sem_debug(FAR struct vnc_session_s *session, syslog(LOG_INFO, " Unqueued: %u\n", unattached); } - nxsem_post(&g_errsem); + nxmutex_unlock(&g_errlock); } #else # define vnc_sem_debug(s,m,u) diff --git a/drivers/wireless/cc1101.c b/drivers/wireless/cc1101.c index 6b876f69d3841..3c3fd2cc2f58a 100644 --- a/drivers/wireless/cc1101.c +++ b/drivers/wireless/cc1101.c @@ -315,17 +315,6 @@ static const struct file_operations g_cc1101ops = * Private Functions ****************************************************************************/ -static int cc1101_takesem(FAR sem_t *sem) -{ - return nxsem_wait(sem); -} - -/**************************************************************************** - * Name: cc1101_givesem - ****************************************************************************/ - -#define cc1101_givesem(sem) nxsem_post(sem) - /**************************************************************************** * Name: cc1101_file_open * @@ -350,7 +339,7 @@ static int cc1101_file_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = cc1101_takesem(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -369,7 +358,7 @@ static int cc1101_file_open(FAR struct file *filep) dev->nopens++; errout: - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -398,7 +387,7 @@ static int cc1101_file_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = cc1101_takesem(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -410,7 +399,7 @@ static int cc1101_file_close(FAR struct file *filep) #endif dev->nopens--; - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return OK; } @@ -440,7 +429,7 @@ static ssize_t cc1101_file_write(FAR struct file *filep, /* Get exclusive access to the driver data structure */ - ret = cc1101_takesem(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -448,7 +437,7 @@ static ssize_t cc1101_file_write(FAR struct file *filep, ret = cc1101_write(dev, (const uint8_t *)buffer, buflen); cc1101_send(dev); - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -465,7 +454,7 @@ static void fifo_put(FAR struct cc1101_dev_s *dev, FAR uint8_t *buffer, int ret; int i; - ret = cc1101_takesem(&dev->sem_rx_buffer); + ret = nxmutex_lock(&dev->lock_rx_buffer); if (ret < 0) { return; @@ -484,7 +473,7 @@ static void fifo_put(FAR struct cc1101_dev_s *dev, FAR uint8_t *buffer, } dev->nxt_write = (dev->nxt_write + 1) % CONFIG_WL_CC1101_RXFIFO_LEN; - nxsem_post(&dev->sem_rx_buffer); + nxmutex_unlock(&dev->lock_rx_buffer); } /**************************************************************************** @@ -501,7 +490,7 @@ static uint8_t fifo_get(FAR struct cc1101_dev_s *dev, FAR uint8_t *buffer, uint8_t i; int ret; - ret = cc1101_takesem(&dev->sem_rx_buffer); + ret = nxmutex_lock(&dev->lock_rx_buffer); if (ret < 0) { return ret; @@ -525,7 +514,7 @@ static uint8_t fifo_get(FAR struct cc1101_dev_s *dev, FAR uint8_t *buffer, dev->fifo_len--; no_data: - nxsem_post(&dev->sem_rx_buffer); + nxmutex_unlock(&dev->lock_rx_buffer); return pktlen; } @@ -550,7 +539,7 @@ static ssize_t cc1101_file_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); dev = (FAR struct cc1101_dev_s *)inode->i_private; - ret = cc1101_takesem(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -572,7 +561,7 @@ static ssize_t cc1101_file_read(FAR struct file *filep, FAR char *buffer, } buflen = fifo_get(dev, (uint8_t *)buffer, buflen); - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return buflen; } @@ -601,7 +590,7 @@ static int cc1101_file_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Exclusive access */ - ret = cc1101_takesem(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -636,13 +625,13 @@ static int cc1101_file_poll(FAR struct file *filep, FAR struct pollfd *fds, * don't wait for RX. */ - cc1101_takesem(&dev->sem_rx_buffer); + nxmutex_lock(&dev->lock_rx_buffer); if (dev->fifo_len > 0) { poll_notify(&dev->pfd, 1, POLLIN); } - nxsem_post(&dev->sem_rx_buffer); + nxmutex_unlock(&dev->lock_rx_buffer); } else /* Tear it down */ { @@ -650,7 +639,7 @@ static int cc1101_file_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -1422,7 +1411,7 @@ int cc1101_send(FAR struct cc1101_dev_s *dev) } cc1101_strobe(dev, CC1101_STX); - cc1101_takesem(&dev->sem_tx); + nxsem_wait(&dev->sem_tx); /* this is set MCSM1, send auto to rx */ @@ -1489,8 +1478,8 @@ int cc1101_register(FAR const char *path, FAR struct cc1101_dev_s *dev) dev->nxt_read = 0; dev->nxt_write = 0; dev->fifo_len = 0; - nxsem_init(&(dev->devsem), 0, 1); - nxsem_init(&(dev->sem_rx_buffer), 0, 1); + nxmutex_init(&dev->devlock); + nxmutex_init(&dev->lock_rx_buffer); nxsem_init(&(dev->sem_rx), 0, 0); nxsem_init(&(dev->sem_tx), 0, 0); diff --git a/drivers/wireless/gs2200m.c b/drivers/wireless/gs2200m.c index e1931898ff7b0..a7f8ee6315fc1 100644 --- a/drivers/wireless/gs2200m.c +++ b/drivers/wireless/gs2200m.c @@ -49,7 +49,7 @@ #include #include #include -#include +#include #include #include #include @@ -180,7 +180,7 @@ struct gs2200m_dev_s struct notif_q_s notif_q; FAR struct spi_dev_s *spi; struct work_s irq_work; - sem_t dev_sem; + mutex_t dev_lock; bool int_enabled; dq_queue_t pkt_q[16]; uint16_t pkt_q_cnt[16]; @@ -691,24 +691,6 @@ static bool _copy_data_from_pkt(FAR struct gs2200m_dev_s *dev, return ret; } -/**************************************************************************** - * Name: gs2200m_lock - ****************************************************************************/ - -static int gs2200m_lock(FAR struct gs2200m_dev_s *dev) -{ - return nxsem_wait_uninterruptible(&dev->dev_sem); -} - -/**************************************************************************** - * Name: gs2200m_unlock - ****************************************************************************/ - -static void gs2200m_unlock(FAR struct gs2200m_dev_s *dev) -{ - nxsem_post(&dev->dev_sem); -} - /**************************************************************************** * Name: gs2200m_read ****************************************************************************/ @@ -728,7 +710,7 @@ static ssize_t gs2200m_read(FAR struct file *filep, FAR char *buffer, ASSERT(1 == len); - ret = nxsem_wait(&dev->dev_sem); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { /* Return if a signal is received or if the the task was canceled @@ -747,7 +729,7 @@ static ssize_t gs2200m_read(FAR struct file *filep, FAR char *buffer, memcpy(buffer, &cid, sizeof(cid)); - gs2200m_unlock(dev); + nxmutex_unlock(&dev->dev_lock); return 1; } @@ -2993,7 +2975,7 @@ static int gs2200m_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Lock the device */ - ret = gs2200m_lock(dev); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { /* Return only if the task was canceled */ @@ -3107,7 +3089,7 @@ static int gs2200m_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Unlock the device */ - gs2200m_unlock(dev); + nxmutex_unlock(&dev->dev_lock); return ret; } @@ -3129,7 +3111,7 @@ static int gs2200m_poll(FAR struct file *filep, FAR struct pollfd *fds, DEBUGASSERT(inode && inode->i_private); dev = (FAR struct gs2200m_dev_s *)inode->i_private; - ret = gs2200m_lock(dev); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { /* Return if the task was canceled */ @@ -3172,7 +3154,7 @@ static int gs2200m_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - gs2200m_unlock(dev); + nxmutex_unlock(&dev->dev_lock); return ret; } @@ -3199,7 +3181,7 @@ static void gs2200m_irq_worker(FAR void *arg) do { - ret = gs2200m_lock(dev); + ret = nxmutex_lock(&dev->dev_lock); /* The only failure would be if the worker thread were canceled. That * is very unlikely, however. @@ -3350,7 +3332,7 @@ static void gs2200m_irq_worker(FAR void *arg) /* NOTE: Enable gs2200m irq which was disabled in gs2200m_irq() */ dev->lower->enable(); - gs2200m_unlock(dev); + nxmutex_unlock(&dev->dev_lock); } /**************************************************************************** @@ -3522,7 +3504,7 @@ FAR void *gs2200m_register(FAR const char *devpath, dev->path = strdup(devpath); dev->lower = lower; - nxsem_init(&dev->dev_sem, 0, 1); + nxmutex_init(&dev->dev_lock); dev->pfd = NULL; diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_cdc.c b/drivers/wireless/ieee80211/bcm43xxx/bcmf_cdc.c index 1c2fc7fb2ce45..7655eea3ee45a 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_cdc.c +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_cdc.c @@ -175,7 +175,6 @@ int bcmf_cdc_control_request(FAR struct bcmf_dev_s *priv, name, data, len); nxsem_post(&priv->control_mutex); - return ret; } @@ -238,7 +237,8 @@ int bcmf_cdc_control_request_unsafe(FAR struct bcmf_dev_s *priv, return ret; } - ret = bcmf_sem_wait(&priv->control_timeout, BCMF_CONTROL_TIMEOUT_MS); + ret = nxsem_tickwait_uninterruptible(&priv->control_timeout, + MSEC2TICK(BCMF_CONTROL_TIMEOUT_MS)); if (ret < 0) { wlerr("Error while waiting for control response %d\n", ret); diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_driver.c b/drivers/wireless/ieee80211/bcm43xxx/bcmf_driver.c index e4b7b71a32779..9e857ddfd57af 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_driver.c +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_driver.c @@ -1804,8 +1804,8 @@ int bcmf_wl_set_ssid(FAR struct bcmf_dev_s *priv, struct iwreq *iwr) WLC_DISASSOC, (uint8_t *)&scbval, &out_len); } - ret = bcmf_sem_wait(&priv->auth_signal, BCMF_AUTH_TIMEOUT_MS); - + ret = nxsem_tickwait_uninterruptible(&priv->auth_signal, + MSEC2TICK(BCMF_AUTH_TIMEOUT_MS)); wlinfo("semwait done ! %d\n", ret); if (ret < 0) diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi.c b/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi.c index 950358823f7ca..a54fa4fb40559 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi.c +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi.c @@ -856,7 +856,7 @@ static int bcmf_bus_gspi_initialize(FAR struct bcmf_dev_s *priv, /* Init transmit frames queue */ - if ((ret = nxsem_init(&gbus->queue_mutex, 0, 1)) != OK) + if ((ret = nxmutex_init(&gbus->queue_lock)) != OK) { goto exit_free_bus; } diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi.h b/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi.h index 58cd1d361e529..06b00d9a167c7 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi.h +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi.h @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -50,6 +51,29 @@ /* gSPI bus structure extension */ +/* Note: + * The structure referred to above must as its first item: + * + * struct bcmf_bus_dev_s bus; --- Default bcmf bus structure + * + * The structure must also contain the following items: + * + * int cur_chip_id; --- Chip ID read from the card + * struct bcmf_chip_data *chip; --- Chip specific configuration + * + * sem_t thread_signal; --- Thread event semaphore + * + * uint32_t backplane_current_addr; --- Current F1 backplane base addr + * + * uint8_t max_seq; --- Maximum TX sequence allowed + * uint8_t tx_seq; --- TX sequence number (next) + * + * mutex_t queue_lock; --- Lock for TX/RX/free queues + * struct list_node tx_queue; --- Queue of frames to transmit + * struct list_node rx_queue; --- Queue of frames for receiving + * volatile int tx_queue_count; --- Count of items in TX queue + */ + typedef struct bcmf_gspi_dev_s { struct bcmf_bus_dev_s bus; /* Default bcmf bus structure */ @@ -73,7 +97,7 @@ typedef struct bcmf_gspi_dev_s uint8_t max_seq; /* Maximum transmit sequence allowed */ uint8_t tx_seq; /* Transmit sequence number (next) */ - sem_t queue_mutex; /* Lock for TX/RX/free queues */ + mutex_t queue_lock; /* Lock for TX/RX/free queues */ struct list_node tx_queue; /* Queue of frames to transmit */ struct list_node rx_queue; /* Queue of frames used to receive */ volatile int tx_queue_count; /* Count of items in TX queue */ diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi_f2_frame.c b/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi_f2_frame.c index 586d636eeade7..ae51d4a7369f4 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi_f2_frame.c +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_gspi_f2_frame.c @@ -264,13 +264,13 @@ int bcmf_gspi_read_f2_frame(FAR struct bcmf_dev_s *priv, /* Queue frame and notify network layer frame is available */ - if (nxsem_wait_uninterruptible(&gbus->queue_mutex) < 0) + if (nxmutex_lock(&gbus->queue_lock) < 0) { DEBUGPANIC(); } list_add_tail(&gbus->rx_queue, &iframe->list_entry); - nxsem_post(&gbus->queue_mutex); + nxmutex_unlock(&gbus->queue_lock); bcmf_netdev_notify_rx(priv); @@ -331,7 +331,7 @@ int bcmf_gspi_send_f2_frame(FAR struct bcmf_dev_s *priv) return -EAGAIN; } - if (nxsem_wait_uninterruptible(&gbus->queue_mutex) < 0) + if (nxmutex_lock(&gbus->queue_lock) < 0) { DEBUGPANIC(); } @@ -340,7 +340,7 @@ int bcmf_gspi_send_f2_frame(FAR struct bcmf_dev_s *priv) bcmf_interface_frame_t, list_entry); - nxsem_post(&gbus->queue_mutex); + nxmutex_unlock(&gbus->queue_lock); is_txframe = iframe->tx; diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_interface.c b/drivers/wireless/ieee80211/bcm43xxx/bcmf_interface.c index a6aa74f24532f..ad9d398fb5fc6 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_interface.c +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_interface.c @@ -68,7 +68,7 @@ void bcmf_interface_free_frame(FAR struct bcmf_dev_s *priv, { FAR bcmf_interface_dev_t *ibus = (FAR bcmf_interface_dev_t *) priv->bus; - if (nxsem_wait_uninterruptible(&ibus->queue_mutex) < 0) + if (nxmutex_lock(&ibus->queue_lock) < 0) { DEBUGPANIC(); } @@ -80,7 +80,7 @@ void bcmf_interface_free_frame(FAR struct bcmf_dev_s *priv, ibus->tx_queue_count--; } - nxsem_post(&ibus->queue_mutex); + nxmutex_unlock(&ibus->queue_lock); } /**************************************************************************** @@ -97,7 +97,7 @@ bcmf_interface_frame_t while (1) { - if (nxsem_wait_uninterruptible(&ibus->queue_mutex) < 0) + if (nxmutex_lock(&ibus->queue_lock) < 0) { DEBUGPANIC(); } @@ -115,12 +115,12 @@ bcmf_interface_frame_t ibus->tx_queue_count++; } - nxsem_post(&ibus->queue_mutex); + nxmutex_unlock(&ibus->queue_lock); break; } } - nxsem_post(&ibus->queue_mutex); + nxmutex_unlock(&ibus->queue_lock); if (!block) { diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_interface.h b/drivers/wireless/ieee80211/bcm43xxx/bcmf_interface.h index e617e880f2f84..5710356d55ea3 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_interface.h +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_interface.h @@ -49,29 +49,6 @@ typedef struct bcmf_sdio_dev_s bcmf_interface_dev_t; typedef bcmf_gspi_dev_t bcmf_interface_dev_t; #endif -/* Note: - * The structure referred to above must as its first item: - * - * struct bcmf_bus_dev_s bus; --- Default bcmf bus structure - * - * The structure must also contain the following items: - * - * int cur_chip_id; --- Chip ID read from the card - * struct bcmf_chip_data *chip; --- Chip specific configuration - * - * sem_t thread_signal; --- Thread event semaphore - * - * uint32_t backplane_current_addr; --- Current F1 backplane base addr - * - * uint8_t max_seq; --- Maximum TX sequence allowed - * uint8_t tx_seq; --- TX sequence number (next) - * - * sem_t queue_mutex; --- Lock for TX/RX/free queues - * struct list_node tx_queue; --- Queue of frames to transmit - * struct list_node rx_queue; --- Queue of frames for receiving - * volatile int tx_queue_count; --- Count of items in TX queue - */ - /* Structure used to manage interface frames */ typedef struct bcmf_interface_frame_s diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio.c b/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio.c index 9b2be08b0bfd1..50b8ab0393617 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio.c +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio.c @@ -674,7 +674,7 @@ static int bcmf_bus_sdio_initialize(FAR struct bcmf_dev_s *priv, /* Init transmit frames queue */ - if ((ret = nxsem_init(&sbus->queue_mutex, 0, 1)) != OK) + if ((ret = nxmutex_init(&sbus->queue_lock)) != OK) { goto exit_free_bus; } diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio.h b/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio.h index 5ee7fee3334fc..8ebe9abaed63c 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio.h +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdio.h @@ -32,6 +32,7 @@ #include #include +#include #include #include "bcmf_chip_data.h" @@ -54,6 +55,29 @@ /* SDIO bus structure extension */ +/* Note: + * The structure referred to above must as its first item: + * + * struct bcmf_bus_dev_s bus; --- Default bcmf bus structure + * + * The structure must also contain the following items: + * + * int cur_chip_id; --- Chip ID read from the card + * struct bcmf_chip_data *chip; --- Chip specific configuration + * + * sem_t thread_signal; --- Thread event semaphore + * + * uint32_t backplane_current_addr; --- Current F1 backplane base addr + * + * uint8_t max_seq; --- Maximum TX sequence allowed + * uint8_t tx_seq; --- TX sequence number (next) + * + * mutex_t queue_lock; --- Lock for TX/RX/free queues + * struct list_node tx_queue; --- Queue of frames to transmit + * struct list_node rx_queue; --- Queue of frames for receiving + * volatile int tx_queue_count; --- Count of items in TX queue + */ + struct bcmf_sdio_dev_s { struct bcmf_bus_dev_s bus; /* Default bcmf bus structure */ @@ -81,7 +105,7 @@ struct bcmf_sdio_dev_s uint8_t rx_seq; /* Receive sequence number (expected) */ bool flow_ctrl; /* Current flow control status */ - sem_t queue_mutex; /* Lock for TX/RX/free queues */ + mutex_t queue_lock; /* Lock for TX/RX/free queues */ struct list_node tx_queue; /* Queue of frames to transmit */ struct list_node rx_queue; /* Queue of frames used to receive */ volatile int tx_queue_count; /* Count of items in TX queue */ diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdpcm.c b/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdpcm.c index eed19c26c09ce..6e9f000997b91 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdpcm.c +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_sdpcm.c @@ -323,13 +323,13 @@ int bcmf_sdpcm_readframe(FAR struct bcmf_dev_s *priv) /* Queue frame and notify network layer frame is available */ - if (nxsem_wait_uninterruptible(&ibus->queue_mutex) < 0) + if (nxmutex_lock(&ibus->queue_lock) < 0) { DEBUGPANIC(); } list_add_tail(&ibus->rx_queue, &iframe->list_entry); - nxsem_post(&ibus->queue_mutex); + nxmutex_unlock(&ibus->queue_lock); bcmf_netdev_notify_rx(priv); @@ -376,14 +376,14 @@ int bcmf_sdpcm_sendframe(FAR struct bcmf_dev_s *priv) return -EAGAIN; } - if (nxsem_wait_uninterruptible(&ibus->queue_mutex) < 0) + if (nxmutex_lock(&ibus->queue_lock) < 0) { DEBUGPANIC(); } iframe = list_remove_head_type(&ibus->tx_queue, bcmf_interface_frame_t, list_entry); - nxsem_post(&ibus->queue_mutex); + nxmutex_unlock(&ibus->queue_lock); header = (struct bcmf_sdpcm_header *)iframe->header.base; @@ -448,14 +448,14 @@ int bcmf_sdpcm_queue_frame(FAR struct bcmf_dev_s *priv, /* Add frame in tx queue */ - if (nxsem_wait_uninterruptible(&ibus->queue_mutex) < 0) + if (nxmutex_lock(&ibus->queue_lock) < 0) { DEBUGPANIC(); } list_add_tail(&ibus->tx_queue, &iframe->list_entry); - nxsem_post(&ibus->queue_mutex); + nxmutex_unlock(&ibus->queue_lock); /* Notify bcmf thread tx frame is ready */ @@ -511,7 +511,7 @@ struct bcmf_frame_s *bcmf_sdpcm_get_rx_frame(FAR struct bcmf_dev_s *priv) bcmf_interface_frame_t *iframe; FAR bcmf_interface_dev_t *ibus = (FAR bcmf_interface_dev_t *)priv->bus; - if (nxsem_wait_uninterruptible(&ibus->queue_mutex) < 0) + if (nxmutex_lock(&ibus->queue_lock) < 0) { DEBUGPANIC(); } @@ -520,7 +520,7 @@ struct bcmf_frame_s *bcmf_sdpcm_get_rx_frame(FAR struct bcmf_dev_s *priv) bcmf_interface_frame_t, list_entry); - nxsem_post(&ibus->queue_mutex); + nxmutex_unlock(&ibus->queue_lock); if (iframe == NULL) { diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.c b/drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.c index b81ea7391b04e..bf1cf902851e2 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.c +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.c @@ -77,12 +77,3 @@ void bcmf_hexdump(uint8_t *data, unsigned int len, unsigned long offset) wlinfo("%08lx: %s%s\n", offset + i - char_count, hex_line, char_line); } } - -/**************************************************************************** - * Name: bcmf_sem_wait - ****************************************************************************/ - -int bcmf_sem_wait(sem_t *sem, unsigned int timeout_ms) -{ - return nxsem_tickwait_uninterruptible(sem, MSEC2TICK(timeout_ms)); -} diff --git a/drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.h b/drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.h index a1165c3fa76ae..0fadea6a61d74 100644 --- a/drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.h +++ b/drivers/wireless/ieee80211/bcm43xxx/bcmf_utils.h @@ -27,8 +27,6 @@ #include -#include - #ifndef min #define min(a,b) ((a) < (b) ? (a) : (b)) #endif @@ -43,8 +41,6 @@ void bcmf_hexdump(uint8_t *data, unsigned int len, unsigned long offset); -int bcmf_sem_wait(sem_t *sem, unsigned int timeout_ms); - static inline uint16_t bcmf_getle16(void *val) { uint8_t *valb = (uint8_t *)val; diff --git a/drivers/wireless/ieee802154/mrf24j40/mrf24j40.c b/drivers/wireless/ieee802154/mrf24j40/mrf24j40.c index 796ac417c29fe..5f3e6e0df00ad 100644 --- a/drivers/wireless/ieee802154/mrf24j40/mrf24j40.c +++ b/drivers/wireless/ieee802154/mrf24j40/mrf24j40.c @@ -146,7 +146,7 @@ void mrf24j40_dopoll_csma(FAR void *arg) /* Get exclusive access to the driver */ - while (nxsem_wait(&dev->exclsem) < 0) + while (nxmutex_lock(&dev->lock) < 0) { } @@ -172,7 +172,7 @@ void mrf24j40_dopoll_csma(FAR void *arg) } } - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); } /**************************************************************************** @@ -205,7 +205,7 @@ void mrf24j40_dopoll_gts(FAR void *arg) /* Get exclusive access to the driver */ - while (nxsem_wait(&dev->exclsem) < 0) + while (nxmutex_lock(&dev->lock) < 0) { } @@ -228,7 +228,7 @@ void mrf24j40_dopoll_gts(FAR void *arg) } } - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); } /**************************************************************************** @@ -437,7 +437,7 @@ FAR struct ieee802154_radio_s * /* Allow exclusive access to the privmac struct */ - nxsem_init(&dev->exclsem, 0, 1); + nxmutex_init(&dev->lock); dev->radio.bind = mrf24j40_bind; dev->radio.reset = mrf24j40_reset; diff --git a/drivers/wireless/ieee802154/mrf24j40/mrf24j40.h b/drivers/wireless/ieee802154/mrf24j40/mrf24j40.h index cfa02860cfad9..cfcc731eb4ad0 100644 --- a/drivers/wireless/ieee802154/mrf24j40/mrf24j40.h +++ b/drivers/wireless/ieee802154/mrf24j40/mrf24j40.h @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include @@ -122,7 +122,7 @@ struct mrf24j40_radio_s struct work_s csma_pollwork; /* For deferring poll work to the work queue */ struct work_s gts_pollwork; /* For deferring poll work to the work queue */ - sem_t exclsem; /* Exclusive access to this struct */ + mutex_t lock; /* Exclusive access to this struct */ /* MAC Attributes */ diff --git a/drivers/wireless/ieee802154/mrf24j40/mrf24j40_interrupt.c b/drivers/wireless/ieee802154/mrf24j40/mrf24j40_interrupt.c index df528c2d42f7f..007e60a9bd4da 100644 --- a/drivers/wireless/ieee802154/mrf24j40/mrf24j40_interrupt.c +++ b/drivers/wireless/ieee802154/mrf24j40/mrf24j40_interrupt.c @@ -134,9 +134,9 @@ static void mrf24j40_irqwork_txnorm(FAR struct mrf24j40_radio_s *dev) /* Must unlock the radio before calling poll */ - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); mrf24j40_dopoll_csma(dev); - while (nxsem_wait(&dev->exclsem) < 0) + while (nxmutex_lock(&dev->lock) < 0) { } } @@ -313,7 +313,7 @@ void mrf24j40_irqworker(FAR void *arg) /* Get exclusive access to the driver */ - while (nxsem_wait(&dev->exclsem) < 0) + while (nxmutex_lock(&dev->lock) < 0) { } @@ -399,7 +399,7 @@ void mrf24j40_irqworker(FAR void *arg) /* Unlock the radio device */ - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); /* Re-enable GPIO interrupts */ diff --git a/drivers/wireless/ieee802154/mrf24j40/mrf24j40_radif.c b/drivers/wireless/ieee802154/mrf24j40/mrf24j40_radif.c index 261364d07c610..802ce8ec6e1bd 100644 --- a/drivers/wireless/ieee802154/mrf24j40/mrf24j40_radif.c +++ b/drivers/wireless/ieee802154/mrf24j40/mrf24j40_radif.c @@ -300,7 +300,7 @@ int mrf24j40_txdelayed(FAR struct ieee802154_radio_s *radio, /* Get exclusive access to the radio device */ - ret = nxsem_wait(&dev->exclsem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -327,12 +327,12 @@ int mrf24j40_txdelayed(FAR struct ieee802154_radio_s *radio, if (!work_available(&dev->irqwork)) { - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); mrf24j40_irqworker((FAR void *)dev); /* Get exclusive access to the radio device */ - ret = nxsem_wait(&dev->exclsem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -355,8 +355,7 @@ int mrf24j40_txdelayed(FAR struct ieee802154_radio_s *radio, mrf24j40_mactimer(dev, symboldelay); } - nxsem_post(&dev->exclsem); - + nxmutex_unlock(&dev->lock); return OK; } diff --git a/drivers/wireless/ieee802154/xbee/xbee.c b/drivers/wireless/ieee802154/xbee/xbee.c index f21d3e95f28f8..cf630c38d3dd4 100644 --- a/drivers/wireless/ieee802154/xbee/xbee.c +++ b/drivers/wireless/ieee802154/xbee/xbee.c @@ -960,10 +960,10 @@ static void xbee_process_txstatus(FAR struct xbee_priv_s *priv, static void xbee_notify(FAR struct xbee_priv_s *priv, FAR struct ieee802154_primitive_s *primitive) { - while (nxsem_wait(&priv->primitive_sem) < 0); + while (nxmutex_lock(&priv->primitive_lock) < 0); sq_addlast((FAR sq_entry_t *)primitive, &priv->primitive_queue); - nxsem_post(&priv->primitive_sem); + nxmutex_unlock(&priv->primitive_lock); if (work_available(&priv->notifwork)) { @@ -990,10 +990,10 @@ static void xbee_notify_worker(FAR void *arg) DEBUGASSERT(priv != NULL); - while (nxsem_wait(&priv->primitive_sem) < 0); + while (nxmutex_lock(&priv->primitive_lock) < 0); primitive = (FAR struct ieee802154_primitive_s *)sq_remfirst(&priv->primitive_queue); - nxsem_post(&priv->primitive_sem); + nxmutex_unlock(&priv->primitive_lock); while (primitive != NULL) { @@ -1063,11 +1063,11 @@ static void xbee_notify_worker(FAR void *arg) /* Get the next primitive then loop */ - while (nxsem_wait(&priv->primitive_sem) < 0); + while (nxmutex_lock(&priv->primitive_lock) < 0); primitive = (FAR struct ieee802154_primitive_s *) sq_remfirst(&priv->primitive_queue); - nxsem_post(&priv->primitive_sem); + nxmutex_unlock(&priv->primitive_lock); } } @@ -1246,9 +1246,9 @@ XBEEHANDLE xbee_init(FAR struct spi_dev_s *spi, priv->lower = lower; priv->spi = spi; - nxsem_init(&priv->primitive_sem, 0, 1); - nxsem_init(&priv->atquery_sem, 0, 1); - nxsem_init(&priv->tx_sem, 0, 1); + nxmutex_init(&priv->primitive_lock); + nxmutex_init(&priv->atquery_lock); + nxmutex_init(&priv->tx_lock); nxsem_init(&priv->txdone_sem, 0, 0); nxsem_set_protocol(&priv->txdone_sem, SEM_PRIO_NONE); @@ -1527,7 +1527,7 @@ int xbee_atquery(FAR struct xbee_priv_s *priv, FAR const char *atcommand) /* Only allow one query at a time */ - ret = nxsem_wait(&priv->atquery_sem); + ret = nxmutex_lock(&priv->atquery_lock); if (ret < 0) { return ret; @@ -1574,7 +1574,7 @@ int xbee_atquery(FAR struct xbee_priv_s *priv, FAR const char *atcommand) wd_cancel(&priv->atquery_wd); priv->querycmd[0] = 0; priv->querycmd[1] = 0; - nxsem_post(&priv->atquery_sem); + nxmutex_unlock(&priv->atquery_lock); return ret; } @@ -1589,8 +1589,7 @@ int xbee_atquery(FAR struct xbee_priv_s *priv, FAR const char *atcommand) } while (!priv->querydone); - nxsem_post(&priv->atquery_sem); - + nxmutex_unlock(&priv->atquery_lock); return OK; } diff --git a/drivers/wireless/ieee802154/xbee/xbee.h b/drivers/wireless/ieee802154/xbee/xbee.h index 247e6adcace6a..d42836eb8a792 100644 --- a/drivers/wireless/ieee802154/xbee/xbee.h +++ b/drivers/wireless/ieee802154/xbee/xbee.h @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -147,20 +148,20 @@ struct xbee_priv_s struct work_s notifwork; /* For deferring notifications to LPWORK queue */ struct work_s attnwork; /* For deferring interrupt work to work queue */ volatile bool attn_latched; /* Latched state of ATTN */ - sem_t primitive_sem; /* Exclusive access to the primitive queue */ + mutex_t primitive_lock; /* Exclusive access to the primitive queue */ sq_queue_t primitive_queue; /* Queue of primitives to pass via notify() * callback to registered receivers */ struct wdog_s assocwd; /* Association watchdog */ struct work_s assocwork; /* For polling for association status */ bool associating; /* Are we currently associating */ - sem_t atquery_sem; /* Only allow one AT query at a time */ + mutex_t atquery_lock; /* Only allow one AT query at a time */ sem_t atresp_sem; /* For signaling pending AT response received */ char querycmd[2]; /* Stores the pending AT Query command */ bool querydone; /* Used to tell waiting thread query is done */ struct wdog_s atquery_wd; /* Support AT Query timeout and retry */ struct wdog_s reqdata_wd; /* Support send timeout and retry */ uint8_t frameid; /* For differentiating AT request/response */ - sem_t tx_sem; /* Support a single pending transmit */ + mutex_t tx_lock; /* Support a single pending transmit */ sem_t txdone_sem; /* For signalling tx is completed */ bool txdone; #ifdef CONFIG_XBEE_LOCKUP_WORKAROUND diff --git a/drivers/wireless/ieee802154/xbee/xbee_mac.c b/drivers/wireless/ieee802154/xbee/xbee_mac.c index 080a029517aa6..c4c52442d087e 100644 --- a/drivers/wireless/ieee802154/xbee/xbee_mac.c +++ b/drivers/wireless/ieee802154/xbee/xbee_mac.c @@ -302,7 +302,7 @@ int xbee_req_data(XBEEHANDLE xbee, /* Support one pending transmit at a time */ - while (nxsem_wait(&priv->tx_sem) < 0); + while (nxmutex_lock(&priv->tx_lock) < 0); /* Figure out how much room we need to place the API frame header */ @@ -403,7 +403,7 @@ int xbee_req_data(XBEEHANDLE xbee, } while (!priv->txdone); - nxsem_post(&priv->tx_sem); + nxmutex_unlock(&priv->tx_lock); iob_free(frame); return OK; } diff --git a/drivers/wireless/ieee802154/xbee/xbee_netdev.c b/drivers/wireless/ieee802154/xbee/xbee_netdev.c index 7a4232c3de7f9..8ee754fcd740d 100644 --- a/drivers/wireless/ieee802154/xbee/xbee_netdev.c +++ b/drivers/wireless/ieee802154/xbee/xbee_netdev.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -119,7 +120,7 @@ struct xbeenet_driver_s /* For internal use by this driver */ - sem_t xd_exclsem; /* Exclusive access to struct */ + mutex_t xd_lock; /* Exclusive access to struct */ struct xbeenet_callback_s xd_cb; /* Callback information */ XBEEHANDLE xd_mac; /* Contained XBee MAC interface */ bool xd_bifup; /* true:ifup false:ifdown */ @@ -394,7 +395,7 @@ static int xbeenet_notify(FAR struct xbee_maccb_s *maccb, * again. */ - while (nxsem_wait(&priv->xd_exclsem) < 0); + while (nxmutex_lock(&priv->xd_lock) < 0); sq_addlast((FAR sq_entry_t *)primitive, &priv->primitive_queue); @@ -415,7 +416,7 @@ static int xbeenet_notify(FAR struct xbee_maccb_s *maccb, SI_QUEUE, &priv->xd_notify_work); } - nxsem_post(&priv->xd_exclsem); + nxmutex_unlock(&priv->xd_lock); return OK; } @@ -904,7 +905,7 @@ static int xbeenet_ioctl(FAR struct net_driver_s *dev, int cmd, (FAR struct xbeenet_driver_s *)dev->d_private; int ret = -EINVAL; - ret = nxsem_wait(&priv->xd_exclsem); + ret = nxmutex_lock(&priv->xd_lock); if (ret < 0) { wlerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -981,7 +982,7 @@ static int xbeenet_ioctl(FAR struct net_driver_s *dev, int cmd, } priv->xd_eventpending = true; - nxsem_post(&priv->xd_exclsem); + nxmutex_unlock(&priv->xd_lock); /* Wait to be signaled when an event is queued */ @@ -996,7 +997,7 @@ static int xbeenet_ioctl(FAR struct net_driver_s *dev, int cmd, * and try and pop an event off the queue */ - ret = nxsem_wait(&priv->xd_exclsem); + ret = nxmutex_lock(&priv->xd_lock); if (ret < 0) { wlerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -1029,7 +1030,7 @@ static int xbeenet_ioctl(FAR struct net_driver_s *dev, int cmd, ret = xbee_ioctl(priv->xd_mac, cmd, arg); } - nxsem_post(&priv->xd_exclsem); + nxmutex_unlock(&priv->xd_lock); return ret; } #endif @@ -1265,9 +1266,9 @@ int xbee_netdev_register(XBEEHANDLE xbee) dev->d_private = priv; /* Used to recover private state from dev */ priv->xd_mac = xbee; /* Save the MAC interface instance */ - /* Setup a locking semaphore for exclusive device driver access */ + /* Setup a locking mutex for exclusive device driver access */ - nxsem_init(&priv->xd_exclsem, 0, 1); + nxmutex_init(&priv->xd_lock); /* Set the network mask. */ diff --git a/drivers/wireless/lpwan/sx127x/sx127x.c b/drivers/wireless/lpwan/sx127x/sx127x.c index abfd7ffcbaa0c..b9e5b42d37395 100644 --- a/drivers/wireless/lpwan/sx127x/sx127x.c +++ b/drivers/wireless/lpwan/sx127x/sx127x.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -284,11 +285,11 @@ struct sx127x_dev_s uint8_t rx_buffer[SX127X_RXFIFO_TOTAL_SIZE]; sem_t rx_sem; /* Wait for availability of received data */ - sem_t rx_buffer_sem; /* Protect access to rx fifo */ + mutex_t rx_buffer_lock; /* Protect access to rx fifo */ #endif uint8_t nopens; /* Number of times the device has been opened */ - sem_t dev_sem; /* Ensures exclusive access to this structure */ + mutex_t dev_lock; /* Ensures exclusive access to this structure */ FAR struct pollfd *pfd; /* Polled file descr (or NULL if any) */ }; @@ -746,7 +747,7 @@ static int sx127x_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&dev->dev_sem); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { return ret; @@ -772,7 +773,7 @@ static int sx127x_open(FAR struct file *filep) dev->nopens++; errout: - nxsem_post(&dev->dev_sem); + nxmutex_unlock(&dev->dev_lock); return ret; } @@ -800,7 +801,7 @@ static int sx127x_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&dev->dev_sem); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { return ret; @@ -814,7 +815,7 @@ static int sx127x_close(FAR struct file *filep) dev->nopens--; - nxsem_post(&dev->dev_sem); + nxmutex_unlock(&dev->dev_lock); return OK; } @@ -842,7 +843,7 @@ static ssize_t sx127x_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); dev = (FAR struct sx127x_dev_s *)inode->i_private; - ret = nxsem_wait(&dev->dev_sem); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { return ret; @@ -867,8 +868,7 @@ static ssize_t sx127x_read(FAR struct file *filep, FAR char *buffer, ret = sx127x_rxfifo_get(dev, (uint8_t *)buffer, buflen); - nxsem_post(&dev->dev_sem); - + nxmutex_unlock(&dev->dev_lock); return ret; #endif } @@ -897,7 +897,7 @@ static ssize_t sx127x_write(FAR struct file *filep, FAR const char *buffer, DEBUGASSERT(inode && inode->i_private); dev = (FAR struct sx127x_dev_s *)inode->i_private; - ret = nxsem_wait(&dev->dev_sem); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { return ret; @@ -942,7 +942,7 @@ static ssize_t sx127x_write(FAR struct file *filep, FAR const char *buffer, */ sx127x_opmode_set(dev, dev->idle); - nxsem_post(&dev->dev_sem); + nxsem_post(&dev->dev_lock); return ret; #endif @@ -971,7 +971,7 @@ static int sx127x_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&dev->dev_sem); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { return ret; @@ -1154,7 +1154,7 @@ static int sx127x_ioctl(FAR struct file *filep, int cmd, unsigned long arg) } } - nxsem_post(&dev->dev_sem); + nxmutex_unlock(&dev->dev_lock); return ret; } @@ -1186,7 +1186,7 @@ static int sx127x_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Exclusive access */ - ret = nxsem_wait(&dev->dev_sem); + ret = nxmutex_lock(&dev->dev_lock); if (ret < 0) { return ret; @@ -1221,7 +1221,7 @@ static int sx127x_poll(FAR struct file *filep, FAR struct pollfd *fds, * don't wait for RX. */ - nxsem_wait(&dev->rx_buffer_sem); + nxmutex_lock(&dev->rx_buffer_lock); if (dev->rx_fifo_len > 0) { /* Data available for input */ @@ -1229,7 +1229,7 @@ static int sx127x_poll(FAR struct file *filep, FAR struct pollfd *fds, poll_notify(&dev->pfd, 1, POLLIN); } - nxsem_post(&dev->rx_buffer_sem); + nxmutex_unlock(&dev->rx_buffer_lock); } else /* Tear it down */ { @@ -1237,7 +1237,7 @@ static int sx127x_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&dev->dev_sem); + nxmutex_unlock(&dev->dev_lock); return ret; #endif } @@ -1708,7 +1708,7 @@ static ssize_t sx127x_rxfifo_get(FAR struct sx127x_dev_s *dev, size_t pktlen = 0; size_t ret = 0; - ret = nxsem_wait(&dev->rx_buffer_sem); + ret = nxmutex_lock(&dev->rx_buffer_lock); if (ret < 0) { return ret; @@ -1745,7 +1745,7 @@ static ssize_t sx127x_rxfifo_get(FAR struct sx127x_dev_s *dev, ret = pktlen; no_data: - nxsem_post(&dev->rx_buffer_sem); + nxmutex_unlock(&dev->rx_buffer_lock); return ret; } @@ -1763,7 +1763,7 @@ static void sx127x_rxfifo_put(FAR struct sx127x_dev_s *dev, size_t i = 0; int ret = 0; - ret = nxsem_wait(&dev->rx_buffer_sem); + ret = nxmutex_lock(&dev->rx_buffer_lock); if (ret < 0) { return; @@ -1785,7 +1785,7 @@ static void sx127x_rxfifo_put(FAR struct sx127x_dev_s *dev, } dev->nxt_write = (dev->nxt_write + 1) % CONFIG_LPWAN_SX127X_RXFIFO_LEN; - nxsem_post(&dev->rx_buffer_sem); + nxmutex_unlock(&dev->rx_buffer_lock); } #endif /* CONFIG_LPWAN_SX127X_RXSUPPORT */ @@ -4518,15 +4518,15 @@ static int sx127x_unregister(FAR struct sx127x_dev_s *dev) sx127x_attachirq0(dev, NULL, NULL); - /* Destroy semaphores */ + /* Destroy mutex & semaphores */ - nxsem_destroy(&dev->dev_sem); + nxmutex_destroy(&dev->dev_lock); #ifdef CONFIG_LPWAN_SX127X_TXSUPPORT nxsem_destroy(&dev->tx_sem); #endif #ifdef CONFIG_LPWAN_SX127X_RXSUPPORT nxsem_destroy(&dev->rx_sem); - nxsem_destroy(&dev->rx_buffer_sem); + nxmutex_destroy(&dev->rx_buffer_lock); #endif return OK; @@ -4589,15 +4589,15 @@ int sx127x_register(FAR struct spi_dev_s *spi, dev->pfd = NULL; - /* Initialize sem */ + /* Initialize mutex & sem */ - nxsem_init(&dev->dev_sem, 0, 1); + nxmutex_init(&dev->dev_lock); #ifdef CONFIG_LPWAN_SX127X_TXSUPPORT nxsem_init(&dev->tx_sem, 0, 0); #endif #ifdef CONFIG_LPWAN_SX127X_RXSUPPORT nxsem_init(&dev->rx_sem, 0, 0); - nxsem_init(&dev->rx_buffer_sem, 0, 1); + nxmutex_init(&dev->rx_buffer_lock); #endif /* Attach irq0 - TXDONE/RXDONE/CADDONE */ diff --git a/drivers/wireless/nrf24l01.c b/drivers/wireless/nrf24l01.c index 190a3d2b4b907..72aff94b4dee9 100644 --- a/drivers/wireless/nrf24l01.c +++ b/drivers/wireless/nrf24l01.c @@ -46,6 +46,7 @@ #include #include +#include #include #include @@ -150,14 +151,14 @@ struct nrf24l01_dev_s uint16_t fifo_len; /* Number of bytes stored in fifo */ uint16_t nxt_read; /* Next read index */ uint16_t nxt_write; /* Next write index */ - sem_t sem_fifo; /* Protect access to rx fifo */ + mutex_t lock_fifo; /* Protect access to rx fifo */ sem_t sem_rx; /* Wait for availability of received data */ struct work_s irq_work; /* Interrupt handling "bottom half" */ #endif uint8_t nopens; /* Number of times the device has been opened */ - sem_t devsem; /* Ensures exclusive access to this structure */ + mutex_t devlock; /* Ensures exclusive access to this structure */ FAR struct pollfd *pfd; /* Polled file descr (or NULL if any) */ }; @@ -494,7 +495,7 @@ static uint8_t nrf24l01_setregbit(FAR struct nrf24l01_dev_s *dev, static void fifoput(FAR struct nrf24l01_dev_s *dev, uint8_t pipeno, FAR uint8_t *buffer, uint8_t buflen) { - nxsem_wait(&dev->sem_fifo); + nxmutex_lock(&dev->lock_fifo); while (dev->fifo_len + buflen + 1 > CONFIG_WL_NRF24L01_RXFIFO_LEN) { /* TODO: Set fifo overrun flag ! */ @@ -518,7 +519,7 @@ static void fifoput(FAR struct nrf24l01_dev_s *dev, uint8_t pipeno, dev->nxt_write = (dev->nxt_write + 1) % CONFIG_WL_NRF24L01_RXFIFO_LEN; } - nxsem_post(&dev->sem_fifo); + nxmutex_unlock(&dev->lock_fifo); } /**************************************************************************** @@ -531,7 +532,7 @@ static uint8_t fifoget(FAR struct nrf24l01_dev_s *dev, FAR uint8_t *buffer, uint8_t pktlen; uint8_t i; - nxsem_wait(&dev->sem_fifo); + nxmutex_lock(&dev->lock_fifo); /* sem_rx contains count of inserted packets in FIFO, but FIFO can * overflow - fail smart. @@ -569,7 +570,7 @@ static uint8_t fifoget(FAR struct nrf24l01_dev_s *dev, FAR uint8_t *buffer, dev->fifo_len -= (pktlen + 1); no_data: - nxsem_post(&dev->sem_fifo); + nxmutex_unlock(&dev->lock_fifo); return pktlen; } #endif @@ -950,7 +951,7 @@ static int nrf24l01_open(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -971,7 +972,7 @@ static int nrf24l01_open(FAR struct file *filep) } errout: - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -994,7 +995,7 @@ static int nrf24l01_close(FAR struct file *filep) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -1003,7 +1004,7 @@ static int nrf24l01_close(FAR struct file *filep) nrf24l01_changestate(dev, ST_POWER_DOWN); dev->nopens--; - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return OK; } @@ -1027,7 +1028,7 @@ static ssize_t nrf24l01_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(inode && inode->i_private); dev = (FAR struct nrf24l01_dev_s *)inode->i_private; - ret = nxsem_wait(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -1055,7 +1056,7 @@ static ssize_t nrf24l01_read(FAR struct file *filep, FAR char *buffer, ret = nrf24l01_recv(dev, (uint8_t *)buffer, buflen, &dev->last_recvpipeno); errout: - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; #endif } @@ -1077,7 +1078,7 @@ static ssize_t nrf24l01_write(FAR struct file *filep, FAR const char *buffer, DEBUGASSERT(inode && inode->i_private); dev = (FAR struct nrf24l01_dev_s *)inode->i_private; - ret = nxsem_wait(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -1085,7 +1086,7 @@ static ssize_t nrf24l01_write(FAR struct file *filep, FAR const char *buffer, ret = nrf24l01_send(dev, (const uint8_t *)buffer, buflen); - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -1108,7 +1109,7 @@ static int nrf24l01_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access to the driver data structure */ - ret = nxsem_wait(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -1339,7 +1340,7 @@ static int nrf24l01_ioctl(FAR struct file *filep, int cmd, unsigned long arg) break; } - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; } @@ -1369,7 +1370,7 @@ static int nrf24l01_poll(FAR struct file *filep, FAR struct pollfd *fds, /* Exclusive access */ - ret = nxsem_wait(&dev->devsem); + ret = nxmutex_lock(&dev->devlock); if (ret < 0) { return ret; @@ -1404,13 +1405,13 @@ static int nrf24l01_poll(FAR struct file *filep, FAR struct pollfd *fds, * don't wait for RX. */ - nxsem_wait(&dev->sem_fifo); + nxmutex_lock(&dev->lock_fifo); if (dev->fifo_len > 0) { poll_notify(&dev->pfd, 1, POLLIN); } - nxsem_post(&dev->sem_fifo); + nxmutex_unlock(&dev->lock_fifo); } else /* Tear it down */ { @@ -1418,7 +1419,7 @@ static int nrf24l01_poll(FAR struct file *filep, FAR struct pollfd *fds, } errout: - nxsem_post(&dev->devsem); + nxmutex_unlock(&dev->devlock); return ret; #endif } @@ -1476,7 +1477,7 @@ int nrf24l01_register(FAR struct spi_dev_s *spi, dev->state = ST_UNKNOWN; dev->ce_enabled = false; - nxsem_init(&(dev->devsem), 0, 1); + nxmutex_init(&dev->devlock); nxsem_init(&dev->sem_tx, 0, 0); nxsem_set_protocol(&dev->sem_tx, SEM_PRIO_NONE); @@ -1489,7 +1490,7 @@ int nrf24l01_register(FAR struct spi_dev_s *spi, dev->rx_fifo = rx_fifo; - nxsem_init(&(dev->sem_fifo), 0, 1); + nxmutex_init(&dev->lock_fifo); nxsem_init(&(dev->sem_rx), 0, 0); nxsem_set_protocol(&dev->sem_rx, SEM_PRIO_NONE); #endif diff --git a/drivers/wireless/spirit/drivers/spirit_netdev.c b/drivers/wireless/spirit/drivers/spirit_netdev.c index 3222d35934e22..5671982e394db 100644 --- a/drivers/wireless/spirit/drivers/spirit_netdev.c +++ b/drivers/wireless/spirit/drivers/spirit_netdev.c @@ -106,7 +106,7 @@ #include #include -#include +#include #include #include #include @@ -263,8 +263,8 @@ struct spirit_driver_s struct work_s rxwork; /* RX work queue support (LP) */ struct work_s pollwork; /* TX network poll work (LP) */ struct wdog_s txtimeout; /* TX timeout timer */ - sem_t rxsem; /* Exclusive access to the RX queue */ - sem_t txsem; /* Exclusive access to the TX queue */ + mutex_t rxlock; /* Exclusive access to the RX queue */ + mutex_t txlock; /* Exclusive access to the TX queue */ bool ifup; /* Spirit is on and interface is up */ uint8_t state; /* See enum spirit_driver_state_e */ uint8_t counter; /* Count used with TX timeout */ @@ -277,11 +277,6 @@ struct spirit_driver_s /* Helpers */ -static void spirit_rxlock(FAR struct spirit_driver_s *priv); -static inline void spirit_rxunlock(FAR struct spirit_driver_s *priv); -static void spirit_txlock(FAR struct spirit_driver_s *priv); -static inline void spirit_txunlock(FAR struct spirit_driver_s *priv); - static void spirit_set_ipaddress(FAR struct net_driver_s *dev); static int spirit_set_readystate(FAR struct spirit_driver_s *priv); @@ -449,110 +444,6 @@ static struct spirit_pktstack_address_s g_addrinit = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: spirit_rxlock - * - * Description: - * Get exclusive access to the incoming RX packet queue. - * - * Input Parameters: - * priv - Reference to a driver state structure instance - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void spirit_rxlock(FAR struct spirit_driver_s *priv) -{ - int ret; - - /* The only error that can be reported by nxsem_wait_uninterruptible() is - * ECANCELED if the the thread is canceled. All of the work runs on the - * LP work thread, however. It is very unlikely that the LP worker thread - * will be canceled and, if so, it should finish the work in progress - * before dying anyway. - */ - - do - { - ret = nxsem_wait_uninterruptible(&priv->rxsem); - DEBUGASSERT(ret == 0 || ret == -ECANCELED); - } - while (ret < 0); -} - -/**************************************************************************** - * Name: spirit_rxunlock - * - * Description: - * Relinquish exclusive access to the incoming RX packet queue. - * - * Input Parameters: - * priv - Reference to a driver state structure instance - * - * Returned Value: - * None - * - ****************************************************************************/ - -static inline void spirit_rxunlock(FAR struct spirit_driver_s *priv) -{ - nxsem_post(&priv->rxsem); -} - -/**************************************************************************** - * Name: spirit_txlock - * - * Description: - * Get exclusive access to the outgoing TX packet queue. - * - * Input Parameters: - * priv - Reference to a driver state structure instance - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void spirit_txlock(FAR struct spirit_driver_s *priv) -{ - int ret; - - /* The only error that can be reported by nxsem_wait_uninterruptible() is - * ECANCELED if the the thread is canceled. All of the work runs on the - * LP work thread, however. It is very unlikely that the LP worker thread - * will be canceled and, if so, it should finish the work in progress - * before dying anyway. - */ - - do - { - ret = nxsem_wait_uninterruptible(&priv->txsem); - DEBUGASSERT(ret == 0 || ret == -ECANCELED); - } - while (ret < 0); -} - -/**************************************************************************** - * Name: spirit_txunlock - * - * Description: - * Relinquish exclusive access to the outgoing TX packet queue. - * - * Input Parameters: - * priv - Reference to a driver state structure instance - * - * Returned Value: - * None - * - ****************************************************************************/ - -static inline void spirit_txunlock(FAR struct spirit_driver_s *priv) -{ - nxsem_post(&priv->txsem); -} - /**************************************************************************** * Name: spirit_set_ipaddress * @@ -740,7 +631,7 @@ static void spirit_transmit_work(FAR void *arg) * cannot run until this completes. */ - spirit_txlock(priv); + nxmutex_lock(&priv->txlock); while (priv->txhead != NULL && priv->state == DRIVER_STATE_IDLE) { @@ -921,7 +812,7 @@ static void spirit_transmit_work(FAR void *arg) spirit_txtimeout_expiry, (wdparm_t)priv); } - spirit_txunlock(priv); + nxmutex_unlock(&priv->txlock); return; errout_with_rxtimeout: @@ -931,7 +822,7 @@ static void spirit_transmit_work(FAR void *arg) spirit_csma_enable(spirit, S_DISABLE); errout_with_lock: - spirit_txunlock(priv); + nxmutex_unlock(&priv->txlock); NETDEV_TXERRORS(&priv->radio.r_dev); return; } @@ -1031,7 +922,7 @@ static void spirit_receive_work(FAR void *arg) while (priv->rxhead != NULL) { - spirit_rxlock(priv); + nxmutex_lock(&priv->rxlock); /* Remove the contained IOB from the RX queue */ @@ -1046,7 +937,7 @@ static void spirit_receive_work(FAR void *arg) priv->rxtail = NULL; } - spirit_rxunlock(priv); + nxmutex_unlock(&priv->rxlock); /* Remove the IOB from the container */ @@ -1451,7 +1342,7 @@ static void spirit_interrupt_work(FAR void *arg) pktmeta->pm_flink = NULL; - spirit_rxlock(priv); + nxmutex_lock(&priv->rxlock); if (priv->rxtail == NULL) { priv->rxhead = pktmeta; @@ -1462,7 +1353,7 @@ static void spirit_interrupt_work(FAR void *arg) } priv->rxtail = pktmeta; - spirit_rxunlock(priv); + nxmutex_unlock(&priv->rxlock); /* Forward the packet to the network. This must be done * on the LP worker thread with the network locked. @@ -2307,7 +2198,7 @@ static int spirit_req_data(FAR struct radio_driver_s *netdev, pktmeta->pm_flink = NULL; - spirit_txlock(priv); + nxmutex_lock(&priv->txlock); if (priv->txtail == NULL) { priv->txhead = pktmeta; @@ -2318,7 +2209,7 @@ static int spirit_req_data(FAR struct radio_driver_s *netdev, } priv->txtail = pktmeta; - spirit_txunlock(priv); + nxmutex_unlock(&priv->txlock); /* If are no transmissions or receptions in progress, then schedule * transmission of the frame in the IOB at the head of the IOB queue. @@ -2744,8 +2635,8 @@ int spirit_netdev_initialize(FAR struct spi_dev_s *spi, /* Attach the interface, lower driver, and devops */ priv->lower = lower; - nxsem_init(&priv->rxsem, 0, 1); /* Access to RX packet queue */ - nxsem_init(&priv->txsem, 0, 1); /* Access to TX packet queue */ + nxmutex_init(&priv->rxlock); /* Access to RX packet queue */ + nxmutex_init(&priv->txlock); /* Access to TX packet queue */ /* Initialize the IEEE 802.15.4 network device fields */ diff --git a/fs/driver/fs_blockproxy.c b/fs/driver/fs_blockproxy.c index 8057427f2a7ce..4f87a0021eaeb 100644 --- a/fs/driver/fs_blockproxy.c +++ b/fs/driver/fs_blockproxy.c @@ -39,7 +39,7 @@ #include #include #include -#include +#include #if !defined(CONFIG_DISABLE_MOUNTPOINT) && \ !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS) @@ -49,7 +49,7 @@ ****************************************************************************/ static uint32_t g_devno; -static sem_t g_devno_sem = SEM_INITIALIZER(1); +static mutex_t g_devno_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -84,19 +84,19 @@ static FAR char *unique_chardev(void) for (; ; ) { - /* Get the semaphore protecting the path number */ + /* Get the mutex protecting the path number */ - ret = nxsem_wait_uninterruptible(&g_devno_sem); + ret = nxmutex_lock(&g_devno_lock); if (ret < 0) { - ferr("ERROR: nxsem_wait_uninterruptible failed: %d\n", ret); + ferr("ERROR: nxmutex_lock failed: %d\n", ret); return NULL; } /* Get the next device number and release the semaphore */ devno = ++g_devno; - nxsem_post(&g_devno_sem); + nxmutex_unlock(&g_devno_lock); /* Construct the full device number */ diff --git a/fs/driver/fs_mtdproxy.c b/fs/driver/fs_mtdproxy.c index 1307a72c66f39..abe68775ba541 100644 --- a/fs/driver/fs_mtdproxy.c +++ b/fs/driver/fs_mtdproxy.c @@ -35,7 +35,7 @@ #include #include -#include +#include #include "driver/driver.h" @@ -44,7 +44,7 @@ ****************************************************************************/ static uint32_t g_devno; -static sem_t g_devno_sem = SEM_INITIALIZER(1); +static mutex_t g_devno_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -79,19 +79,19 @@ static FAR char *unique_blkdev(void) for (; ; ) { - /* Get the semaphore protecting the path number */ + /* Get the mutex protecting the path number */ - ret = nxsem_wait_uninterruptible(&g_devno_sem); + ret = nxmutex_lock(&g_devno_lock); if (ret < 0) { - ferr("ERROR: nxsem_wait_uninterruptible failed: %d\n", ret); + ferr("ERROR: nxmutex_lock failed: %d\n", ret); return NULL; } /* Get the next device number and release the semaphore */ devno = ++g_devno; - nxsem_post(&g_devno_sem); + nxmutex_unlock(&g_devno_lock); /* Construct the full device number */ diff --git a/fs/driver/fs_registerblockdriver.c b/fs/driver/fs_registerblockdriver.c index 2c3e9c5b66f06..94a586a957459 100644 --- a/fs/driver/fs_registerblockdriver.c +++ b/fs/driver/fs_registerblockdriver.c @@ -73,7 +73,7 @@ int register_blockdriver(FAR const char *path, * valid data. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { return ret; @@ -93,7 +93,7 @@ int register_blockdriver(FAR const char *path, ret = OK; } - inode_semgive(); + inode_unlock(); return ret; } diff --git a/fs/driver/fs_registerdriver.c b/fs/driver/fs_registerdriver.c index e663300e8ba03..629c093dcf95f 100644 --- a/fs/driver/fs_registerdriver.c +++ b/fs/driver/fs_registerdriver.c @@ -69,7 +69,7 @@ int register_driver(FAR const char *path, * will have a momentarily bad structure. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { return ret; @@ -89,6 +89,6 @@ int register_driver(FAR const char *path, ret = OK; } - inode_semgive(); + inode_unlock(); return ret; } diff --git a/fs/driver/fs_registermtddriver.c b/fs/driver/fs_registermtddriver.c index 98e49d9ecdaaa..e9f6ef0616c54 100644 --- a/fs/driver/fs_registermtddriver.c +++ b/fs/driver/fs_registermtddriver.c @@ -73,7 +73,7 @@ int register_mtddriver(FAR const char *path, FAR struct mtd_dev_s *mtd, * valid data. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { return ret; @@ -93,7 +93,7 @@ int register_mtddriver(FAR const char *path, FAR struct mtd_dev_s *mtd, ret = OK; } - inode_semgive(); + inode_unlock(); return ret; } diff --git a/fs/driver/fs_unregisterblockdriver.c b/fs/driver/fs_unregisterblockdriver.c index be298d3e6a7aa..be697e1f96ec0 100644 --- a/fs/driver/fs_unregisterblockdriver.c +++ b/fs/driver/fs_unregisterblockdriver.c @@ -44,11 +44,11 @@ int unregister_blockdriver(FAR const char *path) { int ret; - ret = inode_semtake(); + ret = inode_lock(); if (ret >= 0) { ret = inode_remove(path); - inode_semgive(); + inode_unlock(); } return ret; diff --git a/fs/driver/fs_unregisterdriver.c b/fs/driver/fs_unregisterdriver.c index da057f4a61be4..fd6b6ee297b8a 100644 --- a/fs/driver/fs_unregisterdriver.c +++ b/fs/driver/fs_unregisterdriver.c @@ -44,11 +44,11 @@ int unregister_driver(FAR const char *path) { int ret; - ret = inode_semtake(); + ret = inode_lock(); if (ret >= 0) { ret = inode_remove(path); - inode_semgive(); + inode_unlock(); } return ret; diff --git a/fs/driver/fs_unregistermtddriver.c b/fs/driver/fs_unregistermtddriver.c index 1fd0ada219516..3442e92cce3d8 100644 --- a/fs/driver/fs_unregistermtddriver.c +++ b/fs/driver/fs_unregistermtddriver.c @@ -45,11 +45,11 @@ int unregister_mtddriver(FAR const char *path) { int ret; - ret = inode_semtake(); + ret = inode_lock(); if (ret >= 0) { ret = inode_remove(path); - inode_semgive(); + inode_unlock(); } return ret; diff --git a/fs/fat/fs_fat32.c b/fs/fat/fs_fat32.c index 6d77023943423..c0163af64af91 100644 --- a/fs/fat/fs_fat32.c +++ b/fs/fat/fs_fat32.c @@ -172,7 +172,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -181,7 +181,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Initialize the directory info structure */ @@ -208,7 +208,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, /* It is the root directory */ ret = -EISDIR; - goto errout_with_semaphore; + goto errout_with_lock; } direntry = &fs->fs_buffer[dirinfo.fd_seq.ds_offset]; @@ -217,7 +217,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, /* It is a regular directory */ ret = -EISDIR; - goto errout_with_semaphore; + goto errout_with_lock; } /* It would be an error if we are asked to create it exclusively */ @@ -227,7 +227,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, /* Already exists -- can't create it exclusively */ ret = -EEXIST; - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if the caller has sufficient privileges to open the file */ @@ -236,7 +236,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, if (((oflags & O_WRONLY) != 0) && readonly) { ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* If O_TRUNC is specified and the file is opened for writing, @@ -252,7 +252,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, ret = fat_dirtruncate(fs, direntry); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -272,7 +272,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, /* No.. then we fail with -ENOENT */ ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } /* Yes.. create the file */ @@ -280,7 +280,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, ret = fat_dircreate(fs, &dirinfo); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Fall through to finish the file open operation */ @@ -296,7 +296,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, * such as if an invalid path were provided. */ - goto errout_with_semaphore; + goto errout_with_lock; } /* Create an instance of the file private date to describe the opened @@ -307,7 +307,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, if (!ff) { ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Create a file buffer to support partial sector accesses */ @@ -353,7 +353,7 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, ff->ff_next = fs->fs_head; fs->fs_head = ff; - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); /* In write/append mode, we need to set the file pointer to the end of * the file. @@ -378,8 +378,8 @@ static int fat_open(FAR struct file *filep, FAR const char *relpath, errout_with_struct: kmm_free(ff); -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -504,7 +504,7 @@ static ssize_t fat_read(FAR struct file *filep, FAR char *buffer, /* Make sure that the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -513,7 +513,7 @@ static ssize_t fat_read(FAR struct file *filep, FAR char *buffer, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if the file was opened with read access */ @@ -521,7 +521,7 @@ static ssize_t fat_read(FAR struct file *filep, FAR char *buffer, if ((ff->ff_oflags & O_RDOK) == 0) { ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Get the number of bytes left in the file */ @@ -548,7 +548,7 @@ static ssize_t fat_read(FAR struct file *filep, FAR char *buffer, ret = fat_currentsector(fs, ff, filep->f_pos); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -576,7 +576,7 @@ static ssize_t fat_read(FAR struct file *filep, FAR char *buffer, if (cluster < 2 || cluster >= fs->fs_nclusters) { ret = -EINVAL; /* Not the right error */ - goto errout_with_semaphore; + goto errout_with_lock; } /* Setup to read the first sector from the new cluster */ @@ -641,7 +641,7 @@ static ssize_t fat_read(FAR struct file *filep, FAR char *buffer, } #endif /* CONFIG_FAT_DIRECT_RETRY */ - goto errout_with_semaphore; + goto errout_with_lock; } ff->ff_sectorsincluster -= nsectors; @@ -660,7 +660,7 @@ static ssize_t fat_read(FAR struct file *filep, FAR char *buffer, ret = fat_ffcacheread(fs, ff, ff->ff_currentsector); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Copy the requested part of the sector into the user buffer */ @@ -692,11 +692,11 @@ static ssize_t fat_read(FAR struct file *filep, FAR char *buffer, sectorindex = filep->f_pos & SEC_NDXMASK(fs); } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return readsize; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -742,7 +742,7 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, /* Make sure that the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -751,7 +751,7 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if the file was opened for write access */ @@ -759,7 +759,7 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, if ((ff->ff_oflags & O_WROK) == 0) { ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if the file size would exceed the range of off_t */ @@ -767,7 +767,7 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, if (ff->ff_size + buflen < ff->ff_size) { ret = -EFBIG; - goto errout_with_semaphore; + goto errout_with_lock; } /* Get the first sector to write to. */ @@ -792,7 +792,7 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, ret = fat_currentsector(fs, ff, filep->f_pos); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -823,12 +823,12 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, if (cluster < 0) { ret = cluster; - goto errout_with_semaphore; + goto errout_with_lock; } else if (cluster < 2 || cluster >= fs->fs_nclusters) { ret = -ENOSPC; - goto errout_with_semaphore; + goto errout_with_lock; } /* Setup to write the first sector from the new cluster */ @@ -893,7 +893,7 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, } #endif /* CONFIG_FAT_DIRECT_RETRY */ - goto errout_with_semaphore; + goto errout_with_lock; } ff->ff_sectorsincluster -= nsectors; @@ -927,7 +927,7 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, ret = fat_ffcacheflush(fs, ff); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Now mark the clean cache buffer as the current sector. */ @@ -943,7 +943,7 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, ret = fat_ffcacheread(fs, ff, ff->ff_currentsector); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -993,11 +993,11 @@ static ssize_t fat_write(FAR struct file *filep, FAR const char *buffer, ff->ff_size = filep->f_pos; } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return byteswritten; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1076,7 +1076,7 @@ static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) /* Make sure that the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1085,7 +1085,7 @@ static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if there is unwritten data in the file buffer */ @@ -1093,7 +1093,7 @@ static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) ret = fat_ffcacheflush(fs, ff); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Attempts to set the position beyond the end of file will @@ -1128,7 +1128,7 @@ static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) if (cluster < 0) { ret = cluster; - goto errout_with_semaphore; + goto errout_with_lock; } ff->ff_startcluster = cluster; @@ -1186,7 +1186,7 @@ static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) /* An error occurred getting the cluster */ ret = cluster; - goto errout_with_semaphore; + goto errout_with_lock; } /* Zero means that there is no further clusters available @@ -1206,7 +1206,7 @@ static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) if (cluster >= fs->fs_nclusters) { ret = -ENOSPC; - goto errout_with_semaphore; + goto errout_with_lock; } /* Otherwise, update the position and continue looking */ @@ -1236,7 +1236,7 @@ static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) ret = fat_ffcacheread(fs, ff, ff->ff_currentsector); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } } } @@ -1249,11 +1249,11 @@ static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) ff->ff_bflags |= FFBUFF_MODIFIED; } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1289,7 +1289,7 @@ static int fat_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Make sure that the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1298,13 +1298,13 @@ static int fat_ioctl(FAR struct file *filep, int cmd, unsigned long arg) ret = fat_checkmount(fs); if (ret != OK) { - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } /* ioctl calls are just passed through to the contained block driver */ - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return -ENOSYS; } @@ -1346,7 +1346,7 @@ static int fat_sync(FAR struct file *filep) /* Make sure that the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1355,7 +1355,7 @@ static int fat_sync(FAR struct file *filep) ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if the has been modified in any way */ @@ -1369,7 +1369,7 @@ static int fat_sync(FAR struct file *filep) ret = fat_ffcacheflush(fs, ff); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Update the directory entry. First read the directory @@ -1379,7 +1379,7 @@ static int fat_sync(FAR struct file *filep) ret = fat_fscacheread(fs, ff->ff_dirsector); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Recover a pointer to the specific directory entry @@ -1426,8 +1426,8 @@ static int fat_sync(FAR struct file *filep) ret = fat_updatefsinfo(fs); } -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1472,7 +1472,7 @@ static int fat_dup(FAR const struct file *oldp, FAR struct file *newp) /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1481,7 +1481,7 @@ static int fat_dup(FAR const struct file *oldp, FAR struct file *newp) ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Create a new instance of the file private date to describe the @@ -1492,7 +1492,7 @@ static int fat_dup(FAR const struct file *oldp, FAR struct file *newp) if (!newff) { ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Create a file buffer to support partial sector accesses */ @@ -1547,7 +1547,7 @@ static int fat_dup(FAR const struct file *oldp, FAR struct file *newp) newff->ff_next = fs->fs_head; fs->fs_head = newff; - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; /* Error exits -- goto's are nasty things, but they sure can make error @@ -1557,8 +1557,8 @@ static int fat_dup(FAR const struct file *oldp, FAR struct file *newp) errout_with_struct: kmm_free(newff); -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1594,7 +1594,7 @@ static int fat_opendir(FAR struct inode *mountpt, FAR const char *relpath, /* Make sure that the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { goto errout_with_fdir; @@ -1603,7 +1603,7 @@ static int fat_opendir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Find the requested directory */ @@ -1611,7 +1611,7 @@ static int fat_opendir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_finddirentry(fs, &dirinfo, relpath); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if this is the root directory */ @@ -1640,7 +1640,7 @@ static int fat_opendir(FAR struct inode *mountpt, FAR const char *relpath, /* The entry is not a directory */ ret = -ENOTDIR; - goto errout_with_semaphore; + goto errout_with_lock; } else { @@ -1657,11 +1657,11 @@ static int fat_opendir(FAR struct inode *mountpt, FAR const char *relpath, } *dir = (FAR struct fs_dirent_s *)fdir; - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); errout_with_fdir: kmm_free(fdir); @@ -1715,7 +1715,7 @@ static int fat_fstat(FAR const struct file *filep, FAR struct stat *buf) /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1724,7 +1724,7 @@ static int fat_fstat(FAR const struct file *filep, FAR struct stat *buf) ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Recover our private data from the struct file instance */ @@ -1738,7 +1738,7 @@ static int fat_fstat(FAR const struct file *filep, FAR struct stat *buf) ret = fat_fscacheread(fs, ff->ff_dirsector); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Recover a pointer to the specific directory entry in the sector using @@ -1754,8 +1754,8 @@ static int fat_fstat(FAR const struct file *filep, FAR struct stat *buf) ret = fat_stat_file(fs, direntry, buf); -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1796,7 +1796,7 @@ static int fat_truncate(FAR struct file *filep, off_t length) /* Make sure that the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1805,7 +1805,7 @@ static int fat_truncate(FAR struct file *filep, off_t length) ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if the file was opened for write access */ @@ -1813,7 +1813,7 @@ static int fat_truncate(FAR struct file *filep, off_t length) if ((ff->ff_oflags & O_WROK) == 0) { ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Are we shrinking the file? Or extending it? */ @@ -1838,7 +1838,7 @@ static int fat_truncate(FAR struct file *filep, off_t length) ret = fat_fscacheread(fs, ff->ff_dirsector); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Recover a pointer to the specific directory entry in the sector @@ -1894,8 +1894,8 @@ static int fat_truncate(FAR struct file *filep, off_t length) } } -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1932,7 +1932,7 @@ static int fat_readdir(FAR struct inode *mountpt, * REVISIT: What if a forced unmount was done since opendir() was called? */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1941,7 +1941,7 @@ static int fat_readdir(FAR struct inode *mountpt, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Read the next directory entry */ @@ -1954,7 +1954,7 @@ static int fat_readdir(FAR struct inode *mountpt, ret = fat_fscacheread(fs, fdir->dir.fd_currsector); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Get a reference to the current directory entry */ @@ -1972,7 +1972,7 @@ static int fat_readdir(FAR struct inode *mountpt, */ ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } /* No, is the current entry a valid entry? */ @@ -2043,15 +2043,15 @@ static int fat_readdir(FAR struct inode *mountpt, if (fat_nextdirentry(fs, &fdir->dir) != OK) { ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -2082,7 +2082,7 @@ static int fat_rewinddir(FAR struct inode *mountpt, * REVISIT: What if a forced unmount was done since opendir() was called? */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -2091,7 +2091,7 @@ static int fat_rewinddir(FAR struct inode *mountpt, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Check if this is the root directory. If it is the root directory, we @@ -2129,11 +2129,11 @@ static int fat_rewinddir(FAR struct inode *mountpt, fdir->dir.fd_index = 2; } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ERROR; } @@ -2181,7 +2181,7 @@ static int fat_bind(FAR struct inode *blkdriver, FAR const void *data, */ fs->fs_blkdriver = blkdriver; /* Save the block driver reference */ - nxsem_init(&fs->fs_sem, 0, 0); /* Initialize the semaphore that controls access */ + nxmutex_init(&fs->fs_lock); /* Initialize the mutex that controls access */ /* Then get information about the FAT32 filesystem on the devices managed * by this block driver. @@ -2190,13 +2190,12 @@ static int fat_bind(FAR struct inode *blkdriver, FAR const void *data, ret = fat_mount(fs, true); if (ret != 0) { - nxsem_destroy(&fs->fs_sem); + nxmutex_destroy(&fs->fs_lock); kmm_free(fs); return ret; } *handle = (FAR void *)fs; - fat_semgive(fs); return OK; } @@ -2221,7 +2220,7 @@ static int fat_unbind(FAR void *handle, FAR struct inode **blkdriver, /* Check if there are sill any files opened on the filesystem. */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -2256,7 +2255,7 @@ static int fat_unbind(FAR void *handle, FAR struct inode **blkdriver, * options. */ - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return (flags != 0) ? -ENOSYS : -EBUSY; } } @@ -2293,7 +2292,7 @@ static int fat_unbind(FAR void *handle, FAR struct inode **blkdriver, fat_io_free(fs->fs_buffer, fs->fs_hwsectorsize); } - nxsem_destroy(&fs->fs_sem); + nxmutex_destroy(&fs->fs_lock); kmm_free(fs); return OK; } @@ -2320,7 +2319,7 @@ static int fat_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -2329,7 +2328,7 @@ static int fat_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) ret = fat_checkmount(fs); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Fill in the statfs info */ @@ -2359,8 +2358,8 @@ static int fat_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) #endif } -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -2386,7 +2385,7 @@ static int fat_unlink(FAR struct inode *mountpt, FAR const char *relpath) /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -2408,7 +2407,7 @@ static int fat_unlink(FAR struct inode *mountpt, FAR const char *relpath) ret = fat_remove(fs, relpath, false); } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -2444,7 +2443,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -2453,7 +2452,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Find the directory where the new directory should be created. */ @@ -2474,7 +2473,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = -EEXIST; } - goto errout_with_semaphore; + goto errout_with_lock; } /* What we want to see is for fat_finddirentry to fail with -ENOENT. @@ -2485,7 +2484,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, if (ret != -ENOENT) { - goto errout_with_semaphore; + goto errout_with_lock; } /* NOTE: There is no check that dirinfo.fd_name contains the final @@ -2498,7 +2497,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_allocatedirentry(fs, &dirinfo); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } parentsector = fs->fs_currentsector; @@ -2509,19 +2508,19 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, if (dircluster < 0) { ret = dircluster; - goto errout_with_semaphore; + goto errout_with_lock; } else if (dircluster < 2) { ret = -ENOSPC; - goto errout_with_semaphore; + goto errout_with_lock; } dirsector = fat_cluster2sector(fs, dircluster); if (dirsector < 0) { ret = dirsector; - goto errout_with_semaphore; + goto errout_with_lock; } /* Flush any existing, dirty data in fs_buffer (because we need @@ -2531,7 +2530,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_fscacheflush(fs); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Get a pointer to the first directory entry in the sector */ @@ -2552,7 +2551,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_hwwrite(fs, direntry, ++dirsector, 1); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -2602,7 +2601,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_fscacheread(fs, parentsector); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Write the new entry directory entry in the parent directory */ @@ -2610,7 +2609,7 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_dirwrite(fs, &dirinfo, FATATTR_DIRECTORY, crtime); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Set subdirectory start cluster. We assume that fat_dirwrite() did not @@ -2627,14 +2626,14 @@ static int fat_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_updatefsinfo(fs); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -2660,7 +2659,7 @@ int fat_rmdir(FAR struct inode *mountpt, FAR const char *relpath) /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -2682,7 +2681,7 @@ int fat_rmdir(FAR struct inode *mountpt, FAR const char *relpath) ret = fat_remove(fs, relpath, true); } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -2713,7 +2712,7 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -2722,7 +2721,7 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Find the directory entry for the oldrelpath (there may be multiple @@ -2734,7 +2733,7 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, { /* Some error occurred -- probably -ENOENT */ - goto errout_with_semaphore; + goto errout_with_lock; } /* One more check: Make sure that the oldrelpath does not refer to the @@ -2744,7 +2743,7 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, if (dirinfo.fd_root) { ret = -EXDEV; - goto errout_with_semaphore; + goto errout_with_lock; } /* Save the information that will need to recover the directory sector and @@ -2780,7 +2779,7 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, ret = -EEXIST; } - goto errout_with_semaphore; + goto errout_with_lock; } /* Reserve a directory entry. If long file name support is enabled, then @@ -2792,7 +2791,7 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, ret = fat_allocatedirentry(fs, &dirinfo); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Then write the new file name into the directory entry. This, of course, @@ -2804,7 +2803,7 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, ret = fat_dirnamewrite(fs, &dirinfo); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Copy the unchanged information into the new short file name entry. */ @@ -2821,7 +2820,7 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, ret = fat_freedirentry(fs, &dirseq); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Write the old entry to disk and update FSINFO if necessary */ @@ -2829,14 +2828,14 @@ int fat_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, ret = fat_updatefsinfo(fs); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -2981,7 +2980,7 @@ static int fat_stat(FAR struct inode *mountpt, FAR const char *relpath, /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -2990,7 +2989,7 @@ static int fat_stat(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Find the directory entry corresponding to relpath. */ @@ -3001,7 +3000,7 @@ static int fat_stat(FAR struct inode *mountpt, FAR const char *relpath, if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Get the FAT attribute and map it so some meaningful mode_t values */ @@ -3023,8 +3022,8 @@ static int fat_stat(FAR struct inode *mountpt, FAR const char *relpath, ret = fat_stat_file(fs, direntry, buf); } -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } diff --git a/fs/fat/fs_fat32.h b/fs/fat/fs_fat32.h index d53de32b72ded..c7ae093c73163 100644 --- a/fs/fat/fs_fat32.h +++ b/fs/fat/fs_fat32.h @@ -33,7 +33,7 @@ #include #include -#include +#include /**************************************************************************** * Pre-processor Definitions @@ -864,7 +864,7 @@ struct fat_mountpt_s struct inode *fs_blkdriver; /* The block driver inode that hosts the FAT32 fs */ struct fat_file_s *fs_head; /* A list to all files opened on this mountpoint */ - sem_t fs_sem; /* Used to assume thread-safe access */ + mutex_t fs_lock; /* Used to assume thread-safe access */ off_t fs_hwsectorsize; /* HW: Sector size reported by block driver */ off_t fs_hwnsectors; /* HW: The number of sectors reported by the hardware */ off_t fs_fatbase; /* Logical block of start of filesystem (past resd sectors) */ @@ -1018,11 +1018,6 @@ EXTERN uint32_t fat_getuint32(uint8_t *ptr); EXTERN void fat_putuint16(uint8_t *ptr, uint16_t value16); EXTERN void fat_putuint32(uint8_t *ptr, uint32_t value32); -/* Manage the per-mount semaphore that protects access to shared resources */ - -EXTERN int fat_semtake(struct fat_mountpt_s *fs); -EXTERN void fat_semgive(struct fat_mountpt_s *fs); - /* Get the current time for FAT creation and write times */ EXTERN uint32_t fat_systime2fattime(void); diff --git a/fs/fat/fs_fat32attrib.c b/fs/fat/fs_fat32attrib.c index 79df97df27fb2..47948d72d752f 100644 --- a/fs/fat/fs_fat32attrib.c +++ b/fs/fat/fs_fat32attrib.c @@ -86,7 +86,7 @@ static int fat_attrib(const char *path, fat_attrib_t *retattrib, /* Check if the mount is still healthy */ - ret = fat_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { goto errout_with_inode; @@ -95,7 +95,7 @@ static int fat_attrib(const char *path, fat_attrib_t *retattrib, ret = fat_checkmount(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Find the file/directory entry for the relpath */ @@ -105,7 +105,7 @@ static int fat_attrib(const char *path, fat_attrib_t *retattrib, { /* Some error occurred -- probably -ENOENT */ - goto errout_with_semaphore; + goto errout_with_lock; } /* Make sure that we found some valid file or directory */ @@ -115,7 +115,7 @@ static int fat_attrib(const char *path, fat_attrib_t *retattrib, /* Ooops.. we found the root directory */ ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Get the current attributes */ @@ -140,7 +140,7 @@ static int fat_attrib(const char *path, fat_attrib_t *retattrib, ret = fat_updatefsinfo(fs); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -151,13 +151,13 @@ static int fat_attrib(const char *path, fat_attrib_t *retattrib, *retattrib = newattributes; } - fat_semgive(fs); + nxmutex_unlock(&fs->fs_lock); inode_release(inode); RELEASE_SEARCH(&desc); return OK; -errout_with_semaphore: - fat_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); errout_with_inode: inode_release(inode); diff --git a/fs/fat/fs_fat32util.c b/fs/fat/fs_fat32util.c index 29a676c1db13e..6d13561ded2a3 100644 --- a/fs/fat/fs_fat32util.c +++ b/fs/fat/fs_fat32util.c @@ -347,24 +347,6 @@ void fat_putuint32(FAR uint8_t *ptr, uint32_t value32) #endif } -/**************************************************************************** - * Name: fat_semtake - ****************************************************************************/ - -int fat_semtake(struct fat_mountpt_s *fs) -{ - return nxsem_wait_uninterruptible(&fs->fs_sem); -} - -/**************************************************************************** - * Name: fat_semgive - ****************************************************************************/ - -void fat_semgive(struct fat_mountpt_s *fs) -{ - nxsem_post(&fs->fs_sem); -} - /**************************************************************************** * Name: fat_systime2fattime * diff --git a/fs/hostfs/hostfs.c b/fs/hostfs/hostfs.c index ba2bee1fb1eda..b6e50ddb4e30d 100644 --- a/fs/hostfs/hostfs.c +++ b/fs/hostfs/hostfs.c @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -122,8 +123,7 @@ static int hostfs_chstat(FAR struct inode *mountpt, * Private Data ****************************************************************************/ -static uint8_t g_seminitialized = FALSE; -static sem_t g_sem; +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Public Data @@ -170,24 +170,6 @@ const struct mountpt_operations hostfs_operations = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: hostfs_semtake - ****************************************************************************/ - -int hostfs_semtake(FAR struct hostfs_mountpt_s *fs) -{ - return nxsem_wait_uninterruptible(fs->fs_sem); -} - -/**************************************************************************** - * Name: hostfs_semgive - ****************************************************************************/ - -void hostfs_semgive(FAR struct hostfs_mountpt_s *fs) -{ - nxsem_post(fs->fs_sem); -} - /**************************************************************************** * Name: hostfs_mkpath * @@ -275,9 +257,9 @@ static int hostfs_open(FAR struct file *filep, FAR const char *relpath, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -289,7 +271,7 @@ static int hostfs_open(FAR struct file *filep, FAR const char *relpath, if (hf == NULL) { ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Append to the host's root directory */ @@ -340,13 +322,13 @@ static int hostfs_open(FAR struct file *filep, FAR const char *relpath, fs->fs_head = hf; ret = OK; - goto errout_with_semaphore; + goto errout_with_lock; errout_with_buffer: kmm_free(hf); -errout_with_semaphore: - hostfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&g_lock); if (ret == -EINVAL) { ret = -EIO; @@ -378,9 +360,9 @@ static int hostfs_close(FAR struct file *filep) fs = inode->i_private; hf = filep->f_priv; - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -440,7 +422,7 @@ static int hostfs_close(FAR struct file *filep) kmm_free(hf); okout: - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; } @@ -468,9 +450,9 @@ static ssize_t hostfs_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -484,7 +466,7 @@ static ssize_t hostfs_read(FAR struct file *filep, FAR char *buffer, filep->f_pos += ret; } - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -510,9 +492,9 @@ static ssize_t hostfs_write(FAR struct file *filep, const char *buffer, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -525,7 +507,7 @@ static ssize_t hostfs_write(FAR struct file *filep, const char *buffer, if ((hf->oflags & O_WROK) == 0) { ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Call the host to perform the write */ @@ -536,8 +518,8 @@ static ssize_t hostfs_write(FAR struct file *filep, const char *buffer, filep->f_pos += ret; } -errout_with_semaphore: - hostfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&g_lock); return ret; } @@ -564,9 +546,9 @@ static off_t hostfs_seek(FAR struct file *filep, off_t offset, int whence) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -580,7 +562,7 @@ static off_t hostfs_seek(FAR struct file *filep, off_t offset, int whence) filep->f_pos = ret; } - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -607,9 +589,9 @@ static int hostfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -619,7 +601,7 @@ static int hostfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) ret = host_ioctl(hf->fd, cmd, arg); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -650,9 +632,9 @@ static int hostfs_sync(FAR struct file *filep) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -660,7 +642,7 @@ static int hostfs_sync(FAR struct file *filep) host_sync(hf->fd); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; } @@ -724,9 +706,9 @@ static int hostfs_fstat(FAR const struct file *filep, FAR struct stat *buf) fs = inode->i_private; DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -736,7 +718,7 @@ static int hostfs_fstat(FAR const struct file *filep, FAR struct stat *buf) ret = host_fstat(hf->fd, buf); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -770,9 +752,9 @@ static int hostfs_fchstat(FAR const struct file *filep, fs = inode->i_private; DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -782,7 +764,7 @@ static int hostfs_fchstat(FAR const struct file *filep, ret = host_fchstat(hf->fd, buf, flags); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -815,9 +797,9 @@ static int hostfs_ftruncate(FAR struct file *filep, off_t length) fs = inode->i_private; DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -827,7 +809,7 @@ static int hostfs_ftruncate(FAR struct file *filep, off_t length) ret = host_ftruncate(hf->fd, length); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -859,9 +841,9 @@ static int hostfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, return -ENOMEM; } - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { goto errout_with_hdir; @@ -877,15 +859,15 @@ static int hostfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, if (hdir->dir == NULL) { ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } *dir = (FAR struct fs_dirent_s *)hdir; - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; -errout_with_semaphore: - hostfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&g_lock); errout_with_hdir: kmm_free(hdir); @@ -902,7 +884,6 @@ static int hostfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, static int hostfs_closedir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir) { - FAR struct hostfs_mountpt_s *fs; FAR struct hostfs_dir_s *hdir; int ret; @@ -912,12 +893,11 @@ static int hostfs_closedir(FAR struct inode *mountpt, /* Recover our private data from the inode instance */ - fs = mountpt->i_private; hdir = (FAR struct hostfs_dir_s *)dir; - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -927,7 +907,7 @@ static int hostfs_closedir(FAR struct inode *mountpt, host_closedir(hdir->dir); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); kmm_free(hdir); return OK; } @@ -943,7 +923,6 @@ static int hostfs_readdir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir, FAR struct dirent *entry) { - FAR struct hostfs_mountpt_s *fs; FAR struct hostfs_dir_s *hdir; int ret; @@ -953,12 +932,11 @@ static int hostfs_readdir(FAR struct inode *mountpt, /* Recover our private data from the inode instance */ - fs = mountpt->i_private; hdir = (FAR struct hostfs_dir_s *)dir; - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -968,7 +946,7 @@ static int hostfs_readdir(FAR struct inode *mountpt, ret = host_readdir(hdir->dir, entry); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -982,7 +960,6 @@ static int hostfs_readdir(FAR struct inode *mountpt, static int hostfs_rewinddir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir) { - FAR struct hostfs_mountpt_s *fs; FAR struct hostfs_dir_s *hdir; int ret; @@ -992,12 +969,11 @@ static int hostfs_rewinddir(FAR struct inode *mountpt, /* Recover our private data from the inode instance */ - fs = mountpt->i_private; hdir = (FAR struct hostfs_dir_s *)dir; - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1007,7 +983,7 @@ static int hostfs_rewinddir(FAR struct inode *mountpt, host_rewinddir(hdir->dir); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; } @@ -1073,28 +1049,13 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data, kmm_free(options); - /* If the global semaphore hasn't been initialized, then - * initialized it now. - */ - - fs->fs_sem = &g_sem; - if (!g_seminitialized) - { - /* Initialize the semaphore that controls access */ + /* Take the lock for the mount */ - nxsem_init(&g_sem, 0, 0); - g_seminitialized = TRUE; - } - else + ret = nxmutex_lock(&g_lock); + if (ret < 0) { - /* Take the semaphore for the mount */ - - ret = hostfs_semtake(fs); - if (ret < 0) - { - kmm_free(fs); - return ret; - } + kmm_free(fs); + return ret; } /* Initialize the allocated mountpt state structure. The filesystem is @@ -1122,7 +1083,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data, } *handle = (FAR void *)fs; - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; } @@ -1147,7 +1108,7 @@ static int hostfs_unbind(FAR void *handle, FAR struct inode **blkdriver, /* Check if there are sill any files opened on the filesystem. */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1157,7 +1118,7 @@ static int hostfs_unbind(FAR void *handle, FAR struct inode **blkdriver, { /* We cannot unmount now.. there are open files */ - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); /* This implementation currently only supports unmounting if there are * no open file references. @@ -1166,7 +1127,7 @@ static int hostfs_unbind(FAR void *handle, FAR struct inode **blkdriver, return (flags != 0) ? -ENOSYS : -EBUSY; } - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); kmm_free(fs); return ret; } @@ -1191,7 +1152,7 @@ static int hostfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) fs = mountpt->i_private; - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1203,7 +1164,7 @@ static int hostfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) ret = host_statfs(fs->fs_root, buf); buf->f_type = HOSTFS_MAGIC; - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1228,7 +1189,7 @@ static int hostfs_unlink(FAR struct inode *mountpt, FAR const char *relpath) fs = mountpt->i_private; - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1242,7 +1203,7 @@ static int hostfs_unlink(FAR struct inode *mountpt, FAR const char *relpath) ret = host_unlink(path); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1268,7 +1229,7 @@ static int hostfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, fs = mountpt->i_private; - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1282,7 +1243,7 @@ static int hostfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = host_mkdir(path, mode); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1307,9 +1268,9 @@ int hostfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath) fs = mountpt->i_private; - /* Take the semaphore */ + /* Take the lock */ - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1323,7 +1284,7 @@ int hostfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath) ret = host_rmdir(path); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1350,7 +1311,7 @@ int hostfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, fs = mountpt->i_private; - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1367,7 +1328,7 @@ int hostfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, ret = host_rename(oldpath, newpath); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1393,7 +1354,7 @@ static int hostfs_stat(FAR struct inode *mountpt, FAR const char *relpath, fs = mountpt->i_private; - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1407,7 +1368,7 @@ static int hostfs_stat(FAR struct inode *mountpt, FAR const char *relpath, ret = host_stat(path, buf); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1433,7 +1394,7 @@ static int hostfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, fs = mountpt->i_private; - ret = hostfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1447,7 +1408,7 @@ static int hostfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, ret = host_chstat(path, buf, flags); - hostfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } diff --git a/fs/hostfs/hostfs.h b/fs/hostfs/hostfs.h index c3160187607cb..2f6c69148b09a 100644 --- a/fs/hostfs/hostfs.h +++ b/fs/hostfs/hostfs.h @@ -31,8 +31,6 @@ #include #include -#include - /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -62,7 +60,6 @@ struct hostfs_ofile_s struct hostfs_mountpt_s { - sem_t *fs_sem; /* Used to assure thread-safe access */ FAR struct hostfs_ofile_s *fs_head; /* A singly-linked list of open files */ char fs_root[HOSTFS_MAX_PATH]; }; @@ -71,11 +68,6 @@ struct hostfs_mountpt_s * Internal function prototypes ****************************************************************************/ -/* Semaphore access for internal use */ - -int hostfs_semtake(struct hostfs_mountpt_s *fs); -void hostfs_semgive(struct hostfs_mountpt_s *fs); - /* Forward references for utility functions */ struct hostfs_mountpt_s; diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index d1a9a22a1479e..2549dfad528ce 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include "inode/inode.h" @@ -42,21 +42,6 @@ * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: _files_semtake - ****************************************************************************/ - -static int _files_semtake(FAR struct filelist *list) -{ - return nxsem_wait_uninterruptible(&list->fl_sem); -} - -/**************************************************************************** - * Name: _files_semgive - ****************************************************************************/ - -#define _files_semgive(list) nxsem_post(&list->fl_sem) - /**************************************************************************** * Name: files_extend ****************************************************************************/ @@ -129,7 +114,7 @@ void files_initlist(FAR struct filelist *list) /* Initialize the list access mutex */ - nxsem_init(&list->fl_sem, 0, 1); + nxmutex_init(&list->fl_lock); } /**************************************************************************** @@ -148,7 +133,7 @@ void files_releaselist(FAR struct filelist *list) DEBUGASSERT(list); /* Close each file descriptor .. Normally, you would need take the list - * semaphore, but it is safe to ignore the semaphore in this context + * mutex, but it is safe to ignore the mutex in this context * because there should not be any references in this context. */ @@ -164,9 +149,9 @@ void files_releaselist(FAR struct filelist *list) kmm_free(list->fl_files); - /* Destroy the semaphore */ + /* Destroy the mutex */ - nxsem_destroy(&list->fl_sem); + nxmutex_destroy(&list->fl_lock); } /**************************************************************************** @@ -191,7 +176,7 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, list = nxsched_get_files(); DEBUGASSERT(list != NULL); - ret = _files_semtake(list); + ret = nxmutex_lock(&list->fl_lock); if (ret < 0) { /* Probably canceled */ @@ -209,7 +194,7 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, ret = files_extend(list, i + 1); if (ret < 0) { - _files_semgive(list); + nxmutex_unlock(&list->fl_lock); return ret; } } @@ -227,7 +212,7 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, list->fl_files[i][j].f_pos = pos; list->fl_files[i][j].f_inode = inode; list->fl_files[i][j].f_priv = priv; - _files_semgive(list); + nxmutex_unlock(&list->fl_lock); return i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j; } } @@ -249,7 +234,7 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, ret = i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; } - _files_semgive(list); + nxmutex_unlock(&list->fl_lock); return ret; } @@ -267,7 +252,7 @@ int files_duplist(FAR struct filelist *plist, FAR struct filelist *clist) int i; int j; - ret = _files_semtake(plist); + ret = nxmutex_lock(&plist->fl_lock); if (ret < 0) { /* Probably canceled */ @@ -318,7 +303,7 @@ int files_duplist(FAR struct filelist *plist, FAR struct filelist *clist) } out: - _files_semgive(plist); + nxmutex_unlock(&plist->fl_lock); return ret; } @@ -372,7 +357,7 @@ int fs_getfilep(int fd, FAR struct file **filep) /* And return the file pointer from the list */ - ret = _files_semtake(list); + ret = nxmutex_lock(&list->fl_lock); if (ret < 0) { return ret; @@ -389,8 +374,7 @@ int fs_getfilep(int fd, FAR struct file **filep) ret = -EBADF; } - _files_semgive(list); - + nxmutex_unlock(&list->fl_lock); return ret; } @@ -435,7 +419,7 @@ int nx_dup2(int fd1, int fd2) return -EBADF; } - ret = _files_semtake(list); + ret = nxmutex_lock(&list->fl_lock); if (ret < 0) { /* Probably canceled */ @@ -448,7 +432,7 @@ int nx_dup2(int fd1, int fd2) ret = files_extend(list, fd2 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + 1); if (ret < 0) { - _files_semgive(list); + nxmutex_unlock(&list->fl_lock); return ret; } } @@ -463,10 +447,9 @@ int nx_dup2(int fd1, int fd2) ret = file_dup2(&list->fl_files[fd1 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] [fd1 % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK], filep); - _files_semgive(list); + nxmutex_unlock(&list->fl_lock); file_close(&file); - return ret < 0 ? ret : fd2; } @@ -510,7 +493,7 @@ int dup2(int fd1, int fd2) * on any failure. * * Assumptions: - * Caller holds the list semaphore because the file descriptor will be + * Caller holds the list mutex because the file descriptor will be * freed. * ****************************************************************************/ @@ -531,7 +514,7 @@ int nx_close(int fd) /* Perform the protected close operation */ - ret = _files_semtake(list); + ret = nxmutex_lock(&list->fl_lock); if (ret < 0) { return ret; @@ -543,7 +526,7 @@ int nx_close(int fd) !list->fl_files[fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] [fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK].f_inode) { - _files_semgive(list); + nxmutex_unlock(&list->fl_lock); return -EBADF; } @@ -552,8 +535,7 @@ int nx_close(int fd) memcpy(&file, filep, sizeof(struct file)); memset(filep, 0, sizeof(struct file)); - _files_semgive(list); - + nxmutex_unlock(&list->fl_lock); return file_close(&file); } diff --git a/fs/inode/fs_foreachinode.c b/fs/inode/fs_foreachinode.c index 0aae62f7d2e76..8dc1666dbba7d 100644 --- a/fs/inode/fs_foreachinode.c +++ b/fs/inode/fs_foreachinode.c @@ -183,11 +183,11 @@ int foreach_inode(foreach_inode_t handler, FAR void *arg) /* Start the recursion at the root inode */ - ret = inode_semtake(); + ret = inode_lock(); if (ret >= 0) { ret = foreach_inodelevel(g_root_inode->i_child, info); - inode_semgive(); + inode_unlock(); } /* Free the info structure and return the result */ @@ -207,11 +207,11 @@ int foreach_inode(foreach_inode_t handler, FAR void *arg) /* Start the recursion at the root inode */ - ret = inode_semtake(); + ret = inode_lock(); if (ret >= 0) { ret = foreach_inodelevel(g_root_inode->i_child, &info); - inode_semgive(); + inode_unlock(); } return ret; diff --git a/fs/inode/fs_inode.c b/fs/inode/fs_inode.c index 8ed88badd170a..9fb5ace69e769 100644 --- a/fs/inode/fs_inode.c +++ b/fs/inode/fs_inode.c @@ -68,27 +68,27 @@ void inode_initialize(void) } /**************************************************************************** - * Name: inode_semtake + * Name: inode_lock * * Description: * Get exclusive access to the in-memory inode tree (g_inode_sem). * ****************************************************************************/ -int inode_semtake(void) +int inode_lock(void) { return nxrmutex_lock(&g_inode_lock); } /**************************************************************************** - * Name: inode_semgive + * Name: inode_unlock * * Description: * Relinquish exclusive access to the in-memory inode tree (g_inode_sem). * ****************************************************************************/ -void inode_semgive(void) +void inode_unlock(void) { DEBUGVERIFY(nxrmutex_unlock(&g_inode_lock)); } diff --git a/fs/inode/fs_inodeaddref.c b/fs/inode/fs_inodeaddref.c index ba45ca8520310..896e7d1d389cb 100644 --- a/fs/inode/fs_inodeaddref.c +++ b/fs/inode/fs_inodeaddref.c @@ -47,11 +47,11 @@ int inode_addref(FAR struct inode *inode) if (inode) { - ret = inode_semtake(); + ret = inode_lock(); if (ret >= 0) { inode->i_crefs++; - inode_semgive(); + inode_unlock(); } } diff --git a/fs/inode/fs_inodefind.c b/fs/inode/fs_inodefind.c index 268a9fb93f8df..d8e3a9815cefe 100644 --- a/fs/inode/fs_inodefind.c +++ b/fs/inode/fs_inodefind.c @@ -55,7 +55,7 @@ int inode_find(FAR struct inode_search_s *desc) * references on the node. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { return ret; @@ -74,6 +74,6 @@ int inode_find(FAR struct inode_search_s *desc) node->i_crefs++; } - inode_semgive(); + inode_unlock(); return ret; } diff --git a/fs/inode/fs_inoderelease.c b/fs/inode/fs_inoderelease.c index 33d06c0cddc83..651104c4d204f 100644 --- a/fs/inode/fs_inoderelease.c +++ b/fs/inode/fs_inoderelease.c @@ -55,7 +55,7 @@ void inode_release(FAR struct inode *node) do { - ret = inode_semtake(); + ret = inode_lock(); /* This only possible error is due to cancellation of the thread. * We need to try again anyway in this case, otherwise the @@ -82,14 +82,14 @@ void inode_release(FAR struct inode *node) * should be NULL. */ - inode_semgive(); + inode_unlock(); DEBUGASSERT(node->i_peer == NULL); inode_free(node); } else { - inode_semgive(); + inode_unlock(); } } } diff --git a/fs/inode/inode.h b/fs/inode/inode.h index 53053107c3d3e..83d48bdf40891 100644 --- a/fs/inode/inode.h +++ b/fs/inode/inode.h @@ -147,24 +147,24 @@ EXTERN FAR struct inode *g_root_inode; void inode_initialize(void); /**************************************************************************** - * Name: inode_semtake + * Name: inode_lock * * Description: * Get exclusive access to the in-memory inode tree (tree_sem). * ****************************************************************************/ -int inode_semtake(void); +int inode_lock(void); /**************************************************************************** - * Name: inode_semgive + * Name: inode_unlock * * Description: * Relinquish exclusive access to the in-memory inode tree (tree_sem). * ****************************************************************************/ -void inode_semgive(void); +void inode_unlock(void); /**************************************************************************** * Name: inode_checkflags diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c index ce4362e558acd..5687ab15d7a42 100644 --- a/fs/littlefs/lfs_vfs.c +++ b/fs/littlefs/lfs_vfs.c @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include @@ -62,7 +62,7 @@ struct littlefs_file_s struct littlefs_mountpt_s { - sem_t sem; + mutex_t lock; FAR struct inode *drv; struct mtd_geometry_s geo; struct lfs_config cfg; @@ -73,9 +73,6 @@ struct littlefs_mountpt_s * Private Function Prototypes ****************************************************************************/ -static void littlefs_semgive(FAR struct littlefs_mountpt_s *fs); -static int littlefs_semtake(FAR struct littlefs_mountpt_s *fs); - static int littlefs_open(FAR struct file *filep, FAR const char *relpath, int oflags, mode_t mode); static int littlefs_close(FAR struct file *filep); @@ -171,24 +168,6 @@ const struct mountpt_operations littlefs_operations = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: littlefs_semtake - ****************************************************************************/ - -static int littlefs_semtake(FAR struct littlefs_mountpt_s *fs) -{ - return nxsem_wait_uninterruptible(&fs->sem); -} - -/**************************************************************************** - * Name: littlefs_semgive - ****************************************************************************/ - -static void littlefs_semgive(FAR struct littlefs_mountpt_s *fs) -{ - nxsem_post(&fs->sem); -} - /**************************************************************************** * Name: littlefs_convert_result ****************************************************************************/ @@ -315,12 +294,12 @@ static int littlefs_open(FAR struct file *filep, FAR const char *relpath, priv->refs = 1; - /* Take the semaphore */ + /* Lock */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { - goto errsem; + goto errlock; } /* Try to open the file */ @@ -358,7 +337,7 @@ static int littlefs_open(FAR struct file *filep, FAR const char *relpath, */ lfs_file_sync(&fs->lfs, &priv->file); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); /* Attach the private date to the struct file instance */ @@ -368,8 +347,8 @@ static int littlefs_open(FAR struct file *filep, FAR const char *relpath, errout_with_file: lfs_file_close(&fs->lfs, &priv->file); errout: - littlefs_semgive(fs); -errsem: + nxmutex_unlock(&fs->lock); +errlock: kmm_free(priv); return ret; } @@ -393,7 +372,7 @@ static int littlefs_close(FAR struct file *filep) /* Close the file */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -404,7 +383,7 @@ static int littlefs_close(FAR struct file *filep) ret = littlefs_convert_result(lfs_file_close(&fs->lfs, &priv->file)); } - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); if (priv->refs <= 0) { kmm_free(priv); @@ -433,7 +412,7 @@ static ssize_t littlefs_read(FAR struct file *filep, FAR char *buffer, /* Call LFS to perform the read */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -458,7 +437,7 @@ static ssize_t littlefs_read(FAR struct file *filep, FAR char *buffer, } out: - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -482,7 +461,7 @@ static ssize_t littlefs_write(FAR struct file *filep, const char *buffer, /* Call LFS to perform the write */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -507,7 +486,7 @@ static ssize_t littlefs_write(FAR struct file *filep, const char *buffer, } out: - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -530,7 +509,7 @@ static off_t littlefs_seek(FAR struct file *filep, off_t offset, int whence) /* Call LFS to perform the seek */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -543,7 +522,7 @@ static off_t littlefs_seek(FAR struct file *filep, off_t offset, int whence) filep->f_pos = ret; } - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -601,14 +580,14 @@ static int littlefs_sync(FAR struct file *filep) inode = filep->f_inode; fs = inode->i_private; - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; } ret = littlefs_convert_result(lfs_file_sync(&fs->lfs, &priv->file)); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -633,7 +612,7 @@ static int littlefs_dup(FAR const struct file *oldp, FAR struct file *newp) inode = oldp->f_inode; fs = inode->i_private; - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -641,7 +620,7 @@ static int littlefs_dup(FAR const struct file *oldp, FAR struct file *newp) priv->refs++; newp->f_priv = priv; - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -672,14 +651,14 @@ static int littlefs_fstat(FAR const struct file *filep, FAR struct stat *buf) /* Call LFS to get file size */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; } buf->st_size = lfs_file_size(&fs->lfs, &priv->file); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); if (buf->st_size < 0) { @@ -717,7 +696,7 @@ static int littlefs_truncate(FAR struct file *filep, off_t length) /* Call LFS to perform the truncate */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -725,7 +704,7 @@ static int littlefs_truncate(FAR struct file *filep, off_t length) ret = littlefs_convert_result(lfs_file_truncate(&fs->lfs, &priv->file, length)); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -757,12 +736,12 @@ static int littlefs_opendir(FAR struct inode *mountpt, return -ENOMEM; } - /* Take the semaphore */ + /* Take the lock */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { - goto errsem; + goto errlock; } /* Call the LFS's opendir function */ @@ -773,13 +752,13 @@ static int littlefs_opendir(FAR struct inode *mountpt, goto errout; } - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); *dir = &ldir->base; return OK; errout: - littlefs_semgive(fs); -errsem: + nxmutex_unlock(&fs->lock); +errlock: kmm_free(ldir); return ret; } @@ -805,14 +784,14 @@ static int littlefs_closedir(FAR struct inode *mountpt, /* Call the LFS's closedir function */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; } lfs_dir_close(&fs->lfs, &ldir->dir); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); kmm_free(ldir); return OK; @@ -841,7 +820,7 @@ static int littlefs_readdir(FAR struct inode *mountpt, /* Call the LFS's readdir function */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -866,7 +845,7 @@ static int littlefs_readdir(FAR struct inode *mountpt, ret = -ENOENT; } - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -891,7 +870,7 @@ static int littlefs_rewinddir(FAR struct inode *mountpt, /* Call the LFS's rewinddir function */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -899,7 +878,7 @@ static int littlefs_rewinddir(FAR struct inode *mountpt, ret = littlefs_convert_result(lfs_dir_rewind(&fs->lfs, &ldir->dir)); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -1053,8 +1032,8 @@ static int littlefs_bind(FAR struct inode *driver, FAR const void *data, * have to addref() here (but does have to release in unbind(). */ - fs->drv = driver; /* Save the driver reference */ - nxsem_init(&fs->sem, 0, 0); /* Initialize the access control semaphore */ + fs->drv = driver; /* Save the driver reference */ + nxmutex_init(&fs->lock); /* Initialize the access control mutex */ if (INODE_IS_MTD(driver)) { @@ -1159,11 +1138,10 @@ static int littlefs_bind(FAR struct inode *driver, FAR const void *data, } *handle = fs; - littlefs_semgive(fs); return OK; errout_with_fs: - nxsem_destroy(&fs->sem); + nxmutex_destroy(&fs->lock); kmm_free(fs); errout_with_block: if (INODE_IS_BLOCK(driver) && driver->u.i_bops->close) @@ -1191,14 +1169,14 @@ static int littlefs_unbind(FAR void *handle, FAR struct inode **driver, /* Unmount */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; } ret = littlefs_convert_result(lfs_unmount(&fs->lfs)); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); if (ret >= 0) { @@ -1222,7 +1200,7 @@ static int littlefs_unbind(FAR void *handle, FAR struct inode **driver, /* Release the mountpoint private data */ - nxsem_destroy(&fs->sem); + nxmutex_destroy(&fs->lock); kmm_free(fs); } @@ -1255,7 +1233,7 @@ static int littlefs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) buf->f_bfree = fs->cfg.block_count; buf->f_bavail = fs->cfg.block_count; - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -1270,7 +1248,7 @@ static int littlefs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) ret = 0; } - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -1293,14 +1271,14 @@ static int littlefs_unlink(FAR struct inode *mountpt, /* Call the LFS to perform the unlink */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; } ret = littlefs_convert_result(lfs_remove(&fs->lfs, relpath)); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -1324,14 +1302,14 @@ static int littlefs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, /* Call LFS to do the mkdir */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; } ret = lfs_mkdir(&fs->lfs, relpath); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -1378,7 +1356,7 @@ static int littlefs_rename(FAR struct inode *mountpt, /* Call LFS to do the rename */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; @@ -1386,7 +1364,7 @@ static int littlefs_rename(FAR struct inode *mountpt, ret = littlefs_convert_result(lfs_rename(&fs->lfs, oldrelpath, newrelpath)); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); return ret; } @@ -1413,14 +1391,14 @@ static int littlefs_stat(FAR struct inode *mountpt, FAR const char *relpath, /* Call the LFS to do the stat operation */ - ret = littlefs_semtake(fs); + ret = nxmutex_lock(&fs->lock); if (ret < 0) { return ret; } ret = lfs_stat(&fs->lfs, relpath, &info); - littlefs_semgive(fs); + nxmutex_unlock(&fs->lock); if (ret >= 0) { diff --git a/fs/mmap/fs_munmap.c b/fs/mmap/fs_munmap.c index 6badbc0f29fc8..2bcef5de3aec2 100644 --- a/fs/mmap/fs_munmap.c +++ b/fs/mmap/fs_munmap.c @@ -52,7 +52,7 @@ static int file_munmap_(FAR void *start, size_t length, bool kernel) /* Find a region containing this start and length in the list of regions */ - ret = nxsem_wait(&g_rammaps.exclsem); + ret = nxmutex_lock(&g_rammaps.lock); if (ret < 0) { return ret; @@ -78,7 +78,7 @@ static int file_munmap_(FAR void *start, size_t length, bool kernel) { ferr("ERROR: Region not found\n"); ret = -EINVAL; - goto errout_with_semaphore; + goto errout_with_lock; } /* Get the offset from the beginning of the region and the actual number @@ -93,7 +93,7 @@ static int file_munmap_(FAR void *start, size_t length, bool kernel) { ferr("ERROR: Cannot umap without unmapping to the end\n"); ret = -ENOSYS; - goto errout_with_semaphore; + goto errout_with_lock; } /* Okay.. the region is beging umapped to the end. Make sure the length @@ -151,11 +151,11 @@ static int file_munmap_(FAR void *start, size_t length, bool kernel) curr->length = length; } - nxsem_post(&g_rammaps.exclsem); + nxmutex_unlock(&g_rammaps.lock); return OK; -errout_with_semaphore: - nxsem_post(&g_rammaps.exclsem); +errout_with_lock: + nxmutex_unlock(&g_rammaps.lock); return ret; #else return OK; diff --git a/fs/mmap/fs_rammap.c b/fs/mmap/fs_rammap.c index 222a70e3c6f87..95977cd9e81fd 100644 --- a/fs/mmap/fs_rammap.c +++ b/fs/mmap/fs_rammap.c @@ -48,7 +48,7 @@ struct fs_allmaps_s g_rammaps = { - SEM_INITIALIZER(1) + NXMUTEX_INITIALIZER }; /**************************************************************************** @@ -180,7 +180,7 @@ int rammap(FAR struct file *filep, size_t length, /* Add the buffer to the list of regions */ - ret = nxsem_wait(&g_rammaps.exclsem); + ret = nxmutex_lock(&g_rammaps.lock); if (ret < 0) { goto errout_with_region; @@ -189,7 +189,7 @@ int rammap(FAR struct file *filep, size_t length, map->flink = g_rammaps.head; g_rammaps.head = map; - nxsem_post(&g_rammaps.exclsem); + nxmutex_unlock(&g_rammaps.lock); *mapped = map->addr; return OK; diff --git a/fs/mmap/fs_rammap.h b/fs/mmap/fs_rammap.h index fd476645d804e..2ad01cab2e98f 100644 --- a/fs/mmap/fs_rammap.h +++ b/fs/mmap/fs_rammap.h @@ -28,7 +28,7 @@ #include #include -#include +#include #ifdef CONFIG_FS_RAMMAP @@ -65,7 +65,7 @@ struct fs_rammap_s struct fs_allmaps_s { - sem_t exclsem; /* Provides exclusive access the list */ + mutex_t lock; /* Provides exclusive access the list */ struct fs_rammap_s *head; /* List of mapped files */ }; diff --git a/fs/mount/fs_automount.c b/fs/mount/fs_automount.c index 489ee2fc10e90..e7c8caadd13a9 100644 --- a/fs/mount/fs_automount.c +++ b/fs/mount/fs_automount.c @@ -80,7 +80,7 @@ struct automounter_state_s bool inserted; /* True: Media has been inserted */ #ifdef CONFIG_FS_AUTOMOUNTER_DRIVER - sem_t exclsem; /* Supports exclusive access to the device */ + mutex_t lock; /* Supports exclusive access to the device */ bool registered; /* True: if driver has been registered */ /* The following is a singly linked list of open references to the @@ -168,10 +168,10 @@ static void automount_notify(FAR struct automounter_state_s *priv) /* Get exclusive access to the driver structure */ - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait_uninterruptible failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return; } @@ -192,7 +192,7 @@ static void automount_notify(FAR struct automounter_state_s *priv) } } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -208,10 +208,10 @@ static int automount_open(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -223,7 +223,7 @@ static int automount_open(FAR struct file *filep) { ierr("ERROR: Failed to allocate open structure\n"); ret = -ENOMEM; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Attach the open structure to the device */ @@ -236,8 +236,8 @@ static int automount_open(FAR struct file *filep) filep->f_priv = (FAR void *)opriv; ret = OK; -errout_with_exclsem: - nxsem_post(&priv->exclsem); +errout_with_excllock: + nxmutex_unlock(&priv->lock); return ret; } @@ -262,10 +262,10 @@ static int automount_close(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -280,7 +280,7 @@ static int automount_close(FAR struct file *filep) { ierr("ERROR: Failed to find open entry\n"); ret = -ENOENT; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Remove the structure from the device */ @@ -304,8 +304,8 @@ static int automount_close(FAR struct file *filep) ret = OK; -errout_with_exclsem: - nxsem_post(&priv->exclsem); +errout_with_excllock: + nxmutex_unlock(&priv->lock); return ret; } @@ -329,10 +329,10 @@ static int automount_ioctl(FAR struct file *filep, int cmd, /* Get exclusive access to the driver structure */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - ierr("ERROR: nxsem_wait failed: %d\n", ret); + ierr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -374,7 +374,7 @@ static int automount_ioctl(FAR struct file *filep, int cmd, break; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } #endif /* CONFIG_FS_AUTOMOUNTER_DRIVER */ @@ -406,7 +406,7 @@ static int automount_findinode(FAR const char *path) /* Get exclusive access to the in-memory inode tree. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { return ret; @@ -444,7 +444,7 @@ static int automount_findinode(FAR const char *path) /* Relinquish our exclusive access to the inode try and return the result */ - inode_semgive(); + inode_unlock(); RELEASE_SEARCH(&desc); return ret; } @@ -857,7 +857,7 @@ FAR void *automount_initialize(FAR const struct automount_lower_s *lower) /* Initialize the new automount driver instance */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Register driver */ @@ -928,7 +928,7 @@ void automount_uninitialize(FAR void *handle) unregister_driver(devpath); } - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); #endif /* CONFIG_FS_AUTOMOUNTER_DRIVER */ /* Cancel the watchdog timer */ diff --git a/fs/mount/fs_mount.c b/fs/mount/fs_mount.c index b203f5e14a3a7..0e81dd267ab16 100644 --- a/fs/mount/fs_mount.c +++ b/fs/mount/fs_mount.c @@ -335,7 +335,7 @@ int nx_mount(FAR const char *source, FAR const char *target, goto errout; } - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { goto errout_with_inode; @@ -365,7 +365,7 @@ int nx_mount(FAR const char *source, FAR const char *target, ferr("ERROR: target %s exists and is a special node\n", target); ret = -ENOTDIR; inode_release(mountpt_inode); - goto errout_with_semaphore; + goto errout_with_lock; } } else @@ -392,7 +392,7 @@ int nx_mount(FAR const char *source, FAR const char *target, */ ferr("ERROR: Failed to reserve inode for target %s\n", target); - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -455,7 +455,7 @@ int nx_mount(FAR const char *source, FAR const char *target, mountpt_inode->u.i_mops = mops; mountpt_inode->i_private = fshandle; - inode_semgive(); + inode_unlock(); /* We can release our reference to the blkdrver_inode, if the filesystem * wants to retain the blockdriver inode (which it should), then it must @@ -483,8 +483,8 @@ int nx_mount(FAR const char *source, FAR const char *target, inode_release(mountpt_inode); inode_remove(target); -errout_with_semaphore: - inode_semgive(); +errout_with_lock: + inode_unlock(); #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS RELEASE_SEARCH(&desc); #endif diff --git a/fs/mount/fs_umount2.c b/fs/mount/fs_umount2.c index 5a4330aa5e8d9..e7b920685859c 100644 --- a/fs/mount/fs_umount2.c +++ b/fs/mount/fs_umount2.c @@ -109,7 +109,7 @@ int nx_umount2(FAR const char *target, unsigned int flags) /* Hold the semaphore through the unbind logic */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { goto errout_with_mountpt; @@ -121,12 +121,12 @@ int nx_umount2(FAR const char *target, unsigned int flags) { /* The inode is unhappy with the blkdrvr for some reason */ - goto errout_with_semaphore; + goto errout_with_lock; } else if (ret > 0) { ret = -EBUSY; - goto errout_with_semaphore; + goto errout_with_lock; } /* Successfully unbound. Convert the mountpoint inode to regular @@ -146,7 +146,7 @@ int nx_umount2(FAR const char *target, unsigned int flags) DEBUGASSERT(mountpt_inode->i_crefs > 0); mountpt_inode->i_crefs--; - inode_semgive(); + inode_unlock(); } else #endif @@ -157,7 +157,7 @@ int nx_umount2(FAR const char *target, unsigned int flags) */ ret = inode_remove(target); - inode_semgive(); + inode_unlock(); /* The return value of -EBUSY is normal (in fact, it should * not be OK) @@ -188,8 +188,8 @@ int nx_umount2(FAR const char *target, unsigned int flags) /* A lot of goto's! But they make the error handling much simpler */ -errout_with_semaphore: - inode_semgive(); +errout_with_lock: + inode_unlock(); errout_with_mountpt: inode_release(mountpt_inode); diff --git a/fs/mqueue/mq_open.c b/fs/mqueue/mq_open.c index 47e853c51a6c7..35fe10e298436 100644 --- a/fs/mqueue/mq_open.c +++ b/fs/mqueue/mq_open.c @@ -276,14 +276,14 @@ static int file_mq_vopen(FAR struct file *mq, FAR const char *mq_name, /* Create an inode in the pseudo-filesystem at this path */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { goto errout_with_lock; } ret = inode_reserve(fullpath, mode, &inode); - inode_semgive(); + inode_unlock(); if (ret < 0) { diff --git a/fs/mqueue/mq_unlink.c b/fs/mqueue/mq_unlink.c index 58b82f9111d1e..9360ca2f7f90a 100644 --- a/fs/mqueue/mq_unlink.c +++ b/fs/mqueue/mq_unlink.c @@ -138,7 +138,7 @@ int file_mq_unlink(FAR const char *mq_name) * functioning as a directory and the directory is not empty. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { goto errout_with_inode; @@ -147,7 +147,7 @@ int file_mq_unlink(FAR const char *mq_name) if (inode->i_child != NULL) { ret = -ENOTEMPTY; - goto errout_with_semaphore; + goto errout_with_lock; } /* Remove the old inode from the tree. Because we hold a reference count @@ -172,14 +172,14 @@ int file_mq_unlink(FAR const char *mq_name) * in-use. */ - inode_semgive(); + inode_unlock(); mq_inode_release(inode); RELEASE_SEARCH(&desc); sched_unlock(); return OK; -errout_with_semaphore: - inode_semgive(); +errout_with_lock: + inode_unlock(); errout_with_inode: inode_release(inode); diff --git a/fs/nfs/nfs_mount.h b/fs/nfs/nfs_mount.h index d778d61e26ff1..9285828e329eb 100644 --- a/fs/nfs/nfs_mount.h +++ b/fs/nfs/nfs_mount.h @@ -49,7 +49,7 @@ ****************************************************************************/ #include -#include +#include #include "rpc.h" @@ -68,7 +68,7 @@ struct nfsmount { FAR struct nfsnode *nm_head; /* A list of all files opened on this mountpoint */ - sem_t nm_sem; /* Used to assure thread-safe access */ + mutex_t nm_lock; /* Used to assure thread-safe access */ nfsfh_t *nm_fh; /* File handle of root dir */ char nm_path[90]; /* server's path of the directory being mounted */ struct nfs_fattr nm_fattr; /* nfs file attribute cache */ diff --git a/fs/nfs/nfs_vfsops.c b/fs/nfs/nfs_vfsops.c index 2660724d335d1..797beb112cddb 100644 --- a/fs/nfs/nfs_vfsops.c +++ b/fs/nfs/nfs_vfsops.c @@ -126,9 +126,6 @@ static uint32_t nfs_xdrneg1; * Private Function Prototypes ****************************************************************************/ -static int nfs_semtake(FAR struct nfsmount *nmp); -static void nfs_semgive(FAR struct nfsmount *nmp); - static int nfs_filecreate(FAR struct nfsmount *nmp, FAR struct nfsnode *np, FAR const char *relpath, mode_t mode); @@ -229,24 +226,6 @@ const struct mountpt_operations nfs_operations = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: nfs_semtake - ****************************************************************************/ - -static int nfs_semtake(FAR struct nfsmount *nmp) -{ - return nxsem_wait_uninterruptible(&nmp->nm_sem); -} - -/**************************************************************************** - * Name: nfs_semgive - ****************************************************************************/ - -static void nfs_semgive(FAR struct nfsmount *nmp) -{ - nxsem_post(&nmp->nm_sem); -} - /**************************************************************************** * Name: nfs_filecreate * @@ -693,7 +672,7 @@ static int nfs_open(FAR struct file *filep, FAR const char *relpath, return -ENOMEM; } - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { kmm_free(np); @@ -714,7 +693,7 @@ static int nfs_open(FAR struct file *filep, FAR const char *relpath, if (ret != -ENOENT) { ferr("ERROR: nfs_fileopen failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* The file does not exist. Check if we were asked to create the file. @@ -729,7 +708,7 @@ static int nfs_open(FAR struct file *filep, FAR const char *relpath, */ ferr("ERROR: File does not exist\n"); - goto errout_with_semaphore; + goto errout_with_lock; } /* Create the file */ @@ -738,7 +717,7 @@ static int nfs_open(FAR struct file *filep, FAR const char *relpath, if (ret != OK) { ferr("ERROR: nfs_filecreate failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -761,16 +740,16 @@ static int nfs_open(FAR struct file *filep, FAR const char *relpath, np->n_next = nmp->nm_head; nmp->nm_head = np; - nfs_semgive(nmp); + nxmutex_unlock(&nmp->nm_lock); return OK; -errout_with_semaphore: +errout_with_lock: if (np) { kmm_free(np); } - nfs_semgive(nmp); + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -806,7 +785,7 @@ static int nfs_close(FAR struct file *filep) /* Get exclusive access to the mount structure. */ - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -868,7 +847,7 @@ static int nfs_close(FAR struct file *filep) } filep->f_priv = NULL; - nfs_semgive(nmp); + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -907,7 +886,7 @@ static ssize_t nfs_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(nmp != NULL); - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return (ssize_t)ret; @@ -983,7 +962,7 @@ static ssize_t nfs_read(FAR struct file *filep, FAR char *buffer, if (ret) { ferr("ERROR: nfs_request failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* The read was successful. Get a pointer to the beginning of the NFS @@ -1039,8 +1018,8 @@ static ssize_t nfs_read(FAR struct file *filep, FAR char *buffer, } } -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return bytesread > 0 ? bytesread : ret; } @@ -1082,7 +1061,7 @@ static ssize_t nfs_write(FAR struct file *filep, FAR const char *buffer, DEBUGASSERT(nmp != NULL); - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return (ssize_t)ret; @@ -1093,7 +1072,7 @@ static ssize_t nfs_write(FAR struct file *filep, FAR const char *buffer, if (np->n_size + buflen < np->n_size) { ret = -EFBIG; - goto errout_with_semaphore; + goto errout_with_lock; } /* Now loop until we send the entire user buffer */ @@ -1167,7 +1146,7 @@ static ssize_t nfs_write(FAR struct file *filep, FAR const char *buffer, if (ret) { ferr("ERROR: nfs_request failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Get a pointer to the WRITE reply data */ @@ -1203,7 +1182,7 @@ static ssize_t nfs_write(FAR struct file *filep, FAR const char *buffer, if (tmp < 1 || tmp > writesize) { ret = -EIO; - goto errout_with_semaphore; + goto errout_with_lock; } writesize = tmp; @@ -1228,8 +1207,8 @@ static ssize_t nfs_write(FAR struct file *filep, FAR const char *buffer, buffer += writesize; } -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return byteswritten > 0 ? byteswritten : ret; } @@ -1260,7 +1239,7 @@ static int nfs_dup(FAR const struct file *oldp, FAR struct file *newp) DEBUGASSERT(nmp != NULL); - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -1275,7 +1254,7 @@ static int nfs_dup(FAR const struct file *oldp, FAR struct file *newp) newp->f_priv = np; - nfs_semgive(nmp); + nxmutex_unlock(&nmp->nm_lock); return OK; } @@ -1307,7 +1286,7 @@ static int nfs_fstat(FAR const struct file *filep, FAR struct stat *buf) memset(buf, 0, sizeof(*buf)); - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -1326,7 +1305,7 @@ static int nfs_fstat(FAR const struct file *filep, FAR struct stat *buf) buf->st_mtim = np->n_mtime; buf->st_ctim = np->n_ctime; - nfs_semgive(nmp); + nxmutex_unlock(&nmp->nm_lock); return OK; } @@ -1357,7 +1336,7 @@ static int nfs_fchstat(FAR const struct file *filep, nmp = (FAR struct nfsmount *)filep->f_inode->i_private; DEBUGASSERT(nmp != NULL); - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -1367,7 +1346,7 @@ static int nfs_fchstat(FAR const struct file *filep, ret = nfs_filechstat(nmp, np, buf, flags); - nfs_semgive(nmp); + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -1396,7 +1375,7 @@ static int nfs_truncate(FAR struct file *filep, off_t length) DEBUGASSERT(nmp != NULL); - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret >= 0) { struct stat buf; @@ -1406,7 +1385,7 @@ static int nfs_truncate(FAR struct file *filep, off_t length) buf.st_size = length; ret = nfs_filechstat(nmp, np, &buf, CH_STAT_SIZE); - nfs_semgive(nmp); + nxmutex_unlock(&nmp->nm_lock); } return ret; @@ -1448,7 +1427,7 @@ static int nfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, return -ENOMEM; } - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { goto errout_with_ndir; @@ -1460,7 +1439,7 @@ static int nfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, if (ret != OK) { ferr("ERROR: nfs_findnode failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* The entry is a directory */ @@ -1470,7 +1449,7 @@ static int nfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, { ferr("ERROR: Not a directory, type=%" PRId32 "\n", objtype); ret = -ENOTDIR; - goto errout_with_semaphore; + goto errout_with_lock; } /* Save the directory information in struct fs_dirent_s so that it can be @@ -1482,11 +1461,11 @@ static int nfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, memcpy(ndir->nfs_fhandle, &fhandle.handle, fhandle.length); *dir = &ndir->nfs_base; - nfs_semgive(nmp); + nxmutex_unlock(&nmp->nm_lock); return 0; -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); errout_with_ndir: kmm_free(ndir); return ret; @@ -1548,7 +1527,7 @@ static int nfs_readdir(FAR struct inode *mountpt, nmp = mountpt->i_private; ndir = (FAR struct nfs_dir_s *)dir; - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -1605,7 +1584,7 @@ static int nfs_readdir(FAR struct inode *mountpt, if (ret != OK) { ferr("ERROR: nfs_request failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* A new group of entries was successfully read. Process the @@ -1653,7 +1632,7 @@ static int nfs_readdir(FAR struct inode *mountpt, { finfo("End of directory\n"); ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } /* What would it mean if there were not data and we not at the end of @@ -1726,7 +1705,7 @@ static int nfs_readdir(FAR struct inode *mountpt, if (ret != OK) { ferr("ERROR: nfs_lookup failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Set the dirent file type */ @@ -1769,8 +1748,8 @@ static int nfs_readdir(FAR struct inode *mountpt, finfo("type: %d->%d\n", (int)tmp, entry->d_type); -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -2035,13 +2014,13 @@ static int nfs_bind(FAR struct inode *blkdriver, FAR const void *data, /* Initialize the allocated mountpt state structure. */ - /* Initialize the semaphore that controls access. The initial count - * is zero, but nfs_semgive() is called at the completion of + /* Initialize the mutex that controls access. + * nxmutex_lock() is called at the completion of * initialization, incrementing the count to one. */ - nxsem_init(&nmp->nm_sem, 0, 0); /* Initialize the semaphore that - * controls access */ + nxmutex_init(&nmp->nm_lock); /* Initialize the mutex that + * controls access */ /* Initialize NFS */ @@ -2103,7 +2082,6 @@ static int nfs_bind(FAR struct inode *blkdriver, FAR const void *data, /* Mounted! */ *handle = (FAR void *)nmp; - nfs_semgive(nmp); finfo("Successfully mounted\n"); return OK; @@ -2120,7 +2098,7 @@ static int nfs_bind(FAR struct inode *blkdriver, FAR const void *data, /* Free connection-related resources */ - nxsem_destroy(&nmp->nm_sem); + nxmutex_destroy(&nmp->nm_lock); kmm_free(nmp); return ret; @@ -2148,7 +2126,7 @@ static int nfs_unbind(FAR void *handle, FAR struct inode **blkdriver, /* Get exclusive access to the mount structure */ - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -2169,7 +2147,7 @@ static int nfs_unbind(FAR void *handle, FAR struct inode **blkdriver, */ ret = (flags != 0) ? -ENOSYS : -EBUSY; - goto errout_with_semaphore; + goto errout_with_lock; } /* Disconnect from the server */ @@ -2178,14 +2156,14 @@ static int nfs_unbind(FAR void *handle, FAR struct inode **blkdriver, /* And free any allocated resources */ - nxsem_destroy(&nmp->nm_sem); + nxmutex_destroy(&nmp->nm_lock); kmm_free(nmp->nm_rpcclnt); kmm_free(nmp); return OK; -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -2327,7 +2305,7 @@ static int nfs_statfs(FAR struct inode *mountpt, FAR struct statfs *sbp) nmp = (FAR struct nfsmount *)mountpt->i_private; - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -2347,7 +2325,7 @@ static int nfs_statfs(FAR struct inode *mountpt, FAR struct statfs *sbp) (FAR void *)nmp->nm_iobuffer, nmp->nm_buflen); if (ret) { - goto errout_with_semaphore; + goto errout_with_lock; } sfp = (FAR struct rpc_reply_fsstat *)nmp->nm_iobuffer; @@ -2364,8 +2342,8 @@ static int nfs_statfs(FAR struct inode *mountpt, FAR struct statfs *sbp) sbp->f_ffree = tquad; sbp->f_namelen = NAME_MAX; -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -2399,7 +2377,7 @@ static int nfs_remove(FAR struct inode *mountpt, FAR const char *relpath) nmp = (FAR struct nfsmount *)mountpt->i_private; - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -2411,7 +2389,7 @@ static int nfs_remove(FAR struct inode *mountpt, FAR const char *relpath) if (ret != OK) { ferr("ERROR: nfs_finddir returned: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Create the REMOVE RPC call arguments */ @@ -2445,8 +2423,8 @@ static int nfs_remove(FAR struct inode *mountpt, FAR const char *relpath) (FAR void *)&nmp->nm_msgbuffer.removef, reqlen, (FAR void *)nmp->nm_iobuffer, nmp->nm_buflen); -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -2482,7 +2460,7 @@ static int nfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, nmp = (FAR struct nfsmount *) mountpt->i_private; - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -2496,7 +2474,7 @@ static int nfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, if (ret != OK) { ferr("ERROR: nfs_finddir returned: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Format the MKDIR call message arguments */ @@ -2572,8 +2550,8 @@ static int nfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ferr("ERROR: nfs_request failed: %d\n", ret); } -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -2607,7 +2585,7 @@ static int nfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath) nmp = (FAR struct nfsmount *)mountpt->i_private; - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -2621,7 +2599,7 @@ static int nfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath) if (ret != OK) { ferr("ERROR: nfs_finddir returned: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Set up the RMDIR call message arguments */ @@ -2655,8 +2633,8 @@ static int nfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath) (FAR void *)&nmp->nm_msgbuffer.rmdir, reqlen, (FAR void *)nmp->nm_iobuffer, nmp->nm_buflen); -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -2693,7 +2671,7 @@ static int nfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, nmp = (FAR struct nfsmount *)mountpt->i_private; - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -2705,7 +2683,7 @@ static int nfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, if (ret != OK) { ferr("ERROR: nfs_finddir returned: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Find the NFS node of the directory containing the 'to' object */ @@ -2714,7 +2692,7 @@ static int nfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, if (ret != OK) { ferr("ERROR: nfs_finddir returned: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Format the RENAME RPC arguments */ @@ -2768,8 +2746,8 @@ static int nfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, (FAR void *)&nmp->nm_msgbuffer.renamef, reqlen, (FAR void *)nmp->nm_iobuffer, nmp->nm_buflen); -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -2866,7 +2844,7 @@ static int nfs_stat(FAR struct inode *mountpt, FAR const char *relpath, memset(buf, 0, sizeof(*buf)); - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -2878,7 +2856,7 @@ static int nfs_stat(FAR struct inode *mountpt, FAR const char *relpath, if (ret != OK) { ferr("ERROR: nfs_findnode failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Extract the file mode, file type, and file size. */ @@ -2898,8 +2876,8 @@ static int nfs_stat(FAR struct inode *mountpt, FAR const char *relpath, fxdr_nfsv3time(&attributes.fa_ctime, &ts); buf->st_ctime = ts.tv_sec; -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } @@ -2931,7 +2909,7 @@ static int nfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, nmp = (FAR struct nfsmount *)mountpt->i_private; DEBUGASSERT(nmp && buf); - ret = nfs_semtake(nmp); + ret = nxmutex_lock(&nmp->nm_lock); if (ret < 0) { return ret; @@ -2943,7 +2921,7 @@ static int nfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, if (ret != OK) { ferr("ERROR: nfs_findnode failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Initialize the nfs node */ @@ -2955,7 +2933,7 @@ static int nfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, ret = nfs_filechstat(nmp, &np, buf, flags); -errout_with_semaphore: - nfs_semgive(nmp); +errout_with_lock: + nxmutex_unlock(&nmp->nm_lock); return ret; } diff --git a/fs/nxffs/nxffs.h b/fs/nxffs/nxffs.h index 8f1116bc2956e..035b9abd7122b 100644 --- a/fs/nxffs/nxffs.h +++ b/fs/nxffs/nxffs.h @@ -33,6 +33,7 @@ #include #include +#include #include /**************************************************************************** @@ -274,7 +275,7 @@ struct nxffs_wrfile_s struct nxffs_volume_s { FAR struct mtd_dev_s *mtd; /* Supports FLASH access */ - sem_t exclsem; /* Used to assure thread-safe access */ + mutex_t lock; /* Used to assure thread-safe access */ sem_t wrsem; /* Enforces single writer restriction */ struct mtd_geometry_s geo; /* Device geometry */ uint8_t blkper; /* R/W blocks per erase block */ diff --git a/fs/nxffs/nxffs_dirent.c b/fs/nxffs/nxffs_dirent.c index 972f587b431f7..64cd5931525d7 100644 --- a/fs/nxffs/nxffs_dirent.c +++ b/fs/nxffs/nxffs_dirent.c @@ -80,7 +80,7 @@ int nxffs_opendir(FAR struct inode *mountpt, FAR const char *relpath, return -ENOMEM; } - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { goto errout_with_ndir; @@ -91,18 +91,18 @@ int nxffs_opendir(FAR struct inode *mountpt, FAR const char *relpath, if (relpath && relpath[0] != '\0') { ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } /* Set the offset to the offset to the first valid inode */ ndir->offset = volume->inoffset; - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); *dir = &ndir->base; return 0; -errout_with_semaphore: - nxsem_post(&volume->exclsem); +errout_with_lock: + nxmutex_unlock(&volume->lock); errout_with_ndir: kmm_free(ndir); @@ -150,7 +150,7 @@ int nxffs_readdir(FAR struct inode *mountpt, volume = mountpt->i_private; ndir = (FAR struct nxffs_dir_s *)dir; - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { goto errout; @@ -181,7 +181,7 @@ int nxffs_readdir(FAR struct inode *mountpt, ret = OK; } - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); errout: return ret; @@ -209,7 +209,7 @@ int nxffs_rewinddir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir) /* Recover the file system state from the NuttX inode instance */ volume = mountpt->i_private; - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { goto errout; @@ -220,7 +220,7 @@ int nxffs_rewinddir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir) ((FAR struct nxffs_dir_s *)dir)->offset = volume->inoffset; ret = OK; - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); errout: return ret; diff --git a/fs/nxffs/nxffs_initialize.c b/fs/nxffs/nxffs_initialize.c index 1aaf9fe84869c..05b04ae20c577 100644 --- a/fs/nxffs/nxffs_initialize.c +++ b/fs/nxffs/nxffs_initialize.c @@ -171,7 +171,7 @@ int nxffs_initialize(FAR struct mtd_dev_s *mtd) volume->mtd = mtd; volume->cblock = (off_t)-1; - nxsem_init(&volume->exclsem, 0, 1); + nxmutex_init(&volume->lock); nxsem_init(&volume->wrsem, 0, 1); /* Get the volume geometry. (casting to uintptr_t first eliminates diff --git a/fs/nxffs/nxffs_ioctl.c b/fs/nxffs/nxffs_ioctl.c index 7b4bf8522c348..fec6cc35487b9 100644 --- a/fs/nxffs/nxffs_ioctl.c +++ b/fs/nxffs/nxffs_ioctl.c @@ -63,11 +63,11 @@ int nxffs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) volume = filep->f_inode->i_private; DEBUGASSERT(volume != NULL); - /* Get exclusive access to the volume. Note that the volume exclsem + /* Get exclusive access to the volume. Note that the volume lock * protects the open file list. */ - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { ferr("ERROR: nxsem_wait failed: %d\n", ret); @@ -86,7 +86,7 @@ int nxffs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { ferr("ERROR: Open files\n"); ret = -EBUSY; - goto errout_with_semaphore; + goto errout_with_excllock; } /* Re-format the volume -- all is lost */ @@ -109,8 +109,8 @@ int nxffs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) ret = MTD_IOCTL(volume->mtd, cmd, arg); } -errout_with_semaphore: - nxsem_post(&volume->exclsem); +errout_with_excllock: + nxmutex_unlock(&volume->lock); errout: return ret; } diff --git a/fs/nxffs/nxffs_open.c b/fs/nxffs/nxffs_open.c index f7dfeabd8c6e0..5f5e05c9b11af 100644 --- a/fs/nxffs/nxffs_open.c +++ b/fs/nxffs/nxffs_open.c @@ -382,15 +382,15 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, goto errout; } - /* Get exclusive access to the volume. Note that the volume exclsem - * protects the open file list. Note that exclsem is ALWAYS taken + /* Get exclusive access to the volume. Note that the volume lock + * protects the open file list. Note that lock is ALWAYS taken * after wrsem to avoid deadlocks. */ - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { - ferr("ERROR: nxsem_wait failed: %d\n", ret); + ferr("ERROR: nxmutex_lock failed: %d\n", ret); goto errout_with_wrsem; } @@ -416,7 +416,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, ferr("ERROR: File is open for reading\n"); ret = -ENOSYS; - goto errout_with_exclsem; + goto errout_with_excllock; } /* It would be an error if we are asked to create the file @@ -427,7 +427,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, { ferr("ERROR: File exists, can't create O_EXCL\n"); ret = -EEXIST; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Were we asked to truncate the file? NOTE: Don't truncate the @@ -455,7 +455,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, ferr("ERROR: File %s exists and we were not asked to " "truncate it\n", name); ret = -ENOSYS; - goto errout_with_exclsem; + goto errout_with_excllock; } } @@ -467,7 +467,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, { ferr("ERROR: Not asked to create the file\n"); ret = -ENOENT; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Make sure that the length of the file name will fit in a uint8_t */ @@ -477,7 +477,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, { ferr("ERROR: Name is too long: %d\n", namlen); ret = -EINVAL; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Yes.. Create a new structure that will describe the state of this open @@ -494,7 +494,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, if (!wrfile) { ret = -ENOMEM; - goto errout_with_exclsem; + goto errout_with_excllock; } #endif @@ -644,13 +644,13 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, volume->ofiles = &wrfile->ofile; /* Indicate that the volume is open for writing and return the open file - * instance. Releasing exclsem allows other readers while the write is + * instance. Releasing lock allows other readers while the write is * in progress. But wrsem is still held for this open file, preventing * any further writers until this inode is closed.s */ *ppofile = &wrfile->ofile; - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); return OK; errout_with_name: @@ -660,8 +660,8 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, kmm_free(wrfile); #endif -errout_with_exclsem: - nxsem_post(&volume->exclsem); +errout_with_excllock: + nxmutex_unlock(&volume->lock); errout_with_wrsem: nxsem_post(&volume->wrsem); errout: @@ -683,11 +683,11 @@ static inline int nxffs_rdopen(FAR struct nxffs_volume_s *volume, FAR struct nxffs_ofile_s *ofile; int ret; - /* Get exclusive access to the volume. Note that the volume exclsem + /* Get exclusive access to the volume. Note that the volume lock * protects the open file list. */ - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret != OK) { ferr("ERROR: nxsem_wait failed: %d\n", ret); @@ -707,7 +707,7 @@ static inline int nxffs_rdopen(FAR struct nxffs_volume_s *volume, { ferr("ERROR: File is open for writing\n"); ret = -ENOSYS; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Just increment the reference count on the ofile */ @@ -731,7 +731,7 @@ static inline int nxffs_rdopen(FAR struct nxffs_volume_s *volume, { ferr("ERROR: ofile allocation failed\n"); ret = -ENOMEM; - goto errout_with_exclsem; + goto errout_with_excllock; } /* Initialize the open file state structure */ @@ -757,13 +757,13 @@ static inline int nxffs_rdopen(FAR struct nxffs_volume_s *volume, /* Return the open file state structure */ *ppofile = ofile; - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); return OK; errout_with_ofile: kmm_free(ofile); -errout_with_exclsem: - nxsem_post(&volume->exclsem); +errout_with_excllock: + nxmutex_unlock(&volume->lock); errout: return ret; } @@ -928,7 +928,7 @@ FAR struct nxffs_ofile_s *nxffs_findofile(FAR struct nxffs_volume_s *volume, { FAR struct nxffs_ofile_s *ofile; - /* Check every open file. Note that the volume exclsem protects the + /* Check every open file. Note that the volume lock protects the * list of open files. */ @@ -1075,7 +1075,7 @@ int nxffs_dup(FAR const struct file *oldp, FAR struct file *newp) ofile = (FAR struct nxffs_ofile_s *)oldp->f_priv; /* I do not think we need exclusive access to the volume to do this. - * The volume exclsem protects the open file list and, hence, would + * The volume lock protects the open file list and, hence, would * assure that the ofile is stable. However, it is assumed that the * caller holds a value file descriptor associated with this ofile, * so it should be stable throughout the life of this function. @@ -1127,11 +1127,11 @@ int nxffs_close(FAR struct file *filep) volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private; DEBUGASSERT(volume != NULL); - /* Get exclusive access to the volume. Note that the volume exclsem + /* Get exclusive access to the volume. Note that the volume lock * protects the open file list. */ - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret != OK) { ferr("ERROR: nxsem_wait failed: %d\n", ret); @@ -1170,7 +1170,7 @@ int nxffs_close(FAR struct file *filep) } filep->f_priv = NULL; - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); errout: return ret; diff --git a/fs/nxffs/nxffs_read.c b/fs/nxffs/nxffs_read.c index ea8c75e65c1b3..dbc9498628116 100644 --- a/fs/nxffs/nxffs_read.c +++ b/fs/nxffs/nxffs_read.c @@ -153,11 +153,11 @@ ssize_t nxffs_read(FAR struct file *filep, FAR char *buffer, size_t buflen) volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private; DEBUGASSERT(volume != NULL); - /* Get exclusive access to the volume. Note that the volume exclsem + /* Get exclusive access to the volume. Note that the volume lock * protects the open file list. */ - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { ferr("ERROR: nxsem_wait failed: %d\n", ret); @@ -170,7 +170,7 @@ ssize_t nxffs_read(FAR struct file *filep, FAR char *buffer, size_t buflen) { ferr("ERROR: File not open for read access\n"); ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_excllock; } /* Loop until all bytes have been read */ @@ -194,7 +194,7 @@ ssize_t nxffs_read(FAR struct file *filep, FAR char *buffer, size_t buflen) { ferr("ERROR: nxffs_rdseek failed: %d\n", -ret); ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_excllock; } /* How many bytes are available at this offset */ @@ -219,11 +219,11 @@ ssize_t nxffs_read(FAR struct file *filep, FAR char *buffer, size_t buflen) total += readsize; } - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); return total; -errout_with_semaphore: - nxsem_post(&volume->exclsem); +errout_with_excllock: + nxmutex_unlock(&volume->lock); errout: return (ssize_t)ret; } diff --git a/fs/nxffs/nxffs_stat.c b/fs/nxffs/nxffs_stat.c index 93602553f5c42..a5a382bfe35f4 100644 --- a/fs/nxffs/nxffs_stat.c +++ b/fs/nxffs/nxffs_stat.c @@ -63,7 +63,7 @@ int nxffs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) /* Get the mountpoint private data from the NuttX inode structure */ volume = mountpt->i_private; - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { goto errout; @@ -82,7 +82,7 @@ int nxffs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) SIZEOF_NXFFS_INODE_HDR; ret = OK; - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); errout: return ret; @@ -111,7 +111,7 @@ int nxffs_stat(FAR struct inode *mountpt, FAR const char *relpath, /* Get the mountpoint private data from the NuttX inode structure */ volume = mountpt->i_private; - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret != OK) { goto errout; @@ -132,7 +132,7 @@ int nxffs_stat(FAR struct inode *mountpt, FAR const char *relpath, if (ret < 0) { ferr("ERROR: Inode '%s' not found: %d\n", relpath, -ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Return status information based on the directory entry */ @@ -159,8 +159,8 @@ int nxffs_stat(FAR struct inode *mountpt, FAR const char *relpath, ret = OK; -errout_with_semaphore: - nxsem_post(&volume->exclsem); +errout_with_lock: + nxmutex_unlock(&volume->lock); errout: return ret; @@ -194,11 +194,11 @@ int nxffs_fstat(FAR const struct file *filep, FAR struct stat *buf) volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private; DEBUGASSERT(volume != NULL); - /* Get exclusive access to the volume. Note that the volume exclsem + /* Get exclusive access to the volume. Note that the volume lock * protects the open file list. */ - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret != OK) { ferr("ERROR: nxsem_wait failed: %d\n", ret); @@ -215,6 +215,6 @@ int nxffs_fstat(FAR const struct file *filep, FAR struct stat *buf) buf->st_mtime = ofile->entry.utc; buf->st_ctime = ofile->entry.utc; - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); return OK; } diff --git a/fs/nxffs/nxffs_truncate.c b/fs/nxffs/nxffs_truncate.c index becf78e5c99fc..98f5339539b91 100644 --- a/fs/nxffs/nxffs_truncate.c +++ b/fs/nxffs/nxffs_truncate.c @@ -68,11 +68,11 @@ int nxffs_truncate(FAR struct file *filep, off_t length) volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private; DEBUGASSERT(volume != NULL); - /* Get exclusive access to the volume. Note that the volume exclsem + /* Get exclusive access to the volume. Note that the volume lock * protects the open file list. */ - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { ferr("ERROR: nxsem_wait failed: %d\n", ret); @@ -85,7 +85,7 @@ int nxffs_truncate(FAR struct file *filep, off_t length) { ferr("ERROR: File not open for write access\n"); ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Are we shrinking the file? Or extending it? */ @@ -94,7 +94,7 @@ int nxffs_truncate(FAR struct file *filep, off_t length) if (oldsize == length) { ret = OK; - goto errout_with_semaphore; + goto errout_with_lock; } else if (oldsize > length) { @@ -114,8 +114,8 @@ int nxffs_truncate(FAR struct file *filep, off_t length) ret = nxffs_wrextend(volume, wrfile, length); } -errout_with_semaphore: - nxsem_post(&volume->exclsem); +errout_with_lock: + nxmutex_unlock(&volume->lock); errout: return ret; diff --git a/fs/nxffs/nxffs_unlink.c b/fs/nxffs/nxffs_unlink.c index ddd5366823b2e..b2041554c99b9 100644 --- a/fs/nxffs/nxffs_unlink.c +++ b/fs/nxffs/nxffs_unlink.c @@ -141,7 +141,7 @@ int nxffs_unlink(FAR struct inode *mountpt, FAR const char *relpath) /* Get the mountpoint private data from the NuttX inode structure */ volume = mountpt->i_private; - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret != OK) { goto errout; @@ -151,7 +151,7 @@ int nxffs_unlink(FAR struct inode *mountpt, FAR const char *relpath) ret = nxffs_rminode(volume, relpath); - nxsem_post(&volume->exclsem); + nxmutex_unlock(&volume->lock); errout: return ret; diff --git a/fs/nxffs/nxffs_write.c b/fs/nxffs/nxffs_write.c index d32d6450fc01e..14f9cc0d51b0c 100644 --- a/fs/nxffs/nxffs_write.c +++ b/fs/nxffs/nxffs_write.c @@ -530,11 +530,11 @@ ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer, volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private; DEBUGASSERT(volume != NULL); - /* Get exclusive access to the volume. Note that the volume exclsem + /* Get exclusive access to the volume. Note that the volume lock * protects the open file list. */ - ret = nxsem_wait(&volume->exclsem); + ret = nxmutex_lock(&volume->lock); if (ret < 0) { ferr("ERROR: nxsem_wait failed: %d\n", ret); @@ -547,7 +547,7 @@ ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer, { ferr("ERROR: File not open for write access\n"); ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Loop until we successfully appended all of the data to the file (or an @@ -569,7 +569,7 @@ ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer, if (ret < 0) { ferr("ERROR: Failed to allocate a data block: %d\n", -ret); - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -585,7 +585,7 @@ ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer, if (ret < 0) { ferr("ERROR: Failed to verify FLASH data block: %d\n", -ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Append the data to the end of the data block and write the updated @@ -597,7 +597,7 @@ ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer, { ferr("ERROR: Failed to append to FLASH to a data block: %d\n", -ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Decrement the number of bytes remaining to be written */ @@ -610,8 +610,8 @@ ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer, ret = total; filep->f_pos = wrfile->datlen; -errout_with_semaphore: - nxsem_post(&volume->exclsem); +errout_with_lock: + nxmutex_unlock(&volume->lock); errout: return ret; } diff --git a/fs/romfs/fs_romfs.c b/fs/romfs/fs_romfs.c index 5db8e1a34c3d6..031f0078aefdf 100644 --- a/fs/romfs/fs_romfs.c +++ b/fs/romfs/fs_romfs.c @@ -175,7 +175,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, /* Check if the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; @@ -185,7 +185,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, if (ret != OK) { ferr("ERROR: romfs_checkmount failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* ROMFS is read-only. Any attempt to open with any kind of write @@ -196,7 +196,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, { ferr("ERROR: Only O_RDONLY supported\n"); ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Locate the directory entry for this path */ @@ -206,7 +206,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, { ferr("ERROR: Failed to find directory directory entry for '%s': %d\n", relpath, ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* The full path exists -- but is the final component a file @@ -223,7 +223,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, ret = -EISDIR; ferr("ERROR: '%s' is a directory\n", relpath); - goto errout_with_semaphore; + goto errout_with_lock; } else if (!IS_FILE(nodeinfo.rn_next)) { @@ -237,7 +237,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, ret = -ENXIO; ferr("ERROR: '%s' is a special file\n", relpath); - goto errout_with_semaphore; + goto errout_with_lock; } /* Create an instance of the file private data to describe the opened @@ -249,7 +249,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, { ferr("ERROR: Failed to allocate private data\n"); ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Initialize the file private data (only need to initialize @@ -267,7 +267,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, { ferr("ERROR: Failed to locate start of file data: %d\n", ret); kmm_free(rf); - goto errout_with_semaphore; + goto errout_with_lock; } /* Configure buffering to support access to this file */ @@ -277,7 +277,7 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, { ferr("ERROR: Failed configure buffering: %d\n", ret); kmm_free(rf); - goto errout_with_semaphore; + goto errout_with_lock; } /* Attach the private date to the struct file instance */ @@ -286,8 +286,8 @@ static int romfs_open(FAR struct file *filep, FAR const char *relpath, rm->rm_refs++; -errout_with_semaphore: - romfs_semgive(rm); +errout_with_lock: + nxmutex_unlock(&rm->rm_lock); return ret; } @@ -314,14 +314,14 @@ static int romfs_close(FAR struct file *filep) DEBUGASSERT(rm != NULL); - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; } rm->rm_refs--; - romfs_semgive(rm); + nxmutex_unlock(&rm->rm_lock); /* Do not check if the mount is healthy. We must support closing of * the file even when there is healthy mount. @@ -380,7 +380,7 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer, /* Make sure that the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return (ssize_t)ret; @@ -390,7 +390,7 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer, if (ret != OK) { ferr("ERROR: romfs_checkmount failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Get the number of bytes left in the file */ @@ -438,7 +438,7 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer, if (ret < 0) { ferr("ERROR: romfs_hwread failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } bytesread = nsectors * rm->rm_hwsectorsize; @@ -455,7 +455,7 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer, if (ret < 0) { ferr("ERROR: romfs_filecacheread failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Copy the partial sector into the user buffer */ @@ -481,8 +481,8 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer, buflen -= bytesread; } -errout_with_semaphore: - romfs_semgive(rm); +errout_with_lock: + nxmutex_unlock(&rm->rm_lock); return ret < 0 ? ret : readsize; } @@ -537,7 +537,7 @@ static off_t romfs_seek(FAR struct file *filep, off_t offset, int whence) /* Make sure that the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return (off_t)ret; @@ -547,7 +547,7 @@ static off_t romfs_seek(FAR struct file *filep, off_t offset, int whence) if (ret != OK) { ferr("ERROR: romfs_checkmount failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Limit positions to the end of the file. */ @@ -564,8 +564,8 @@ static off_t romfs_seek(FAR struct file *filep, off_t offset, int whence) filep->f_pos = position; finfo("New file position: %jd\n", (intmax_t)filep->f_pos); -errout_with_semaphore: - romfs_semgive(rm); +errout_with_lock: + nxmutex_unlock(&rm->rm_lock); return ret; } @@ -643,7 +643,7 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp) /* Check if the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; @@ -653,7 +653,7 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp) if (ret != OK) { ferr("ERROR: romfs_checkmount failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Recover the old private data from the old struct file instance */ @@ -669,7 +669,7 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp) { ferr("ERROR: Failed to allocate private data\n"); ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Copy all file private data (except for the buffer) */ @@ -684,7 +684,7 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp) { kmm_free(newrf); ferr("ERROR: Failed configure buffering: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Attach the new private date to the new struct file instance */ @@ -693,8 +693,8 @@ static int romfs_dup(FAR const struct file *oldp, FAR struct file *newp) rm->rm_refs++; -errout_with_semaphore: - romfs_semgive(rm); +errout_with_lock: + nxmutex_unlock(&rm->rm_lock); return ret; } @@ -729,7 +729,7 @@ static int romfs_fstat(FAR const struct file *filep, FAR struct stat *buf) /* Check if the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; @@ -744,7 +744,7 @@ static int romfs_fstat(FAR const struct file *filep, FAR struct stat *buf) rm->rm_hwsectorsize, buf); } - romfs_semgive(rm); + nxmutex_unlock(&rm->rm_lock); return ret; } @@ -782,7 +782,7 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, /* Make sure that the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { goto errout_with_rdir; @@ -792,7 +792,7 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, if (ret != OK) { ferr("ERROR: romfs_checkmount failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Find the requested directory */ @@ -801,7 +801,7 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, if (ret < 0) { ferr("ERROR: Failed to find directory '%s': %d\n", relpath, ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Verify that it is some kind of directory */ @@ -812,7 +812,7 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, ferr("ERROR: '%s' is not a directory\n", relpath); ret = -ENOTDIR; - goto errout_with_semaphore; + goto errout_with_lock; } /* The entry is a directory */ @@ -826,11 +826,11 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, #endif *dir = &rdir->base; - romfs_semgive(rm); + nxmutex_unlock(&rm->rm_lock); return OK; -errout_with_semaphore: - romfs_semgive(rm); +errout_with_lock: + nxmutex_unlock(&rm->rm_lock); errout_with_rdir: kmm_free(rdir); @@ -886,7 +886,7 @@ static int romfs_readdir(FAR struct inode *mountpt, /* Make sure that the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; @@ -896,7 +896,7 @@ static int romfs_readdir(FAR struct inode *mountpt, if (ret != OK) { ferr("ERROR: omfs_checkmount failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Loop, skipping over unsupported items in the file system */ @@ -917,7 +917,7 @@ static int romfs_readdir(FAR struct inode *mountpt, finfo("End of directory\n"); ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } #ifdef CONFIG_FS_ROMFS_CACHE_NODE @@ -933,7 +933,7 @@ static int romfs_readdir(FAR struct inode *mountpt, if (ret < 0) { ferr("ERROR: romfs_parsedirentry failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Save the filename */ @@ -943,7 +943,7 @@ static int romfs_readdir(FAR struct inode *mountpt, if (ret < 0) { ferr("ERROR: romfs_parsefilename failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Set up the next directory entry offset */ @@ -970,8 +970,8 @@ static int romfs_readdir(FAR struct inode *mountpt, } } -errout_with_semaphore: - romfs_semgive(rm); +errout_with_lock: + nxmutex_unlock(&rm->rm_lock); return ret; } @@ -1002,7 +1002,7 @@ static int romfs_rewinddir(FAR struct inode *mountpt, /* Make sure that the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; @@ -1018,7 +1018,7 @@ static int romfs_rewinddir(FAR struct inode *mountpt, #endif } - romfs_semgive(rm); + nxmutex_unlock(&rm->rm_lock); return ret; } @@ -1071,8 +1071,8 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data, * have to addref() here (but does have to release in ubind(). */ - nxsem_init(&rm->rm_sem, 0, 0); /* Initialize the semaphore that controls access */ - rm->rm_blkdriver = blkdriver; /* Save the block driver reference */ + nxmutex_init(&rm->rm_lock); /* Initialize the mutex that controls access */ + rm->rm_blkdriver = blkdriver; /* Save the block driver reference */ /* Get the hardware configuration and setup buffering appropriately */ @@ -1080,7 +1080,7 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data, if (ret < 0) { ferr("ERROR: romfs_hwconfigure failed: %d\n", ret); - goto errout_with_sem; + goto errout; } /* Then complete the mount by getting the ROMFS configuratrion from @@ -1097,7 +1097,6 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data, /* Mounted! */ *handle = (FAR void *)rm; - romfs_semgive(rm); return OK; errout_with_buffer: @@ -1106,8 +1105,7 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data, kmm_free(rm->rm_buffer); } -errout_with_sem: - nxsem_destroy(&rm->rm_sem); +errout: kmm_free(rm); return ret; } @@ -1137,7 +1135,7 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver, /* Check if there are sill any files opened on the filesystem. */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; @@ -1192,12 +1190,12 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver, #ifdef CONFIG_FS_ROMFS_CACHE_NODE romfs_freenode(rm->rm_root); #endif - nxsem_destroy(&rm->rm_sem); + nxmutex_destroy(&rm->rm_lock); kmm_free(rm); return OK; } - romfs_semgive(rm); + nxmutex_unlock(&rm->rm_lock); return ret; } @@ -1225,7 +1223,7 @@ static int romfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) /* Check if the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; @@ -1235,7 +1233,7 @@ static int romfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) if (ret < 0) { ferr("ERROR: romfs_checkmount failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Fill in the statfs info */ @@ -1254,8 +1252,8 @@ static int romfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) buf->f_bavail = 0; buf->f_namelen = NAME_MAX; -errout_with_semaphore: - romfs_semgive(rm); +errout_with_lock: + nxmutex_unlock(&rm->rm_lock); return ret; } @@ -1342,7 +1340,7 @@ static int romfs_stat(FAR struct inode *mountpt, FAR const char *relpath, /* Check if the mount is still healthy */ - ret = romfs_semtake(rm); + ret = nxmutex_lock(&rm->rm_lock); if (ret < 0) { return ret; @@ -1352,7 +1350,7 @@ static int romfs_stat(FAR struct inode *mountpt, FAR const char *relpath, if (ret != OK) { ferr("ERROR: romfs_checkmount failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Find the directory entry corresponding to relpath. */ @@ -1364,7 +1362,7 @@ static int romfs_stat(FAR struct inode *mountpt, FAR const char *relpath, if (ret < 0) { finfo("Failed to find directory: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Return information about the directory entry */ @@ -1372,8 +1370,8 @@ static int romfs_stat(FAR struct inode *mountpt, FAR const char *relpath, type = (uint8_t)(nodeinfo.rn_next & RFNEXT_ALLMODEMASK); ret = romfs_stat_common(type, nodeinfo.rn_size, rm->rm_hwsectorsize, buf); -errout_with_semaphore: - romfs_semgive(rm); +errout_with_lock: + nxmutex_unlock(&rm->rm_lock); return ret; } diff --git a/fs/romfs/fs_romfs.h b/fs/romfs/fs_romfs.h index 314f6fafddb5b..4e28b8e319b4e 100644 --- a/fs/romfs/fs_romfs.h +++ b/fs/romfs/fs_romfs.h @@ -132,7 +132,7 @@ struct romfs_mountpt_s #endif bool rm_mounted; /* true: The file system is ready */ uint16_t rm_hwsectorsize; /* HW: Sector size reported by block driver */ - sem_t rm_sem; /* Used to assume thread-safe access */ + mutex_t rm_lock; /* Used to assume thread-safe access */ uint32_t rm_refs; /* The references for all files opened on this mountpoint */ uint32_t rm_hwnsectors; /* HW: The number of sectors reported by the hardware */ uint32_t rm_volsize; /* Size of the ROMFS volume */ @@ -188,8 +188,6 @@ extern "C" * Public Function Prototypes ****************************************************************************/ -int romfs_semtake(FAR struct romfs_mountpt_s *rm); -void romfs_semgive(FAR struct romfs_mountpt_s *rm); int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer, uint32_t sector, unsigned int nsectors); int romfs_filecacheread(FAR struct romfs_mountpt_s *rm, diff --git a/fs/romfs/fs_romfsutil.c b/fs/romfs/fs_romfsutil.c index d70b20a8160dd..1f9a057be3a25 100644 --- a/fs/romfs/fs_romfsutil.c +++ b/fs/romfs/fs_romfsutil.c @@ -507,24 +507,6 @@ static int romfs_cachenode(FAR struct romfs_mountpt_s *rm, * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: romfs_semtake - ****************************************************************************/ - -int romfs_semtake(FAR struct romfs_mountpt_s *rm) -{ - return nxsem_wait_uninterruptible(&rm->rm_sem); -} - -/**************************************************************************** - * Name: romfs_semgive - ****************************************************************************/ - -void romfs_semgive(FAR struct romfs_mountpt_s *rm) -{ - nxsem_post(&rm->rm_sem); -} - /**************************************************************************** * Name: romfs_hwread * diff --git a/fs/rpmsgfs/rpmsgfs.c b/fs/rpmsgfs/rpmsgfs.c index e95851097be5a..50a71a9af1d60 100644 --- a/fs/rpmsgfs/rpmsgfs.c +++ b/fs/rpmsgfs/rpmsgfs.c @@ -37,6 +37,7 @@ #include #include +#include #include #include @@ -77,7 +78,7 @@ struct rpmsgfs_ofile_s struct rpmsgfs_mountpt_s { - sem_t fs_sem; /* Assure thread-safe access */ + mutex_t fs_lock; /* Assure thread-safe access */ FAR struct rpmsgfs_ofile_s *fs_head; /* Singly-linked list of open files */ char fs_root[PATH_MAX]; void *handle; @@ -188,24 +189,6 @@ const struct mountpt_operations rpmsgfs_operations = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: rpmsgfs_semtake - ****************************************************************************/ - -static int rpmsgfs_semtake(FAR struct rpmsgfs_mountpt_s *fs) -{ - return nxsem_wait_uninterruptible(&fs->fs_sem); -} - -/**************************************************************************** - * Name: rpmsgfs_semgive - ****************************************************************************/ - -static void rpmsgfs_semgive(FAR struct rpmsgfs_mountpt_s *fs) -{ - nxsem_post(&fs->fs_sem); -} - /**************************************************************************** * Name: rpmsgfs_mkpath * @@ -308,9 +291,9 @@ static int rpmsgfs_open(FAR struct file *filep, FAR const char *relpath, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -322,7 +305,7 @@ static int rpmsgfs_open(FAR struct file *filep, FAR const char *relpath, if (hf == NULL) { ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Append to the host's root directory */ @@ -373,13 +356,13 @@ static int rpmsgfs_open(FAR struct file *filep, FAR const char *relpath, fs->fs_head = hf; ret = OK; - goto errout_with_semaphore; + goto errout_with_lock; errout_with_buffer: kmm_free(hf); -errout_with_semaphore: - rpmsgfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); if (ret == -EINVAL) { ret = -EIO; @@ -411,9 +394,9 @@ static int rpmsgfs_close(FAR struct file *filep) fs = inode->i_private; hf = filep->f_priv; - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -473,7 +456,7 @@ static int rpmsgfs_close(FAR struct file *filep) kmm_free(hf); okout: - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; } @@ -501,9 +484,9 @@ static ssize_t rpmsgfs_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -517,7 +500,7 @@ static ssize_t rpmsgfs_read(FAR struct file *filep, FAR char *buffer, filep->f_pos += ret; } - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -543,9 +526,9 @@ static ssize_t rpmsgfs_write(FAR struct file *filep, const char *buffer, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -558,7 +541,7 @@ static ssize_t rpmsgfs_write(FAR struct file *filep, const char *buffer, if ((hf->oflags & O_WROK) == 0) { ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Call the host to perform the write */ @@ -569,8 +552,8 @@ static ssize_t rpmsgfs_write(FAR struct file *filep, const char *buffer, filep->f_pos += ret; } -errout_with_semaphore: - rpmsgfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -597,9 +580,9 @@ static off_t rpmsgfs_seek(FAR struct file *filep, off_t offset, int whence) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -613,7 +596,7 @@ static off_t rpmsgfs_seek(FAR struct file *filep, off_t offset, int whence) filep->f_pos = ret; } - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -640,9 +623,9 @@ static int rpmsgfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -652,7 +635,7 @@ static int rpmsgfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) ret = rpmsgfs_client_ioctl(fs->handle, hf->fd, cmd, arg); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -683,9 +666,9 @@ static int rpmsgfs_sync(FAR struct file *filep) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -693,7 +676,7 @@ static int rpmsgfs_sync(FAR struct file *filep) rpmsgfs_client_sync(fs->handle, hf->fd); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; } @@ -757,9 +740,9 @@ static int rpmsgfs_fstat(FAR const struct file *filep, FAR struct stat *buf) fs = inode->i_private; DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -769,7 +752,7 @@ static int rpmsgfs_fstat(FAR const struct file *filep, FAR struct stat *buf) ret = rpmsgfs_client_fstat(fs->handle, hf->fd, buf); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -803,9 +786,9 @@ static int rpmsgfs_fchstat(FAR const struct file *filep, fs = inode->i_private; DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -815,7 +798,7 @@ static int rpmsgfs_fchstat(FAR const struct file *filep, ret = rpmsgfs_client_fchstat(fs->handle, hf->fd, buf, flags); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -848,9 +831,9 @@ static int rpmsgfs_ftruncate(FAR struct file *filep, off_t length) fs = inode->i_private; DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -860,7 +843,7 @@ static int rpmsgfs_ftruncate(FAR struct file *filep, off_t length) ret = rpmsgfs_client_ftruncate(fs->handle, hf->fd, length); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -893,9 +876,9 @@ static int rpmsgfs_opendir(FAR struct inode *mountpt, return -ENOMEM; } - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { goto errout_with_rdir; @@ -911,15 +894,15 @@ static int rpmsgfs_opendir(FAR struct inode *mountpt, if (rdir->dir == NULL) { ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } *dir = (FAR struct fs_dirent_s *)rdir; - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; -errout_with_semaphore: - rpmsgfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&fs->fs_lock); errout_with_rdir: kmm_free(rdir); @@ -949,9 +932,9 @@ static int rpmsgfs_closedir(FAR struct inode *mountpt, fs = mountpt->i_private; rdir = (FAR struct rpmsgfs_dir_s *)dir; - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -961,7 +944,7 @@ static int rpmsgfs_closedir(FAR struct inode *mountpt, rpmsgfs_client_closedir(fs->handle, rdir->dir); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); kmm_free(rdir); return OK; } @@ -990,9 +973,9 @@ static int rpmsgfs_readdir(FAR struct inode *mountpt, fs = mountpt->i_private; rdir = (FAR struct rpmsgfs_dir_s *)dir; - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1002,7 +985,7 @@ static int rpmsgfs_readdir(FAR struct inode *mountpt, ret = rpmsgfs_client_readdir(fs->handle, rdir->dir, entry); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1029,9 +1012,9 @@ static int rpmsgfs_rewinddir(FAR struct inode *mountpt, fs = mountpt->i_private; rdir = (FAR struct rpmsgfs_dir_s *)dir; - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1041,7 +1024,7 @@ static int rpmsgfs_rewinddir(FAR struct inode *mountpt, rpmsgfs_client_rewinddir(fs->handle, rdir->dir); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return OK; } @@ -1127,13 +1110,9 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data, return ret; } - /* If the global semaphore hasn't been initialized, then - * initialized it now. - */ - - /* Initialize the semaphore that controls access */ + /* Initialize the mutex that controls access */ - nxsem_init(&fs->fs_sem, 0, 1); + nxmutex_init(&fs->fs_lock); /* Initialize the allocated mountpt state structure. The filesystem is * responsible for one reference ont the blkdriver inode and does not @@ -1184,7 +1163,7 @@ static int rpmsgfs_unbind(FAR void *handle, FAR struct inode **blkdriver, /* Check if there are sill any files opened on the filesystem. */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1194,7 +1173,7 @@ static int rpmsgfs_unbind(FAR void *handle, FAR struct inode **blkdriver, { /* We cannot unmount now.. there are open files */ - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); /* This implementation currently only supports unmounting if there are * no open file references. @@ -1204,13 +1183,13 @@ static int rpmsgfs_unbind(FAR void *handle, FAR struct inode **blkdriver, } ret = rpmsgfs_client_unbind(fs->handle); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); if (ret < 0) { return ret; } - nxsem_destroy(&fs->fs_sem); + nxmutex_destroy(&fs->fs_lock); kmm_free(fs); return 0; } @@ -1235,7 +1214,7 @@ static int rpmsgfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) fs = mountpt->i_private; - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1244,7 +1223,7 @@ static int rpmsgfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) if (fs->statfs.f_type == RPMSGFS_MAGIC) { memcpy(buf, &fs->statfs, sizeof(struct statfs)); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return 0; } @@ -1256,7 +1235,7 @@ static int rpmsgfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) memcpy(&fs->statfs, buf, sizeof(struct statfs)); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1281,7 +1260,7 @@ static int rpmsgfs_unlink(FAR struct inode *mountpt, FAR const char *relpath) fs = mountpt->i_private; - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1295,7 +1274,7 @@ static int rpmsgfs_unlink(FAR struct inode *mountpt, FAR const char *relpath) ret = rpmsgfs_client_unlink(fs->handle, path); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1321,7 +1300,7 @@ static int rpmsgfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, fs = mountpt->i_private; - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1335,7 +1314,7 @@ static int rpmsgfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath, ret = rpmsgfs_client_mkdir(fs->handle, path, mode); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1360,9 +1339,9 @@ int rpmsgfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath) fs = mountpt->i_private; - /* Take the semaphore */ + /* Take the lock */ - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1376,7 +1355,7 @@ int rpmsgfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath) ret = rpmsgfs_client_rmdir(fs->handle, path); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1403,7 +1382,7 @@ int rpmsgfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, fs = mountpt->i_private; - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1420,7 +1399,7 @@ int rpmsgfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath, ret = rpmsgfs_client_rename(fs->handle, oldpath, newpath); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1446,7 +1425,7 @@ static int rpmsgfs_stat(FAR struct inode *mountpt, FAR const char *relpath, fs = mountpt->i_private; - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1460,7 +1439,7 @@ static int rpmsgfs_stat(FAR struct inode *mountpt, FAR const char *relpath, ret = rpmsgfs_client_stat(fs->handle, path, buf); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } @@ -1486,7 +1465,7 @@ static int rpmsgfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, fs = mountpt->i_private; - ret = rpmsgfs_semtake(fs); + ret = nxmutex_lock(&fs->fs_lock); if (ret < 0) { return ret; @@ -1500,6 +1479,6 @@ static int rpmsgfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, ret = rpmsgfs_client_chstat(fs->handle, path, buf, flags); - rpmsgfs_semgive(fs); + nxmutex_unlock(&fs->fs_lock); return ret; } diff --git a/fs/rpmsgfs/rpmsgfs_server.c b/fs/rpmsgfs/rpmsgfs_server.c index 2b7387173521e..4385a29034266 100644 --- a/fs/rpmsgfs/rpmsgfs_server.c +++ b/fs/rpmsgfs/rpmsgfs_server.c @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -48,7 +49,7 @@ struct rpmsgfs_server_s FAR void **dirs; int file_rows; int dir_nums; - sem_t sem; + mutex_t lock; }; /**************************************************************************** @@ -175,7 +176,7 @@ static int rpmsgfs_attach_file(FAR struct rpmsgfs_server_s *priv, int i; int j; - nxsem_wait(&priv->sem); + nxmutex_lock(&priv->lock); for (i = 0; i < priv->file_rows; i++) { @@ -215,7 +216,7 @@ static int rpmsgfs_attach_file(FAR struct rpmsgfs_server_s *priv, ret = i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; out: - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); return ret; } @@ -229,12 +230,12 @@ static int rpmsgfs_detach_file(FAR struct rpmsgfs_server_s *priv, return -EBADF; } - nxsem_wait(&priv->sem); + nxmutex_lock(&priv->lock); tfilep = &priv->files[fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] [fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]; memcpy(filep, tfilep, sizeof(*filep)); memset(tfilep, 0, sizeof(*tfilep)); - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); return 0; } @@ -250,10 +251,10 @@ static FAR struct file *rpmsgfs_get_file( return NULL; } - nxsem_wait(&priv->sem); + nxmutex_lock(&priv->lock); filep = &priv->files[fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK] [fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]; - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); return filep; } @@ -264,13 +265,13 @@ static int rpmsgfs_attach_dir(FAR struct rpmsgfs_server_s *priv, FAR void **tmp; int i; - nxsem_wait(&priv->sem); + nxmutex_lock(&priv->lock); for (i = 1; i < priv->dir_nums; i++) { if (priv->dirs[i] == NULL) { priv->dirs[i] = dir; - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); return i; } } @@ -280,7 +281,7 @@ static int rpmsgfs_attach_dir(FAR struct rpmsgfs_server_s *priv, DEBUGASSERT(tmp); if (tmp == NULL) { - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); return -ENOMEM; } @@ -288,7 +289,7 @@ static int rpmsgfs_attach_dir(FAR struct rpmsgfs_server_s *priv, priv->dir_nums += CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; priv->dirs[i] = dir; - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); return i; } @@ -299,10 +300,10 @@ static void *rpmsgfs_detach_dir(FAR struct rpmsgfs_server_s *priv, if (fd >= 1 && fd < priv->dir_nums) { - nxsem_wait(&priv->sem); + nxmutex_lock(&priv->lock); dir = priv->dirs[fd]; priv->dirs[fd] = NULL; - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); } return dir; @@ -316,9 +317,9 @@ static FAR void *rpmsgfs_get_dir( if (fd >= 1 && fd < priv->dir_nums) { - nxsem_wait(&priv->sem); + nxmutex_lock(&priv->lock); dir = priv->dirs[fd]; - nxsem_post(&priv->sem); + nxmutex_unlock(&priv->lock); } return dir; @@ -849,14 +850,14 @@ static void rpmsgfs_ns_bind(FAR struct rpmsg_device *rdev, } priv->ept.priv = priv; - nxsem_init(&priv->sem, 0, 1); + nxmutex_init(&priv->lock); ret = rpmsg_create_ept(&priv->ept, rdev, name, RPMSG_ADDR_ANY, dest, rpmsgfs_ept_cb, rpmsgfs_ns_unbind); if (ret) { - nxsem_destroy(&priv->sem); + nxmutex_destroy(&priv->lock); kmm_free(priv); } } @@ -889,7 +890,7 @@ static void rpmsgfs_ns_unbind(FAR struct rpmsg_endpoint *ept) } rpmsg_destroy_ept(&priv->ept); - nxsem_destroy(&priv->sem); + nxmutex_destroy(&priv->lock); kmm_free(priv->files); kmm_free(priv->dirs); diff --git a/fs/semaphore/sem_close.c b/fs/semaphore/sem_close.c index 8e5176c701d07..122236b941558 100644 --- a/fs/semaphore/sem_close.c +++ b/fs/semaphore/sem_close.c @@ -84,7 +84,7 @@ int sem_close(FAR sem_t *sem) do { - ret = inode_semtake(); + ret = inode_lock(); /* The only error that is expected is due to thread cancellation. * At this point, we must continue to free the semaphore anyway. @@ -115,14 +115,14 @@ int sem_close(FAR sem_t *sem) * unlinked, then the peer pointer should be NULL. */ - inode_semgive(); + inode_unlock(); DEBUGASSERT(inode->i_peer == NULL); inode_free(inode); return OK; } - inode_semgive(); + inode_unlock(); return OK; } diff --git a/fs/semaphore/sem_open.c b/fs/semaphore/sem_open.c index e41b24d219d20..7ba1a7cb9fd7c 100644 --- a/fs/semaphore/sem_open.c +++ b/fs/semaphore/sem_open.c @@ -192,7 +192,7 @@ FAR sem_t *sem_open(FAR const char *name, int oflags, ...) * inode will be created with a reference count of zero. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { errcode = -ret; @@ -200,7 +200,7 @@ FAR sem_t *sem_open(FAR const char *name, int oflags, ...) } ret = inode_reserve(fullpath, mode, &inode); - inode_semgive(); + inode_unlock(); if (ret < 0) { diff --git a/fs/semaphore/sem_unlink.c b/fs/semaphore/sem_unlink.c index c4703a62bb706..1d1314136d088 100644 --- a/fs/semaphore/sem_unlink.c +++ b/fs/semaphore/sem_unlink.c @@ -105,7 +105,7 @@ int sem_unlink(FAR const char *name) * functioning as a directory and the directory is not empty. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { errcode = -ret; @@ -115,7 +115,7 @@ int sem_unlink(FAR const char *name) if (inode->i_child != NULL) { errcode = ENOTEMPTY; - goto errout_with_semaphore; + goto errout_with_lock; } /* Remove the old inode from the tree. Because we hold a reference count @@ -140,14 +140,14 @@ int sem_unlink(FAR const char *name) * reference, that can only occur if the semaphore is not in-use. */ - inode_semgive(); + inode_unlock(); ret = sem_close((FAR sem_t *)inode->u.i_nsem); RELEASE_SEARCH(&desc); sched_unlock(); return ret; -errout_with_semaphore: - inode_semgive(); +errout_with_lock: + inode_unlock(); errout_with_inode: inode_release(inode); diff --git a/fs/smartfs/smartfs.h b/fs/smartfs/smartfs.h index 0e66875c7db8b..ea773514c5e46 100644 --- a/fs/smartfs/smartfs.h +++ b/fs/smartfs/smartfs.h @@ -33,7 +33,7 @@ #include #include -#include +#include /**************************************************************************** * Pre-processor Definitions @@ -321,7 +321,6 @@ struct smartfs_mountpt_s struct smartfs_mountpt_s *fs_next; /* Pointer to next SMART filesystem */ #endif FAR struct inode *fs_blkdriver; /* Our underlying block device */ - sem_t *fs_sem; /* Used to assure thread-safe access */ FAR struct smartfs_ofile_s *fs_head; /* A singly-linked list of open files */ bool fs_mounted; /* true: The file system is ready */ struct smart_format_s fs_llformat; /* Low level device format info */ @@ -338,11 +337,6 @@ struct smartfs_mountpt_s * Public Functions Prototypes ****************************************************************************/ -/* Semaphore access for internal use */ - -int smartfs_semtake(struct smartfs_mountpt_s *fs); -void smartfs_semgive(struct smartfs_mountpt_s *fs); - /* Forward references for utility functions */ struct smartfs_mountpt_s; diff --git a/fs/smartfs/smartfs_smart.c b/fs/smartfs/smartfs_smart.c index 9b48562a0a77f..de9b6c4a7f0be 100644 --- a/fs/smartfs/smartfs_smart.c +++ b/fs/smartfs/smartfs_smart.c @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -120,8 +121,7 @@ static int smartfs_stat(FAR struct inode *mountpt, * Private Data ****************************************************************************/ -static uint8_t g_seminitialized = FALSE; -static sem_t g_sem; +static mutex_t g_lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Public Data @@ -198,9 +198,9 @@ static int smartfs_open(FAR struct file *filep, const char *relpath, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -212,7 +212,7 @@ static int smartfs_open(FAR struct file *filep, const char *relpath, if (sf == NULL) { ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Allocate a sector buffer if CRC enabled in the MTD layer */ @@ -225,7 +225,7 @@ static int smartfs_open(FAR struct file *filep, const char *relpath, kmm_free(sf); ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } sf->bflags = 0; @@ -377,7 +377,7 @@ static int smartfs_open(FAR struct file *filep, const char *relpath, fs->fs_head = sf; ret = OK; - goto errout_with_semaphore; + goto errout_with_lock; errout_with_buffer: if (sf->entry.name != NULL) @@ -393,8 +393,8 @@ static int smartfs_open(FAR struct file *filep, const char *relpath, #endif /* CONFIG_SMARTFS_USE_SECTOR_BUFFER */ kmm_free(sf); -errout_with_semaphore: - smartfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&g_lock); if (ret == -EINVAL) { ret = -EIO; @@ -430,9 +430,9 @@ static int smartfs_close(FAR struct file *filep) smartfs_sync(filep); - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -503,7 +503,7 @@ static int smartfs_close(FAR struct file *filep) kmm_free(sf); okout: - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; } @@ -536,9 +536,9 @@ static ssize_t smartfs_read(FAR struct file *filep, char *buffer, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -569,7 +569,7 @@ static ssize_t smartfs_read(FAR struct file *filep, char *buffer, { ferr("ERROR: Error %d reading sector %d data\n", ret, sf->currsector); - goto errout_with_semaphore; + goto errout_with_lock; } /* Point header to the read data to get used byte count */ @@ -635,8 +635,8 @@ static ssize_t smartfs_read(FAR struct file *filep, char *buffer, ret = bytesread; -errout_with_semaphore: - smartfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&g_lock); return ret; } @@ -665,9 +665,9 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (ssize_t)ret; @@ -680,7 +680,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, if ((sf->oflags & O_WROK) == 0) { ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Test if we opened for APPEND mode. If we did, then seek to the @@ -693,7 +693,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, if (ret < 0) { ret = -EIO; - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -742,7 +742,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, { ferr("ERROR: Error %d writing sector %d data\n", ret, sf->currsector); - goto errout_with_semaphore; + goto errout_with_lock; } /* Update our control variables */ @@ -770,7 +770,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, { ferr("ERROR: Error %d reading sector %d header\n", ret, sf->currsector); - goto errout_with_semaphore; + goto errout_with_lock; } /* Now get the chained sector info and reset the offset */ @@ -820,7 +820,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, { ferr("ERROR: Error %d writing sector %d data\n", ret, sf->currsector); - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -846,7 +846,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, if (ret < 0) { ferr("ERROR: Error %d allocating new sector\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Copy the new sector to the old one and chain it */ @@ -859,7 +859,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, ret = smartfs_sync_internal(fs, sf); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Record the new sector in our tracking variables and @@ -889,7 +889,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, ret = smartfs_sync_internal(fs, sf); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Allocate a new sector if needed */ @@ -902,7 +902,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, if (ret < 0) { ferr("ERROR: Error %d allocating new sector\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Copy the new sector to the old one and chain it */ @@ -917,7 +917,7 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, if (ret < 0) { ferr("ERROR: Error %d writing next sector\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Record the new sector in our tracking variables and @@ -941,8 +941,8 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, ret = byteswritten; -errout_with_semaphore: - smartfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&g_lock); return ret; } @@ -969,9 +969,9 @@ static off_t smartfs_seek(FAR struct file *filep, off_t offset, int whence) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return (off_t)ret; @@ -985,7 +985,7 @@ static off_t smartfs_seek(FAR struct file *filep, off_t offset, int whence) filep->f_pos = ret; } - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1027,9 +1027,9 @@ static int smartfs_sync(FAR struct file *filep) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1037,7 +1037,7 @@ static int smartfs_sync(FAR struct file *filep) ret = smartfs_sync_internal(fs, sf); - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1101,9 +1101,9 @@ static int smartfs_fstat(FAR const struct file *filep, FAR struct stat *buf) fs = inode->i_private; DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1112,7 +1112,7 @@ static int smartfs_fstat(FAR const struct file *filep, FAR struct stat *buf) /* Return information about the directory entry in the stat structure */ smartfs_stat_common(fs, &sf->entry, buf); - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; } @@ -1143,9 +1143,9 @@ static int smartfs_truncate(FAR struct file *filep, off_t length) DEBUGASSERT(fs != NULL); - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1158,7 +1158,7 @@ static int smartfs_truncate(FAR struct file *filep, off_t length) if ((sf->oflags & O_WROK) == 0) { ret = -EACCES; - goto errout_with_semaphore; + goto errout_with_lock; } /* Are we shrinking the file? Or extending it? */ @@ -1186,11 +1186,11 @@ static int smartfs_truncate(FAR struct file *filep, off_t length) ret = smartfs_extendfile(fs, sf, length); } -errout_with_semaphore: +errout_with_lock: /* Relinquish exclusive access */ - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1225,9 +1225,9 @@ static int smartfs_opendir(FAR struct inode *mountpt, return -ENOMEM; } - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { goto errout_with_sdir; @@ -1240,7 +1240,7 @@ static int smartfs_opendir(FAR struct inode *mountpt, &filename); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Populate our private data in the fs_dirent_s struct */ @@ -1250,10 +1250,10 @@ static int smartfs_opendir(FAR struct inode *mountpt, sdir->fs_curroffset = sizeof(struct smartfs_chain_header_s); *dir = &sdir->fs_base; - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; -errout_with_semaphore: +errout_with_lock: /* If space for the entry name was allocated, then free it */ @@ -1263,7 +1263,7 @@ static int smartfs_opendir(FAR struct inode *mountpt, entry.name = NULL; } - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); errout_with_sdir: kmm_free(sdir); @@ -1313,9 +1313,9 @@ static int smartfs_readdir(FAR struct inode *mountpt, fs = mountpt->i_private; sdir = (FAR struct smartfs_dir_s *)dir; - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1337,7 +1337,7 @@ static int smartfs_readdir(FAR struct inode *mountpt, ret = FS_IOCTL(fs, BIOC_READSECT, (unsigned long) &readwrite); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Now search for entries, starting at curroffset */ @@ -1413,7 +1413,7 @@ static int smartfs_readdir(FAR struct inode *mountpt, /* Now exit */ ret = OK; - goto errout_with_semaphore; + goto errout_with_lock; } /* No more entries in this sector. Move on to next sector and @@ -1430,8 +1430,8 @@ static int smartfs_readdir(FAR struct inode *mountpt, ret = -ENOENT; -errout_with_semaphore: - smartfs_semgive(fs); +errout_with_lock: + nxmutex_unlock(&g_lock); return ret; } @@ -1497,26 +1497,11 @@ static int smartfs_bind(FAR struct inode *blkdriver, const void *data, return -ENOMEM; } - /* If the global semaphore hasn't been initialized, then - * initialized it now. - */ - - fs->fs_sem = &g_sem; - if (!g_seminitialized) - { - nxsem_init(&g_sem, 0, 0); /* Initialize the semaphore that controls access */ - g_seminitialized = TRUE; - } - else + ret = nxmutex_lock(&g_lock); + if (ret < 0) { - /* Take the semaphore for the mount */ - - ret = smartfs_semtake(fs); - if (ret < 0) - { - kmm_free(fs); - return ret; - } + kmm_free(fs); + return ret; } /* Initialize the allocated mountpt state structure. The filesystem is @@ -1532,13 +1517,13 @@ static int smartfs_bind(FAR struct inode *blkdriver, const void *data, ret = smartfs_mount(fs, true); if (ret != 0) { - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); kmm_free(fs); return ret; } *handle = (FAR void *)fs; - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return OK; } @@ -1565,7 +1550,7 @@ static int smartfs_unbind(FAR void *handle, FAR struct inode **blkdriver, ret = OK; /* Assume success */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1575,7 +1560,7 @@ static int smartfs_unbind(FAR void *handle, FAR struct inode **blkdriver, { /* We cannot unmount now.. there are open files */ - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); /* This implementation currently only supports unmounting if there are * no open file references. @@ -1590,7 +1575,7 @@ static int smartfs_unbind(FAR void *handle, FAR struct inode **blkdriver, ret = smartfs_unmount(fs); } - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); kmm_free(fs); return ret; } @@ -1615,7 +1600,7 @@ static int smartfs_statfs(struct inode *mountpt, struct statfs *buf) fs = mountpt->i_private; - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1643,7 +1628,7 @@ static int smartfs_statfs(struct inode *mountpt, struct statfs *buf) buf->f_files = 0; buf->f_ffree = fs->fs_llformat.nfreesectors; - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1670,7 +1655,7 @@ static int smartfs_unlink(struct inode *mountpt, const char *relpath) fs = mountpt->i_private; - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1689,7 +1674,7 @@ static int smartfs_unlink(struct inode *mountpt, const char *relpath) if ((entry.flags & SMARTFS_DIRENT_TYPE) == SMARTFS_DIRENT_TYPE_DIR) { ret = -EISDIR; - goto errout_with_semaphore; + goto errout_with_lock; } /* TODO: Need to check permissions? */ @@ -1704,18 +1689,18 @@ static int smartfs_unlink(struct inode *mountpt, const char *relpath) { /* Just report the error */ - goto errout_with_semaphore; + goto errout_with_lock; } ret = OK; -errout_with_semaphore: +errout_with_lock: if (entry.name != NULL) { kmm_free(entry.name); } - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1743,7 +1728,7 @@ static int smartfs_mkdir(struct inode *mountpt, const char *relpath, fs = mountpt->i_private; - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1765,7 +1750,7 @@ static int smartfs_mkdir(struct inode *mountpt, const char *relpath, /* The name exists -- can't create */ ret = -EEXIST; - goto errout_with_semaphore; + goto errout_with_lock; } else if (ret == -ENOENT) { @@ -1777,7 +1762,7 @@ static int smartfs_mkdir(struct inode *mountpt, const char *relpath, { /* Invalid entry in the path (non-existent dir segment) */ - goto errout_with_semaphore; + goto errout_with_lock; } /* Check mode */ @@ -1788,13 +1773,13 @@ static int smartfs_mkdir(struct inode *mountpt, const char *relpath, SMARTFS_DIRENT_TYPE_DIR, mode, &entry, 0xffff, NULL); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } ret = OK; } -errout_with_semaphore: +errout_with_lock: if (entry.name != NULL) { /* Free the filename space allocation */ @@ -1803,7 +1788,7 @@ static int smartfs_mkdir(struct inode *mountpt, const char *relpath, entry.name = NULL; } - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1830,9 +1815,9 @@ int smartfs_rmdir(struct inode *mountpt, const char *relpath) fs = mountpt->i_private; - /* Take the semaphore */ + /* Take the lock */ - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1851,7 +1836,7 @@ int smartfs_rmdir(struct inode *mountpt, const char *relpath) if ((entry.flags & SMARTFS_DIRENT_TYPE) == SMARTFS_DIRENT_TYPE_FILE) { ret = -ENOTDIR; - goto errout_with_semaphore; + goto errout_with_lock; } /* TODO: Need to check permissions? */ @@ -1861,7 +1846,7 @@ int smartfs_rmdir(struct inode *mountpt, const char *relpath) ret = smartfs_countdirentries(fs, &entry); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Only continue if there are zero entries in the directory */ @@ -1869,7 +1854,7 @@ int smartfs_rmdir(struct inode *mountpt, const char *relpath) if (ret != 0) { ret = -ENOTEMPTY; - goto errout_with_semaphore; + goto errout_with_lock; } /* Okay, we are clear to delete the directory. Use the deleteentry @@ -1879,25 +1864,25 @@ int smartfs_rmdir(struct inode *mountpt, const char *relpath) ret = smartfs_deleteentry(fs, &entry); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } } else { /* Just report the error */ - goto errout_with_semaphore; + goto errout_with_lock; } ret = OK; -errout_with_semaphore: +errout_with_lock: if (entry.name != NULL) { kmm_free(entry.name); } - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -1932,7 +1917,7 @@ int smartfs_rename(struct inode *mountpt, const char *oldrelpath, fs = mountpt->i_private; - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -1946,7 +1931,7 @@ int smartfs_rename(struct inode *mountpt, const char *oldrelpath, &oldfilename); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Search for the new entry and validate it DOESN'T exist. */ @@ -1961,7 +1946,7 @@ int smartfs_rename(struct inode *mountpt, const char *oldrelpath, */ ret = -EEXIST; - goto errout_with_semaphore; + goto errout_with_lock; } /* Test if the new parent directory is valid */ @@ -1977,7 +1962,7 @@ int smartfs_rename(struct inode *mountpt, const char *oldrelpath, NULL); if (ret != OK) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Now mark the old entry as inactive */ @@ -1991,7 +1976,7 @@ int smartfs_rename(struct inode *mountpt, const char *oldrelpath, { ferr("ERROR: Error %d reading sector %d data\n", ret, oldentry.dsector); - goto errout_with_semaphore; + goto errout_with_lock; } direntry = (struct smartfs_entry_header_s *) @@ -2012,7 +1997,7 @@ int smartfs_rename(struct inode *mountpt, const char *oldrelpath, { ferr("ERROR: Error %d writing flag bytes for sector %d\n", ret, readwrite.logsector); - goto errout_with_semaphore; + goto errout_with_lock; } } else @@ -2020,12 +2005,12 @@ int smartfs_rename(struct inode *mountpt, const char *oldrelpath, /* Trying to create in a directory that doesn't exist */ ret = -ENOENT; - goto errout_with_semaphore; + goto errout_with_lock; } ret = OK; -errout_with_semaphore: +errout_with_lock: if (oldentry.name != NULL) { kmm_free(oldentry.name); @@ -2038,7 +2023,7 @@ int smartfs_rename(struct inode *mountpt, const char *oldrelpath, newentry.name = NULL; } - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } @@ -2113,7 +2098,7 @@ static int smartfs_stat(FAR struct inode *mountpt, FAR const char *relpath, fs = mountpt->i_private; - ret = smartfs_semtake(fs); + ret = nxmutex_lock(&g_lock); if (ret < 0) { return ret; @@ -2126,7 +2111,7 @@ static int smartfs_stat(FAR struct inode *mountpt, FAR const char *relpath, &filename); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Return information about the directory entry in the stat structure */ @@ -2134,14 +2119,14 @@ static int smartfs_stat(FAR struct inode *mountpt, FAR const char *relpath, smartfs_stat_common(fs, &entry, buf); ret = OK; -errout_with_semaphore: +errout_with_lock: if (entry.name != NULL) { kmm_free(entry.name); entry.name = NULL; } - smartfs_semgive(fs); + nxmutex_unlock(&g_lock); return ret; } diff --git a/fs/smartfs/smartfs_utils.c b/fs/smartfs/smartfs_utils.c index 24267ca65f288..00e7f103c764f 100644 --- a/fs/smartfs/smartfs_utils.c +++ b/fs/smartfs/smartfs_utils.c @@ -59,24 +59,6 @@ static struct smartfs_mountpt_s *g_mounthead = NULL; * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: smartfs_semtake - ****************************************************************************/ - -int smartfs_semtake(struct smartfs_mountpt_s *fs) -{ - return nxsem_wait_uninterruptible(fs->fs_sem); -} - -/**************************************************************************** - * Name: smartfs_semgive - ****************************************************************************/ - -void smartfs_semgive(struct smartfs_mountpt_s *fs) -{ - nxsem_post(fs->fs_sem); -} - /**************************************************************************** * Name: smartfs_rdle16 * diff --git a/fs/spiffs/src/spiffs_vfs.c b/fs/spiffs/src/spiffs_vfs.c index 305dc3159b24d..369d557a4c56d 100644 --- a/fs/spiffs/src/spiffs_vfs.c +++ b/fs/spiffs/src/spiffs_vfs.c @@ -1558,7 +1558,7 @@ static int spiffs_unbind(FAR void *handle, FAR struct inode **mtdinode, kmm_free(fs->cache); } - /* Free the volume memory (note that the semaphore is now stale!) */ + /* Free the volume memory (note that the mutex is now stale!) */ nxrmutex_destroy(&fs->lock); kmm_free(fs); diff --git a/fs/unionfs/fs_unionfs.c b/fs/unionfs/fs_unionfs.c index 7d3e22019b641..657becdcaf6e5 100644 --- a/fs/unionfs/fs_unionfs.c +++ b/fs/unionfs/fs_unionfs.c @@ -43,7 +43,7 @@ #include #include #include -#include +#include #include "inode/inode.h" @@ -85,7 +85,7 @@ struct unionfs_mountpt_s struct unionfs_inode_s { struct unionfs_mountpt_s ui_fs[2]; /* Contained file systems */ - sem_t ui_exclsem; /* Enforces mutually exclusive access */ + mutex_t ui_lock; /* Enforces mutually exclusive access */ int16_t ui_nopen; /* Number of open references */ bool ui_unmounted; /* File system has been unmounted */ }; @@ -104,9 +104,6 @@ struct unionfs_file_s /* Helper functions */ -static int unionfs_semtake(FAR struct unionfs_inode_s *ui, bool noint); -#define unionfs_semgive(ui) nxsem_post(&(ui)->ui_exclsem) - static FAR const char *unionfs_offsetpath(FAR const char *relpath, FAR const char *prefix); static bool unionfs_ispartprefix(FAR const char *partprefix, @@ -253,22 +250,6 @@ const struct mountpt_operations unionfs_operations = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: unionfs_semtake - ****************************************************************************/ - -static int unionfs_semtake(FAR struct unionfs_inode_s *ui, bool noint) -{ - if (noint) - { - return nxsem_wait_uninterruptible(&ui->ui_exclsem); - } - else - { - return nxsem_wait(&ui->ui_exclsem); - } -} - /**************************************************************************** * Name: unionfs_offsetpath ****************************************************************************/ @@ -861,7 +842,7 @@ static void unionfs_destroy(FAR struct unionfs_inode_s *ui) /* And finally free the allocated unionfs state structure as well */ - nxsem_destroy(&ui->ui_exclsem); + nxmutex_destroy(&ui->ui_lock); kmm_free(ui); } @@ -886,7 +867,7 @@ static int unionfs_open(FAR struct file *filep, FAR const char *relpath, /* Get exclusive access to the file system data structures */ - ret = unionfs_semtake(ui, false); + ret = nxmutex_lock(&ui->ui_lock); if (ret < 0) { return ret; @@ -899,7 +880,7 @@ static int unionfs_open(FAR struct file *filep, FAR const char *relpath, if (uf == NULL) { ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Try to open the file on file system 1 */ @@ -935,7 +916,7 @@ static int unionfs_open(FAR struct file *filep, FAR const char *relpath, mode); if (ret < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Successfully opened on file system 1 */ @@ -953,8 +934,8 @@ static int unionfs_open(FAR struct file *filep, FAR const char *relpath, filep->f_priv = (FAR void *)uf; ret = OK; -errout_with_semaphore: - unionfs_semgive(ui); +errout_with_lock: + nxmutex_unlock(&ui->ui_lock); return ret; } @@ -977,7 +958,7 @@ static int unionfs_close(FAR struct file *filep) /* Get exclusive access to the file system data structures */ - ret = unionfs_semtake(ui, false); + ret = nxmutex_lock(&ui->ui_lock); if (ret < 0) { return ret; @@ -1013,7 +994,7 @@ static int unionfs_close(FAR struct file *filep) } else { - unionfs_semgive(ui); + nxmutex_unlock(&ui->ui_lock); } /* Free the open file container */ @@ -1131,7 +1112,7 @@ static off_t unionfs_seek(FAR struct file *filep, off_t offset, int whence) /* Get exclusive access to the file system data structures */ - ret = unionfs_semtake(ui, false); + ret = nxmutex_lock(&ui->ui_lock); if (ret < 0) { return ret; @@ -1164,7 +1145,7 @@ static off_t unionfs_seek(FAR struct file *filep, off_t offset, int whence) break; } - unionfs_semgive(ui); + nxmutex_unlock(&ui->ui_lock); } return offset; @@ -1447,7 +1428,7 @@ static int unionfs_opendir(FAR struct inode *mountpt, /* Get exclusive access to the file system data structures */ - ret = unionfs_semtake(ui, false); + ret = nxmutex_lock(&ui->ui_lock); if (ret < 0) { goto errout_with_udir; @@ -1464,7 +1445,7 @@ static int unionfs_opendir(FAR struct inode *mountpt, udir->fu_relpath = strdup(relpath); if (!udir->fu_relpath) { - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -1540,7 +1521,7 @@ static int unionfs_opendir(FAR struct inode *mountpt, ui->ui_nopen++; DEBUGASSERT(ui->ui_nopen > 0); - unionfs_semgive(ui); + nxmutex_unlock(&ui->ui_lock); *dir = &udir->fu_base; return OK; @@ -1550,8 +1531,8 @@ static int unionfs_opendir(FAR struct inode *mountpt, kmm_free(udir->fu_relpath); } -errout_with_semaphore: - unionfs_semgive(ui); +errout_with_lock: + nxmutex_unlock(&ui->ui_lock); errout_with_udir: kmm_free(udir); @@ -1581,7 +1562,7 @@ static int unionfs_closedir(FAR struct inode *mountpt, /* Get exclusive access to the file system data structures */ - ret = unionfs_semtake(ui, true); + ret = nxmutex_lock(&ui->ui_lock); if (ret < 0) { return ret; @@ -1636,7 +1617,7 @@ static int unionfs_closedir(FAR struct inode *mountpt, } else { - unionfs_semgive(ui); + nxmutex_unlock(&ui->ui_lock); } return ret; @@ -2026,7 +2007,7 @@ static int unionfs_unbind(FAR void *handle, FAR struct inode **blkdriver, /* Get exclusive access to the file system data structures */ - ret = unionfs_semtake(ui, true); + ret = nxmutex_lock(&ui->ui_lock); if (ret < 0) { return ret; @@ -2042,12 +2023,12 @@ static int unionfs_unbind(FAR void *handle, FAR struct inode **blkdriver, if (ui->ui_nopen <= 0) { - unionfs_semgive(ui); + nxmutex_unlock(&ui->ui_lock); unionfs_destroy(ui); } else { - unionfs_semgive(ui); + nxmutex_unlock(&ui->ui_lock); } return OK; @@ -2630,7 +2611,7 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1, return -ENOMEM; } - nxsem_init(&ui->ui_exclsem, 0, 1); + nxmutex_init(&ui->ui_lock); /* Get the inodes associated with fspath1 and fspath2 */ @@ -2698,7 +2679,7 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1, inode_release(ui->ui_fs[0].um_node); errout_with_uinode: - nxsem_destroy(&ui->ui_exclsem); + nxmutex_lock(&ui->ui_lock); kmm_free(ui); return ret; } diff --git a/fs/userfs/fs_userfs.c b/fs/userfs/fs_userfs.c index f4efa49aaf98a..063c3aec3067a 100644 --- a/fs/userfs/fs_userfs.c +++ b/fs/userfs/fs_userfs.c @@ -45,7 +45,7 @@ #include #include #include -#include +#include /**************************************************************************** * Pre-processor Definitions @@ -75,7 +75,7 @@ struct userfs_state_s struct socket psock; /* Client socket instance */ struct sockaddr_in server; /* Server address */ - sem_t exclsem; /* Exclusive access for request-response sequence */ + mutex_t lock; /* Exclusive access for request-response sequence */ /* I/O Buffer (actual size depends on USERFS_REQ_MAXSIZE and the configured * mxwrite). @@ -221,7 +221,7 @@ static int userfs_open(FAR struct file *filep, FAR const char *relpath, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -243,7 +243,7 @@ static int userfs_open(FAR struct file *filep, FAR const char *relpath, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -251,7 +251,7 @@ static int userfs_open(FAR struct file *filep, FAR const char *relpath, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -298,7 +298,7 @@ static int userfs_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -317,7 +317,7 @@ static int userfs_close(FAR struct file *filep) if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -325,7 +325,7 @@ static int userfs_close(FAR struct file *filep) nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -378,7 +378,7 @@ static ssize_t userfs_read(FAR struct file *filep, char *buffer, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -398,7 +398,7 @@ static ssize_t userfs_read(FAR struct file *filep, char *buffer, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -406,7 +406,7 @@ static ssize_t userfs_read(FAR struct file *filep, char *buffer, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -478,7 +478,7 @@ static ssize_t userfs_write(FAR struct file *filep, FAR const char *buffer, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -499,7 +499,7 @@ static ssize_t userfs_write(FAR struct file *filep, FAR const char *buffer, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -507,7 +507,7 @@ static ssize_t userfs_write(FAR struct file *filep, FAR const char *buffer, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -553,7 +553,7 @@ static off_t userfs_seek(FAR struct file *filep, off_t offset, int whence) /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -574,7 +574,7 @@ static off_t userfs_seek(FAR struct file *filep, off_t offset, int whence) if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -582,7 +582,7 @@ static off_t userfs_seek(FAR struct file *filep, off_t offset, int whence) nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -628,7 +628,7 @@ static int userfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -649,7 +649,7 @@ static int userfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -657,7 +657,7 @@ static int userfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg) nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -701,7 +701,7 @@ static int userfs_sync(FAR struct file *filep) /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -720,7 +720,7 @@ static int userfs_sync(FAR struct file *filep) if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -728,7 +728,7 @@ static int userfs_sync(FAR struct file *filep) nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -778,7 +778,7 @@ static int userfs_dup(FAR const struct file *oldp, FAR struct file *newp) /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -797,7 +797,7 @@ static int userfs_dup(FAR const struct file *oldp, FAR struct file *newp) if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -805,7 +805,7 @@ static int userfs_dup(FAR const struct file *oldp, FAR struct file *newp) nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -855,7 +855,7 @@ static int userfs_fstat(FAR const struct file *filep, FAR struct stat *buf) /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -874,7 +874,7 @@ static int userfs_fstat(FAR const struct file *filep, FAR struct stat *buf) if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -882,7 +882,7 @@ static int userfs_fstat(FAR const struct file *filep, FAR struct stat *buf) nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -936,7 +936,7 @@ static int userfs_fchstat(FAR const struct file *filep, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -957,7 +957,7 @@ static int userfs_fchstat(FAR const struct file *filep, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %zd\n", nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -965,7 +965,7 @@ static int userfs_fchstat(FAR const struct file *filep, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1013,7 +1013,7 @@ static int userfs_truncate(FAR struct file *filep, off_t length) /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1033,7 +1033,7 @@ static int userfs_truncate(FAR struct file *filep, off_t length) if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1041,7 +1041,7 @@ static int userfs_truncate(FAR struct file *filep, off_t length) nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1104,7 +1104,7 @@ static int userfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1124,7 +1124,7 @@ static int userfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1132,7 +1132,7 @@ static int userfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1193,7 +1193,7 @@ static int userfs_closedir(FAR struct inode *mountpt, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1212,7 +1212,7 @@ static int userfs_closedir(FAR struct inode *mountpt, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1220,7 +1220,7 @@ static int userfs_closedir(FAR struct inode *mountpt, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1271,7 +1271,7 @@ static int userfs_readdir(FAR struct inode *mountpt, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1290,7 +1290,7 @@ static int userfs_readdir(FAR struct inode *mountpt, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1298,7 +1298,7 @@ static int userfs_readdir(FAR struct inode *mountpt, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1351,7 +1351,7 @@ static int userfs_rewinddir(FAR struct inode *mountpt, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1370,7 +1370,7 @@ static int userfs_rewinddir(FAR struct inode *mountpt, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1378,7 +1378,7 @@ static int userfs_rewinddir(FAR struct inode *mountpt, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1435,11 +1435,11 @@ static int userfs_bind(FAR struct inode *blkdriver, FAR const void *data, return -ENOMEM; } - /* Initialize the semaphore that assures mutually exclusive access through + /* Initialize the mutex that assures mutually exclusive access through * the entire request-response sequence. */ - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); /* Copy the configuration data into the allocated structure. Why? First * we can't be certain of the life time of the memory underlying the config @@ -1513,7 +1513,7 @@ static int userfs_unbind(FAR void *handle, FAR struct inode **blkdriver, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1531,7 +1531,7 @@ static int userfs_unbind(FAR void *handle, FAR struct inode **blkdriver, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1539,7 +1539,7 @@ static int userfs_unbind(FAR void *handle, FAR struct inode **blkdriver, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1597,7 +1597,7 @@ static int userfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1615,7 +1615,7 @@ static int userfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1623,7 +1623,7 @@ static int userfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1685,7 +1685,7 @@ static int userfs_unlink(FAR struct inode *mountpt, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1705,7 +1705,7 @@ static int userfs_unlink(FAR struct inode *mountpt, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1713,7 +1713,7 @@ static int userfs_unlink(FAR struct inode *mountpt, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1771,7 +1771,7 @@ static int userfs_mkdir(FAR struct inode *mountpt, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1792,7 +1792,7 @@ static int userfs_mkdir(FAR struct inode *mountpt, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1800,7 +1800,7 @@ static int userfs_mkdir(FAR struct inode *mountpt, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1858,7 +1858,7 @@ static int userfs_rmdir(FAR struct inode *mountpt, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1878,7 +1878,7 @@ static int userfs_rmdir(FAR struct inode *mountpt, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1886,7 +1886,7 @@ static int userfs_rmdir(FAR struct inode *mountpt, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -1948,7 +1948,7 @@ static int userfs_rename(FAR struct inode *mountpt, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -1970,7 +1970,7 @@ static int userfs_rename(FAR struct inode *mountpt, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -1978,7 +1978,7 @@ static int userfs_rename(FAR struct inode *mountpt, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -2036,7 +2036,7 @@ static int userfs_stat(FAR struct inode *mountpt, FAR const char *relpath, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2056,7 +2056,7 @@ static int userfs_stat(FAR struct inode *mountpt, FAR const char *relpath, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %d\n", (int)nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -2064,7 +2064,7 @@ static int userfs_stat(FAR struct inode *mountpt, FAR const char *relpath, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { @@ -2126,7 +2126,7 @@ static int userfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, /* Get exclusive access */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -2148,7 +2148,7 @@ static int userfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, if (nsent < 0) { ferr("ERROR: psock_sendto failed: %zd\n", nsent); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return (int)nsent; } @@ -2156,7 +2156,7 @@ static int userfs_chstat(FAR struct inode *mountpt, FAR const char *relpath, nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv), 0, NULL, NULL); - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); if (nrecvd < 0) { diff --git a/fs/vfs/fs_dir.c b/fs/vfs/fs_dir.c index 1397884206eff..ee335e8efc3d5 100644 --- a/fs/vfs/fs_dir.c +++ b/fs/vfs/fs_dir.c @@ -203,7 +203,7 @@ static off_t seek_pseudodir(FAR struct file *filep, off_t offset) * be a very unpredictable operation. */ - inode_semtake(); + inode_lock(); for (; curr != NULL && pos != offset; pos++, curr = curr->i_peer); @@ -221,7 +221,7 @@ static off_t seek_pseudodir(FAR struct file *filep, off_t offset) curr->i_crefs++; } - inode_semgive(); + inode_unlock(); if (prev != NULL) { @@ -372,7 +372,7 @@ static int read_pseudodir(FAR struct fs_dirent_s *dir, /* Now get the inode to visit next time that readdir() is called */ - inode_semtake(); + inode_lock(); prev = pdir->next; pdir->next = prev->i_peer; /* The next node to visit */ @@ -384,7 +384,7 @@ static int read_pseudodir(FAR struct fs_dirent_s *dir, pdir->next->i_crefs++; } - inode_semgive(); + inode_unlock(); if (prev != NULL) { diff --git a/fs/vfs/fs_eventfd.c b/fs/vfs/fs_eventfd.c index ef56686bde6a2..152d81fec822d 100644 --- a/fs/vfs/fs_eventfd.c +++ b/fs/vfs/fs_eventfd.c @@ -30,7 +30,7 @@ #include #include - +#include #include #include @@ -50,7 +50,7 @@ typedef struct eventfd_waiter_sem_s struct eventfd_priv_s { - sem_t exclsem; /* Enforces device exclusive access */ + mutex_t lock; /* Enforces device exclusive access */ eventfd_waiter_sem_t *rdsems; /* List of blocking readers */ eventfd_waiter_sem_t *wrsems; /* List of blocking writers */ eventfd_t counter; /* eventfd counter */ @@ -129,7 +129,8 @@ static FAR struct eventfd_priv_s *eventfd_allocdev(void) { /* Initialize the private structure */ - nxsem_init(&dev->exclsem, 0, 0); + nxmutex_init(&dev->lock); + nxmutex_lock(&dev->lock); } return dev; @@ -137,7 +138,7 @@ static FAR struct eventfd_priv_s *eventfd_allocdev(void) static void eventfd_destroy(FAR struct eventfd_priv_s *dev) { - nxsem_destroy(&dev->exclsem); + nxmutex_destroy(&dev->lock); kmm_free(dev); } @@ -160,7 +161,7 @@ static int eventfd_do_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -182,7 +183,7 @@ static int eventfd_do_open(FAR struct file *filep) ret = OK; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -198,7 +199,7 @@ static int eventfd_do_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -215,7 +216,7 @@ static int eventfd_do_close(FAR struct file *filep) /* Just decrement the reference count and release the semaphore */ priv->crefs -= 1; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -228,7 +229,7 @@ static int eventfd_do_close(FAR struct file *filep) unregister_driver(devpath); - DEBUGASSERT(priv->exclsem.semcount == 0); + DEBUGASSERT(nxmutex_is_locked(&priv->lock)); eventfd_release_minor(priv->minor); eventfd_destroy(priv); @@ -243,7 +244,7 @@ static int eventfd_blocking_io(FAR struct eventfd_priv_s *dev, sem->next = *slist; *slist = sem; - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); /* Wait for eventfd to notify */ @@ -252,10 +253,10 @@ static int eventfd_blocking_io(FAR struct eventfd_priv_s *dev, if (ret < 0) { /* Interrupted wait, unregister semaphore - * TODO ensure that exclsem wait does not fail (ECANCELED) + * TODO ensure that lock wait does not fail (ECANCELED) */ - nxsem_wait_uninterruptible(&dev->exclsem); + nxmutex_lock(&dev->lock); eventfd_waiter_sem_t *cur_sem = *slist; @@ -275,11 +276,11 @@ static int eventfd_blocking_io(FAR struct eventfd_priv_s *dev, } } - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return ret; } - return nxsem_wait(&dev->exclsem); + return nxmutex_lock(&dev->lock); } static ssize_t eventfd_do_read(FAR struct file *filep, FAR char *buffer, @@ -294,7 +295,7 @@ static ssize_t eventfd_do_read(FAR struct file *filep, FAR char *buffer, return -EINVAL; } - ret = nxsem_wait(&dev->exclsem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -306,7 +307,7 @@ static ssize_t eventfd_do_read(FAR struct file *filep, FAR char *buffer, { if (filep->f_oflags & O_NONBLOCK) { - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return -EAGAIN; } @@ -358,7 +359,7 @@ static ssize_t eventfd_do_read(FAR struct file *filep, FAR char *buffer, dev->wrsems = NULL; - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return sizeof(eventfd_t); } @@ -377,7 +378,7 @@ static ssize_t eventfd_do_write(FAR struct file *filep, return -EINVAL; } - ret = nxsem_wait(&dev->exclsem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -391,7 +392,7 @@ static ssize_t eventfd_do_write(FAR struct file *filep, if (filep->f_oflags & O_NONBLOCK) { - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return -EAGAIN; } @@ -435,7 +436,7 @@ static ssize_t eventfd_do_write(FAR struct file *filep, dev->rdsems = NULL; - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return sizeof(eventfd_t); } @@ -449,7 +450,7 @@ static int eventfd_do_poll(FAR struct file *filep, FAR struct pollfd *fds, int i; pollevent_t eventset; - ret = nxsem_wait(&dev->exclsem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -515,7 +516,7 @@ static int eventfd_do_poll(FAR struct file *filep, FAR struct pollfd *fds, poll_notify(dev->fds, CONFIG_EVENT_FD_NPOLLWAITERS, eventset); out: - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return ret; } #endif @@ -567,7 +568,7 @@ int eventfd(unsigned int count, int flags) /* Device is ready for use */ - nxsem_post(&new_dev->exclsem); + nxmutex_unlock(&new_dev->lock); /* Try open new device */ diff --git a/fs/vfs/fs_fdopen.c b/fs/vfs/fs_fdopen.c index b1114a70021bb..8f14738860593 100644 --- a/fs/vfs/fs_fdopen.c +++ b/fs/vfs/fs_fdopen.c @@ -172,7 +172,7 @@ int fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb, /* Add FILE structure to the stream list */ - ret = nxsem_wait(&slist->sl_sem); + ret = nxmutex_lock(&slist->sl_lock); if (ret < 0) { group_free(tcb->group, stream); @@ -190,11 +190,11 @@ int fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb, slist->sl_tail = stream; } - nxsem_post(&slist->sl_sem); + nxmutex_unlock(&slist->sl_lock); - /* Initialize the semaphore the manages access to the buffer */ + /* Initialize the mutex the manages access to the buffer */ - lib_sem_initialize(stream); + lib_lock_init(stream); } else { diff --git a/fs/vfs/fs_mkdir.c b/fs/vfs/fs_mkdir.c index 67229e7130800..fbe71e89bcda2 100644 --- a/fs/vfs/fs_mkdir.c +++ b/fs/vfs/fs_mkdir.c @@ -138,7 +138,7 @@ int mkdir(const char *pathname, mode_t mode) * count of zero. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { errcode = -ret; @@ -146,7 +146,7 @@ int mkdir(const char *pathname, mode_t mode) } ret = inode_reserve(pathname, mode, &inode); - inode_semgive(); + inode_unlock(); if (ret < 0) { diff --git a/fs/vfs/fs_poll.c b/fs/vfs/fs_poll.c index 781d3cf093828..d89c5cd5c387d 100644 --- a/fs/vfs/fs_poll.c +++ b/fs/vfs/fs_poll.c @@ -41,25 +41,10 @@ #include "inode/inode.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define poll_semgive(sem) nxsem_post(sem) - /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: poll_semtake - ****************************************************************************/ - -static int poll_semtake(FAR sem_t *sem) -{ - return nxsem_wait(sem); -} - /**************************************************************************** * Name: poll_fdsetup * @@ -512,7 +497,7 @@ int nx_poll(FAR struct pollfd *fds, unsigned int nfds, int timeout) { /* Wait for the poll event or signal with no timeout */ - ret = poll_semtake(&sem); + ret = nxsem_wait(&sem); } /* Teardown the poll operation and get the count of events. Zero will diff --git a/fs/vfs/fs_rename.c b/fs/vfs/fs_rename.c index 91c09230ed521..e87518e8e1de1 100644 --- a/fs/vfs/fs_rename.c +++ b/fs/vfs/fs_rename.c @@ -168,7 +168,7 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode, * of zero. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { goto errout; @@ -183,7 +183,7 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode, */ ret = -EEXIST; - goto errout_with_sem; + goto errout_with_lock; } /* Copy the inode state from the old inode to the newly allocated inode */ @@ -228,7 +228,7 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode, /* Remove the new node we just recreated */ inode_remove(newpath); - goto errout_with_sem; + goto errout_with_lock; } /* Remove all of the children from the unlinked inode */ @@ -237,8 +237,8 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode, oldinode->i_parent = NULL; ret = OK; -errout_with_sem: - inode_semgive(); +errout_with_lock: + inode_unlock(); errout: RELEASE_SEARCH(&newdesc); diff --git a/fs/vfs/fs_rmdir.c b/fs/vfs/fs_rmdir.c index 2a98ea38acbf1..e4249a4e8a0f4 100644 --- a/fs/vfs/fs_rmdir.c +++ b/fs/vfs/fs_rmdir.c @@ -133,7 +133,7 @@ int rmdir(FAR const char *pathname) * -EBUSY to indicate that the inode was not deleted now. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { errcode = -ret; @@ -141,7 +141,7 @@ int rmdir(FAR const char *pathname) } ret = inode_remove(pathname); - inode_semgive(); + inode_unlock(); if (ret < 0 && ret != -EBUSY) { diff --git a/fs/vfs/fs_symlink.c b/fs/vfs/fs_symlink.c index 8fc80fb936c9e..e55067191bdf8 100644 --- a/fs/vfs/fs_symlink.c +++ b/fs/vfs/fs_symlink.c @@ -140,7 +140,7 @@ int symlink(FAR const char *path1, FAR const char *path2) * count of zero. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { kmm_free(newpath2); @@ -149,7 +149,7 @@ int symlink(FAR const char *path1, FAR const char *path2) } ret = inode_reserve(path2, 0777, &inode); - inode_semgive(); + inode_unlock(); if (ret < 0) { diff --git a/fs/vfs/fs_timerfd.c b/fs/vfs/fs_timerfd.c index 2cc37160b02a3..d17ef7c834980 100644 --- a/fs/vfs/fs_timerfd.c +++ b/fs/vfs/fs_timerfd.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -61,7 +62,7 @@ typedef struct timerfd_waiter_sem_s struct timerfd_priv_s { - sem_t exclsem; /* Enforces device exclusive access */ + mutex_t lock; /* Enforces device exclusive access */ timerfd_waiter_sem_t *rdsems; /* List of blocking readers */ int clock; /* Clock to use as the timing base */ int delay; /* If non-zero, used to reset repetitive @@ -145,7 +146,8 @@ static FAR struct timerfd_priv_s *timerfd_allocdev(void) { /* Initialize the private structure */ - nxsem_init(&dev->exclsem, 0, 0); + nxmutex_init(&dev->lock); + nxmutex_lock(&dev->lock); } return dev; @@ -155,7 +157,7 @@ static void timerfd_destroy(FAR struct timerfd_priv_s *dev) { wd_cancel(&dev->wdog); work_cancel(TIMER_FD_WORK, &dev->work); - nxsem_destroy(&dev->exclsem); + nxmutex_destroy(&dev->lock); kmm_free(dev); } @@ -190,7 +192,7 @@ static int timerfd_open(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -212,7 +214,7 @@ static int timerfd_open(FAR struct file *filep) ret = OK; } - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return ret; } @@ -228,7 +230,7 @@ static int timerfd_close(FAR struct file *filep) /* Get exclusive access to the device structures */ - ret = nxsem_wait(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -245,7 +247,7 @@ static int timerfd_close(FAR struct file *filep) /* Just decrement the reference count and release the semaphore */ priv->crefs--; - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; } @@ -258,7 +260,7 @@ static int timerfd_close(FAR struct file *filep) unregister_driver(devpath); - DEBUGASSERT(priv->exclsem.semcount == 0); + DEBUGASSERT(nxmutex_is_locked(&priv->lock)); timerfd_release_minor(priv->minor); timerfd_destroy(priv); @@ -273,7 +275,7 @@ static int timerfd_blocking_io(FAR struct timerfd_priv_s *dev, sem->next = *slist; *slist = sem; - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); /* Wait for timerfd to notify */ @@ -282,10 +284,10 @@ static int timerfd_blocking_io(FAR struct timerfd_priv_s *dev, if (ret < 0) { /* Interrupted wait, unregister semaphore - * TODO ensure that exclsem wait does not fail (ECANCELED) + * TODO ensure that lock wait does not fail (ECANCELED) */ - nxsem_wait_uninterruptible(&dev->exclsem); + nxmutex_lock(&dev->lock); timerfd_waiter_sem_t *cur_sem = *slist; @@ -305,11 +307,11 @@ static int timerfd_blocking_io(FAR struct timerfd_priv_s *dev, } } - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return ret; } - return nxsem_wait(&dev->exclsem); + return nxmutex_lock(&dev->lock); } static ssize_t timerfd_read(FAR struct file *filep, FAR char *buffer, @@ -325,7 +327,7 @@ static ssize_t timerfd_read(FAR struct file *filep, FAR char *buffer, return -EINVAL; } - ret = nxsem_wait(&dev->exclsem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -337,7 +339,7 @@ static ssize_t timerfd_read(FAR struct file *filep, FAR char *buffer, { if (filep->f_oflags & O_NONBLOCK) { - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return -EAGAIN; } @@ -371,7 +373,7 @@ static ssize_t timerfd_read(FAR struct file *filep, FAR char *buffer, spin_unlock_irqrestore(&dev->lock, intflags); - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return sizeof(timerfd_t); } @@ -384,7 +386,7 @@ static int timerfd_poll(FAR struct file *filep, FAR struct pollfd *fds, int ret; int i; - ret = nxsem_wait(&dev->exclsem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { return ret; @@ -440,7 +442,7 @@ static int timerfd_poll(FAR struct file *filep, FAR struct pollfd *fds, } out: - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); return ret; } #endif @@ -450,7 +452,7 @@ static void timerfd_timeout_work(FAR void *arg) FAR struct timerfd_priv_s *dev = (FAR struct timerfd_priv_s *)arg; int ret; - ret = nxsem_wait_uninterruptible(&dev->exclsem); + ret = nxmutex_lock(&dev->lock); if (ret < 0) { wd_cancel(&dev->wdog); @@ -473,8 +475,7 @@ static void timerfd_timeout_work(FAR void *arg) } dev->rdsems = NULL; - - nxsem_post(&dev->exclsem); + nxmutex_unlock(&dev->lock); } static void timerfd_timeout(wdparm_t idev) @@ -566,7 +567,7 @@ int timerfd_create(int clockid, int flags) /* Device is ready for use */ - nxsem_post(&new_dev->exclsem); + nxmutex_unlock(&new_dev->lock); /* Try open new device */ diff --git a/fs/vfs/fs_unlink.c b/fs/vfs/fs_unlink.c index 8600572d81e8f..e3b77b78460a0 100644 --- a/fs/vfs/fs_unlink.c +++ b/fs/vfs/fs_unlink.c @@ -169,14 +169,14 @@ int nx_unlink(FAR const char *pathname) * return -EBUSY to indicate that the inode was not deleted now. */ - ret = inode_semtake(); + ret = inode_lock(); if (ret < 0) { goto errout_with_inode; } ret = inode_remove(pathname); - inode_semgive(); + inode_unlock(); if (ret < 0 && ret != -EBUSY) { diff --git a/graphics/nxterm/Make.defs b/graphics/nxterm/Make.defs index 0b135c98df45b..1842dbff196c4 100644 --- a/graphics/nxterm/Make.defs +++ b/graphics/nxterm/Make.defs @@ -32,10 +32,6 @@ ifeq ($(CONFIG_NXTERM_NXKBDIN),y) CSRCS += nxterm_kbdin.c endif -ifeq ($(CONFIG_DEBUG_GRAPHICS),y) -CSRCS += nxterm_sem.c -endif - DEPPATH += --dep-path nxterm CFLAGS += ${shell $(INCDIR) "$(CC)" $(TOPDIR)/graphics/nxterm} VPATH += :nxterm diff --git a/graphics/nxterm/nxterm.h b/graphics/nxterm/nxterm.h index 70a25c240c5c0..7cb2459b992cd 100644 --- a/graphics/nxterm/nxterm.h +++ b/graphics/nxterm/nxterm.h @@ -29,7 +29,7 @@ #include -#include +#include #include #include #include @@ -110,7 +110,7 @@ struct nxterm_state_s FAR const struct nxterm_operations_s *ops; /* Window operations */ FAR void *handle; /* The window handle */ FAR struct nxterm_window_s wndo; /* Describes the window and font */ - sem_t exclsem; /* Forces mutually exclusive access */ + mutex_t lock; /* Forces mutually exclusive access */ #ifdef CONFIG_DEBUG_GRAPHICS pid_t holder; /* Deadlock avoidance */ #endif @@ -173,16 +173,6 @@ extern const struct file_operations g_nxterm_drvrops; * Public Function Prototypes ****************************************************************************/ -/* Semaphore helpers */ - -#ifdef CONFIG_DEBUG_GRAPHICS -int nxterm_semwait(FAR struct nxterm_state_s *priv); -int nxterm_sempost(FAR struct nxterm_state_s *priv); -#else -# define nxterm_semwait(p) nxsem_wait(&p->exclsem) -# define nxterm_sempost(p) nxsem_post(&p->exclsem) -#endif - /* Common device registration/un-registration */ FAR struct nxterm_state_s *nxterm_register(NXTERM handle, diff --git a/graphics/nxterm/nxterm_driver.c b/graphics/nxterm/nxterm_driver.c index 082404826a627..ddc45450392b6 100644 --- a/graphics/nxterm/nxterm_driver.c +++ b/graphics/nxterm/nxterm_driver.c @@ -151,7 +151,7 @@ static int nxterm_close(FAR struct file *filep) /* Get exclusive access */ - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -166,7 +166,7 @@ static int nxterm_close(FAR struct file *filep) { /* Yes.. Unregister the terminal device */ - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); nxterm_unregister(priv); return OK; } @@ -177,7 +177,7 @@ static int nxterm_close(FAR struct file *filep) priv->orefs--; } - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); #endif return OK; @@ -203,7 +203,7 @@ static ssize_t nxterm_write(FAR struct file *filep, FAR const char *buffer, /* Get exclusive access */ - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -292,7 +292,7 @@ static ssize_t nxterm_write(FAR struct file *filep, FAR const char *buffer, /* Show the cursor at its new position */ nxterm_showcursor(priv); - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); return (ssize_t)buflen; } @@ -328,7 +328,7 @@ static int nxterm_unlink(FAR struct inode *inode) /* Get exclusive access */ - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -348,12 +348,12 @@ static int nxterm_unlink(FAR struct inode *inode) { /* No.. Unregister the terminal device now */ - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); nxterm_unregister(priv); return OK; } - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); return OK; } #endif diff --git a/graphics/nxterm/nxterm_kbdin.c b/graphics/nxterm/nxterm_kbdin.c index 068d502fb1236..7c10572e58c6f 100644 --- a/graphics/nxterm/nxterm_kbdin.c +++ b/graphics/nxterm/nxterm_kbdin.c @@ -88,10 +88,10 @@ ssize_t nxterm_read(FAR struct file *filep, FAR char *buffer, size_t len) /* Get exclusive access to the driver structure */ - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - gerr("ERROR: nxterm_semwait failed\n"); + gerr("ERROR: nxmutex_lock failed\n"); return ret; } @@ -130,7 +130,7 @@ ssize_t nxterm_read(FAR struct file *filep, FAR char *buffer, size_t len) sched_lock(); priv->nwaiters++; - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); /* We may now be pre-empted! But that should be okay because we * have already incremented nwaiters. Pre-emption is disabled @@ -150,20 +150,20 @@ ssize_t nxterm_read(FAR struct file *filep, FAR char *buffer, size_t len) if (ret >= 0) { - /* Yes... then retake the mutual exclusion semaphore */ + /* Yes... then retake the mutual exclusion mutex */ - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->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) { - /* No.. One of the two nxterm_semwait's failed. */ + /* No.. One of the two nxmutex_lock's failed. */ - gerr("ERROR: nxterm_semwait failed\n"); + gerr("ERROR: nxmutex_lock failed\n"); /* Were we awakened by a signal? Did we read anything before * we received the signal? @@ -178,10 +178,10 @@ ssize_t nxterm_read(FAR struct file *filep, FAR char *buffer, size_t len) /* 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; } } else @@ -206,13 +206,13 @@ ssize_t nxterm_read(FAR struct file *filep, FAR char *buffer, size_t len) } } - /* Relinquish the mutual exclusion semaphore */ + /* Relinquish the mutual exclusion mutex */ - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); /* Notify all poll/select waiters that they can write to the FIFO */ -errout_without_sem: +errout_without_lock: if (nread > 0) { nxterm_pollnotify(priv, POLLOUT); @@ -242,10 +242,10 @@ int nxterm_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup) /* Get exclusive access to the driver structure */ - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - gerr("ERROR: nxterm_semwait failed\n"); + gerr("ERROR: nxmutex_lock failed\n"); return ret; } @@ -318,7 +318,7 @@ int nxterm_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup) } errout: - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); return ret; } @@ -365,10 +365,10 @@ void nxterm_kbdin(NXTERM handle, FAR const uint8_t *buffer, uint8_t buflen) /* Get exclusive access to the driver structure */ - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - gerr("ERROR: nxterm_semwait failed\n"); + gerr("ERROR: nxmutex_lock failed\n"); return; } @@ -441,7 +441,7 @@ void nxterm_kbdin(NXTERM handle, FAR const uint8_t *buffer, uint8_t buflen) sched_unlock(); } - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); } #endif /* CONFIG_NXTERM_NXKBDIN */ diff --git a/graphics/nxterm/nxterm_redraw.c b/graphics/nxterm/nxterm_redraw.c index 58cece5883799..e8208f0ca513b 100644 --- a/graphics/nxterm/nxterm_redraw.c +++ b/graphics/nxterm/nxterm_redraw.c @@ -79,7 +79,7 @@ void nxterm_redraw(NXTERM handle, do { - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->lock); } while (ret < 0); @@ -100,5 +100,5 @@ void nxterm_redraw(NXTERM handle, nxterm_fillchar(priv, rect, &priv->bm[i]); } - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); } diff --git a/graphics/nxterm/nxterm_register.c b/graphics/nxterm/nxterm_register.c index 83b3e1c94a1c1..50708b5b04e7d 100644 --- a/graphics/nxterm/nxterm_register.c +++ b/graphics/nxterm/nxterm_register.c @@ -74,7 +74,7 @@ FAR struct nxterm_state_s * priv->minor = minor; memcpy(&priv->wndo, wndo, sizeof(struct nxterm_window_s)); - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #ifdef CONFIG_DEBUG_GRAPHICS priv->holder = NO_HOLDER; #endif diff --git a/graphics/nxterm/nxterm_resize.c b/graphics/nxterm/nxterm_resize.c index 6a3f62165f51f..cea5db19f240c 100644 --- a/graphics/nxterm/nxterm_resize.c +++ b/graphics/nxterm/nxterm_resize.c @@ -69,7 +69,7 @@ int nxterm_resize(NXTERM handle, FAR const struct nxgl_size_s *size) /* Get exclusive access to the state structure */ - ret = nxterm_semwait(priv); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -82,6 +82,6 @@ int nxterm_resize(NXTERM handle, FAR const struct nxgl_size_s *size) priv->wndo.wsize.w = size->w; priv->wndo.wsize.h = size->h; - nxterm_sempost(priv); + nxmutex_unlock(&priv->lock); return true; } diff --git a/graphics/nxterm/nxterm_sem.c b/graphics/nxterm/nxterm_sem.c deleted file mode 100644 index ef6a1425ec2ec..0000000000000 --- a/graphics/nxterm/nxterm_sem.c +++ /dev/null @@ -1,97 +0,0 @@ -/**************************************************************************** - * graphics/nxterm/nxterm_sem.c - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include - -#include "nxterm.h" - -#ifdef CONFIG_DEBUG_GRAPHICS - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: nxterm_semwait and nxterm_sempost - * - * Description: - * If debug is on, then lower level code may attempt console output while - * we are doing console output! In this case, we will toss the nested - * output to avoid deadlocks and infinite loops. - * - * Input Parameters: - * priv - Driver data structure - * - * Returned Value: - * - * - ****************************************************************************/ - -int nxterm_semwait(FAR struct nxterm_state_s *priv) -{ - pid_t me; - int ret; - - /* Does I already hold the semaphore */ - - me = getpid(); - if (priv->holder != me) - { - /* No.. - * then wait until the thread that does hold it is finished with it - */ - - ret = nxsem_wait(&priv->exclsem); - if (ret == OK) - { - /* No I hold the semaphore */ - - priv->holder = me; - } - - return ret; - } - - /* Abort, abort, abort! I have been re-entered */ - - return -EBUSY; -} - -int nxterm_sempost(FAR struct nxterm_state_s *priv) -{ - /* Make sure that I really hold the semaphore */ - - DEBUGASSERT(priv->holder == getpid()); - - /* Then let go of it */ - - priv->holder = NO_HOLDER; - return nxsem_post(&priv->exclsem); -} - -#endif /* CONFIG_DEBUG_GRAPHICS */ diff --git a/graphics/nxterm/nxterm_unregister.c b/graphics/nxterm/nxterm_unregister.c index 6c6721cc848af..73551e9fbff99 100644 --- a/graphics/nxterm/nxterm_unregister.c +++ b/graphics/nxterm/nxterm_unregister.c @@ -62,9 +62,9 @@ void nxterm_unregister(FAR struct nxterm_state_s *priv) DEBUGASSERT(priv != NULL); - /* Destroy semaphores */ + /* Destroy mutex */ - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); #ifdef CONFIG_NXTERM_NXKBDIN nxsem_destroy(&priv->waitsem); #endif diff --git a/include/nuttx/analog/adc.h b/include/nuttx/analog/adc.h index 5b23355f42666..718e7ca5d0765 100644 --- a/include/nuttx/analog/adc.h +++ b/include/nuttx/analog/adc.h @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -188,7 +189,7 @@ struct adc_dev_s uint8_t ad_ocount; /* The number of times the device has been opened */ uint8_t ad_nrxwaiters; /* Number of threads waiting to enqueue a message */ - sem_t ad_closesem; /* Locks out new opens while close is in progress */ + mutex_t ad_closelock; /* Locks out new opens while close is in progress */ sem_t ad_recvsem; /* Used to wakeup user waiting for space in ad_recv.buffer */ struct adc_fifo_s ad_recv; /* Describes receive FIFO */ bool ad_isovr; /* Flag to indicate an ADC overrun */ diff --git a/include/nuttx/analog/comp.h b/include/nuttx/analog/comp.h index 01fb7544fc6cd..fea245ec40763 100644 --- a/include/nuttx/analog/comp.h +++ b/include/nuttx/analog/comp.h @@ -33,6 +33,7 @@ #include #include +#include #include #ifndef CONFIG_DEV_COMP_NPOLLWAITERS @@ -101,7 +102,7 @@ struct comp_dev_s uint8_t ad_ocount; /* The number of times the device has been opened */ uint8_t val; /* Comparator value after output transition event */ - sem_t ad_sem; /* Used to serialize access */ + mutex_t ad_lock; /* Used to serialize access */ sem_t ad_readsem; /* Blocking read */ /* pollfd's for output transition events */ diff --git a/include/nuttx/analog/dac.h b/include/nuttx/analog/dac.h index a2fc9c68afc5f..97320e717863e 100644 --- a/include/nuttx/analog/dac.h +++ b/include/nuttx/analog/dac.h @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -127,14 +128,14 @@ struct dac_ops_s struct dac_dev_s { - const struct dac_ops_s *ad_ops; /* Arch-specific operations */ - void *ad_priv; /* Used by the arch-specific logic */ - uint8_t ad_ocount; /* The number of times the device has - * been opened */ - uint8_t ad_nchannel; /* Number of dac channel */ - sem_t ad_closesem; /* Locks out new opens while close is - * in progress */ - struct dac_fifo_s ad_xmit; /* Describes receive FIFO */ + const struct dac_ops_s *ad_ops; /* Arch-specific operations */ + void *ad_priv; /* Used by the arch-specific logic */ + uint8_t ad_ocount; /* The number of times the device has + * been opened */ + uint8_t ad_nchannel; /* Number of dac channel */ + mutex_t ad_closelock; /* Locks out new opens while close is + * in progress */ + struct dac_fifo_s ad_xmit; /* Describes receive FIFO */ }; /**************************************************************************** diff --git a/include/nuttx/analog/opamp.h b/include/nuttx/analog/opamp.h index 8a1d9a8c40f1f..5b4f540069041 100644 --- a/include/nuttx/analog/opamp.h +++ b/include/nuttx/analog/opamp.h @@ -33,7 +33,7 @@ #include #include -#include +#include /************************************************************************************ * Public Types @@ -68,7 +68,7 @@ struct opamp_dev_s /* Fields managed by common upper half OPAMP logic */ uint8_t ad_ocount; /* The number of times the device has been opened */ - sem_t ad_closesem; /* Locks out new opens while close is in progress */ + mutex_t ad_closelock; /* Locks out new opens while close is in progress */ #endif /* Fields provided by lower half OPAMP logic */ diff --git a/include/nuttx/audio/audio.h b/include/nuttx/audio/audio.h index 435c2826505d0..49f6a6771cb29 100644 --- a/include/nuttx/audio/audio.h +++ b/include/nuttx/audio/audio.h @@ -42,7 +42,7 @@ #include #include -#include +#include #include #include @@ -412,7 +412,7 @@ struct ap_buffer_s apb_samp_t nmaxbytes; /* The maximum number of bytes */ apb_samp_t nbytes; /* The number of bytes used */ apb_samp_t curbyte; /* Next byte to be processed */ - sem_t sem; /* Reference locking semaphore */ + mutex_t lock; /* Reference locking mutex */ uint16_t flags; /* Buffer flags */ uint16_t crefs; /* Number of reference counts */ FAR uint8_t *samp; /* Offset of the first sample */ diff --git a/include/nuttx/can/can.h b/include/nuttx/can/can.h index 0b5d28287a7a1..abaab50e43fcd 100644 --- a/include/nuttx/can/can.h +++ b/include/nuttx/can/can.h @@ -36,7 +36,7 @@ #include #include #include -#include +#include #ifdef CONFIG_CAN_TXREADY # include @@ -616,8 +616,8 @@ struct can_dev_s uint8_t cd_error; /* Flags to indicate internal device errors */ #endif struct list_node cd_readers; /* List of readers */ - sem_t cd_closesem; /* Locks out new opens while close is in progress */ - sem_t cd_pollsem; /* Manages exclusive access to cd_fds[] */ + mutex_t cd_closelock; /* Locks out new opens while close is in progress */ + mutex_t cd_polllock; /* Manages exclusive access to cd_fds[] */ struct can_txfifo_s cd_xmit; /* Describes transmit FIFO */ #ifdef CONFIG_CAN_TXREADY struct work_s cd_work; /* Use to manage can_txready() work */ diff --git a/include/nuttx/drivers/rwbuffer.h b/include/nuttx/drivers/rwbuffer.h index f83fc832616dc..ca906fc44b4a4 100644 --- a/include/nuttx/drivers/rwbuffer.h +++ b/include/nuttx/drivers/rwbuffer.h @@ -30,7 +30,7 @@ #include #include -#include +#include #include #if defined(CONFIG_DRVR_WRITEBUFFER) || defined(CONFIG_DRVR_READAHEAD) @@ -127,7 +127,7 @@ struct rwbuffer_s /* This is the state of the write buffering */ #ifdef CONFIG_DRVR_WRITEBUFFER - sem_t wrsem; /* Enforces exclusive access to the write buffer */ + mutex_t wrlock; /* Enforces exclusive access to the write buffer */ struct work_s work; /* Delayed work to flush buffer after a delay with no activity */ uint8_t *wrbuffer; /* Allocated write buffer */ uint16_t wrnblocks; /* Number of blocks in write buffer */ @@ -137,7 +137,7 @@ struct rwbuffer_s /* This is the state of the read-ahead buffering */ #ifdef CONFIG_DRVR_READAHEAD - sem_t rhsem; /* Enforces exclusive access to the write buffer */ + mutex_t rhlock; /* Enforces exclusive access to the write buffer */ uint8_t *rhbuffer; /* Allocated read-ahead buffer */ uint16_t rhnblocks; /* Number of blocks in read-ahead buffer */ off_t rhblockstart; /* First block in read-ahead buffer */ diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index 05cd0c7fe1f87..2d9d7be9f0e6b 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -430,7 +430,7 @@ struct file struct filelist { - sem_t fl_sem; /* Manage access to the file list */ + mutex_t fl_lock; /* Manage access to the file list */ uint8_t fl_rows; /* The number of rows of fl_files array */ FAR struct file **fl_files; /* The pointer of two layer file descriptors array */ }; @@ -496,7 +496,7 @@ struct file_struct struct streamlist { - sem_t sl_sem; /* For thread safety */ + mutex_t sl_lock; /* For thread safety */ struct file_struct sl_std[3]; FAR struct file_struct *sl_head; FAR struct file_struct *sl_tail; diff --git a/include/nuttx/leds/ws2812.h b/include/nuttx/leds/ws2812.h index 5ab44468cbd74..db7d467c70615 100644 --- a/include/nuttx/leds/ws2812.h +++ b/include/nuttx/leds/ws2812.h @@ -30,7 +30,7 @@ #include #ifdef CONFIG_WS2812_NON_SPI_DRIVER -#include +#include #else /* CONFIG_WS2812_NON_SPI_DRIVER */ #include #endif /* CONFIG_WS2812_NON_SPI_DRIVER */ @@ -90,7 +90,7 @@ struct ws2812_dev_s void *private; /* Private data for opened device */ uint32_t clock; - sem_t exclsem; + mutex_t lock; int port; uint16_t nleds; bool has_white; diff --git a/include/nuttx/lib/lib.h b/include/nuttx/lib/lib.h index 6b5d025a1ae61..2668588caa1e5 100644 --- a/include/nuttx/lib/lib.h +++ b/include/nuttx/lib/lib.h @@ -112,13 +112,13 @@ void lib_stream_release(FAR struct task_group_s *group); /* Functions defined in lib_filesem.c ***************************************/ #ifdef CONFIG_STDIO_DISABLE_BUFFERING -# define lib_sem_initialize(s) -# define lib_take_semaphore(s) -# define lib_give_semaphore(s) +# define lib_lock_init(s) +# define lib_take_lock(s) +# define lib_give_lock(s) #else -void lib_sem_initialize(FAR struct file_struct *stream); -void lib_take_semaphore(FAR struct file_struct *stream); -void lib_give_semaphore(FAR struct file_struct *stream); +void lib_lock_init(FAR struct file_struct *stream); +void lib_take_lock(FAR struct file_struct *stream); +void lib_give_lock(FAR struct file_struct *stream); #endif /* Functions defined in lib_srand.c *****************************************/ diff --git a/include/nuttx/motor/foc/foc.h b/include/nuttx/motor/foc/foc.h index b364752527214..bee45811e8910 100644 --- a/include/nuttx/motor/foc/foc.h +++ b/include/nuttx/motor/foc/foc.h @@ -27,9 +27,10 @@ #include #include +#include +#include #include -#include #include @@ -117,7 +118,7 @@ struct foc_dev_s uint8_t ocount; /* The number of times the device * has been opened */ - sem_t closesem; /* Locks out new opens while close + mutex_t closelock; /* Locks out new opens while close * is in progress */ sem_t statesem; /* Notifier semaphore */ diff --git a/include/nuttx/mtd/nand.h b/include/nuttx/mtd/nand.h index d271f6b1b87f1..a1b2378ef1d0c 100644 --- a/include/nuttx/mtd/nand.h +++ b/include/nuttx/mtd/nand.h @@ -46,7 +46,7 @@ #include #include -#include +#include /**************************************************************************** * Pre-processor Definitions @@ -66,7 +66,7 @@ struct nand_dev_s { struct mtd_dev_s mtd; /* Externally visible part of the driver */ FAR struct nand_raw_s *raw; /* Retained reference to the lower half */ - sem_t exclsem; /* For exclusive access to the NAND FLASH */ + mutex_t lock; /* For exclusive access to the NAND FLASH */ }; /**************************************************************************** diff --git a/include/nuttx/mutex.h b/include/nuttx/mutex.h index 1753934e46d45..129521f06d2d9 100644 --- a/include/nuttx/mutex.h +++ b/include/nuttx/mutex.h @@ -29,6 +29,7 @@ #include #include #include + #include /**************************************************************************** @@ -36,8 +37,9 @@ ****************************************************************************/ #define NXRMUTEX_NO_HOLDER (pid_t)-1 -#define NXMUTEX_INITIALIZER SEM_INITIALIZER(1) -#define NXRMUTEX_INITIALIZER {SEM_INITIALIZER(1), NXRMUTEX_NO_HOLDER, 0} +#define NXMUTEX_INITIALIZER NXSEM_INITIALIZER(1, 0) +#define NXRMUTEX_INITIALIZER {NXSEM_INITIALIZER(1, 0), \ + NXRMUTEX_NO_HOLDER, 0} /**************************************************************************** * Public Type Definitions @@ -49,7 +51,7 @@ struct rmutex_s { mutex_t mutex; pid_t holder; - uint16_t count; + unsigned int count; }; typedef struct rmutex_s rmutex_t; @@ -226,7 +228,6 @@ static inline bool nxmutex_is_locked(FAR mutex_t *mutex) int ret; ret = _SEM_GETVALUE(mutex, &cnt); - DEBUGASSERT(ret == OK); return cnt < 1; @@ -265,6 +266,24 @@ static inline int nxmutex_unlock(FAR mutex_t *mutex) return ret; } +/**************************************************************************** + * Name: nxmutex_reset + * + * Description: + * This function reset lock state. + * + * Parameters: + * mutex - mutex descriptor. + * + * Return Value: + * + ****************************************************************************/ + +static inline int nxmutex_reset(FAR mutex_t *mutex) +{ + return nxsem_reset(mutex, 1); +} + /**************************************************************************** * Name: nxrmutex_init * @@ -339,7 +358,7 @@ static inline int nxrmutex_lock(FAR rmutex_t *rmutex) if (rmutex->holder == tid) { - DEBUGASSERT(rmutex->count < UINT16_MAX); + DEBUGASSERT(rmutex->count < UINT_MAX); rmutex->count++; ret = OK; } @@ -386,7 +405,7 @@ static inline int nxrmutex_trylock(FAR rmutex_t *rmutex) if (rmutex->holder == tid) { - DEBUGASSERT(rmutex->count < UINT16_MAX); + DEBUGASSERT(rmutex->count < UINT_MAX); rmutex->count++; ret = OK; } @@ -470,15 +489,79 @@ static inline int nxrmutex_unlock(FAR rmutex_t *rmutex) DEBUGASSERT(rmutex->holder == tid); DEBUGASSERT(rmutex->count > 0); - if (rmutex->count == 1) + if (rmutex->count-- == 1) { - rmutex->count = 0; rmutex->holder = NXRMUTEX_NO_HOLDER; ret = nxmutex_unlock(&rmutex->mutex); } - else + + return ret; +} + +/**************************************************************************** + * Name: nrxmutex_breaklock + * + * Description: + * This function attempts to break the recursive mutex + * + * Parameters: + * rmutex - Recursive mutex descriptor. + * + * Return Value: + * This is an internal OS interface and should not be used by applications. + * It follows the NuttX internal error return policy: Zero (OK) is + * returned on success. A negated errno value is returned on failure. + * Possible returned errors: + * + ****************************************************************************/ + +static inline int nxrmutex_breaklock(FAR rmutex_t *rmutex, + FAR unsigned int *count) +{ + pid_t tid = gettid(); + int ret = -EPERM; + + if (rmutex->holder == tid) + { + *count = rmutex->count; + rmutex->count = 0; + rmutex->holder = NXRMUTEX_NO_HOLDER; + nxmutex_unlock(&rmutex->mutex); + ret = 0; + } + + return ret; +} + +/**************************************************************************** + * Name: nxrmutex_restorelock + * + * Description: + * This function attempts to restore the recursive mutex. + * + * Parameters: + * rmutex - Recursive mutex descriptor. + * + * Return Value: + * This is an internal OS interface and should not be used by applications. + * It follows the NuttX internal error return policy: Zero (OK) is + * returned on success. A negated errno value is returned on failure. + * Possible returned errors: + * + ****************************************************************************/ + +static inline int nxrmutex_restorelock(FAR rmutex_t *rmutex, + unsigned int count) +{ + pid_t tid = gettid(); + int ret; + + DEBUGASSERT(rmutex->holder != tid); + ret = nxmutex_lock(&rmutex->mutex); + if (ret == OK) { - rmutex->count--; + rmutex->holder = tid; + rmutex->count = count; } return ret; diff --git a/include/nuttx/nx/nxmu.h b/include/nuttx/nx/nxmu.h index 9f55dc35675a0..c2d425d986853 100644 --- a/include/nuttx/nx/nxmu.h +++ b/include/nuttx/nx/nxmu.h @@ -65,10 +65,6 @@ #define NX_CLIMSG_PRIO 42 #define NX_SVRMSG_PRIO 42 -/* Handy macros */ - -#define nxmu_semgive(sem) _SEM_POST(sem) /* To match nxmu_semtake() */ - /**************************************************************************** * Public Types ****************************************************************************/ @@ -567,22 +563,6 @@ extern "C" int nxmu_start(int display, int plane); -/**************************************************************************** - * Name: nxmu_semtake - * - * Description: - * Take the semaphore, handling EINTR wakeups. See the nxmu_semgive macro. - * - * Input Parameters: - * sem - the semaphore to be taken. - * - * Returned Value: - * None - * - ****************************************************************************/ - -void nxmu_semtake(sem_t *sem); - /**************************************************************************** * Name: nxmu_sendserver * diff --git a/include/nuttx/power/battery_charger.h b/include/nuttx/power/battery_charger.h index 90edad2d4df85..c380468a37118 100644 --- a/include/nuttx/power/battery_charger.h +++ b/include/nuttx/power/battery_charger.h @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include @@ -140,7 +140,7 @@ struct battery_charger_dev_s FAR const struct battery_charger_operations_s *ops; /* Battery operations */ - sem_t batsem; /* Enforce mutually exclusive access */ + mutex_t batlock; /* Enforce mutually exclusive access */ struct list_node flist; diff --git a/include/nuttx/power/battery_gauge.h b/include/nuttx/power/battery_gauge.h index d5a093d800148..d7a503c419951 100644 --- a/include/nuttx/power/battery_gauge.h +++ b/include/nuttx/power/battery_gauge.h @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include @@ -127,7 +127,7 @@ struct battery_gauge_dev_s /* Fields required by the upper-half driver */ FAR const struct battery_gauge_operations_s *ops; /* Battery operations */ - sem_t batsem; /* Enforce mutually exclusive access */ + mutex_t batlock; /* Enforce mutually exclusive access */ struct list_node flist; diff --git a/include/nuttx/power/battery_monitor.h b/include/nuttx/power/battery_monitor.h index cc92e9b641cbb..bb14c2786bbff 100644 --- a/include/nuttx/power/battery_monitor.h +++ b/include/nuttx/power/battery_monitor.h @@ -34,7 +34,7 @@ #include #include -#include +#include #ifdef CONFIG_BATTERY_MONITOR @@ -301,7 +301,7 @@ struct battery_monitor_dev_s /* Fields required by the upper-half driver */ FAR const struct battery_monitor_operations_s *ops; /* Battery operations */ - sem_t batsem; /* Enforce mutually exclusive access */ + mutex_t batlock; /* Enforce mutually exclusive access */ struct list_node flist; diff --git a/include/nuttx/power/powerled.h b/include/nuttx/power/powerled.h index 7e0090bf88ad8..627e6025edd35 100644 --- a/include/nuttx/power/powerled.h +++ b/include/nuttx/power/powerled.h @@ -37,7 +37,7 @@ #include #include -#include +#include #ifdef CONFIG_DRIVERS_POWERLED @@ -203,7 +203,7 @@ struct powerled_dev_s uint8_t ocount; /* The number of times the device * has been opened */ - sem_t closesem; /* Locks out new opens while close + mutex_t closelock; /* Locks out new opens while close * is in progress */ diff --git a/include/nuttx/power/regulator.h b/include/nuttx/power/regulator.h index bec5c37fb896c..b9234d90f5fcd 100644 --- a/include/nuttx/power/regulator.h +++ b/include/nuttx/power/regulator.h @@ -31,6 +31,7 @@ #include #include +#include #include /**************************************************************************** @@ -92,7 +93,7 @@ struct regulator_dev_s FAR const struct regulator_ops_s *ops; uint32_t use_count; uint32_t open_count; - sem_t regulator_sem; + mutex_t regulator_lock; struct list_node list; struct list_node consumer_list; diff --git a/include/nuttx/power/smps.h b/include/nuttx/power/smps.h index e12bbb036b19a..acbecc189bb0c 100644 --- a/include/nuttx/power/smps.h +++ b/include/nuttx/power/smps.h @@ -246,10 +246,10 @@ struct smps_dev_s { /* Fields managed by common upper half SMPS logic */ - uint8_t ocount; /* The number of times the device + uint8_t ocount; /* The number of times the device * has been opened */ - sem_t closesem; /* Locks out new opens while close + mutex_t closelock; /* Locks out new opens while close * is in progress */ diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 0811d08d86b47..2bd184adda49b 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -460,7 +460,7 @@ struct task_group_s /* Pthread join Info: */ - sem_t tg_joinsem; /* Mutually exclusive access to join data */ + sem_t tg_joinlock; /* Mutually exclusive access to join data */ FAR struct join_s *tg_joinhead; /* Head of a list of join data */ FAR struct join_s *tg_jointail; /* Tail of a list of join data */ #endif diff --git a/include/nuttx/serial/serial.h b/include/nuttx/serial/serial.h index 61acebd5df991..56cee09de3cc2 100644 --- a/include/nuttx/serial/serial.h +++ b/include/nuttx/serial/serial.h @@ -120,7 +120,7 @@ struct uart_buffer_s { - sem_t sem; /* Used to control exclusive access to the buffer */ + mutex_t lock; /* Used to control exclusive access to the buffer */ volatile int16_t head; /* Index to the head [IN] index in the buffer */ volatile int16_t tail; /* Index to the tail [OUT] index in the buffer */ int16_t size; /* The allocated size of the buffer */ @@ -291,12 +291,12 @@ struct uart_dev_s tcflag_t tc_lflag; /* Local modes */ #endif - /* Semaphores */ + /* Semaphores & mutex */ - sem_t closesem; /* Locks out new open while close is in progress */ sem_t xmitsem; /* Wakeup user waiting for space in xmit.buffer */ sem_t recvsem; /* Wakeup user waiting for data in recv.buffer */ - sem_t pollsem; /* Manages exclusive access to fds[] */ + mutex_t closelock; /* Locks out new open while close is in progress */ + mutex_t polllock; /* Manages exclusive access to fds[] */ /* I/O buffers */ diff --git a/include/nuttx/spi/spi_bitbang.h b/include/nuttx/spi/spi_bitbang.h index 27c6c747264d9..2343a75e5fe61 100644 --- a/include/nuttx/spi/spi_bitbang.h +++ b/include/nuttx/spi/spi_bitbang.h @@ -27,7 +27,7 @@ #include -#include +#include #include #ifdef CONFIG_SPI_BITBANG @@ -87,7 +87,7 @@ struct spi_bitbang_s FAR const struct spi_bitbang_ops_s *low; /* Low-level operations */ uint32_t holdtime; /* SCK hold time to achieve requested frequency */ bitexchange_t exchange; /* The select bit exchange function */ - sem_t exclsem; /* Supports mutually exclusive access to SPI */ + mutex_t lock; /* Supports mutually exclusive access to SPI */ #ifdef CONFIG_SPI_BITBANG_VARWIDTH uint8_t nbits; /* Number of bits in the transfer */ #endif diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h index a3f7be7b2c644..8d233f860169a 100644 --- a/include/nuttx/tls.h +++ b/include/nuttx/tls.h @@ -119,7 +119,7 @@ struct getopt_s struct task_info_s { - sem_t ta_sem; + mutex_t ta_lock; FAR char **argv; /* Name+start-up parameters */ #if CONFIG_TLS_TASK_NELEM > 0 uintptr_t ta_telem[CONFIG_TLS_TASK_NELEM]; /* Task local storage elements */ diff --git a/include/nuttx/usb/usbhost_devaddr.h b/include/nuttx/usb/usbhost_devaddr.h index 1e18cae2d5198..77e06c4f6a21f 100644 --- a/include/nuttx/usb/usbhost_devaddr.h +++ b/include/nuttx/usb/usbhost_devaddr.h @@ -30,7 +30,7 @@ #include -#include +#include /**************************************************************************** * Pre-processor Definitions @@ -48,7 +48,7 @@ struct usbhost_devaddr_s { uint8_t next; /* Next device address */ - sem_t exclsem; /* Enforces mutually exclusive access */ + mutex_t lock; /* Enforces mutually exclusive access */ uint32_t alloctab[4]; /* Bit allocation table */ }; diff --git a/include/nuttx/wireless/cc1101.h b/include/nuttx/wireless/cc1101.h index 5e1541d24513c..f17e34dbe8c40 100644 --- a/include/nuttx/wireless/cc1101.h +++ b/include/nuttx/wireless/cc1101.h @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -283,12 +284,12 @@ struct cc1101_dev_s uint32_t miso_pin; /* MISO Pin */ struct work_s irq_work; /* Interrupt handling "bottom half" */ uint8_t nopens; /* The number of times the device has been opened */ - sem_t devsem; /* Ensures exclusive access to this structure */ + mutex_t devlock; /* Ensures exclusive access to this structure */ uint8_t *rx_buffer; /* Circular RX buffer. [pipe# / pkt_len] [packet data...] */ uint16_t fifo_len; /* Number of bytes stored in fifo */ uint16_t nxt_read; /* Next read index */ uint16_t nxt_write; /* Next write index */ - sem_t sem_rx_buffer; /* Protect access to rx fifo */ + mutex_t lock_rx_buffer; /* Protect access to rx fifo */ sem_t sem_rx; /* Wait for availability of received data */ sem_t sem_tx; /* Wait for availability of send data */ FAR struct pollfd *pfd; /* Polled file descr (or NULL if any) */ diff --git a/include/nuttx/wireless/ieee80211/bcmf_gspi.h b/include/nuttx/wireless/ieee80211/bcmf_gspi.h index 984d13ef80770..0d8b24405b350 100644 --- a/include/nuttx/wireless/ieee80211/bcmf_gspi.h +++ b/include/nuttx/wireless/ieee80211/bcmf_gspi.h @@ -28,6 +28,7 @@ #include #include +#include #include "nuttx/net/net.h" #include "nuttx/net/netdev.h" @@ -85,7 +86,7 @@ typedef struct gspi_dev_s uint16_t length, FAR uint32_t *buffer); - sem_t exclsem; + mutex_t lock; /* -------------------------------------------------------- * Other fields must be set to zero. diff --git a/libs/libc/audio/lib_buffer.c b/libs/libc/audio/lib_buffer.c index c1f6a66023161..ef248df4ae169 100644 --- a/libs/libc/audio/lib_buffer.c +++ b/libs/libc/audio/lib_buffer.c @@ -41,39 +41,9 @@ #if defined(CONFIG_AUDIO) /**************************************************************************** - * Private Functions + * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: apb_semtake - * - * Take an Audio Pipeline Buffer. - * - ****************************************************************************/ - -static void apb_semtake(FAR struct ap_buffer_s *apb) -{ - int ret; - - /* Take the semaphore (perhaps waiting) */ - - while (_SEM_WAIT(&apb->sem) < 0) - { - /* The only case that an error should occr here is if - * the wait was awakened by a signal. - */ - - DEBUGASSERT(_SEM_ERRNO(ret) == EINTR || _SEM_ERRNO(ret) == ECANCELED); - UNUSED(ret); - } -} - -/**************************************************************************** - * Name: apb_semgive - ****************************************************************************/ - -#define apb_semgive(b) _SEM_POST(&b->sem) - /**************************************************************************** * Name: apb_alloc * @@ -118,7 +88,7 @@ int apb_alloc(FAR struct audio_buf_desc_s *bufdesc) apb->session = bufdesc->session; #endif - _SEM_INIT(&apb->sem, 0, 1); + nxmutex_init(&apb->lock); ret = sizeof(struct audio_buf_desc_s); } @@ -138,14 +108,14 @@ void apb_free(FAR struct ap_buffer_s *apb) /* Perform a reference count decrement and possibly release the memory */ - apb_semtake(apb); + nxmutex_lock(&apb->lock); refcount = apb->crefs--; - apb_semgive(apb); + nxmutex_unlock(&apb->lock); if (refcount <= 1) { audinfo("Freeing %p\n", apb); - _SEM_DESTROY(&apb->sem); + nxmutex_destroy(&apb->lock); lib_ufree(apb); } } @@ -163,9 +133,9 @@ void apb_reference(FAR struct ap_buffer_s *apb) { /* Do we need any thread protection here? Almost certainly... */ - apb_semtake(apb); + nxmutex_lock(&apb->lock); apb->crefs++; - apb_semgive(apb); + nxmutex_unlock(&apb->lock); } #endif /* CONFIG_AUDIO */ diff --git a/libs/libc/locale/lib_gettext.c b/libs/libc/locale/lib_gettext.c index 2c6bef67c7136..e5835f1d14a48 100644 --- a/libs/libc/locale/lib_gettext.c +++ b/libs/libc/locale/lib_gettext.c @@ -30,13 +30,14 @@ #include #include #include -#include #include -#include #include #include #include +#include +#include + #include "libc.h" #ifdef CONFIG_LIBC_LOCALE_GETTEXT @@ -83,7 +84,7 @@ static FAR const char *g_catname[] = "LC_ALL", }; -static sem_t g_sem = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; static FAR struct mofile_s *g_mofile; #ifdef CONFIG_BUILD_KERNEL @@ -577,7 +578,7 @@ FAR char *dcngettext(FAR const char *domainname, CONFIG_LIBC_LOCALE_PATH"/%s/%s/%s.mo", lang, g_catname[category], domainname); - while (_SEM_WAIT(&g_sem) < 0); + while (nxmutex_lock(&g_lock) < 0); for (mofile = g_mofile; mofile; mofile = mofile->next) { @@ -594,7 +595,7 @@ FAR char *dcngettext(FAR const char *domainname, mofile = lib_malloc(sizeof(*mofile)); if (mofile == NULL) { - _SEM_POST(&g_sem); + nxmutex_unlock(&g_lock); return notrans; } @@ -602,7 +603,7 @@ FAR char *dcngettext(FAR const char *domainname, mofile->map = momap(path, &mofile->size); if (mofile->map == MAP_FAILED) { - _SEM_POST(&g_sem); + nxmutex_unlock(&g_lock); lib_free(mofile); return notrans; } @@ -647,7 +648,7 @@ FAR char *dcngettext(FAR const char *domainname, g_mofile = mofile; } - _SEM_POST(&g_sem); /* Leave look before search */ + nxmutex_unlock(&g_lock); /* Leave look before search */ trans = molookup(mofile->map, mofile->size, msgid1); if (trans == NULL) diff --git a/libs/libc/pthread/pthread_keycreate.c b/libs/libc/pthread/pthread_keycreate.c index 478dedec1f1c8..4a70a8a8f70a8 100644 --- a/libs/libc/pthread/pthread_keycreate.c +++ b/libs/libc/pthread/pthread_keycreate.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include #if CONFIG_TLS_NELEM > 0 @@ -83,12 +83,10 @@ int pthread_key_create(FAR pthread_key_t *key, * avoid concurrent modification of the group TLS index set. */ - ret = _SEM_WAIT(&info->ta_sem); - + ret = nxmutex_lock(&info->ta_lock); if (ret < 0) { - ret = _SEM_ERRNO(ret); - return ret; + return -ret; } ret = EAGAIN; @@ -110,7 +108,7 @@ int pthread_key_create(FAR pthread_key_t *key, } } - _SEM_POST(&info->ta_sem); + nxmutex_unlock(&info->ta_lock); return ret; } diff --git a/libs/libc/pthread/pthread_keydelete.c b/libs/libc/pthread/pthread_keydelete.c index 51693a4337221..c0ac344b69aa2 100644 --- a/libs/libc/pthread/pthread_keydelete.c +++ b/libs/libc/pthread/pthread_keydelete.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include #if CONFIG_TLS_NELEM > 0 @@ -70,16 +70,16 @@ int pthread_key_delete(pthread_key_t key) */ mask = (tls_ndxset_t)1 << key; - ret = _SEM_WAIT(&info->ta_sem); + ret = nxmutex_lock(&info->ta_lock); if (ret == OK) { DEBUGASSERT((info->ta_tlsset & mask) != 0); info->ta_tlsset &= ~mask; - _SEM_POST(&info->ta_sem); + nxmutex_unlock(&info->ta_lock); } else { - ret = _SEM_ERRNO(ret); + ret = -ret; } } diff --git a/libs/libc/stdio/lib_fclose.c b/libs/libc/stdio/lib_fclose.c index d414f5abee5e0..8588a0406db23 100644 --- a/libs/libc/stdio/lib_fclose.c +++ b/libs/libc/stdio/lib_fclose.c @@ -133,7 +133,7 @@ int fclose(FAR FILE *stream) } #ifndef CONFIG_STDIO_DISABLE_BUFFERING - /* Destroy the semaphore */ + /* Destroy the mutex */ nxrmutex_destroy(&stream->fs_lock); diff --git a/libs/libc/stdio/lib_ftello.c b/libs/libc/stdio/lib_ftello.c index d9651f91abc1c..8fa1e1ee4dc83 100644 --- a/libs/libc/stdio/lib_ftello.c +++ b/libs/libc/stdio/lib_ftello.c @@ -53,7 +53,7 @@ static off_t lib_getoffset(FAR FILE *stream) { off_t offset = 0; - lib_take_semaphore(stream); + lib_take_lock(stream); if (stream->fs_bufstart != NULL && stream->fs_bufread != @@ -71,7 +71,7 @@ static off_t lib_getoffset(FAR FILE *stream) offset = -(stream->fs_bufpos - stream->fs_bufstart); } - lib_give_semaphore(stream); + lib_give_lock(stream); return offset; } #else diff --git a/libs/libc/stdio/lib_libfflush.c b/libs/libc/stdio/lib_libfflush.c index f44dc22a28b1c..8790bbf77f8cd 100644 --- a/libs/libc/stdio/lib_libfflush.c +++ b/libs/libc/stdio/lib_libfflush.c @@ -73,7 +73,7 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) /* Make sure that we have exclusive access to the stream */ - lib_take_semaphore(stream); + lib_take_lock(stream); /* Check if there is an allocated I/O buffer */ @@ -82,7 +82,7 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) /* No, then there can be nothing remaining in the buffer. */ ret = 0; - goto errout_with_sem; + goto errout_with_lock; } /* Make sure that the buffer holds valid data */ @@ -100,7 +100,7 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) */ ret = 0; - goto errout_with_sem; + goto errout_with_lock; } /* How many bytes of write data are used in the buffer now */ @@ -123,7 +123,7 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) stream->fs_flags |= __FS_FLAG_ERROR; ret = _NX_GETERRVAL(bytes_written); - goto errout_with_sem; + goto errout_with_lock; } /* Handle partial writes. fflush() must either return with @@ -156,11 +156,11 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) * remaining in the buffer. */ - lib_give_semaphore(stream); + lib_give_lock(stream); return stream->fs_bufpos - stream->fs_bufstart; -errout_with_sem: - lib_give_semaphore(stream); +errout_with_lock: + lib_give_lock(stream); return ret; #else diff --git a/libs/libc/stdio/lib_libfilesem.c b/libs/libc/stdio/lib_libfilesem.c index 6f778a62f05a7..3fc19c7672a5a 100644 --- a/libs/libc/stdio/lib_libfilesem.c +++ b/libs/libc/stdio/lib_libfilesem.c @@ -41,12 +41,12 @@ ****************************************************************************/ /**************************************************************************** - * lib_sem_initialize + * lib_lock_init ****************************************************************************/ -void lib_sem_initialize(FAR struct file_struct *stream) +void lib_lock_init(FAR struct file_struct *stream) { - /* Initialize the LIB semaphore to one (to support one-at-a-time access + /* Initialize the LIB mutex to one (to support one-at-a-time access * to private data sets. */ @@ -54,19 +54,19 @@ void lib_sem_initialize(FAR struct file_struct *stream) } /**************************************************************************** - * lib_take_semaphore + * lib_take_lock ****************************************************************************/ -void lib_take_semaphore(FAR struct file_struct *stream) +void lib_take_lock(FAR struct file_struct *stream) { nxrmutex_lock(&stream->fs_lock); } /**************************************************************************** - * lib_give_semaphore + * lib_give_lock ****************************************************************************/ -void lib_give_semaphore(FAR struct file_struct *stream) +void lib_give_lock(FAR struct file_struct *stream) { nxrmutex_unlock(&stream->fs_lock); } diff --git a/libs/libc/stdio/lib_libfread.c b/libs/libc/stdio/lib_libfread.c index e4b218ff018d2..98a4704160ee6 100644 --- a/libs/libc/stdio/lib_libfread.c +++ b/libs/libc/stdio/lib_libfread.c @@ -64,7 +64,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream) { /* The stream must be stable until we complete the read */ - lib_take_semaphore(stream); + lib_take_lock(stream); #if CONFIG_NUNGET_CHARS > 0 /* First, re-read any previously ungotten characters */ @@ -298,7 +298,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream) stream->fs_flags |= __FS_FLAG_EOF; } - lib_give_semaphore(stream); + lib_give_lock(stream); return count - remaining; } @@ -306,6 +306,6 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream) errout_with_errno: stream->fs_flags |= __FS_FLAG_ERROR; - lib_give_semaphore(stream); + lib_give_lock(stream); return ERROR; } diff --git a/libs/libc/stdio/lib_libfwrite.c b/libs/libc/stdio/lib_libfwrite.c index 1619396488932..024a7223e9e11 100644 --- a/libs/libc/stdio/lib_libfwrite.c +++ b/libs/libc/stdio/lib_libfwrite.c @@ -83,7 +83,7 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream) /* Get exclusive access to the stream */ - lib_take_semaphore(stream); + lib_take_lock(stream); /* If the buffer is currently being used for read access, then * discard all of the read-ahead data. We do not support concurrent @@ -92,7 +92,7 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream) if (lib_rdflush(stream) < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } /* Determine the number of bytes left in the buffer */ @@ -128,7 +128,7 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream) int bytes_buffered = lib_fflush(stream, false); if (bytes_buffered < 0) { - goto errout_with_semaphore; + goto errout_with_lock; } } } @@ -140,7 +140,7 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream) { _NX_SETERRNO(ret); ret = ERROR; - goto errout_with_semaphore; + goto errout_with_lock; } src += ret; @@ -156,8 +156,8 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream) ret = (uintptr_t)src - (uintptr_t)start; -errout_with_semaphore: - lib_give_semaphore(stream); +errout_with_lock: + lib_give_lock(stream); errout: if (ret < 0) diff --git a/libs/libc/stdio/lib_libstream.c b/libs/libc/stdio/lib_libstream.c index 3a25734287aff..873a2268ccfc4 100644 --- a/libs/libc/stdio/lib_libstream.c +++ b/libs/libc/stdio/lib_libstream.c @@ -66,18 +66,18 @@ void lib_stream_initialize(FAR struct task_group_s *group) /* Initialize the list access mutex */ - _SEM_INIT(&list->sl_sem, 0, 1); + nxmutex_init(&list->sl_lock); list->sl_head = NULL; list->sl_tail = NULL; /* Initialize stdin, stdout and stderr stream */ list->sl_std[0].fs_fd = -1; - lib_sem_initialize(&list->sl_std[0]); + lib_lock_init(&list->sl_std[0]); list->sl_std[1].fs_fd = -1; - lib_sem_initialize(&list->sl_std[1]); + lib_lock_init(&list->sl_std[1]); list->sl_std[2].fs_fd = -1; - lib_sem_initialize(&list->sl_std[2]); + lib_lock_init(&list->sl_std[2]); } /**************************************************************************** @@ -102,9 +102,9 @@ void lib_stream_release(FAR struct task_group_s *group) list = &group->tg_streamlist; #endif - /* Destroy the semaphore and release the filelist */ + /* Destroy the mutex and release the filelist */ - _SEM_DESTROY(&list->sl_sem); + nxmutex_destroy(&list->sl_lock); /* Release each stream in the list */ @@ -116,7 +116,7 @@ void lib_stream_release(FAR struct task_group_s *group) list->sl_head = stream->fs_next; #ifndef CONFIG_STDIO_DISABLE_BUFFERING - /* Destroy the semaphore that protects the IO buffer */ + /* Destroy the mutex that protects the IO buffer */ nxrmutex_destroy(&stream->fs_lock); #endif @@ -152,22 +152,10 @@ void lib_stream_release(FAR struct task_group_s *group) void lib_stream_semtake(FAR struct streamlist *list) { - int ret; - - /* Take the semaphore (perhaps waiting) */ - - while ((ret = _SEM_WAIT(&list->sl_sem)) < 0) - { - /* The only case that an error should occr here is if - * the wait was awakened by a signal. - */ - - DEBUGASSERT(_SEM_ERRNO(ret) == EINTR || _SEM_ERRNO(ret) == ECANCELED); - UNUSED(ret); - } + nxmutex_lock(&list->sl_lock); } void lib_stream_semgive(FAR struct streamlist *list) { - _SEM_POST(&list->sl_sem); + nxmutex_unlock(&list->sl_lock); } diff --git a/libs/libc/stdio/lib_puts.c b/libs/libc/stdio/lib_puts.c index 859149bad0c3d..841a03faccabf 100644 --- a/libs/libc/stdio/lib_puts.c +++ b/libs/libc/stdio/lib_puts.c @@ -50,7 +50,7 @@ int puts(FAR const IPTR char *s) /* Write the string (the next two steps must be atomic) */ - lib_take_semaphore(stream); + lib_take_lock(stream); /* Write the string without its trailing '\0' */ @@ -80,7 +80,7 @@ int puts(FAR const IPTR char *s) } } - lib_give_semaphore(stdout); + lib_give_lock(stdout); return nput; #else size_t len = strlen(s); diff --git a/libs/libc/stdio/lib_rdflush.c b/libs/libc/stdio/lib_rdflush.c index be7d097dbea7a..926cf75e3b84e 100644 --- a/libs/libc/stdio/lib_rdflush.c +++ b/libs/libc/stdio/lib_rdflush.c @@ -65,7 +65,7 @@ int lib_rdflush(FAR FILE *stream) /* Get exclusive access to the stream */ - lib_take_semaphore(stream); + lib_take_lock(stream); /* If the buffer is currently being used for read access, then discard all * of the read-ahead data. We do not support concurrent buffered read/write @@ -98,12 +98,12 @@ int lib_rdflush(FAR FILE *stream) if (fseek(stream, -rdoffset, SEEK_CUR) < 0) { - lib_give_semaphore(stream); + lib_give_lock(stream); return ERROR; } } - lib_give_semaphore(stream); + lib_give_lock(stream); return OK; } diff --git a/libs/libc/stdio/lib_rewind.c b/libs/libc/stdio/lib_rewind.c index b590930ec66cb..c456e054e36d0 100644 --- a/libs/libc/stdio/lib_rewind.c +++ b/libs/libc/stdio/lib_rewind.c @@ -46,8 +46,8 @@ void rewind(FAR FILE *stream) return; } - lib_take_semaphore(stream); + lib_take_lock(stream); (void) fseek(stream, 0L, SEEK_SET); stream->fs_flags &= ~__FS_FLAG_ERROR; - lib_give_semaphore(stream); + lib_give_lock(stream); } diff --git a/libs/libc/stdio/lib_setvbuf.c b/libs/libc/stdio/lib_setvbuf.c index 5102f9c3f18fc..5bd5db8ae44b7 100644 --- a/libs/libc/stdio/lib_setvbuf.c +++ b/libs/libc/stdio/lib_setvbuf.c @@ -125,7 +125,7 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size) /* Make sure that we have exclusive access to the stream */ - lib_take_semaphore(stream); + lib_take_lock(stream); /* setvbuf() may only be called AFTER the stream has been opened and * BEFORE any operations have been performed on the stream. @@ -136,7 +136,7 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size) if (stream->fs_fd < 0) { errcode = EBADF; - goto errout_with_semaphore; + goto errout_with_lock; } /* Return EBUSY if operations have already been performed on the buffer. @@ -149,7 +149,7 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size) if (stream->fs_bufpos != stream->fs_bufstart) { errcode = EBUSY; - goto errout_with_semaphore; + goto errout_with_lock; } /* Initialize by clearing related flags. We try to avoid any permanent @@ -196,7 +196,7 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size) if (newbuf == NULL) { errcode = ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } } } @@ -246,11 +246,11 @@ int setvbuf(FAR FILE *stream, FAR char *buffer, int mode, size_t size) reuse_buffer: stream->fs_flags = flags; - lib_give_semaphore(stream); + lib_give_lock(stream); return OK; -errout_with_semaphore: - lib_give_semaphore(stream); +errout_with_lock: + lib_give_lock(stream); errout: set_errno(errcode); diff --git a/libs/libc/stdio/lib_vfprintf.c b/libs/libc/stdio/lib_vfprintf.c index 72db5c304113f..97e504cb702eb 100644 --- a/libs/libc/stdio/lib_vfprintf.c +++ b/libs/libc/stdio/lib_vfprintf.c @@ -48,9 +48,9 @@ int vfprintf(FAR FILE *stream, FAR const IPTR char *fmt, va_list ap) * before being pre-empted by the next thread. */ - lib_take_semaphore(stream); + lib_take_lock(stream); n = lib_vsprintf(&stdoutstream.public, fmt, ap); - lib_give_semaphore(stream); + lib_give_lock(stream); return n; } diff --git a/libs/libc/stdio/lib_vfscanf.c b/libs/libc/stdio/lib_vfscanf.c index b76d85faed082..9be80ebf6fea7 100644 --- a/libs/libc/stdio/lib_vfscanf.c +++ b/libs/libc/stdio/lib_vfscanf.c @@ -55,7 +55,7 @@ int vfscanf(FAR FILE *stream, FAR const IPTR char *fmt, va_list ap) * by the next thread. */ - lib_take_semaphore(stream); + lib_take_lock(stream); n = lib_vscanf(&stdinstream.public, &lastc, fmt, ap); @@ -68,7 +68,7 @@ int vfscanf(FAR FILE *stream, FAR const IPTR char *fmt, va_list ap) ungetc(lastc, stream); } - lib_give_semaphore(stream); + lib_give_lock(stream); } return n; diff --git a/libs/libc/stdlib/lib_atexit.c b/libs/libc/stdlib/lib_atexit.c index fe476a806b33b..74f831e3bc724 100644 --- a/libs/libc/stdlib/lib_atexit.c +++ b/libs/libc/stdlib/lib_atexit.c @@ -27,7 +27,7 @@ #include #include -#include +#include #include #if CONFIG_LIBC_MAX_EXITFUNS > 0 @@ -72,11 +72,11 @@ static FAR struct atexit_list_s * get_exitfuncs(void) static int exitfunc_lock(void) { FAR struct task_info_s *info = task_get_info(); - int ret = _SEM_WAIT(&info->ta_sem); + int ret = nxmutex_lock(&info->ta_lock); if (ret < 0) { - ret = _SEM_ERRVAL(ret); + ret = -ret; } return ret; @@ -94,7 +94,7 @@ static void exitfunc_unlock(void) { FAR struct task_info_s *info = task_get_info(); - _SEM_POST(&info->ta_sem); + nxmutex_unlock(&info->ta_lock); } /**************************************************************************** diff --git a/libs/libc/stdlib/lib_mktemp.c b/libs/libc/stdlib/lib_mktemp.c index cfbf7eed1eded..aca6861129e1e 100644 --- a/libs/libc/stdlib/lib_mktemp.c +++ b/libs/libc/stdlib/lib_mktemp.c @@ -35,7 +35,7 @@ #include #include -#include +#include /**************************************************************************** * Pre-processor definitions @@ -65,7 +65,7 @@ ****************************************************************************/ static uint8_t g_base62[MAX_XS]; -static sem_t g_b62sem = SEM_INITIALIZER(1); +static mutex_t g_b62lock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions @@ -132,16 +132,10 @@ static void incr_base62(void) static void get_base62(FAR uint8_t *ptr) { - int ret; - - while ((ret = _SEM_WAIT(&g_b62sem)) < 0) - { - DEBUGASSERT(_SEM_ERRNO(ret) == EINTR || _SEM_ERRNO(ret) == ECANCELED); - } - + nxmutex_lock(&g_b62lock); memcpy(ptr, g_base62, MAX_XS); incr_base62(); - _SEM_POST(&g_b62sem); + nxmutex_unlock(&g_b62lock); } /**************************************************************************** diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c index 0f981b08d37b6..4ac8a3cfcedb9 100644 --- a/libs/libc/time/lib_localtime.c +++ b/libs/libc/time/lib_localtime.c @@ -60,6 +60,7 @@ #include #include +#include #include "libc.h" @@ -324,8 +325,8 @@ static int g_lcl_isset; static int g_gmt_isset; static FAR struct state_s *g_lcl_ptr; static FAR struct state_s *g_gmt_ptr; -static sem_t g_lcl_sem = SEM_INITIALIZER(1); -static sem_t g_gmt_sem = SEM_INITIALIZER(1); +static mutex_t g_lcl_lock = NXMUTEX_INITIALIZER; +static mutex_t g_gmt_lock = NXMUTEX_INITIALIZER; /* Section 4.12.3 of X3.159-1989 requires that * Except for the strftime function, these functions [asctime, @@ -417,28 +418,6 @@ static int tzparse(FAR const char *name, FAR struct state_s *sp, * Private Functions ****************************************************************************/ -static void tz_semtake(FAR sem_t *sem) -{ - int errcode = 0; - int ret; - - do - { - ret = _SEM_WAIT(sem); - if (ret < 0) - { - errcode = _SEM_ERRNO(ret); - DEBUGASSERT(errcode == EINTR || errcode == ECANCELED); - } - } - while (ret < 0 && errcode == EINTR); -} - -static void tz_semgive(FAR sem_t *sem) -{ - DEBUGVERIFY(_SEM_POST(sem)); -} - static int_fast32_t detzcode(FAR const char *codep) { int_fast32_t result; @@ -1795,7 +1774,7 @@ static FAR struct tm *gmtsub(FAR const time_t *timep, } #endif - tz_semtake(&g_gmt_sem); + nxmutex_lock(&g_gmt_lock); if (!g_gmt_isset) { g_gmt_ptr = lib_malloc(sizeof *g_gmt_ptr); @@ -1806,7 +1785,7 @@ static FAR struct tm *gmtsub(FAR const time_t *timep, } } - tz_semgive(&g_gmt_sem); + nxmutex_unlock(&g_gmt_lock); } tmp->tm_zone = GMT; @@ -2513,7 +2492,7 @@ void tzset(void) } #endif - tz_semtake(&g_lcl_sem); + nxmutex_lock(&g_lcl_lock); name = getenv("TZ"); if (name == NULL) { @@ -2565,7 +2544,7 @@ void tzset(void) tzname: settzname(); out: - tz_semgive(&g_lcl_sem); + nxmutex_unlock(&g_lcl_lock); } FAR struct tm *localtime(FAR const time_t *timep) diff --git a/libs/libc/userfs/lib_userfs.c b/libs/libc/userfs/lib_userfs.c index 7847b5d3606f3..3a73670cc5847 100644 --- a/libs/libc/userfs/lib_userfs.c +++ b/libs/libc/userfs/lib_userfs.c @@ -40,7 +40,7 @@ #include #include -#include +#include #include "libc.h" @@ -70,7 +70,7 @@ struct userfs_info_s * not generate unique instance numbers. */ -static sem_t g_userfs_exclsem = SEM_INITIALIZER(1); +static mutex_t g_userfs_lock = NXMUTEX_INITIALIZER; static uint8_t g_userfs_next_instance; /**************************************************************************** @@ -85,7 +85,7 @@ static inline uint16_t userfs_server_portno(void) { int ret; - ret = _SEM_WAIT(&g_userfs_exclsem); + ret = nxmutex_lock(&g_userfs_lock); if (ret >= 0) { /* Get the next instance number. @@ -96,7 +96,7 @@ static inline uint16_t userfs_server_portno(void) */ ret = USERFS_SERVER_PORTBASE | g_userfs_next_instance++; - _SEM_POST(&g_userfs_exclsem); + nxmutex_unlock(&g_userfs_lock); } return ret; diff --git a/libs/libc/wqueue/work_cancel.c b/libs/libc/wqueue/work_cancel.c index 39a2d97430e93..760267d29c5cd 100644 --- a/libs/libc/wqueue/work_cancel.c +++ b/libs/libc/wqueue/work_cancel.c @@ -72,7 +72,7 @@ static int work_qcancel(FAR struct usr_wqueue_s *wqueue, /* Get exclusive access to the work queue */ - while (_SEM_WAIT(&wqueue->lock) < 0); + while (nxmutex_lock(&wqueue->lock) < 0); /* Cancelling the work is simply a matter of removing the work structure * from the work queue. This must be done with interrupts disabled because @@ -123,7 +123,7 @@ static int work_qcancel(FAR struct usr_wqueue_s *wqueue, ret = OK; } - _SEM_POST(&wqueue->lock); + nxmutex_unlock(&wqueue->lock); return ret; } diff --git a/libs/libc/wqueue/work_queue.c b/libs/libc/wqueue/work_queue.c index b2f61a5cd8aec..145d5f81fc777 100644 --- a/libs/libc/wqueue/work_queue.c +++ b/libs/libc/wqueue/work_queue.c @@ -32,7 +32,6 @@ #include #include #include -#include #include "wqueue/wqueue.h" @@ -82,7 +81,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue, /* Get exclusive access to the work queue */ - while (_SEM_WAIT(&wqueue->lock) < 0); + while (nxmutex_lock(&wqueue->lock) < 0); /* Initialize the work structure */ @@ -142,7 +141,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue, } } - _SEM_POST(&wqueue->lock); + nxmutex_unlock(&wqueue->lock); return OK; } diff --git a/libs/libc/wqueue/work_usrthread.c b/libs/libc/wqueue/work_usrthread.c index 27f69c90eb393..9b5e68cfa3ad4 100644 --- a/libs/libc/wqueue/work_usrthread.c +++ b/libs/libc/wqueue/work_usrthread.c @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -97,7 +96,7 @@ static void work_process(FAR struct usr_wqueue_s *wqueue) */ next = WORK_DELAY_MAX; - ret = _SEM_WAIT(&wqueue->lock); + ret = nxmutex_lock(&wqueue->lock); if (ret < 0) { /* Break out earlier if we were awakened by a signal */ @@ -153,7 +152,7 @@ static void work_process(FAR struct usr_wqueue_s *wqueue) * performed... we don't have any idea how long this will take! */ - _SEM_POST(&wqueue->lock); + nxmutex_unlock(&wqueue->lock); worker(arg); /* Now, unfortunately, since we unlocked the work queue we @@ -161,7 +160,7 @@ static void work_process(FAR struct usr_wqueue_s *wqueue) * start back at the head of the list. */ - ret = _SEM_WAIT(&wqueue->lock); + ret = nxmutex_lock(&wqueue->lock); if (ret < 0) { /* Break out earlier if we were awakened by a signal */ @@ -181,13 +180,13 @@ static void work_process(FAR struct usr_wqueue_s *wqueue) /* Unlock the work queue before waiting. */ - _SEM_POST(&wqueue->lock); + nxmutex_unlock(&wqueue->lock); if (next == WORK_DELAY_MAX) { /* Wait indefinitely until work_queue has new items */ - _SEM_WAIT(&wqueue->wake); + nxmutex_lock(&wqueue->wake); } else { @@ -281,7 +280,7 @@ int work_usrstart(void) /* Set up the work queue lock */ - _SEM_INIT(&g_usrwork.lock, 0, 1); + nxmutex_init(&g_usrwork.lock); _SEM_INIT(&g_usrwork.wake, 0, 0); _SEM_SETPROTOCOL(&g_usrwork.wake, SEM_PRIO_NONE); diff --git a/libs/libc/wqueue/wqueue.h b/libs/libc/wqueue/wqueue.h index 1d6280288c478..6a58b5052dfad 100644 --- a/libs/libc/wqueue/wqueue.h +++ b/libs/libc/wqueue/wqueue.h @@ -27,9 +27,10 @@ #include -#include #include +#include +#include #include #if defined(CONFIG_LIBC_USRWORK) && !defined(__KERNEL__) @@ -47,7 +48,7 @@ struct usr_wqueue_s { struct dq_queue_s q; /* The queue of pending work */ - sem_t lock; /* exclusive access to user-mode work queue */ + mutex_t lock; /* exclusive access to user-mode work queue */ sem_t wake; /* The wake-up semaphore of the usrthread */ }; diff --git a/libs/libnx/nxfonts/nxfonts_cache.c b/libs/libnx/nxfonts/nxfonts_cache.c index a08ceeecc79e3..d022e2963d930 100644 --- a/libs/libnx/nxfonts/nxfonts_cache.c +++ b/libs/libnx/nxfonts/nxfonts_cache.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include "nxcontext.h" @@ -53,7 +53,7 @@ struct nxfonts_fcache_s { FAR struct nxfonts_fcache_s *flink; /* Supports a singly linked list */ NXHANDLE font; /* Font handle associated with fontid */ - sem_t fsem; /* Serializes access to the font cache */ + mutex_t flock; /* Serializes access to the font cache */ uint16_t fontid; /* ID of font in this cache */ int16_t fclients; /* Number of connected clients */ uint8_t maxglyphs; /* Maximum size of glyph[] array */ @@ -76,36 +76,12 @@ struct nxfonts_fcache_s /* Head of a list of font caches */ static FAR struct nxfonts_fcache_s *g_fcaches; -static sem_t g_cachesem = SEM_INITIALIZER(1); +static mutex_t g_cachelock = NXMUTEX_INITIALIZER; /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: nxf_list_lock and nxf_list_unlock - * - * Description: - * Get/relinquish exclusive access to the font cache list - * - ****************************************************************************/ - -static void nxf_list_lock(void) -{ - int ret; - - /* Get exclusive access to the font cache */ - - while ((ret = _SEM_WAIT(&g_cachesem)) < 0) - { - int errorcode = _SEM_ERRNO(ret); - DEBUGASSERT(errorcode == EINTR || errorcode == ECANCELED); - UNUSED(errorcode); - } -} - -#define nxf_list_unlock() (_SEM_POST(&g_cachesem)) - /**************************************************************************** * Name: nxf_removecache * @@ -113,7 +89,7 @@ static void nxf_list_lock(void) * Removes the entry 'glyph' from the font cache. * * Assumptions: - * The caller holds the font cache list semaphore. + * The caller holds the font cache list lock. * ****************************************************************************/ @@ -139,30 +115,6 @@ static inline void nxf_removecache(FAR struct nxfonts_fcache_s *fcache, fcache->flink = NULL; } -/**************************************************************************** - * Name: nxf_cache_lock and nxf_cache_unlock - * - * Description: - * Get/relinquish exclusive access to the font cache - * - ****************************************************************************/ - -static void nxf_cache_lock(FAR struct nxfonts_fcache_s *priv) -{ - int ret; - - /* Get exclusive access to the font cache */ - - while ((ret = _SEM_WAIT(&priv->fsem)) < 0) - { - int errorcode = _SEM_ERRNO(ret); - DEBUGASSERT(errorcode == EINTR || errorcode == ECANCELED); - UNUSED(errorcode); - } -} - -#define nxf_cache_unlock(p) (_SEM_POST(&priv->fsem)) - /**************************************************************************** * Name: nxf_removeglyph * @@ -273,7 +225,7 @@ static inline void nxf_addglyph(FAR struct nxfonts_fcache_s *priv, ****************************************************************************/ static FAR struct nxfonts_glyph_s * - nxf_findglyph(FAR struct nxfonts_fcache_s *priv, uint8_t ch) +nxf_findglyph(FAR struct nxfonts_fcache_s *priv, uint8_t ch) { FAR struct nxfonts_glyph_s *glyph; FAR struct nxfonts_glyph_s *prev; @@ -501,13 +453,13 @@ static inline void nxf_fillglyph(FAR struct nxfonts_fcache_s *priv, * allocated glyph memory. * * Assumptions: - * The caller holds the font cache semaphore. + * The caller holds the font cache lock. * ****************************************************************************/ static inline FAR struct nxfonts_glyph_s * - nxf_renderglyph(FAR struct nxfonts_fcache_s *priv, - FAR const struct nx_fontbitmap_s *fbm, uint8_t ch) +nxf_renderglyph(FAR struct nxfonts_fcache_s *priv, + FAR const struct nx_fontbitmap_s *fbm, uint8_t ch) { FAR struct nxfonts_glyph_s *glyph = NULL; size_t bmsize; @@ -578,13 +530,13 @@ static inline FAR struct nxfonts_glyph_s * * Find a font cache tht matches the font charcteristics. * * Assumptions: - * The caller holds the font cache list semaphore. + * The caller holds the font cache list lock. * ****************************************************************************/ static FAR struct nxfonts_fcache_s * - nxf_findcache(enum nx_fontid_e fontid, nxgl_mxpixel_t fgcolor, - nxgl_mxpixel_t bgcolor, int bpp) +nxf_findcache(enum nx_fontid_e fontid, nxgl_mxpixel_t fgcolor, + nxgl_mxpixel_t bgcolor, int bpp) { FAR struct nxfonts_fcache_s *fcache; @@ -652,7 +604,7 @@ FCACHE nxf_cache_connect(enum nx_fontid_e fontid, /* Get exclusive access to the font cache list */ - nxf_list_lock(); + nxmutex_lock(&g_cachelock); /* Find a font cache with the matching font characteristics */ @@ -743,9 +695,9 @@ FCACHE nxf_cache_connect(enum nx_fontid_e fontid, goto errout_with_fcache; } - /* Initialize the mutual exclusion semaphore */ + /* Initialize the mutual exclusion mutex */ - _SEM_INIT(&priv->fsem, 0, 1); + nxmutex_init(&priv->flock); /* Add the new font cache to the list of font caches */ @@ -768,7 +720,7 @@ FCACHE nxf_cache_connect(enum nx_fontid_e fontid, priv->fclients++; } - nxf_list_unlock(); + nxmutex_unlock(&g_cachelock); ginfo("fhandle=%p\n", priv); return (FCACHE)priv; @@ -776,7 +728,7 @@ FCACHE nxf_cache_connect(enum nx_fontid_e fontid, lib_free(priv); errout_with_lock: - nxf_list_unlock(); + nxmutex_unlock(&g_cachelock); set_errno(errcode); return NULL; } @@ -812,7 +764,7 @@ void nxf_cache_disconnect(FCACHE fhandle) /* Get exclusive access to the font cache */ - nxf_cache_lock(priv); + nxmutex_lock(&priv->flock); /* Is this the last client of the font cache? */ @@ -820,7 +772,7 @@ void nxf_cache_disconnect(FCACHE fhandle) { /* Get exclusive access to the font cache list */ - nxf_list_lock(); + nxmutex_lock(&g_cachelock); /* Remove the font cache from the list of caches. This is a singly * linked list, so we must do this the hard way. @@ -834,7 +786,7 @@ void nxf_cache_disconnect(FCACHE fhandle) DEBUGASSERT(fcache == priv); nxf_removecache(fcache, prev); - nxf_list_unlock(); + nxmutex_unlock(&g_cachelock); /* Free all allocated glyph memory */ @@ -844,9 +796,9 @@ void nxf_cache_disconnect(FCACHE fhandle) lib_free(glyph); } - /* Destroy the serializing semaphore... while we are holding it? */ + /* Destroy the serializing lock... while we are holding it? */ - _SEM_DESTROY(&priv->fsem); + nxmutex_destroy(&priv->flock); /* Finally, free the font cache structure itself */ @@ -859,7 +811,7 @@ void nxf_cache_disconnect(FCACHE fhandle) */ priv->fclients--; - nxf_cache_unlock(priv); + nxmutex_unlock(&priv->flock); } } @@ -905,7 +857,7 @@ NXHANDLE nxf_cache_getfonthandle(FCACHE fhandle) ****************************************************************************/ FAR const struct nxfonts_glyph_s * - nxf_cache_getglyph(FCACHE fhandle, uint8_t ch) +nxf_cache_getglyph(FCACHE fhandle, uint8_t ch) { FAR struct nxfonts_fcache_s *priv = (FAR struct nxfonts_fcache_s *)fhandle; FAR struct nxfonts_glyph_s *glyph; @@ -915,7 +867,7 @@ FAR const struct nxfonts_glyph_s * /* Get exclusive access to the font cache */ - nxf_cache_lock(priv); + nxmutex_lock(&priv->flock); /* First, try to find the glyph in the cache of pre-rendered glyphs */ @@ -933,6 +885,6 @@ FAR const struct nxfonts_glyph_s * } } - nxf_cache_unlock(priv); + nxmutex_unlock(&priv->flock); return glyph; } diff --git a/libs/libnx/nxmu/Make.defs b/libs/libnx/nxmu/Make.defs index 1d9068ecd45f7..1a103827c5126 100644 --- a/libs/libnx/nxmu/Make.defs +++ b/libs/libnx/nxmu/Make.defs @@ -25,9 +25,8 @@ ifeq ($(CONFIG_NX),y) CSRCS += nxmu_sendserver.c nx_connect.c nx_disconnect.c -CSRCS += nx_eventhandler.c nx_eventnotify.c nxmu_semtake.c -CSRCS += nx_block.c nx_synch.c -CSRCS += nx_kbdchin.c nx_kbdin.c nx_mousein.c +CSRCS += nx_eventhandler.c nx_eventnotify.c nx_block.c +CSRCS += nx_synch.c nx_kbdchin.c nx_kbdin.c nx_mousein.c CSRCS += nx_releasebkgd.c nx_requestbkgd.c nx_setbgcolor.c CSRCS += nxmu_sendwindow.c nx_closewindow.c nx_constructwindow.c diff --git a/libs/libnx/nxmu/nx_connect.c b/libs/libnx/nxmu/nx_connect.c index 59f2eddf8d6d2..08762685e30b8 100644 --- a/libs/libnx/nxmu/nx_connect.c +++ b/libs/libnx/nxmu/nx_connect.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -51,8 +52,8 @@ * NOTE: that client ID 0 is reserved for the server(s) themselves */ -static sem_t g_nxlibsem = SEM_INITIALIZER(1); -static uint32_t g_nxcid = 1; +static mutex_t g_nxliblock = NXMUTEX_INITIALIZER; +static uint32_t g_nxcid = 1; /**************************************************************************** * Public Functions @@ -111,9 +112,9 @@ NXHANDLE nx_connectinstance(FAR const char *svrmqname) /* Create the client MQ name */ - nxmu_semtake(&g_nxlibsem); + nxmutex_lock(&g_nxliblock); conn->cid = g_nxcid++; - nxmu_semgive(&g_nxlibsem); + nxmutex_unlock(&g_nxliblock); sprintf(climqname, NX_CLIENT_MQNAMEFMT, conn->cid); diff --git a/libs/libnx/nxmu/nxmu_semtake.c b/libs/libnx/nxmu/nxmu_semtake.c deleted file mode 100644 index 33f69798a10f9..0000000000000 --- a/libs/libnx/nxmu/nxmu_semtake.c +++ /dev/null @@ -1,55 +0,0 @@ -/**************************************************************************** - * libs/libnx/nxmu/nxmu_semtake.c - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include - -#include -#include - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: nxmu_semtake - ****************************************************************************/ - -void nxmu_semtake(sem_t *sem) -{ - int ret; - - while ((ret = _SEM_WAIT(sem)) < 0) - { - /* The only case that an error should occur here is if the wait was - * awakened by a signal. - */ - - DEBUGASSERT(_SEM_ERRNO(ret) == EINTR || _SEM_ERRNO(ret) == ECANCELED); - UNUSED(ret); - } -} diff --git a/mm/iob/iob_initialize.c b/mm/iob/iob_initialize.c index 5d3cbbb57c5f3..7d3c3b72c42cf 100644 --- a/mm/iob/iob_initialize.c +++ b/mm/iob/iob_initialize.c @@ -87,19 +87,22 @@ FAR struct iob_qentry_s *g_iob_qcommitted; /* Counting semaphores that tracks the number of free IOBs/qentries */ -sem_t g_iob_sem = SEM_INITIALIZER(CONFIG_IOB_NBUFFERS); +sem_t g_iob_sem = NXSEM_INITIALIZER(CONFIG_IOB_NBUFFERS, + PRIOINHERIT_FLAGS_DISABLE); #if CONFIG_IOB_THROTTLE > 0 /* Counts available I/O buffers when throttled */ -sem_t g_throttle_sem = SEM_INITIALIZER(CONFIG_IOB_NBUFFERS - - CONFIG_IOB_THROTTLE); +sem_t g_throttle_sem = NXSEM_INITIALIZER(CONFIG_IOB_NBUFFERS - + CONFIG_IOB_THROTTLE, + PRIOINHERIT_FLAGS_DISABLE); #endif #if CONFIG_IOB_NCHAINS > 0 /* Counts free I/O buffer queue containers */ -sem_t g_qentry_sem = SEM_INITIALIZER(CONFIG_IOB_NCHAINS); +sem_t g_qentry_sem = NXSEM_INITIALIZER(CONFIG_IOB_NCHAINS, + PRIOINHERIT_FLAGS_DISABLE); #endif /**************************************************************************** diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c index d9684b4959ace..aec0e20c2b887 100644 --- a/mm/kasan/kasan.c +++ b/mm/kasan/kasan.c @@ -22,7 +22,7 @@ * Included Files ****************************************************************************/ -#include +#include #include #include @@ -68,7 +68,7 @@ struct kasan_region_s * Private Data ****************************************************************************/ -static sem_t g_lock = SEM_INITIALIZER(1); +static mutex_t g_lock = NXMUTEX_INITIALIZER; static FAR struct kasan_region_s *g_region; static uint32_t g_region_init; @@ -199,11 +199,11 @@ void kasan_register(FAR void *addr, FAR size_t *size) region->begin = (uintptr_t)addr; region->end = region->begin + *size; - _SEM_WAIT(&g_lock); + nxmutex_lock(&g_lock); region->next = g_region; g_region = region; g_region_init = KASAN_INIT_VALUE; - _SEM_POST(&g_lock); + nxmutex_unlock(&g_lock); kasan_poison(addr, *size); *size -= KASAN_REGION_SIZE(*size); diff --git a/mm/mm_gran/mm_gran.h b/mm/mm_gran/mm_gran.h index 31ae6d95753e7..b9fe04bebace0 100644 --- a/mm/mm_gran/mm_gran.h +++ b/mm/mm_gran/mm_gran.h @@ -31,7 +31,7 @@ #include #include -#include +#include /**************************************************************************** * Pre-processor Definitions @@ -69,7 +69,7 @@ struct gran_s #ifdef CONFIG_GRAN_INTR irqstate_t irqstate; /* For exclusive access to the GAT */ #else - sem_t exclsem; /* For exclusive access to the GAT */ + mutex_t lock; /* For exclusive access to the GAT */ #endif uintptr_t heapstart; /* The aligned start of the granule heap */ uint32_t gat[1]; /* Start of the granule allocation table */ diff --git a/mm/mm_gran/mm_grancritical.c b/mm/mm_gran/mm_grancritical.c index 13c14aaa7ea88..5a99a89a6decb 100644 --- a/mm/mm_gran/mm_grancritical.c +++ b/mm/mm_gran/mm_grancritical.c @@ -60,7 +60,7 @@ int gran_enter_critical(FAR struct gran_s *priv) priv->irqstate = enter_critical_section(); return OK; #else - return nxsem_wait_uninterruptible(&priv->exclsem); + return nxmutex_lock(&priv->lock); #endif } @@ -69,7 +69,7 @@ void gran_leave_critical(FAR struct gran_s *priv) #ifdef CONFIG_GRAN_INTR leave_critical_section(priv->irqstate); #else - nxsem_post(&priv->exclsem); + nxmutex_unlock(&priv->lock); #endif } diff --git a/mm/mm_gran/mm_graninit.c b/mm/mm_gran/mm_graninit.c index 1dd50ae318dc2..9170a78840758 100644 --- a/mm/mm_gran/mm_graninit.c +++ b/mm/mm_gran/mm_graninit.c @@ -138,7 +138,7 @@ GRAN_HANDLE gran_initialize(FAR void *heapstart, size_t heapsize, /* Initialize mutual exclusion support */ #ifndef CONFIG_GRAN_INTR - nxsem_init(&priv->exclsem, 0, 1); + nxmutex_init(&priv->lock); #endif } diff --git a/mm/mm_gran/mm_granrelease.c b/mm/mm_gran/mm_granrelease.c index 2953931804e56..67c3c444079f8 100644 --- a/mm/mm_gran/mm_granrelease.c +++ b/mm/mm_gran/mm_granrelease.c @@ -60,7 +60,7 @@ void gran_release(GRAN_HANDLE handle) DEBUGASSERT(priv != NULL); #ifndef CONFIG_GRAN_INTR - nxsem_destroy(&priv->exclsem); + nxmutex_destroy(&priv->lock); #endif kmm_free(priv); } diff --git a/mm/mm_heap/Make.defs b/mm/mm_heap/Make.defs index fdeb15dbae71c..88d682759e239 100644 --- a/mm/mm_heap/Make.defs +++ b/mm/mm_heap/Make.defs @@ -22,7 +22,7 @@ ifeq ($(CONFIG_MM_DEFAULT_MANAGER),y) -CSRCS += mm_initialize.c mm_sem.c mm_addfreechunk.c mm_size2ndx.c +CSRCS += mm_initialize.c mm_lock.c mm_addfreechunk.c mm_size2ndx.c CSRCS += mm_malloc_size.c mm_shrinkchunk.c mm_brkaddr.c mm_calloc.c CSRCS += mm_extend.c mm_free.c mm_mallinfo.c mm_malloc.c mm_foreach.c CSRCS += mm_memalign.c mm_realloc.c mm_zalloc.c mm_heapmember.c mm_memdump.c diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h index f82bc308816d5..a53b63aa60ebc 100644 --- a/mm/mm_heap/mm.h +++ b/mm/mm_heap/mm.h @@ -27,6 +27,7 @@ #include +#include #include #include @@ -34,7 +35,6 @@ #include #include #include -#include #include /**************************************************************************** @@ -217,10 +217,10 @@ struct mm_delaynode_s struct mm_heap_s { /* Mutually exclusive access to this data set is enforced with - * the following un-named semaphore. + * the following un-named mutex. */ - sem_t mm_semaphore; + sem_t mm_lock; /* This is the size of the heap provided to mm */ @@ -262,12 +262,10 @@ typedef CODE void (*mmchunk_handler_t)(FAR struct mm_allocnode_s *node, * Public Function Prototypes ****************************************************************************/ -/* Functions contained in mm_sem.c ******************************************/ +/* Functions contained in mm_lock.c *****************************************/ -void mm_seminitialize(FAR struct mm_heap_s *heap); -void mm_semuninitialize(FAR struct mm_heap_s *heap); -bool mm_takesemaphore(FAR struct mm_heap_s *heap); -void mm_givesemaphore(FAR struct mm_heap_s *heap); +bool mm_lock(FAR struct mm_heap_s *heap); +void mm_unlock(FAR struct mm_heap_s *heap); /* Functions contained in mm_shrinkchunk.c **********************************/ diff --git a/mm/mm_heap/mm_addfreechunk.c b/mm/mm_heap/mm_addfreechunk.c index 22df38631ab72..77f8f2e4f731c 100644 --- a/mm/mm_heap/mm_addfreechunk.c +++ b/mm/mm_heap/mm_addfreechunk.c @@ -39,7 +39,7 @@ * * Description: * Add a free chunk to the nodes list. It is assumed that the caller holds - * the mm semaphore. + * the mm mutex. * ****************************************************************************/ diff --git a/mm/mm_heap/mm_extend.c b/mm/mm_heap/mm_extend.c index 230845761543a..bdfee0293d7a6 100644 --- a/mm/mm_heap/mm_extend.c +++ b/mm/mm_heap/mm_extend.c @@ -76,9 +76,9 @@ void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size, DEBUGASSERT(MM_ALIGN_UP(blockstart) == blockstart); DEBUGASSERT(MM_ALIGN_DOWN(blockend) == blockend); - /* Take the memory manager semaphore */ + /* Take the memory manager mutex */ - ret = mm_takesemaphore(heap); + ret = mm_lock(heap); DEBUGASSERT(ret); /* Get the terminal node in the old heap. The block to extend must @@ -108,7 +108,7 @@ void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size, newnode->preceding = oldnode->size | MM_ALLOC_BIT; heap->mm_heapend[region] = newnode; - mm_givesemaphore(heap); + mm_unlock(heap); /* Finally "free" the new block of memory where the old terminal node was * located. diff --git a/mm/mm_heap/mm_foreach.c b/mm/mm_heap/mm_foreach.c index f2ad78bc2669b..70cf1579c002c 100644 --- a/mm/mm_heap/mm_foreach.c +++ b/mm/mm_heap/mm_foreach.c @@ -65,10 +65,10 @@ void mm_foreach(FAR struct mm_heap_s *heap, mmchunk_handler_t handler, prev = NULL; /* Visit each node in the region - * Retake the semaphore for each region to reduce latencies + * Retake the mutex for each region to reduce latencies */ - if (!mm_takesemaphore(heap)) + if (!mm_lock(heap)) { return; } @@ -94,7 +94,7 @@ void mm_foreach(FAR struct mm_heap_s *heap, mmchunk_handler_t handler, region, node, heap->mm_heapend[region]); DEBUGASSERT(node == heap->mm_heapend[region]); - mm_givesemaphore(heap); + mm_unlock(heap); } #undef region } diff --git a/mm/mm_heap/mm_free.c b/mm/mm_heap/mm_free.c index e281ec919ac97..dcb89fe05f27b 100644 --- a/mm/mm_heap/mm_free.c +++ b/mm/mm_heap/mm_free.c @@ -84,10 +84,10 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem) return; } - if (mm_takesemaphore(heap) == false) + if (mm_lock(heap) == false) { /* Meet -ESRCH return, which means we are in situations - * during context switching(See mm_takesemaphore() & getpid()). + * during context switching(See mm_lock() & getpid()). * Then add to the delay list. */ @@ -173,6 +173,5 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem) /* Add the merged node to the nodelist */ mm_addfreechunk(heap, node); - - mm_givesemaphore(heap); + mm_unlock(heap); } diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c index 1204ec9c5fc09..d0459175fbb7c 100644 --- a/mm/mm_heap/mm_initialize.c +++ b/mm/mm_heap/mm_initialize.c @@ -92,7 +92,7 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, kasan_register(heapstart, &heapsize); - ret = mm_takesemaphore(heap); + ret = mm_lock(heap); DEBUGASSERT(ret); /* Adjust the provided heap start and size. @@ -152,8 +152,7 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, /* Add the single, large free node to the nodelist */ mm_addfreechunk(heap, node); - - mm_givesemaphore(heap); + mm_unlock(heap); } /**************************************************************************** @@ -212,11 +211,11 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name, heap->mm_nodelist[i].blink = &heap->mm_nodelist[i - 1]; } - /* Initialize the malloc semaphore to one (to support one-at- + /* Initialize the malloc mutex to one (to support one-at- * a-time access to private data sets). */ - mm_seminitialize(heap); + nxmutex_init(&heap->mm_lock); #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO) # if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) @@ -262,5 +261,5 @@ void mm_uninitialize(FAR struct mm_heap_s *heap) procfs_unregister_meminfo(&heap->mm_procfs); # endif #endif - mm_semuninitialize(heap); + nxmutex_destroy(&heap->mm_lock); } diff --git a/mm/mm_heap/mm_sem.c b/mm/mm_heap/mm_lock.c similarity index 60% rename from mm/mm_heap/mm_sem.c rename to mm/mm_heap/mm_lock.c index a60c91a7d5176..b77ad77a0da84 100644 --- a/mm/mm_heap/mm_sem.c +++ b/mm/mm_heap/mm_lock.c @@ -1,5 +1,5 @@ /**************************************************************************** - * mm/mm_heap/mm_sem.c + * mm/mm_heap/mm_lock.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -30,7 +30,6 @@ #include #include -#include #include #include "mm_heap/mm.h" @@ -40,55 +39,23 @@ ****************************************************************************/ /**************************************************************************** - * Name: mm_seminitialize - * - * Description: - * Initialize the MM mutex - * - ****************************************************************************/ - -void mm_seminitialize(FAR struct mm_heap_s *heap) -{ - /* Initialize the MM semaphore to one (to support one-at-a-time access to - * private data sets). - */ - - _SEM_INIT(&heap->mm_semaphore, 0, 1); -} - -/**************************************************************************** - * Name: mm_seminitialize - * - * Description: - * Uninitialize the MM mutex - * - ****************************************************************************/ - -void mm_semuninitialize(FAR struct mm_heap_s *heap) -{ - /* Uninitialize the MM semaphore */ - - _SEM_DESTROY(&heap->mm_semaphore); -} - -/**************************************************************************** - * Name: mm_takesemaphore + * Name: mm_lock * * Description: * Take the MM mutex. This may be called from the OS in certain conditions - * when it is impossible to wait on a semaphore: + * when it is impossible to wait on a mutex: * 1.The idle process performs the memory corruption check. * 2.The task/thread free the memory in the exiting process. * * Input Parameters: - * heap - heap instance want to take semaphore + * heap - heap instance want to take mutex * * Returned Value: - * true if the semaphore can be taken, otherwise false. + * true if the lock can be taken, otherwise false. * ****************************************************************************/ -bool mm_takesemaphore(FAR struct mm_heap_s *heap) +bool mm_lock(FAR struct mm_heap_s *heap) { #if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) /* Check current environment */ @@ -96,17 +63,13 @@ bool mm_takesemaphore(FAR struct mm_heap_s *heap) if (up_interrupt_context()) { #if !defined(CONFIG_SMP) - int val; - - /* Check the semaphore value, if held by someone, then return false. + /* Check the mutex value, if held by someone, then return false. * Or, touch the heap internal data directly. */ - _SEM_GETVALUE(&heap->mm_semaphore, &val); - - return val > 0; + return !nxmutex_is_locked(&heap->mm_lock); #else - /* Can't take semaphore in SMP interrupt handler */ + /* Can't take mutex in SMP interrupt handler */ return false; #endif @@ -115,7 +78,7 @@ bool mm_takesemaphore(FAR struct mm_heap_s *heap) #endif /* getpid() returns the task ID of the task at the head of the ready-to- - * run task list. mm_takesemaphore() may be called during context + * run task list. mm_lock() may be called during context * switches. There are certain situations during context switching when * the OS data structures are in flux and then can't be freed immediately * (e.g. the running thread stack). @@ -130,39 +93,19 @@ bool mm_takesemaphore(FAR struct mm_heap_s *heap) } else { - int ret; - - /* Take the semaphore (perhaps waiting) */ - - do - { - ret = _SEM_WAIT(&heap->mm_semaphore); - - /* The only case that an error should occur here is if the wait - * was awakened by a signal. - */ - - if (ret < 0) - { - ret = _SEM_ERRVAL(ret); - DEBUGASSERT(ret == -EINTR || ret == -ECANCELED); - } - } - while (ret < 0); - - return true; + return nxmutex_lock(&heap->mm_lock) >= 0; } } /**************************************************************************** - * Name: mm_givesemaphore + * Name: mm_unlock * * Description: * Release the MM mutex when it is not longer needed. * ****************************************************************************/ -void mm_givesemaphore(FAR struct mm_heap_s *heap) +void mm_unlock(FAR struct mm_heap_s *heap) { #if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) if (up_interrupt_context()) @@ -171,5 +114,5 @@ void mm_givesemaphore(FAR struct mm_heap_s *heap) } #endif - DEBUGVERIFY(_SEM_POST(&heap->mm_semaphore)); + DEBUGVERIFY(nxmutex_unlock(&heap->mm_lock)); } diff --git a/mm/mm_heap/mm_malloc.c b/mm/mm_heap/mm_malloc.c index de9542be5ee72..c1d52b23c983e 100644 --- a/mm/mm_heap/mm_malloc.c +++ b/mm/mm_heap/mm_malloc.c @@ -136,9 +136,9 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size) DEBUGASSERT(alignsize >= MM_MIN_CHUNK); DEBUGASSERT(alignsize >= SIZEOF_MM_FREENODE); - /* We need to hold the MM semaphore while we muck with the nodelist. */ + /* We need to hold the MM mutex while we muck with the nodelist. */ - val = mm_takesemaphore(heap); + val = mm_lock(heap); DEBUGASSERT(val); /* Get the location in the node list to start the search. Special case @@ -235,7 +235,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size) } DEBUGASSERT(ret == NULL || mm_heapmember(heap, ret)); - mm_givesemaphore(heap); + mm_unlock(heap); if (ret) { diff --git a/mm/mm_heap/mm_memalign.c b/mm/mm_heap/mm_memalign.c index 2f92f53a28554..4608aa18c698d 100644 --- a/mm/mm_heap/mm_memalign.c +++ b/mm/mm_heap/mm_memalign.c @@ -115,11 +115,11 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment, kasan_poison((FAR void *)rawchunk, mm_malloc_size((FAR void *)rawchunk)); - /* We need to hold the MM semaphore while we muck with the chunks and + /* We need to hold the MM mutex while we muck with the chunks and * nodelist. */ - ret = mm_takesemaphore(heap); + ret = mm_lock(heap); DEBUGASSERT(ret); /* Get the node associated with the allocation and the next node after @@ -223,7 +223,7 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment, mm_shrinkchunk(heap, node, size); } - mm_givesemaphore(heap); + mm_unlock(heap); MM_ADD_BACKTRACE(heap, node); diff --git a/mm/mm_heap/mm_realloc.c b/mm/mm_heap/mm_realloc.c index a1ef1bd2893d0..11a937a1a218e 100644 --- a/mm/mm_heap/mm_realloc.c +++ b/mm/mm_heap/mm_realloc.c @@ -107,9 +107,9 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem, oldnode = (FAR struct mm_allocnode_s *) ((FAR char *)oldmem - SIZEOF_MM_ALLOCNODE); - /* We need to hold the MM semaphore while we muck with the nodelist. */ + /* We need to hold the MM mutex while we muck with the nodelist. */ - ret = mm_takesemaphore(heap); + ret = mm_lock(heap); DEBUGASSERT(ret); DEBUGASSERT(oldnode->preceding & MM_ALLOC_BIT); DEBUGASSERT(mm_heapmember(heap, oldmem)); @@ -132,8 +132,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem, /* Then return the original address */ - mm_givesemaphore(heap); - + mm_unlock(heap); MM_ADD_BACKTRACE(heap, oldnode); return oldmem; @@ -337,8 +336,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem, } } - mm_givesemaphore(heap); - + mm_unlock(heap); MM_ADD_BACKTRACE(heap, (FAR char *)newmem - SIZEOF_MM_ALLOCNODE); kasan_unpoison(newmem, mm_malloc_size(newmem)); @@ -364,7 +362,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem, * leave the original memory in place. */ - mm_givesemaphore(heap); + mm_unlock(heap); newmem = mm_malloc(heap, size); if (newmem) { diff --git a/mm/mm_heap/mm_shrinkchunk.c b/mm/mm_heap/mm_shrinkchunk.c index 0fd35fe58f9aa..1743e5468710e 100644 --- a/mm/mm_heap/mm_shrinkchunk.c +++ b/mm/mm_heap/mm_shrinkchunk.c @@ -45,7 +45,7 @@ * * NOTES: * (1) size is the whole chunk size (payload and header) - * (2) the caller must hold the MM semaphore. + * (2) the caller must hold the MM mutex. * ****************************************************************************/ diff --git a/mm/shm/shm.h b/mm/shm/shm.h index 97045afc4fbd0..591790eabe96c 100644 --- a/mm/shm/shm.h +++ b/mm/shm/shm.h @@ -32,7 +32,7 @@ #include #include -#include +#include #ifdef CONFIG_MM_SHM @@ -56,10 +56,10 @@ struct shm_region_s { - struct shmid_ds sr_ds; /* Region info */ - bool sr_flags; /* See SRFLAGS_* definitions */ - key_t sr_key; /* Lookup key */ - sem_t sr_sem; /* Manages exclusive access to this region */ + struct shmid_ds sr_ds; /* Region info */ + bool sr_flags; /* See SRFLAGS_* definitions */ + key_t sr_key; /* Lookup key */ + mutex_t sr_lock; /* Manages exclusive access to this region */ /* List of physical pages allocated for this memory region */ @@ -72,7 +72,7 @@ struct shm_region_s struct shm_info_s { - sem_t si_sem; /* Manages exclusive access to the region list */ + mutex_t si_lock; /* Manages exclusive access to the region list */ struct shm_region_s si_region[CONFIG_ARCH_SHM_MAXREGIONS]; }; @@ -108,8 +108,8 @@ extern struct shm_info_s g_shminfo; * None * * Assumption: - * The caller holds either the region table semaphore or else the - * semaphore on the particular entry being deleted. + * The caller holds either the region table mutex or else the + * mutex on the particular entry being deleted. * ****************************************************************************/ diff --git a/mm/shm/shm_initialize.c b/mm/shm/shm_initialize.c index 1e49bcbe6e680..af93d6dcde466 100644 --- a/mm/shm/shm_initialize.c +++ b/mm/shm/shm_initialize.c @@ -45,7 +45,7 @@ struct shm_info_s g_shminfo = { - SEM_INITIALIZER(1) + NXMUTEX_INITIALIZER }; /**************************************************************************** diff --git a/mm/shm/shmat.c b/mm/shm/shmat.c index 7c7a76b736d50..feab41bf36950 100644 --- a/mm/shm/shmat.c +++ b/mm/shm/shmat.c @@ -119,7 +119,7 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg) /* Get exclusive access to the region data structure */ - ret = nxsem_wait(®ion->sr_sem); + ret = nxmutex_lock(®ion->sr_lock); if (ret < 0) { shmerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -134,7 +134,7 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg) { shmerr("ERROR: gran_alloc() failed\n"); ret = -ENOMEM; - goto errout_with_semaphore; + goto errout_with_lock; } /* Convert the region size to pages */ @@ -171,15 +171,15 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg) /* Release our lock on the entry */ - nxsem_post(®ion->sr_sem); + nxmutex_unlock(®ion->sr_lock); return (FAR void *)vaddr; errout_with_vaddr: gran_free(group->tg_shm.gs_handle, (FAR void *)vaddr, region->sr_ds.shm_segsz); -errout_with_semaphore: - nxsem_post(®ion->sr_sem); +errout_with_lock: + nxmutex_unlock(®ion->sr_lock); errout_with_ret: set_errno(-ret); diff --git a/mm/shm/shmctl.c b/mm/shm/shmctl.c index 4607956924790..987a2e9b085e2 100644 --- a/mm/shm/shmctl.c +++ b/mm/shm/shmctl.c @@ -115,7 +115,7 @@ int shmctl(int shmid, int cmd, struct shmid_ds *buf) /* Get exclusive access to the region data structure */ - ret = nxsem_wait(®ion->sr_sem); + ret = nxmutex_lock(®ion->sr_lock); if (ret < 0) { shmerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -178,7 +178,7 @@ int shmctl(int shmid, int cmd, struct shmid_ds *buf) default: shmerr("ERROR: Unrecognized command: %d\n", cmd); ret = -EINVAL; - goto errout_with_semaphore; + goto errout_with_lock; } /* Save the process ID of the last operation */ @@ -192,11 +192,11 @@ int shmctl(int shmid, int cmd, struct shmid_ds *buf) /* Release our lock on the entry */ - nxsem_post(®ion->sr_sem); + nxmutex_unlock(®ion->sr_lock); return ret; -errout_with_semaphore: - nxsem_post(®ion->sr_sem); +errout_with_lock: + nxmutex_unlock(®ion->sr_lock); errout_with_ret: set_errno(-ret); @@ -223,8 +223,8 @@ int shmctl(int shmid, int cmd, struct shmid_ds *buf) * None * * Assumption: - * The caller holds either the region table semaphore or else the - * semaphore on the particular entry being deleted. + * The caller holds either the region table mutex or else the + * mutex on the particular entry being deleted. * ****************************************************************************/ @@ -242,7 +242,7 @@ void shm_destroy(int shmid) /* Reset the region entry to its initial state */ - nxsem_destroy(®ion->sr_sem); + nxmutex_destroy(®ion->sr_lock); memset(region, 0, sizeof(struct shm_region_s)); } diff --git a/mm/shm/shmdt.c b/mm/shm/shmdt.c index 7008da85c7c33..50889f575f6e6 100644 --- a/mm/shm/shmdt.c +++ b/mm/shm/shmdt.c @@ -105,7 +105,7 @@ int shmdt(FAR const void *shmaddr) /* Get exclusive access to the region data structure */ - ret = nxsem_wait(®ion->sr_sem); + ret = nxmutex_lock(®ion->sr_lock); if (ret < 0) { shmerr("ERROR: nxsem_wait failed: %d\n", ret); @@ -170,7 +170,7 @@ int shmdt(FAR const void *shmaddr) /* Release our lock on the entry */ - nxsem_post(®ion->sr_sem); + nxmutex_unlock(®ion->sr_lock); return OK; errout_with_errno: diff --git a/mm/shm/shmget.c b/mm/shm/shmget.c index c28cc3146f981..57481da7437c7 100644 --- a/mm/shm/shmget.c +++ b/mm/shm/shmget.c @@ -106,7 +106,7 @@ static int shm_reserve(key_t key, int shmflg) region->sr_key = key; region->sr_flags = SRFLAG_INUSE; - nxsem_init(®ion->sr_sem, 0, 1); + nxmutex_init(®ion->sr_lock); /* Set the low-order nine bits of shm_perm.mode to the low-order * nine bits of shmflg. @@ -365,7 +365,7 @@ int shmget(key_t key, size_t size, int shmflg) /* Get exclusive access to the global list of shared memory regions */ - ret = nxsem_wait(&g_shminfo.si_sem); + ret = nxmutex_lock(&g_shminfo.si_lock); if (ret < 0) { goto errout; @@ -388,7 +388,7 @@ int shmget(key_t key, size_t size, int shmflg) if (ret < 0) { shmerr("ERROR: shm_create failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } /* Return the shared memory ID */ @@ -399,7 +399,7 @@ int shmget(key_t key, size_t size, int shmflg) { /* Fail with ENOENT */ - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -431,7 +431,7 @@ int shmget(key_t key, size_t size, int shmflg) if (ret < 0) { shmerr("ERROR: shm_create failed: %d\n", ret); - goto errout_with_semaphore; + goto errout_with_lock; } } else @@ -439,7 +439,7 @@ int shmget(key_t key, size_t size, int shmflg) /* Fail with EINVAL */ ret = -EINVAL; - goto errout_with_semaphore; + goto errout_with_lock; } } @@ -454,11 +454,11 @@ int shmget(key_t key, size_t size, int shmflg) /* Release our lock on the shared memory region list */ - nxsem_post(&g_shminfo.si_sem); + nxmutex_unlock(&g_shminfo.si_lock); return shmid; -errout_with_semaphore: - nxsem_post(&g_shminfo.si_sem); +errout_with_lock: + nxmutex_unlock(&g_shminfo.si_lock); errout: set_errno(-ret); diff --git a/net/can/can_conn.c b/net/can/can_conn.c index 82917f7731ed4..b12683427b203 100644 --- a/net/can/can_conn.c +++ b/net/can/can_conn.c @@ -34,7 +34,7 @@ #include #include -#include +#include #include #include @@ -56,34 +56,12 @@ static struct can_conn_s g_can_connections[CONFIG_CAN_CONNS]; /* A list of all free NetLink connections */ static dq_queue_t g_free_can_connections; -static sem_t g_free_sem = SEM_INITIALIZER(1); +static mutex_t g_free_lock = NXMUTEX_INITIALIZER; /* A list of all allocated NetLink connections */ static dq_queue_t g_active_can_connections; -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: _can_semtake() and _can_semgive() - * - * Description: - * Take/give semaphore - * - ****************************************************************************/ - -static void _can_semtake(FAR sem_t *sem) -{ - net_lockedwait_uninterruptible(sem); -} - -static void _can_semgive(FAR sem_t *sem) -{ - nxsem_post(sem); -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -127,9 +105,9 @@ FAR struct can_conn_s *can_alloc(void) int i; #endif - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a a mutex. */ - _can_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); #ifdef CONFIG_NET_ALLOC_CONNS if (dq_peek(&g_free_can_connections) == NULL) { @@ -170,7 +148,7 @@ FAR struct can_conn_s *can_alloc(void) dq_addlast(&conn->sconn.node, &g_active_can_connections); } - _can_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); return conn; } @@ -185,11 +163,11 @@ FAR struct can_conn_s *can_alloc(void) void can_free(FAR struct can_conn_s *conn) { - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ DEBUGASSERT(conn->crefs == 0); - _can_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); /* Remove the connection from the active list */ @@ -202,7 +180,7 @@ void can_free(FAR struct can_conn_s *conn) /* Free the connection */ dq_addlast(&conn->sconn.node, &g_free_can_connections); - _can_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); } /**************************************************************************** diff --git a/net/icmp/icmp_conn.c b/net/icmp/icmp_conn.c index 3ab0f27cba25a..a8456c4967dd8 100644 --- a/net/icmp/icmp_conn.c +++ b/net/icmp/icmp_conn.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include #include @@ -55,7 +55,7 @@ static struct icmp_conn_s g_icmp_connections[CONFIG_NET_ICMP_NCONNS]; /* A list of all free IPPROTO_ICMP socket connections */ static dq_queue_t g_free_icmp_connections; -static sem_t g_free_sem = SEM_INITIALIZER(1); +static mutex_t g_free_lock = NXMUTEX_INITIALIZER; /* A list of all allocated IPPROTO_ICMP socket connections */ @@ -104,9 +104,9 @@ FAR struct icmp_conn_s *icmp_alloc(void) FAR struct icmp_conn_s *conn = NULL; int ret; - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ - ret = net_lockedwait(&g_free_sem); + ret = nxmutex_lock(&g_free_lock); if (ret >= 0) { #ifdef CONFIG_NET_ALLOC_CONNS @@ -132,7 +132,7 @@ FAR struct icmp_conn_s *icmp_alloc(void) dq_addlast(&conn->sconn.node, &g_active_icmp_connections); } - nxsem_post(&g_free_sem); + nxmutex_unlock(&g_free_lock); } return conn; @@ -149,13 +149,13 @@ FAR struct icmp_conn_s *icmp_alloc(void) void icmp_free(FAR struct icmp_conn_s *conn) { - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ DEBUGASSERT(conn->crefs == 0); - /* Take the semaphore (perhaps waiting) */ + /* Take the mutex (perhaps waiting) */ - net_lockedwait_uninterruptible(&g_free_sem); + nxmutex_lock(&g_free_lock); /* Is this the last reference on the connection? It might not be if the * socket was cloned. @@ -182,7 +182,7 @@ void icmp_free(FAR struct icmp_conn_s *conn) dq_addlast(&conn->sconn.node, &g_free_icmp_connections); } - nxsem_post(&g_free_sem); + nxmutex_unlock(&g_free_lock); } /**************************************************************************** diff --git a/net/icmpv6/icmpv6_conn.c b/net/icmpv6/icmpv6_conn.c index b026581867f5c..b10e7daf3dadd 100644 --- a/net/icmpv6/icmpv6_conn.c +++ b/net/icmpv6/icmpv6_conn.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include #include @@ -55,7 +55,7 @@ static struct icmpv6_conn_s g_icmpv6_connections[CONFIG_NET_ICMPv6_NCONNS]; /* A list of all free IPPROTO_ICMP socket connections */ static dq_queue_t g_free_icmpv6_connections; -static sem_t g_free_sem = SEM_INITIALIZER(1); +static mutex_t g_free_lock = NXMUTEX_INITIALIZER; /* A list of all allocated IPPROTO_ICMP socket connections */ @@ -104,9 +104,9 @@ FAR struct icmpv6_conn_s *icmpv6_alloc(void) FAR struct icmpv6_conn_s *conn = NULL; int ret; - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ - ret = net_lockedwait(&g_free_sem); + ret = nxmutex_lock(&g_free_lock); if (ret >= 0) { #ifdef CONFIG_NET_ALLOC_CONNS @@ -133,7 +133,7 @@ FAR struct icmpv6_conn_s *icmpv6_alloc(void) dq_addlast(&conn->sconn.node, &g_active_icmpv6_connections); } - nxsem_post(&g_free_sem); + nxmutex_unlock(&g_free_lock); } return conn; @@ -150,13 +150,13 @@ FAR struct icmpv6_conn_s *icmpv6_alloc(void) void icmpv6_free(FAR struct icmpv6_conn_s *conn) { - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ DEBUGASSERT(conn->crefs == 0); - /* Take the semaphore (perhaps waiting) */ + /* Take the mutex (perhaps waiting) */ - net_lockedwait_uninterruptible(&g_free_sem); + nxmutex_lock(&g_free_lock); /* Remove the connection from the active list */ @@ -169,7 +169,7 @@ void icmpv6_free(FAR struct icmpv6_conn_s *conn) /* Free the connection */ dq_addlast(&conn->sconn.node, &g_free_icmpv6_connections); - nxsem_post(&g_free_sem); + nxmutex_unlock(&g_free_lock); } /**************************************************************************** diff --git a/net/local/local.h b/net/local/local.h index 6eb0e28aa99ae..47235a318da26 100644 --- a/net/local/local.h +++ b/net/local/local.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #ifdef CONFIG_NET_LOCAL @@ -130,7 +131,7 @@ struct local_conn_s lc_cfps[LOCAL_NCONTROLFDS]; /* Socket message control filep */ #endif /* CONFIG_NET_LOCAL_SCM */ - sem_t lc_sendsem; /* Make sending multi-thread safe */ + mutex_t lc_sendlock; /* Make sending multi-thread safe */ #ifdef CONFIG_NET_LOCAL_STREAM /* SOCK_STREAM fields common to both client and server */ diff --git a/net/local/local_conn.c b/net/local/local_conn.c index 1432054ce2493..cdc79b26d2640 100644 --- a/net/local/local_conn.c +++ b/net/local/local_conn.c @@ -134,7 +134,7 @@ FAR struct local_conn_s *local_alloc(void) * Make sure data will not be garbled when multi-thread sends. */ - nxsem_init(&conn->lc_sendsem, 0, 1); + nxmutex_init(&conn->lc_sendlock); /* Add the connection structure to the list of listeners */ @@ -218,7 +218,7 @@ void local_free(FAR struct local_conn_s *conn) /* Destory sem associated with the connection */ - nxsem_destroy(&conn->lc_sendsem); + nxmutex_destroy(&conn->lc_sendlock); /* And free the connection structure */ diff --git a/net/local/local_connect.c b/net/local/local_connect.c index 9b20aa9fe876d..3ffc3314d7629 100644 --- a/net/local/local_connect.c +++ b/net/local/local_connect.c @@ -45,21 +45,6 @@ * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: _local_semtake() and _local_semgive() - * - * Description: - * Take/give semaphore - * - ****************************************************************************/ - -static inline void _local_semtake(sem_t *sem) -{ - net_lockedwait_uninterruptible(sem); -} - -#define _local_semgive(sem) nxsem_post(sem) - /**************************************************************************** * Name: local_stream_connect * @@ -143,7 +128,7 @@ static int inline local_stream_connect(FAR struct local_conn_s *client, if (nxsem_get_value(&server->lc_waitsem, &sval) >= 0 && sval < 1) { - _local_semgive(&server->lc_waitsem); + nxsem_post(&server->lc_waitsem); } /* Wait for the server to accept the connections */ @@ -152,7 +137,7 @@ static int inline local_stream_connect(FAR struct local_conn_s *client, { do { - _local_semtake(&client->lc_waitsem); + net_lockedwait_uninterruptible(&client->lc_waitsem); ret = client->u.client.lc_result; } while (ret == -EBUSY); diff --git a/net/local/local_sendmsg.c b/net/local/local_sendmsg.c index 7a15d4c18a309..eed2d22a81100 100644 --- a/net/local/local_sendmsg.c +++ b/net/local/local_sendmsg.c @@ -196,7 +196,7 @@ static ssize_t local_send(FAR struct socket *psock, /* Send the packet */ - ret = nxsem_wait_uninterruptible(&peer->lc_sendsem); + ret = nxmutex_lock(&peer->lc_sendlock); if (ret < 0) { /* May fail because the task was canceled. */ @@ -205,7 +205,7 @@ static ssize_t local_send(FAR struct socket *psock, } ret = local_send_packet(&peer->lc_outfile, buf, len, false); - nxsem_post(&peer->lc_sendsem); + nxmutex_unlock(&peer->lc_sendlock); } break; #endif /* CONFIG_NET_LOCAL_STREAM */ @@ -345,7 +345,7 @@ static ssize_t local_sendto(FAR struct socket *psock, /* Make sure that dgram is sent safely */ - ret = nxsem_wait_uninterruptible(&conn->lc_sendsem); + ret = nxmutex_lock(&conn->lc_sendlock); if (ret < 0) { /* May fail because the task was canceled. */ @@ -361,7 +361,7 @@ static ssize_t local_sendto(FAR struct socket *psock, nerr("ERROR: Failed to send the packet: %zd\n", ret); } - nxsem_post(&conn->lc_sendsem); + nxmutex_unlock(&conn->lc_sendlock); errout_with_sender: diff --git a/net/netlink/netlink_conn.c b/net/netlink/netlink_conn.c index e245072d3fa70..dc15170828f91 100644 --- a/net/netlink/netlink_conn.c +++ b/net/netlink/netlink_conn.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -57,7 +58,7 @@ static struct netlink_conn_s g_netlink_connections[CONFIG_NETLINK_CONNS]; /* A list of all free NetLink connections */ static dq_queue_t g_free_netlink_connections; -static sem_t g_free_sem = SEM_INITIALIZER(1); +static mutex_t g_free_lock = NXMUTEX_INITIALIZER; /* A list of all allocated NetLink connections */ @@ -67,24 +68,6 @@ static dq_queue_t g_active_netlink_connections; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: _netlink_semtake() and _netlink_semgive() - * - * Description: - * Take/give semaphore - * - ****************************************************************************/ - -static void _netlink_semtake(FAR sem_t *sem) -{ - net_lockedwait_uninterruptible(sem); -} - -static void _netlink_semgive(FAR sem_t *sem) -{ - nxsem_post(sem); -} - /**************************************************************************** * Name: netlink_response_available * @@ -105,7 +88,7 @@ static void netlink_response_available(FAR void *arg) /* wakeup the waiter */ - _netlink_semgive(arg); + nxsem_post(arg); } /**************************************************************************** @@ -152,9 +135,9 @@ FAR struct netlink_conn_s *netlink_alloc(void) int i; #endif - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ - _netlink_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); #ifdef CONFIG_NET_ALLOC_CONNS if (dq_peek(&g_free_netlink_connections) == NULL) { @@ -178,7 +161,7 @@ FAR struct netlink_conn_s *netlink_alloc(void) dq_addlast(&conn->sconn.node, &g_active_netlink_connections); } - _netlink_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); return conn; } @@ -195,11 +178,11 @@ void netlink_free(FAR struct netlink_conn_s *conn) { FAR sq_entry_t *resp; - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ DEBUGASSERT(conn->crefs == 0); - _netlink_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); /* Remove the connection from the active list */ @@ -219,7 +202,7 @@ void netlink_free(FAR struct netlink_conn_s *conn) /* Free the connection */ dq_addlast(&conn->sconn.node, &g_free_netlink_connections); - _netlink_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); } /**************************************************************************** @@ -445,7 +428,7 @@ netlink_get_response(FAR struct netlink_conn_s *conn) { /* Wait for a response to be queued */ - _netlink_semtake(&waitsem); + nxsem_post(&waitsem); } /* Clean-up the semaphore */ diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c index d348fd0d2df70..5392861d13cf1 100644 --- a/net/pkt/pkt_conn.c +++ b/net/pkt/pkt_conn.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include #include @@ -63,31 +63,12 @@ static struct pkt_conn_s g_pkt_connections[CONFIG_NET_PKT_CONNS]; /* A list of all free packet socket connections */ static dq_queue_t g_free_pkt_connections; -static sem_t g_free_sem = SEM_INITIALIZER(1); +static mutex_t g_free_lock = NXMUTEX_INITIALIZER; /* A list of all allocated packet socket connections */ static dq_queue_t g_active_pkt_connections; -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: _pkt_semtake() and _pkt_semgive() - * - * Description: - * Take/give semaphore - * - ****************************************************************************/ - -static inline void _pkt_semtake(FAR sem_t *sem) -{ - net_lockedwait_uninterruptible(sem); -} - -#define _pkt_semgive(sem) nxsem_post(sem) - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -129,9 +110,9 @@ FAR struct pkt_conn_s *pkt_alloc(void) int i; #endif - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ - _pkt_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); #ifdef CONFIG_NET_ALLOC_CONNS if (dq_peek(&g_free_pkt_connections) == NULL) { @@ -154,7 +135,7 @@ FAR struct pkt_conn_s *pkt_alloc(void) dq_addlast(&conn->sconn.node, &g_active_pkt_connections); } - _pkt_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); return conn; } @@ -169,11 +150,11 @@ FAR struct pkt_conn_s *pkt_alloc(void) void pkt_free(FAR struct pkt_conn_s *conn) { - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ DEBUGASSERT(conn->crefs == 0); - _pkt_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); /* Remove the connection from the active list */ @@ -186,7 +167,7 @@ void pkt_free(FAR struct pkt_conn_s *conn) /* Free the connection */ dq_addlast(&conn->sconn.node, &g_free_pkt_connections); - _pkt_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); } /**************************************************************************** diff --git a/net/route/net_cacheroute.c b/net/route/net_cacheroute.c index 5f6dd12ce7417..9477ce59e37a5 100644 --- a/net/route/net_cacheroute.c +++ b/net/route/net_cacheroute.c @@ -29,7 +29,7 @@ #include #include -#include +#include #include "route/cacheroute.h" #include "route/route.h" @@ -122,7 +122,7 @@ static struct net_cache_ipv4_entry_s /* Serializes access to the routing table cache */ -static sem_t g_ipv4_cachelock = SEM_INITIALIZER(1); +static mutex_t g_ipv4_cachelock = NXMUTEX_INITIALIZER; #endif #if defined(CONFIG_ROUTE_IPv6_CACHEROUTE) @@ -141,48 +141,13 @@ static struct net_cache_ipv6_entry_s /* Serializes access to the routing table cache */ -static sem_t g_ipv6_cachelock = SEM_INITIALIZER(1); +static mutex_t g_ipv6_cachelock = NXMUTEX_INITIALIZER; #endif /**************************************************************************** * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: net_lock_ipv4_cache and net_lock_ipv6_cache - * - * Description: - * Lock the routing table cache list. - * - * Input Parameters: - * None - * - * Returned Value: - * Zero (OK) is returned on success; A negated errno value is returned on - * any failure. - * - ****************************************************************************/ - -#define net_lock_ipv4_cache() nxsem_wait_uninterruptible(&g_ipv4_cachelock) -#define net_lock_ipv6_cache() nxsem_wait_uninterruptible(&g_ipv6_cachelock) - -/**************************************************************************** - * Name: net_unlock_ipv4_cache and net_unlock_ipv6_cache - * - * Description: - * Unlock the routing table cache list. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -#define net_unlock_ipv4_cache() nxsem_post(&g_ipv4_cachelock) -#define net_unlock_ipv6_cache() nxsem_post(&g_ipv6_cachelock) - /**************************************************************************** * Name: net_add_newest_ipv4 and net_add_newest_ipv6 * @@ -515,7 +480,7 @@ int net_addcache_ipv4(FAR struct net_route_ipv4_s *route) /* Get exclusive access to the cache */ - ret = net_lock_ipv4_cache(); + ret = nxmutex_lock(&g_ipv4_cachelock); if (ret < 0) { return ret; @@ -543,7 +508,7 @@ int net_addcache_ipv4(FAR struct net_route_ipv4_s *route) if (prev == NULL) { - net_unlock_ipv4_cache(); + nxmutex_unlock(&g_ipv4_cachelock); return OK; } @@ -581,7 +546,7 @@ int net_addcache_ipv4(FAR struct net_route_ipv4_s *route) /* Then add the new cache entry as the newest entry in the table */ net_add_newest_ipv4(cache); - net_unlock_ipv4_cache(); + nxmutex_unlock(&g_ipv4_cachelock); return OK; } #endif @@ -597,7 +562,7 @@ int net_addcache_ipv6(FAR struct net_route_ipv6_s *route) /* Get exclusive access to the cache */ - ret = net_lock_ipv6_cache(); + ret = nxmutex_lock(&g_ipv6_cachelock); if (ret < 0) { return ret; @@ -625,7 +590,7 @@ int net_addcache_ipv6(FAR struct net_route_ipv6_s *route) if (prev == NULL) { - net_unlock_ipv6_cache(); + nxmutex_unlock(&g_ipv6_cachelock); return OK; } @@ -663,7 +628,7 @@ int net_addcache_ipv6(FAR struct net_route_ipv6_s *route) /* Then add the new cache entry as the newest entry in the table */ net_add_newest_ipv6(cache); - net_unlock_ipv6_cache(); + nxmutex_unlock(&g_ipv6_cachelock); return OK; } #endif @@ -694,7 +659,7 @@ int net_foreachcache_ipv4(route_handler_ipv4_t handler, FAR void *arg) /* Get exclusive access to the cache */ - ret = net_lock_ipv4_cache(); + ret = nxmutex_lock(&g_ipv4_cachelock); if (ret < 0) { return ret; @@ -714,7 +679,7 @@ int net_foreachcache_ipv4(route_handler_ipv4_t handler, FAR void *arg) /* Unlock the cache */ - net_unlock_ipv4_cache(); + nxmutex_unlock(&g_ipv4_cachelock); return ret; } #endif @@ -728,7 +693,7 @@ int net_foreachcache_ipv6(route_handler_ipv6_t handler, FAR void *arg) /* Get exclusive access to the cache */ - ret = net_lock_ipv6_cache(); + ret = nxmutex_lock(&g_ipv6_cachelock); if (ret < 0) { return ret; @@ -748,7 +713,7 @@ int net_foreachcache_ipv6(route_handler_ipv6_t handler, FAR void *arg) /* Unlock the cache */ - net_unlock_ipv6_cache(); + nxmutex_unlock(&g_ipv6_cachelock); return ret; } #endif @@ -774,7 +739,7 @@ void net_flushcache_ipv4(void) /* Get exclusive access to the cache */ - ret = net_lock_ipv4_cache(); + ret = nxmutex_lock(&g_ipv4_cachelock); if (ret >= 0) { /* Reset the cache */ @@ -783,7 +748,7 @@ void net_flushcache_ipv4(void) /* Unlock the cache */ - net_unlock_ipv4_cache(); + nxmutex_unlock(&g_ipv4_cachelock); } } #endif @@ -795,7 +760,7 @@ void net_flushcache_ipv6(void) /* Get exclusive access to the cache */ - ret = net_lock_ipv6_cache(); + ret = nxmutex_lock(&g_ipv6_cachelock); if (ret >= 0) { /* Reset the cache */ @@ -804,7 +769,7 @@ void net_flushcache_ipv6(void) /* Unlock the cache */ - net_unlock_ipv6_cache(); + nxmutex_unlock(&g_ipv6_cachelock); } } #endif diff --git a/net/rpmsg/rpmsg_sockif.c b/net/rpmsg/rpmsg_sockif.c index 6d40d90e1ce03..a4590f9ad3aba 100644 --- a/net/rpmsg/rpmsg_sockif.c +++ b/net/rpmsg/rpmsg_sockif.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -92,10 +93,10 @@ struct rpmsg_socket_conn_s FAR struct pollfd *fds[CONFIG_NET_RPMSG_NPOLLWAITERS]; sem_t sendsem; - sem_t sendlock; + mutex_t sendlock; sem_t recvsem; - sem_t recvlock; + mutex_t recvlock; FAR void *recvdata; uint32_t recvlen; @@ -184,16 +185,6 @@ static unsigned int g_rpmsg_id; * Private Functions ****************************************************************************/ -static inline void rpmsg_socket_lock(FAR sem_t *sem) -{ - net_lockedwait_uninterruptible(sem); -} - -static inline void rpmsg_socket_unlock(FAR sem_t *sem) -{ - nxsem_post(sem); -} - static inline void rpmsg_socket_post(FAR sem_t *sem) { int semcount; @@ -217,8 +208,8 @@ static FAR struct rpmsg_socket_conn_s *rpmsg_socket_alloc(void) circbuf_init(&conn->recvbuf, NULL, 0); - nxsem_init(&conn->sendlock, 0, 1); - nxsem_init(&conn->recvlock, 0, 1); + nxmutex_init(&conn->sendlock); + nxmutex_init(&conn->recvlock); nxsem_init(&conn->sendsem, 0, 0); nxsem_init(&conn->recvsem, 0, 0); nxsem_set_protocol(&conn->sendsem, SEM_PRIO_NONE); @@ -232,8 +223,8 @@ static void rpmsg_socket_free(FAR struct rpmsg_socket_conn_s *conn) { circbuf_uninit(&conn->recvbuf); - nxsem_destroy(&conn->recvlock); - nxsem_destroy(&conn->sendlock); + nxmutex_destroy(&conn->recvlock); + nxmutex_destroy(&conn->sendlock); nxsem_destroy(&conn->sendsem); nxsem_destroy(&conn->recvsem); @@ -251,7 +242,7 @@ static int rpmsg_socket_wakeup(FAR struct rpmsg_socket_conn_s *conn) return ret; } - rpmsg_socket_lock(&conn->recvlock); + nxmutex_lock(&conn->recvlock); space = conn->recvpos - conn->lastpos; if (space > circbuf_size(&conn->recvbuf) / 2) @@ -263,7 +254,7 @@ static int rpmsg_socket_wakeup(FAR struct rpmsg_socket_conn_s *conn) ret = 1; } - rpmsg_socket_unlock(&conn->recvlock); + nxmutex_unlock(&conn->recvlock); return ret ? rpmsg_send(&conn->ept, &msg, sizeof(msg)) : 0; } @@ -283,7 +274,7 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, if (head->cmd == RPMSG_SOCKET_CMD_SYNC) { - rpmsg_socket_lock(&conn->recvlock); + nxmutex_lock(&conn->recvlock); conn->sendsize = head->size; if (conn->psock) @@ -294,14 +285,14 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, rpmsg_socket_post(&conn->sendsem); poll_notify(conn->fds, CONFIG_NET_RPMSG_NPOLLWAITERS, POLLOUT); - rpmsg_socket_unlock(&conn->recvlock); + nxmutex_unlock(&conn->recvlock); } else { FAR struct rpmsg_socket_data_s *msg = data; FAR uint8_t *buf = (FAR uint8_t *)msg->data; - rpmsg_socket_lock(&conn->sendlock); + nxmutex_lock(&conn->sendlock); conn->ackpos = msg->pos; @@ -311,7 +302,7 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, poll_notify(conn->fds, CONFIG_NET_RPMSG_NPOLLWAITERS, POLLOUT); } - rpmsg_socket_unlock(&conn->sendlock); + nxmutex_unlock(&conn->sendlock); if (len > sizeof(*msg)) { @@ -319,7 +310,7 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, DEBUGASSERT(len == msg->len || len == msg->len + sizeof(uint32_t)); - rpmsg_socket_lock(&conn->recvlock); + nxmutex_lock(&conn->recvlock); if (conn->recvdata) { @@ -361,7 +352,7 @@ static int rpmsg_socket_ept_cb(FAR struct rpmsg_endpoint *ept, poll_notify(conn->fds, CONFIG_NET_RPMSG_NPOLLWAITERS, POLLIN); } - rpmsg_socket_unlock(&conn->recvlock); + nxmutex_unlock(&conn->recvlock); } } @@ -376,7 +367,7 @@ static inline void rpmsg_socket_destroy_ept( return; } - rpmsg_socket_lock(&conn->recvlock); + nxmutex_lock(&conn->recvlock); if (conn->ept.rdev) { @@ -394,7 +385,7 @@ static inline void rpmsg_socket_destroy_ept( POLLIN | POLLOUT); } - rpmsg_socket_unlock(&conn->recvlock); + nxmutex_unlock(&conn->recvlock); } static void rpmsg_socket_ns_bound(struct rpmsg_endpoint *ept) @@ -511,7 +502,7 @@ static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev, strlcpy(new->rpaddr.rp_name, name + RPMSG_SOCKET_NAME_PREFIX_LEN, sizeof(new->rpaddr.rp_name)); - rpmsg_socket_lock(&server->recvlock); + nxmutex_lock(&server->recvlock); for (tmp = server; tmp->next; tmp = tmp->next) { @@ -519,7 +510,7 @@ static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev, { /* Reject the connection */ - rpmsg_socket_unlock(&server->recvlock); + nxmutex_unlock(&server->recvlock); rpmsg_destroy_ept(&new->ept); rpmsg_socket_free(new); return; @@ -528,7 +519,7 @@ static void rpmsg_socket_ns_bind(FAR struct rpmsg_device *rdev, tmp->next = new; - rpmsg_socket_unlock(&server->recvlock); + nxmutex_unlock(&server->recvlock); rpmsg_socket_ns_bound(&new->ept); @@ -745,7 +736,7 @@ static int rpmsg_socket_accept(FAR struct socket *psock, { FAR struct rpmsg_socket_conn_s *conn = NULL; - rpmsg_socket_lock(&server->recvlock); + nxmutex_lock(&server->recvlock); if (server->next) { @@ -754,7 +745,7 @@ static int rpmsg_socket_accept(FAR struct socket *psock, conn->next = NULL; } - rpmsg_socket_unlock(&server->recvlock); + nxmutex_unlock(&server->recvlock); if (conn) { @@ -859,23 +850,23 @@ static int rpmsg_socket_poll(FAR struct socket *psock, } else { - rpmsg_socket_lock(&conn->sendlock); + nxmutex_lock(&conn->sendlock); if (rpmsg_socket_get_space(conn) > 0) { eventset |= POLLOUT; } - rpmsg_socket_unlock(&conn->sendlock); + nxmutex_unlock(&conn->sendlock); - rpmsg_socket_lock(&conn->recvlock); + nxmutex_lock(&conn->recvlock); if (!circbuf_is_empty(&conn->recvbuf)) { eventset |= POLLIN; } - rpmsg_socket_unlock(&conn->recvlock); + nxmutex_unlock(&conn->recvlock); } } else if (!_SS_ISCONNECTED(conn->sconn.s_flags) && @@ -936,9 +927,9 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock, uint32_t ipcsize; uint32_t block; - rpmsg_socket_lock(&conn->sendlock); + nxmutex_lock(&conn->sendlock); block = MIN(len - written, rpmsg_socket_get_space(conn)); - rpmsg_socket_unlock(&conn->sendlock); + nxmutex_unlock(&conn->sendlock); if (block == 0) { @@ -972,7 +963,7 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock, break; } - rpmsg_socket_lock(&conn->sendlock); + nxmutex_lock(&conn->sendlock); block = MIN(len - written, rpmsg_socket_get_space(conn)); block = MIN(block, ipcsize - sizeof(*msg)); @@ -998,7 +989,7 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock, conn->lastpos = conn->recvpos; conn->sendpos += msg->len; - rpmsg_socket_unlock(&conn->sendlock); + nxmutex_unlock(&conn->sendlock); ret = rpmsg_sendto_nocopy(&conn->ept, msg, block + sizeof(*msg), conn->ept.dest_addr); @@ -1034,9 +1025,9 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock, while (1) { - rpmsg_socket_lock(&conn->sendlock); + nxmutex_lock(&conn->sendlock); space = rpmsg_socket_get_space(conn); - rpmsg_socket_unlock(&conn->sendlock); + nxmutex_unlock(&conn->sendlock); if (space >= total - sizeof(*msg)) break; @@ -1067,7 +1058,7 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock, return -EINVAL; } - rpmsg_socket_lock(&conn->sendlock); + nxmutex_lock(&conn->sendlock); space = rpmsg_socket_get_space(conn); total = MIN(total, space + sizeof(*msg)); @@ -1100,7 +1091,7 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock, conn->lastpos = conn->recvpos; conn->sendpos += len + sizeof(uint32_t); - rpmsg_socket_unlock(&conn->sendlock); + nxmutex_unlock(&conn->sendlock); ret = rpmsg_sendto_nocopy(&conn->ept, msg, total, conn->ept.dest_addr); @@ -1177,7 +1168,7 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, return -EISCONN; } - rpmsg_socket_lock(&conn->recvlock); + nxmutex_lock(&conn->recvlock); if (psock->s_type != SOCK_STREAM) { @@ -1223,7 +1214,7 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, conn->recvdata = buf; conn->recvlen = len; - rpmsg_socket_unlock(&conn->recvlock); + nxmutex_unlock(&conn->recvlock); ret = net_timedwait(&conn->recvsem, _SO_TIMEOUT(conn->sconn.s_rcvtimeo)); @@ -1232,7 +1223,7 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, ret = -ECONNRESET; } - rpmsg_socket_lock(&conn->recvlock); + nxmutex_lock(&conn->recvlock); if (!conn->recvdata) { @@ -1244,7 +1235,7 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock, } out: - rpmsg_socket_unlock(&conn->recvlock); + nxmutex_unlock(&conn->recvlock); if (ret > 0) { diff --git a/net/udp/udp_conn.c b/net/udp/udp_conn.c index 52d4ac64de151..d4fdf5d42045d 100644 --- a/net/udp/udp_conn.c +++ b/net/udp/udp_conn.c @@ -56,7 +56,7 @@ #include #include -#include +#include #include #include #include @@ -88,7 +88,7 @@ struct udp_conn_s g_udp_connections[CONFIG_NET_UDP_CONNS]; /* A list of all free UDP connections */ static dq_queue_t g_free_udp_connections; -static sem_t g_free_sem = SEM_INITIALIZER(1); +static mutex_t g_free_lock = NXMUTEX_INITIALIZER; /* A list of all allocated UDP connections */ @@ -98,21 +98,6 @@ static dq_queue_t g_active_udp_connections; * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: _udp_semtake() and _udp_semgive() - * - * Description: - * Take/give semaphore - * - ****************************************************************************/ - -static inline void _udp_semtake(FAR sem_t *sem) -{ - net_lockedwait_uninterruptible(sem); -} - -#define _udp_semgive(sem) nxsem_post(sem) - /**************************************************************************** * Name: udp_find_conn() * @@ -604,9 +589,9 @@ FAR struct udp_conn_s *udp_alloc(uint8_t domain) { FAR struct udp_conn_s *conn; - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ - _udp_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); #ifndef CONFIG_NET_ALLOC_CONNS conn = (FAR struct udp_conn_s *)dq_remfirst(&g_free_udp_connections); #else @@ -642,7 +627,7 @@ FAR struct udp_conn_s *udp_alloc(uint8_t domain) dq_addlast(&conn->sconn.node, &g_active_udp_connections); } - _udp_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); return conn; } @@ -661,11 +646,11 @@ void udp_free(FAR struct udp_conn_s *conn) FAR struct udp_wrbuffer_s *wrbuffer; #endif - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ DEBUGASSERT(conn->crefs == 0); - _udp_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); conn->lport = 0; /* Remove the connection from the active list */ @@ -700,7 +685,7 @@ void udp_free(FAR struct udp_conn_s *conn) /* Free the connection */ dq_addlast(&conn->sconn.node, &g_free_udp_connections); - _udp_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); } /**************************************************************************** diff --git a/net/usrsock/usrsock_conn.c b/net/usrsock/usrsock_conn.c index f42ebe187089c..9536dc820a7f5 100644 --- a/net/usrsock/usrsock_conn.c +++ b/net/usrsock/usrsock_conn.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -54,34 +55,12 @@ static struct usrsock_conn_s g_usrsock_connections[CONFIG_NET_USRSOCK_CONNS]; /* A list of all free usrsock connections */ static dq_queue_t g_free_usrsock_connections; -static sem_t g_free_sem = SEM_INITIALIZER(1); +static mutex_t g_free_lock = NXMUTEX_INITIALIZER; /* A list of all allocated usrsock connections */ static dq_queue_t g_active_usrsock_connections; -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: _usrsock_semtake() and _usrsock_semgive() - * - * Description: - * Take/give semaphore - * - ****************************************************************************/ - -static void _usrsock_semtake(FAR sem_t *sem) -{ - net_lockedwait_uninterruptible(sem); -} - -static void _usrsock_semgive(FAR sem_t *sem) -{ - nxsem_post(sem); -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -102,9 +81,9 @@ FAR struct usrsock_conn_s *usrsock_alloc(void) int i; #endif - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a a mutex. */ - _usrsock_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); #ifdef CONFIG_NET_ALLOC_CONNS if (dq_peek(&g_free_usrsock_connections) == NULL) { @@ -134,7 +113,7 @@ FAR struct usrsock_conn_s *usrsock_alloc(void) dq_addlast(&conn->sconn.node, &g_active_usrsock_connections); } - _usrsock_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); return conn; } @@ -149,11 +128,11 @@ FAR struct usrsock_conn_s *usrsock_alloc(void) void usrsock_free(FAR struct usrsock_conn_s *conn) { - /* The free list is protected by a semaphore (that behaves like a mutex). */ + /* The free list is protected by a mutex. */ DEBUGASSERT(conn->crefs == 0); - _usrsock_semtake(&g_free_sem); + nxmutex_lock(&g_free_lock); /* Remove the connection from the active list */ @@ -167,7 +146,7 @@ void usrsock_free(FAR struct usrsock_conn_s *conn) /* Free the connection */ dq_addlast(&conn->sconn.node, &g_free_usrsock_connections); - _usrsock_semgive(&g_free_sem); + nxmutex_unlock(&g_free_lock); } /**************************************************************************** @@ -246,7 +225,7 @@ int usrsock_setup_request_callback(FAR struct usrsock_conn_s *conn, if ((flags & USRSOCK_EVENT_REQ_COMPLETE) != 0) { - _usrsock_semtake(&conn->resp.sem); + nxsem_wait_uninterruptible(&conn->resp.sem); pstate->unlock = true; } @@ -288,7 +267,7 @@ void usrsock_teardown_request_callback(FAR struct usrsock_reqstate_s *pstate) if (pstate->unlock) { - _usrsock_semgive(&conn->resp.sem); + nxsem_post(&conn->resp.sem); } /* Make sure that no further events are processed */ diff --git a/net/usrsock/usrsock_devif.c b/net/usrsock/usrsock_devif.c index c71eb6a76b8b3..1afcf008c75df 100644 --- a/net/usrsock/usrsock_devif.c +++ b/net/usrsock/usrsock_devif.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -48,7 +49,7 @@ struct usrsock_req_s { - sem_t sem; /* Request semaphore (only one outstanding + mutex_t lock; /* Request mutex (only one outstanding * request) */ sem_t acksem; /* Request acknowledgment notification */ uint32_t newxid; /* New transcation Id */ @@ -72,7 +73,7 @@ struct usrsock_req_s static struct usrsock_req_s g_usrsock_req = { - NXSEM_INITIALIZER(1, PRIOINHERIT_FLAGS_DISABLE), + NXMUTEX_INITIALIZER, NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE), 0, 0, @@ -640,7 +641,7 @@ int usrsock_do_request(FAR struct usrsock_conn_s *conn, /* Set outstanding request for daemon to handle. */ - net_lockedwait_uninterruptible(&req->sem); + net_lockedwait_uninterruptible(&req->lock); if (++req->newxid == 0) { ++req->newxid; @@ -667,7 +668,7 @@ int usrsock_do_request(FAR struct usrsock_conn_s *conn, /* Free request line for next command. */ - nxsem_post(&req->sem); + nxmutex_unlock(&req->lock); return ret; } @@ -699,7 +700,7 @@ void usrsock_abort(void) * requests. */ - ret = net_timedwait(&req->sem, 10); + ret = net_timedwait(&req->lock, 10); if (ret < 0) { if (ret != -ETIMEDOUT && ret != -EINTR) @@ -710,7 +711,7 @@ void usrsock_abort(void) } else { - nxsem_post(&req->sem); + nxmutex_unlock(&req->lock); } /* Wake-up pending requests. */ diff --git a/net/utils/net_lock.c b/net/utils/net_lock.c index 2334d80b7480a..3ce295f5b7502 100644 --- a/net/utils/net_lock.c +++ b/net/utils/net_lock.c @@ -50,28 +50,12 @@ * Private Data ****************************************************************************/ -static sem_t g_netlock = SEM_INITIALIZER(1); -static pid_t g_holder = NO_HOLDER; -static unsigned int g_count; +static rmutex_t g_netlock = NXRMUTEX_INITIALIZER; /**************************************************************************** * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: _net_takesem - * - * Description: - * Take the semaphore, waiting indefinitely. - * REVISIT: Should this return if -EINTR? - * - ****************************************************************************/ - -static int _net_takesem(void) -{ - return nxsem_wait_uninterruptible(&g_netlock); -} - /**************************************************************************** * Name: _net_timedwait ****************************************************************************/ @@ -155,32 +139,7 @@ _net_timedwait(sem_t *sem, bool interruptible, unsigned int timeout) int net_lock(void) { - pid_t me = getpid(); - int ret = OK; - - /* Does this thread already hold the semaphore? */ - - if (g_holder == me) - { - /* Yes.. just increment the reference count */ - - g_count++; - } - else - { - /* No.. take the semaphore (perhaps waiting) */ - - ret = _net_takesem(); - if (ret >= 0) - { - /* Now this thread holds the semaphore */ - - g_holder = me; - g_count = 1; - } - } - - return ret; + return nxrmutex_lock(&g_netlock); } /**************************************************************************** @@ -202,30 +161,7 @@ int net_lock(void) int net_trylock(void) { - pid_t me = getpid(); - int ret = OK; - - /* Does this thread already hold the semaphore? */ - - if (g_holder == me) - { - /* Yes.. just increment the reference count */ - - g_count++; - } - else - { - ret = nxsem_trywait(&g_netlock); - if (ret >= 0) - { - /* Now this thread holds the semaphore */ - - g_holder = me; - g_count = 1; - } - } - - return ret; + return nxrmutex_trylock(&g_netlock); } /**************************************************************************** @@ -244,24 +180,7 @@ int net_trylock(void) void net_unlock(void) { - DEBUGASSERT(g_holder == getpid() && g_count > 0); - - /* If the count would go to zero, then release the semaphore */ - - if (g_count == 1) - { - /* We no longer hold the semaphore */ - - g_holder = NO_HOLDER; - g_count = 0; - nxsem_post(&g_netlock); - } - else - { - /* We still hold the semaphore. Just decrement the count */ - - g_count--; - } + nxrmutex_unlock(&g_netlock); } /**************************************************************************** @@ -275,30 +194,8 @@ void net_unlock(void) int net_breaklock(FAR unsigned int *count) { - irqstate_t flags; - pid_t me = getpid(); - int ret = -EPERM; - DEBUGASSERT(count != NULL); - - flags = enter_critical_section(); /* No interrupts */ - if (g_holder == me) - { - /* Return the lock setting */ - - *count = g_count; - - /* Release the network lock */ - - g_holder = NO_HOLDER; - g_count = 0; - - nxsem_post(&g_netlock); - ret = OK; - } - - leave_critical_section(flags); - return ret; + return nxrmutex_breaklock(&g_netlock, count); } /**************************************************************************** @@ -315,21 +212,7 @@ int net_breaklock(FAR unsigned int *count) int net_restorelock(unsigned int count) { - pid_t me = getpid(); - int ret; - - DEBUGASSERT(g_holder != me); - - /* Recover the network lock at the proper count */ - - ret = _net_takesem(); - if (ret >= 0) - { - g_holder = me; - g_count = count; - } - - return ret; + return nxrmutex_restorelock(&g_netlock, count); } /**************************************************************************** diff --git a/sched/group/group_create.c b/sched/group/group_create.c index 319eadffe3c82..98b6eed8d6be6 100644 --- a/sched/group/group_create.c +++ b/sched/group/group_create.c @@ -210,9 +210,9 @@ int group_allocate(FAR struct task_tcb_s *tcb, uint8_t ttype) #endif #ifndef CONFIG_DISABLE_PTHREAD - /* Initialize the pthread join semaphore */ + /* Initialize the pthread join mutex */ - nxsem_init(&group->tg_joinsem, 0, 1); + nxmutex_init(&group->tg_joinlock); #endif #if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT) diff --git a/sched/pthread/pthread_completejoin.c b/sched/pthread/pthread_completejoin.c index 1e76d555a72ce..02dae59b187f9 100644 --- a/sched/pthread/pthread_completejoin.c +++ b/sched/pthread/pthread_completejoin.c @@ -196,12 +196,12 @@ int pthread_completejoin(pid_t pid, FAR void *exit_value) /* First, find thread's structure in the private data set. */ - nxsem_wait_uninterruptible(&group->tg_joinsem); + nxmutex_lock(&group->tg_joinlock); pjoin = pthread_findjoininfo(group, pid); if (!pjoin) { serr("ERROR: Could not find join info, pid=%d\n", pid); - pthread_sem_give(&group->tg_joinsem); + nxmutex_unlock(&group->tg_joinlock); return ERROR; } else @@ -232,7 +232,7 @@ int pthread_completejoin(pid_t pid, FAR void *exit_value) * to call pthread_destroyjoin. */ - pthread_sem_give(&group->tg_joinsem); + nxmutex_unlock(&group->tg_joinlock); } return OK; @@ -250,7 +250,7 @@ int pthread_completejoin(pid_t pid, FAR void *exit_value) * no thread ever calls pthread_join. In case, there is a memory leak! * * Assumptions: - * The caller holds tg_joinsem + * The caller holds tg_joinlock * ****************************************************************************/ diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index 7eff071fad546..bbdafbf58071f 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -558,9 +558,9 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, sched_lock(); if (ret == OK) { - nxsem_wait_uninterruptible(&ptcb->cmn.group->tg_joinsem); + nxmutex_lock(&ptcb->cmn.group->tg_joinlock); pthread_addjoininfo(ptcb->cmn.group, pjoin); - pthread_sem_give(&ptcb->cmn.group->tg_joinsem); + nxmutex_unlock(&ptcb->cmn.group->tg_joinlock); nxtask_activate((FAR struct tcb_s *)ptcb); /* Return the thread information to the caller */ diff --git a/sched/pthread/pthread_detach.c b/sched/pthread/pthread_detach.c index a519234fc1341..8535971084ffb 100644 --- a/sched/pthread/pthread_detach.c +++ b/sched/pthread/pthread_detach.c @@ -72,7 +72,7 @@ int pthread_detach(pthread_t thread) /* Find the entry associated with this pthread. */ - nxsem_wait_uninterruptible(&group->tg_joinsem); + nxmutex_lock(&group->tg_joinlock); pjoin = pthread_findjoininfo(group, (pid_t)thread); if (!pjoin) { @@ -107,7 +107,7 @@ int pthread_detach(pthread_t thread) } } - pthread_sem_give(&group->tg_joinsem); + nxmutex_unlock(&group->tg_joinlock); sinfo("Returning %d\n", ret); return ret; diff --git a/sched/pthread/pthread_join.c b/sched/pthread/pthread_join.c index 887cef5f92ee4..51f2cbcf84e0a 100644 --- a/sched/pthread/pthread_join.c +++ b/sched/pthread/pthread_join.c @@ -100,7 +100,7 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) * because it will also attempt to get this semaphore. */ - nxsem_wait_uninterruptible(&group->tg_joinsem); + nxmutex_lock(&group->tg_joinlock); /* Find the join information associated with this thread. * This can fail for one of three reasons: (1) There is no @@ -134,13 +134,13 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) ret = EINVAL; } - pthread_sem_give(&group->tg_joinsem); + nxmutex_unlock(&group->tg_joinlock); } else { if (pjoin->detached) { - pthread_sem_give(&group->tg_joinsem); + nxmutex_unlock(&group->tg_joinlock); leave_cancellation_point(); return EINVAL; } @@ -191,7 +191,7 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) * semaphore. */ - pthread_sem_give(&group->tg_joinsem); + nxmutex_unlock(&group->tg_joinlock); /* Take the thread's thread exit semaphore. We will sleep here * until the thread exits. We need to exercise caution because @@ -219,7 +219,7 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) * pthread_destroyjoin is called. */ - nxsem_wait_uninterruptible(&group->tg_joinsem); + nxmutex_lock(&group->tg_joinlock); } /* Pre-emption is okay now. The logic still cannot be re-entered @@ -241,7 +241,7 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) pthread_destroyjoin(group, pjoin); } - pthread_sem_give(&group->tg_joinsem); + nxmutex_unlock(&group->tg_joinlock); ret = OK; } diff --git a/sched/pthread/pthread_release.c b/sched/pthread/pthread_release.c index 5c55427de4d2f..c4186177cf99b 100644 --- a/sched/pthread/pthread_release.c +++ b/sched/pthread/pthread_release.c @@ -83,7 +83,7 @@ void pthread_release(FAR struct task_group_s *group) kmm_free(join); } - /* Destroy the join list semaphore */ + /* Destroy the join list mutex */ - nxsem_destroy(&group->tg_joinsem); + nxmutex_destroy(&group->tg_joinlock); } diff --git a/sched/task/spawn.h b/sched/task/spawn.h index 521b065064458..9b75c5a8cc41b 100644 --- a/sched/task/spawn.h +++ b/sched/task/spawn.h @@ -26,6 +26,7 @@ ****************************************************************************/ #include +#include #include /**************************************************************************** @@ -71,7 +72,7 @@ struct spawn_parms_s * Public Data ****************************************************************************/ -extern sem_t g_spawn_parmsem; +extern mutex_t g_spawn_parmlock; #ifndef CONFIG_SCHED_WAITPID extern sem_t g_spawn_execsem; #endif @@ -81,24 +82,6 @@ extern struct spawn_parms_s g_spawn_parms; * Public Function Prototypes ****************************************************************************/ -/**************************************************************************** - * Name: spawn_semtake and spawn_semgive - * - * Description: - * Give and take semaphores - * - * Input Parameters: - * - * sem - The semaphore to act on. - * - * Returned Value: - * None - * - ****************************************************************************/ - -int spawn_semtake(FAR sem_t *sem); -#define spawn_semgive(sem) nxsem_post(sem) - /**************************************************************************** * Name: spawn_execattrs * diff --git a/sched/task/task_posixspawn.c b/sched/task/task_posixspawn.c index cb7f78d51cbd9..8d7e357049858 100644 --- a/sched/task/task_posixspawn.c +++ b/sched/task/task_posixspawn.c @@ -210,7 +210,7 @@ static int nxposix_spawn_proxy(int argc, FAR char *argv[]) g_spawn_parms.result = ret; #ifndef CONFIG_SCHED_WAITPID - spawn_semgive(&g_spawn_execsem); + nxsem_post(&g_spawn_execsem); #endif return OK; } @@ -347,10 +347,10 @@ int posix_spawn(FAR pid_t *pid, FAR const char *path, /* Get exclusive access to the global parameter structure */ - ret = spawn_semtake(&g_spawn_parmsem); + ret = nxmutex_lock(&g_spawn_parmlock); if (ret < 0) { - serr("ERROR: spawn_semtake failed: %d\n", ret); + serr("ERROR: nxmutex_lock failed: %d\n", ret); return -ret; } @@ -370,7 +370,7 @@ int posix_spawn(FAR pid_t *pid, FAR const char *path, if (ret < 0) { serr("ERROR: nxsched_get_param failed: %d\n", ret); - spawn_semgive(&g_spawn_parmsem); + nxmutex_unlock(&g_spawn_parmlock); return -ret; } @@ -414,10 +414,10 @@ int posix_spawn(FAR pid_t *pid, FAR const char *path, goto errout_with_lock; } #else - ret = spawn_semtake(&g_spawn_execsem); + ret = nxsem_wait_uninterruptible(&g_spawn_execsem); if (ret < 0) { - serr("ERROR: spawn_semtake() failed: %d\n", ret); + serr("ERROR: nxsem_wait_uninterruptible() failed: %d\n", ret); goto errout_with_lock; } #endif @@ -430,6 +430,6 @@ int posix_spawn(FAR pid_t *pid, FAR const char *path, #ifdef CONFIG_SCHED_WAITPID sched_unlock(); #endif - spawn_semgive(&g_spawn_parmsem); + nxmutex_unlock(&g_spawn_parmlock); return ret; } diff --git a/sched/task/task_spawn.c b/sched/task/task_spawn.c index af397ceb25997..e7c0e39b29e3d 100644 --- a/sched/task/task_spawn.c +++ b/sched/task/task_spawn.c @@ -239,7 +239,7 @@ static int nxtask_spawn_proxy(int argc, FAR char *argv[]) g_spawn_parms.result = ret; #ifndef CONFIG_SCHED_WAITPID - spawn_semgive(&g_spawn_execsem); + nxsem_post(&g_spawn_execsem); #endif return OK; } @@ -356,10 +356,10 @@ int task_spawn(FAR const char *name, main_t entry, /* Get exclusive access to the global parameter structure */ - ret = spawn_semtake(&g_spawn_parmsem); + ret = nxmutex_lock(&g_spawn_parmlock); if (ret < 0) { - serr("ERROR: spawn_semtake failed: %d\n", ret); + serr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -381,7 +381,7 @@ int task_spawn(FAR const char *name, main_t entry, { serr("ERROR: nxsched_get_param failed: %d\n", ret); g_spawn_parms.pid = NULL; - spawn_semgive(&g_spawn_parmsem); + nxmutex_unlock(&g_spawn_parmlock); return ret; } @@ -427,7 +427,7 @@ int task_spawn(FAR const char *name, main_t entry, goto errout_with_lock; } #else - ret = spawn_semtake(&g_spawn_execsem); + ret = nxsem_wait_uninterruptible(&g_spawn_execsem); if (ret < 0) { serr("ERROR: g_spawn_execsem() failed: %d\n", ret); @@ -450,7 +450,7 @@ int task_spawn(FAR const char *name, main_t entry, sched_unlock(); #endif g_spawn_parms.pid = NULL; - spawn_semgive(&g_spawn_parmsem); + nxmutex_unlock(&g_spawn_parmlock); return ret; } diff --git a/sched/task/task_spawnparms.c b/sched/task/task_spawnparms.c index b5ee997c4874e..e829598972890 100644 --- a/sched/task/task_spawnparms.c +++ b/sched/task/task_spawnparms.c @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include #include @@ -42,9 +42,9 @@ * Public Data ****************************************************************************/ -sem_t g_spawn_parmsem = SEM_INITIALIZER(1); +mutex_t g_spawn_parmlock = NXMUTEX_INITIALIZER; #ifndef CONFIG_SCHED_WAITPID -sem_t g_spawn_execsem = SEM_INITIALIZER(0); +sem_t g_spawn_execsem = NXSEM_INITIALIZER(0, PRIOINHERIT_FLAGS_DISABLE); #endif struct spawn_parms_s g_spawn_parms; @@ -144,26 +144,6 @@ static inline int nxspawn_open(FAR struct spawn_open_file_action_s *action) * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: spawn_semtake and spawn_semgive - * - * Description: - * Give and take semaphores - * - * Input Parameters: - * - * sem - The semaphore to act on. - * - * Returned Value: - * See nxsem_wait_uninterruptible - * - ****************************************************************************/ - -int spawn_semtake(FAR sem_t *sem) -{ - return nxsem_wait_uninterruptible(sem); -} - /**************************************************************************** * Name: spawn_execattrs * diff --git a/sched/task/task_tls_alloc.c b/sched/task/task_tls_alloc.c index 864135305dd31..71f87eaa9b582 100644 --- a/sched/task/task_tls_alloc.c +++ b/sched/task/task_tls_alloc.c @@ -28,7 +28,7 @@ #include #include -#include +#include /**************************************************************************** * Private Data @@ -37,7 +37,7 @@ #if CONFIG_TLS_TASK_NELEM > 0 static tls_task_ndxset_t g_tlsset; -static sem_t g_tlssem = SEM_INITIALIZER(1); +static mutex_t g_tlslock = NXMUTEX_INITIALIZER; static tls_dtor_t g_tlsdtor[CONFIG_TLS_TASK_NELEM]; /**************************************************************************** @@ -63,8 +63,7 @@ int task_tls_alloc(tls_dtor_t dtor) int ret; int candidate; - ret = nxsem_wait(&g_tlssem); - + ret = nxmutex_lock(&g_tlslock); if (ret < 0) { return ret; @@ -84,7 +83,7 @@ int task_tls_alloc(tls_dtor_t dtor) } } - nxsem_post(&g_tlssem); + nxmutex_unlock(&g_tlslock); return ret; } diff --git a/sched/tls/task_initinfo.c b/sched/tls/task_initinfo.c index 761c349d96c14..d7983b9197c32 100644 --- a/sched/tls/task_initinfo.c +++ b/sched/tls/task_initinfo.c @@ -25,7 +25,7 @@ #include #include -#include +#include #include "tls.h" @@ -59,9 +59,9 @@ int task_init_info(FAR struct task_group_s *group) return -ENOMEM; } - /* Initialize user space semaphore */ + /* Initialize user space mutex */ - nxsem_init(&info->ta_sem, 0, 1); + nxmutex_init(&info->ta_lock); group->tg_info = info; return OK; diff --git a/sched/tls/task_uninitinfo.c b/sched/tls/task_uninitinfo.c index bd7e028b4b295..6c28b90559c37 100644 --- a/sched/tls/task_uninitinfo.c +++ b/sched/tls/task_uninitinfo.c @@ -23,7 +23,7 @@ ****************************************************************************/ #include -#include +#include #include "tls.h" @@ -49,6 +49,6 @@ void task_uninit_info(FAR struct task_group_s *group) { FAR struct task_info_s *info = group->tg_info; - nxsem_destroy(&info->ta_sem); + nxmutex_destroy(&info->ta_lock); group_free(group, info); } diff --git a/wireless/bluetooth/bt_conn.c b/wireless/bluetooth/bt_conn.c index c69658a6857ba..68c958723ca0d 100644 --- a/wireless/bluetooth/bt_conn.c +++ b/wireless/bluetooth/bt_conn.c @@ -78,7 +78,7 @@ struct bt_conn_handoff_s static struct bt_conn_s g_conns[CONFIG_BLUETOOTH_MAX_CONN]; static struct bt_conn_handoff_s g_conn_handoff = { - SEM_INITIALIZER(1), + NXSEM_INITIALIZER(1, PRIOINHERIT_FLAGS_DISABLE), NULL }; diff --git a/wireless/bluetooth/bt_ioctl.c b/wireless/bluetooth/bt_ioctl.c index 5e6011cc40cca..8f61e51838284 100644 --- a/wireless/bluetooth/bt_ioctl.c +++ b/wireless/bluetooth/bt_ioctl.c @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -53,7 +54,7 @@ struct btnet_scanstate_s { - sem_t bs_exclsem; /* Manages exclusive access */ + mutex_t bs_lock; /* Manages exclusive access */ volatile bool bs_scanning; /* True: Scanning in progress */ volatile uint8_t bs_head; /* Head of circular list (for removal) */ uint8_t bs_tail; /* Tail of circular list (for addition) */ @@ -159,7 +160,7 @@ static void btnet_scan_callback(FAR const bt_addr_le_t *addr, /* Get exclusive access to the scan data */ - ret = nxsem_wait_uninterruptible(&g_scanstate.bs_exclsem); + ret = nxmutex_lock(&g_scanstate.bs_lock); if (ret < 0) { wlerr("nxsem_wait_uninterruptible() failed: %d\n", ret); @@ -201,7 +202,7 @@ static void btnet_scan_callback(FAR const bt_addr_le_t *addr, memcpy(&rsp->sr_data, adv_data, len); g_scanstate.bs_tail = nexttail; - nxsem_post(&g_scanstate.bs_exclsem); + nxmutex_unlock(&g_scanstate.bs_lock); } /**************************************************************************** @@ -241,7 +242,7 @@ static int btnet_scan_result(FAR struct bt_scanresponse_s *result, { /* Get exclusive access to the scan data */ - ret = nxsem_wait(&g_scanstate.bs_exclsem); + ret = nxmutex_lock(&g_scanstate.bs_lock); if (ret < 0) { return ret; @@ -276,7 +277,7 @@ static int btnet_scan_result(FAR struct bt_scanresponse_s *result, if (scanning) { - nxsem_post(&g_scanstate.bs_exclsem); + nxmutex_unlock(&g_scanstate.bs_lock); } return nrsp; @@ -617,7 +618,7 @@ int btnet_ioctl(FAR struct net_driver_s *netdev, int cmd, unsigned long arg) { /* Initialize scan state */ - nxsem_init(&g_scanstate.bs_exclsem, 0, 1); + nxmutex_init(&g_scanstate.bs_lock); g_scanstate.bs_scanning = true; g_scanstate.bs_head = 0; g_scanstate.bs_tail = 0; @@ -628,7 +629,7 @@ int btnet_ioctl(FAR struct net_driver_s *netdev, int cmd, unsigned long arg) if (ret < 0) { - nxsem_destroy(&g_scanstate.bs_exclsem); + nxmutex_destroy(&g_scanstate.bs_lock); g_scanstate.bs_scanning = false; } } @@ -663,7 +664,7 @@ int btnet_ioctl(FAR struct net_driver_s *netdev, int cmd, unsigned long arg) ret = bt_stop_scanning(); wlinfo("Stop scanning: %d\n", ret); - nxsem_destroy(&g_scanstate.bs_exclsem); + nxmutex_destroy(&g_scanstate.bs_lock); g_scanstate.bs_scanning = false; } break; diff --git a/wireless/bluetooth/bt_netdev.c b/wireless/bluetooth/bt_netdev.c index 656f537902a91..6c21a7dff0f58 100644 --- a/wireless/bluetooth/bt_netdev.c +++ b/wireless/bluetooth/bt_netdev.c @@ -103,7 +103,6 @@ struct btnet_driver_s /* For internal use by this driver */ - sem_t bd_exclsem; /* Exclusive access to struct */ bool bd_bifup; /* true:ifup false:ifdown */ struct work_s bd_pollwork; /* Defer poll work to the work queue */ @@ -1249,10 +1248,6 @@ int bt_netdev_register(FAR struct bt_driver_s *btdev) bt_hci_cb_register(hcicb); #endif - /* Setup a locking semaphore for exclusive device driver access */ - - nxsem_init(&priv->bd_exclsem, 0, 1); - /* Set the network mask. */ btnet_netmask(netdev); @@ -1321,10 +1316,6 @@ int bt_netdev_register(FAR struct bt_driver_s *btdev) errout: - /* Un-initialize semaphores */ - - nxsem_destroy(&priv->bd_exclsem); - /* Free memory and return the error */ kmm_free(priv); diff --git a/wireless/ieee802154/Kconfig b/wireless/ieee802154/Kconfig index ce5df88b1f523..c1adbbb3e70d3 100644 --- a/wireless/ieee802154/Kconfig +++ b/wireless/ieee802154/Kconfig @@ -91,13 +91,6 @@ config MAC802154_SFEVENT_VERBOSE ---help--- Enable verbose logging of superframe events Default: false -config MAC802154_LOCK_VERBOSE - bool "Verbose logging related to MAC lock management" - default n - depends on DEBUG_WIRELESS_INFO - ---help--- - Enable verbose logging of MAC lock management. Default: false - config IEEE802154_MACDEV bool "Character driver for IEEE 802.15.4 MAC layer" default n diff --git a/wireless/ieee802154/mac802154.c b/wireless/ieee802154/mac802154.c index adc4e7bae56b4..1dba8a23bccf6 100644 --- a/wireless/ieee802154/mac802154.c +++ b/wireless/ieee802154/mac802154.c @@ -140,8 +140,7 @@ static void mac802154_resetqueues(FAR struct ieee802154_privmac_s *priv) ****************************************************************************/ int mac802154_txdesc_alloc(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_txdesc_s **txdesc, - bool allow_interrupt) + FAR struct ieee802154_txdesc_s **txdesc) { int ret; FAR struct ieee802154_primitive_s *primitive; @@ -162,19 +161,19 @@ int mac802154_txdesc_alloc(FAR struct ieee802154_privmac_s *priv, { /* Unlock MAC so that other work can be done to free a notification */ - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); /* Take a count from the tx desc semaphore, waiting if necessary. We * only return from here with an error if we are allowing interruptions * and we received a signal. */ - ret = mac802154_takesem(&priv->txdesc_sem, allow_interrupt); + ret = nxsem_wait_uninterruptible(&priv->txdesc_sem); if (ret < 0) { /* MAC is already released */ - wlwarn("WARNING: mac802154_takesem failed: %d\n", ret); + wlwarn("WARNING: nxsem_wait_uninterruptible failed: %d\n", ret); return ret; } @@ -183,12 +182,12 @@ int mac802154_txdesc_alloc(FAR struct ieee802154_privmac_s *priv, * re-lock the MAC in order to ensure this happens correctly. */ - ret = mac802154_lock(priv, allow_interrupt); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - wlwarn("WARNING: mac802154_lock failed: %d\n", ret); + wlwarn("WARNING: nxmutex_lock failed: %d\n", ret); - mac802154_givesem(&priv->txdesc_sem); + nxsem_post(&priv->txdesc_sem); return ret; } @@ -377,10 +376,10 @@ static void mac802154_notify_worker(FAR void *arg) FAR struct ieee802154_primitive_s *primitive; int ret; - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); primitive = (FAR struct ieee802154_primitive_s *)sq_remfirst(&priv->primitive_queue); - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); while (primitive != NULL) { @@ -450,10 +449,10 @@ static void mac802154_notify_worker(FAR void *arg) /* Get the next primitive then loop */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); primitive = (FAR struct ieee802154_primitive_s *) sq_remfirst(&priv->primitive_queue); - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); } } @@ -722,7 +721,7 @@ static void mac802154_purge_worker(FAR void *arg) * signals so don't allow interruptions */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); while (1) { @@ -768,7 +767,7 @@ static void mac802154_purge_worker(FAR void *arg) } } - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -796,7 +795,7 @@ static int /* Get exclusive access to the driver structure. Ignore EINTR signals */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); if (gts) { @@ -813,7 +812,7 @@ static int sq_remfirst(&priv->csma_queue); } - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); if (*txdesc != NULL) { @@ -851,11 +850,11 @@ static void mac802154_txdone(FAR const struct ieee802154_radiocb_s *radiocb, * signals so don't allow interruptions */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); sq_addlast((FAR sq_entry_t *)txdesc, &priv->txdone_queue); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); /* Schedule work with the work queue to process the completion further */ @@ -887,7 +886,7 @@ static void mac802154_txdone_worker(FAR void *arg) * signals so don't allow interruptions */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); while (1) { @@ -995,7 +994,7 @@ static void mac802154_txdone_worker(FAR void *arg) mac802154_txdesc_free(priv, txdesc); } - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -1026,7 +1025,7 @@ static void mac802154_rxframe(FAR const struct ieee802154_radiocb_s *radiocb, * signals so if we see one, just go back to trying to get access again. */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* Push the iob onto the tail of the frame list for processing */ @@ -1034,7 +1033,7 @@ static void mac802154_rxframe(FAR const struct ieee802154_radiocb_s *radiocb, wlinfo("Frame received\n"); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); /* Schedule work with the work queue to process the completion further */ @@ -1073,7 +1072,7 @@ static void mac802154_rxframe_worker(FAR void *arg) * again. */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* Pop the data indication from the head of the frame list for * processing. Note: dataind_queue contains ieee802154_primitive_s @@ -1085,7 +1084,7 @@ static void mac802154_rxframe_worker(FAR void *arg) /* Once we pop off the indication, we needn't to keep the mac locked */ - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); if (ind == NULL) { @@ -1284,7 +1283,7 @@ static void mac802154_rxdataframe(FAR struct ieee802154_privmac_s *priv, /* Get exclusive access to the MAC */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* If we are currently performing a POLL operation and we've * received a data response, use the addressing information @@ -1408,7 +1407,7 @@ static void mac802154_rxdataframe(FAR struct ieee802154_privmac_s *priv, priv->curr_op = MAC802154_OP_NONE; priv->cmd_desc = NULL; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); /* Release the MAC and notify the next highest layer */ @@ -1431,7 +1430,7 @@ static void mac802154_rxdataframe(FAR struct ieee802154_privmac_s *priv, mac802154_notify(priv, (FAR struct ieee802154_primitive_s *)ind); } - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -1452,7 +1451,7 @@ static void mac802154_rxdatareq(FAR struct ieee802154_privmac_s *priv, /* Get exclusive access to the MAC */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* Search the list of indirect transactions to see if there are any waiting * for the requesting device. @@ -1492,7 +1491,7 @@ static void mac802154_rxdatareq(FAR struct ieee802154_privmac_s *priv, priv->radio->txdelayed(priv->radio, txdesc, 0); priv->beaconupdate = true; - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); return; } } @@ -1509,7 +1508,7 @@ static void mac802154_rxdatareq(FAR struct ieee802154_privmac_s *priv, priv->radio->txdelayed(priv->radio, txdesc, 0); priv->beaconupdate = true; - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); return; } } @@ -1614,13 +1613,13 @@ static void mac802154_rxdatareq(FAR struct ieee802154_privmac_s *priv, /* Allocate the txdesc, waiting if necessary, allow interruptions */ - mac802154_txdesc_alloc(priv, &txdesc, false); + mac802154_txdesc_alloc(priv, &txdesc); txdesc->frame = iob; txdesc->frametype = IEEE802154_FRAME_DATA; txdesc->ackreq = false; - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); priv->radio->txdelayed(priv->radio, txdesc, 0); } @@ -1651,7 +1650,7 @@ mac802154_edresult(FAR const struct ieee802154_radiocb_s *radiocb, * signals so if we see one, just go back to trying to get access again. */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* If we are actively performing a scan operation, notify the handler */ @@ -1662,7 +1661,7 @@ mac802154_edresult(FAR const struct ieee802154_radiocb_s *radiocb, /* Relinquish control of the private structure */ - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); } static void mac802154_sfevent(FAR const struct ieee802154_radiocb_s *radiocb, @@ -1679,7 +1678,7 @@ static void mac802154_sfevent(FAR const struct ieee802154_radiocb_s *radiocb, * signals so if we see one, just go back to trying to get access again. */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); switch (sfevent) { @@ -1705,7 +1704,7 @@ static void mac802154_sfevent(FAR const struct ieee802154_radiocb_s *radiocb, break; } - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -1902,7 +1901,7 @@ static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, /* At this point, all relevant info is extracted from the incoming frame */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); if (priv->curr_op == MAC802154_OP_SCAN) { @@ -1925,7 +1924,7 @@ static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, /* The beacon is the same as another, so discard it */ ieee802154_primitive_free(primitive); - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); return; } @@ -1980,7 +1979,7 @@ static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, if (priv->curr_op == MAC802154_OP_ASSOC && pending_eaddr) { priv->curr_cmd = IEEE802154_CMD_DATA_REQ; - mac802154_txdesc_alloc(priv, &respdesc, false); + mac802154_txdesc_alloc(priv, &respdesc); mac802154_createdatareq(priv, &priv->pandesc.coordaddr, IEEE802154_ADDRMODE_EXTENDED, respdesc); @@ -2015,7 +2014,7 @@ static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, if (pending_saddr | pending_eaddr) { - mac802154_txdesc_alloc(priv, &respdesc, false); + mac802154_txdesc_alloc(priv, &respdesc); if (priv->curr_op == MAC802154_OP_POLL) { @@ -2028,7 +2027,7 @@ static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, else if (priv->curr_op == MAC802154_OP_NONE) { DEBUGASSERT(priv->opsem.semcount == 1); - mac802154_takesem(&priv->opsem, false); + nxsem_wait_uninterruptible(&priv->opsem); priv->curr_op = MAC802154_OP_AUTOEXTRACT; priv->curr_cmd = IEEE802154_CMD_DATA_REQ; } @@ -2061,7 +2060,7 @@ static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, if (beacon->payloadlength > 0) { - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); return; } } @@ -2074,13 +2073,13 @@ static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, */ mac802154_notify(priv, primitive); - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); return; /* Return so that we don't free the primitive */ } } } - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); ieee802154_primitive_free(primitive); return; @@ -2136,7 +2135,7 @@ MACHANDLE mac802154_create(FAR struct ieee802154_radio_s *radiodev) /* Allow exclusive access to the privmac struct */ - nxsem_init(&mac->exclsem, 0, 1); + nxmutex_init(&mac->lock); /* Allow exclusive access to the dedicated command transaction */ diff --git a/wireless/ieee802154/mac802154.h b/wireless/ieee802154/mac802154.h index d2881aa724098..36377ba38724d 100644 --- a/wireless/ieee802154/mac802154.h +++ b/wireless/ieee802154/mac802154.h @@ -138,7 +138,7 @@ int mac802154_get_mhrlen(MACHANDLE mac, int mac802154_req_data(MACHANDLE mac, FAR const struct ieee802154_frame_meta_s *meta, - FAR struct iob_s *frame, bool allowinterrupt); + FAR struct iob_s *frame); /**************************************************************************** * Name: mac802154_req_purge diff --git a/wireless/ieee802154/mac802154_assoc.c b/wireless/ieee802154/mac802154_assoc.c index ec23e934b91a7..8eaaf0bfd3ca2 100644 --- a/wireless/ieee802154/mac802154_assoc.c +++ b/wireless/ieee802154/mac802154_assoc.c @@ -86,7 +86,7 @@ int mac802154_req_associate(MACHANDLE mac, * cmdtrans but needs access to the MAC in order to unlock it. */ - ret = mac802154_takesem(&priv->opsem, true); + ret = nxsem_wait_uninterruptible(&priv->opsem); if (ret < 0) { return ret; @@ -97,10 +97,10 @@ int mac802154_req_associate(MACHANDLE mac, /* Get exclusive access to the MAC */ - ret = mac802154_lock(priv, true); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); return ret; } @@ -142,12 +142,12 @@ int mac802154_req_associate(MACHANDLE mac, /* Allocate the txdesc, waiting if necessary */ - ret = mac802154_txdesc_alloc(priv, &txdesc, true); + ret = mac802154_txdesc_alloc(priv, &txdesc); if (ret < 0) { iob_free(iob); - mac802154_unlock(priv) - mac802154_givesem(&priv->opsem); + nxmutex_unlock(&priv->lock); + nxsem_post(&priv->opsem); return ret; } @@ -300,8 +300,7 @@ int mac802154_req_associate(MACHANDLE mac, /* We no longer need to have the MAC layer locked. */ - mac802154_unlock(priv) - + nxmutex_unlock(&priv->lock); return OK; } @@ -398,7 +397,7 @@ int mac802154_resp_associate(MACHANDLE mac, /* Get exclusive access to the MAC */ - ret = mac802154_lock(priv, true); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { iob_free(iob); @@ -407,11 +406,11 @@ int mac802154_resp_associate(MACHANDLE mac, /* Allocate the txdesc, waiting if necessary */ - ret = mac802154_txdesc_alloc(priv, &txdesc, true); + ret = mac802154_txdesc_alloc(priv, &txdesc); if (ret < 0) { iob_free(iob); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); return ret; } @@ -425,8 +424,7 @@ int mac802154_resp_associate(MACHANDLE mac, mac802154_setupindirect(priv, txdesc); - mac802154_unlock(priv) - + nxmutex_unlock(&priv->lock); return OK; } @@ -484,7 +482,7 @@ void mac802154_txdone_assocreq(FAR struct ieee802154_privmac_s *priv, priv->curr_op = MAC802154_OP_NONE; priv->cmd_desc = NULL; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); /* Release the MAC, call the callback, get exclusive access again */ @@ -623,7 +621,7 @@ void mac802154_txdone_datareq_assoc(FAR struct ieee802154_privmac_s *priv, priv->curr_op = MAC802154_OP_NONE; priv->cmd_desc = NULL; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); mac802154_notify(priv, primitive); } @@ -719,12 +717,12 @@ void mac802154_rx_assocreq(FAR struct ieee802154_privmac_s *priv, /* Get exclusive access to the MAC */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* Notify the next highest layer of the association status */ mac802154_notify(priv, primitive); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -775,7 +773,7 @@ void mac802154_rx_assocresp(FAR struct ieee802154_privmac_s *priv, /* Get exclusive access to the MAC */ - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* Parse the short address from the response */ @@ -818,13 +816,13 @@ void mac802154_rx_assocresp(FAR struct ieee802154_privmac_s *priv, priv->curr_op = MAC802154_OP_NONE; priv->cmd_desc = NULL; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); mac802154_rxdisable(priv); /* Notify the next highest layer of the association status */ mac802154_notify(priv, primitive); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -882,12 +880,12 @@ static void mac802154_assoctimeout(FAR void *arg) priv->curr_op = MAC802154_OP_NONE; priv->cmd_desc = NULL; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); mac802154_rxdisable(priv); - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); mac802154_notify(priv, primitive); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -908,14 +906,14 @@ static void mac802154_extract_assocresp(FAR void *arg) (FAR struct ieee802154_privmac_s *)arg; FAR struct ieee802154_txdesc_s *respdesc; - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); - mac802154_txdesc_alloc(priv, &respdesc, false); + mac802154_txdesc_alloc(priv, &respdesc); mac802154_createdatareq(priv, &priv->pandesc.coordaddr, IEEE802154_ADDRMODE_EXTENDED, respdesc); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); priv->curr_cmd = IEEE802154_CMD_DATA_REQ; diff --git a/wireless/ieee802154/mac802154_data.c b/wireless/ieee802154/mac802154_data.c index 9b7d8c13df328..8d4729ce43378 100644 --- a/wireless/ieee802154/mac802154_data.c +++ b/wireless/ieee802154/mac802154_data.c @@ -55,7 +55,7 @@ int mac802154_req_data(MACHANDLE mac, FAR const struct ieee802154_frame_meta_s *meta, - FAR struct iob_s *frame, bool allowinterrupt) + FAR struct iob_s *frame) { FAR struct ieee802154_privmac_s *priv = (FAR struct ieee802154_privmac_s *)mac; @@ -141,12 +141,12 @@ int mac802154_req_data(MACHANDLE mac, /* From this point on, we need exclusive access to the privmac struct */ - ret = mac802154_lock(priv, allowinterrupt); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { /* Should only fail if interrupted by a signal */ - wlwarn("WARNING: mac802154_takesem failed: %d\n", ret); + wlwarn("WARNING: nxmutex_lock failed: %d\n", ret); return ret; } @@ -212,7 +212,7 @@ int mac802154_req_data(MACHANDLE mac, if (priv->devmode != IEEE802154_DEVMODE_PANCOORD) { ret = -EINVAL; - goto errout_with_sem; + goto errout_with_lock; } } @@ -238,15 +238,15 @@ int mac802154_req_data(MACHANDLE mac, /* Allocate the txdesc, waiting if necessary, allow interruptions */ - ret = mac802154_txdesc_alloc(priv, &txdesc, true); + ret = mac802154_txdesc_alloc(priv, &txdesc); if (ret < 0) { /* Should only fail if interrupted by a signal while re-acquiring - * exclsem. So the lock is not held if a failure is returned. + * lock. So the lock is not held if a failure is returned. */ wlwarn("WARNING: mac802154_txdesc_alloc failed: %d\n", ret); - return ret; + goto errout_with_lock; } /* Set the offset to 0 to include the header ( we do not want to @@ -320,7 +320,7 @@ int mac802154_req_data(MACHANDLE mac, memcpy(&txdesc->destaddr, &meta->destaddr, sizeof(struct ieee802154_addr_s)); mac802154_setupindirect(priv, txdesc); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); } else { @@ -336,7 +336,7 @@ int mac802154_req_data(MACHANDLE mac, /* We no longer need to have the MAC layer locked. */ - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); /* Notify the radio driver that there is data available */ @@ -353,8 +353,8 @@ int mac802154_req_data(MACHANDLE mac, txdesc->frame = NULL; mac802154_txdesc_free(priv, txdesc); -errout_with_sem: - mac802154_unlock(priv) +errout_with_lock: + nxmutex_unlock(&priv->lock); return ret; } diff --git a/wireless/ieee802154/mac802154_device.c b/wireless/ieee802154/mac802154_device.c index 5f87d753a01f6..04c574dccd437 100644 --- a/wireless/ieee802154/mac802154_device.c +++ b/wireless/ieee802154/mac802154_device.c @@ -84,7 +84,7 @@ struct mac802154_chardevice_s { MACHANDLE md_mac; /* Saved binding to the mac layer */ struct mac802154dev_callback_s md_cb; /* Callback information */ - sem_t md_exclsem; /* Exclusive device access */ + sem_t md_lock; /* Exclusive device access */ /* Hold a list of events */ @@ -117,11 +117,6 @@ struct mac802154_chardevice_s * Private Function Prototypes ****************************************************************************/ -/* Semaphore helpers */ - -static inline int mac802154dev_takesem(sem_t *sem); -#define mac802154dev_givesem(s) nxsem_post(s); - static int mac802154dev_notify(FAR struct mac802154_maccb_s *maccb, FAR struct ieee802154_primitive_s *primitive); static int mac802154dev_rxframe(FAR struct mac802154_chardevice_s *dev, @@ -158,19 +153,6 @@ static const struct file_operations mac802154dev_fops = * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: mac802154dev_semtake - * - * Description: - * Acquire the semaphore used for access serialization. - * - ****************************************************************************/ - -static inline int mac802154dev_takesem(sem_t *sem) -{ - return nxsem_wait(sem); -} - /**************************************************************************** * Name: mac802154dev_open * @@ -194,10 +176,10 @@ static int mac802154dev_open(FAR struct file *filep) /* Get exclusive access to the MAC driver data structure */ - ret = mac802154dev_takesem(&dev->md_exclsem); + ret = nxmutex_lock(&dev->md_lock); if (ret < 0) { - wlerr("ERROR: mac802154dev_takesem failed: %d\n", ret); + wlerr("ERROR: nxsem_wait failed: %d\n", ret); return ret; } @@ -210,7 +192,7 @@ static int mac802154dev_open(FAR struct file *filep) { wlerr("ERROR: Failed to allocate new open struct\n"); ret = -ENOMEM; - goto errout_with_sem; + goto errout_with_lock; } /* Attach the open struct to the device */ @@ -223,8 +205,8 @@ static int mac802154dev_open(FAR struct file *filep) filep->f_priv = (FAR void *)opriv; ret = OK; -errout_with_sem: - mac802154dev_givesem(&dev->md_exclsem); +errout_with_lock: + nxmutex_unlock(&dev->md_lock); return ret; } @@ -277,10 +259,10 @@ static int mac802154dev_close(FAR struct file *filep) /* Get exclusive access to the driver structure */ - ret = mac802154dev_takesem(&dev->md_exclsem); + ret = nxmutex_lock(&dev->md_lock); if (ret < 0) { - wlerr("ERROR: mac802154_takesem failed: %d\n", ret); + wlerr("ERROR: nxsem_wait failed: %d\n", ret); return ret; } @@ -337,7 +319,7 @@ static int mac802154dev_close(FAR struct file *filep) ret = OK; errout_with_exclsem: - mac802154dev_givesem(&dev->md_exclsem); + nxmutex_unlock(&dev->md_lock); return ret; } @@ -380,10 +362,10 @@ static ssize_t mac802154dev_read(FAR struct file *filep, FAR char *buffer, { /* Get exclusive access to the driver structure */ - ret = mac802154dev_takesem(&dev->md_exclsem); + ret = nxmutex_lock(&dev->md_lock); if (ret < 0) { - wlerr("ERROR: mac802154dev_takesem failed: %d\n", ret); + wlerr("ERROR: nxsem_wait failed: %d\n", ret); return ret; } @@ -398,7 +380,7 @@ static ssize_t mac802154dev_read(FAR struct file *filep, FAR char *buffer, if (ind != NULL) { - mac802154dev_givesem(&dev->md_exclsem); + nxmutex_unlock(&dev->md_lock); break; } @@ -412,12 +394,12 @@ static ssize_t mac802154dev_read(FAR struct file *filep, FAR char *buffer, if ((filep->f_oflags & O_NONBLOCK) || dev->readpending) { - mac802154dev_givesem(&dev->md_exclsem); + nxmutex_unlock(&dev->md_lock); return -EAGAIN; } dev->readpending = true; - mac802154dev_givesem(&dev->md_exclsem); + nxmutex_unlock(&dev->md_lock); /* Wait to be signaled when a frame is added to the list */ @@ -551,7 +533,7 @@ static ssize_t mac802154dev_write(FAR struct file *filep, /* Pass the request to the MAC layer */ - ret = mac802154_req_data(dev->md_mac, &tx->meta, iob, true); + ret = mac802154_req_data(dev->md_mac, &tx->meta, iob); if (ret < 0) { iob_free(iob); @@ -587,10 +569,10 @@ static int mac802154dev_ioctl(FAR struct file *filep, int cmd, /* Get exclusive access to the driver structure */ - ret = mac802154dev_takesem(&dev->md_exclsem); + ret = nxmutex_lock(&dev->md_lock); if (ret < 0) { - wlerr("ERROR: mac802154dev_takesem failed: %d\n", ret); + wlerr("ERROR: nxsem_wait failed: %d\n", ret); return ret; } @@ -660,7 +642,7 @@ static int mac802154dev_ioctl(FAR struct file *filep, int cmd, } dev->geteventpending = true; - mac802154dev_givesem(&dev->md_exclsem); + nxmutex_unlock(&dev->md_lock); /* Wait to be signaled when an event is queued */ @@ -675,10 +657,10 @@ static int mac802154dev_ioctl(FAR struct file *filep, int cmd, * and pop an event off the queue */ - ret = mac802154dev_takesem(&dev->md_exclsem); + ret = nxmutex_lock(&dev->md_lock); if (ret < 0) { - wlerr("ERROR: mac802154dev_takesem failed: %d\n", ret); + wlerr("ERROR: nxsem_wait failed: %d\n", ret); return ret; } } @@ -701,7 +683,7 @@ static int mac802154dev_ioctl(FAR struct file *filep, int cmd, break; } - mac802154dev_givesem(&dev->md_exclsem); + nxmutex_unlock(&dev->md_lock); return ret; } @@ -736,7 +718,7 @@ static int mac802154dev_notify(FAR struct mac802154_maccb_s *maccb, * again */ - while (mac802154dev_takesem(&dev->md_exclsem) != 0); + while (nxmutex_lock(&dev->md_lock) != 0); sq_addlast((FAR sq_entry_t *)primitive, &dev->primitive_queue); @@ -757,7 +739,7 @@ static int mac802154dev_notify(FAR struct mac802154_maccb_s *maccb, SI_QUEUE, &dev->md_notify_work); } - mac802154dev_givesem(&dev->md_exclsem); + nxmutex_unlock(&dev->md_lock); return OK; } @@ -787,7 +769,7 @@ static int mac802154dev_rxframe(FAR struct mac802154_chardevice_s *dev, * signals so if we see one, just go back to trying to get access again */ - while (mac802154dev_takesem(&dev->md_exclsem) != 0); + while (nxmutex_lock(&dev->md_lock) != 0); /* Push the indication onto the list */ @@ -805,7 +787,7 @@ static int mac802154dev_rxframe(FAR struct mac802154_chardevice_s *dev, /* Release the driver */ - mac802154dev_givesem(&dev->md_exclsem); + nxmutex_unlock(&dev->md_lock); return OK; } @@ -848,8 +830,8 @@ int mac802154dev_register(MACHANDLE mac, int minor) /* Initialize the new mac driver instance */ dev->md_mac = mac; - nxsem_init(&dev->md_exclsem, 0, 1); /* Allow the device to be opened once - * before blocking */ + nxmutex_init(&dev->md_lock); /* Allow the device to be opened once + * before blocking */ nxsem_init(&dev->readsem, 0, 0); nxsem_set_protocol(&dev->readsem, SEM_PRIO_NONE); @@ -904,7 +886,7 @@ int mac802154dev_register(MACHANDLE mac, int minor) return OK; errout_with_priv: - nxsem_destroy(&dev->md_exclsem); + nxmutex_destroy(&dev->md_lock); kmm_free(dev); return ret; } diff --git a/wireless/ieee802154/mac802154_internal.h b/wireless/ieee802154/mac802154_internal.h index c9ed38e820af3..4f3b6835e0aaf 100644 --- a/wireless/ieee802154/mac802154_internal.h +++ b/wireless/ieee802154/mac802154_internal.h @@ -35,6 +35,7 @@ #include #include +#include #include #include @@ -104,8 +105,8 @@ struct ieee802154_privmac_s FAR struct mac802154_maccb_s *cb; /* Head of a list of MAC callbacks */ FAR struct mac802154_radiocb_s radiocb; /* Interface to bind to radio */ - sem_t exclsem; /* Support exclusive access */ - uint8_t nclients; /* Number of notification clients */ + mutex_t lock; /* Support exclusive access */ + uint8_t nclients; /* Number of notification clients */ /* Only support a single command at any given time. As of now I see no * condition where you need to have more than one command frame @@ -305,7 +306,7 @@ struct ieee802154_privmac_s ****************************************************************************/ int mac802154_txdesc_alloc(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_txdesc_s **txdesc, bool allow_interrupt); + FAR struct ieee802154_txdesc_s **txdesc); void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, FAR struct ieee802154_txdesc_s *txdesc); @@ -510,62 +511,12 @@ void mac802154_notify(FAR struct ieee802154_privmac_s *priv, /* General helpers **********************************************************/ -#define mac802154_givesem(s) nxsem_post(s) - -static inline int mac802154_takesem(sem_t *sem, bool allowinterrupt) -{ - if (allowinterrupt) - { - return nxsem_wait(sem); - } - else - { - return nxsem_wait_uninterruptible(sem); - } -} - -#ifdef CONFIG_MAC802154_LOCK_VERBOSE -#define mac802154_unlock(dev) \ - mac802154_givesem(&dev->exclsem); \ - wlinfo("MAC unlocked\n"); -#else -#define mac802154_unlock(dev) \ - mac802154_givesem(&dev->exclsem); -#endif - -#define mac802154_lock(dev, allowinterrupt) \ - mac802154_lockpriv(dev, allowinterrupt, __FUNCTION__) - -static inline int -mac802154_lockpriv(FAR struct ieee802154_privmac_s *dev, - bool allowinterrupt, FAR const char *funcname) -{ - int ret; - -#ifdef CONFIG_MAC802154_LOCK_VERBOSE - wlinfo("Locking MAC: %s\n", funcname); -#endif - ret = mac802154_takesem(&dev->exclsem, allowinterrupt); - if (ret < 0) - { - wlwarn("Failed to lock MAC\n"); - } - else - { -#ifdef CONFIG_MAC802154_LOCK_VERBOSE - wlinfo("MAC locked\n"); -#endif - } - - return ret; -} - static inline void mac802154_txdesc_free(FAR struct ieee802154_privmac_s *priv, FAR struct ieee802154_txdesc_s *txdesc) { sq_addlast((FAR sq_entry_t *)txdesc, &priv->txdesc_queue); - mac802154_givesem(&priv->txdesc_sem); + nxsem_post(&priv->txdesc_sem); } /**************************************************************************** diff --git a/wireless/ieee802154/mac802154_netdev.c b/wireless/ieee802154/mac802154_netdev.c index 52f7a8e61952a..ce3ac93594ebd 100644 --- a/wireless/ieee802154/mac802154_netdev.c +++ b/wireless/ieee802154/mac802154_netdev.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -130,7 +131,7 @@ struct macnet_driver_s /* For internal use by this driver */ - sem_t md_exclsem; /* Exclusive access to struct */ + mutex_t md_lock; /* Exclusive access to struct */ struct macnet_callback_s md_cb; /* Callback information */ MACHANDLE md_mac; /* Contained MAC interface */ bool md_bifup; /* true:ifup false:ifdown */ @@ -362,7 +363,7 @@ static int macnet_notify(FAR struct mac802154_maccb_s *maccb, * back to trying to get access again */ - while (nxsem_wait(&priv->md_exclsem) < 0); + while (nxmutex_lock(&priv->md_lock) < 0); sq_addlast((FAR sq_entry_t *)primitive, &priv->primitive_queue); @@ -383,7 +384,7 @@ static int macnet_notify(FAR struct mac802154_maccb_s *maccb, SI_QUEUE, &priv->md_notify_work); } - nxsem_post(&priv->md_exclsem); + nxmutex_unlock(&priv->md_lock); return OK; } @@ -904,10 +905,10 @@ static int macnet_ioctl(FAR struct net_driver_s *dev, int cmd, dev->d_private; int ret = -EINVAL; - ret = nxsem_wait(&priv->md_exclsem); + ret = nxmutex_lock(&priv->md_lock); if (ret < 0) { - wlerr("ERROR: nxsem_wait failed: %d\n", ret); + wlerr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } @@ -981,7 +982,7 @@ static int macnet_ioctl(FAR struct net_driver_s *dev, int cmd, } priv->md_eventpending = true; - nxsem_post(&priv->md_exclsem); + nxmutex_unlock(&priv->md_lock); /* Wait to be signaled when an event is queued */ @@ -996,10 +997,10 @@ static int macnet_ioctl(FAR struct net_driver_s *dev, int cmd, * and try and pop an event off the queue */ - ret = nxsem_wait(&priv->md_exclsem); + ret = nxmutex_lock(&priv->md_lock); if (ret < 0) { - wlerr("ERROR: nxsem_wait failed: %d\n", ret); + wlerr("ERROR: nxmutex_lock failed: %d\n", ret); return ret; } } @@ -1031,7 +1032,7 @@ static int macnet_ioctl(FAR struct net_driver_s *dev, int cmd, ret = mac802154_ioctl(priv->md_mac, cmd, arg); } - nxsem_post(&priv->md_exclsem); + nxmutex_unlock(&priv->md_lock); return ret; } #endif @@ -1112,7 +1113,7 @@ static int macnet_req_data(FAR struct radio_driver_s *netdev, /* Transfer the frame to the MAC. */ - ret = mac802154_req_data(priv->md_mac, pktmeta, iob, false); + ret = mac802154_req_data(priv->md_mac, pktmeta, iob); if (ret < 0) { wlerr("ERROR: mac802154_req_data failed: %d\n", ret); @@ -1266,9 +1267,9 @@ int mac802154netdev_register(MACHANDLE mac) dev->d_private = priv; /* Used to recover private state from dev */ priv->md_mac = mac; /* Save the MAC interface instance */ - /* Setup a locking semaphore for exclusive device driver access */ + /* Setup a locking mutex for exclusive device driver access */ - nxsem_init(&priv->md_exclsem, 0, 1); + nxmutex_init(&priv->md_lock); /* Set the network mask. */ diff --git a/wireless/ieee802154/mac802154_poll.c b/wireless/ieee802154/mac802154_poll.c index 9cabe622efde1..63fb90802e3f3 100644 --- a/wireless/ieee802154/mac802154_poll.c +++ b/wireless/ieee802154/mac802154_poll.c @@ -78,7 +78,7 @@ int mac802154_req_poll(MACHANDLE mac, FAR struct ieee802154_poll_req_s *req) * cmdtrans but needs access to the MAC in order to unlock it. */ - ret = mac802154_takesem(&priv->opsem, true); + ret = nxsem_wait_uninterruptible(&priv->opsem); if (ret < 0) { return ret; @@ -86,10 +86,10 @@ int mac802154_req_poll(MACHANDLE mac, FAR struct ieee802154_poll_req_s *req) /* Get exclusive access to the MAC */ - ret = mac802154_lock(priv, true); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); return ret; } @@ -98,11 +98,11 @@ int mac802154_req_poll(MACHANDLE mac, FAR struct ieee802154_poll_req_s *req) /* Allocate the txdesc, waiting if necessary */ - ret = mac802154_txdesc_alloc(priv, &txdesc, true); + ret = mac802154_txdesc_alloc(priv, &txdesc); if (ret < 0) { - mac802154_unlock(priv) - mac802154_givesem(&priv->opsem); + nxmutex_unlock(&priv->lock); + nxsem_post(&priv->opsem); return ret; } @@ -148,7 +148,7 @@ int mac802154_req_poll(MACHANDLE mac, FAR struct ieee802154_poll_req_s *req) /* We no longer need to have the MAC layer locked. */ - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); /* Notify the radio driver that there is data available */ @@ -208,7 +208,7 @@ void mac802154_txdone_datareq_poll(FAR struct ieee802154_privmac_s *priv, priv->curr_op = MAC802154_OP_NONE; priv->cmd_desc = NULL; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); mac802154_notify(priv, primitive); } @@ -268,14 +268,14 @@ void mac802154_polltimeout(FAR void *arg) primitive->type = IEEE802154_PRIMITIVE_CONF_POLL; primitive->u.pollconf.status = IEEE802154_STATUS_NO_DATA; - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* We are no longer performing the association operation */ priv->curr_op = MAC802154_OP_NONE; priv->cmd_desc = NULL; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); mac802154_notify(priv, primitive); - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); } diff --git a/wireless/ieee802154/mac802154_rxenable.c b/wireless/ieee802154/mac802154_rxenable.c index 2b2ad0e558086..401d58ce45876 100644 --- a/wireless/ieee802154/mac802154_rxenable.c +++ b/wireless/ieee802154/mac802154_rxenable.c @@ -60,20 +60,20 @@ static void mac802154_rxenabletimeout(FAR void *arg) FAR struct ieee802154_privmac_s *priv = (FAR struct ieee802154_privmac_s *)arg; - while (mac802154_lock(priv, true) != 0); + while (nxmutex_lock(&priv->lock) != 0); if (priv->curr_op != MAC802154_OP_RXENABLE) { - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); return; } mac802154_rxdisable(priv); priv->curr_op = MAC802154_OP_NONE; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); } /**************************************************************************** @@ -103,7 +103,7 @@ int mac802154_req_rxenable(MACHANDLE mac, if (priv->sfspec.sforder < 15) { return -EINVAL; - goto errout_with_sem; + goto errout_with_lock; } /* Non-beacon enabled network */ @@ -119,7 +119,7 @@ int mac802154_req_rxenable(MACHANDLE mac, * MAC in order to unlock it. */ - ret = mac802154_takesem(&priv->opsem, true); + ret = nxsem_wait_uninterruptible(&priv->opsem); if (ret < 0) { return ret; @@ -129,14 +129,14 @@ int mac802154_req_rxenable(MACHANDLE mac, /* Get exclusive access to the MAC */ - ret = mac802154_lock(priv, true); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { /* Should only fail if interrupted by a signal */ - wlwarn("WARNING: mac802154_takesem failed: %d\n", ret); + wlwarn("WARNING: nxmutex_lock failed: %d\n", ret); - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); return ret; } @@ -150,33 +150,33 @@ int mac802154_req_rxenable(MACHANDLE mac, } else { - ret = mac802154_lock(priv, true); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { /* Should only fail if interrupted by a signal */ - wlwarn("WARNING: mac802154_takesem failed: %d\n", ret); + wlwarn("WARNING: nxmutex_lock failed: %d\n", ret); return ret; } if (priv->curr_op != MAC802154_OP_RXENABLE) { ret = -EINVAL; - goto errout_with_sem; + goto errout_with_lock; } mac802154_timercancel(priv); mac802154_rxdisable(priv); priv->curr_op = MAC802154_OP_NONE; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); } } - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); return OK; -errout_with_sem: - mac802154_unlock(priv) +errout_with_lock: + nxmutex_unlock(&priv->lock); return ret; } diff --git a/wireless/ieee802154/mac802154_scan.c b/wireless/ieee802154/mac802154_scan.c index 3defd7d9de670..376dd069537e3 100644 --- a/wireless/ieee802154/mac802154_scan.c +++ b/wireless/ieee802154/mac802154_scan.c @@ -79,7 +79,7 @@ int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req) * This must be done before locking the MAC so that we don't hold the MAC */ - ret = mac802154_takesem(&priv->opsem, true); + ret = nxsem_wait_uninterruptible(&priv->opsem); if (ret < 0) { goto errout; @@ -89,10 +89,10 @@ int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req) /* Get exclusive access to the MAC */ - ret = mac802154_lock(priv, true); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); goto errout; } @@ -144,7 +144,7 @@ int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req) case IEEE802154_SCANTYPE_ACTIVE: { ret = -ENOTTY; - goto errout_with_sem; + goto errout_with_lock; } break; case IEEE802154_SCANTYPE_ED: @@ -163,24 +163,23 @@ int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req) case IEEE802154_SCANTYPE_ORPHAN: { ret = -ENOTTY; - goto errout_with_sem; + goto errout_with_lock; } break; default: { ret = -EINVAL; - goto errout_with_sem; + goto errout_with_lock; } break; } - mac802154_unlock(priv) - + nxmutex_unlock(&priv->lock); return OK; -errout_with_sem: - mac802154_unlock(priv) - mac802154_givesem(&priv->opsem); +errout_with_lock: + nxmutex_unlock(&priv->lock); + nxsem_post(&priv->opsem); errout: return ret; } @@ -255,7 +254,7 @@ void mac802154_scanfinish(FAR struct ieee802154_privmac_s *priv, scanconf->status = status; priv->curr_op = MAC802154_OP_NONE; - mac802154_givesem(&priv->opsem); + nxsem_post(&priv->opsem); mac802154_notify(priv, primitive); } @@ -326,7 +325,7 @@ static void mac802154_scantimeout(FAR void *arg) (FAR struct ieee802154_privmac_s *)arg; DEBUGASSERT(priv->curr_op == MAC802154_OP_SCAN); - mac802154_lock(priv, false); + nxmutex_lock(&priv->lock); /* If we got here it means we are done scanning that channel */ @@ -359,5 +358,5 @@ static void mac802154_scantimeout(FAR void *arg) mac802154_rxenable(priv); mac802154_timerstart(priv, priv->scansymdur, mac802154_scantimeout); - mac802154_unlock(priv); + nxmutex_unlock(&priv->lock); } diff --git a/wireless/ieee802154/mac802154_start.c b/wireless/ieee802154/mac802154_start.c index 87e2887718a72..5fa03755bbafb 100644 --- a/wireless/ieee802154/mac802154_start.c +++ b/wireless/ieee802154/mac802154_start.c @@ -58,7 +58,7 @@ int mac802154_req_start(MACHANDLE mac, /* Get exclusive access to the MAC */ - ret = mac802154_lock(priv, true); + ret = nxmutex_lock(&priv->lock); if (ret < 0) { return ret; @@ -185,11 +185,10 @@ int mac802154_req_start(MACHANDLE mac, } } - mac802154_unlock(priv) - + nxmutex_unlock(&priv->lock); return OK; errout: - mac802154_unlock(priv) + nxmutex_unlock(&priv->lock); return ret; } diff --git a/wireless/pktradio/pktradio_metadata.c b/wireless/pktradio/pktradio_metadata.c index d435249636567..72199c8630b29 100644 --- a/wireless/pktradio/pktradio_metadata.c +++ b/wireless/pktradio/pktradio_metadata.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include @@ -49,7 +49,7 @@ static FAR struct pktradio_metadata_s *g_free_metadata; /* Supports mutually exclusive access to the free list */ -static sem_t g_metadata_sem; +static mutex_t g_metadata_lock; /* Idempotence support */ @@ -103,9 +103,9 @@ void pktradio_metadata_initialize(void) g_free_metadata = metadata; } - /* Initialize the mutual exclusion semaphore */ + /* Initialize the mutual exclusion mutex */ - nxsem_init(&g_metadata_sem, 0, 1); + nxmutex_init(&g_metadata_lock); g_metadata_initialized = true; } } @@ -138,7 +138,7 @@ FAR struct pktradio_metadata_s *pktradio_metadata_allocate(void) /* Get exclusive access to the free list */ - nxsem_wait_uninterruptible(&g_metadata_sem); + nxmutex_lock(&g_metadata_lock); /* Try the free list first */ @@ -150,7 +150,7 @@ FAR struct pktradio_metadata_s *pktradio_metadata_allocate(void) /* We are finished with the free list */ - nxsem_post(&g_metadata_sem); + nxmutex_unlock(&g_metadata_lock); } else { @@ -159,7 +159,7 @@ FAR struct pktradio_metadata_s *pktradio_metadata_allocate(void) * access the free list. */ - nxsem_post(&g_metadata_sem); + nxmutex_unlock(&g_metadata_lock); metadata = (FAR struct pktradio_metadata_s *) kmm_malloc((sizeof (struct pktradio_metadata_s))); @@ -200,7 +200,7 @@ void pktradio_metadata_free(FAR struct pktradio_metadata_s *metadata) { /* Get exclusive access to the free list */ - nxsem_wait_uninterruptible(&g_metadata_sem); + nxmutex_lock(&g_metadata_lock); /* If this is a pre-allocated meta-data structure, then just put it back * in the free list. @@ -213,7 +213,7 @@ void pktradio_metadata_free(FAR struct pktradio_metadata_s *metadata) /* We are finished with the free list */ - nxsem_post(&g_metadata_sem); + nxmutex_unlock(&g_metadata_lock); } else { @@ -221,7 +221,7 @@ void pktradio_metadata_free(FAR struct pktradio_metadata_s *metadata) /* Otherwise, deallocate it. We won't access the free list */ - nxsem_post(&g_metadata_sem); + nxmutex_unlock(&g_metadata_lock); kmm_free(metadata); } }