Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [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 | #ifndef ART_RUNTIME_TRANSACTION_H_ |
| 18 | #define ART_RUNTIME_TRANSACTION_H_ |
| 19 | |
| 20 | #include "base/macros.h" |
| 21 | #include "base/mutex.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 22 | #include "object_callbacks.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 23 | #include "offsets.h" |
| 24 | #include "primitive.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 25 | #include "safe_map.h" |
| 26 | |
| 27 | #include <list> |
| 28 | #include <map> |
| 29 | |
| 30 | namespace art { |
| 31 | namespace mirror { |
| 32 | class Array; |
| 33 | class Object; |
| 34 | class String; |
| 35 | } |
| 36 | class InternTable; |
| 37 | |
| 38 | class Transaction { |
| 39 | public: |
| 40 | Transaction(); |
| 41 | ~Transaction(); |
| 42 | |
| 43 | // Record object field changes. |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame^] | 44 | void RecordWriteFieldBoolean(mirror::Object* obj, MemberOffset field_offset, uint8_t value, |
| 45 | bool is_volatile) |
| 46 | LOCKS_EXCLUDED(log_lock_); |
| 47 | void RecordWriteFieldByte(mirror::Object* obj, MemberOffset field_offset, int8_t value, |
| 48 | bool is_volatile) |
| 49 | LOCKS_EXCLUDED(log_lock_); |
| 50 | void RecordWriteFieldChar(mirror::Object* obj, MemberOffset field_offset, uint16_t value, |
| 51 | bool is_volatile) |
| 52 | LOCKS_EXCLUDED(log_lock_); |
| 53 | void RecordWriteFieldShort(mirror::Object* obj, MemberOffset field_offset, int16_t value, |
| 54 | bool is_volatile) |
| 55 | LOCKS_EXCLUDED(log_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 56 | void RecordWriteField32(mirror::Object* obj, MemberOffset field_offset, uint32_t value, |
| 57 | bool is_volatile) |
| 58 | LOCKS_EXCLUDED(log_lock_); |
| 59 | void RecordWriteField64(mirror::Object* obj, MemberOffset field_offset, uint64_t value, |
| 60 | bool is_volatile) |
| 61 | LOCKS_EXCLUDED(log_lock_); |
| 62 | void RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset, |
| 63 | mirror::Object* value, bool is_volatile) |
| 64 | LOCKS_EXCLUDED(log_lock_); |
| 65 | |
| 66 | // Record array change. |
| 67 | void RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) |
| 68 | LOCKS_EXCLUDED(log_lock_) |
| 69 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 70 | |
| 71 | // Record intern string table changes. |
| 72 | void RecordStrongStringInsertion(mirror::String* s, uint32_t hash_code) |
| 73 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_) |
| 74 | LOCKS_EXCLUDED(log_lock_); |
| 75 | void RecordWeakStringInsertion(mirror::String* s, uint32_t hash_code) |
| 76 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_) |
| 77 | LOCKS_EXCLUDED(log_lock_); |
| 78 | void RecordStrongStringRemoval(mirror::String* s, uint32_t hash_code) |
| 79 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_) |
| 80 | LOCKS_EXCLUDED(log_lock_); |
| 81 | void RecordWeakStringRemoval(mirror::String* s, uint32_t hash_code) |
| 82 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_) |
| 83 | LOCKS_EXCLUDED(log_lock_); |
| 84 | |
| 85 | // Abort transaction by undoing all recorded changes. |
| 86 | void Abort() |
| 87 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 88 | LOCKS_EXCLUDED(log_lock_); |
| 89 | |
| 90 | void VisitRoots(RootCallback* callback, void* arg) |
| 91 | LOCKS_EXCLUDED(log_lock_) |
| 92 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 93 | |
| 94 | private: |
| 95 | class ObjectLog { |
| 96 | public: |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame^] | 97 | void LogBooleanValue(MemberOffset offset, uint8_t value, bool is_volatile); |
| 98 | void LogByteValue(MemberOffset offset, int8_t value, bool is_volatile); |
| 99 | void LogCharValue(MemberOffset offset, uint16_t value, bool is_volatile); |
| 100 | void LogShortValue(MemberOffset offset, int16_t value, bool is_volatile); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 101 | void Log32BitsValue(MemberOffset offset, uint32_t value, bool is_volatile); |
| 102 | void Log64BitsValue(MemberOffset offset, uint64_t value, bool is_volatile); |
| 103 | void LogReferenceValue(MemberOffset offset, mirror::Object* obj, bool is_volatile); |
| 104 | |
| 105 | void Undo(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 106 | void VisitRoots(RootCallback* callback, void* arg); |
| 107 | |
| 108 | size_t Size() const { |
| 109 | return field_values_.size(); |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | enum FieldValueKind { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame^] | 114 | kBoolean, |
| 115 | kByte, |
| 116 | kChar, |
| 117 | kShort, |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 118 | k32Bits, |
| 119 | k64Bits, |
| 120 | kReference |
| 121 | }; |
| 122 | struct FieldValue { |
| 123 | // TODO use JValue instead ? |
| 124 | uint64_t value; |
| 125 | FieldValueKind kind; |
| 126 | bool is_volatile; |
| 127 | }; |
| 128 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame^] | 129 | void LogValue(FieldValueKind kind, MemberOffset offset, uint64_t value, bool is_volatile); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 130 | void UndoFieldWrite(mirror::Object* obj, MemberOffset field_offset, |
| 131 | const FieldValue& field_value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 132 | |
| 133 | // Maps field's offset to its value. |
| 134 | std::map<uint32_t, FieldValue> field_values_; |
| 135 | }; |
| 136 | |
| 137 | class ArrayLog { |
| 138 | public: |
| 139 | void LogValue(size_t index, uint64_t value); |
| 140 | |
| 141 | void Undo(mirror::Array* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 142 | |
| 143 | size_t Size() const { |
| 144 | return array_values_.size(); |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | void UndoArrayWrite(mirror::Array* array, Primitive::Type array_type, size_t index, |
| 149 | uint64_t value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 150 | |
| 151 | // Maps index to value. |
| 152 | // TODO use JValue instead ? |
| 153 | std::map<size_t, uint64_t> array_values_; |
| 154 | }; |
| 155 | |
| 156 | class InternStringLog { |
| 157 | public: |
| 158 | enum StringKind { |
| 159 | kStrongString, |
| 160 | kWeakString |
| 161 | }; |
| 162 | enum StringOp { |
| 163 | kInsert, |
| 164 | kRemove |
| 165 | }; |
| 166 | InternStringLog(mirror::String* s, uint32_t hash_code, StringKind kind, StringOp op) |
| 167 | : str_(s), hash_code_(hash_code), string_kind_(kind), string_op_(op) { |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 168 | DCHECK(s != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 171 | void Undo(InternTable* intern_table) |
| 172 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 173 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 174 | void VisitRoots(RootCallback* callback, void* arg); |
| 175 | |
| 176 | private: |
| 177 | mirror::String* str_; |
| 178 | uint32_t hash_code_; |
| 179 | StringKind string_kind_; |
| 180 | StringOp string_op_; |
| 181 | }; |
| 182 | |
| 183 | void LogInternedString(InternStringLog& log) |
| 184 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_) |
| 185 | LOCKS_EXCLUDED(log_lock_); |
| 186 | |
| 187 | void UndoObjectModifications() |
| 188 | EXCLUSIVE_LOCKS_REQUIRED(log_lock_) |
| 189 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 190 | void UndoArrayModifications() |
| 191 | EXCLUSIVE_LOCKS_REQUIRED(log_lock_) |
| 192 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 193 | void UndoInternStringTableModifications() |
| 194 | EXCLUSIVE_LOCKS_REQUIRED(Locks::intern_table_lock_) |
Hiroshi Yamauchi | 1bd4872 | 2014-05-23 19:58:15 -0700 | [diff] [blame] | 195 | EXCLUSIVE_LOCKS_REQUIRED(log_lock_) |
| 196 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 197 | |
| 198 | void VisitObjectLogs(RootCallback* callback, void* arg) |
| 199 | EXCLUSIVE_LOCKS_REQUIRED(log_lock_) |
| 200 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 201 | void VisitArrayLogs(RootCallback* callback, void* arg) |
| 202 | EXCLUSIVE_LOCKS_REQUIRED(log_lock_) |
| 203 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 204 | void VisitStringLogs(RootCallback* callback, void* arg) |
| 205 | EXCLUSIVE_LOCKS_REQUIRED(log_lock_) |
| 206 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 207 | |
| 208 | Mutex log_lock_ ACQUIRED_AFTER(Locks::intern_table_lock_); |
| 209 | std::map<mirror::Object*, ObjectLog> object_logs_ GUARDED_BY(log_lock_); |
| 210 | std::map<mirror::Array*, ArrayLog> array_logs_ GUARDED_BY(log_lock_); |
| 211 | std::list<InternStringLog> intern_string_logs_ GUARDED_BY(log_lock_); |
| 212 | |
| 213 | DISALLOW_COPY_AND_ASSIGN(Transaction); |
| 214 | }; |
| 215 | |
| 216 | } // namespace art |
| 217 | |
| 218 | #endif // ART_RUNTIME_TRANSACTION_H_ |