1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License.
5060ff233Sopenharmony_ci * You may obtain a copy of the License at
6060ff233Sopenharmony_ci *
7060ff233Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8060ff233Sopenharmony_ci *
9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and
13060ff233Sopenharmony_ci * limitations under the License.
14060ff233Sopenharmony_ci */
15060ff233Sopenharmony_ci
16060ff233Sopenharmony_ci#ifndef SOFTBUS_ADAPTER_ATOMIC_H
17060ff233Sopenharmony_ci#define SOFTBUS_ADAPTER_ATOMIC_H
18060ff233Sopenharmony_ci
19060ff233Sopenharmony_ci#include "stdbool.h"
20060ff233Sopenharmony_ci#include "stdint.h"
21060ff233Sopenharmony_ci#include "stdio.h"
22060ff233Sopenharmony_ci
23060ff233Sopenharmony_ci#ifdef _WIN32
24060ff233Sopenharmony_ci#include "intrin.h"
25060ff233Sopenharmony_ci#endif
26060ff233Sopenharmony_ci
27060ff233Sopenharmony_cistatic inline void SoftBusAtomicAdd32(volatile uint32_t* ptr, int32_t value)
28060ff233Sopenharmony_ci{
29060ff233Sopenharmony_ci#ifdef _WIN32
30060ff233Sopenharmony_ci    _InterlockedExchangeAdd(ptr, value);
31060ff233Sopenharmony_ci#elif defined __ICCARM__
32060ff233Sopenharmony_ci    return;
33060ff233Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
34060ff233Sopenharmony_ci    __sync_fetch_and_add(ptr, value);
35060ff233Sopenharmony_ci#endif
36060ff233Sopenharmony_ci}
37060ff233Sopenharmony_ci
38060ff233Sopenharmony_cistatic inline uint32_t SoftBusAtomicAddAndFetch32(volatile uint32_t* ptr, int32_t value)
39060ff233Sopenharmony_ci{
40060ff233Sopenharmony_ci#ifdef _WIN32
41060ff233Sopenharmony_ci    return _InterlockedExchangeAdd(ptr, value) + value;
42060ff233Sopenharmony_ci#elif defined __ICCARM__
43060ff233Sopenharmony_ci    return -1;
44060ff233Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
45060ff233Sopenharmony_ci    return __sync_add_and_fetch(ptr, value);
46060ff233Sopenharmony_ci#else
47060ff233Sopenharmony_ci    return -1;
48060ff233Sopenharmony_ci#endif
49060ff233Sopenharmony_ci}
50060ff233Sopenharmony_ci
51060ff233Sopenharmony_cistatic inline void SoftBusAtomicAdd64(uint64_t* ptr, int64_t value)
52060ff233Sopenharmony_ci{
53060ff233Sopenharmony_ci#ifdef _WIN32
54060ff233Sopenharmony_ci    _InterlockedExchangeAdd64((volatile long long*)ptr, value);
55060ff233Sopenharmony_ci#elif defined __ICCARM__
56060ff233Sopenharmony_ci    return;
57060ff233Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
58060ff233Sopenharmony_ci    __sync_fetch_and_add(ptr, value);
59060ff233Sopenharmony_ci#endif
60060ff233Sopenharmony_ci}
61060ff233Sopenharmony_ci
62060ff233Sopenharmony_cistatic inline bool SoftBusAtomicCmpAndSwap32(volatile uint32_t* ptr, int32_t oldValue, int32_t newValue)
63060ff233Sopenharmony_ci{
64060ff233Sopenharmony_ci#ifdef _WIN32
65060ff233Sopenharmony_ci    uint32_t initial = _InterlockedCompareExchange(ptr, newValue, oldValue);
66060ff233Sopenharmony_ci    return initial == oldValue;
67060ff233Sopenharmony_ci#elif defined __ICCARM__
68060ff233Sopenharmony_ci    return false;
69060ff233Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
70060ff233Sopenharmony_ci    return __sync_bool_compare_and_swap(ptr, oldValue, newValue);
71060ff233Sopenharmony_ci#else
72060ff233Sopenharmony_ci    return false;
73060ff233Sopenharmony_ci#endif
74060ff233Sopenharmony_ci}
75060ff233Sopenharmony_ci#endif // SOFTBUS_ADAPTER_ATOMIC_H
76