• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "runtime/monitor_pool.h"
17 
18 #include "runtime/include/object_header.h"
19 #include "runtime/include/runtime.h"
20 #include "runtime/include/panda_vm.h"
21 #include "runtime/mark_word.h"
22 #include "runtime/monitor.h"
23 
24 namespace panda {
25 
CreateMonitor(PandaVM * vm,ObjectHeader * obj)26 Monitor *MonitorPool::CreateMonitor(PandaVM *vm, ObjectHeader *obj)
27 {
28     MonitorPool *pool = vm->GetMonitorPool();
29 
30     os::memory::LockHolder lock(pool->pool_lock_);
31     for (Monitor::MonitorId i = 0; i < MAX_MONITOR_ID; i++) {
32         pool->last_id_ = (pool->last_id_ + 1) % MAX_MONITOR_ID;
33         if (pool->monitors_.count(pool->last_id_) == 0) {
34             auto monitor = pool->allocator_->New<Monitor>(pool->last_id_);
35             if (monitor == nullptr) {
36                 return nullptr;
37             }
38             (pool->monitors_)[pool->last_id_] = monitor;
39             monitor->SetObject(obj);
40             return monitor;
41         }
42     }
43     LOG(FATAL, RUNTIME) << "Out of MonitorPool indexes";
44     UNREACHABLE();
45 }
46 
LookupMonitor(PandaVM * vm,Monitor::MonitorId id)47 Monitor *MonitorPool::LookupMonitor(PandaVM *vm, Monitor::MonitorId id)
48 {
49     MonitorPool *pool = vm->GetMonitorPool();
50     os::memory::LockHolder lock(pool->pool_lock_);
51     if (pool->monitors_.count(id) == 0) {
52         return nullptr;
53     }
54     return (pool->monitors_)[id];
55 }
56 
FreeMonitor(PandaVM * vm,Monitor::MonitorId id)57 void MonitorPool::FreeMonitor(PandaVM *vm, Monitor::MonitorId id)
58 {
59     MonitorPool *pool = vm->GetMonitorPool();
60     os::memory::LockHolder lock(pool->pool_lock_);
61     auto monitor = (pool->monitors_)[id];
62     pool->monitors_.erase(id);
63     pool->allocator_->Delete(monitor);
64 }
65 
DeflateMonitors()66 void MonitorPool::DeflateMonitors()
67 {
68     os::memory::LockHolder lock(pool_lock_);
69     for (auto monitor_iter = monitors_.begin(); monitor_iter != monitors_.end();) {
70         auto monitor = monitor_iter->second;
71         if (monitor->DeflateInternal()) {
72             monitor_iter = monitors_.erase(monitor_iter);
73             allocator_->Delete(monitor);
74         } else {
75             monitor_iter++;
76         }
77     }
78 }
79 
80 }  // namespace panda
81