Here is a snippet of code from OpenJDK6 hotspot/src/share/vm/prims/unsafe.cpp (starting at line 1082):
// JSR166 ------------------------------------------------------------------ UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) UnsafeWrapper("Unsafe_CompareAndSwapObject"); oop x = JNIHandles::resolve(x_h); oop e = JNIHandles::resolve(e_h); oop p = JNIHandles::resolve(obj); HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset); if (UseCompressedOops) { update_barrier_set_pre((narrowOop*)addr, e); } else { update_barrier_set_pre((oop*)addr, e); } oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e); jboolean success = (res == e); if (success) update_barrier_set((void*)addr, x); return success; UNSAFE_END
The key method oopDesc :: atomic_compare_exchange_oop is also added.
inline oop oopDesc::atomic_compare_exchange_oop(oop exchange_value, volatile HeapWord *dest, oop compare_value) { if (UseCompressedOops) { // encode exchange and compare value from oop to T narrowOop val = encode_heap_oop(exchange_value); narrowOop cmp = encode_heap_oop(compare_value); narrowOop old = (narrowOop) Atomic::cmpxchg(val, (narrowOop*)dest, cmp); // decode old from T to oop return decode_heap_oop(old); } else { return (oop)Atomic::cmpxchg_ptr(exchange_value, (oop*)dest, compare_value); } }
What is the purpose of this code in the context of the JVM? I am not familiar with C ++.
Atomic :: cmpxchg and Atomic :: cmpxchg_ptr become OS and CPU dependent and 32 bit / 64 bit. So, the JVMs are split here.
EDIT
As Steve-O noted, CAS has its weakness as an ABA problem, so a memory barrier is needed here to ensure that CAS is still valid in a multi-threaded environment. In addition, CAS will require three parameters: address, old value, and new value, so a modern processor is required for this process.
EDIT
With the new C ++ 0x standard (not officially published now?), Does this mean that the JVM does not need to be split then? At least at the source code level. The binary file can be split, but it will be processed by the C ++ compiler.