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 "runtime/orderAccess.hpp"
  31 #include "utilities/macros.hpp"
  32 
  33 #ifdef VM_LITTLE_ENDIAN
  34 static const int low_offset = 0;
  35 static const int leakp_offset = low_offset + 1;
  36 #else
  37 static const int low_offset = 7;
  38 static const int leakp_offset = low_offset - 1;
  39 #endif
  40 
  41 inline void set_bits(jbyte bits, jbyte* const dest) {
  42   assert(dest != NULL, "invariant");
  43   if (bits != (*dest & bits)) {
  44     *dest |= bits;
  45   }
  46 }
  47 
  48 inline jbyte traceid_and(jbyte current, jbyte bits) {
  49   return current & bits;
  50 }
  51 
  52 inline jbyte traceid_or(jbyte current, jbyte bits) {
  53   return current | bits;
  54 }
  55 
  56 inline jbyte traceid_xor(jbyte current, jbyte bits) {
  57   return current ^ bits;
  58 }
  59 
  60 template <jbyte op(jbyte, jbyte)>
  61 inline void set_bits_cas_form(jbyte bits, jbyte* const dest) {
  62   assert(dest != NULL, "invariant");
  63   do {
  64     const jbyte current = OrderAccess::load_acquire(dest);
  65     const jbyte new_value = op(current, bits);
  66     if (Atomic::cmpxchg(new_value, dest, current) == current) {
  67       return;
  68     }
  69   } while (true);
  70 }
  71 
  72 inline void set_bits_cas(jbyte bits, jbyte* const dest) {
  73   set_bits_cas_form<traceid_or>(bits, dest);
  74 }
  75 
  76 inline void clear_bits_cas(jbyte bits, jbyte* const dest) {
  77   set_bits_cas_form<traceid_xor>(bits, dest);
  78 }
  79 
  80 inline void set_mask(jbyte mask, jbyte* const dest) {
  81   set_bits_cas_form<traceid_and>(mask, dest);
  82 }
  83 
  84 inline void set_traceid_bits(jbyte bits, traceid* dest) {
  85   set_bits(bits, ((jbyte*)dest) + low_offset);
  86 }
  87 
  88 inline void set_traceid_bits_cas(jbyte bits, traceid* dest) {
  89   set_bits_cas(bits, ((jbyte*)dest) + low_offset);
  90 }
  91 
  92 inline void set_traceid_mask(jbyte mask, traceid* dest) {
  93   set_mask(mask, ((jbyte*)dest) + low_offset);
  94 }
  95 
  96 inline void set_leakp_traceid_bits(jbyte bits, traceid* dest) {
  97   set_bits(bits, ((jbyte*)dest) + leakp_offset);
  98 }
  99 
 100 inline void set_leakp_traceid_bits_cas(jbyte bits, traceid* dest) {
 101   set_bits_cas(bits, ((jbyte*)dest) + leakp_offset);
 102 }
 103 
 104 inline void set_leakp_traceid_mask(jbyte mask, traceid* dest) {
 105   set_mask(mask, ((jbyte*)dest) + leakp_offset);
 106 }
 107 
 108 #endif // SHARE_JFR_RECORDER_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDBITS_INLINE_HPP