< prev index next >

src/share/vm/runtime/atomic.cpp

Print this page
rev 7584 : code minimum


  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 #include "precompiled.hpp"
  26 #include "runtime/atomic.inline.hpp"
  27 
  28 /*
  29  * This is the default implementation of byte-sized cmpxchg. It emulates jbyte-sized cmpxchg
  30  * in terms of jint-sized cmpxchg. Platforms may override this by defining their own inline definition
  31  * as well as defining VM_HAS_SPECIALIZED_CMPXCHG_BYTE. This will cause the platform specific
  32  * implementation to be used instead.
  33  */
  34 jbyte Atomic::cmpxchg_general(jbyte exchange_value, volatile jbyte* dest, jbyte compare_value) {
  35   assert(sizeof(jbyte) == 1, "assumption.");
  36   uintptr_t dest_addr = (uintptr_t)dest;
  37   uintptr_t offset = dest_addr % sizeof(jint);
  38   volatile jint* dest_int = (volatile jint*)(dest_addr - offset);
  39   jint cur = *dest_int;
  40   jbyte* cur_as_bytes = (jbyte*)(&cur);
  41   jint new_val = cur;
  42   jbyte* new_val_as_bytes = (jbyte*)(&new_val);
  43   new_val_as_bytes[offset] = exchange_value;
  44   while (cur_as_bytes[offset] == compare_value) {
  45     jint res = cmpxchg(new_val, dest_int, cur);
  46     if (res == cur) break;
  47     cur = res;
  48     new_val = cur;
  49     new_val_as_bytes[offset] = exchange_value;
  50   }
  51   return cur_as_bytes[offset];
  52 }
  53 
  54 unsigned Atomic::xchg(unsigned int exchange_value, volatile unsigned int* dest) {
  55   assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
  56   return (unsigned int)Atomic::xchg((jint)exchange_value, (volatile jint*)dest);
  57 }
  58 
  59 unsigned Atomic::cmpxchg(unsigned int exchange_value,
  60                          volatile unsigned int* dest, unsigned int compare_value) {
  61   assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
  62   return (unsigned int)Atomic::cmpxchg((jint)exchange_value, (volatile jint*)dest,
  63                                        (jint)compare_value);
  64 }
  65 




  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 #include "precompiled.hpp"
  26 #include "runtime/atomic.inline.hpp"
  27 
  28 /*
  29  * This is the default implementation of byte-sized cmpxchg. It emulates jbyte-sized cmpxchg
  30  * in terms of jint-sized cmpxchg. Platforms may override this by defining their own inline definition
  31  * in their AtomicPlatform class.

  32  */
  33 jbyte AtomicBase::cmpxchg_general(jbyte exchange_value, volatile jbyte* dest, jbyte compare_value) {
  34   assert(sizeof(jbyte) == 1, "assumption.");
  35   uintptr_t dest_addr = (uintptr_t)dest;
  36   uintptr_t offset = dest_addr % sizeof(jint);
  37   volatile jint* dest_int = (volatile jint*)(dest_addr - offset);
  38   jint cur = *dest_int;
  39   jbyte* cur_as_bytes = (jbyte*)(&cur);
  40   jint new_val = cur;
  41   jbyte* new_val_as_bytes = (jbyte*)(&new_val);
  42   new_val_as_bytes[offset] = exchange_value;
  43   while (cur_as_bytes[offset] == compare_value) {
  44     jint res = Atomic::cmpxchg(new_val, dest_int, cur);
  45     if (res == cur) break;
  46     cur = res;
  47     new_val = cur;
  48     new_val_as_bytes[offset] = exchange_value;
  49   }
  50   return cur_as_bytes[offset];
  51 }
  52 
  53 unsigned Atomic::xchg(unsigned int exchange_value, volatile unsigned int* dest) {
  54   assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
  55   return (unsigned int)Atomic::xchg((jint)exchange_value, (volatile jint*)dest);
  56 }
  57 
  58 unsigned Atomic::cmpxchg(unsigned int exchange_value,
  59                          volatile unsigned int* dest, unsigned int compare_value) {
  60   assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
  61   return (unsigned int)Atomic::cmpxchg((jint)exchange_value, (volatile jint*)dest,
  62                                        (jint)compare_value);
  63 }
  64 


< prev index next >