src/hotspot/share/runtime/handshake.hpp
author kbarrett
Thu, 04 Jan 2018 18:18:18 -0500
changeset 48488 51825789dd89
parent 47881 0ce0ac68ace7
child 49364 601146c66cad
permissions -rw-r--r--
8194406: Use Atomic::replace_if_null Reviewed-by: coleenp, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47881
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     1
/*
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     4
 *
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     8
 *
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    13
 * accompanied this code).
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    14
 *
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    18
 *
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    21
 * questions.
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    22
 *
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    23
 */
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    24
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    25
#ifndef SHARE_VM_RUNTIME_HANDSHAKE_HPP
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    26
#define SHARE_VM_RUNTIME_HANDSHAKE_HPP
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    27
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    28
#include "memory/allocation.hpp"
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    29
#include "runtime/semaphore.hpp"
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    30
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    31
class ThreadClosure;
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    32
class JavaThread;
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    33
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    34
// A handshake operation is a callback that is executed for each JavaThread
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    35
// while that thread is in a safepoint safe state. The callback is executed
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    36
// either by the thread itself or by the VM thread while keeping the thread
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    37
// in a blocked state. A handshake can be performed with a single
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    38
// JavaThread as well.
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    39
class Handshake : public AllStatic {
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    40
 public:
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    41
  // Execution of handshake operation
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    42
  static void execute(ThreadClosure* thread_cl);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    43
  static bool execute(ThreadClosure* thread_cl, JavaThread* target);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    44
};
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    45
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    46
class HandshakeOperation;
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    47
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    48
// The HandshakeState keep tracks of an ongoing handshake for one JavaThread.
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    49
// VM thread and JavaThread are serialized with the semaphore making sure
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    50
// the operation is only done by either VM thread on behalf of the JavaThread
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    51
// or the JavaThread itself.
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    52
class HandshakeState VALUE_OBJ_CLASS_SPEC {
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    53
  HandshakeOperation* volatile _operation;
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    54
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    55
  Semaphore _semaphore;
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    56
  bool _vmthread_holds_semaphore;
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    57
  bool _thread_in_process_handshake;
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    58
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    59
  bool claim_handshake_for_vmthread();
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    60
  bool vmthread_can_process_handshake(JavaThread* target);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    61
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    62
  void clear_handshake(JavaThread* thread);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    63
  void cancel_inner(JavaThread* thread);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    64
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    65
  void process_self_inner(JavaThread* thread);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    66
public:
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    67
  HandshakeState();
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    68
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    69
  void set_operation(JavaThread* thread, HandshakeOperation* op);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    70
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    71
  bool has_operation() const {
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    72
    return _operation != NULL;
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    73
  }
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    74
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    75
  void cancel(JavaThread* thread) {
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    76
    if (!_thread_in_process_handshake) {
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    77
      FlagSetting fs(_thread_in_process_handshake, true);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    78
      cancel_inner(thread);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    79
    }
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    80
  }
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    81
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    82
  void process_by_self(JavaThread* thread) {
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    83
    if (!_thread_in_process_handshake) {
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    84
      FlagSetting fs(_thread_in_process_handshake, true);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    85
      process_self_inner(thread);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    86
    }
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    87
  }
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    88
  void process_by_vmthread(JavaThread* target);
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    89
};
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    90
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
diff changeset
    91
#endif // SHARE_VM_RUNTIME_HANDSHAKE_HPP