Skip to content

Commit

Permalink
arch_atomic: support nx atomic function
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
zyfeier committed Nov 19, 2024
1 parent a88652f commit 962c8c1
Show file tree
Hide file tree
Showing 39 changed files with 459 additions and 555 deletions.
2 changes: 1 addition & 1 deletion arch/arm/src/cxd56xx/cxd56_sph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions arch/arm/src/cxd56xx/cxd56_uart0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions arch/sim/src/sim/sim_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void *mm_realloc(struct mm_heap_s *heap, void *oldmem,
break;
}
}
while (atomic_compare_exchange_weak(&heap->usmblks, &usmblks, uordblks));
while (atomic_try_cmpxchg(&heap->usmblks, &usmblks, uordblks));

#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
if (mem == NULL && free_delaylist(heap, true))
Expand Down Expand Up @@ -498,7 +498,7 @@ void *mm_memalign(struct mm_heap_s *heap, size_t alignment, size_t size)
break;
}
}
while (atomic_compare_exchange_weak(&heap->usmblks, &usmblks, uordblks));
while (atomic_try_cmpxchg(&heap->usmblks, &usmblks, uordblks));

#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
if (mem == NULL && free_delaylist(heap, true))
Expand Down
6 changes: 3 additions & 3 deletions drivers/i3c/master.c
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ static void i3c_master_handle_ibi(FAR void *arg)

master->ops->recycle_ibi_slot(dev, slot);
atomic_fetch_sub(&dev->ibi->pending_ibis, 1);
if (!atomic_load(&dev->ibi->pending_ibis))
if (!atomic_read(&dev->ibi->pending_ibis))
{
sem_post(&dev->ibi->all_ibis_handled);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions drivers/input/aw86225.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -1121,24 +1121,24 @@ 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__);

ret = nxsem_wait(&aw86225->wait_q);

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__);
return;
}
}

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);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions drivers/net/netdev_upperhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
}

/****************************************************************************
Expand Down
10 changes: 5 additions & 5 deletions drivers/note/notesnap_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

Expand Down Expand Up @@ -212,7 +212,7 @@ static inline void notesnap_common(FAR struct note_driver_s *drv,
FAR struct notesnap_chunk_s *note;
size_t index;

if (atomic_load(&snap->dumping))
if (atomic_read(&snap->dumping))
{
return;
}
Expand Down Expand Up @@ -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++)
{
Expand All @@ -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);
}

/****************************************************************************
Expand Down
10 changes: 5 additions & 5 deletions drivers/reset/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -528,7 +528,7 @@ int reset_control_reset(FAR struct reset_control *rstc)

if (rstc->shared)
{
if (atomic_load(&rstc->deassert_count) != 0)
if (atomic_read(&rstc->deassert_count) != 0)
{
return -EINVAL;
}
Expand Down Expand Up @@ -598,12 +598,12 @@ int reset_control_assert(FAR struct reset_control *rstc)

if (rstc->shared)
{
if (atomic_load(&rstc->triggered_count) != 0)
if (atomic_read(&rstc->triggered_count) != 0)
{
return -EINVAL;
}

if (atomic_load(&rstc->deassert_count) == 0)
if (atomic_read(&rstc->deassert_count) == 0)
{
rsterr("deassert_count = 0, invalid value\n");
return -EINVAL;
Expand Down Expand Up @@ -682,7 +682,7 @@ int reset_control_deassert(FAR struct reset_control *rstc)

if (rstc->shared)
{
if (atomic_load(&rstc->triggered_count) != 0)
if (atomic_read(&rstc->triggered_count) != 0)
{
rsterr("triggered_count != 0, invalid value\n");
return -EINVAL;
Expand Down
6 changes: 3 additions & 3 deletions drivers/rpmsg/rpmsg_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include <stdbool.h>

#include <metal/atomic.h>
#include <nuttx/atomic.h>

#include <nuttx/list.h>
#include <nuttx/spinlock.h>
Expand Down Expand Up @@ -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);
}

/****************************************************************************
Expand All @@ -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);
}

/****************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion drivers/rpmsg/rpmsg_port_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/rpmsg/rpmsg_port_spi_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion drivers/serial/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -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? */

Expand Down
32 changes: 16 additions & 16 deletions drivers/serial/uart_ram.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/****************************************************************************
Expand All @@ -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);
}

/****************************************************************************
Expand All @@ -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);
}

/****************************************************************************
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion drivers/wireless/bluetooth/bt_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

Expand Down
2 changes: 1 addition & 1 deletion fs/inode/fs_inoderemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 962c8c1

Please sign in to comment.