• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LOCK_REQUEST_H
17 #define JS_CONCURRENT_MODULE_UTILS_LOCKS_LOCK_REQUEST_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <queue>
22 #include <string>
23 
24 #include "common.h"
25 #include "helper/error_helper.h"
26 #include "helper/napi_helper.h"
27 #include "helper/object_helper.h"
28 
29 namespace Commonlibrary::Concurrent::LocksModule {
30 enum LockMode {
31     LOCK_MODE_UNLOCK,
32     LOCK_MODE_SHARED,
33     LOCK_MODE_EXCLUSIVE,
34     LOCK_MODE_MAX
35 };
36 
37 struct LockOptions {
38     bool isAvailable = false;
39     napi_ref signal = nullptr;
40     uint32_t timeoutMillis = 0;
41 };
42 
43 class AsyncLock;
44 
45 class LockRequest : public std::enable_shared_from_this<LockRequest> {
46 public:
47     LockRequest(AsyncLock* lock, tid_t tid, napi_env env, napi_ref cb, LockMode mode, const LockOptions &options,
48         napi_deferred deferred);
49 
GetWeakPtr()50     std::weak_ptr<LockRequest> GetWeakPtr()
51     {
52         return weak_from_this();
53     }
54 
GetTid()55     tid_t GetTid() const
56     {
57         return tid_;
58     }
59 
GetCreationStacktrace()60     std::string GetCreationStacktrace()
61     {
62         return creationStacktrace_;
63     }
64 
GetMode()65     LockMode GetMode() const
66     {
67         return mode_;
68     }
69 
GetOptions()70     const LockOptions &GetOptions() const
71     {
72         return options_;
73     }
74 
GetEnv()75     napi_env GetEnv()
76     {
77         return env_;
78     }
79 
80     void CallCallbackAsync();
81     void CallCallback();
82 
83 private:
84     bool AbortIfNeeded();
85     void ArmTimeoutTimer(napi_env env, uint32_t timeoutMillis);
86     void DisarmTimeoutTimer(napi_env env);
87     void HandleRequestTimeout(std::string &&errorMessage);
88     std::string GetLockInfo() const;
89     static void AsyncAfterWorkCallback(napi_env env, napi_status status, void *data);
90     static napi_value FinallyCallback(napi_env env, napi_callback_info info);
91     static void TimeoutCallback(uv_timer_t *handle);
92     static void DeallocateTimeoutTimerCallback(uv_handle_t* handle);
93     static void EnvCleanup(void *arg);
94 
95     void InitTimer();
96     void StopTimer();
97     void CloseTimer();
98 
99     void AddEnvCleanupHook();
100     void RemoveEnvCleanupHook();
101 
102     void Release();
103 
104     AsyncLock* lock_;
105     tid_t tid_;
106     std::string creationStacktrace_;
107     NativeEngine *engine_;
108     napi_env env_;
109     napi_ref callback_;
110     LockMode mode_;
111     LockOptions options_;
112     napi_deferred deferred_;
113     napi_async_work work_;
114     uv_timer_t *timeoutTimer_ {nullptr};
115     uint64_t engineId_;
116     std::atomic_bool envIsInvalid_ {false};
117 };
118 
119 }  // namespace Commonlibrary::Concurrent::LocksModule
120 #endif  // JS_CONCURRENT_MODULE_UTILS_LOCKS_LOCK_REQUEST_H
121