Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions testing/atomic/Kconfig
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions testing/atomic/Make.defs
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions testing/atomic/Makefile
Original file line number Diff line number Diff line change
@@ -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
102 changes: 102 additions & 0 deletions testing/atomic/atomic_main.c
Original file line number Diff line number Diff line change
@@ -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;
}