add test atomic

Signed-off-by: yangguangcai <yangguangcai@xiaomi.com>
This commit is contained in:
yangguangcai 2023-08-22 11:16:38 +08:00 committed by Xiang Xiao
parent 14d06ea011
commit de82824fbf
4 changed files with 188 additions and 0 deletions

29
testing/atomic/Kconfig Normal file
View File

@ -0,0 +1,29 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config TESTING_ATOMIC
tristate "\"Test atomic\" testing"
default n
---help---
Enable the \"Test atomic!\" testing.
if TESTING_ATOMIC
config TESTING_ATOMIC_PROGNAME
string "Program name"
default "atomic"
---help---
This is the name of the program that will be used when the NSH ELF
program is atomic.
config TESTING_ATOMIC_PRIORITY
int "Atomic task priority"
default 100
config TESTING_ATOMIC_STACKSIZE
int "Atomic stack size"
default DEFAULT_TASK_STACKSIZE
endif

23
testing/atomic/Make.defs Normal file
View File

@ -0,0 +1,23 @@
############################################################################
# apps/testing/atomic/Make.defs
#
# 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.
#
############################################################################
ifneq ($(CONFIG_TESTING_ATOMIC),)
CONFIGURED_APPS += $(APPDIR)/testing/atomic
endif

34
testing/atomic/Makefile Normal file
View File

@ -0,0 +1,34 @@
############################################################################
# apps/testing/atomic/Make.defs
#
# 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.
#
############################################################################
include $(APPDIR)/Make.defs
# Atomic ! built-in application info
PROGNAME = $(CONFIG_TESTING_ATOMIC_PROGNAME)
PRIORITY = $(CONFIG_TESTING_ATOMIC_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_ATOMIC_STACKSIZE)
MODULE = $(CONFIG_TESTING_ATOMIC)
# Atomic! Example
MAINSRC = atomic_main.c
include $(APPDIR)/Application.mk

View File

@ -0,0 +1,102 @@
/****************************************************************************
* apps/testing/atomic/atomic_main.c
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdatomic.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define ATOMIC_CHECK(value, expected) \
if ((value) != (expected)) \
{ \
printf("atomic test fail,line:%d\n",__LINE__); \
}
#define ATOMIC_TEST(type, init) \
{ \
atomic_##type object = init; \
atomic_##type expected = 2; \
\
atomic_##type old_value = atomic_fetch_add(&object, 1); \
ATOMIC_CHECK(old_value, 1); \
ATOMIC_CHECK(object, 2); \
\
atomic_store(&object, 1); \
ATOMIC_CHECK(object, 1); \
\
old_value = atomic_load(&object); \
ATOMIC_CHECK(object, 1) \
\
old_value = atomic_fetch_or(&object, 4); \
ATOMIC_CHECK(old_value, 1); \
ATOMIC_CHECK(object, 5); \
\
old_value = atomic_fetch_xor(&object, 7); \
ATOMIC_CHECK(old_value, 5); \
ATOMIC_CHECK(object, 2); \
\
old_value = atomic_fetch_and(&object, 3); \
ATOMIC_CHECK(old_value, 2); \
ATOMIC_CHECK(object, 2); \
\
old_value = atomic_exchange(&object, 5); \
ATOMIC_CHECK(old_value, 2); \
ATOMIC_CHECK(object, 5); \
\
old_value = atomic_fetch_sub(&object, 3); \
ATOMIC_CHECK(old_value, 5); \
ATOMIC_CHECK(object, 2); \
\
atomic_compare_exchange_weak(&object, &expected, 5); \
ATOMIC_CHECK(object, 5); \
\
expected = 5; \
atomic_compare_exchange_strong(&object, &expected, 2); \
ATOMIC_CHECK(object, 2); \
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* atomic_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
ATOMIC_TEST(int, 1);
ATOMIC_TEST(uint, 1U);
ATOMIC_TEST(long, 1L);
ATOMIC_TEST(ulong, 1UL);
ATOMIC_TEST(short, 1);
ATOMIC_TEST(ushort, 1);
ATOMIC_TEST(char, 1);
printf("atomic test complete!\n");
return 0;
}