Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "monitor_pool.h" |
| 18 | |
| 19 | #include "base/logging.h" |
| 20 | #include "base/mutex-inl.h" |
Ian Rogers | 44d6ff1 | 2014-03-06 23:11:11 -0800 | [diff] [blame] | 21 | #include "thread-inl.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 22 | #include "monitor.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 26 | namespace mirror { |
| 27 | class Object; |
| 28 | } // namespace mirror |
| 29 | |
| 30 | MonitorPool::MonitorPool() |
| 31 | : num_chunks_(0), capacity_(0), first_free_(nullptr) { |
| 32 | AllocateChunk(); // Get our first chunk. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 35 | // Assumes locks are held appropriately when necessary. |
| 36 | // We do not need a lock in the constructor, but we need one when in CreateMonitorInPool. |
| 37 | void MonitorPool::AllocateChunk() { |
| 38 | DCHECK(first_free_ == nullptr); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 39 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 40 | // Do we need to resize? |
| 41 | if (num_chunks_ == capacity_) { |
| 42 | if (capacity_ == 0U) { |
| 43 | // Initialization. |
| 44 | capacity_ = kInitialChunkStorage; |
Andreas Gampe | 057134b | 2016-03-10 08:33:45 -0800 | [diff] [blame^] | 45 | uintptr_t* new_backing = new uintptr_t[capacity_](); |
| 46 | DCHECK(monitor_chunks_.LoadRelaxed() == nullptr); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 47 | monitor_chunks_.StoreRelaxed(new_backing); |
| 48 | } else { |
| 49 | size_t new_capacity = 2 * capacity_; |
Andreas Gampe | 057134b | 2016-03-10 08:33:45 -0800 | [diff] [blame^] | 50 | uintptr_t* new_backing = new uintptr_t[new_capacity](); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 51 | uintptr_t* old_backing = monitor_chunks_.LoadRelaxed(); |
| 52 | memcpy(new_backing, old_backing, sizeof(uintptr_t) * capacity_); |
| 53 | monitor_chunks_.StoreRelaxed(new_backing); |
| 54 | capacity_ = new_capacity; |
Andreas Gampe | 4464a3e | 2016-03-03 20:15:47 -0800 | [diff] [blame] | 55 | old_chunk_arrays_.push_back(std::unique_ptr<uintptr_t[]>(old_backing)); |
Mathieu Chartier | 2c26501 | 2014-08-05 18:15:56 -0700 | [diff] [blame] | 56 | VLOG(monitor) << "Resizing to capacity " << capacity_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 59 | |
| 60 | // Allocate the chunk. |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 61 | void* chunk = allocator_.allocate(kChunkSize); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 62 | // Check we allocated memory. |
| 63 | CHECK_NE(reinterpret_cast<uintptr_t>(nullptr), reinterpret_cast<uintptr_t>(chunk)); |
| 64 | // Check it is aligned as we need it. |
| 65 | CHECK_EQ(0U, reinterpret_cast<uintptr_t>(chunk) % kMonitorAlignment); |
| 66 | |
| 67 | // Add the chunk. |
Mathieu Chartier | 2c26501 | 2014-08-05 18:15:56 -0700 | [diff] [blame] | 68 | *(monitor_chunks_.LoadRelaxed() + num_chunks_) = reinterpret_cast<uintptr_t>(chunk); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 69 | num_chunks_++; |
| 70 | |
| 71 | // Set up the free list |
| 72 | Monitor* last = reinterpret_cast<Monitor*>(reinterpret_cast<uintptr_t>(chunk) + |
| 73 | (kChunkCapacity - 1) * kAlignedMonitorSize); |
| 74 | last->next_free_ = nullptr; |
| 75 | // Eagerly compute id. |
| 76 | last->monitor_id_ = OffsetToMonitorId((num_chunks_ - 1) * kChunkSize + |
| 77 | (kChunkCapacity - 1) * kAlignedMonitorSize); |
| 78 | for (size_t i = 0; i < kChunkCapacity - 1; ++i) { |
| 79 | Monitor* before = reinterpret_cast<Monitor*>(reinterpret_cast<uintptr_t>(last) - |
| 80 | kAlignedMonitorSize); |
| 81 | before->next_free_ = last; |
| 82 | // Derive monitor_id from last. |
| 83 | before->monitor_id_ = OffsetToMonitorId(MonitorIdToOffset(last->monitor_id_) - |
| 84 | kAlignedMonitorSize); |
| 85 | |
| 86 | last = before; |
| 87 | } |
| 88 | DCHECK(last == reinterpret_cast<Monitor*>(chunk)); |
| 89 | first_free_ = last; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Andreas Gampe | 057134b | 2016-03-10 08:33:45 -0800 | [diff] [blame^] | 92 | void MonitorPool::FreeInternal() { |
| 93 | // This is on shutdown with NO_THREAD_SAFETY_ANALYSIS, can't/don't need to lock. |
| 94 | uintptr_t* backing = monitor_chunks_.LoadRelaxed(); |
| 95 | DCHECK(backing != nullptr); |
| 96 | DCHECK_GT(capacity_, 0U); |
| 97 | DCHECK_GT(num_chunks_, 0U); |
| 98 | |
| 99 | for (size_t i = 0; i < capacity_; ++i) { |
| 100 | if (i < num_chunks_) { |
| 101 | DCHECK_NE(backing[i], 0U); |
| 102 | allocator_.deallocate(reinterpret_cast<uint8_t*>(backing[i]), kChunkSize); |
| 103 | } else { |
| 104 | DCHECK_EQ(backing[i], 0U); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | delete[] backing; |
| 109 | } |
| 110 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 111 | Monitor* MonitorPool::CreateMonitorInPool(Thread* self, Thread* owner, mirror::Object* obj, |
| 112 | int32_t hash_code) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 113 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 114 | // We are gonna allocate, so acquire the writer lock. |
| 115 | MutexLock mu(self, *Locks::allocated_monitor_ids_lock_); |
| 116 | |
| 117 | // Enough space, or need to resize? |
| 118 | if (first_free_ == nullptr) { |
Mathieu Chartier | 2c26501 | 2014-08-05 18:15:56 -0700 | [diff] [blame] | 119 | VLOG(monitor) << "Allocating a new chunk."; |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 120 | AllocateChunk(); |
| 121 | } |
| 122 | |
| 123 | Monitor* mon_uninitialized = first_free_; |
| 124 | first_free_ = first_free_->next_free_; |
| 125 | |
| 126 | // Pull out the id which was preinitialized. |
| 127 | MonitorId id = mon_uninitialized->monitor_id_; |
| 128 | |
| 129 | // Initialize it. |
| 130 | Monitor* monitor = new(mon_uninitialized) Monitor(self, owner, obj, hash_code, id); |
| 131 | |
| 132 | return monitor; |
| 133 | } |
| 134 | |
| 135 | void MonitorPool::ReleaseMonitorToPool(Thread* self, Monitor* monitor) { |
| 136 | // Might be racy with allocation, so acquire lock. |
| 137 | MutexLock mu(self, *Locks::allocated_monitor_ids_lock_); |
| 138 | |
| 139 | // Keep the monitor id. Don't trust it's not cleared. |
| 140 | MonitorId id = monitor->monitor_id_; |
| 141 | |
| 142 | // Call the destructor. |
| 143 | // TODO: Exception safety? |
| 144 | monitor->~Monitor(); |
| 145 | |
| 146 | // Add to the head of the free list. |
| 147 | monitor->next_free_ = first_free_; |
| 148 | first_free_ = monitor; |
| 149 | |
| 150 | // Rewrite monitor id. |
| 151 | monitor->monitor_id_ = id; |
| 152 | } |
| 153 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 154 | void MonitorPool::ReleaseMonitorsToPool(Thread* self, MonitorList::Monitors* monitors) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 155 | for (Monitor* mon : *monitors) { |
| 156 | ReleaseMonitorToPool(self, mon); |
| 157 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | } // namespace art |