2022-05-25 14:03:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ECMASCRIPT_WAITER_LIST_H
|
|
|
|
#define ECMASCRIPT_WAITER_LIST_H
|
|
|
|
|
|
|
|
#include "ecmascript/ecma_macros.h"
|
|
|
|
#include "ecmascript/mem/c_containers.h"
|
2022-07-23 10:54:34 +00:00
|
|
|
|
2023-09-27 07:07:10 +00:00
|
|
|
#include "ecmascript/platform/mutex.h"
|
2022-05-25 14:03:38 +00:00
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
|
|
|
class WaiterListNode {
|
|
|
|
public:
|
|
|
|
WaiterListNode() = default;
|
|
|
|
~WaiterListNode() = default;
|
|
|
|
|
|
|
|
NO_COPY_SEMANTIC(WaiterListNode);
|
|
|
|
NO_MOVE_SEMANTIC(WaiterListNode);
|
|
|
|
|
|
|
|
WaiterListNode *prev_ {nullptr};
|
|
|
|
WaiterListNode *next_ {nullptr};
|
|
|
|
// Used to call wait or Signal() to unlock wait and wake up
|
2023-09-27 07:07:10 +00:00
|
|
|
ConditionVariable cond_;
|
2022-05-25 14:03:38 +00:00
|
|
|
|
|
|
|
// Managed Arraybuffer or SharedArrayBuffer memory data
|
|
|
|
void *date_ {nullptr};
|
|
|
|
|
|
|
|
// the offset of the element in the typedArray
|
|
|
|
size_t index_ {0};
|
|
|
|
|
|
|
|
// the memory address data corresponding to the offset
|
|
|
|
int8_t *waitPointer_ {nullptr};
|
|
|
|
|
|
|
|
// used to determine whether to wait, start wait when waiting_ is true
|
|
|
|
bool waiting_ {false};
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class WaiterList;
|
|
|
|
};
|
|
|
|
|
|
|
|
// WaiterList to manage WaiterListNode
|
|
|
|
class WaiterList {
|
|
|
|
public:
|
|
|
|
WaiterList() = default;
|
2022-07-11 01:50:28 +00:00
|
|
|
~WaiterList() = default;
|
2022-05-25 14:03:38 +00:00
|
|
|
void AddNode(WaiterListNode *node);
|
|
|
|
void DeleteNode(WaiterListNode *node);
|
|
|
|
struct HeadAndTail {
|
|
|
|
WaiterListNode *pHead {nullptr};
|
|
|
|
WaiterListNode *pTail {nullptr};
|
|
|
|
};
|
|
|
|
|
|
|
|
// locationListMap_ is used AddNode or DeleteNode
|
|
|
|
// When calling addnode If there is no corresponding memory data, add the node corresponding to the key
|
|
|
|
CMap<int8_t *, HeadAndTail> locationListMap_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The Singleton pattern is used to creat a global metux and WaiterList
|
|
|
|
template <class T>
|
|
|
|
class Singleton {
|
|
|
|
public:
|
|
|
|
~Singleton() {}
|
|
|
|
NO_COPY_SEMANTIC(Singleton);
|
|
|
|
NO_MOVE_SEMANTIC(Singleton);
|
|
|
|
static T *GetInstance()
|
|
|
|
{
|
|
|
|
static T instance;
|
|
|
|
return &instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Singleton() = default;
|
|
|
|
};
|
|
|
|
|
2023-09-27 07:07:10 +00:00
|
|
|
class MutexGuard {
|
2022-05-25 14:03:38 +00:00
|
|
|
public:
|
|
|
|
explicit MutexGuard(Mutex *mutex) : mutex_(mutex), lockHolder_(*mutex) {}
|
2023-09-27 07:07:10 +00:00
|
|
|
void Unlock()
|
2022-05-25 14:03:38 +00:00
|
|
|
{
|
|
|
|
mutex_->Unlock();
|
|
|
|
}
|
2022-08-23 09:54:30 +00:00
|
|
|
|
2023-09-27 07:07:10 +00:00
|
|
|
void Lock()
|
2022-05-25 14:03:38 +00:00
|
|
|
{
|
|
|
|
mutex_->Lock();
|
|
|
|
}
|
2023-09-27 07:07:10 +00:00
|
|
|
private:
|
2022-05-25 14:03:38 +00:00
|
|
|
Mutex *mutex_;
|
|
|
|
LockHolder lockHolder_;
|
|
|
|
};
|
|
|
|
} // namespace
|
2022-07-11 01:50:28 +00:00
|
|
|
#endif // ECMASCRIPT_WAITER_LIST_H
|