1 /* 2 * Copyright (c) 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 JS_CONCURRENT_MODULE_UTILS_LOCKS_ASYNC_LOCK_H 17 #define JS_CONCURRENT_MODULE_UTILS_LOCKS_ASYNC_LOCK_H 18 19 #include <list> 20 #include <string> 21 #include "common.h" 22 #include "lock_request.h" 23 #include "helper/error_helper.h" 24 #include "helper/napi_helper.h" 25 #include "helper/object_helper.h" 26 27 namespace Commonlibrary::Concurrent::LocksModule { 28 29 struct RequestCreationInfo { 30 tid_t tid; 31 std::string creationStacktrace; 32 }; 33 34 class AsyncLock { 35 public: 36 explicit AsyncLock(const std::string &lockName); 37 explicit AsyncLock(uint32_t lockId); 38 ~AsyncLock() = default; 39 40 napi_value LockAsync(napi_env env, napi_ref cb, LockMode mode, const LockOptions &options); 41 void CleanUpLockRequestOnCompletion(LockRequest* lockRequest); 42 bool CleanUpLockRequestOnTimeout(LockRequest* lockRequest); 43 napi_status FillLockState(napi_env env, napi_value held, napi_value pending); 44 void ProcessPendingLockRequest(napi_env env, LockRequest* syncLockRequest = nullptr); 45 46 // Increment the reference counter 47 uint32_t IncRefCount(); 48 // Decrement the reference counter. 49 // When the counter gets 0 the method deletes the instance if possible. 50 // Any way you cannot use the instance if the method returns 0. 51 uint32_t DecRefCount(); 52 53 std::vector<RequestCreationInfo> GetSatisfiedRequestInfos(); 54 std::vector<RequestCreationInfo> GetPendingRequestInfos(); 55 GetLockName()56 std::string GetLockName() const 57 { 58 return lockName_; 59 } GetLockId()60 uint32_t GetLockId() const 61 { 62 return anonymousLockId_; 63 } 64 65 private: 66 bool CanAcquireLock(LockRequest *lockRequest); 67 napi_value CreateLockInfo(napi_env env, const LockRequest *rq); 68 void AsyncDestroy(napi_env env); 69 static void AsyncDestroyCallback(napi_env env, napi_status status, void *data); 70 template <bool isAsync> 71 void ProcessLockRequest(napi_env env, LockRequest *lockRequest); 72 void ProcessPendingLockRequestUnsafe(napi_env env, LockRequest* syncLockRequest = nullptr); 73 74 std::list<LockRequest *> pendingList_ {}; 75 std::list<LockRequest *> heldList_ {}; 76 LockMode lockStatus_ = LOCK_MODE_UNLOCK; 77 std::string lockName_ = ""; // "" for anonymous lock 78 uint32_t anonymousLockId_ {}; // 0 for Non-anonymous lock 79 std::mutex asyncLockMutex_; 80 uint32_t refCount_ = 1; 81 }; 82 83 } // namespace Commonlibrary::Concurrent::LocksModule 84 #endif // JS_CONCURRENT_MODULE_UTILS_LOCKS_ASYNC_LOCK_H 85