src/hotspot/share/utilities/spinYield.hpp
author pliden
Mon, 14 May 2018 15:42:59 +0200
changeset 50102 454fa295105c
parent 49718 713d9b03e990
child 53077 33b8f6f4cdf5
permissions -rw-r--r--
8202976: Add C1 lea patching support for x86 Reviewed-by: kvn, neliasso
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49718
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     1
/*
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     4
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     7
 * published by the Free Software Foundation.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     8
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    13
 * accompanied this code).
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    14
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    18
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    21
 * questions.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    22
 *
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    23
 */
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    24
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    25
#ifndef SHARE_UTILITIES_SPINYIELD_HPP
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    26
#define SHARE_UTILITIES_SPINYIELD_HPP
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    27
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    28
#include "memory/allocation.hpp"
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    29
#include "utilities/globalDefinitions.hpp"
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    30
#include "utilities/ticks.hpp"
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    31
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    32
class outputStream;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    33
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    34
extern "C" int SpinPause();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    35
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    36
class SpinYield : public StackObj {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    37
  Tickspan _sleep_time;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    38
  uint _spins;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    39
  uint _yields;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    40
  uint _spin_limit;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    41
  uint _yield_limit;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    42
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    43
  void yield_or_sleep();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    44
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    45
public:
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    46
  static const uint default_spin_limit = 4096;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    47
  static const uint default_yield_limit = 64;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    48
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    49
  // spin_limit is ignored (treated as zero) when !os::is_MP().
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    50
  explicit SpinYield(uint spin_limit = default_spin_limit,
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    51
                     uint yield_limit = default_yield_limit);
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    52
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    53
  // Perform next round of delay.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    54
  void wait() {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    55
    // Simple policy: return immediately (spinning) configured number
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    56
    // of times, then switch to yield/sleep.  Future work might
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    57
    // provide other policies, such as (1) always spin if system is
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    58
    // not saturated, or (2) sleeping if yielding is ineffective.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    59
    if (_spins < _spin_limit) {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    60
      ++_spins;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    61
      SpinPause();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    62
    } else {
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    63
      yield_or_sleep();
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    64
    }
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    65
  }
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    66
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    67
  // Write information about the wait duration to s.
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    68
  void report(outputStream* s) const;
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    69
};
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    70
713d9b03e990 8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff changeset
    71
#endif // SHARE_UTILITIES_SPINYIELD_HPP