From 35500e035b3e3158aff90ed5304c244e7833f94f Mon Sep 17 00:00:00 2001 From: zhangyuan29 Date: Tue, 19 Nov 2024 10:13:33 +0800 Subject: [PATCH 1/2] 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 | 38 +-- drivers/i3c/master.c | 10 +- drivers/input/aw86225.c | 14 +- drivers/input/aw86225_internal.h | 6 +- drivers/net/netdev_upperhalf.c | 16 +- drivers/note/notesnap_driver.c | 12 +- drivers/reset/core.c | 22 +- drivers/rpmsg/rpmsg_port.c | 4 +- drivers/rpmsg/rpmsg_port.h | 6 +- drivers/rpmsg/rpmsg_port_spi.c | 6 +- drivers/rpmsg/rpmsg_port_spi_slave.c | 8 +- drivers/serial/pty.c | 2 +- drivers/serial/uart_ram.c | 32 +- drivers/wireless/bluetooth/bt_bridge.c | 8 +- drivers/wireless/bluetooth/bt_slip.c | 2 +- fs/event/event_close.c | 2 +- fs/event/event_open.c | 2 +- fs/inode/fs_inodeaddref.c | 2 +- fs/inode/fs_inodefind.c | 2 +- fs/inode/fs_inoderelease.c | 2 +- fs/inode/fs_inoderemove.c | 4 +- fs/inode/fs_inodereserve.c | 2 +- fs/mount/fs_mount.c | 4 +- fs/mount/fs_umount2.c | 2 +- fs/mqueue/mq_open.c | 4 +- fs/mqueue/mq_unlink.c | 2 +- fs/semaphore/sem_close.c | 2 +- fs/semaphore/sem_open.c | 2 +- fs/shm/shm_open.c | 2 +- fs/shm/shmfs.c | 4 +- fs/vfs/fs_dir.c | 4 +- fs/vfs/fs_pseudofile.c | 2 +- include/nuttx/atomic.h | 413 +++++++++++++++++++++---- include/nuttx/fs/fs.h | 2 +- include/nuttx/i3c/master.h | 4 +- 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/addrenv/addrenv.c | 4 +- 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 - 55 files changed, 524 insertions(+), 620 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..f6cde9f2ba177 100644 --- a/arch/sim/src/sim/sim_heap.c +++ b/arch/sim/src/sim/sim_heap.c @@ -58,9 +58,9 @@ struct mm_heap_s size_t mm_delaycount[CONFIG_SMP_NCPUS]; #endif - atomic_int aordblks; - atomic_int uordblks; - atomic_int usmblks; + atomic_t aordblks; + atomic_t uordblks; + atomic_t usmblks; #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO) struct procfs_meminfo_entry_s mm_procfs; @@ -184,8 +184,8 @@ static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay) else { int size = host_mallocsize(mem); - atomic_fetch_sub(&heap->aordblks, 1); - atomic_fetch_sub(&heap->uordblks, size); + atomic_fetch_sub_full(&heap->aordblks, 1); + atomic_fetch_sub_full(&heap->uordblks, size); sched_note_heap(NOTE_HEAP_FREE, heap, mem, size, 0); host_free(mem); } @@ -379,13 +379,13 @@ void *mm_realloc(struct mm_heap_s *heap, void *oldmem, } oldsize = host_mallocsize(oldmem); - atomic_fetch_sub(&heap->uordblks, oldsize); + atomic_fetch_sub_full(&heap->uordblks, oldsize); mem = host_realloc(oldmem, size); - atomic_fetch_add(&heap->aordblks, oldmem == NULL && mem != NULL); + atomic_fetch_add_full(&heap->aordblks, oldmem == NULL && mem != NULL); newsize = host_mallocsize(mem ? mem : oldmem); - atomic_fetch_add(&heap->uordblks, newsize); - usmblks = atomic_load(&heap->usmblks); + atomic_fetch_add_full(&heap->uordblks, newsize); + usmblks = atomic_read(&heap->usmblks); if (mem != NULL) { if (oldmem != NULL) @@ -398,13 +398,13 @@ void *mm_realloc(struct mm_heap_s *heap, void *oldmem, do { - uordblks = atomic_load(&heap->uordblks); + uordblks = atomic_read(&heap->uordblks); if (uordblks <= usmblks) { 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)) @@ -486,19 +486,19 @@ void *mm_memalign(struct mm_heap_s *heap, size_t alignment, size_t size) size = host_mallocsize(mem); sched_note_heap(NOTE_HEAP_ALLOC, heap, mem, size, 0); - atomic_fetch_add(&heap->aordblks, 1); - atomic_fetch_add(&heap->uordblks, size); - usmblks = atomic_load(&heap->usmblks); + atomic_fetch_add_full(&heap->aordblks, 1); + atomic_fetch_add_full(&heap->uordblks, size); + usmblks = atomic_read(&heap->usmblks); do { - uordblks = atomic_load(&heap->uordblks); + uordblks = atomic_read(&heap->uordblks); if (uordblks <= usmblks) { 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)) @@ -573,9 +573,9 @@ struct mallinfo mm_mallinfo(struct mm_heap_s *heap) struct mallinfo info; memset(&info, 0, sizeof(struct mallinfo)); - info.aordblks = atomic_load(&heap->aordblks); - info.uordblks = atomic_load(&heap->uordblks); - info.usmblks = atomic_load(&heap->usmblks); + info.aordblks = atomic_read(&heap->aordblks); + info.uordblks = atomic_read(&heap->uordblks); + info.usmblks = atomic_read(&heap->usmblks); return info; } diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c index f2d3c452fa354..10fd318c20d93 100644 --- a/drivers/i3c/master.c +++ b/drivers/i3c/master.c @@ -1180,8 +1180,8 @@ 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)) + atomic_fetch_sub_full(&dev->ibi->pending_ibis, 1); + if (!atomic_read(&dev->ibi->pending_ibis)) { sem_post(&dev->ibi->all_ibis_handled); } @@ -1800,7 +1800,7 @@ int i3c_master_add_i3c_dev_locked(FAR struct i3c_master_controller *master, void i3c_master_queue_ibi(FAR struct i3c_dev_desc *dev, FAR struct i3c_ibi_slot *slot) { - atomic_fetch_add(&dev->ibi->pending_ibis, 1); + atomic_fetch_add_full(&dev->ibi->pending_ibis, 1); work_queue(HPWORK, &slot->work, i3c_master_handle_ibi, slot, 0); } @@ -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/input/aw86225_internal.h b/drivers/input/aw86225_internal.h index 1dda28f179926..dd32251dfd96e 100644 --- a/drivers/input/aw86225_internal.h +++ b/drivers/input/aw86225_internal.h @@ -29,10 +29,10 @@ #include -#include #include #include +#include #include #include #include @@ -287,8 +287,8 @@ struct aw86225 unsigned char level; unsigned int osc_cali_run; unsigned char ram_vbat_comp; - atomic_int is_in_rtp_loop; - atomic_int exit_in_rtp_loop; + atomic_t is_in_rtp_loop; + atomic_t exit_in_rtp_loop; sem_t wait_q; sem_t stop_wait_q; }; diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c index cf5f65223ef87..2c0aaa4fa0e92 100644 --- a/drivers/net/netdev_upperhalf.c +++ b/drivers/net/netdev_upperhalf.c @@ -155,7 +155,7 @@ static FAR netpkt_t *netpkt_get(FAR struct net_driver_s *dev, * cases will be limited by netdev_upper_can_tx and seldom reaches here. */ - if (atomic_fetch_sub(&upper->lower->quota[type], 1) <= 0) + if (atomic_fetch_sub_full(&upper->lower->quota[type], 1) <= 0) { nwarn("WARNING: Allowing temperarily exceeding quota of %s.\n", dev->d_ifname); @@ -187,7 +187,7 @@ static void netpkt_put(FAR struct net_driver_s *dev, FAR netpkt_t *pkt, * but we don't want these changes. */ - atomic_fetch_add(&upper->lower->quota[type], 1); + atomic_fetch_add_full(&upper->lower->quota[type], 1); netdev_iob_release(dev); dev->d_iob = pkt; dev->d_len = netpkt_getdatalen(upper->lower, pkt); @@ -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]); } /**************************************************************************** @@ -1403,16 +1403,16 @@ FAR netpkt_t *netpkt_alloc(FAR struct netdev_lowerhalf_s *dev, { FAR netpkt_t *pkt; - if (atomic_fetch_sub(&dev->quota[type], 1) <= 0) + if (atomic_fetch_sub_full(&dev->quota[type], 1) <= 0) { - atomic_fetch_add(&dev->quota[type], 1); + atomic_fetch_add_full(&dev->quota[type], 1); return NULL; } pkt = iob_tryalloc(false); if (pkt == NULL) { - atomic_fetch_add(&dev->quota[type], 1); + atomic_fetch_add_full(&dev->quota[type], 1); return NULL; } @@ -1436,7 +1436,7 @@ FAR netpkt_t *netpkt_alloc(FAR struct netdev_lowerhalf_s *dev, void netpkt_free(FAR struct netdev_lowerhalf_s *dev, FAR netpkt_t *pkt, enum netpkt_type_e type) { - atomic_fetch_add(&dev->quota[type], 1); + atomic_fetch_add_full(&dev->quota[type], 1); iob_free_chain(pkt); } diff --git a/drivers/note/notesnap_driver.c b/drivers/note/notesnap_driver.c index 636fc37f69e4c..1c024b72668a0 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,14 +212,14 @@ 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; } /* Atomic operation, equivalent to snap.index++; */ - index = atomic_fetch_add(&snap->index, 1); + index = atomic_fetch_add_full(&snap->index, 1); note = &snap->buffer[index % CONFIG_DRIVERS_NOTESNAP_NBUFFERS]; note->type = type; @@ -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..30998dfdf008a 100644 --- a/drivers/reset/core.c +++ b/drivers/reset/core.c @@ -280,7 +280,7 @@ reset_control_get_internal(FAR struct reset_controller_dev *rcdev, return NULL; } - atomic_fetch_add(&rstc->refcnt, 1); + atomic_fetch_add_full(&rstc->refcnt, 1); return rstc; } } @@ -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; @@ -332,7 +332,7 @@ static void reset_control_put_internal(FAR struct reset_control *rstc) { DEBUGASSERT(nxmutex_is_locked(&g_reset_list_mutex)); - if (atomic_fetch_sub(&rstc->refcnt, 1) == 1) + if (atomic_fetch_sub_full(&rstc->refcnt, 1) == 1) { DEBUGASSERT(nxmutex_is_locked(&g_reset_list_mutex)); list_delete(&rstc->list); @@ -528,12 +528,12 @@ 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; } - if (atomic_fetch_add(&rstc->triggered_count, 1) != 0) + if (atomic_fetch_add_full(&rstc->triggered_count, 1) != 0) { return 0; } @@ -552,7 +552,7 @@ int reset_control_reset(FAR struct reset_control *rstc) if (rstc->shared && ret < 0) { - atomic_fetch_sub(&rstc->triggered_count, 1); + atomic_fetch_sub_full(&rstc->triggered_count, 1); } return ret; @@ -598,18 +598,18 @@ 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; } - if (atomic_fetch_sub(&rstc->deassert_count, 1) != 1) + if (atomic_fetch_sub_full(&rstc->deassert_count, 1) != 1) { return 0; } @@ -682,13 +682,13 @@ 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; } - if (atomic_fetch_add(&rstc->deassert_count, 1) != 0) + if (atomic_fetch_add_full(&rstc->deassert_count, 1) != 0) { return 0; } diff --git a/drivers/rpmsg/rpmsg_port.c b/drivers/rpmsg/rpmsg_port.c index 2d11d243a6722..5f8c54fed85b4 100644 --- a/drivers/rpmsg/rpmsg_port.c +++ b/drivers/rpmsg/rpmsg_port.c @@ -325,7 +325,7 @@ static void rpmsg_port_hold_rx_buffer(FAR struct rpmsg_device *rdev, { FAR struct rpmsg_hdr *rphdr = RPMSG_LOCATE_HDR(rxbuf); - atomic_fetch_add(&rphdr->reserved, 1 << RPMSG_BUF_HELD_SHIFT); + atomic_fetch_add_full(&rphdr->reserved, 1 << RPMSG_BUF_HELD_SHIFT); } /**************************************************************************** @@ -341,7 +341,7 @@ static void rpmsg_port_release_rx_buffer(FAR struct rpmsg_device *rdev, FAR struct rpmsg_port_header_s *hdr = metal_container_of(rphdr, struct rpmsg_port_header_s, buf); uint32_t reserved = - atomic_fetch_sub(&rphdr->reserved, 1 << RPMSG_BUF_HELD_SHIFT); + atomic_fetch_sub_full(&rphdr->reserved, 1 << RPMSG_BUF_HELD_SHIFT); if ((reserved & RPMSG_BUF_HELD_MASK) == (1 << RPMSG_BUF_HELD_SHIFT)) { 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..99da7356f4745 100644 --- a/drivers/rpmsg/rpmsg_port_spi.c +++ b/drivers/rpmsg/rpmsg_port_spi.c @@ -95,7 +95,7 @@ struct rpmsg_port_spi_s uint16_t rxavail; uint16_t rxthres; - atomic_int transferring; + atomic_t transferring; }; /**************************************************************************** @@ -197,7 +197,7 @@ static void rpmsg_port_spi_exchange(FAR struct rpmsg_port_spi_s *rpspi) FAR struct rpmsg_port_header_s *txhdr; IOEXP_WRITEPIN(rpspi->ioe, rpspi->mreq, 0); - if (atomic_fetch_add(&rpspi->transferring, 1)) + if (atomic_fetch_add_full(&rpspi->transferring, 1)) { return; } @@ -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..71680470f33c2 100644 --- a/drivers/rpmsg/rpmsg_port_spi_slave.c +++ b/drivers/rpmsg/rpmsg_port_spi_slave.c @@ -26,9 +26,9 @@ #include #include -#include #include +#include #include #include #include @@ -97,7 +97,7 @@ struct rpmsg_port_spi_s uint16_t rxavail; uint16_t rxthres; - atomic_int transferring; + atomic_t transferring; }; /**************************************************************************** @@ -171,7 +171,7 @@ static void rpmsg_port_spi_exchange(FAR struct rpmsg_port_spi_s *rpspi) { FAR struct rpmsg_port_header_s *txhdr; - if (atomic_fetch_add(&rpspi->transferring, 1)) + if (atomic_fetch_add_full(&rpspi->transferring, 1)) { return; } @@ -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..500f09ce2c466 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]; }; @@ -420,12 +420,12 @@ static int bt_bridge_open(FAR struct bt_driver_s *drv) FAR struct bt_bridge_s *bridge = device->bridge; FAR struct bt_driver_s *driver = bridge->driver; - if (atomic_fetch_add(&bridge->refs, 1) == 0) + if (atomic_fetch_add_full(&bridge->refs, 1) == 0) { int ret = driver->open(driver); if (ret < 0) { - atomic_fetch_sub(&bridge->refs, 1); + atomic_fetch_sub_full(&bridge->refs, 1); } return ret; @@ -514,7 +514,7 @@ static void bt_bridge_close(FAR struct bt_driver_s *drv) FAR struct bt_bridge_s *bridge = device->bridge; FAR struct bt_driver_s *driver = bridge->driver; - if (atomic_fetch_sub(&bridge->refs, 1) == 1) + if (atomic_fetch_sub_full(&bridge->refs, 1) == 1) { driver->close(driver); }; diff --git a/drivers/wireless/bluetooth/bt_slip.c b/drivers/wireless/bluetooth/bt_slip.c index b886a4b2949b8..cfbce689f1908 100644 --- a/drivers/wireless/bluetooth/bt_slip.c +++ b/drivers/wireless/bluetooth/bt_slip.c @@ -27,12 +27,12 @@ #include #include #include -#include #include #include #include #include +#include #include #include #include diff --git a/fs/event/event_close.c b/fs/event/event_close.c index 1749cf1eda558..3b1ea990df5ee 100644 --- a/fs/event/event_close.c +++ b/fs/event/event_close.c @@ -79,7 +79,7 @@ int nxevent_close(FAR nxevent_t *event) * now. */ - if (atomic_fetch_sub(&inode->i_crefs, 1) <= 1) + if (atomic_fetch_sub_full(&inode->i_crefs, 1) <= 1) { nxevent_destroy(&nevent->ne_event); group_free(NULL, nevent); diff --git a/fs/event/event_open.c b/fs/event/event_open.c index f0b2bf76c524d..4c4808da30e84 100644 --- a/fs/event/event_open.c +++ b/fs/event/event_open.c @@ -191,7 +191,7 @@ int nxevent_open(FAR nxevent_t **event, FAR const char *name, /* Initialize the inode */ INODE_SET_NAMEDEVENT(inode); - atomic_fetch_add(&inode->i_crefs, 1); + atomic_fetch_add_full(&inode->i_crefs, 1); /* Initialize the event groups */ diff --git a/fs/inode/fs_inodeaddref.c b/fs/inode/fs_inodeaddref.c index 240c0a07b7580..ff2c37fbb04c4 100644 --- a/fs/inode/fs_inodeaddref.c +++ b/fs/inode/fs_inodeaddref.c @@ -47,6 +47,6 @@ void inode_addref(FAR struct inode *inode) { if (inode) { - atomic_fetch_add(&inode->i_crefs, 1); + atomic_fetch_add_full(&inode->i_crefs, 1); } } diff --git a/fs/inode/fs_inodefind.c b/fs/inode/fs_inodefind.c index f53b502b4e2f6..7d4911ad4f3d2 100644 --- a/fs/inode/fs_inodefind.c +++ b/fs/inode/fs_inodefind.c @@ -68,7 +68,7 @@ int inode_find(FAR struct inode_search_s *desc) /* Increment the reference count on the inode */ - atomic_fetch_add(&inode->i_crefs, 1); + atomic_fetch_add_full(&inode->i_crefs, 1); } inode_runlock(); diff --git a/fs/inode/fs_inoderelease.c b/fs/inode/fs_inoderelease.c index 214fa98d59776..b5b1a5bc26313 100644 --- a/fs/inode/fs_inoderelease.c +++ b/fs/inode/fs_inoderelease.c @@ -53,7 +53,7 @@ void inode_release(FAR struct inode *inode) { /* Decrement the references of the inode */ - if (atomic_fetch_sub(&inode->i_crefs, 1) <= 1) + if (atomic_fetch_sub_full(&inode->i_crefs, 1) <= 1) { DEBUGASSERT(inode->i_peer == NULL); inode_free(inode); diff --git a/fs/inode/fs_inoderemove.c b/fs/inode/fs_inoderemove.c index 792eabe0a5d17..e376ac24a4b27 100644 --- a/fs/inode/fs_inoderemove.c +++ b/fs/inode/fs_inoderemove.c @@ -98,7 +98,7 @@ static FAR struct inode *inode_unlink(FAR const char *path) inode->i_peer = NULL; inode->i_parent = NULL; - atomic_fetch_sub(&inode->i_crefs, 1); + atomic_fetch_sub_full(&inode->i_crefs, 1); } RELEASE_SEARCH(&desc); @@ -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/mount/fs_mount.c b/fs/mount/fs_mount.c index 1d852bee725a5..900e7953e75ba 100644 --- a/fs/mount/fs_mount.c +++ b/fs/mount/fs_mount.c @@ -420,7 +420,7 @@ int nx_mount(FAR const char *source, FAR const char *target, if (drvr_inode != NULL) #endif { - atomic_fetch_add(&drvr_inode->i_crefs, 1); + atomic_fetch_add_full(&drvr_inode->i_crefs, 1); } #endif @@ -448,7 +448,7 @@ int nx_mount(FAR const char *source, FAR const char *target, if (drvr_inode != NULL) #endif { - atomic_fetch_sub(&drvr_inode->i_crefs, 1); + atomic_fetch_sub_full(&drvr_inode->i_crefs, 1); } #endif diff --git a/fs/mount/fs_umount2.c b/fs/mount/fs_umount2.c index 546f6ff98fb2c..0a8adc68f7f4c 100644 --- a/fs/mount/fs_umount2.c +++ b/fs/mount/fs_umount2.c @@ -142,7 +142,7 @@ int nx_umount2(FAR const char *target, unsigned int flags) { /* Just decrement the reference count (without deleting it) */ - atomic_fetch_sub(&mountpt_inode->i_crefs, 1); + atomic_fetch_sub_full(&mountpt_inode->i_crefs, 1); inode_unlock(); } else diff --git a/fs/mqueue/mq_open.c b/fs/mqueue/mq_open.c index d2e5cc79bc888..97f0b39aaf82f 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; @@ -319,7 +319,7 @@ static int file_mq_vopen(FAR struct file *mq, FAR const char *mq_name, /* Set the initial reference count on this inode to one */ - atomic_fetch_add(&inode->i_crefs, 1); + atomic_fetch_add_full(&inode->i_crefs, 1); if (created) { 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/semaphore/sem_close.c b/fs/semaphore/sem_close.c index 09bb257d00f36..8ca9a68aa657d 100644 --- a/fs/semaphore/sem_close.c +++ b/fs/semaphore/sem_close.c @@ -87,7 +87,7 @@ int nxsem_close(FAR sem_t *sem) * now. */ - if (atomic_fetch_sub(&inode->i_crefs, 1) <= 1) + if (atomic_fetch_sub_full(&inode->i_crefs, 1) <= 1) { nxsem_destroy(&nsem->ns_sem); group_free(NULL, nsem); diff --git a/fs/semaphore/sem_open.c b/fs/semaphore/sem_open.c index cacccb2fc21a7..9fe43ad2e81e9 100644 --- a/fs/semaphore/sem_open.c +++ b/fs/semaphore/sem_open.c @@ -208,7 +208,7 @@ int nxsem_open(FAR sem_t **sem, FAR const char *name, int oflags, ...) /* Initialize the inode */ INODE_SET_NAMEDSEM(inode); - atomic_fetch_add(&inode->i_crefs, 1); + atomic_fetch_add_full(&inode->i_crefs, 1); /* Initialize the semaphore */ diff --git a/fs/shm/shm_open.c b/fs/shm/shm_open.c index 1230585ee6bc6..89245658fcfd1 100644 --- a/fs/shm/shm_open.c +++ b/fs/shm/shm_open.c @@ -145,7 +145,7 @@ static int file_shm_open(FAR struct file *shm, FAR const char *name, INODE_SET_SHM(inode); inode->u.i_ops = &g_shmfs_operations; inode->i_private = NULL; - atomic_fetch_add(&inode->i_crefs, 1); + atomic_fetch_add_full(&inode->i_crefs, 1); } /* Associate the inode with a file structure */ 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_dir.c b/fs/vfs/fs_dir.c index 585df77e14dcf..eaaf05ed16404 100644 --- a/fs/vfs/fs_dir.c +++ b/fs/vfs/fs_dir.c @@ -220,7 +220,7 @@ static off_t seek_pseudodir(FAR struct file *filep, off_t offset) { /* Increment the reference count on this next node */ - atomic_fetch_add(&curr->i_crefs, 1); + atomic_fetch_add_full(&curr->i_crefs, 1); } inode_unlock(); @@ -387,7 +387,7 @@ static int read_pseudodir(FAR struct fs_dirent_s *dir, { /* Increment the reference count on this next node */ - atomic_fetch_add(&pdir->next->i_crefs, 1); + atomic_fetch_add_full(&pdir->next->i_crefs, 1); } inode_unlock(); 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..aa57b69de1eb7 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_full(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_full(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_full(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_full(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_full(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/i3c/master.h b/include/nuttx/i3c/master.h index fc78722ea76af..cbc40db4a25e5 100644 --- a/include/nuttx/i3c/master.h +++ b/include/nuttx/i3c/master.h @@ -28,8 +28,8 @@ ****************************************************************************/ #include -#include +#include #include #include #include @@ -231,7 +231,7 @@ struct i3c_ibi_slot struct i3c_device_ibi_info { sem_t all_ibis_handled; - atomic_int pending_ibis; + atomic_t pending_ibis; unsigned int max_payload_len; unsigned int num_slots; unsigned int enabled; 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..198e88fba5be6 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_full(&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_full(&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_full(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/addrenv/addrenv.c b/sched/addrenv/addrenv.c index a3a0d93ad4009..3c1070a8c7529 100644 --- a/sched/addrenv/addrenv.c +++ b/sched/addrenv/addrenv.c @@ -401,7 +401,7 @@ int addrenv_restore(FAR struct addrenv_s *addrenv) void addrenv_take(FAR struct addrenv_s *addrenv) { - atomic_fetch_add(&addrenv->refs, 1); + atomic_fetch_add_full(&addrenv->refs, 1); } /**************************************************************************** @@ -421,7 +421,7 @@ void addrenv_take(FAR struct addrenv_s *addrenv) int addrenv_give(FAR struct addrenv_s *addrenv) { - return atomic_fetch_sub(&addrenv->refs, 1); + return atomic_fetch_sub_full(&addrenv->refs, 1); } /**************************************************************************** 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..195be178150ea 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_full(&sem->semcount, 1); } } diff --git a/sched/semaphore/sem_post.c b/sched/semaphore/sem_post.c index 500b00f5954ce..951f1bd76b866 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_full(&sem->semcount, 1); /* Check the maximum allowable value */ if (sem_count >= SEM_VALUE_MAX) { - atomic_fetch_sub(NXSEM_COUNT(sem), 1); + atomic_fetch_sub_full(&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..b36b5aa8f0e61 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_full(&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..bfcd9e0d630ab 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_full(&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..0e34d0454b090 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_full(&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..2b99e7ee41e24 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_full(&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 ****************************************************************************/ From 91a989b1ebeb2a98634afe83434cd7e11294c1ad Mon Sep 17 00:00:00 2001 From: zhangyuan29 Date: Wed, 20 Nov 2024 15:18:54 +0800 Subject: [PATCH 2/2] lpc17xx_40xx: enable CONFIG_DISABLE_MQUEUE_SYSV Because SysV is not used, disable SysV to save RAM space. Signed-off-by: zhangyuan29 --- boards/arm/lpc17xx_40xx/open1788/configs/knxterm/defconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/boards/arm/lpc17xx_40xx/open1788/configs/knxterm/defconfig b/boards/arm/lpc17xx_40xx/open1788/configs/knxterm/defconfig index 9b73fe9e04189..5f7bf6f0952bc 100644 --- a/boards/arm/lpc17xx_40xx/open1788/configs/knxterm/defconfig +++ b/boards/arm/lpc17xx_40xx/open1788/configs/knxterm/defconfig @@ -19,6 +19,7 @@ CONFIG_BOARD_LATE_INITIALIZE=y CONFIG_BOARD_LOOPSPERMSEC=11934 CONFIG_BUILD_PROTECTED=y CONFIG_DEV_LOOP=y +CONFIG_DISABLE_MQUEUE_SYSV=y CONFIG_EXAMPLES_NXTERM=y CONFIG_EXAMPLES_NXTERM_FONTID=1 CONFIG_FAT_LCNAMES=y