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 20, 2024
1 parent edc410f commit 35500e0
Show file tree
Hide file tree
Showing 55 changed files with 524 additions and 620 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
38 changes: 19 additions & 19 deletions arch/sim/src/sim/sim_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
}

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

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
6 changes: 3 additions & 3 deletions drivers/input/aw86225_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

#include <nuttx/config.h>

#include <stdatomic.h>
#include <stdio.h>
#include <time.h>

#include <nuttx/atomic.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/input/ff.h>
#include <nuttx/wqueue.h>
Expand Down Expand Up @@ -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;
};
Expand Down
16 changes: 8 additions & 8 deletions drivers/net/netdev_upperhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down 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 All @@ -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;
}

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

Expand Down
12 changes: 6 additions & 6 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,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;
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
Loading

0 comments on commit 35500e0

Please sign in to comment.