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 "runtime/include/thread_scopes.h"
17 #include "plugins/ets/runtime/types/ets_shared_memory.h"
18
19 #ifndef PANDA_PLUGINS_ETS_RUNTIME_FFI_CLASSES_ETS_BUFFER_INL_H
20 #define PANDA_PLUGINS_ETS_RUNTIME_FFI_CLASSES_ETS_BUFFER_INL_H
21
22 namespace panda::ets {
23
24 template <typename F>
ReadModifyWriteI8(int32_t index,const F & f)25 std::pair<int8_t, int8_t> EtsSharedMemory::ReadModifyWriteI8(int32_t index, const F &f)
26 {
27 auto coroutine = EtsCoroutine::GetCurrent();
28 [[maybe_unused]] EtsHandleScope scope(coroutine);
29 EtsHandle<EtsSharedMemory> thisHandle(coroutine, this);
30
31 // NOTE(egor-porsev): add LIKELY(std::try_lock) path to prevent ScopedNativeCodeThread creation if no blocking
32 // occurs
33 ScopedNativeCodeThread n(coroutine);
34 os::memory::LockHolder lock(coroutine->GetPandaVM()->GetAtomicsMutex());
35 ScopedManagedCodeThread m(coroutine);
36
37 auto oldValue = thisHandle->GetElement(index);
38 auto newValue = f(oldValue);
39 thisHandle->SetElement(index, newValue);
40
41 return std::pair(oldValue, newValue);
42 }
43
44 } // namespace panda::ets
45
46 #endif // PANDA_PLUGINS_ETS_RUNTIME_FFI_CLASSES_ETS_BUFFER_INL_H