Skip to content

Commit

Permalink
newlib: provide missing atomic libcalls
Browse files Browse the repository at this point in the history
Provide emulated atomic load & store libcalls for u8, u16 & u32 integer
types. This is required when building with Clang as llvm does not lower
these operations to native load / stores, where as gcc does.

Provide `sync_lock_test_and_set` atomic implementations for all
supported integer types.

Closes #7591.
Closes #7592.
  • Loading branch information
MabezDev authored and igrr committed Oct 8, 2021
1 parent 76a4410 commit d93b53b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
64 changes: 51 additions & 13 deletions components/newlib/stdatomic.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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.
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

//replacement for gcc built-in functions

Expand Down Expand Up @@ -208,6 +200,24 @@ CLANG_DECLARE_ALIAS( __sync_bool_compare_and_swap_ ## n )
} \
CLANG_DECLARE_ALIAS( __sync_val_compare_and_swap_ ## n )

#define SYNC_LOCK_TEST_AND_SET(n, type) type CLANG_ATOMIC_SUFFIX(__sync_lock_test_and_set_ ## n) (type *ptr, type val, ...) \
{ \
unsigned state = _ATOMIC_ENTER_CRITICAL(); \
type ret = *ptr; \
*ptr = val; \
_ATOMIC_EXIT_CRITICAL(state); \
return ret; \
}
CLANG_DECLARE_ALIAS( __sync_lock_test_and_set_ ## n )

#define SYNC_LOCK_RELEASE(n, type) void CLANG_ATOMIC_SUFFIX(__sync_lock_release_ ## n) (type *ptr, ...) \
{ \
unsigned state = _ATOMIC_ENTER_CRITICAL(); \
*ptr = 0; \
_ATOMIC_EXIT_CRITICAL(state); \
}
CLANG_DECLARE_ALIAS( __sync_lock_release_ ## n )


#if !HAS_ATOMICS_32

Expand Down Expand Up @@ -267,6 +277,27 @@ SYNC_VAL_CMP_EXCHANGE(1, uint8_t)
SYNC_VAL_CMP_EXCHANGE(2, uint16_t)
SYNC_VAL_CMP_EXCHANGE(4, uint32_t)

#ifdef __clang__

// LLVM has not implemented native atomic load/stores for riscv targets without the Atomic extension
// therfore we provide libcalls here when building with the clang toolchain. LLVM thread: https://reviews.llvm.org/D47553.
ATOMIC_LOAD(1, uint8_t)
ATOMIC_LOAD(2, uint16_t)
ATOMIC_LOAD(4, uint32_t)
ATOMIC_STORE(1, uint8_t)
ATOMIC_STORE(2, uint16_t)
ATOMIC_STORE(4, uint32_t)

SYNC_LOCK_TEST_AND_SET(1, uint8_t)
SYNC_LOCK_TEST_AND_SET(2, uint16_t)
SYNC_LOCK_TEST_AND_SET(4, uint32_t)

SYNC_LOCK_RELEASE(1, uint8_t)
SYNC_LOCK_RELEASE(2, uint16_t)
SYNC_LOCK_RELEASE(4, uint32_t)

#endif

#endif // !HAS_ATOMICS_32

#if !HAS_ATOMICS_64
Expand Down Expand Up @@ -303,4 +334,11 @@ SYNC_BOOL_CMP_EXCHANGE(8, uint64_t)

SYNC_VAL_CMP_EXCHANGE(8, uint64_t)

#ifdef __clang__

SYNC_LOCK_TEST_AND_SET(8, uint64_t)
SYNC_LOCK_RELEASE(8, uint64_t)

#endif

#endif // !HAS_ATOMICS_64
1 change: 0 additions & 1 deletion tools/ci/check_copyright_ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,6 @@ components/newlib/priv_include/esp_time_impl.h
components/newlib/pthread.c
components/newlib/random.c
components/newlib/reent_init.c
components/newlib/stdatomic.c
components/newlib/syscalls.c
components/newlib/termios.c
components/newlib/test/test_atomic.c
Expand Down

0 comments on commit d93b53b

Please sign in to comment.