From 962c8c14e1205e8863f9fde78900bf37be1e6dfd Mon Sep 17 00:00:00 2001 From: zhangyuan29 Date: Tue, 19 Nov 2024 10:13:33 +0800 Subject: [PATCH] arch_atomic: support nx atomic function Modify the kernel to use nx atomic interfaces, avoiding the use of sizeof or typeof to determine the type of atomic operations, thereby simplifying the kernel's atomic interface operations. Signed-off-by: zhangyuan29 --- arch/arm/src/cxd56xx/cxd56_sph.c | 2 +- arch/arm/src/cxd56xx/cxd56_uart0.c | 4 +- arch/sim/src/sim/sim_heap.c | 4 +- drivers/i3c/master.c | 6 +- drivers/input/aw86225.c | 14 +- drivers/net/netdev_upperhalf.c | 4 +- drivers/note/notesnap_driver.c | 10 +- drivers/reset/core.c | 10 +- drivers/rpmsg/rpmsg_port.h | 6 +- drivers/rpmsg/rpmsg_port_spi.c | 2 +- drivers/rpmsg/rpmsg_port_spi_slave.c | 2 +- drivers/serial/pty.c | 2 +- drivers/serial/uart_ram.c | 32 +- drivers/wireless/bluetooth/bt_bridge.c | 2 +- fs/inode/fs_inoderemove.c | 2 +- fs/inode/fs_inodereserve.c | 2 +- fs/mqueue/mq_open.c | 2 +- fs/mqueue/mq_unlink.c | 2 +- fs/shm/shmfs.c | 4 +- fs/vfs/fs_pseudofile.c | 2 +- include/nuttx/atomic.h | 413 +++++++++++++++++++++---- include/nuttx/fs/fs.h | 2 +- include/nuttx/lib/stdatomic.h | 278 ----------------- include/nuttx/net/netdev_lowerhalf.h | 2 +- include/nuttx/reset/reset.h | 6 +- include/nuttx/serial/uart_ram.h | 4 +- include/nuttx/spinlock.h | 43 ++- include/semaphore.h | 3 +- libs/libc/machine/arch_atomic.c | 72 ----- libs/libc/misc/lib_fdsan.c | 2 +- sched/semaphore/sem_destroy.c | 9 +- sched/semaphore/sem_holder.c | 4 +- sched/semaphore/sem_post.c | 10 +- sched/semaphore/sem_recover.c | 4 +- sched/semaphore/sem_reset.c | 11 +- sched/semaphore/sem_trywait.c | 18 +- sched/semaphore/sem_wait.c | 8 +- sched/semaphore/sem_waitirq.c | 4 +- sched/semaphore/semaphore.h | 7 - 39 files changed, 459 insertions(+), 555 deletions(-) delete mode 100644 include/nuttx/lib/stdatomic.h diff --git a/arch/arm/src/cxd56xx/cxd56_sph.c b/arch/arm/src/cxd56xx/cxd56_sph.c index 72e6b0cbb257c..a1bd1bdfde5fb 100644 --- a/arch/arm/src/cxd56xx/cxd56_sph.c +++ b/arch/arm/src/cxd56xx/cxd56_sph.c @@ -104,7 +104,7 @@ static int sph_open(struct file *filep) { /* Exclusive access */ - if (atomic_load(&filep->f_inode->i_crefs) > 2) + if (atomic_read(&filep->f_inode->i_crefs) > 2) { return ERROR; } diff --git a/arch/arm/src/cxd56xx/cxd56_uart0.c b/arch/arm/src/cxd56xx/cxd56_uart0.c index c0eeb97125331..d34d620b3941f 100644 --- a/arch/arm/src/cxd56xx/cxd56_uart0.c +++ b/arch/arm/src/cxd56xx/cxd56_uart0.c @@ -115,7 +115,7 @@ static int uart0_open(struct file *filep) int stop; int ret; - if (atomic_load(&inode->i_crefs) > 2) + if (atomic_read(&inode->i_crefs) > 2) { return OK; } @@ -172,7 +172,7 @@ static int uart0_close(struct file *filep) { struct inode *inode = filep->f_inode; - if (atomic_load(&inode->i_crefs) == 2) + if (atomic_read(&inode->i_crefs) == 2) { fw_pd_uartdisable(0); fw_pd_uartuninit(0); diff --git a/arch/sim/src/sim/sim_heap.c b/arch/sim/src/sim/sim_heap.c index 7b95441f7a115..c262f56dd2dfb 100644 --- a/arch/sim/src/sim/sim_heap.c +++ b/arch/sim/src/sim/sim_heap.c @@ -404,7 +404,7 @@ void *mm_realloc(struct mm_heap_s *heap, void *oldmem, break; } } - while (atomic_compare_exchange_weak(&heap->usmblks, &usmblks, uordblks)); + while (atomic_try_cmpxchg(&heap->usmblks, &usmblks, uordblks)); #if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0 if (mem == NULL && free_delaylist(heap, true)) @@ -498,7 +498,7 @@ void *mm_memalign(struct mm_heap_s *heap, size_t alignment, size_t size) break; } } - while (atomic_compare_exchange_weak(&heap->usmblks, &usmblks, uordblks)); + while (atomic_try_cmpxchg(&heap->usmblks, &usmblks, uordblks)); #if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0 if (mem == NULL && free_delaylist(heap, true)) diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c index f2d3c452fa354..40ab0a763b0a2 100644 --- a/drivers/i3c/master.c +++ b/drivers/i3c/master.c @@ -1181,7 +1181,7 @@ static void i3c_master_handle_ibi(FAR void *arg) master->ops->recycle_ibi_slot(dev, slot); atomic_fetch_sub(&dev->ibi->pending_ibis, 1); - if (!atomic_load(&dev->ibi->pending_ibis)) + if (!atomic_read(&dev->ibi->pending_ibis)) { sem_post(&dev->ibi->all_ibis_handled); } @@ -2034,7 +2034,7 @@ int i3c_dev_disable_ibi_locked(FAR struct i3c_dev_desc *dev) return ret; } - if (atomic_load(&dev->ibi->pending_ibis)) + if (atomic_read(&dev->ibi->pending_ibis)) { sem_wait(&dev->ibi->all_ibis_handled); } @@ -2087,7 +2087,7 @@ int i3c_dev_request_ibi_locked(FAR struct i3c_dev_desc *dev, return -ENOMEM; } - atomic_init(&ibi->pending_ibis, 0); + atomic_set(&ibi->pending_ibis, 0); sem_init(&ibi->all_ibis_handled, 0, 1); ibi->handler = req->handler; ibi->max_payload_len = req->max_payload_len; diff --git a/drivers/input/aw86225.c b/drivers/input/aw86225.c index 24fb868fb809e..c93d2f48a7a8c 100644 --- a/drivers/input/aw86225.c +++ b/drivers/input/aw86225.c @@ -956,7 +956,7 @@ static int aw86225_haptic_rtp_init(FAR struct aw86225 *aw86225) nxmutex_lock(&aw86225->rtp_lock); while ((!aw86225_haptic_rtp_get_fifo_afs(aw86225)) && (aw86225->play_mode == AW86225_HAPTIC_RTP_MODE) - && !atomic_load(&aw86225->exit_in_rtp_loop)) + && !atomic_read(&aw86225->exit_in_rtp_loop)) { if (!aw86225->rtp_container) { @@ -1014,7 +1014,7 @@ static int aw86225_haptic_rtp_init(FAR struct aw86225 *aw86225) nxmutex_unlock(&aw86225->rtp_lock); if (aw86225->play_mode == AW86225_HAPTIC_RTP_MODE - && !atomic_load(&aw86225->exit_in_rtp_loop)) + && !atomic_read(&aw86225->exit_in_rtp_loop)) { aw86225_haptic_set_rtp_aei(aw86225, true); } @@ -1121,8 +1121,8 @@ static void aw86225_rtp_work_routine(FAR void *arg) /* wait for irq to exit */ - atomic_store(&aw86225->exit_in_rtp_loop, 1); - while (atomic_load(&aw86225->is_in_rtp_loop)) + atomic_set(&aw86225->exit_in_rtp_loop, 1); + while (atomic_read(&aw86225->is_in_rtp_loop)) { iinfo("%s: goint to waiting irq exit\n", __func__); @@ -1130,7 +1130,7 @@ static void aw86225_rtp_work_routine(FAR void *arg) if (ret == -ERESTART) { - atomic_store(&aw86225->exit_in_rtp_loop, 0); + atomic_set(&aw86225->exit_in_rtp_loop, 0); nxsem_post(&aw86225->stop_wait_q); nxmutex_unlock(&aw86225->lock); ierr("%s: wake up by signal return erro\n", __func__); @@ -1138,7 +1138,7 @@ static void aw86225_rtp_work_routine(FAR void *arg) } } - atomic_store(&aw86225->exit_in_rtp_loop, 0); + atomic_set(&aw86225->exit_in_rtp_loop, 0); nxsem_post(&aw86225->stop_wait_q); aw86225_haptic_stop(aw86225); @@ -2155,7 +2155,7 @@ static int aw86225_haptics_upload_effect(FAR struct ff_lowerhalf_s *lower, aw86225->effect_type = effect->type; nxmutex_lock(&aw86225->lock); - while (atomic_load(&aw86225->exit_in_rtp_loop)) + while (atomic_read(&aw86225->exit_in_rtp_loop)) { iinfo("%s: goint to waiting rtp exit\n", __func__); nxmutex_unlock(&aw86225->lock); diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c index cf5f65223ef87..4a3132efeba12 100644 --- a/drivers/net/netdev_upperhalf.c +++ b/drivers/net/netdev_upperhalf.c @@ -1369,7 +1369,7 @@ void netdev_lower_txdone(FAR struct netdev_lowerhalf_s *dev) * Name: netdev_lower_quota_load * * Description: - * Fetch the quota, works like atomic_load. + * Fetch the quota, works like atomic_read. * * Input Parameters: * dev - The lower half device driver structure @@ -1380,7 +1380,7 @@ void netdev_lower_txdone(FAR struct netdev_lowerhalf_s *dev) int netdev_lower_quota_load(FAR struct netdev_lowerhalf_s *dev, enum netpkt_type_e type) { - return atomic_load(&dev->quota[type]); + return atomic_read(&dev->quota[type]); } /**************************************************************************** diff --git a/drivers/note/notesnap_driver.c b/drivers/note/notesnap_driver.c index 636fc37f69e4c..d1d345e1b823e 100644 --- a/drivers/note/notesnap_driver.c +++ b/drivers/note/notesnap_driver.c @@ -50,8 +50,8 @@ struct notesnap_s { struct note_driver_s driver; struct notifier_block nb; - atomic_int index; - atomic_bool dumping; + atomic_t index; + atomic_t dumping; struct notesnap_chunk_s buffer[CONFIG_DRIVERS_NOTESNAP_NBUFFERS]; }; @@ -212,7 +212,7 @@ static inline void notesnap_common(FAR struct note_driver_s *drv, FAR struct notesnap_chunk_s *note; size_t index; - if (atomic_load(&snap->dumping)) + if (atomic_read(&snap->dumping)) { return; } @@ -388,7 +388,7 @@ void notesnap_dump_with_stream(FAR struct lib_outstream_s *stream) /* Stop recording while dumping */ - atomic_store(&g_notesnap.dumping, true); + atomic_set(&g_notesnap.dumping, true); for (i = 0; i < CONFIG_DRIVERS_NOTESNAP_NBUFFERS; i++) { @@ -411,7 +411,7 @@ void notesnap_dump_with_stream(FAR struct lib_outstream_s *stream) note->pid, g_notesnap_type[note->type], note->args); } - atomic_store(&g_notesnap.dumping, false); + atomic_set(&g_notesnap.dumping, false); } /**************************************************************************** diff --git a/drivers/reset/core.c b/drivers/reset/core.c index 23193f1cde3d5..aa852c2e32dd6 100644 --- a/drivers/reset/core.c +++ b/drivers/reset/core.c @@ -310,7 +310,7 @@ reset_control_get_internal(FAR struct reset_controller_dev *rcdev, rstc->rcdev = rcdev; list_add_after(&rcdev->reset_control_head, &rstc->list); rstc->id = index; - atomic_init(&rstc->refcnt, 1); + atomic_set(&rstc->refcnt, 1); rstc->acquired = acquired; rstc->shared = shared; @@ -528,7 +528,7 @@ int reset_control_reset(FAR struct reset_control *rstc) if (rstc->shared) { - if (atomic_load(&rstc->deassert_count) != 0) + if (atomic_read(&rstc->deassert_count) != 0) { return -EINVAL; } @@ -598,12 +598,12 @@ int reset_control_assert(FAR struct reset_control *rstc) if (rstc->shared) { - if (atomic_load(&rstc->triggered_count) != 0) + if (atomic_read(&rstc->triggered_count) != 0) { return -EINVAL; } - if (atomic_load(&rstc->deassert_count) == 0) + if (atomic_read(&rstc->deassert_count) == 0) { rsterr("deassert_count = 0, invalid value\n"); return -EINVAL; @@ -682,7 +682,7 @@ int reset_control_deassert(FAR struct reset_control *rstc) if (rstc->shared) { - if (atomic_load(&rstc->triggered_count) != 0) + if (atomic_read(&rstc->triggered_count) != 0) { rsterr("triggered_count != 0, invalid value\n"); return -EINVAL; diff --git a/drivers/rpmsg/rpmsg_port.h b/drivers/rpmsg/rpmsg_port.h index 9e55058694e27..e46edc39c11ea 100644 --- a/drivers/rpmsg/rpmsg_port.h +++ b/drivers/rpmsg/rpmsg_port.h @@ -29,7 +29,7 @@ #include -#include +#include #include #include @@ -237,7 +237,7 @@ void rpmsg_port_queue_add_buffer(FAR struct rpmsg_port_queue_s *queue, static inline_function uint16_t rpmsg_port_queue_navail(FAR struct rpmsg_port_queue_s *queue) { - return atomic_load(&queue->free.num); + return atomic_read(&queue->free.num); } /**************************************************************************** @@ -257,7 +257,7 @@ uint16_t rpmsg_port_queue_navail(FAR struct rpmsg_port_queue_s *queue) static inline_function uint16_t rpmsg_port_queue_nused(FAR struct rpmsg_port_queue_s *queue) { - return atomic_load(&queue->ready.num); + return atomic_read(&queue->ready.num); } /**************************************************************************** diff --git a/drivers/rpmsg/rpmsg_port_spi.c b/drivers/rpmsg/rpmsg_port_spi.c index 9552339f77532..b2e8f7c78f2ff 100644 --- a/drivers/rpmsg/rpmsg_port_spi.c +++ b/drivers/rpmsg/rpmsg_port_spi.c @@ -300,7 +300,7 @@ static void rpmsg_port_spi_complete_handler(FAR void *arg) } out: - if (atomic_exchange(&rpspi->transferring, 0) > 1) + if (atomic_xchg(&rpspi->transferring, 0) > 1) { rpmsg_port_spi_exchange(rpspi); } diff --git a/drivers/rpmsg/rpmsg_port_spi_slave.c b/drivers/rpmsg/rpmsg_port_spi_slave.c index 609252c6b3bb2..1c3bbb9043a56 100644 --- a/drivers/rpmsg/rpmsg_port_spi_slave.c +++ b/drivers/rpmsg/rpmsg_port_spi_slave.c @@ -369,7 +369,7 @@ static void rpmsg_port_spi_slave_notify(FAR struct spi_slave_dev_s *dev, } out: - if (atomic_exchange(&rpspi->transferring, 0) > 1 || + if (atomic_xchg(&rpspi->transferring, 0) > 1 || (rpspi->txavail > 0 && rpmsg_port_queue_nused(&rpspi->port.txq) > 0)) { rpmsg_port_spi_exchange(rpspi); diff --git a/drivers/serial/pty.c b/drivers/serial/pty.c index 11e7855424f9a..4b04b2fd7b6a5 100644 --- a/drivers/serial/pty.c +++ b/drivers/serial/pty.c @@ -340,7 +340,7 @@ static int pty_close(FAR struct file *filep) /* Check if the decremented inode reference count would go to zero */ - if (atomic_load(&inode->i_crefs) == 1) + if (atomic_read(&inode->i_crefs) == 1) { /* Did the (single) master just close its reference? */ diff --git a/drivers/serial/uart_ram.c b/drivers/serial/uart_ram.c index a0042bfb0eca5..c4124a9e1e6bd 100644 --- a/drivers/serial/uart_ram.c +++ b/drivers/serial/uart_ram.c @@ -229,8 +229,8 @@ static struct uart_ram_s g_uart_ram2 = static size_t uart_rambuf_txready(FAR struct uart_rambuf_s *buf) { - atomic_uint wroff = atomic_load(&buf->wroff); - atomic_uint rdoff = atomic_load(&buf->rdoff); + int wroff = atomic_read(&buf->wroff); + int rdoff = atomic_read(&buf->rdoff); return rdoff > wroff ? rdoff - wroff - 1 : sizeof(buf->buffer) - wroff + rdoff - 1; } @@ -241,8 +241,8 @@ static size_t uart_rambuf_txready(FAR struct uart_rambuf_s *buf) static size_t uart_rambuf_rxavailable(FAR struct uart_rambuf_s *buf) { - atomic_uint wroff = atomic_load(&buf->wroff); - atomic_uint rdoff = atomic_load(&buf->rdoff); + int wroff = atomic_read(&buf->wroff); + int rdoff = atomic_read(&buf->rdoff); return wroff >= rdoff ? wroff - rdoff : sizeof(buf->buffer) - rdoff + wroff; @@ -303,19 +303,19 @@ static int uart_ram_ioctl(FAR struct file *filep, int cmd, unsigned long arg) static int uart_ram_receive(FAR struct uart_dev_s *dev, FAR uint32_t *status) { FAR struct uart_ram_s *priv = dev->priv; - atomic_uint rdoff; + int rdoff; int ch; while (!uart_rambuf_rxavailable(priv->rx)); - rdoff = atomic_load(&priv->rx->rdoff); + rdoff = atomic_read(&priv->rx->rdoff); ch = priv->rx->buffer[rdoff]; if (++rdoff >= sizeof(priv->rx->buffer)) { rdoff = 0; } - atomic_store(&priv->rx->rdoff, rdoff); + atomic_set(&priv->rx->rdoff, rdoff); *status = 0; return ch; @@ -347,7 +347,7 @@ static void uart_ram_dmasend(FAR struct uart_dev_s *dev) { FAR struct uart_ram_s *priv = dev->priv; - atomic_store(&priv->tx->wroff, dev->xmit.head); + atomic_set(&priv->tx->wroff, dev->xmit.head); } /**************************************************************************** @@ -371,7 +371,7 @@ static void uart_ram_dmarxfree(FAR struct uart_dev_s *dev) /* When the dma RX buffer is free, update the read data position */ - atomic_store(&priv->rx->rdoff, dev->recv.tail); + atomic_set(&priv->rx->rdoff, dev->recv.tail); } /**************************************************************************** @@ -393,18 +393,18 @@ static void uart_ram_dmatxavail(FAR struct uart_dev_s *dev) static void uart_ram_send(FAR struct uart_dev_s *dev, int ch) { FAR struct uart_ram_s *priv = dev->priv; - atomic_uint wroff; + int wroff; while (!uart_rambuf_txready(priv->tx)); - wroff = atomic_load(&priv->tx->wroff); + wroff = atomic_read(&priv->tx->wroff); priv->tx->buffer[wroff] = ch; if (++wroff >= sizeof(priv->tx->buffer)) { wroff = 0; } - atomic_store(&priv->tx->wroff, wroff); + atomic_set(&priv->tx->wroff, wroff); } /**************************************************************************** @@ -488,10 +488,10 @@ int uart_ram_register(FAR const char *devname, return -ENOMEM; } - atomic_store(&rambuf[0].wroff, 0); - atomic_store(&rambuf[0].rdoff, 0); - atomic_store(&rambuf[1].wroff, 0); - atomic_store(&rambuf[1].rdoff, 0); + atomic_set(&rambuf[0].wroff, 0); + atomic_set(&rambuf[0].rdoff, 0); + atomic_set(&rambuf[1].wroff, 0); + atomic_set(&rambuf[1].rdoff, 0); if (slave) { diff --git a/drivers/wireless/bluetooth/bt_bridge.c b/drivers/wireless/bluetooth/bt_bridge.c index d53f62cfaa086..6e440396a5704 100644 --- a/drivers/wireless/bluetooth/bt_bridge.c +++ b/drivers/wireless/bluetooth/bt_bridge.c @@ -75,7 +75,7 @@ struct bt_bridge_s #ifdef CONFIG_BLUETOOTH_BRIDGE_BTSNOOP FAR struct snoop_s *snoop; #endif /* CONFIG_BLUETOOTH_BRIDGE_BTSNOOP */ - atomic_uint refs; + atomic_t refs; bool dispatched[BT_FILTER_CMD_COUNT]; }; diff --git a/fs/inode/fs_inoderemove.c b/fs/inode/fs_inoderemove.c index 792eabe0a5d17..3b8660e71ffa8 100644 --- a/fs/inode/fs_inoderemove.c +++ b/fs/inode/fs_inoderemove.c @@ -136,7 +136,7 @@ int inode_remove(FAR const char *path) * to it */ - if (atomic_load(&inode->i_crefs)) + if (atomic_read(&inode->i_crefs)) { return -EBUSY; } diff --git a/fs/inode/fs_inodereserve.c b/fs/inode/fs_inodereserve.c index 35fae49dd7fc6..8cd41a5b1869e 100644 --- a/fs/inode/fs_inodereserve.c +++ b/fs/inode/fs_inodereserve.c @@ -88,7 +88,7 @@ static FAR struct inode *inode_alloc(FAR const char *name, mode_t mode) if (inode) { inode->i_ino = g_ino++; - atomic_init(&inode->i_crefs, 1); + atomic_set(&inode->i_crefs, 1); #ifdef CONFIG_PSEUDOFS_ATTRIBUTES inode->i_mode = mode; clock_gettime(CLOCK_REALTIME, &inode->i_atime); diff --git a/fs/mqueue/mq_open.c b/fs/mqueue/mq_open.c index d2e5cc79bc888..402ed9a5d144d 100644 --- a/fs/mqueue/mq_open.c +++ b/fs/mqueue/mq_open.c @@ -76,7 +76,7 @@ static int nxmq_file_close(FAR struct file *filep) { FAR struct inode *inode = filep->f_inode; - if (atomic_load(&inode->i_crefs) <= 0) + if (atomic_read(&inode->i_crefs) <= 0) { FAR struct mqueue_inode_s *msgq = inode->i_private; diff --git a/fs/mqueue/mq_unlink.c b/fs/mqueue/mq_unlink.c index fb4d9d1550a40..cc32de0b56599 100644 --- a/fs/mqueue/mq_unlink.c +++ b/fs/mqueue/mq_unlink.c @@ -58,7 +58,7 @@ static void mq_inode_release(FAR struct inode *inode) { - if (atomic_load(&inode->i_crefs) <= 1) + if (atomic_read(&inode->i_crefs) <= 1) { FAR struct mqueue_inode_s *msgq = inode->i_private; diff --git a/fs/shm/shmfs.c b/fs/shm/shmfs.c index 752e2d5dacdf9..e1cc39a539a5e 100644 --- a/fs/shm/shmfs.c +++ b/fs/shm/shmfs.c @@ -186,7 +186,7 @@ static int shmfs_release(FAR struct inode *inode) * The inode is released after this call, hence checking if i_crefs <= 1. */ - if (inode->i_parent == NULL && atomic_load(&inode->i_crefs) <= 1) + if (inode->i_parent == NULL && atomic_read(&inode->i_crefs) <= 1) { shmfs_free_object(inode->i_private); inode->i_private = NULL; @@ -256,7 +256,7 @@ static int shmfs_truncate(FAR struct file *filep, off_t length) #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS static int shmfs_unlink(FAR struct inode *inode) { - if (atomic_load(&inode->i_crefs) <= 1) + if (atomic_read(&inode->i_crefs) <= 1) { shmfs_free_object(inode->i_private); inode->i_private = NULL; diff --git a/fs/vfs/fs_pseudofile.c b/fs/vfs/fs_pseudofile.c index 314c79eed8b19..df896a507859c 100644 --- a/fs/vfs/fs_pseudofile.c +++ b/fs/vfs/fs_pseudofile.c @@ -354,7 +354,7 @@ static int pseudofile_munmap(FAR struct task_group_s *group, */ if (inode->i_parent == NULL && - atomic_load(&inode->i_crefs) <= 1) + atomic_read(&inode->i_crefs) <= 1) { /* Delete the inode metadata */ diff --git a/include/nuttx/atomic.h b/include/nuttx/atomic.h index 1c430f963f9c1..29b72918c69cd 100644 --- a/include/nuttx/atomic.h +++ b/include/nuttx/atomic.h @@ -27,74 +27,11 @@ * Included Files ****************************************************************************/ -#ifdef __has_include -# if defined(__cplusplus) && __has_include() -extern "C++" -{ -# include - -# define ATOMIC_VAR_INIT(value) (value) - - using std::memory_order; - using std::atomic_bool; - using std::atomic_char; - using std::atomic_schar; - using std::atomic_uchar; - using std::atomic_short; - using std::atomic_ushort; - using std::atomic_int; - using std::atomic_uint; - using std::atomic_long; - using std::atomic_ulong; - using std::atomic_llong; - using std::atomic_ullong; - - using std::atomic_load; - using std::atomic_load_explicit; - using std::atomic_store; - using std::atomic_store_explicit; - using std::atomic_exchange; - using std::atomic_exchange_explicit; - using std::atomic_compare_exchange_strong; - using std::atomic_compare_exchange_strong_explicit; - using std::atomic_compare_exchange_weak; - using std::atomic_compare_exchange_weak_explicit; - using std::atomic_flag_test_and_set; - using std::atomic_flag_test_and_set_explicit; - using std::atomic_flag_clear; - using std::atomic_flag_clear_explicit; - using std::atomic_fetch_add; - using std::atomic_fetch_add_explicit; - using std::atomic_fetch_sub; - using std::atomic_fetch_sub_explicit; - using std::atomic_fetch_and; - using std::atomic_fetch_and_explicit; - using std::atomic_fetch_or; - using std::atomic_fetch_or_explicit; - using std::atomic_fetch_xor; - using std::atomic_fetch_xor_explicit; -} -# elif __has_include() && \ - ((defined(__cplusplus) && __cplusplus >= 201103L) || \ - (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)) && \ - !defined(__STDC_NO_ATOMICS__) -# if !(__clang__) && defined(__cplusplus) -# define _Atomic -# endif -# include -# include -# ifndef ATOMIC_VAR_INIT -# define ATOMIC_VAR_INIT(value) (value) -# endif -# else -# include -# endif -#else -# include -#endif +#include +#include /**************************************************************************** - * Public Function Prototypes + * Pre-processor Definitions ****************************************************************************/ #undef EXTERN @@ -106,6 +43,350 @@ extern "C" #define EXTERN extern #endif +#ifndef __ATOMIC_RELAXED +# define __ATOMIC_RELAXED 0 +#endif + +#ifndef __ATOMIC_CONSUME +# define __ATOMIC_CONSUME 1 +#endif + +#ifndef __ATOMIC_ACQUIRE +# define __ATOMIC_ACQUIRE 2 +#endif + +#ifndef __ATOMIC_RELEASE +# define __ATOMIC_RELEASE 3 +#endif + +#ifndef __ATOMIC_ACQ_REL +# define __ATOMIC_ACQ_REL 4 +#endif + +#ifndef __ATOMIC_SEQ_CST +# define __ATOMIC_SEQ_CST 5 +#endif + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +typedef volatile int32_t atomic_t; +typedef volatile int64_t atomic64_t; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#if !((defined(__cplusplus) && __cplusplus >= 201103L) && \ + !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)) || \ + defined(__STDC_NO_ATOMICS__) +void __atomic_store_4(FAR volatile void *ptr, uint32_t value, int memorder); +void __atomic_store_8(FAR volatile void *ptr, uint64_t value, int memorder); +uint32_t __atomic_load_4(FAR const volatile void *ptr, int memorder); +uint64_t __atomic_load_8(FAR const volatile void *ptr, int memorder); +uint32_t __atomic_exchange_4(FAR volatile void *ptr, uint32_t value, + int memorder); +uint64_t __atomic_exchange_8(FAR volatile void *ptr, uint64_t value, + int memorder); +bool __atomic_compare_exchange_4(FAR volatile void *mem, FAR void *expect, + uint32_t desired, bool weak, int success, + int failure); +bool __atomic_compare_exchange_8(FAR volatile void *mem, FAR void *expect, + uint64_t desired, bool weak, int success, + int failure); +uint32_t __atomic_fetch_add_4(FAR volatile void *ptr, uint32_t value, + int memorder); +uint64_t __atomic_fetch_add_8(FAR volatile void *ptr, uint64_t value, + int memorder); +uint32_t __atomic_fetch_sub_4(FAR volatile void *ptr, uint32_t value, + int memorder); +uint64_t __atomic_fetch_sub_8(FAR volatile void *ptr, uint64_t value, + int memorder); +uint32_t __atomic_fetch_and_4(FAR volatile void *ptr, uint32_t value, + int memorder); +uint64_t __atomic_fetch_and_8(FAR volatile void *ptr, uint64_t value, + int memorder); +uint32_t __atomic_fetch_or_4(FAR volatile void *ptr, uint32_t value, + int memorder); +uint64_t __atomic_fetch_or_8(FAR volatile void *ptr, uint64_t value, + int memorder); +uint32_t __atomic_fetch_xor_4(FAR volatile void *ptr, uint32_t value, + int memorder); +uint64_t __atomic_fetch_xor_8(FAR volatile void *ptr, uint64_t value, + int memorder); +#endif + +#define ATOMIC_SET(func, n, type) \ + static inline_function void func##_set(FAR volatile void *obj, \ + type val) \ + { \ + __atomic_store_##n(obj, val, __ATOMIC_RELAXED); \ + } \ + \ + static inline_function void func##_set_release(FAR volatile void *obj, \ + type val) \ + { \ + __atomic_store_##n(obj, val, __ATOMIC_RELEASE); \ + } + +#define ATOMIC_READ(func, n, type) \ + static inline_function type func##_read(FAR const volatile void *obj) \ + { \ + return (type)__atomic_load_##n(obj, __ATOMIC_RELAXED); \ + } \ + \ + static inline_function \ + type func##_read_acquire(FAR const volatile void *obj) \ + { \ + return (type)__atomic_load_##n(obj, __ATOMIC_ACQUIRE); \ + } + +#define ATOMIC_XCHG(func, n, type) \ + static inline_function type func##_xchg(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_exchange_##n(obj, val, __ATOMIC_ACQ_REL); \ + } \ + \ + static inline_function type func##_xchg_acquire(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_exchange_##n(obj, val, __ATOMIC_ACQUIRE); \ + } \ + \ + static inline_function type func##_xchg_release(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_exchange_##n(obj, val, __ATOMIC_RELEASE); \ + } \ + \ + static inline_function type func##_xchg_relaxed(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_exchange_##n(obj, val, __ATOMIC_RELAXED); \ + } + +#define ATOMIC_CMPXCHG(func, n, type) \ + static inline_function bool func##_cmpxchg(FAR volatile void *obj, \ + FAR void *expected, \ + type desired) \ + { \ + return __atomic_compare_exchange_##n(obj, expected, desired, false, \ + __ATOMIC_ACQ_REL, \ + __ATOMIC_RELAXED); \ + } \ + \ + static inline_function \ + bool func##_cmpxchg_acquire(FAR volatile void *obj, FAR void *expected, \ + type desired) \ + { \ + return __atomic_compare_exchange_##n(obj, expected, desired, false, \ + __ATOMIC_ACQUIRE, \ + __ATOMIC_RELAXED); \ + } \ + \ + static inline_function \ + bool func##_cmpxchg_release(FAR volatile void *obj, FAR void *expected, \ + type desired) \ + { \ + return __atomic_compare_exchange_##n(obj, expected, desired, false, \ + __ATOMIC_RELEASE, \ + __ATOMIC_RELAXED); \ + } \ + \ + static inline_function \ + bool func##_cmpxchg_relaxed(FAR volatile void *obj, FAR void *expected, \ + type desired) \ + { \ + return __atomic_compare_exchange_##n(obj, expected, desired, false, \ + __ATOMIC_RELAXED, \ + __ATOMIC_RELAXED); \ + } \ + \ + static inline_function bool func##_try_cmpxchg(FAR volatile void *obj, \ + FAR void *expected, \ + type desired) \ + { \ + return __atomic_compare_exchange_##n(obj, expected, desired, true, \ + __ATOMIC_ACQ_REL, \ + __ATOMIC_RELAXED); \ + } \ + \ + static inline_function \ + bool func##_try_cmpxchg_acquire(FAR volatile void *obj, \ + FAR void *expected, \ + type desired) \ + { \ + return __atomic_compare_exchange_##n(obj, expected, desired, true, \ + __ATOMIC_ACQUIRE, \ + __ATOMIC_RELAXED); \ + } \ + \ + static inline_function \ + bool func##_try_cmpxchg_release(FAR volatile void *obj, \ + FAR void *expected, \ + type desired) \ + { \ + return __atomic_compare_exchange_##n(obj, expected, desired, true, \ + __ATOMIC_RELEASE, \ + __ATOMIC_RELAXED); \ + } \ + \ + static inline_function \ + bool func##_try_cmpxchg_relaxed(FAR volatile void *obj, \ + FAR void *expected, \ + type desired) \ + { \ + return __atomic_compare_exchange_##n(obj, expected, desired, true, \ + __ATOMIC_RELAXED, \ + __ATOMIC_RELAXED); \ + } + \ +#define ATOMIC_ADD(func, n, type) \ + static inline_function type func##_fetch_add(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_fetch_add_##n(obj, val, __ATOMIC_ACQ_REL); \ + } \ + \ + static inline_function \ + type func##_fetch_add_acquire(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_add_##n(obj, val, __ATOMIC_ACQUIRE); \ + } \ + \ + static inline_function \ + type func##_fetch_add_release(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_add_##n(obj, val, __ATOMIC_RELEASE); \ + } \ + \ + static inline_function \ + type func##_fetch_add_relaxed(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_add_##n(obj, val, __ATOMIC_RELAXED); \ + } + \ +#define ATOMIC_SUB(func, n, type) \ + static inline_function type func##_fetch_sub(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_fetch_sub_##n(obj, val, __ATOMIC_ACQ_REL); \ + } \ + \ + static inline_function \ + type func##_fetch_sub_acquire(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_sub_##n(obj, val, __ATOMIC_ACQUIRE); \ + } \ + \ + static inline_function \ + type func##_fetch_sub_release(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_sub_##n(obj, val, __ATOMIC_RELEASE); \ + } \ + \ + static inline_function \ + type func##_fetch_sub_relaxed(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_sub_##n(obj, val, __ATOMIC_RELAXED); \ + } + \ +#define ATOMIC_AND(func, n, type) \ + static inline_function type func##_fetch_and(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_fetch_and_##n(obj, val, __ATOMIC_ACQ_REL); \ + } \ + \ + static inline_function \ + type func##_fetch_and_acquire(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_and_##n(obj, val, __ATOMIC_ACQUIRE); \ + } \ + \ + static inline_function \ + type func##_fetch_and_release(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_and_##n(obj, val, __ATOMIC_RELEASE); \ + } \ + \ + static inline_function \ + type func##_fetch_and_relaxed(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_and_##n(obj, val, __ATOMIC_RELAXED); \ + } + \ +#define ATOMIC_OR(func, n, type) \ + static inline_function type func##_fetch_or(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_fetch_or_##n(obj, val, __ATOMIC_ACQ_REL); \ + } \ + \ + static inline_function \ + type func##_fetch_or_acquire(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_or_##n(obj, val, __ATOMIC_ACQUIRE); \ + } \ + \ + static inline_function \ + type func##_fetch_or_release(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_or_##n(obj, val, __ATOMIC_RELEASE); \ + } \ + \ + static inline_function \ + type func##_fetch_or_relaxed(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_or_##n(obj, val, __ATOMIC_RELAXED); \ + } + \ +#define ATOMIC_XOR(func, n, type) \ + static inline_function type func##_fetch_xor(FAR volatile void *obj, \ + type val) \ + { \ + return (type)__atomic_fetch_xor_##n(obj, val, __ATOMIC_ACQ_REL); \ + } \ + \ + static inline_function \ + type func##_fetch_xor_acquire(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_xor_##n(obj, val, __ATOMIC_ACQUIRE); \ + } \ + \ + static inline_function \ + type func##_fetch_xor_release(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_xor_##n(obj, val, __ATOMIC_RELEASE); \ + } \ + \ + static inline_function \ + type func##_fetch_xor_relaxed(FAR volatile void *obj, type val) \ + { \ + return (type)__atomic_fetch_xor_##n(obj, val, __ATOMIC_RELAXED); \ + } + +ATOMIC_SET(atomic, 4, int32_t) +ATOMIC_SET(atomic64, 8, int64_t) +ATOMIC_READ(atomic, 4, int32_t) +ATOMIC_READ(atomic64, 8, int64_t) +ATOMIC_XCHG(atomic, 4, int32_t) +ATOMIC_XCHG(atomic64, 8, int64_t) +ATOMIC_CMPXCHG(atomic, 4, int32_t) +ATOMIC_CMPXCHG(atomic64, 8, int64_t) +ATOMIC_ADD(atomic, 4, int32_t) +ATOMIC_ADD(atomic64, 8, int64_t) +ATOMIC_SUB(atomic, 4, int32_t) +ATOMIC_SUB(atomic64, 8, int64_t) +ATOMIC_AND(atomic, 4, int32_t) +ATOMIC_AND(atomic64, 8, int64_t) +ATOMIC_OR(atomic, 4, int32_t) +ATOMIC_OR(atomic64, 8, int64_t) +ATOMIC_XOR(atomic, 4, int32_t) +ATOMIC_XOR(atomic64, 8, int64_t) + #undef EXTERN #if defined(__cplusplus) } diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index d9aa023b8383b..fdd4aa16890f0 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -403,7 +403,7 @@ struct inode FAR struct inode *i_parent; /* Link to parent level inode */ FAR struct inode *i_peer; /* Link to same level inode */ FAR struct inode *i_child; /* Link to lower level inode */ - atomic_short i_crefs; /* References to inode */ + atomic_t i_crefs; /* References to inode */ uint16_t i_flags; /* Flags for inode */ union inode_ops_u u; /* Inode operations */ ino_t i_ino; /* Inode serial number */ diff --git a/include/nuttx/lib/stdatomic.h b/include/nuttx/lib/stdatomic.h deleted file mode 100644 index 48972b61ae7fd..0000000000000 --- a/include/nuttx/lib/stdatomic.h +++ /dev/null @@ -1,278 +0,0 @@ -/**************************************************************************** - * include/nuttx/lib/stdatomic.h - * - * SPDX-License-Identifier: Apache-2.0 - * - * 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. - * - ****************************************************************************/ - -#ifndef __INCLUDE_NUTTX_LIB_STDATOMIC_H -#define __INCLUDE_NUTTX_LIB_STDATOMIC_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include -#include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#ifndef __ATOMIC_RELAXED -# define __ATOMIC_RELAXED 0 -#endif - -#ifndef __ATOMIC_CONSUM -# define __ATOMIC_CONSUME 1 -#endif - -#ifndef __ATOMIC_ACQUIR -# define __ATOMIC_ACQUIRE 2 -#endif - -#ifndef __ATOMIC_RELEAS -# define __ATOMIC_RELEASE 3 -#endif - -#ifndef __ATOMIC_ACQ_REL -# define __ATOMIC_ACQ_REL 4 -#endif - -#ifndef __ATOMIC_SEQ_CS -# define __ATOMIC_SEQ_CST 5 -#endif - -#define ATOMIC_FLAG_INIT 0 -#define ATOMIC_VAR_INIT(value) (value) - -#define atomic_store_n(obj, val, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_store_1(obj, val, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_store_2(obj, val, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_store_4(obj, val, type) : \ - nx_atomic_store_8(obj, val, type)) - -#define atomic_store(obj, val) atomic_store_n(obj, val, __ATOMIC_RELAXED) -#define atomic_store_explicit(obj, val, type) atomic_store_n(obj, val, type) -#define atomic_init(obj, val) atomic_store(obj, val) - -#define atomic_load_n(obj, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_load_1(obj, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_load_2(obj, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_load_4(obj, type) : \ - nx_atomic_load_8(obj, type)) - -#define atomic_load(obj) atomic_load_n(obj, __ATOMIC_RELAXED) -#define atomic_load_explicit(obj, type) atomic_load_n(obj, type) - -#define atomic_exchange_n(obj, val, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_exchange_1(obj, val, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_exchange_2(obj, val, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_exchange_4(obj, val, type) : \ - nx_atomic_exchange_8(obj, val, type)) - -#define atomic_exchange(obj, val) atomic_exchange_n(obj, val, __ATOMIC_RELAXED) -#define atomic_exchange_explicit(obj, val, type) atomic_exchange_n(obj, val, type) - -#define atomic_compare_exchange_n(obj, expected, desired, weak, success, failure) \ - (sizeof(*(obj)) == 1 ? nx_atomic_compare_exchange_1(obj, expected, desired, weak, success, failure) : \ - sizeof(*(obj)) == 2 ? nx_atomic_compare_exchange_2(obj, expected, desired, weak, success, failure) : \ - sizeof(*(obj)) == 4 ? nx_atomic_compare_exchange_4(obj, expected, desired, weak, success, failure) : \ - nx_atomic_compare_exchange_8(obj, expected, desired, weak, success, failure)) - -#define atomic_compare_exchange_strong(obj, expected, desired) \ - atomic_compare_exchange_n(obj, expected, desired, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED) -#define atomic_compare_exchange_strong_explicit(obj, expected, desired, success, failure) \ - atomic_compare_exchange_n(obj, expected, desired, false, success, failure) -#define atomic_compare_exchange_weak(obj, expected, desired) \ - atomic_compare_exchange_n(obj, expected, desired, true, __ATOMIC_RELAXED, __ATOMIC_RELAXED) -#define atomic_compare_exchange_weak_explicit(obj, expected, desired, success, failure) \ - atomic_compare_exchange_n(obj, expected, desired, true, success, failure) - -#define atomic_flag_test_and_set_n(obj, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_flag_test_and_set_1(obj, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_flag_test_and_set_2(obj, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_flag_test_and_set_4(obj, type) : \ - nx_atomic_flag_test_and_set_8(obj, type)) - -#define atomic_flag_test_and_set(obj) atomic_flag_test_and_set_n(obj, __ATOMIC_RELAXED) -#define atomic_flag_test_and_set_explicit(obj, type) atomic_flag_test_and_set_n(obj, 1, type) -#define atomic_flag_clear(obj) atomic_store(obj, 0) -#define atomic_flag_clear_explicit(obj, type) atomic_store_explicit(obj, 0, type) - -#define atomic_fetch_and_n(obj, val, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_fetch_and_1(obj, val, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_fetch_and_2(obj, val, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_fetch_and_4(obj, val, type) : \ - nx_atomic_fetch_and_8(obj, val, type)) - -#define atomic_fetch_and(obj, val) atomic_fetch_and_n(obj, val, __ATOMIC_RELAXED) -#define atomic_fetch_and_explicit(obj, val, type) atomic_fetch_and_n(obj, val, type) - -#define atomic_fetch_or_n(obj, val, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_fetch_or_1(obj, val, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_fetch_or_2(obj, val, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_fetch_or_4(obj, val, type) : \ - nx_atomic_fetch_or_8(obj, val, type)) - -#define atomic_fetch_or(obj, val) atomic_fetch_or_n(obj, val, __ATOMIC_RELAXED) -#define atomic_fetch_or_explicit(obj, val, type) atomic_fetch_or_n(obj, val, type) - -#define atomic_fetch_xor_n(obj, val, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_fetch_xor_1(obj, val, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_fetch_xor_2(obj, val, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_fetch_xor_4(obj, val, type) : \ - nx_atomic_fetch_xor_8(obj, val, type)) - -#define atomic_fetch_xor(obj, val) atomic_fetch_xor_n(obj, val, __ATOMIC_RELAXED) -#define atomic_fetch_xor_explicit(obj, val, type) atomic_fetch_xor_n(obj, val, type) - -#define atomic_fetch_add_n(obj, val, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_fetch_add_1(obj, val, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_fetch_add_2(obj, val, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_fetch_add_4(obj, val, type) : \ - nx_atomic_fetch_add_8(obj, val, type)) - -#define atomic_fetch_add(obj, val) atomic_fetch_add_n(obj, val, __ATOMIC_RELAXED) -#define atomic_fetch_add_explicit(obj, val, type) atomic_fetch_add_n(obj, val, type) - -#define atomic_fetch_sub_n(obj, val, type) \ - (sizeof(*(obj)) == 1 ? nx_atomic_fetch_sub_1(obj, val, type) : \ - sizeof(*(obj)) == 2 ? nx_atomic_fetch_sub_2(obj, val, type) : \ - sizeof(*(obj)) == 4 ? nx_atomic_fetch_sub_4(obj, val, type) : \ - nx_atomic_fetch_sub_8(obj, val, type)) - -#define atomic_fetch_sub(obj, val) atomic_fetch_sub_n(obj, val, __ATOMIC_RELAXED) -#define atomic_fetch_sub_explicit(obj, val, type) atomic_fetch_sub_n(obj, val, type) - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -typedef enum -{ - memory_order_relaxed = __ATOMIC_RELAXED, - memory_order_consume = __ATOMIC_CONSUME, - memory_order_acquire = __ATOMIC_ACQUIRE, - memory_order_release = __ATOMIC_RELEASE, - memory_order_acq_rel = __ATOMIC_ACQ_REL, - memory_order_seq_cst = __ATOMIC_SEQ_CST -} memory_order; - -typedef volatile int atomic_flag; -typedef volatile bool atomic_bool; -typedef volatile char atomic_char; -typedef volatile signed char atomic_schar; -typedef volatile unsigned char atomic_uchar; -typedef volatile short atomic_short; -typedef volatile unsigned short atomic_ushort; -typedef volatile int atomic_int; -typedef volatile unsigned int atomic_uint; -typedef volatile long atomic_long; -typedef volatile unsigned long atomic_ulong; -typedef volatile long long atomic_llong; -typedef volatile unsigned long long atomic_ullong; -typedef volatile wchar_t atomic_wchar_t; - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -void nx_atomic_store_1(FAR volatile void *ptr, uint8_t value, - int memorder); -void nx_atomic_store_2(FAR volatile void *ptr, uint16_t value, - int memorder); -void nx_atomic_store_4(FAR volatile void *ptr, uint32_t value, - int memorder); -void nx_atomic_store_8(FAR volatile void *ptr, uint64_t value, - int memorder); -uint8_t nx_atomic_load_1(FAR const volatile void *ptr, int memorder); -uint16_t nx_atomic_load_2(FAR const volatile void *ptr, int memorder); -uint32_t nx_atomic_load_4(FAR const volatile void *ptr, int memorder); -uint64_t nx_atomic_load_8(FAR const volatile void *ptr, int memorder); -uint8_t nx_atomic_exchange_1(FAR volatile void *ptr, uint8_t value, - int memorder); -uint16_t nx_atomic_exchange_2(FAR volatile void *ptr, uint16_t value, - int memorder); -uint32_t nx_atomic_exchange_4(FAR volatile void *ptr, uint32_t value, - int memorder); -uint64_t nx_atomic_exchange_8(FAR volatile void *ptr, uint64_t value, - int memorder); -bool nx_atomic_compare_exchange_1(FAR volatile void *mem, FAR void *expect, - uint8_t desired, bool weak, int success, - int failure); -bool nx_atomic_compare_exchange_2(FAR volatile void *mem, FAR void *expect, - uint16_t desired, bool weak, int success, - int failure); -bool nx_atomic_compare_exchange_4(FAR volatile void *mem, FAR void *expect, - uint32_t desired, bool weak, int success, - int failure); -bool nx_atomic_compare_exchange_8(FAR volatile void *mem, FAR void *expect, - uint64_t desired, bool weak, int success, - int failure); -uint8_t nx_atomic_flag_test_and_set_1(FAR const volatile void *ptr, - int memorder); -uint16_t nx_atomic_flag_test_and_set_2(FAR const volatile void *ptr, - int memorder); -uint32_t nx_atomic_flag_test_and_set_4(FAR const volatile void *ptr, - int memorder); -uint64_t nx_atomic_flag_test_and_set_8(FAR const volatile void *ptr, - int memorder); -uint8_t nx_atomic_fetch_add_1(FAR volatile void *ptr, uint8_t value, - int memorder); -uint16_t nx_atomic_fetch_add_2(FAR volatile void *ptr, uint16_t value, - int memorder); -uint32_t nx_atomic_fetch_add_4(FAR volatile void *ptr, uint32_t value, - int memorder); -uint64_t nx_atomic_fetch_add_8(FAR volatile void *ptr, uint64_t value, - int memorder); -uint8_t nx_atomic_fetch_sub_1(FAR volatile void *ptr, uint8_t value, - int memorder); -uint16_t nx_atomic_fetch_sub_2(FAR volatile void *ptr, uint16_t value, - int memorder); -uint32_t nx_atomic_fetch_sub_4(FAR volatile void *ptr, uint32_t value, - int memorder); -uint64_t nx_atomic_fetch_sub_8(FAR volatile void *ptr, uint64_t value, - int memorder); -uint8_t nx_atomic_fetch_and_1(FAR volatile void *ptr, uint8_t value, - int memorder); -uint16_t nx_atomic_fetch_and_2(FAR volatile void *ptr, uint16_t value, - int memorder); -uint32_t nx_atomic_fetch_and_4(FAR volatile void *ptr, uint32_t value, - int memorder); -uint64_t nx_atomic_fetch_and_8(FAR volatile void *ptr, uint64_t value, - int memorder); -uint8_t nx_atomic_fetch_or_1(FAR volatile void *ptr, uint8_t value, - int memorder); -uint16_t nx_atomic_fetch_or_2(FAR volatile void *ptr, uint16_t value, - int memorder); -uint32_t nx_atomic_fetch_or_4(FAR volatile void *ptr, uint32_t value, - int memorder); -uint64_t nx_atomic_fetch_or_8(FAR volatile void *ptr, uint64_t value, - int memorder); -uint8_t nx_atomic_fetch_xor_1(FAR volatile void *ptr, uint8_t value, - int memorder); -uint16_t nx_atomic_fetch_xor_2(FAR volatile void *ptr, uint16_t value, - int memorder); -uint32_t nx_atomic_fetch_xor_4(FAR volatile void *ptr, uint32_t value, - int memorder); -uint64_t nx_atomic_fetch_xor_8(FAR volatile void *ptr, uint64_t value, - int memorder); - -#endif /* __INCLUDE_NUTTX_LIB_STDATOMIC_H */ diff --git a/include/nuttx/net/netdev_lowerhalf.h b/include/nuttx/net/netdev_lowerhalf.h index bf56e0f26718c..ee61e49108e45 100644 --- a/include/nuttx/net/netdev_lowerhalf.h +++ b/include/nuttx/net/netdev_lowerhalf.h @@ -117,7 +117,7 @@ struct netdev_lowerhalf_s /* Max # of buffer held by driver */ - atomic_int quota[NETPKT_TYPENUM]; + atomic_t quota[NETPKT_TYPENUM]; /* The structure used by net stack. * Note: Do not change its fields unless you know what you are doing. diff --git a/include/nuttx/reset/reset.h b/include/nuttx/reset/reset.h index 9d5d908b79654..da98d0795f514 100644 --- a/include/nuttx/reset/reset.h +++ b/include/nuttx/reset/reset.h @@ -60,12 +60,12 @@ struct reset_control FAR struct reset_controller_dev *rcdev; struct list_node list; unsigned int id; - atomic_int refcnt; + atomic_t refcnt; bool acquired; bool shared; bool array; - atomic_int deassert_count; - atomic_int triggered_count; + atomic_t deassert_count; + atomic_t triggered_count; }; /**************************************************************************** diff --git a/include/nuttx/serial/uart_ram.h b/include/nuttx/serial/uart_ram.h index 04cff1b1f032f..f9cd25c46a74f 100644 --- a/include/nuttx/serial/uart_ram.h +++ b/include/nuttx/serial/uart_ram.h @@ -45,8 +45,8 @@ struct uart_rambuf_s { char buffer[CONFIG_RAM_UART_BUFSIZE]; - atomic_uint wroff; - atomic_uint rdoff; + atomic_t wroff; + atomic_t rdoff; }; /**************************************************************************** diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h index bd15818d6a7e7..466c20e28832d 100644 --- a/include/nuttx/spinlock.h +++ b/include/nuttx/spinlock.h @@ -68,10 +68,10 @@ union spinlock_u { struct { - unsigned short owner; - unsigned short next; + atomic_t owner; + atomic_t next; } tickets; - unsigned int value; + atomic64_t value; }; typedef union spinlock_u spinlock_t; @@ -243,9 +243,8 @@ static inline spinlock_t up_testset(FAR volatile spinlock_t *lock) static inline_function void spin_lock_wo_note(FAR volatile spinlock_t *lock) { #ifdef CONFIG_TICKET_SPINLOCK - unsigned short ticket = - atomic_fetch_add((FAR atomic_ushort *)&lock->tickets.next, 1); - while (atomic_load((FAR atomic_ushort *)&lock->tickets.owner) != ticket) + int ticket = atomic_fetch_add(&lock->tickets.next, 1); + while (atomic_read(&lock->tickets.owner) != ticket) #else /* CONFIG_TICKET_SPINLOCK */ while (up_testset(lock) == SP_LOCKED) #endif @@ -325,8 +324,7 @@ static inline_function bool spin_trylock_wo_note(FAR volatile spinlock_t *lock) { #ifdef CONFIG_TICKET_SPINLOCK - unsigned short ticket = - atomic_load((FAR atomic_ushort *)&lock->tickets.next); + int ticket = atomic_read(&lock->tickets.next); spinlock_t oldval = { @@ -342,8 +340,7 @@ spin_trylock_wo_note(FAR volatile spinlock_t *lock) } }; - if (!atomic_compare_exchange_strong((FAR atomic_uint *)&lock->value, - &oldval.value, newval.value)) + if (!atomic64_cmpxchg(&lock->value, &oldval.value, newval.value)) #else /* CONFIG_TICKET_SPINLOCK */ if (up_testset(lock) == SP_LOCKED) #endif /* CONFIG_TICKET_SPINLOCK */ @@ -431,7 +428,7 @@ spin_unlock_wo_note(FAR volatile spinlock_t *lock) { SP_DMB(); #ifdef CONFIG_TICKET_SPINLOCK - atomic_fetch_add((FAR atomic_ushort *)&lock->tickets.owner, 1); + atomic_fetch_add(&lock->tickets.owner, 1); #else *lock = SP_UNLOCKED; #endif @@ -813,15 +810,14 @@ static inline_function void read_lock(FAR volatile rwlock_t *lock) { while (true) { - int old = atomic_load((FAR atomic_int *)lock); + int old = atomic_read(lock); if (old <= RW_SP_WRITE_LOCKED) { DEBUGASSERT(old == RW_SP_WRITE_LOCKED); SP_DSB(); SP_WFE(); } - else if(atomic_compare_exchange_strong((FAR atomic_int *)lock, - &old, old + 1)) + else if(atomic_cmpxchg(lock, &old, old + 1)) { break; } @@ -858,14 +854,13 @@ static inline_function bool read_trylock(FAR volatile rwlock_t *lock) { while (true) { - int old = atomic_load((FAR atomic_int *)lock); + int old = atomic_read(lock); if (old <= RW_SP_WRITE_LOCKED) { DEBUGASSERT(old == RW_SP_WRITE_LOCKED); return false; } - else if (atomic_compare_exchange_strong((FAR atomic_int *)lock, - &old, old + 1)) + else if (atomic_cmpxchg(lock, &old, old + 1)) { break; } @@ -894,10 +889,10 @@ static inline_function bool read_trylock(FAR volatile rwlock_t *lock) static inline_function void read_unlock(FAR volatile rwlock_t *lock) { - DEBUGASSERT(atomic_load((FAR atomic_int *)lock) >= RW_SP_READ_LOCKED); + DEBUGASSERT(atomic_read(lock) >= RW_SP_READ_LOCKED); SP_DMB(); - atomic_fetch_sub((FAR atomic_int *)lock, 1); + atomic_fetch_sub(lock, 1); SP_DSB(); SP_SEV(); } @@ -931,8 +926,7 @@ static inline_function void write_lock(FAR volatile rwlock_t *lock) { int zero = RW_SP_UNLOCKED; - while (!atomic_compare_exchange_strong((FAR atomic_int *)lock, - &zero, RW_SP_WRITE_LOCKED)) + while (!atomic_cmpxchg(lock, &zero, RW_SP_WRITE_LOCKED)) { SP_DSB(); SP_WFE(); @@ -970,8 +964,7 @@ static inline_function bool write_trylock(FAR volatile rwlock_t *lock) { int zero = RW_SP_UNLOCKED; - if (atomic_compare_exchange_strong((FAR atomic_int *)lock, - &zero, RW_SP_WRITE_LOCKED)) + if (atomic_cmpxchg(lock, &zero, RW_SP_WRITE_LOCKED)) { SP_DMB(); return true; @@ -1002,10 +995,10 @@ static inline_function void write_unlock(FAR volatile rwlock_t *lock) { /* Ensure this cpu already get write lock */ - DEBUGASSERT(atomic_load((FAR atomic_int *)lock) == RW_SP_WRITE_LOCKED); + DEBUGASSERT(atomic_read(lock) == RW_SP_WRITE_LOCKED); SP_DMB(); - atomic_store((FAR atomic_int *)lock, RW_SP_UNLOCKED); + atomic_set(lock, RW_SP_UNLOCKED); SP_DSB(); SP_SEV(); } diff --git a/include/semaphore.h b/include/semaphore.h index bfd96cab57a81..079dae67b3277 100644 --- a/include/semaphore.h +++ b/include/semaphore.h @@ -33,6 +33,7 @@ #include #include #include +#include /**************************************************************************** * Pre-processor Definitions @@ -104,7 +105,7 @@ struct semholder_s struct sem_s { - volatile int16_t semcount; /* >0 -> Num counts available */ + atomic_t semcount; /* >0 -> Num counts available */ /* <0 -> Num tasks waiting for semaphore */ /* If priority inheritance is enabled, then we have to keep track of which diff --git a/libs/libc/machine/arch_atomic.c b/libs/libc/machine/arch_atomic.c index bdecc8c057f69..b03e4cbe113f9 100644 --- a/libs/libc/machine/arch_atomic.c +++ b/libs/libc/machine/arch_atomic.c @@ -323,280 +323,240 @@ ****************************************************************************/ STORE(__atomic_store_, 1, uint8_t) -STORE(nx_atomic_store_, 1, uint8_t) /**************************************************************************** * Name: __atomic_store_2 ****************************************************************************/ STORE(__atomic_store_, 2, uint16_t) -STORE(nx_atomic_store_, 2, uint16_t) /**************************************************************************** * Name: __atomic_store_4 ****************************************************************************/ STORE(__atomic_store_, 4, uint32_t) -STORE(nx_atomic_store_, 4, uint32_t) /**************************************************************************** * Name: __atomic_store_8 ****************************************************************************/ STORE(__atomic_store_, 8, uint64_t) -STORE(nx_atomic_store_, 8, uint64_t) /**************************************************************************** * Name: __atomic_load_1 ****************************************************************************/ LOAD(__atomic_load_, 1, uint8_t) -LOAD(nx_atomic_load_, 1, uint8_t) /**************************************************************************** * Name: __atomic_load__2 ****************************************************************************/ LOAD(__atomic_load_, 2, uint16_t) -LOAD(nx_atomic_load_, 2, uint16_t) /**************************************************************************** * Name: __atomic_load__4 ****************************************************************************/ LOAD(__atomic_load_, 4, uint32_t) -LOAD(nx_atomic_load_, 4, uint32_t) /**************************************************************************** * Name: __atomic_load__8 ****************************************************************************/ LOAD(__atomic_load_, 8, uint64_t) -LOAD(nx_atomic_load_, 8, uint64_t) /**************************************************************************** * Name: __atomic_exchange_1 ****************************************************************************/ EXCHANGE(__atomic_exchange_, 1, uint8_t) -EXCHANGE(nx_atomic_exchange_, 1, uint8_t) /**************************************************************************** * Name: __atomic_exchange__2 ****************************************************************************/ EXCHANGE(__atomic_exchange_, 2, uint16_t) -EXCHANGE(nx_atomic_exchange_, 2, uint16_t) /**************************************************************************** * Name: __atomic_exchange__4 ****************************************************************************/ EXCHANGE(__atomic_exchange_, 4, uint32_t) -EXCHANGE(nx_atomic_exchange_, 4, uint32_t) /**************************************************************************** * Name: __atomic_exchange__8 ****************************************************************************/ EXCHANGE(__atomic_exchange_, 8, uint64_t) -EXCHANGE(nx_atomic_exchange_, 8, uint64_t) /**************************************************************************** * Name: __atomic_compare_exchange_1 ****************************************************************************/ CMP_EXCHANGE(__atomic_compare_exchange_, 1, uint8_t) -CMP_EXCHANGE(nx_atomic_compare_exchange_, 1, uint8_t) /**************************************************************************** * Name: __atomic_compare_exchange_2 ****************************************************************************/ CMP_EXCHANGE(__atomic_compare_exchange_, 2, uint16_t) -CMP_EXCHANGE(nx_atomic_compare_exchange_, 2, uint16_t) /**************************************************************************** * Name: __atomic_compare_exchange_4 ****************************************************************************/ CMP_EXCHANGE(__atomic_compare_exchange_, 4, uint32_t) -CMP_EXCHANGE(nx_atomic_compare_exchange_, 4, uint32_t) /**************************************************************************** * Name: __atomic_compare_exchange_8 ****************************************************************************/ CMP_EXCHANGE(__atomic_compare_exchange_, 8, uint64_t) -CMP_EXCHANGE(nx_atomic_compare_exchange_, 8, uint64_t) /**************************************************************************** * Name: __atomic_flag_test_and_set_1 ****************************************************************************/ FLAG_TEST_AND_SET(__atomic_flags_test_and_set_, 1, uint8_t) -FLAG_TEST_AND_SET(nx_atomic_flags_test_and_set_, 1, uint8_t) /**************************************************************************** * Name: __atomic_flag_test_and_set_2 ****************************************************************************/ FLAG_TEST_AND_SET(__atomic_flags_test_and_set_, 2, uint16_t) -FLAG_TEST_AND_SET(nx_atomic_flags_test_and_set_, 2, uint16_t) /**************************************************************************** * Name: __atomic_flag_test_and_set_4 ****************************************************************************/ FLAG_TEST_AND_SET(__atomic_flags_test_and_set_, 4, uint32_t) -FLAG_TEST_AND_SET(nx_atomic_flags_test_and_set_, 4, uint32_t) /**************************************************************************** * Name: __atomic_flag_test_and_set_8 ****************************************************************************/ FLAG_TEST_AND_SET(__atomic_flags_test_and_set_, 8, uint64_t) -FLAG_TEST_AND_SET(nx_atomic_flags_test_and_set_, 8, uint64_t) /**************************************************************************** * Name: __atomic_fetch_add_1 ****************************************************************************/ FETCH_ADD(__atomic_fetch_add_, 1, uint8_t) -FETCH_ADD(nx_atomic_fetch_add_, 1, uint8_t) /**************************************************************************** * Name: __atomic_fetch_add_2 ****************************************************************************/ FETCH_ADD(__atomic_fetch_add_, 2, uint16_t) -FETCH_ADD(nx_atomic_fetch_add_, 2, uint16_t) /**************************************************************************** * Name: __atomic_fetch_add_4 ****************************************************************************/ FETCH_ADD(__atomic_fetch_add_, 4, uint32_t) -FETCH_ADD(nx_atomic_fetch_add_, 4, uint32_t) /**************************************************************************** * Name: __atomic_fetch_add_8 ****************************************************************************/ FETCH_ADD(__atomic_fetch_add_, 8, uint64_t) -FETCH_ADD(nx_atomic_fetch_add_, 8, uint64_t) /**************************************************************************** * Name: __atomic_fetch_sub_1 ****************************************************************************/ FETCH_SUB(__atomic_fetch_sub_, 1, uint8_t) -FETCH_SUB(nx_atomic_fetch_sub_, 1, uint8_t) /**************************************************************************** * Name: __atomic_fetch_sub_2 ****************************************************************************/ FETCH_SUB(__atomic_fetch_sub_, 2, uint16_t) -FETCH_SUB(nx_atomic_fetch_sub_, 2, uint16_t) /**************************************************************************** * Name: __atomic_fetch_sub_4 ****************************************************************************/ FETCH_SUB(__atomic_fetch_sub_, 4, uint32_t) -FETCH_SUB(nx_atomic_fetch_sub_, 4, uint32_t) /**************************************************************************** * Name: __atomic_fetch_sub_8 ****************************************************************************/ FETCH_SUB(__atomic_fetch_sub_, 8, uint64_t) -FETCH_SUB(nx_atomic_fetch_sub_, 8, uint64_t) /**************************************************************************** * Name: __atomic_fetch_and_1 ****************************************************************************/ FETCH_AND(__atomic_fetch_and_, 1, uint8_t) -FETCH_AND(nx_atomic_fetch_and_, 1, uint8_t) /**************************************************************************** * Name: __atomic_fetch_and_2 ****************************************************************************/ FETCH_AND(__atomic_fetch_and_, 2, uint16_t) -FETCH_AND(nx_atomic_fetch_and_, 2, uint16_t) /**************************************************************************** * Name: __atomic_fetch_and_4 ****************************************************************************/ FETCH_AND(__atomic_fetch_and_, 4, uint32_t) -FETCH_AND(nx_atomic_fetch_and_, 4, uint32_t) /**************************************************************************** * Name: __atomic_fetch_and_8 ****************************************************************************/ FETCH_AND(__atomic_fetch_and_, 8, uint64_t) -FETCH_AND(nx_atomic_fetch_and_, 8, uint64_t) /**************************************************************************** * Name: __atomic_fetch_or_1 ****************************************************************************/ FETCH_OR(__atomic_fetch_or_, 1, uint8_t) -FETCH_OR(nx_atomic_fetch_or_, 1, uint8_t) /**************************************************************************** * Name: __atomic_fetch_or_2 ****************************************************************************/ FETCH_OR(__atomic_fetch_or_, 2, uint16_t) -FETCH_OR(nx_atomic_fetch_or_, 2, uint16_t) /**************************************************************************** * Name: __atomic_fetch_or_4 ****************************************************************************/ FETCH_OR(__atomic_fetch_or_, 4, uint32_t) -FETCH_OR(nx_atomic_fetch_or_, 4, uint32_t) /**************************************************************************** * Name: __atomic_fetch_or_4 ****************************************************************************/ FETCH_OR(__atomic_fetch_or_, 8, uint64_t) -FETCH_OR(nx_atomic_fetch_or_, 8, uint64_t) /**************************************************************************** * Name: __atomic_fetch_xor_1 ****************************************************************************/ FETCH_XOR(__atomic_fetch_xor_, 1, uint8_t) -FETCH_XOR(nx_atomic_fetch_xor_, 1, uint8_t) /**************************************************************************** * Name: __atomic_fetch_xor_2 ****************************************************************************/ FETCH_XOR(__atomic_fetch_xor_, 2, uint16_t) -FETCH_XOR(nx_atomic_fetch_xor_, 2, uint16_t) /**************************************************************************** * Name: __atomic_fetch_xor_4 ****************************************************************************/ FETCH_XOR(__atomic_fetch_xor_, 4, uint32_t) -FETCH_XOR(nx_atomic_fetch_xor_, 4, uint32_t) /**************************************************************************** * Name: __atomic_fetch_xor_8 ****************************************************************************/ FETCH_XOR(__atomic_fetch_xor_, 8, uint64_t) -FETCH_XOR(nx_atomic_fetch_xor_, 8, uint64_t) /* Clang define the __sync builtins, add #ifndef to avoid * redefined/redeclared problem. @@ -609,224 +569,192 @@ FETCH_XOR(nx_atomic_fetch_xor_, 8, uint64_t) ****************************************************************************/ SYNC_ADD_FETCH(__sync_add_and_fetch_, 1, uint8_t) -SYNC_ADD_FETCH(nx_sync_add_and_fetch_, 1, uint8_t) /**************************************************************************** * Name: __sync_add_and_fetch_2 ****************************************************************************/ SYNC_ADD_FETCH(__sync_add_and_fetch_, 2, uint16_t) -SYNC_ADD_FETCH(nx_sync_add_and_fetch_, 2, uint16_t) /**************************************************************************** * Name: __sync_add_and_fetch_4 ****************************************************************************/ SYNC_ADD_FETCH(__sync_add_and_fetch_, 4, uint32_t) -SYNC_ADD_FETCH(nx_sync_add_and_fetch_, 4, uint32_t) /**************************************************************************** * Name: __sync_add_and_fetch_8 ****************************************************************************/ SYNC_ADD_FETCH(__sync_add_and_fetch_, 8, uint64_t) -SYNC_ADD_FETCH(nx_sync_add_and_fetch_, 8, uint64_t) /**************************************************************************** * Name: __sync_sub_and_fetch_1 ****************************************************************************/ SYNC_SUB_FETCH(__sync_sub_and_fetch_, 1, uint8_t) -SYNC_SUB_FETCH(nx_sync_sub_and_fetch_, 1, uint8_t) /**************************************************************************** * Name: __sync_sub_and_fetch_2 ****************************************************************************/ SYNC_SUB_FETCH(__sync_sub_and_fetch_, 2, uint16_t) -SYNC_SUB_FETCH(nx_sync_sub_and_fetch_, 2, uint16_t) /**************************************************************************** * Name: __sync_sub_and_fetch_4 ****************************************************************************/ SYNC_SUB_FETCH(__sync_sub_and_fetch_, 4, uint32_t) -SYNC_SUB_FETCH(nx_sync_sub_and_fetch_, 4, uint32_t) /**************************************************************************** * Name: __sync_sub_and_fetch_8 ****************************************************************************/ SYNC_SUB_FETCH(__sync_sub_and_fetch_, 8, uint64_t) -SYNC_SUB_FETCH(nx_sync_sub_and_fetch_, 8, uint64_t) /**************************************************************************** * Name: __sync_or_and_fetch_1 ****************************************************************************/ SYNC_OR_FETCH(__sync_or_and_fetch_, 1, uint8_t) -SYNC_OR_FETCH(nx_sync_or_and_fetch_, 1, uint8_t) /**************************************************************************** * Name: __sync_or_and_fetch_2 ****************************************************************************/ SYNC_OR_FETCH(__sync_or_and_fetch_, 2, uint16_t) -SYNC_OR_FETCH(nx_sync_or_and_fetch_, 2, uint16_t) /**************************************************************************** * Name: __sync_or_and_fetch_4 ****************************************************************************/ SYNC_OR_FETCH(__sync_or_and_fetch_, 4, uint32_t) -SYNC_OR_FETCH(nx_sync_or_and_fetch_, 4, uint32_t) /**************************************************************************** * Name: __sync_or_and_fetch_8 ****************************************************************************/ SYNC_OR_FETCH(__sync_or_and_fetch_, 8, uint64_t) -SYNC_OR_FETCH(nx_sync_or_and_fetch_, 8, uint64_t) /**************************************************************************** * Name: __sync_and_and_fetch_1 ****************************************************************************/ SYNC_AND_FETCH(__sync_and_and_fetch_, 1, uint8_t) -SYNC_AND_FETCH(nx_sync_and_and_fetch_, 1, uint8_t) /**************************************************************************** * Name: __sync_and_and_fetch_2 ****************************************************************************/ SYNC_AND_FETCH(__sync_and_and_fetch_, 2, uint16_t) -SYNC_AND_FETCH(nx_sync_and_and_fetch_, 2, uint16_t) /**************************************************************************** * Name: __sync_and_and_fetch_4 ****************************************************************************/ SYNC_AND_FETCH(__sync_and_and_fetch_, 4, uint32_t) -SYNC_AND_FETCH(nx_sync_and_and_fetch_, 4, uint32_t) /**************************************************************************** * Name: __sync_and_and_fetch_8 ****************************************************************************/ SYNC_AND_FETCH(__sync_and_and_fetch_, 8, uint64_t) -SYNC_AND_FETCH(nx_sync_and_and_fetch_, 8, uint64_t) /**************************************************************************** * Name: __sync_xor_and_fetch_1 ****************************************************************************/ SYNC_XOR_FETCH(__sync_xor_and_fetch_, 1, uint8_t) -SYNC_XOR_FETCH(nx_sync_xor_and_fetch_, 1, uint8_t) /**************************************************************************** * Name: __sync_xor_and_fetch_2 ****************************************************************************/ SYNC_XOR_FETCH(__sync_xor_and_fetch_, 2, uint16_t) -SYNC_XOR_FETCH(nx_sync_xor_and_fetch_, 2, uint16_t) /**************************************************************************** * Name: __sync_xor_and_fetch_4 ****************************************************************************/ SYNC_XOR_FETCH(__sync_xor_and_fetch_, 4, uint32_t) -SYNC_XOR_FETCH(nx_sync_xor_and_fetch_, 4, uint32_t) /**************************************************************************** * Name: __sync_xor_and_fetch_8 ****************************************************************************/ SYNC_XOR_FETCH(__sync_xor_and_fetch_, 8, uint64_t) -SYNC_XOR_FETCH(nx_sync_xor_and_fetch_, 8, uint64_t) /**************************************************************************** * Name: __sync_nand_and_fetch_1 ****************************************************************************/ SYNC_NAND_FETCH(__sync_nand_and_fetch_, 1, uint8_t) -SYNC_NAND_FETCH(nx_sync_nand_and_fetch_, 1, uint8_t) /**************************************************************************** * Name: __sync_nand_and_fetch_2 ****************************************************************************/ SYNC_NAND_FETCH(__sync_nand_and_fetch_, 2, uint16_t) -SYNC_NAND_FETCH(nx_sync_nand_and_fetch_, 2, uint16_t) /**************************************************************************** * Name: __sync_nand_and_fetch_4 ****************************************************************************/ SYNC_NAND_FETCH(__sync_nand_and_fetch_, 4, uint32_t) -SYNC_NAND_FETCH(nx_sync_nand_and_fetch_, 4, uint32_t) /**************************************************************************** * Name: __sync_nand_and_fetch_8 ****************************************************************************/ SYNC_NAND_FETCH(__sync_nand_and_fetch_, 8, uint64_t) -SYNC_NAND_FETCH(nx_sync_nand_and_fetch_, 8, uint64_t) /**************************************************************************** * Name: __sync_bool_compare_and_swap_1 ****************************************************************************/ SYNC_BOOL_CMP_SWAP(__sync_bool_compare_and_swap_, 1, uint8_t) -SYNC_BOOL_CMP_SWAP(nx_sync_bool_compare_and_swap_, 1, uint8_t) /**************************************************************************** * Name: __sync_bool_compare_and_swap_2 ****************************************************************************/ SYNC_BOOL_CMP_SWAP(__sync_bool_compare_and_swap_, 2, uint16_t) -SYNC_BOOL_CMP_SWAP(nx_sync_bool_compare_and_swap_, 2, uint16_t) /**************************************************************************** * Name: __sync_bool_compare_and_swap_4 ****************************************************************************/ SYNC_BOOL_CMP_SWAP(__sync_bool_compare_and_swap_, 4, uint32_t) -SYNC_BOOL_CMP_SWAP(nx_sync_bool_compare_and_swap_, 4, uint32_t) /**************************************************************************** * Name: __sync_bool_compare_and_swap_8 ****************************************************************************/ SYNC_BOOL_CMP_SWAP(__sync_bool_compare_and_swap_, 8, uint64_t) -SYNC_BOOL_CMP_SWAP(nx_sync_bool_compare_and_swap_, 8, uint64_t) /**************************************************************************** * Name: __sync_val_compare_and_swap_1 ****************************************************************************/ SYNC_VAL_CMP_SWAP(__sync_val_compare_and_swap_, 1, uint8_t) -SYNC_VAL_CMP_SWAP(nx_sync_val_compare_and_swap_, 1, uint8_t) /**************************************************************************** * Name: __sync_val_compare_and_swap_2 ****************************************************************************/ SYNC_VAL_CMP_SWAP(__sync_val_compare_and_swap_, 2, uint16_t) -SYNC_VAL_CMP_SWAP(nx_sync_val_compare_and_swap_, 2, uint16_t) /**************************************************************************** * Name: __sync_val_compare_and_swap_4 ****************************************************************************/ SYNC_VAL_CMP_SWAP(__sync_val_compare_and_swap_, 4, uint32_t) -SYNC_VAL_CMP_SWAP(nx_sync_val_compare_and_swap_, 4, uint32_t) /**************************************************************************** * Name: __sync_val_compare_and_swap_8 ****************************************************************************/ SYNC_VAL_CMP_SWAP(__sync_val_compare_and_swap_, 8, uint64_t) -SYNC_VAL_CMP_SWAP(nx_sync_val_compare_and_swap_, 8, uint64_t) /**************************************************************************** * Name: __sync_synchronize diff --git a/libs/libc/misc/lib_fdsan.c b/libs/libc/misc/lib_fdsan.c index f85dd59b34f9d..0d349b7c7639b 100644 --- a/libs/libc/misc/lib_fdsan.c +++ b/libs/libc/misc/lib_fdsan.c @@ -199,7 +199,7 @@ void android_fdsan_exchange_owner_tag(int fd, uint64_t expected_tag, * but expected == actual? ******************************************************************/ - ferr("fdsan atomic_compare_exchange_strong failed unexpectedly " + ferr("fdsan atomic_cmpxchg failed unexpectedly " "while exchanging owner tag\n"); PANIC(); } diff --git a/sched/semaphore/sem_destroy.c b/sched/semaphore/sem_destroy.c index fa94ef92ad456..391b9cb508351 100644 --- a/sched/semaphore/sem_destroy.c +++ b/sched/semaphore/sem_destroy.c @@ -60,7 +60,7 @@ int nxsem_destroy(FAR sem_t *sem) { - short old; + int old; DEBUGASSERT(sem != NULL); @@ -76,16 +76,13 @@ int nxsem_destroy(FAR sem_t *sem) do { - old = atomic_load(NXSEM_COUNT(sem)); + old = atomic_read(&sem->semcount); if (old < 0) { break; } } - while (!atomic_compare_exchange_weak_explicit(NXSEM_COUNT(sem), - &old, 1, - memory_order_release, - memory_order_relaxed)); + while (!atomic_try_cmpxchg_release(&sem->semcount, &old, 1)); /* Release holders of the semaphore */ diff --git a/sched/semaphore/sem_holder.c b/sched/semaphore/sem_holder.c index 1da9f3e2a94de..307c9aa81aed2 100644 --- a/sched/semaphore/sem_holder.c +++ b/sched/semaphore/sem_holder.c @@ -880,7 +880,7 @@ void nxsem_canceled(FAR struct tcb_s *stcb, FAR sem_t *sem) { /* Check our assumptions */ - DEBUGASSERT(atomic_load(NXSEM_COUNT(sem)) <= 0); + DEBUGASSERT(atomic_read(&sem->semcount) <= 0); /* Adjust the priority of every holder as necessary */ @@ -978,7 +978,7 @@ void nxsem_release_all(FAR struct tcb_s *htcb) * that was taken by sem_wait() or sem_post(). */ - atomic_fetch_add(NXSEM_COUNT(sem), 1); + atomic_fetch_add(&sem->semcount, 1); } } diff --git a/sched/semaphore/sem_post.c b/sched/semaphore/sem_post.c index 500b00f5954ce..6f8d358521fc5 100644 --- a/sched/semaphore/sem_post.c +++ b/sched/semaphore/sem_post.c @@ -85,13 +85,13 @@ static int nxsem_post_slow(FAR sem_t *sem) flags = enter_critical_section(); - sem_count = atomic_fetch_add(NXSEM_COUNT(sem), 1); + sem_count = atomic_fetch_add(&sem->semcount, 1); /* Check the maximum allowable value */ if (sem_count >= SEM_VALUE_MAX) { - atomic_fetch_sub(NXSEM_COUNT(sem), 1); + atomic_fetch_sub(&sem->semcount, 1); leave_critical_section(flags); return -EOVERFLOW; } @@ -259,10 +259,8 @@ int nxsem_post(FAR sem_t *sem) #if !defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_PRIORITY_PROTECT) if (sem->flags & SEM_TYPE_MUTEX) { - short old = 0; - if (atomic_compare_exchange_weak_explicit(NXSEM_COUNT(sem), &old, 1, - memory_order_release, - memory_order_relaxed)) + int old = 0; + if (atomic_try_cmpxchg_release(&sem->semcount, &old, 1)) { return OK; } diff --git a/sched/semaphore/sem_recover.c b/sched/semaphore/sem_recover.c index dd0418d4bbdce..44fcd57744347 100644 --- a/sched/semaphore/sem_recover.c +++ b/sched/semaphore/sem_recover.c @@ -85,7 +85,7 @@ void nxsem_recover(FAR struct tcb_s *tcb) if (tcb->task_state == TSTATE_WAIT_SEM) { FAR sem_t *sem = tcb->waitobj; - DEBUGASSERT(sem != NULL && atomic_load(NXSEM_COUNT(sem)) < 0); + DEBUGASSERT(sem != NULL && atomic_read(&sem->semcount) < 0); /* Restore the correct priority of all threads that hold references * to this semaphore. @@ -99,7 +99,7 @@ void nxsem_recover(FAR struct tcb_s *tcb) * place. */ - atomic_fetch_add(NXSEM_COUNT(sem), 1); + atomic_fetch_add(&sem->semcount, 1); } /* Release all semphore holders for the task */ diff --git a/sched/semaphore/sem_reset.c b/sched/semaphore/sem_reset.c index d7774ea504291..f7333fbca752a 100644 --- a/sched/semaphore/sem_reset.c +++ b/sched/semaphore/sem_reset.c @@ -60,7 +60,7 @@ int nxsem_reset(FAR sem_t *sem, int16_t count) { irqstate_t flags; - short semcount; + int semcount; DEBUGASSERT(sem != NULL && count >= 0); @@ -81,7 +81,7 @@ int nxsem_reset(FAR sem_t *sem, int16_t count) * out counts to any waiting threads. */ - while (atomic_load(NXSEM_COUNT(sem)) < 0 && count > 0) + while (atomic_read(&sem->semcount) < 0 && count > 0) { /* Give out one counting, waking up one of the waiting threads * and, perhaps, kicking off a lot of priority inheritance @@ -101,16 +101,13 @@ int nxsem_reset(FAR sem_t *sem, int16_t count) do { - semcount = atomic_load(NXSEM_COUNT(sem)); + semcount = atomic_read(&sem->semcount); if (semcount < 0) { break; } } - while (!atomic_compare_exchange_weak_explicit(NXSEM_COUNT(sem), - &semcount, count, - memory_order_release, - memory_order_relaxed)); + while (!atomic_try_cmpxchg_release(&sem->semcount, &semcount, count)); /* Allow any pending context switches to occur now */ diff --git a/sched/semaphore/sem_trywait.c b/sched/semaphore/sem_trywait.c index c4b512547bd7f..f0542ab8b4923 100644 --- a/sched/semaphore/sem_trywait.c +++ b/sched/semaphore/sem_trywait.c @@ -64,7 +64,7 @@ static int nxsem_trywait_slow(FAR sem_t *sem) { FAR struct tcb_s *rtcb; irqstate_t flags; - short semcount; + int semcount; int ret; /* The following operations must be performed with interrupts disabled @@ -78,24 +78,22 @@ static int nxsem_trywait_slow(FAR sem_t *sem) do { - semcount = atomic_load(NXSEM_COUNT(sem)); + semcount = atomic_read(&sem->semcount); if (semcount <= 0) { leave_critical_section(flags); return -EAGAIN; } } - while (!atomic_compare_exchange_weak_explicit(NXSEM_COUNT(sem), - &semcount, semcount - 1, - memory_order_acquire, - memory_order_relaxed)); + while (!atomic_try_cmpxchg_acquire(&sem->semcount, + &semcount, semcount - 1)); /* It is, let the task take the semaphore */ ret = nxsem_protect_wait(sem); if (ret < 0) { - atomic_fetch_add(NXSEM_COUNT(sem), 1); + atomic_fetch_add(&sem->semcount, 1); leave_critical_section(flags); return ret; } @@ -153,10 +151,8 @@ int nxsem_trywait(FAR sem_t *sem) #if !defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_PRIORITY_PROTECT) if (sem->flags & SEM_TYPE_MUTEX) { - short old = 1; - if (atomic_compare_exchange_weak_explicit(NXSEM_COUNT(sem), &old, 0, - memory_order_acquire, - memory_order_relaxed)) + int old = 1; + if (atomic_try_cmpxchg_acquire(&sem->semcount, &old, 0)) { return OK; } diff --git a/sched/semaphore/sem_wait.c b/sched/semaphore/sem_wait.c index a0a8b9d76f177..b1f5fea7d0a4a 100644 --- a/sched/semaphore/sem_wait.c +++ b/sched/semaphore/sem_wait.c @@ -85,7 +85,7 @@ static int nxsem_wait_slow(FAR sem_t *sem) /* Check if the lock is available */ - if (atomic_fetch_sub(NXSEM_COUNT(sem), 1) > 0) + if (atomic_fetch_sub(&sem->semcount, 1) > 0) { /* It is, let the task take the semaphore. */ @@ -259,10 +259,8 @@ int nxsem_wait(FAR sem_t *sem) #if !defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_PRIORITY_PROTECT) if (sem->flags & SEM_TYPE_MUTEX) { - short old = 1; - if (atomic_compare_exchange_weak_explicit(NXSEM_COUNT(sem), &old, 0, - memory_order_acquire, - memory_order_relaxed)) + int old = 1; + if (atomic_try_cmpxchg_acquire(&sem->semcount, &old, 0)) { return OK; } diff --git a/sched/semaphore/sem_waitirq.c b/sched/semaphore/sem_waitirq.c index e3cd08db1825d..eaabe37044e73 100644 --- a/sched/semaphore/sem_waitirq.c +++ b/sched/semaphore/sem_waitirq.c @@ -87,7 +87,7 @@ void nxsem_wait_irq(FAR struct tcb_s *wtcb, int errcode) * and already changed the task's state. */ - DEBUGASSERT(sem != NULL && atomic_load(NXSEM_COUNT(sem)) < 0); + DEBUGASSERT(sem != NULL && atomic_read(&sem->semcount) < 0); /* Restore the correct priority of all threads that hold references * to this semaphore. @@ -101,7 +101,7 @@ void nxsem_wait_irq(FAR struct tcb_s *wtcb, int errcode) * place. */ - atomic_fetch_add(NXSEM_COUNT(sem), 1); + atomic_fetch_add(&sem->semcount, 1); /* Remove task from waiting list */ diff --git a/sched/semaphore/semaphore.h b/sched/semaphore/semaphore.h index 45f745f58bc26..b61085ea3c3e6 100644 --- a/sched/semaphore/semaphore.h +++ b/sched/semaphore/semaphore.h @@ -31,17 +31,10 @@ #include #include #include -#include #include #include -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define NXSEM_COUNT(s) ((FAR atomic_short *)&(s)->semcount) - /**************************************************************************** * Public Function Prototypes ****************************************************************************/