• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-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 #include "session_manager.h"
17 
18 namespace ark::tooling::inspector {
AddSession(PtThread thread)19 const std::string &SessionManager::AddSession(PtThread thread)
20 {
21     ASSERT(thread.GetManagedThread());
22 
23     os::memory::LockHolder lock(mutex_);
24     return sessions_.insert({!sessions_.empty() ? std::to_string(thread.GetId()) : "", thread}).first->first;
25 }
26 
RemoveSession(const std::string & id)27 void SessionManager::RemoveSession(const std::string &id)
28 {
29     os::memory::LockHolder lock(mutex_);
30     sessions_.erase(id);
31 }
32 
GetSessionIdByThread(PtThread thread) const33 std::string SessionManager::GetSessionIdByThread(PtThread thread) const
34 {
35     ASSERT(thread.GetManagedThread());
36 
37     os::memory::LockHolder lock(mutex_);
38 
39     auto it = sessions_.find("");
40     if (it != sessions_.end() && it->second == thread) {
41         return "";
42     }
43 
44     return std::to_string(thread.GetId());
45 }
46 
GetThreadBySessionId(const std::string & id) const47 PtThread SessionManager::GetThreadBySessionId(const std::string &id) const
48 {
49     os::memory::LockHolder lock(mutex_);
50 
51     auto it = sessions_.find(id);
52     if (it == sessions_.end()) {
53         return PtThread::NONE;
54     }
55 
56     return it->second;
57 }
58 
EnumerateSessions(const std::function<void (const std::string &,PtThread)> & handler) const59 void SessionManager::EnumerateSessions(const std::function<void(const std::string &, PtThread)> &handler) const
60 {
61     os::memory::LockHolder lock(mutex_);
62     for (auto &[id, thread] : sessions_) {
63         handler(id, thread);
64     }
65 }
66 }  // namespace ark::tooling::inspector
67