1 /*
2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDBITS_INLINE_HPP
26 #define SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDBITS_INLINE_HPP
27 
28 #include "jfr/utilities/jfrTypes.hpp"
29 #include "runtime/atomic.hpp"
30 #include "utilities/macros.hpp"
31 
32 #ifdef VM_LITTLE_ENDIAN
33 static const int low_offset = 0;
34 static const int meta_offset = low_offset + 1;
35 #else
36 static const int low_offset = 7;
37 static const int meta_offset = low_offset - 1;
38 #endif
39 
set_bits(jbyte bits,jbyte volatile * const dest)40 inline void set_bits(jbyte bits, jbyte volatile* const dest) {
41   assert(dest != NULL, "invariant");
42   if (bits != (*dest & bits)) {
43     *dest |= bits;
44   }
45 }
46 
traceid_and(jbyte current,jbyte bits)47 inline jbyte traceid_and(jbyte current, jbyte bits) {
48   return current & bits;
49 }
50 
traceid_or(jbyte current,jbyte bits)51 inline jbyte traceid_or(jbyte current, jbyte bits) {
52   return current | bits;
53 }
54 
traceid_xor(jbyte current,jbyte bits)55 inline jbyte traceid_xor(jbyte current, jbyte bits) {
56   return current ^ bits;
57 }
58 
59 template <jbyte op(jbyte, jbyte)>
set_bits_cas_form(jbyte bits,jbyte * const dest)60 inline void set_bits_cas_form(jbyte bits, jbyte* const dest) {
61   assert(dest != NULL, "invariant");
62   do {
63     const jbyte current = *dest;
64     const jbyte new_value = op(current, bits);
65     if (Atomic::cmpxchg(new_value, dest, current) == current) {
66       return;
67     }
68   } while (true);
69 }
70 
set_bits_cas(jbyte bits,jbyte * const dest)71 inline void set_bits_cas(jbyte bits, jbyte* const dest) {
72   set_bits_cas_form<traceid_or>(bits, dest);
73 }
74 
clear_bits_cas(jbyte bits,jbyte * const dest)75 inline void clear_bits_cas(jbyte bits, jbyte* const dest) {
76   set_bits_cas_form<traceid_xor>(bits, dest);
77 }
78 
set_mask(jbyte mask,jbyte * const dest)79 inline void set_mask(jbyte mask, jbyte* const dest) {
80   set_bits_cas_form<traceid_and>(mask, dest);
81 }
82 
set_traceid_bits(jbyte bits,traceid * dest)83 inline void set_traceid_bits(jbyte bits, traceid* dest) {
84   set_bits(bits, ((jbyte*)dest) + low_offset);
85 }
86 
set_traceid_bits_cas(jbyte bits,traceid * dest)87 inline void set_traceid_bits_cas(jbyte bits, traceid* dest) {
88   set_bits_cas(bits, ((jbyte*)dest) + low_offset);
89 }
90 
set_traceid_mask(jbyte mask,traceid * dest)91 inline void set_traceid_mask(jbyte mask, traceid* dest) {
92   set_mask(mask, ((jbyte*)dest) + low_offset);
93 }
94 
set_meta_bits(jbyte bits,jbyte * const dest)95 inline void set_meta_bits(jbyte bits, jbyte* const dest) {
96   assert(dest != NULL, "invariant");
97   *dest |= bits;
98 }
99 
set_traceid_meta_bits(jbyte bits,traceid * dest)100 inline void set_traceid_meta_bits(jbyte bits, traceid* dest) {
101   set_meta_bits(bits, ((jbyte*)dest) + meta_offset);
102 }
103 
set_meta_mask(jbyte mask,jbyte * const dest)104 inline void set_meta_mask(jbyte mask, jbyte* const dest) {
105   assert(dest != NULL, "invariant");
106   *dest &= mask;
107 }
108 
set_traceid_meta_mask(jbyte mask,traceid * dest)109 inline void set_traceid_meta_mask(jbyte mask, traceid* dest) {
110   set_meta_mask(mask, ((jbyte*)dest) + meta_offset);
111 }
112 
113 // only used by a single thread with no visibility requirements
clear_meta_bits(jbyte bits,jbyte * const dest)114 inline void clear_meta_bits(jbyte bits, jbyte* const dest) {
115   assert(dest != NULL, "invariant");
116   *dest ^= bits;
117 }
118 
119 #endif // SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDBITS_INLINE_HPP
120