-
Notifications
You must be signed in to change notification settings - Fork 1.7k
libc/atomic: refactor inline arch atomics, add atomic_ptr_t/atomic64/cmpxchg_release_acquire #20043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhangyu-duck
wants to merge
6
commits into
apache:master
Choose a base branch
from
zhangyu-duck:atomic_part2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6319a56
include/atomic: add atomic_ptr_t type and operations
CV-Bowen 1311d12
libc/atomic: support inline arch atomic for tricore
CV-Bowen ec557ba
libc/atomic: support inline arch atomic for generic IRQ path
CV-Bowen f1f46f0
libc/atomic: merge tricore arch_atomic.c into machine/arch_atomic.c
CV-Bowen 5c36cc3
include/atomic: add cmpxchg_release_acquire memory order variant
CV-Bowen 2802f3b
libc: realize atomic64 via a spinlock helper
zhangyu-duck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /**************************************************************************** | ||
| * arch/tricore/include/atomic.h | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * 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. | ||
| * | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef __ARCH_TRICORE_INCLUDE_ATOMIC_H | ||
| #define __ARCH_TRICORE_INCLUDE_ATOMIC_H | ||
|
|
||
| /**************************************************************************** | ||
| * Included Files | ||
| ****************************************************************************/ | ||
|
|
||
| #include <nuttx/config.h> | ||
| #include <nuttx/compiler.h> | ||
|
|
||
| /**************************************************************************** | ||
| * Pre-processor Definitions | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef ARCH_ATOMIC_SPECIFIER | ||
| # define ARCH_ATOMIC_SPECIFIER static always_inline_function | ||
| #endif | ||
|
|
||
| #define ARCH_ATOMIC_STORE(func, t) \ | ||
| ARCH_ATOMIC_SPECIFIER \ | ||
| void func(volatile void *ptr, t value, int memorder) \ | ||
| { \ | ||
| tricore_atomic_swap(ptr, value); \ | ||
| } | ||
|
|
||
| #define ARCH_ATOMIC_LOAD(func, t) \ | ||
| ARCH_ATOMIC_SPECIFIER \ | ||
| t func(const volatile void *ptr, int memorder) \ | ||
| { \ | ||
| return *(volatile t *)ptr; \ | ||
| } | ||
|
|
||
| #define ARCH_ATOMIC_EXCHANGE(func, t) \ | ||
| ARCH_ATOMIC_SPECIFIER \ | ||
| t func(volatile void *ptr, t value, int memorder) \ | ||
| { \ | ||
| return tricore_atomic_swap(ptr, value); \ | ||
| } | ||
|
|
||
| #define ARCH_ATOMIC_COMPARE_EXCHANGE(func, t) \ | ||
| ARCH_ATOMIC_SPECIFIER \ | ||
| bool func(volatile void *ptr, volatile void *expect, \ | ||
| t desired, bool weak, int success, int failure) \ | ||
| { \ | ||
| t old; \ | ||
| \ | ||
| old = tricore_atomic_cmpswap(ptr, desired, *(t *)expect); \ | ||
| if (old == *(t *)expect) \ | ||
| { \ | ||
| return true; \ | ||
| } \ | ||
| \ | ||
| *(t *)expect = old; \ | ||
| \ | ||
| return false; \ | ||
| } | ||
|
|
||
| #define ARCH_ATOMIC_FLAGS_TEST_AND_SET(func, t) \ | ||
| ARCH_ATOMIC_SPECIFIER \ | ||
| t func(volatile void *ptr, int memorder) \ | ||
| { \ | ||
| return tricore_atomic_swap(ptr, 1); \ | ||
| } | ||
|
|
||
| #define ARCH_ATOMIC_FETCH_OP(func, t, n, op) \ | ||
| ARCH_ATOMIC_SPECIFIER \ | ||
| t func(volatile void *ptr, t value, int memorder) \ | ||
| { \ | ||
| t old_val; \ | ||
| \ | ||
| do \ | ||
| { \ | ||
| old_val = atomic_load_ ## n(ptr, memorder); \ | ||
| } \ | ||
| while (tricore_atomic_cmpswap(ptr, old_val op value, old_val) \ | ||
| != old_val); \ | ||
| \ | ||
| return old_val; \ | ||
| } | ||
|
|
||
| #define ARCH_ATOMIC_DEFINE(prefix, t, n) \ | ||
| ARCH_ATOMIC_STORE(prefix ## _store_ ## n, t) \ | ||
| ARCH_ATOMIC_LOAD(prefix ## _load_ ## n, t) \ | ||
| ARCH_ATOMIC_EXCHANGE(prefix ## _exchange_ ## n, t) \ | ||
| ARCH_ATOMIC_COMPARE_EXCHANGE(prefix ## _compare_exchange_ ## n, t) \ | ||
| ARCH_ATOMIC_FLAGS_TEST_AND_SET(prefix ## _flags_test_and_set_ ## n, t) \ | ||
| ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_add_ ## n, t, n, +) \ | ||
| ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_sub_ ## n, t, n, -) \ | ||
| ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_and_ ## n, t, n, &) \ | ||
| ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_or_ ## n, t, n, |) \ | ||
| ARCH_ATOMIC_FETCH_OP(prefix ## _fetch_xor_ ## n, t, n, ^) | ||
|
|
||
| #define ARCH_HAVE_ATOMIC_4 | ||
|
|
||
| /**************************************************************************** | ||
| * Inline Functions | ||
| ****************************************************************************/ | ||
|
|
||
| /**************************************************************************** | ||
| * Name: tricore_atomic_swap | ||
| ****************************************************************************/ | ||
|
|
||
| always_inline_function | ||
| static uint32_t tricore_atomic_swap(volatile void *addr, uint32_t value) | ||
| { | ||
| uint32_t res; | ||
|
|
||
| __asm__ volatile ("swap.w [%1]0, %2" | ||
| : "=d"(res) : "a"(addr), "0"(value)); | ||
| return res; | ||
| } | ||
|
|
||
| /**************************************************************************** | ||
| * Name: tricore_atomic_cmpswap | ||
| ****************************************************************************/ | ||
|
|
||
| always_inline_function | ||
| static uint32_t tricore_atomic_cmpswap(volatile void *addr, uint32_t value, | ||
| uint32_t condition) | ||
| { | ||
| uint64_t reg64 = value | ((uint64_t)condition << 32); | ||
|
|
||
| __asm__ __volatile__ ("cmpswap.w [%1]0, %A0" | ||
| : "+d" (reg64) | ||
| : "a" (addr) | ||
| : "memory"); | ||
| return (uint32_t)reg64; | ||
| } | ||
|
|
||
| ARCH_ATOMIC_DEFINE(atomic, int32_t, 4) | ||
|
|
||
| #endif /* __ARCH_TRICORE_INCLUDE_ATOMIC_H */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why add here