author | rehn |
Thu, 20 Dec 2018 10:05:19 +0100 | |
changeset 53077 | 33b8f6f4cdf5 |
parent 49718 | 713d9b03e990 |
permissions | -rw-r--r-- |
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; |
53077 | 42 |
uint _sleep_ns; |
49718
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
43 |
|
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
44 |
void yield_or_sleep(); |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
45 |
|
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
46 |
public: |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
47 |
static const uint default_spin_limit = 4096; |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
48 |
static const uint default_yield_limit = 64; |
53077 | 49 |
static const uint default_sleep_ns = 1000; |
49718
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
50 |
|
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
51 |
// 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
|
52 |
explicit SpinYield(uint spin_limit = default_spin_limit, |
53077 | 53 |
uint yield_limit = default_yield_limit, |
54 |
uint sleep_ns = default_sleep_ns); |
|
49718
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
55 |
|
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
56 |
// Perform next round of delay. |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
57 |
void wait() { |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
58 |
// Simple policy: return immediately (spinning) configured number |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
59 |
// 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
|
60 |
// 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
|
61 |
// 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
|
62 |
if (_spins < _spin_limit) { |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
63 |
++_spins; |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
64 |
SpinPause(); |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
65 |
} else { |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
66 |
yield_or_sleep(); |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
67 |
} |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
68 |
} |
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 |
// Write information about the wait duration to s. |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
71 |
void report(outputStream* s) const; |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
72 |
}; |
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
73 |
|
713d9b03e990
8200697: Add utility for spin wait with fallback to yield/sleep
kbarrett
parents:
diff
changeset
|
74 |
#endif // SHARE_UTILITIES_SPINYIELD_HPP |