1bafb9395Sopenharmony_ci/* 2bafb9395Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3bafb9395Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bafb9395Sopenharmony_ci * you may not use this file except in compliance with the License. 5bafb9395Sopenharmony_ci * You may obtain a copy of the License at 6bafb9395Sopenharmony_ci * 7bafb9395Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bafb9395Sopenharmony_ci * 9bafb9395Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bafb9395Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bafb9395Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bafb9395Sopenharmony_ci * See the License for the specific language governing permissions and 13bafb9395Sopenharmony_ci * limitations under the License. 14bafb9395Sopenharmony_ci */ 15bafb9395Sopenharmony_ci 16bafb9395Sopenharmony_ci#ifndef GRAPHIC_LITE_HAL_ATOMIC_H 17bafb9395Sopenharmony_ci#define GRAPHIC_LITE_HAL_ATOMIC_H 18bafb9395Sopenharmony_ci 19bafb9395Sopenharmony_ci#include "stdbool.h" 20bafb9395Sopenharmony_ci#include "stdint.h" 21bafb9395Sopenharmony_ci#include "stdio.h" 22bafb9395Sopenharmony_ci 23bafb9395Sopenharmony_cistatic inline void HalAtomicAdd32(volatile uint32_t* ptr, uint32_t value) 24bafb9395Sopenharmony_ci{ 25bafb9395Sopenharmony_ci#ifdef _WIN32 26bafb9395Sopenharmony_ci InterlockedExchangeAdd(ptr, value); 27bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__ 28bafb9395Sopenharmony_ci __sync_fetch_and_add(ptr, value); 29bafb9395Sopenharmony_ci#endif 30bafb9395Sopenharmony_ci} 31bafb9395Sopenharmony_ci 32bafb9395Sopenharmony_cistatic inline uint32_t HalAtomicAddAndFetch32(volatile uint32_t* ptr, uint32_t value) 33bafb9395Sopenharmony_ci{ 34bafb9395Sopenharmony_ci#ifdef _WIN32 35bafb9395Sopenharmony_ci return InterlockedExchangeAdd(ptr, value) + value; 36bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__ 37bafb9395Sopenharmony_ci return __sync_add_and_fetch(ptr, value); 38bafb9395Sopenharmony_ci#else 39bafb9395Sopenharmony_ci return -1; 40bafb9395Sopenharmony_ci#endif 41bafb9395Sopenharmony_ci} 42bafb9395Sopenharmony_ci 43bafb9395Sopenharmony_cistatic inline void HalAtomicAdd64(uint64_t* ptr, uint64_t value) 44bafb9395Sopenharmony_ci{ 45bafb9395Sopenharmony_ci#ifdef _WIN32 46bafb9395Sopenharmony_ci InterlockedExchangeAdd64((volatile long long*)ptr, value); 47bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__ 48bafb9395Sopenharmony_ci __sync_fetch_and_add(ptr, value); 49bafb9395Sopenharmony_ci#endif 50bafb9395Sopenharmony_ci} 51bafb9395Sopenharmony_ci 52bafb9395Sopenharmony_cistatic inline bool HalAtomicCmpAndSwap32(volatile uint32_t* ptr, uint32_t oldValue, uint32_t newValue) 53bafb9395Sopenharmony_ci{ 54bafb9395Sopenharmony_ci#ifdef _WIN32 55bafb9395Sopenharmony_ci uint32_t initial = InterlockedCompareExchange(ptr, newValue, oldValue); 56bafb9395Sopenharmony_ci return initial == oldValue; 57bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__ 58bafb9395Sopenharmony_ci return __sync_bool_compare_and_swap(ptr, oldValue, newValue); 59bafb9395Sopenharmony_ci#else 60bafb9395Sopenharmony_ci return false; 61bafb9395Sopenharmony_ci#endif 62bafb9395Sopenharmony_ci} 63bafb9395Sopenharmony_ci#endif // GRAPHIC_LITE_HAL_ATOMIC_H 64