Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

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(__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 
00060 int32_t aq_atomic_inc_int32(int32_t *ptr, int32_t inc) {
00061     return __sync_add_and_fetch(ptr, inc);
00062 }
00063 #else
00064 int aq_atomic_cas_uintptr(uintptr_t *ptr, uintptr_t oldval, uintptr_t newval)
00065 {
00066     int rc;
00067     aq_critical_section_enter();
00068     if (*ptr == oldval) {
00069         rc = 1;
00070         *ptr = newval;
00071     } else {
00072         rc = 0;
00073     }
00074     aq_critical_section_exit();
00075     return rc;
00076 }
00077 int32_t aq_atomic_inc_int32(int32_t *ptr, int32_t inc) {
00078     int32_t ret;
00079     aq_critical_section_enter();
00080     ret = *ptr + inc;
00081     *ptr = ret;
00082     aq_critical_section_exit();
00083     return ret;
00084 }
00085 #endif