netdev/upper: Use atomic when we have atomic support from compiler

Ref: How to test atomic support https://beej.us/guide/bgc/html/split/chapter-atomics.html

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2023-06-14 16:29:15 +08:00 committed by Alan Carvalho de Assis
parent 483eb10f37
commit f80539b6a8
2 changed files with 37 additions and 1 deletions

View File

@ -91,19 +91,27 @@ struct netdev_upperhalf_s
static int quota_fetch_inc(FAR struct netdev_lowerhalf_s *lower,
enum netpkt_type_e type)
{
#ifndef HAVE_ATOMIC
irqstate_t flags = spin_lock_irqsave(NULL);
int ret = lower->quota[type]++;
spin_unlock_irqrestore(NULL, flags);
return ret;
#else
return atomic_fetch_add(&lower->quota[type], 1);
#endif
}
static int quota_fetch_dec(FAR struct netdev_lowerhalf_s *lower,
enum netpkt_type_e type)
{
#ifndef HAVE_ATOMIC
irqstate_t flags = spin_lock_irqsave(NULL);
int ret = lower->quota[type]--;
spin_unlock_irqrestore(NULL, flags);
return ret;
#else
return atomic_fetch_sub(&lower->quota[type], 1);
#endif
}
/****************************************************************************
@ -838,10 +846,14 @@ 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)
{
#ifndef HAVE_ATOMIC
irqstate_t flags = spin_lock_irqsave(NULL);
int ret = dev->quota[type];
spin_unlock_irqrestore(NULL, flags);
return ret;
#else
return atomic_load(&dev->quota[type]);
#endif
}
/****************************************************************************

View File

@ -32,6 +32,17 @@
#include <net/if.h>
#include <netinet/in.h>
/* Compiler may not have stdatomic.h before C11 or with __STDC_NO_ATOMICS__
* But we can also get atomic support from libmetal.
*/
#if defined(CONFIG_OPENAMP)
#include <metal/atomic.h>
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
!defined(__STDC_NO_ATOMICS__)
#include <stdatomic.h>
#endif
#include <nuttx/net/ip.h>
#include <nuttx/net/net.h>
#include <nuttx/net/netdev.h>
@ -50,6 +61,12 @@
#define NETPKT_BUFLEN CONFIG_IOB_BUFSIZE
#if defined(CONFIG_OPENAMP) || \
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
!defined(__STDC_NO_ATOMICS__))
#define HAVE_ATOMIC
#endif
/****************************************************************************
* Public Types
****************************************************************************/
@ -84,7 +101,14 @@ struct netdev_ops_s;
struct netdev_lowerhalf_s
{
FAR const struct netdev_ops_s *ops;
int quota[NETPKT_TYPENUM]; /* Max # of buffer held by driver */
/* Max # of buffer held by driver */
#ifdef HAVE_ATOMIC
atomic_int quota[NETPKT_TYPENUM];
#else
int quota[NETPKT_TYPENUM];
#endif
/* The structure used by net stack.
* Note: Do not change its fields unless you know what you are doing.