53 "This lock should %s have a safepoint check for Java threads: %s",
54 _safepoint_check_required ? "always" : "never", name());
55
56 // Also check NoSafepointVerifier, and thread state is _thread_in_vm
57 thread->check_for_valid_safepoint_state();
58 } else {
59 // If initialized with safepoint_check_never, a NonJavaThread should never ask to safepoint check either.
60 assert(_safepoint_check_required != _safepoint_check_never,
61 "NonJavaThread should not check for safepoint");
62 }
63 }
64
65 void Mutex::check_no_safepoint_state(Thread* thread) {
66 check_block_state(thread);
67 assert(!thread->is_active_Java_thread() || _safepoint_check_required != _safepoint_check_always,
68 "This lock should %s have a safepoint check for Java threads: %s",
69 _safepoint_check_required ? "always" : "never", name());
70 }
71 #endif // ASSERT
72
73 void Mutex::lock(Thread* self) {
74 check_safepoint_state(self);
75
76 assert(_owner != self, "invariant");
77
78 Mutex* in_flight_mutex = NULL;
79 DEBUG_ONLY(int retry_cnt = 0;)
80 bool is_active_Java_thread = self->is_active_Java_thread();
81 while (!_lock.try_lock()) {
82 // The lock is contended
83
84 #ifdef ASSERT
85 if (retry_cnt++ > 3) {
86 log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
87 }
88 #endif // ASSERT
89
90 // Is it a JavaThread participating in the safepoint protocol.
91 if (is_active_Java_thread) {
92 assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
93 { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex);
94 in_flight_mutex = this; // save for ~ThreadBlockInVMWithDeadlockCheck
95 _lock.lock();
96 }
97 if (in_flight_mutex != NULL) {
98 // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
99 break;
100 }
101 } else {
102 _lock.lock();
103 break;
104 }
105 }
106
107 assert_owner(NULL);
108 set_owner(self);
109 }
110
111 void Mutex::lock() {
112 this->lock(Thread::current());
113 }
114
115 // Lock without safepoint check - a degenerate variant of lock() for use by
116 // JavaThreads when it is known to be safe to not check for a safepoint when
117 // acquiring this lock. If the thread blocks acquiring the lock it is not
118 // safepoint-safe and so will prevent a safepoint from being reached. If used
119 // in the wrong way this can lead to a deadlock with the safepoint code.
120
121 void Mutex::lock_without_safepoint_check(Thread * self) {
122 check_no_safepoint_state(self);
123 assert(_owner != self, "invariant");
124 _lock.lock();
125 assert_owner(NULL);
126 set_owner(self);
127 }
128
129 void Mutex::lock_without_safepoint_check() {
130 lock_without_safepoint_check(Thread::current());
131 }
132
|
53 "This lock should %s have a safepoint check for Java threads: %s",
54 _safepoint_check_required ? "always" : "never", name());
55
56 // Also check NoSafepointVerifier, and thread state is _thread_in_vm
57 thread->check_for_valid_safepoint_state();
58 } else {
59 // If initialized with safepoint_check_never, a NonJavaThread should never ask to safepoint check either.
60 assert(_safepoint_check_required != _safepoint_check_never,
61 "NonJavaThread should not check for safepoint");
62 }
63 }
64
65 void Mutex::check_no_safepoint_state(Thread* thread) {
66 check_block_state(thread);
67 assert(!thread->is_active_Java_thread() || _safepoint_check_required != _safepoint_check_always,
68 "This lock should %s have a safepoint check for Java threads: %s",
69 _safepoint_check_required ? "always" : "never", name());
70 }
71 #endif // ASSERT
72
73 void Mutex::lock_contended(Thread* self) {
74 // The lock is contended
75 Mutex *in_flight_mutex = NULL;
76 DEBUG_ONLY(int retry_cnt = 0;)
77 bool is_active_Java_thread = self->is_active_Java_thread();
78 do {
79 #ifdef ASSERT
80 if (retry_cnt++ > 3) {
81 log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
82 }
83 #endif // ASSERT
84
85 // Is it a JavaThread participating in the safepoint protocol.
86 if (is_active_Java_thread) {
87 assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
88 {
89 ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex);
90 in_flight_mutex = this; // save for ~ThreadBlockInVMWithDeadlockCheck
91 _lock.lock();
92 }
93 if (in_flight_mutex != NULL) {
94 // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
95 break;
96 }
97 } else {
98 _lock.lock();
99 break;
100 }
101 } while (!_lock.try_lock());
102 }
103
104 void Mutex::lock(Thread* self) {
105 check_safepoint_state(self);
106
107 assert(_owner != self, "invariant");
108
109 if (!_lock.try_lock()) {
110 // The lock is contended, use contended slow-path function to lock
111 lock_contended(self);
112 }
113
114 assert_owner(NULL);
115 set_owner(self);
116 }
117
118 void Mutex::lock() {
119 lock(Thread::current());
120 }
121
122 // Lock without safepoint check - a degenerate variant of lock() for use by
123 // JavaThreads when it is known to be safe to not check for a safepoint when
124 // acquiring this lock. If the thread blocks acquiring the lock it is not
125 // safepoint-safe and so will prevent a safepoint from being reached. If used
126 // in the wrong way this can lead to a deadlock with the safepoint code.
127
128 void Mutex::lock_without_safepoint_check(Thread * self) {
129 check_no_safepoint_state(self);
130 assert(_owner != self, "invariant");
131 _lock.lock();
132 assert_owner(NULL);
133 set_owner(self);
134 }
135
136 void Mutex::lock_without_safepoint_check() {
137 lock_without_safepoint_check(Thread::current());
138 }
139
|