1 /*
   2  * Copyright (c) 2016, 2018, 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_VM_JFR_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDBITS_INLINE_HPP
  26 #define SHARE_VM_JFR_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   const jbyte current = OrderAccess::load_acquire(dest);
  44   if (bits != (current & bits)) {
  45     *dest |= bits;
  46   }
  47 }
  48 
  49 inline void set_mask(jbyte mask, jbyte* const dest) {
  50   assert(dest != NULL, "invariant");
  51   const jbyte current = OrderAccess::load_acquire(dest);
  52   if (mask != (current & mask)) {
  53     *dest &= mask;
  54   }
  55 }
  56 
  57 inline void set_bits_cas(jbyte bits, jbyte* const dest) {
  58   assert(dest != NULL, "invariant");
  59   do {
  60     const jbyte current = OrderAccess::load_acquire(dest);
  61     if (bits == (current & bits)) {
  62       return;
  63     }
  64     const jbyte new_value = current | bits;
  65     if (Atomic::cmpxchg(new_value, dest, current) == current) {
  66       return;
  67     }
  68   } while (true);
  69 }
  70 
  71 inline void clear_bits_cas(jbyte bits, jbyte* const dest) {
  72   assert(dest != NULL, "invariant");
  73   do {
  74     const jbyte current = OrderAccess::load_acquire(dest);
  75     if (bits != (current & bits)) {
  76       return;
  77     }
  78     const jbyte new_value = current ^ bits;
  79     if (Atomic::cmpxchg(new_value, dest, current) == current) {
  80       return;
  81     }
  82   } while (true);
  83 }
  84 
  85 inline void set_traceid_bits(jbyte bits, traceid* dest) {
  86   set_bits(bits, ((jbyte*)dest) + low_offset);
  87 }
  88 
  89 inline void set_traceid_bits_cas(jbyte bits, traceid* dest) {
  90   set_bits_cas(bits, ((jbyte*)dest) + low_offset);
  91 }
  92 
  93 inline void set_traceid_mask(jbyte mask, traceid* dest) {
  94   set_mask(mask, ((jbyte*)dest) + low_offset);
  95 }
  96 
  97 inline void set_leakp_traceid_bits(jbyte bits, traceid* dest) {
  98   set_bits(bits, ((jbyte*)dest) + leakp_offset);
  99 }
 100 
 101 inline void set_leakp_traceid_bits_cas(jbyte bits, traceid* dest) {
 102   set_bits_cas(bits, ((jbyte*)dest) + leakp_offset);
 103 }
 104 
 105 inline void set_leakp_traceid_mask(jbyte mask, traceid* dest) {
 106   set_mask(mask, ((jbyte*)dest) + leakp_offset);
 107 }
 108 
 109 #endif // SHARE_VM_JFR_CHECKPOINT_TYPES_TRACEID_JFRTRACEIDBITS_INLINE_HPP
 110