• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_SHARED_MEMORY_INL_H
17 #define PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_SHARED_MEMORY_INL_H
18 
19 #include "runtime/include/thread_scopes.h"
20 #include "plugins/ets/runtime/types/ets_shared_memory.h"
21 
22 namespace ark::ets {
23 
24 template <typename T>
GetElement(uint32_t index)25 T EtsSharedMemory::GetElement(uint32_t index)
26 {
27     ASSERT_PRINT(index < GetLength(), "SharedMemory index out of bounds");
28     auto *currentCoro = EtsCoroutine::GetCurrent();
29     auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsSharedMemory, array_));
30     if constexpr (std::is_same_v<T, int8_t>) {
31         return reinterpret_cast<EtsByteArray *>(obj)->Get(index);
32     } else if constexpr (std::is_same_v<T, int16_t>) {
33         return reinterpret_cast<EtsShortArray *>(obj)->Get(index);
34     } else if constexpr (std::is_same_v<T, int32_t>) {
35         return reinterpret_cast<EtsIntArray *>(obj)->Get(index);
36     } else if constexpr (std::is_same_v<T, int64_t>) {
37         return reinterpret_cast<EtsLongArray *>(obj)->Get(index);
38     } else if constexpr (std::is_same_v<T, uint8_t>) {
39         return reinterpret_cast<EtsBooleanArray *>(obj)->Get(index);
40     } else if constexpr (std::is_same_v<T, uint16_t>) {
41         return reinterpret_cast<EtsCharArray *>(obj)->Get(index);
42     } else if constexpr (std::is_same_v<T, uint32_t>) {
43         return reinterpret_cast<EtsUintArray *>(obj)->Get(index);
44     } else if constexpr (std::is_same_v<T, uint64_t>) {
45         return reinterpret_cast<EtsUlongArray *>(obj)->Get(index);
46     } else {
47         UNREACHABLE();
48     }
49 }
50 
51 template <typename T>
SetElement(uint32_t index,T element)52 void EtsSharedMemory::SetElement(uint32_t index, T element)
53 {
54     ASSERT_PRINT(index < GetLength(), "SharedMemory index out of bounds");
55     auto *currentCoro = EtsCoroutine::GetCurrent();
56     auto *obj = ObjectAccessor::GetObject(currentCoro, this, MEMBER_OFFSET(EtsSharedMemory, array_));
57     if constexpr (std::is_same_v<T, int8_t>) {
58         reinterpret_cast<EtsByteArray *>(obj)->Set(index, element);
59     } else if constexpr (std::is_same_v<T, int16_t>) {
60         reinterpret_cast<EtsShortArray *>(obj)->Set(index, element);
61     } else if constexpr (std::is_same_v<T, int32_t>) {
62         reinterpret_cast<EtsIntArray *>(obj)->Set(index, element);
63     } else if constexpr (std::is_same_v<T, int64_t>) {
64         reinterpret_cast<EtsLongArray *>(obj)->Set(index, element);
65     } else if constexpr (std::is_same_v<T, uint8_t>) {
66         reinterpret_cast<EtsBooleanArray *>(obj)->Set(index, element);
67     } else if constexpr (std::is_same_v<T, uint16_t>) {
68         reinterpret_cast<EtsCharArray *>(obj)->Set(index, element);
69     } else if constexpr (std::is_same_v<T, uint32_t>) {
70         reinterpret_cast<EtsUintArray *>(obj)->Set(index, element);
71     } else if constexpr (std::is_same_v<T, uint64_t>) {
72         reinterpret_cast<EtsUlongArray *>(obj)->Set(index, element);
73     } else {
74         UNREACHABLE();
75     }
76 }
77 
78 template <typename T, typename F>
ReadModifyWrite(int32_t index,const F & f)79 std::pair<T, T> EtsSharedMemory::ReadModifyWrite(int32_t index, const F &f)
80 {
81     auto coroutine = EtsCoroutine::GetCurrent();
82     [[maybe_unused]] EtsHandleScope scope(coroutine);
83     EtsHandle<EtsSharedMemory> thisHandle(coroutine, this);
84 
85     // NOTE(egor-porsev): add LIKELY(std::try_lock) path to prevent ScopedNativeCodeThread creation if no blocking
86     // occurs
87     ScopedNativeCodeThread n(coroutine);
88     os::memory::LockHolder lock(coroutine->GetPandaVM()->GetAtomicsMutex());
89     ScopedManagedCodeThread m(coroutine);
90 
91     auto oldValue = thisHandle->GetElement<T>(index);
92     auto newValue = f(oldValue);
93     thisHandle->SetElement<T>(index, newValue);
94 
95     return std::pair(oldValue, newValue);
96 }
97 
98 }  // namespace ark::ets
99 
100 #endif  // PANDA_PLUGINS_ETS_RUNTIME_TYPES_ETS_SHARED_MEMORY_INL_H
101