1 /* 2 * Copyright (c) 2022 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 ECMASCRIPT_WAITER_LIST_H 17 #define ECMASCRIPT_WAITER_LIST_H 18 19 #include "ecmascript/ecma_macros.h" 20 #include "ecmascript/mem/c_containers.h" 21 22 #include "libpandabase/os/mutex.h" 23 24 namespace panda::ecmascript { 25 using Mutex = os::memory::Mutex; 26 using LockHolder = os::memory::LockHolder<Mutex>; 27 28 class WaiterListNode { 29 public: 30 WaiterListNode() = default; 31 ~WaiterListNode() = default; 32 33 NO_COPY_SEMANTIC(WaiterListNode); 34 NO_MOVE_SEMANTIC(WaiterListNode); 35 36 WaiterListNode *prev_ {nullptr}; 37 WaiterListNode *next_ {nullptr}; 38 // Used to call wait or Signal() to unlock wait and wake up 39 os::memory::ConditionVariable cond_; 40 41 // Managed Arraybuffer or SharedArrayBuffer memory data 42 void *date_ {nullptr}; 43 44 // the offset of the element in the typedArray 45 size_t index_ {0}; 46 47 // the memory address data corresponding to the offset 48 int8_t *waitPointer_ {nullptr}; 49 50 // used to determine whether to wait, start wait when waiting_ is true 51 bool waiting_ {false}; 52 53 private: 54 friend class WaiterList; 55 }; 56 57 // WaiterList to manage WaiterListNode 58 class WaiterList { 59 public: 60 WaiterList() = default; 61 ~WaiterList() = default; 62 void AddNode(WaiterListNode *node); 63 void DeleteNode(WaiterListNode *node); 64 struct HeadAndTail { 65 WaiterListNode *pHead {nullptr}; 66 WaiterListNode *pTail {nullptr}; 67 }; 68 69 // locationListMap_ is used AddNode or DeleteNode 70 // When calling addnode If there is no corresponding memory data, add the node corresponding to the key 71 CMap<int8_t *, HeadAndTail> locationListMap_; 72 }; 73 74 // The Singleton pattern is used to creat a global metux and WaiterList 75 template <class T> 76 class Singleton { 77 public: ~Singleton()78 ~Singleton() {} 79 NO_COPY_SEMANTIC(Singleton); 80 NO_MOVE_SEMANTIC(Singleton); GetInstance()81 static T *GetInstance() 82 { 83 static T instance; 84 return &instance; 85 } 86 87 private: 88 Singleton() = default; 89 }; 90 91 class SCOPED_CAPABILITY MutexGuard 92 { 93 public: MutexGuard(Mutex * mutex)94 explicit MutexGuard(Mutex *mutex) : mutex_(mutex), lockHolder_(*mutex) {} Unlock()95 void Unlock() RELEASE() 96 { 97 mutex_->Unlock(); 98 } 99 Lock()100 void Lock() ACQUIRE() 101 { 102 mutex_->Lock(); 103 } 104 105 Mutex *mutex_; 106 LockHolder lockHolder_; 107 }; 108 } // namespace 109 #endif // ECMASCRIPT_WAITER_LIST_H 110