• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.perfmark.java9;
18 
19 import io.perfmark.impl.Generator;
20 import java.lang.invoke.MethodHandles;
21 import java.lang.invoke.VarHandle;
22 
23 final class SecretVarHandleGenerator {
24 
25   /**
26    * This class let's PerfMark have fairly low overhead detection if it is enabled, with reasonable
27    * time between enabled and other threads noticing. Since this uses Java 9 APIs, it may not be
28    * available.
29    */
30   public static final class VarHandleGenerator extends Generator {
31 
32     private static final VarHandle GEN;
33 
34     static {
35       try {
36         GEN = MethodHandles.lookup().findVarHandle(VarHandleGenerator.class, "gen", long.class);
37       } catch (RuntimeException e) {
38         throw e;
39       } catch (Exception e) {
40         throw new RuntimeException(e);
41       }
42     }
43 
VarHandleGenerator()44     public VarHandleGenerator() {}
45 
46     @SuppressWarnings("unused")
47     private long gen;
48 
49     @Override
setGeneration(long generation)50     public void setGeneration(long generation) {
51       GEN.setRelease(this, generation);
52     }
53 
54     @Override
getGeneration()55     public long getGeneration() {
56       return (long) GEN.getAcquire(this);
57     }
58 
59     @Override
costOfSetNanos()60     public long costOfSetNanos() {
61       return 3;
62     }
63 
64     @Override
costOfGetNanos()65     public long costOfGetNanos() {
66       return 2;
67     }
68   }
69 
SecretVarHandleGenerator()70   private SecretVarHandleGenerator() {}
71 }
72