Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers atomic.c Source File

atomic.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2015-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include <stddef.h>
00020 #include "atomic.h"
00021 #include "aq_critical.h"
00022 
00023 #if defined(TARGET_LIKE_MBED)
00024 #include "cmsis.h"
00025 #endif
00026 
00027 #if !defined(__CORTEX_M) || (__CORTEX_M < 0x03)
00028 
00029 int aq_atomic_cas_deref_uintptr(uintptr_t *volatile *ptrAddr,
00030                                 uintptr_t **currentPtrValue,
00031                                 uintptr_t expectedDerefValue,
00032                                 uintptr_t *newPtrValue,
00033                                 uintptr_t valueOffset)
00034 {
00035     int rc;
00036     aq_critical_section_enter();
00037     uintptr_t *current = *ptrAddr;
00038     if (currentPtrValue != NULL) {
00039         *currentPtrValue = current;
00040     }
00041     if (current == NULL) {
00042         rc = AQ_ATOMIC_CAS_DEREF_NULLPTR;
00043     } else if (*(uintptr_t *)((uintptr_t)current + valueOffset) != expectedDerefValue) {
00044         rc = AQ_ATOMIC_CAS_DEREF_VALUE;
00045     } else {
00046         *ptrAddr = newPtrValue;
00047         rc = AQ_ATOMIC_CAS_DEREF_SUCCESS;
00048     }
00049     aq_critical_section_exit();
00050     return rc;
00051 }
00052 #endif
00053 
00054 
00055 #if !defined(__SXOS__) && defined(__GNUC__) && (!defined(__CORTEX_M) || (__CORTEX_M >= 0x03))
00056 int aq_atomic_cas_uintptr(uintptr_t *ptr, uintptr_t oldval, uintptr_t newval)
00057 {
00058     return __sync_bool_compare_and_swap(ptr, oldval, newval);
00059 }
00060 
00061 int32_t aq_atomic_inc_int32(int32_t *ptr, int32_t inc)
00062 {
00063     return __sync_add_and_fetch(ptr, inc);
00064 }
00065 #else
00066 int aq_atomic_cas_uintptr(uintptr_t *ptr, uintptr_t oldval, uintptr_t newval)
00067 {
00068     int rc;
00069     aq_critical_section_enter();
00070     if (*ptr == oldval) {
00071         rc = 1;
00072         *ptr = newval;
00073     } else {
00074         rc = 0;
00075     }
00076     aq_critical_section_exit();
00077     return rc;
00078 }
00079 int32_t aq_atomic_inc_int32(int32_t *ptr, int32_t inc)
00080 {
00081     int32_t ret;
00082     aq_critical_section_enter();
00083     ret = *ptr + inc;
00084     *ptr = ret;
00085     aq_critical_section_exit();
00086     return ret;
00087 }
00088 #endif