• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "mpl_atomic.h"
17 #include <cstdint>
18 #include "mpl_logging.h"
19 
20 namespace maple {
21 namespace {
22 constexpr int32 kMaxSizeOfTab = 6;
23 };
MemOrdFromU32(uint32 val)24 MemOrd MemOrdFromU32(uint32 val)
25 {
26     /* 6 is the size of tab below. 2 is memory_order_consume, it is Disabled. */
27     CHECK_FATAL(val <= kMaxSizeOfTab, "Illegal number for MemOrd: %u", val);
28     CHECK_FATAL(val != 2, "Illegal number for MemOrd: %u", val); // 2 is memory_order_consume
29     static std::array<MemOrd, kMaxSizeOfTab + 1> tab = {
30         MemOrd::kNotAtomic,
31         MemOrd::memory_order_relaxed,
32         /*
33          * memory_order_consume Disabled. Its semantics is debatable.
34          * We don't support it now, but reserve the number. Use memory_order_acquire instead.
35          */
36         MemOrd::memory_order_acquire, /* padding entry */
37         MemOrd::memory_order_acquire,
38         MemOrd::memory_order_release,
39         MemOrd::memory_order_acq_rel,
40         MemOrd::memory_order_seq_cst,
41     };
42     return tab[val];
43 }
44 
MemOrdIsAcquire(MemOrd ord)45 bool MemOrdIsAcquire(MemOrd ord)
46 {
47     static std::array<bool, kMaxSizeOfTab + 1> tab = {
48         false, /* kNotAtomic */
49         false, /* memory_order_relaxed */
50         true,  /* memory_order_consume */
51         true,  /* memory_order_acquire */
52         false, /* memory_order_release */
53         true,  /* memory_order_acq_rel */
54         true,  /* memory_order_seq_cst */
55     };
56     uint32 tabIndex = static_cast<uint32>(ord);
57     CHECK_FATAL(tabIndex <= kMaxSizeOfTab, "Illegal number for MemOrd: %u", tabIndex);
58     return tab[tabIndex];
59 }
60 
MemOrdIsRelease(MemOrd ord)61 bool MemOrdIsRelease(MemOrd ord)
62 {
63     static std::array<bool, kMaxSizeOfTab + 1> tab = {
64         false, /* kNotAtomic */
65         false, /* memory_order_relaxed */
66         false, /* memory_order_consume */
67         false, /* memory_order_acquire */
68         true,  /* memory_order_release */
69         true,  /* memory_order_acq_rel */
70         true,  /* memory_order_seq_cst */
71     };
72     uint32 tabIndex = static_cast<uint32>(ord);
73     CHECK_FATAL(tabIndex <= kMaxSizeOfTab, "Illegal number for MemOrd: %u", tabIndex);
74     return tab[tabIndex];
75 }
76 } /* namespace maple */
77