1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <sys/types.h> // pid_t 18 #include <mutex> 19 20 #include "berberis/base/checks.h" 21 #include "berberis/guest_os_primitives/guest_thread.h" 22 23 #include "guest_thread_map.h" 24 25 namespace berberis { 26 ResetThreadTable(pid_t tid,GuestThread * thread)27void GuestThreadMap::ResetThreadTable(pid_t tid, GuestThread* thread) { 28 std::lock_guard<std::mutex> lock(mutex_); 29 map_.clear(); 30 map_[tid] = thread; 31 } 32 InsertThread(pid_t tid,GuestThread * thread)33void GuestThreadMap::InsertThread(pid_t tid, GuestThread* thread) { 34 std::lock_guard<std::mutex> lock(mutex_); 35 auto result = map_.insert({tid, thread}); 36 CHECK(result.second); 37 } 38 RemoveThread(pid_t tid)39GuestThread* GuestThreadMap::RemoveThread(pid_t tid) { 40 std::lock_guard<std::mutex> lock(mutex_); 41 auto it = map_.find(tid); 42 CHECK(it != map_.end()); 43 GuestThread* thread = it->second; 44 map_.erase(it); 45 return thread; 46 } 47 FindThread(pid_t tid)48GuestThread* GuestThreadMap::FindThread(pid_t tid) { 49 std::lock_guard<std::mutex> lock(mutex_); 50 auto it = map_.find(tid); 51 if (it == map_.end()) { 52 return nullptr; 53 } 54 return it->second; 55 } 56 57 } // namespace berberis