blob: 4a364cab62c3ce0e64191fff042612804766df61 [file] [log] [blame]
Ian Rogersef7d42f2014-01-06 12:55:46 -08001/*
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 Rogers44d6ff12014-03-06 23:11:11 -080021#include "thread-inl.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080022#include "monitor.h"
23
24namespace art {
25
Andreas Gampe74240812014-04-17 10:35:09 -070026namespace mirror {
27 class Object;
28} // namespace mirror
29
30MonitorPool::MonitorPool()
31 : num_chunks_(0), capacity_(0), first_free_(nullptr) {
32 AllocateChunk(); // Get our first chunk.
Ian Rogersef7d42f2014-01-06 12:55:46 -080033}
34
Andreas Gampe74240812014-04-17 10:35:09 -070035// Assumes locks are held appropriately when necessary.
36// We do not need a lock in the constructor, but we need one when in CreateMonitorInPool.
37void MonitorPool::AllocateChunk() {
38 DCHECK(first_free_ == nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -080039
Andreas Gampe74240812014-04-17 10:35:09 -070040 // Do we need to resize?
41 if (num_chunks_ == capacity_) {
42 if (capacity_ == 0U) {
43 // Initialization.
44 capacity_ = kInitialChunkStorage;
45 uintptr_t* new_backing = new uintptr_t[capacity_];
46 monitor_chunks_.StoreRelaxed(new_backing);
47 } else {
48 size_t new_capacity = 2 * capacity_;
49 uintptr_t* new_backing = new uintptr_t[new_capacity];
50 uintptr_t* old_backing = monitor_chunks_.LoadRelaxed();
51 memcpy(new_backing, old_backing, sizeof(uintptr_t) * capacity_);
52 monitor_chunks_.StoreRelaxed(new_backing);
53 capacity_ = new_capacity;
54 old_chunk_arrays_.push_back(old_backing);
Mathieu Chartier2c265012014-08-05 18:15:56 -070055 VLOG(monitor) << "Resizing to capacity " << capacity_;
Ian Rogersef7d42f2014-01-06 12:55:46 -080056 }
57 }
Andreas Gampe74240812014-04-17 10:35:09 -070058
59 // Allocate the chunk.
Mathieu Chartierbad02672014-08-25 13:08:22 -070060 void* chunk = allocator_.allocate(kChunkSize);
Andreas Gampe74240812014-04-17 10:35:09 -070061 // Check we allocated memory.
62 CHECK_NE(reinterpret_cast<uintptr_t>(nullptr), reinterpret_cast<uintptr_t>(chunk));
63 // Check it is aligned as we need it.
64 CHECK_EQ(0U, reinterpret_cast<uintptr_t>(chunk) % kMonitorAlignment);
65
66 // Add the chunk.
Mathieu Chartier2c265012014-08-05 18:15:56 -070067 *(monitor_chunks_.LoadRelaxed() + num_chunks_) = reinterpret_cast<uintptr_t>(chunk);
Andreas Gampe74240812014-04-17 10:35:09 -070068 num_chunks_++;
69
70 // Set up the free list
71 Monitor* last = reinterpret_cast<Monitor*>(reinterpret_cast<uintptr_t>(chunk) +
72 (kChunkCapacity - 1) * kAlignedMonitorSize);
73 last->next_free_ = nullptr;
74 // Eagerly compute id.
75 last->monitor_id_ = OffsetToMonitorId((num_chunks_ - 1) * kChunkSize +
76 (kChunkCapacity - 1) * kAlignedMonitorSize);
77 for (size_t i = 0; i < kChunkCapacity - 1; ++i) {
78 Monitor* before = reinterpret_cast<Monitor*>(reinterpret_cast<uintptr_t>(last) -
79 kAlignedMonitorSize);
80 before->next_free_ = last;
81 // Derive monitor_id from last.
82 before->monitor_id_ = OffsetToMonitorId(MonitorIdToOffset(last->monitor_id_) -
83 kAlignedMonitorSize);
84
85 last = before;
86 }
87 DCHECK(last == reinterpret_cast<Monitor*>(chunk));
88 first_free_ = last;
Ian Rogersef7d42f2014-01-06 12:55:46 -080089}
90
Andreas Gampe74240812014-04-17 10:35:09 -070091Monitor* MonitorPool::CreateMonitorInPool(Thread* self, Thread* owner, mirror::Object* obj,
92 int32_t hash_code)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
94 // We are gonna allocate, so acquire the writer lock.
95 MutexLock mu(self, *Locks::allocated_monitor_ids_lock_);
96
97 // Enough space, or need to resize?
98 if (first_free_ == nullptr) {
Mathieu Chartier2c265012014-08-05 18:15:56 -070099 VLOG(monitor) << "Allocating a new chunk.";
Andreas Gampe74240812014-04-17 10:35:09 -0700100 AllocateChunk();
101 }
102
103 Monitor* mon_uninitialized = first_free_;
104 first_free_ = first_free_->next_free_;
105
106 // Pull out the id which was preinitialized.
107 MonitorId id = mon_uninitialized->monitor_id_;
108
109 // Initialize it.
110 Monitor* monitor = new(mon_uninitialized) Monitor(self, owner, obj, hash_code, id);
111
112 return monitor;
113}
114
115void MonitorPool::ReleaseMonitorToPool(Thread* self, Monitor* monitor) {
116 // Might be racy with allocation, so acquire lock.
117 MutexLock mu(self, *Locks::allocated_monitor_ids_lock_);
118
119 // Keep the monitor id. Don't trust it's not cleared.
120 MonitorId id = monitor->monitor_id_;
121
122 // Call the destructor.
123 // TODO: Exception safety?
124 monitor->~Monitor();
125
126 // Add to the head of the free list.
127 monitor->next_free_ = first_free_;
128 first_free_ = monitor;
129
130 // Rewrite monitor id.
131 monitor->monitor_id_ = id;
132}
133
Mathieu Chartierbad02672014-08-25 13:08:22 -0700134void MonitorPool::ReleaseMonitorsToPool(Thread* self, MonitorList::Monitors* monitors) {
Andreas Gampe74240812014-04-17 10:35:09 -0700135 for (Monitor* mon : *monitors) {
136 ReleaseMonitorToPool(self, mon);
137 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138}
139
140} // namespace art