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 leakp_offset = low_offset + 1; 35 #else 36 static const int low_offset = 7; 37 static const int leakp_offset = low_offset - 1; 38 #endif 39 40 inline void set_bits(jbyte bits, jbyte* const dest) { 41 assert(dest != NULL, "invariant"); 42 if (bits != (*dest & bits)) { 43 *dest |= bits; 44 } 45 } 46 47 inline jbyte traceid_and(jbyte current, jbyte bits) { 48 return current & bits; 49 } 50 51 inline jbyte traceid_or(jbyte current, jbyte bits) { 52 return current | bits; 53 } 54 55 inline jbyte traceid_xor(jbyte current, jbyte bits) { 56 return current ^ bits; 57 } 58 59 template <jbyte op(jbyte, jbyte)> 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 71 inline void set_bits_cas(jbyte bits, jbyte* const dest) { 72 set_bits_cas_form<traceid_or>(bits, dest); 73 } 74 75 inline void clear_bits_cas(jbyte bits, jbyte* const dest) { 76 set_bits_cas_form<traceid_xor>(bits, dest); 77 } 78 79 inline void set_mask(jbyte mask, jbyte* const dest) { 80 set_bits_cas_form<traceid_and>(mask, dest); 81 } 82 83 inline void set_traceid_bits(jbyte bits, traceid* dest) { 84 set_bits(bits, ((jbyte*)dest) + low_offset); 85 } 86 87 inline void set_traceid_bits_cas(jbyte bits, traceid* dest) { 88 set_bits_cas(bits, ((jbyte*)dest) + low_offset); 89 } 90 91 inline void set_traceid_mask(jbyte mask, traceid* dest) { 92 set_mask(mask, ((jbyte*)dest) + low_offset); 93 } 94 95 inline void set_leakp_traceid_bits(jbyte bits, traceid* dest) { 96 set_bits(bits, ((jbyte*)dest) + leakp_offset); 97 } 98 99 inline void set_leakp_traceid_bits_cas(jbyte bits, traceid* dest) { 100 set_bits_cas(bits, ((jbyte*)dest) + leakp_offset); 101 } 102 103 inline void set_leakp_traceid_mask(jbyte mask, traceid* dest) { 104 set_mask(mask, ((jbyte*)dest) + leakp_offset); 105 } 106 107 #endif // SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDBITS_INLINE_HPP