Simple interface for Mbed Cloud Client

Dependents:  

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 
00022 #if defined(TARGET_LIKE_MBED)
00023 #include "cmsis.h"
00024 #endif
00025 
00026 #if !defined(__CORTEX_M) || (__CORTEX_M < 0x03)
00027 #include "aq_critical.h"
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(__GNUC__) && (!defined(__CORTEX_M) || (__CORTEX_M >= 0x03))
00056 int aq_atomic_cas_uintptr(uintptr_t *ptr, uintptr_t oldval, uintptr_t newval) {
00057     return __sync_bool_compare_and_swap(ptr, oldval, newval);
00058 }
00059 #else
00060 int aq_atomic_cas_uintptr(uintptr_t *ptr, uintptr_t oldval, uintptr_t newval)
00061 {
00062     int rc;
00063     aq_critical_section_enter();
00064     if (*ptr == oldval) {
00065         rc = 1;
00066         *ptr = newval;
00067     } else {
00068         rc = 0;
00069     }
00070     aq_critical_section_exit();
00071     return rc;
00072 }
00073 
00074 #endif