1 /**
2 * Copyright (c) 2021-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 <cstdint>
17 #include <optional>
18 #include <sstream>
19 #include <string>
20 #include <utility>
21 #include "libpandabase/macros.h"
22 #include "libpandabase/os/mutex.h"
23 #include "runtime/include/runtime.h"
24 #include "runtime/include/panda_vm.h"
25 #include "runtime/include/object_header.h"
26 #include "plugins/ets/runtime/ets_vm.h"
27 #include "plugins/ets/runtime/types/ets_array.h"
28 #include "plugins/ets/runtime/types/ets_shared_memory.h"
29 #include "plugins/ets/runtime/types/ets_shared_memory-inl.h"
30 #include "plugins/ets/runtime/types/ets_void.h"
31 #include "plugins/ets/runtime/ets_coroutine.h"
32
33 namespace panda::ets::intrinsics {
34
SharedMemoryCreate(int32_t byteLength)35 extern "C" EtsSharedMemory *SharedMemoryCreate(int32_t byteLength)
36 {
37 return EtsSharedMemory::Create(byteLength);
38 }
39
SharedMemoryAt(EtsSharedMemory * mem,int32_t index)40 extern "C" int8_t SharedMemoryAt(EtsSharedMemory *mem, int32_t index)
41 {
42 return mem->GetElement(index);
43 }
44
SharedMemorySet(EtsSharedMemory * mem,int32_t index,int8_t value)45 extern "C" EtsVoid *SharedMemorySet(EtsSharedMemory *mem, int32_t index, int8_t value)
46 {
47 mem->SetElement(index, value);
48 return EtsVoid::GetInstance();
49 }
50
SharedMemoryGetByteLength(EtsSharedMemory * mem)51 extern "C" int32_t SharedMemoryGetByteLength(EtsSharedMemory *mem)
52 {
53 return static_cast<int32_t>(mem->GetLength());
54 }
55
SharedMemoryAddI8(EtsSharedMemory * mem,int32_t index,int8_t value)56 extern "C" int8_t SharedMemoryAddI8(EtsSharedMemory *mem, int32_t index, int8_t value)
57 {
58 auto add = [value](int8_t oldValue) { return oldValue + value; };
59 auto result = mem->ReadModifyWriteI8(index, add);
60 return result.first;
61 }
62
SharedMemoryAndI8(EtsSharedMemory * mem,int32_t index,int8_t value)63 extern "C" int8_t SharedMemoryAndI8(EtsSharedMemory *mem, int32_t index, int8_t value)
64 {
65 auto bitwiseAnd = [value](int8_t oldValue) {
66 return static_cast<int8_t>(bit_cast<uint8_t>(oldValue) & bit_cast<uint8_t>(value));
67 };
68 auto result = mem->ReadModifyWriteI8(index, bitwiseAnd);
69 return result.first;
70 }
71
SharedMemoryCompareExchangeI8(EtsSharedMemory * mem,int32_t index,int8_t expectedValue,int8_t replacementValue)72 extern "C" int8_t SharedMemoryCompareExchangeI8(EtsSharedMemory *mem, int32_t index, int8_t expectedValue,
73 int8_t replacementValue)
74 {
75 auto compareExchange = [expectedValue, replacementValue](int8_t oldValue) {
76 return oldValue == expectedValue ? replacementValue : oldValue;
77 };
78 auto result = mem->ReadModifyWriteI8(index, compareExchange);
79 return result.first;
80 }
81
SharedMemoryExchangeI8(EtsSharedMemory * mem,int32_t index,int8_t value)82 extern "C" int8_t SharedMemoryExchangeI8(EtsSharedMemory *mem, int32_t index, int8_t value)
83 {
84 auto exchange = [value]([[maybe_unused]] int8_t oldValue) { return value; };
85 auto result = mem->ReadModifyWriteI8(index, exchange);
86 return result.first;
87 }
88
SharedMemoryLoadI8(EtsSharedMemory * mem,int32_t index)89 extern "C" int8_t SharedMemoryLoadI8(EtsSharedMemory *mem, int32_t index)
90 {
91 auto load = [](int8_t value) { return value; };
92 auto result = mem->ReadModifyWriteI8(index, load);
93 return result.first;
94 }
95
SharedMemoryOrI8(EtsSharedMemory * mem,int32_t index,int8_t value)96 extern "C" int8_t SharedMemoryOrI8(EtsSharedMemory *mem, int32_t index, int8_t value)
97 {
98 auto orBitwise = [value](int8_t oldValue) {
99 return static_cast<int8_t>(bit_cast<uint8_t>(oldValue) | bit_cast<uint8_t>(value));
100 };
101 auto result = mem->ReadModifyWriteI8(index, orBitwise);
102 return result.first;
103 }
104
SharedMemoryStoreI8(EtsSharedMemory * mem,int32_t index,int8_t value)105 extern "C" int8_t SharedMemoryStoreI8(EtsSharedMemory *mem, int32_t index, int8_t value)
106 {
107 auto store = [value]([[maybe_unused]] int8_t oldValue) { return value; };
108 auto result = mem->ReadModifyWriteI8(index, store);
109 return result.second;
110 }
111
SharedMemorySubI8(EtsSharedMemory * mem,int32_t index,int8_t value)112 extern "C" int8_t SharedMemorySubI8(EtsSharedMemory *mem, int32_t index, int8_t value)
113 {
114 auto add = [value](int8_t oldValue) { return oldValue - value; };
115 auto result = mem->ReadModifyWriteI8(index, add);
116 return result.first;
117 }
118
SharedMemoryXorI8(EtsSharedMemory * mem,int32_t index,int8_t value)119 extern "C" int8_t SharedMemoryXorI8(EtsSharedMemory *mem, int32_t index, int8_t value)
120 {
121 auto xorBitwise = [value](int8_t oldValue) {
122 return static_cast<int8_t>(bit_cast<uint8_t>(oldValue) ^ bit_cast<uint8_t>(value));
123 };
124 auto result = mem->ReadModifyWriteI8(index, xorBitwise);
125 return result.first;
126 }
127
PrintWaiters(EtsSharedMemory & mem)128 std::string PrintWaiters(EtsSharedMemory &mem)
129 {
130 std::stringstream stream;
131 auto curWaiter = mem.GetHeadWaiter();
132 while (curWaiter != nullptr) {
133 stream << reinterpret_cast<size_t>(curWaiter) << " -> ";
134 curWaiter = curWaiter->GetNext();
135 }
136 return stream.str();
137 }
138
SharedMemoryWaitI32(EtsSharedMemory * mem,int32_t byteOffset,int32_t expectedValue)139 extern "C" int32_t SharedMemoryWaitI32(EtsSharedMemory *mem, int32_t byteOffset, int32_t expectedValue)
140 {
141 auto result = mem->WaitI32(byteOffset, expectedValue, std::nullopt);
142 return static_cast<int32_t>(result);
143 }
144
SharedMemoryWaitI64(EtsSharedMemory * mem,int32_t byteOffset,int64_t expectedValue)145 extern "C" int32_t SharedMemoryWaitI64(EtsSharedMemory *mem, int32_t byteOffset, int64_t expectedValue)
146 {
147 auto result = mem->WaitI64(byteOffset, expectedValue, std::nullopt);
148 return static_cast<int32_t>(result);
149 }
150
SharedMemoryTimedWaitI32(EtsSharedMemory * mem,int32_t byteOffset,int32_t expectedValue,int64_t ms)151 extern "C" int32_t SharedMemoryTimedWaitI32(EtsSharedMemory *mem, int32_t byteOffset, int32_t expectedValue, int64_t ms)
152 {
153 ASSERT(ms >= 0);
154 auto uMs = static_cast<uint64_t>(ms);
155 auto result = mem->WaitI32(byteOffset, expectedValue, std::optional(uMs));
156 return static_cast<int32_t>(result);
157 }
158
SharedMemoryTimedWaitI64(EtsSharedMemory * mem,int32_t byteOffset,int64_t expectedValue,int64_t ms)159 extern "C" int32_t SharedMemoryTimedWaitI64(EtsSharedMemory *mem, int32_t byteOffset, int64_t expectedValue, int64_t ms)
160 {
161 auto *currentCoro = EtsCoroutine::GetCurrent();
162 [[maybe_unused]] EtsHandleScope scope(currentCoro);
163 EtsHandle<EtsSharedMemory> hmem(currentCoro, mem);
164
165 ASSERT(ms >= 0);
166 auto uMs = static_cast<uint64_t>(ms);
167 ScopedNativeCodeThread n(currentCoro);
168
169 auto result = hmem->WaitI64(byteOffset, expectedValue, std::optional(uMs));
170 return static_cast<int32_t>(result);
171 }
172
SharedMemoryNotify(EtsSharedMemory * mem,int32_t byteOffset)173 extern "C" int32_t SharedMemoryNotify(EtsSharedMemory *mem, int32_t byteOffset)
174 {
175 return mem->NotifyI32(byteOffset, std::nullopt);
176 }
177
SharedMemoryBoundedNotify(EtsSharedMemory * mem,int32_t byteOffset,int32_t count)178 extern "C" int32_t SharedMemoryBoundedNotify(EtsSharedMemory *mem, int32_t byteOffset, int32_t count)
179 {
180 ASSERT(count >= 0);
181 return mem->NotifyI32(byteOffset, std::optional(count));
182 }
183
184 } // namespace panda::ets::intrinsics
185