//java.util.concurrent.atomic.AtomicInteger.java public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); }
//sun.misc.Unsafe.java public final native boolean compareAndSwapInt(Object o, long offset, int expected, int x);
// http://hg.openjdk.java.net/jdk8u/jdk8u/ hotspotsrcsharevmprimsunsafe.cpp // These are the methods prior to the JSR 166 changes in 1.6.0 static JNINativeMethod methods_15[] = { //... {CC "compareAndSwapInt", CC "(" OBJ "J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, //... }; UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) UnsafeWrapper("Unsafe_CompareAndSwapInt"); oop p = JNIHandles::resolve(obj); jint* addr = (jint *) index_oop_from_field_offset_long(p, offset); return (jint)(Atomic::cmpxchg(x, addr, e)) == e; UNSAFE_END
// http://hg.openjdk.java.net/jdk8u/jdk8u/ hotspotsrcsharevm untimeatomic.cpp unsigned Atomic::cmpxchg(unsigned int exchange_value, volatile unsigned int* dest, unsigned int compare_value) { assert(sizeof(unsigned int) == sizeof(jint), "more work to do"); return (unsigned int)Atomic::cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value); }
// http://hg.openjdk.java.net/jdk8u/jdk8u/ hotspotsrcos_cpuwindows_x86vmatomic_windows_x86.inline.hpp inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) { // alternative for InterlockedCompareExchange int mp = os::is_MP(); __asm { mov edx, dest mov ecx, exchange_value mov eax, compare_value LOCK_IF_MP(mp) cmpxchg dword ptr [edx], ecx } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72918.html
摘要:本篇我們將以的公平鎖為例來詳細看看使用獲取獨占鎖的流程。本文中的源碼基于。由于本篇我們分析的是獨占鎖,同一時刻,鎖只能被一個線程所持有。由于在整個搶鎖過程中,我們都是不響應中斷的。 前言 AQS(AbstractQueuedSynchronizer)是JAVA中眾多鎖以及并發工具的基礎,其底層采用樂觀鎖,大量使用了CAS操作, 并且在沖突時,采用自旋方式重試,以實現輕量級和高效地獲取鎖...
ConcurrentHashMap源碼分析_JDK1.8版本 聲明 文章均為本人技術筆記,轉載請注明出處[1] https://segmentfault.com/u/yzwall[2] blog.csdn.net/j_dark/ JDK1.6版本 ConcurrentHashMap結構 showImg(https://segmentfault.com/img/remote/146000000900...
摘要:等到所有子線程都執行完后即,會主調用線程,然后主調用線程就會從函數返回,繼續后余動作。 原理剖析(第 005 篇)AQS工作原理分析 - 一、大致介紹 1、前面章節講解了一下CAS,簡單講就是cmpxchg+lock的原子操作; 2、而在談到并發操作里面,我們不得不談到AQS,JDK的源碼里面好多并發的類都是通過Sync的內部類繼承AQS而實現出五花八門的功能; 3、本章節就和大家分享...
閱讀 2137·2021-11-22 15:22
閱讀 1286·2021-11-11 16:54
閱讀 1807·2021-09-23 11:32
閱讀 3007·2021-09-22 10:02
閱讀 1771·2019-08-30 12:59
閱讀 1085·2019-08-29 16:27
閱讀 622·2019-08-29 13:21
閱讀 2464·2019-08-28 17:57