leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2015-2017 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include <stddef.h>
leothedragon 0:8f0bb79ddd48 20 #include "atomic.h"
leothedragon 0:8f0bb79ddd48 21 #include "aq_critical.h"
leothedragon 0:8f0bb79ddd48 22
leothedragon 0:8f0bb79ddd48 23 #if defined(TARGET_LIKE_MBED)
leothedragon 0:8f0bb79ddd48 24 #include "cmsis.h"
leothedragon 0:8f0bb79ddd48 25 #endif
leothedragon 0:8f0bb79ddd48 26
leothedragon 0:8f0bb79ddd48 27 #if !defined(__CORTEX_M) || (__CORTEX_M < 0x03)
leothedragon 0:8f0bb79ddd48 28
leothedragon 0:8f0bb79ddd48 29 int aq_atomic_cas_deref_uintptr(uintptr_t *volatile *ptrAddr,
leothedragon 0:8f0bb79ddd48 30 uintptr_t **currentPtrValue,
leothedragon 0:8f0bb79ddd48 31 uintptr_t expectedDerefValue,
leothedragon 0:8f0bb79ddd48 32 uintptr_t *newPtrValue,
leothedragon 0:8f0bb79ddd48 33 uintptr_t valueOffset)
leothedragon 0:8f0bb79ddd48 34 {
leothedragon 0:8f0bb79ddd48 35 int rc;
leothedragon 0:8f0bb79ddd48 36 aq_critical_section_enter();
leothedragon 0:8f0bb79ddd48 37 uintptr_t *current = *ptrAddr;
leothedragon 0:8f0bb79ddd48 38 if (currentPtrValue != NULL) {
leothedragon 0:8f0bb79ddd48 39 *currentPtrValue = current;
leothedragon 0:8f0bb79ddd48 40 }
leothedragon 0:8f0bb79ddd48 41 if (current == NULL) {
leothedragon 0:8f0bb79ddd48 42 rc = AQ_ATOMIC_CAS_DEREF_NULLPTR;
leothedragon 0:8f0bb79ddd48 43 } else if (*(uintptr_t *)((uintptr_t)current + valueOffset) != expectedDerefValue) {
leothedragon 0:8f0bb79ddd48 44 rc = AQ_ATOMIC_CAS_DEREF_VALUE;
leothedragon 0:8f0bb79ddd48 45 } else {
leothedragon 0:8f0bb79ddd48 46 *ptrAddr = newPtrValue;
leothedragon 0:8f0bb79ddd48 47 rc = AQ_ATOMIC_CAS_DEREF_SUCCESS;
leothedragon 0:8f0bb79ddd48 48 }
leothedragon 0:8f0bb79ddd48 49 aq_critical_section_exit();
leothedragon 0:8f0bb79ddd48 50 return rc;
leothedragon 0:8f0bb79ddd48 51 }
leothedragon 0:8f0bb79ddd48 52 #endif
leothedragon 0:8f0bb79ddd48 53
leothedragon 0:8f0bb79ddd48 54
leothedragon 0:8f0bb79ddd48 55 #if !defined(__SXOS__) && defined(__GNUC__) && (!defined(__CORTEX_M) || (__CORTEX_M >= 0x03))
leothedragon 0:8f0bb79ddd48 56 int aq_atomic_cas_uintptr(uintptr_t *ptr, uintptr_t oldval, uintptr_t newval)
leothedragon 0:8f0bb79ddd48 57 {
leothedragon 0:8f0bb79ddd48 58 return __sync_bool_compare_and_swap(ptr, oldval, newval);
leothedragon 0:8f0bb79ddd48 59 }
leothedragon 0:8f0bb79ddd48 60
leothedragon 0:8f0bb79ddd48 61 int32_t aq_atomic_inc_int32(int32_t *ptr, int32_t inc)
leothedragon 0:8f0bb79ddd48 62 {
leothedragon 0:8f0bb79ddd48 63 return __sync_add_and_fetch(ptr, inc);
leothedragon 0:8f0bb79ddd48 64 }
leothedragon 0:8f0bb79ddd48 65 #else
leothedragon 0:8f0bb79ddd48 66 int aq_atomic_cas_uintptr(uintptr_t *ptr, uintptr_t oldval, uintptr_t newval)
leothedragon 0:8f0bb79ddd48 67 {
leothedragon 0:8f0bb79ddd48 68 int rc;
leothedragon 0:8f0bb79ddd48 69 aq_critical_section_enter();
leothedragon 0:8f0bb79ddd48 70 if (*ptr == oldval) {
leothedragon 0:8f0bb79ddd48 71 rc = 1;
leothedragon 0:8f0bb79ddd48 72 *ptr = newval;
leothedragon 0:8f0bb79ddd48 73 } else {
leothedragon 0:8f0bb79ddd48 74 rc = 0;
leothedragon 0:8f0bb79ddd48 75 }
leothedragon 0:8f0bb79ddd48 76 aq_critical_section_exit();
leothedragon 0:8f0bb79ddd48 77 return rc;
leothedragon 0:8f0bb79ddd48 78 }
leothedragon 0:8f0bb79ddd48 79 int32_t aq_atomic_inc_int32(int32_t *ptr, int32_t inc)
leothedragon 0:8f0bb79ddd48 80 {
leothedragon 0:8f0bb79ddd48 81 int32_t ret;
leothedragon 0:8f0bb79ddd48 82 aq_critical_section_enter();
leothedragon 0:8f0bb79ddd48 83 ret = *ptr + inc;
leothedragon 0:8f0bb79ddd48 84 *ptr = ret;
leothedragon 0:8f0bb79ddd48 85 aq_critical_section_exit();
leothedragon 0:8f0bb79ddd48 86 return ret;
leothedragon 0:8f0bb79ddd48 87 }
leothedragon 0:8f0bb79ddd48 88 #endif