Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: runtime/vm/simulator_arm.cc

Issue 677193002: - Use the simulator to do atomic operations that could also (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include <setjmp.h> 5 #include <setjmp.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 7
8 #include "vm/globals.h" 8 #include "vm/globals.h"
9 #if defined(TARGET_ARCH_ARM) 9 #if defined(TARGET_ARCH_ARM)
10 10
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 offset += len; 671 offset += len;
672 } 672 }
673 ASSERT(result != NULL); 673 ASSERT(result != NULL);
674 result[offset] = '\0'; 674 result[offset] = '\0';
675 return result; 675 return result;
676 } 676 }
677 677
678 678
679 // Synchronization primitives support. 679 // Synchronization primitives support.
680 Mutex* Simulator::exclusive_access_lock_ = NULL; 680 Mutex* Simulator::exclusive_access_lock_ = NULL;
681 Simulator::AddressTag Simulator::exclusive_access_state_[kNumAddressTags]; 681 Simulator::AddressTag Simulator::exclusive_access_state_[kNumAddressTags] =
682 int Simulator::next_address_tag_; 682 {{NULL, 0}};
683 int Simulator::next_address_tag_ = 0;
683 684
684 685
685 void Simulator::SetExclusiveAccess(uword addr) { 686 void Simulator::InitOnce() {
686 Isolate* isolate = Isolate::Current(); 687 // Setup exclusive access state lock.
687 ASSERT(isolate != NULL); 688 exclusive_access_lock_ = new Mutex();
688 int i = 0;
689 while ((i < kNumAddressTags) &&
690 (exclusive_access_state_[i].isolate != isolate)) {
691 i++;
692 }
693 if (i == kNumAddressTags) {
694 i = next_address_tag_;
695 if (++next_address_tag_ == kNumAddressTags) next_address_tag_ = 0;
696 exclusive_access_state_[i].isolate = isolate;
697 }
698 exclusive_access_state_[i].addr = addr;
699 } 689 }
700 690
701 691
702 bool Simulator::HasExclusiveAccessAndOpen(uword addr) {
703 Isolate* isolate = Isolate::Current();
704 ASSERT(isolate != NULL);
705 bool result = false;
706 for (int i = 0; i < kNumAddressTags; i++) {
707 if (exclusive_access_state_[i].isolate == isolate) {
708 if (exclusive_access_state_[i].addr == addr) {
709 result = true;
710 }
711 exclusive_access_state_[i].addr = NULL;
712 continue;
713 }
714 if (exclusive_access_state_[i].addr == addr) {
715 exclusive_access_state_[i].addr = NULL;
716 }
717 }
718 return result;
719 }
720
721
722 void Simulator::InitOnce() {
723 // Setup exclusive access state.
724 exclusive_access_lock_ = new Mutex();
725 for (int i = 0; i < kNumAddressTags; i++) {
726 exclusive_access_state_[i].isolate = NULL;
727 exclusive_access_state_[i].addr = NULL;
728 }
729 next_address_tag_ = 0;
730 }
731
732
733 Simulator::Simulator() { 692 Simulator::Simulator() {
734 // Setup simulator support first. Some of this information is needed to 693 // Setup simulator support first. Some of this information is needed to
735 // setup the architecture state. 694 // setup the architecture state.
736 // We allocate the stack here, the size is computed as the sum of 695 // We allocate the stack here, the size is computed as the sum of
737 // the size specified by the user and the buffer space needed for 696 // the size specified by the user and the buffer space needed for
738 // handling stack overflow exceptions. To be safe in potential 697 // handling stack overflow exceptions. To be safe in potential
739 // stack underflows we also add some underflow buffer space. 698 // stack underflows we also add some underflow buffer space.
740 stack_ = new char[(Isolate::GetSpecifiedStackSize() + 699 stack_ = new char[(Isolate::GetSpecifiedStackSize() +
741 Isolate::kStackSizeBuffer + 700 Isolate::kStackSizeBuffer +
742 kSimulatorStackUnderflowSize)]; 701 kSimulatorStackUnderflowSize)];
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 1064
1106 void Simulator::WriteB(uword addr, uint8_t value) { 1065 void Simulator::WriteB(uword addr, uint8_t value) {
1107 static StatsCounter counter_write_b("Simulated byte writes"); 1066 static StatsCounter counter_write_b("Simulated byte writes");
1108 counter_write_b.Increment(); 1067 counter_write_b.Increment();
1109 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr); 1068 uint8_t* ptr = reinterpret_cast<uint8_t*>(addr);
1110 *ptr = value; 1069 *ptr = value;
1111 } 1070 }
1112 1071
1113 1072
1114 // Synchronization primitives support. 1073 // Synchronization primitives support.
1074 void Simulator::SetExclusiveAccess(uword addr) {
1075 Isolate* isolate = Isolate::Current();
1076 ASSERT(isolate != NULL);
1077 int i = 0;
1078 // Find an entry for this isolate in the exclusive access state.
1079 while ((i < kNumAddressTags) &&
1080 (exclusive_access_state_[i].isolate != isolate)) {
1081 i++;
1082 }
1083 // Round-robin replacement of previously used entries.
1084 if (i == kNumAddressTags) {
1085 i = next_address_tag_;
1086 if (++next_address_tag_ == kNumAddressTags) {
1087 next_address_tag_ = 0;
1088 }
1089 exclusive_access_state_[i].isolate = isolate;
zra 2014/10/27 14:53:12 Should we assert here that exclusive_access_state_
Ivan Posva 2014/10/27 17:58:24 You cannot assert this. As it is perfectly legal t
zra 2014/10/27 18:09:17 What happens when you run out of address tags?
1090 }
1091 // Remember the address being reserved.
1092 exclusive_access_state_[i].addr = addr;
1093 }
1094
1095
1096 bool Simulator::HasExclusiveAccessAndOpen(uword addr) {
1097 Isolate* isolate = Isolate::Current();
1098 ASSERT(isolate != NULL);
1099 ASSERT(addr != 0);
1100 bool result = false;
1101 for (int i = 0; i < kNumAddressTags; i++) {
1102 if (exclusive_access_state_[i].isolate == isolate) {
1103 // Check whether the current isolates address reservation matches.
zra 2014/10/27 14:53:12 isolate's
Ivan Posva 2014/10/27 17:58:23 Done.
1104 if (exclusive_access_state_[i].addr == addr) {
1105 result = true;
1106 }
1107 exclusive_access_state_[i].addr = 0;
1108 } else if (exclusive_access_state_[i].addr == addr) {
1109 // Other isolates with matching address lose their reservations.
1110 exclusive_access_state_[i].addr = 0;
1111 }
1112 }
1113 return result;
1114 }
1115
1116
1115 void Simulator::ClearExclusive() { 1117 void Simulator::ClearExclusive() {
1116 // This lock is initialized in Simulator::InitOnce().
1117 MutexLocker ml(exclusive_access_lock_); 1118 MutexLocker ml(exclusive_access_lock_);
1118 // Set exclusive access to open state for this isolate. 1119 // Remove the reservation for this isolate.
1119 HasExclusiveAccessAndOpen(NULL); 1120 SetExclusiveAccess(NULL);
1120 } 1121 }
1121 1122
1122 1123
1123 intptr_t Simulator::ReadExclusiveW(uword addr, Instr* instr) { 1124 intptr_t Simulator::ReadExclusiveW(uword addr, Instr* instr) {
1124 // This lock is initialized in Simulator::InitOnce().
1125 MutexLocker ml(exclusive_access_lock_); 1125 MutexLocker ml(exclusive_access_lock_);
1126 SetExclusiveAccess(addr); 1126 SetExclusiveAccess(addr);
1127 return ReadW(addr, instr); 1127 return ReadW(addr, instr);
1128 } 1128 }
1129 1129
1130 1130
1131 intptr_t Simulator::WriteExclusiveW(uword addr, intptr_t value, Instr* instr) { 1131 intptr_t Simulator::WriteExclusiveW(uword addr, intptr_t value, Instr* instr) {
1132 // This lock is initialized in Simulator::InitOnce().
1133 MutexLocker ml(exclusive_access_lock_); 1132 MutexLocker ml(exclusive_access_lock_);
1134 bool write_allowed = HasExclusiveAccessAndOpen(addr); 1133 bool write_allowed = HasExclusiveAccessAndOpen(addr);
1135 if (write_allowed) { 1134 if (write_allowed) {
1136 WriteW(addr, value, instr); 1135 WriteW(addr, value, instr);
1137 return 0; // Success. 1136 return 0; // Success.
1138 } 1137 }
1139 return 1; // Failure. 1138 return 1; // Failure.
1140 } 1139 }
1141 1140
1142 1141
1143 uword Simulator::CompareExchange(uword* address, 1142 uword Simulator::CompareExchange(uword* address,
1144 uword compare_value, 1143 uword compare_value,
1145 uword new_value) { 1144 uword new_value) {
1146 // This lock is initialized in Simulator::InitOnce(). 1145 // This lock is initialized in Simulator::InitOnce().
koda 2014/10/27 14:05:10 Why keep this comment but remove the other two ins
Ivan Posva 2014/10/27 17:58:24 Done. Oversight, thanks for noticing.
1147 MutexLocker ml(exclusive_access_lock_); 1146 MutexLocker ml(exclusive_access_lock_);
1147 // We do not check for a reservation as it would be guaranteed to be found. No
koda 2014/10/27 14:05:10 Why not ASSERT this then?
Ivan Posva 2014/10/27 17:58:23 Maybe the comment needs rephrasing: What it is sup
koda 2014/10/27 18:32:48 Then you should assert that the current isolate ho
Ivan Posva 2014/10/27 19:21:08 Done.
1148 // other isolate is able to make a reservation while we hold the lock.
1148 uword value = *address; 1149 uword value = *address;
1149 if (value == compare_value) { 1150 if (value == compare_value) {
1150 *address = new_value; 1151 *address = new_value;
1151 // Same effect on exclusive access state as a successful STREX. 1152 // Same effect on exclusive access state as a successful STREX.
1152 HasExclusiveAccessAndOpen(reinterpret_cast<uword>(address)); 1153 HasExclusiveAccessAndOpen(reinterpret_cast<uword>(address));
1153 } else { 1154 } else {
1154 // Same effect on exclusive access state as an LDREX. 1155 // Same effect on exclusive access state as an LDREX.
1155 SetExclusiveAccess(reinterpret_cast<uword>(address)); 1156 SetExclusiveAccess(reinterpret_cast<uword>(address));
1156 } 1157 }
1157 return value; 1158 return value;
(...skipping 2709 matching lines...) Expand 10 before | Expand all | Expand 10 after
3867 set_register(kExceptionObjectReg, bit_cast<int32_t>(raw_exception)); 3868 set_register(kExceptionObjectReg, bit_cast<int32_t>(raw_exception));
3868 set_register(kStackTraceObjectReg, bit_cast<int32_t>(raw_stacktrace)); 3869 set_register(kStackTraceObjectReg, bit_cast<int32_t>(raw_stacktrace));
3869 buf->Longjmp(); 3870 buf->Longjmp();
3870 } 3871 }
3871 3872
3872 } // namespace dart 3873 } // namespace dart
3873 3874
3874 #endif // !defined(HOST_ARCH_ARM) 3875 #endif // !defined(HOST_ARCH_ARM)
3875 3876
3876 #endif // defined TARGET_ARCH_ARM 3877 #endif // defined TARGET_ARCH_ARM
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698