330 void HandshakeState::cancel_inner(JavaThread* thread) {
331 assert(Thread::current() == thread, "should call from thread");
332 assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
333 HandshakeOperation* op = _operation;
334 clear_handshake(thread);
335 if (op != NULL) {
336 op->cancel_handshake(thread);
337 }
338 }
339
340 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
341 // SafepointSynchronize::safepoint_safe() does not consider an externally
342 // suspended thread to be safe. However, this function must be called with
343 // the Threads_lock held so an externally suspended thread cannot be
344 // resumed thus it is safe.
345 assert(Threads_lock->owned_by_self(), "Not holding Threads_lock.");
346 return SafepointSynchronize::safepoint_safe(target, target->thread_state()) ||
347 target->is_ext_suspended();
348 }
349
350 bool HandshakeState::claim_handshake_for_vmthread() {
351 if (!_semaphore.trywait()) {
352 return false;
353 }
354 if (has_operation()) {
355 return true;
356 }
357 _semaphore.signal();
358 return false;
359 }
360
361 void HandshakeState::process_by_vmthread(JavaThread* target) {
362 assert(Thread::current()->is_VM_thread(), "should call from vm thread");
363
364 if (!has_operation()) {
365 // JT has already cleared its handshake
366 return;
367 }
368
369 if (!vmthread_can_process_handshake(target)) {
370 // JT is observed in an unsafe state, it must notice the handshake itself
371 return;
372 }
373
374 // Claim the semaphore if there still an operation to be executed.
375 if (!claim_handshake_for_vmthread()) {
376 return;
377 }
378
379 // If we own the semaphore at this point and while owning the semaphore
380 // can observe a safe state the thread cannot possibly continue without
381 // getting caught by the semaphore.
382 if (vmthread_can_process_handshake(target)) {
383 guarantee(!_semaphore.trywait(), "we should already own the semaphore");
384
385 _operation->do_handshake(target);
386 // Disarm after VM thread have executed the operation.
387 clear_handshake(target);
388 // Release the thread
389 }
|
330 void HandshakeState::cancel_inner(JavaThread* thread) {
331 assert(Thread::current() == thread, "should call from thread");
332 assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
333 HandshakeOperation* op = _operation;
334 clear_handshake(thread);
335 if (op != NULL) {
336 op->cancel_handshake(thread);
337 }
338 }
339
340 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
341 // SafepointSynchronize::safepoint_safe() does not consider an externally
342 // suspended thread to be safe. However, this function must be called with
343 // the Threads_lock held so an externally suspended thread cannot be
344 // resumed thus it is safe.
345 assert(Threads_lock->owned_by_self(), "Not holding Threads_lock.");
346 return SafepointSynchronize::safepoint_safe(target, target->thread_state()) ||
347 target->is_ext_suspended();
348 }
349
350 static bool possibly_vmthread_can_process_handshake(JavaThread* target) {
351 if (target->is_ext_suspended()) {
352 return true;
353 }
354 switch(target->thread_state()) {
355 case _thread_in_native:
356 // native threads are safe if they have no java stack or have walkable stack
357 return !target->has_last_Java_frame() || target->frame_anchor()->walkable();
358
359 case _thread_blocked:
360 return true;
361
362 default:
363 return false;
364 }
365 }
366
367 bool HandshakeState::claim_handshake_for_vmthread() {
368 if (!_semaphore.trywait()) {
369 return false;
370 }
371 if (has_operation()) {
372 return true;
373 }
374 _semaphore.signal();
375 return false;
376 }
377
378 void HandshakeState::process_by_vmthread(JavaThread* target) {
379 assert(Thread::current()->is_VM_thread(), "should call from vm thread");
380
381 if (!has_operation()) {
382 // JT has already cleared its handshake
383 return;
384 }
385
386 if (!possibly_vmthread_can_process_handshake(target)) {
387 // JT is observed in an unsafe state, it must notice the handshake itself
388 return;
389 }
390
391 // Claim the semaphore if there still an operation to be executed.
392 if (!claim_handshake_for_vmthread()) {
393 return;
394 }
395
396 // If we own the semaphore at this point and while owning the semaphore
397 // can observe a safe state the thread cannot possibly continue without
398 // getting caught by the semaphore.
399 if (vmthread_can_process_handshake(target)) {
400 guarantee(!_semaphore.trywait(), "we should already own the semaphore");
401
402 _operation->do_handshake(target);
403 // Disarm after VM thread have executed the operation.
404 clear_handshake(target);
405 // Release the thread
406 }
|