From de82824fbf19e32200adee3d515e00d0bd4c1a68 Mon Sep 17 00:00:00 2001 From: yangguangcai Date: Tue, 22 Aug 2023 11:16:38 +0800 Subject: [PATCH] add test atomic Signed-off-by: yangguangcai --- testing/atomic/Kconfig | 29 ++++++++++ testing/atomic/Make.defs | 23 ++++++++ testing/atomic/Makefile | 34 ++++++++++++ testing/atomic/atomic_main.c | 102 +++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 testing/atomic/Kconfig create mode 100644 testing/atomic/Make.defs create mode 100644 testing/atomic/Makefile create mode 100644 testing/atomic/atomic_main.c diff --git a/testing/atomic/Kconfig b/testing/atomic/Kconfig new file mode 100644 index 000000000..3d0546219 --- /dev/null +++ b/testing/atomic/Kconfig @@ -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 diff --git a/testing/atomic/Make.defs b/testing/atomic/Make.defs new file mode 100644 index 000000000..d9d848810 --- /dev/null +++ b/testing/atomic/Make.defs @@ -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 diff --git a/testing/atomic/Makefile b/testing/atomic/Makefile new file mode 100644 index 000000000..8d904c810 --- /dev/null +++ b/testing/atomic/Makefile @@ -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 diff --git a/testing/atomic/atomic_main.c b/testing/atomic/atomic_main.c new file mode 100644 index 000000000..90314190d --- /dev/null +++ b/testing/atomic/atomic_main.c @@ -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 +#include +#include + +/**************************************************************************** + * 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; +}