Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
129aa67a
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
129aa67a
编写于
12月 04, 2015
作者:
R
robm
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8140587: Atomic*FieldUpdaters should use Class.isInstance instead of direct class check
Reviewed-by: martin
上级
711ac304
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
261 addition
and
240 deletion
+261
-240
src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
...ava/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
+70
-61
src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java
...s/java/util/concurrent/atomic/AtomicLongFieldUpdater.java
+119
-104
src/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java
...a/util/concurrent/atomic/AtomicReferenceFieldUpdater.java
+72
-75
未找到文件。
src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
浏览文件 @
129aa67a
...
@@ -34,14 +34,14 @@
...
@@ -34,14 +34,14 @@
*/
*/
package
java.util.concurrent.atomic
;
package
java.util.concurrent.atomic
;
import
java.util.function.IntUnaryOperator
;
import
java.util.function.IntBinaryOperator
;
import
sun.misc.Unsafe
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Modifier
;
import
java.lang.reflect.Modifier
;
import
java.security.AccessController
;
import
java.security.AccessController
;
import
java.security.PrivilegedExceptionAction
;
import
java.security.PrivilegedActionException
;
import
java.security.PrivilegedActionException
;
import
java.security.PrivilegedExceptionAction
;
import
java.util.function.IntBinaryOperator
;
import
java.util.function.IntUnaryOperator
;
import
sun.reflect.CallerSensitive
;
import
sun.reflect.CallerSensitive
;
import
sun.reflect.Reflection
;
import
sun.reflect.Reflection
;
...
@@ -363,14 +363,19 @@ public abstract class AtomicIntegerFieldUpdater<T> {
...
@@ -363,14 +363,19 @@ public abstract class AtomicIntegerFieldUpdater<T> {
}
}
/**
/**
* Standard hotspot implementation using intrinsics
* Standard hotspot implementation using intrinsics
.
*/
*/
private
static
class
AtomicIntegerFieldUpdaterImpl
<
T
>
private
static
final
class
AtomicIntegerFieldUpdaterImpl
<
T
>
extends
AtomicIntegerFieldUpdater
<
T
>
{
extends
AtomicIntegerFieldUpdater
<
T
>
{
private
static
final
Unsafe
unsafe
=
Unsafe
.
getUnsafe
();
private
static
final
sun
.
misc
.
Unsafe
U
=
sun
.
misc
.
Unsafe
.
getUnsafe
();
private
final
long
offset
;
private
final
long
offset
;
private
final
Class
<
T
>
tclass
;
/**
* if field is protected, the subclass constructing updater, else
* the same as tclass
*/
private
final
Class
<?>
cclass
;
private
final
Class
<?>
cclass
;
/** class holding the field */
private
final
Class
<
T
>
tclass
;
AtomicIntegerFieldUpdaterImpl
(
final
Class
<
T
>
tclass
,
AtomicIntegerFieldUpdaterImpl
(
final
Class
<
T
>
tclass
,
final
String
fieldName
,
final
String
fieldName
,
...
@@ -399,17 +404,15 @@ public abstract class AtomicIntegerFieldUpdater<T> {
...
@@ -399,17 +404,15 @@ public abstract class AtomicIntegerFieldUpdater<T> {
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
}
}
Class
<?>
fieldt
=
field
.
getType
();
if
(
field
.
getType
()
!=
int
.
class
)
if
(
fieldt
!=
int
.
class
)
throw
new
IllegalArgumentException
(
"Must be integer type"
);
throw
new
IllegalArgumentException
(
"Must be integer type"
);
if
(!
Modifier
.
isVolatile
(
modifiers
))
if
(!
Modifier
.
isVolatile
(
modifiers
))
throw
new
IllegalArgumentException
(
"Must be volatile type"
);
throw
new
IllegalArgumentException
(
"Must be volatile type"
);
this
.
cclass
=
(
Modifier
.
isProtected
(
modifiers
)
&&
this
.
cclass
=
(
Modifier
.
isProtected
(
modifiers
))
?
caller
:
tclass
;
caller
!=
tclass
)
?
caller
:
null
;
this
.
tclass
=
tclass
;
this
.
tclass
=
tclass
;
offset
=
unsafe
.
objectFieldOffset
(
field
);
this
.
offset
=
U
.
objectFieldOffset
(
field
);
}
}
/**
/**
...
@@ -428,81 +431,87 @@ public abstract class AtomicIntegerFieldUpdater<T> {
...
@@ -428,81 +431,87 @@ public abstract class AtomicIntegerFieldUpdater<T> {
return
false
;
return
false
;
}
}
private
void
fullCheck
(
T
obj
)
{
/**
if
(!
tclass
.
isInstance
(
obj
))
* Checks that target argument is instance of cclass. On
* failure, throws cause.
*/
private
final
void
accessCheck
(
T
obj
)
{
if
(!
cclass
.
isInstance
(
obj
))
throwAccessCheckException
(
obj
);
}
/**
* Throws access exception if accessCheck failed due to
* protected access, else ClassCastException.
*/
private
final
void
throwAccessCheckException
(
T
obj
)
{
if
(
cclass
==
tclass
)
throw
new
ClassCastException
();
throw
new
ClassCastException
();
if
(
cclass
!=
null
)
else
ensureProtectedAccess
(
obj
);
throw
new
RuntimeException
(
new
IllegalAccessException
(
"Class "
+
cclass
.
getName
()
+
" can not access a protected member of class "
+
tclass
.
getName
()
+
" using an instance of "
+
obj
.
getClass
().
getName
()));
}
}
public
boolean
compareAndSet
(
T
obj
,
int
expect
,
int
update
)
{
public
final
boolean
compareAndSet
(
T
obj
,
int
expect
,
int
update
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
return
unsafe
.
compareAndSwapInt
(
obj
,
offset
,
expect
,
update
);
return
U
.
compareAndSwapInt
(
obj
,
offset
,
expect
,
update
);
}
}
public
boolean
weakCompareAndSet
(
T
obj
,
int
expect
,
int
update
)
{
public
final
boolean
weakCompareAndSet
(
T
obj
,
int
expect
,
int
update
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
return
unsafe
.
compareAndSwapInt
(
obj
,
offset
,
expect
,
update
);
return
U
.
compareAndSwapInt
(
obj
,
offset
,
expect
,
update
);
}
}
public
void
set
(
T
obj
,
int
newValue
)
{
public
final
void
set
(
T
obj
,
int
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
unsafe
.
putIntVolatile
(
obj
,
offset
,
newValue
);
U
.
putIntVolatile
(
obj
,
offset
,
newValue
);
}
}
public
void
lazySet
(
T
obj
,
int
newValue
)
{
public
final
void
lazySet
(
T
obj
,
int
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
unsafe
.
putOrderedInt
(
obj
,
offset
,
newValue
);
U
.
putOrderedInt
(
obj
,
offset
,
newValue
);
}
}
public
final
int
get
(
T
obj
)
{
public
final
int
get
(
T
obj
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
return
unsafe
.
getIntVolatile
(
obj
,
offset
);
return
U
.
getIntVolatile
(
obj
,
offset
);
}
}
public
int
getAndSet
(
T
obj
,
int
newValue
)
{
public
final
int
getAndSet
(
T
obj
,
int
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
return
unsafe
.
getAndSetInt
(
obj
,
offset
,
newValue
);
return
U
.
getAndSetInt
(
obj
,
offset
,
newValue
);
}
}
public
int
getAndIncrement
(
T
obj
)
{
public
final
int
getAndAdd
(
T
obj
,
int
delta
)
{
return
getAndAdd
(
obj
,
1
);
accessCheck
(
obj
);
return
U
.
getAndAddInt
(
obj
,
offset
,
delta
);
}
}
public
int
getAndDe
crement
(
T
obj
)
{
public
final
int
getAndIn
crement
(
T
obj
)
{
return
getAndAdd
(
obj
,
-
1
);
return
getAndAdd
(
obj
,
1
);
}
}
public
int
getAndAdd
(
T
obj
,
int
delta
)
{
public
final
int
getAndDecrement
(
T
obj
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
fullCheck
(
obj
);
return
getAndAdd
(
obj
,
-
1
);
return
unsafe
.
getAndAddInt
(
obj
,
offset
,
delta
);
}
}
public
int
incrementAndGet
(
T
obj
)
{
public
final
int
incrementAndGet
(
T
obj
)
{
return
getAndAdd
(
obj
,
1
)
+
1
;
return
getAndAdd
(
obj
,
1
)
+
1
;
}
}
public
int
decrementAndGet
(
T
obj
)
{
public
final
int
decrementAndGet
(
T
obj
)
{
return
getAndAdd
(
obj
,
-
1
)
-
1
;
return
getAndAdd
(
obj
,
-
1
)
-
1
;
}
}
public
int
addAndGet
(
T
obj
,
int
delta
)
{
public
final
int
addAndGet
(
T
obj
,
int
delta
)
{
return
getAndAdd
(
obj
,
delta
)
+
delta
;
return
getAndAdd
(
obj
,
delta
)
+
delta
;
}
}
private
void
ensureProtectedAccess
(
T
obj
)
{
if
(
cclass
.
isInstance
(
obj
))
{
return
;
}
throw
new
RuntimeException
(
new
IllegalAccessException
(
"Class "
+
cclass
.
getName
()
+
" can not access a protected member of class "
+
tclass
.
getName
()
+
" using an instance of "
+
obj
.
getClass
().
getName
()
)
);
}
}
}
}
}
src/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java
浏览文件 @
129aa67a
...
@@ -34,14 +34,14 @@
...
@@ -34,14 +34,14 @@
*/
*/
package
java.util.concurrent.atomic
;
package
java.util.concurrent.atomic
;
import
java.util.function.LongUnaryOperator
;
import
java.util.function.LongBinaryOperator
;
import
sun.misc.Unsafe
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Modifier
;
import
java.lang.reflect.Modifier
;
import
java.security.AccessController
;
import
java.security.AccessController
;
import
java.security.PrivilegedExceptionAction
;
import
java.security.PrivilegedActionException
;
import
java.security.PrivilegedActionException
;
import
java.security.PrivilegedExceptionAction
;
import
java.util.function.LongBinaryOperator
;
import
java.util.function.LongUnaryOperator
;
import
sun.reflect.CallerSensitive
;
import
sun.reflect.CallerSensitive
;
import
sun.reflect.Reflection
;
import
sun.reflect.Reflection
;
...
@@ -365,11 +365,16 @@ public abstract class AtomicLongFieldUpdater<T> {
...
@@ -365,11 +365,16 @@ public abstract class AtomicLongFieldUpdater<T> {
return
next
;
return
next
;
}
}
private
static
class
CASUpdater
<
T
>
extends
AtomicLongFieldUpdater
<
T
>
{
private
static
final
class
CASUpdater
<
T
>
extends
AtomicLongFieldUpdater
<
T
>
{
private
static
final
Unsafe
unsafe
=
Unsafe
.
getUnsafe
();
private
static
final
sun
.
misc
.
Unsafe
U
=
sun
.
misc
.
Unsafe
.
getUnsafe
();
private
final
long
offset
;
private
final
long
offset
;
private
final
Class
<
T
>
tclass
;
/**
* if field is protected, the subclass constructing updater, else
* the same as tclass
*/
private
final
Class
<?>
cclass
;
private
final
Class
<?>
cclass
;
/** class holding the field */
private
final
Class
<
T
>
tclass
;
CASUpdater
(
final
Class
<
T
>
tclass
,
final
String
fieldName
,
CASUpdater
(
final
Class
<
T
>
tclass
,
final
String
fieldName
,
final
Class
<?>
caller
)
{
final
Class
<?>
caller
)
{
...
@@ -397,103 +402,110 @@ public abstract class AtomicLongFieldUpdater<T> {
...
@@ -397,103 +402,110 @@ public abstract class AtomicLongFieldUpdater<T> {
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
}
}
Class
<?>
fieldt
=
field
.
getType
();
if
(
field
.
getType
()
!=
long
.
class
)
if
(
fieldt
!=
long
.
class
)
throw
new
IllegalArgumentException
(
"Must be long type"
);
throw
new
IllegalArgumentException
(
"Must be long type"
);
if
(!
Modifier
.
isVolatile
(
modifiers
))
if
(!
Modifier
.
isVolatile
(
modifiers
))
throw
new
IllegalArgumentException
(
"Must be volatile type"
);
throw
new
IllegalArgumentException
(
"Must be volatile type"
);
this
.
cclass
=
(
Modifier
.
isProtected
(
modifiers
)
&&
this
.
cclass
=
(
Modifier
.
isProtected
(
modifiers
))
?
caller
:
tclass
;
caller
!=
tclass
)
?
caller
:
null
;
this
.
tclass
=
tclass
;
this
.
tclass
=
tclass
;
offset
=
unsafe
.
objectFieldOffset
(
field
);
this
.
offset
=
U
.
objectFieldOffset
(
field
);
}
/**
* Checks that target argument is instance of cclass. On
* failure, throws cause.
*/
private
final
void
accessCheck
(
T
obj
)
{
if
(!
cclass
.
isInstance
(
obj
))
throwAccessCheckException
(
obj
);
}
}
private
void
fullCheck
(
T
obj
)
{
/**
if
(!
tclass
.
isInstance
(
obj
))
* Throws access exception if accessCheck failed due to
* protected access, else ClassCastException.
*/
private
final
void
throwAccessCheckException
(
T
obj
)
{
if
(
cclass
==
tclass
)
throw
new
ClassCastException
();
throw
new
ClassCastException
();
if
(
cclass
!=
null
)
else
ensureProtectedAccess
(
obj
);
throw
new
RuntimeException
(
new
IllegalAccessException
(
"Class "
+
cclass
.
getName
()
+
" can not access a protected member of class "
+
tclass
.
getName
()
+
" using an instance of "
+
obj
.
getClass
().
getName
()));
}
}
public
boolean
compareAndSet
(
T
obj
,
long
expect
,
long
update
)
{
public
final
boolean
compareAndSet
(
T
obj
,
long
expect
,
long
update
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
return
unsafe
.
compareAndSwapLong
(
obj
,
offset
,
expect
,
update
);
return
U
.
compareAndSwapLong
(
obj
,
offset
,
expect
,
update
);
}
}
public
boolean
weakCompareAndSet
(
T
obj
,
long
expect
,
long
update
)
{
public
final
boolean
weakCompareAndSet
(
T
obj
,
long
expect
,
long
update
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
return
unsafe
.
compareAndSwapLong
(
obj
,
offset
,
expect
,
update
);
return
U
.
compareAndSwapLong
(
obj
,
offset
,
expect
,
update
);
}
}
public
void
set
(
T
obj
,
long
newValue
)
{
public
final
void
set
(
T
obj
,
long
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
unsafe
.
putLongVolatile
(
obj
,
offset
,
newValue
);
U
.
putLongVolatile
(
obj
,
offset
,
newValue
);
}
}
public
void
lazySet
(
T
obj
,
long
newValue
)
{
public
final
void
lazySet
(
T
obj
,
long
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
unsafe
.
putOrderedLong
(
obj
,
offset
,
newValue
);
U
.
putOrderedLong
(
obj
,
offset
,
newValue
);
}
}
public
long
get
(
T
obj
)
{
public
final
long
get
(
T
obj
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
return
unsafe
.
getLongVolatile
(
obj
,
offset
);
return
U
.
getLongVolatile
(
obj
,
offset
);
}
}
public
long
getAndSet
(
T
obj
,
long
newValue
)
{
public
final
long
getAndSet
(
T
obj
,
long
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
return
unsafe
.
getAndSetLong
(
obj
,
offset
,
newValue
);
return
U
.
getAndSetLong
(
obj
,
offset
,
newValue
);
}
}
public
long
getAndIncrement
(
T
obj
)
{
public
final
long
getAndAdd
(
T
obj
,
long
delta
)
{
return
getAndAdd
(
obj
,
1
);
accessCheck
(
obj
);
return
U
.
getAndAddLong
(
obj
,
offset
,
delta
);
}
}
public
long
getAndDe
crement
(
T
obj
)
{
public
final
long
getAndIn
crement
(
T
obj
)
{
return
getAndAdd
(
obj
,
-
1
);
return
getAndAdd
(
obj
,
1
);
}
}
public
long
getAndAdd
(
T
obj
,
long
delta
)
{
public
final
long
getAndDecrement
(
T
obj
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
fullCheck
(
obj
);
return
getAndAdd
(
obj
,
-
1
);
return
unsafe
.
getAndAddLong
(
obj
,
offset
,
delta
);
}
}
public
long
incrementAndGet
(
T
obj
)
{
public
final
long
incrementAndGet
(
T
obj
)
{
return
getAndAdd
(
obj
,
1
)
+
1
;
return
getAndAdd
(
obj
,
1
)
+
1
;
}
}
public
long
decrementAndGet
(
T
obj
)
{
public
final
long
decrementAndGet
(
T
obj
)
{
return
getAndAdd
(
obj
,
-
1
)
-
1
;
return
getAndAdd
(
obj
,
-
1
)
-
1
;
}
}
public
long
addAndGet
(
T
obj
,
long
delta
)
{
public
final
long
addAndGet
(
T
obj
,
long
delta
)
{
return
getAndAdd
(
obj
,
delta
)
+
delta
;
return
getAndAdd
(
obj
,
delta
)
+
delta
;
}
}
private
void
ensureProtectedAccess
(
T
obj
)
{
if
(
cclass
.
isInstance
(
obj
))
{
return
;
}
}
throw
new
RuntimeException
(
new
IllegalAccessException
(
"Class "
+
cclass
.
getName
()
+
" can not access a protected member of class "
+
tclass
.
getName
()
+
" using an instance of "
+
obj
.
getClass
().
getName
()
)
);
}
}
private
static
class
LockedUpdater
<
T
>
extends
AtomicLongFieldUpdater
<
T
>
{
private
static
final
class
LockedUpdater
<
T
>
extends
AtomicLongFieldUpdater
<
T
>
{
private
static
final
Unsafe
unsafe
=
Unsafe
.
getUnsafe
();
private
static
final
sun
.
misc
.
Unsafe
U
=
sun
.
misc
.
Unsafe
.
getUnsafe
();
private
final
long
offset
;
private
final
long
offset
;
private
final
Class
<
T
>
tclass
;
/**
* if field is protected, the subclass constructing updater, else
* the same as tclass
*/
private
final
Class
<?>
cclass
;
private
final
Class
<?>
cclass
;
/** class holding the field */
private
final
Class
<
T
>
tclass
;
LockedUpdater
(
final
Class
<
T
>
tclass
,
final
String
fieldName
,
LockedUpdater
(
final
Class
<
T
>
tclass
,
final
String
fieldName
,
final
Class
<?>
caller
)
{
final
Class
<?>
caller
)
{
...
@@ -521,73 +533,76 @@ public abstract class AtomicLongFieldUpdater<T> {
...
@@ -521,73 +533,76 @@ public abstract class AtomicLongFieldUpdater<T> {
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
}
}
Class
<?>
fieldt
=
field
.
getType
();
if
(
field
.
getType
()
!=
long
.
class
)
if
(
fieldt
!=
long
.
class
)
throw
new
IllegalArgumentException
(
"Must be long type"
);
throw
new
IllegalArgumentException
(
"Must be long type"
);
if
(!
Modifier
.
isVolatile
(
modifiers
))
if
(!
Modifier
.
isVolatile
(
modifiers
))
throw
new
IllegalArgumentException
(
"Must be volatile type"
);
throw
new
IllegalArgumentException
(
"Must be volatile type"
);
this
.
cclass
=
(
Modifier
.
isProtected
(
modifiers
)
&&
this
.
cclass
=
(
Modifier
.
isProtected
(
modifiers
))
?
caller
:
tclass
;
caller
!=
tclass
)
?
caller
:
null
;
this
.
tclass
=
tclass
;
this
.
tclass
=
tclass
;
offset
=
unsafe
.
objectFieldOffset
(
field
);
this
.
offset
=
U
.
objectFieldOffset
(
field
);
}
}
private
void
fullCheck
(
T
obj
)
{
/**
if
(!
tclass
.
isInstance
(
obj
))
* Checks that target argument is instance of cclass. On
throw
new
ClassCastException
();
* failure, throws cause.
if
(
cclass
!=
null
)
*/
ensureProtectedAccess
(
obj
);
private
final
void
accessCheck
(
T
obj
)
{
if
(!
cclass
.
isInstance
(
obj
))
throw
accessCheckException
(
obj
);
}
}
public
boolean
compareAndSet
(
T
obj
,
long
expect
,
long
update
)
{
/**
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
fullCheck
(
obj
);
* Returns access exception if accessCheck failed due to
* protected access, else ClassCastException.
*/
private
final
RuntimeException
accessCheckException
(
T
obj
)
{
if
(
cclass
==
tclass
)
return
new
ClassCastException
();
else
return
new
RuntimeException
(
new
IllegalAccessException
(
"Class "
+
cclass
.
getName
()
+
" can not access a protected member of class "
+
tclass
.
getName
()
+
" using an instance of "
+
obj
.
getClass
().
getName
()));
}
public
final
boolean
compareAndSet
(
T
obj
,
long
expect
,
long
update
)
{
accessCheck
(
obj
);
synchronized
(
this
)
{
synchronized
(
this
)
{
long
v
=
unsafe
.
getLong
(
obj
,
offset
);
long
v
=
U
.
getLong
(
obj
,
offset
);
if
(
v
!=
expect
)
if
(
v
!=
expect
)
return
false
;
return
false
;
unsafe
.
putLong
(
obj
,
offset
,
update
);
U
.
putLong
(
obj
,
offset
,
update
);
return
true
;
return
true
;
}
}
}
}
public
boolean
weakCompareAndSet
(
T
obj
,
long
expect
,
long
update
)
{
public
final
boolean
weakCompareAndSet
(
T
obj
,
long
expect
,
long
update
)
{
return
compareAndSet
(
obj
,
expect
,
update
);
return
compareAndSet
(
obj
,
expect
,
update
);
}
}
public
void
set
(
T
obj
,
long
newValue
)
{
public
final
void
set
(
T
obj
,
long
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
synchronized
(
this
)
{
synchronized
(
this
)
{
unsafe
.
putLong
(
obj
,
offset
,
newValue
);
U
.
putLong
(
obj
,
offset
,
newValue
);
}
}
}
}
public
void
lazySet
(
T
obj
,
long
newValue
)
{
public
final
void
lazySet
(
T
obj
,
long
newValue
)
{
set
(
obj
,
newValue
);
set
(
obj
,
newValue
);
}
}
public
long
get
(
T
obj
)
{
public
final
long
get
(
T
obj
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
full
Check
(
obj
);
access
Check
(
obj
);
synchronized
(
this
)
{
synchronized
(
this
)
{
return
unsafe
.
getLong
(
obj
,
offset
);
return
U
.
getLong
(
obj
,
offset
);
}
}
}
}
private
void
ensureProtectedAccess
(
T
obj
)
{
if
(
cclass
.
isInstance
(
obj
))
{
return
;
}
throw
new
RuntimeException
(
new
IllegalAccessException
(
"Class "
+
cclass
.
getName
()
+
" can not access a protected member of class "
+
tclass
.
getName
()
+
" using an instance of "
+
obj
.
getClass
().
getName
()
)
);
}
}
}
/**
/**
...
@@ -595,7 +610,7 @@ public abstract class AtomicLongFieldUpdater<T> {
...
@@ -595,7 +610,7 @@ public abstract class AtomicLongFieldUpdater<T> {
* classloader's delegation chain.
* classloader's delegation chain.
* Equivalent to the inaccessible: first.isAncestor(second).
* Equivalent to the inaccessible: first.isAncestor(second).
*/
*/
private
static
boolean
isAncestor
(
ClassLoader
first
,
ClassLoader
second
)
{
static
boolean
isAncestor
(
ClassLoader
first
,
ClassLoader
second
)
{
ClassLoader
acl
=
first
;
ClassLoader
acl
=
first
;
do
{
do
{
acl
=
acl
.
getParent
();
acl
=
acl
.
getParent
();
...
...
src/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java
浏览文件 @
129aa67a
...
@@ -34,14 +34,14 @@
...
@@ -34,14 +34,14 @@
*/
*/
package
java.util.concurrent.atomic
;
package
java.util.concurrent.atomic
;
import
java.util.function.UnaryOperator
;
import
java.util.function.BinaryOperator
;
import
sun.misc.Unsafe
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Modifier
;
import
java.lang.reflect.Modifier
;
import
java.security.AccessController
;
import
java.security.AccessController
;
import
java.security.PrivilegedExceptionAction
;
import
java.security.PrivilegedActionException
;
import
java.security.PrivilegedActionException
;
import
java.security.PrivilegedExceptionAction
;
import
java.util.function.BinaryOperator
;
import
java.util.function.UnaryOperator
;
import
sun.reflect.CallerSensitive
;
import
sun.reflect.CallerSensitive
;
import
sun.reflect.Reflection
;
import
sun.reflect.Reflection
;
...
@@ -284,11 +284,17 @@ public abstract class AtomicReferenceFieldUpdater<T,V> {
...
@@ -284,11 +284,17 @@ public abstract class AtomicReferenceFieldUpdater<T,V> {
private
static
final
class
AtomicReferenceFieldUpdaterImpl
<
T
,
V
>
private
static
final
class
AtomicReferenceFieldUpdaterImpl
<
T
,
V
>
extends
AtomicReferenceFieldUpdater
<
T
,
V
>
{
extends
AtomicReferenceFieldUpdater
<
T
,
V
>
{
private
static
final
Unsafe
unsafe
=
Unsafe
.
getUnsafe
();
private
static
final
sun
.
misc
.
Unsafe
U
=
sun
.
misc
.
Unsafe
.
getUnsafe
();
private
final
long
offset
;
private
final
long
offset
;
/**
* if field is protected, the subclass constructing updater, else
* the same as tclass
*/
private
final
Class
<?>
cclass
;
/** class holding the field */
private
final
Class
<
T
>
tclass
;
private
final
Class
<
T
>
tclass
;
/** field value type */
private
final
Class
<
V
>
vclass
;
private
final
Class
<
V
>
vclass
;
private
final
Class
<?>
cclass
;
/*
/*
* Internal type checks within all update methods contain
* Internal type checks within all update methods contain
...
@@ -340,14 +346,10 @@ public abstract class AtomicReferenceFieldUpdater<T,V> {
...
@@ -340,14 +346,10 @@ public abstract class AtomicReferenceFieldUpdater<T,V> {
if
(!
Modifier
.
isVolatile
(
modifiers
))
if
(!
Modifier
.
isVolatile
(
modifiers
))
throw
new
IllegalArgumentException
(
"Must be volatile type"
);
throw
new
IllegalArgumentException
(
"Must be volatile type"
);
this
.
cclass
=
(
Modifier
.
isProtected
(
modifiers
)
&&
this
.
cclass
=
(
Modifier
.
isProtected
(
modifiers
))
?
caller
:
tclass
;
caller
!=
tclass
)
?
caller
:
null
;
this
.
tclass
=
tclass
;
this
.
tclass
=
tclass
;
if
(
vclass
==
Object
.
class
)
this
.
vclass
=
null
;
else
this
.
vclass
=
vclass
;
this
.
vclass
=
vclass
;
offset
=
unsafe
.
objectFieldOffset
(
field
);
this
.
offset
=
U
.
objectFieldOffset
(
field
);
}
}
/**
/**
...
@@ -366,83 +368,78 @@ public abstract class AtomicReferenceFieldUpdater<T,V> {
...
@@ -366,83 +368,78 @@ public abstract class AtomicReferenceFieldUpdater<T,V> {
return
false
;
return
false
;
}
}
void
targetCheck
(
T
obj
)
{
/**
if
(!
tclass
.
isInstance
(
obj
))
* Checks that target argument is instance of cclass. On
* failure, throws cause.
*/
private
final
void
accessCheck
(
T
obj
)
{
if
(!
cclass
.
isInstance
(
obj
))
throwAccessCheckException
(
obj
);
}
/**
* Throws access exception if accessCheck failed due to
* protected access, else ClassCastException.
*/
private
final
void
throwAccessCheckException
(
T
obj
)
{
if
(
cclass
==
tclass
)
throw
new
ClassCastException
();
throw
new
ClassCastException
();
if
(
cclass
!=
null
)
else
ensureProtectedAccess
(
obj
);
throw
new
RuntimeException
(
new
IllegalAccessException
(
"Class "
+
cclass
.
getName
()
+
" can not access a protected member of class "
+
tclass
.
getName
()
+
" using an instance of "
+
obj
.
getClass
().
getName
()));
}
private
final
void
valueCheck
(
V
v
)
{
if
(
v
!=
null
&&
!(
vclass
.
isInstance
(
v
)))
throwCCE
();
}
}
void
updateCheck
(
T
obj
,
V
update
)
{
static
void
throwCCE
()
{
if
(!
tclass
.
isInstance
(
obj
)
||
(
update
!=
null
&&
vclass
!=
null
&&
!
vclass
.
isInstance
(
update
)))
throw
new
ClassCastException
();
throw
new
ClassCastException
();
if
(
cclass
!=
null
)
ensureProtectedAccess
(
obj
);
}
}
public
boolean
compareAndSet
(
T
obj
,
V
expect
,
V
update
)
{
public
final
boolean
compareAndSet
(
T
obj
,
V
expect
,
V
update
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
||
accessCheck
(
obj
);
(
update
!=
null
&&
vclass
!=
null
&&
valueCheck
(
update
);
vclass
!=
update
.
getClass
()))
return
U
.
compareAndSwapObject
(
obj
,
offset
,
expect
,
update
);
updateCheck
(
obj
,
update
);
return
unsafe
.
compareAndSwapObject
(
obj
,
offset
,
expect
,
update
);
}
}
public
boolean
weakCompareAndSet
(
T
obj
,
V
expect
,
V
update
)
{
public
final
boolean
weakCompareAndSet
(
T
obj
,
V
expect
,
V
update
)
{
// same implementation as strong form for now
// same implementation as strong form for now
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
||
accessCheck
(
obj
);
(
update
!=
null
&&
vclass
!=
null
&&
valueCheck
(
update
);
vclass
!=
update
.
getClass
()))
return
U
.
compareAndSwapObject
(
obj
,
offset
,
expect
,
update
);
updateCheck
(
obj
,
update
);
return
unsafe
.
compareAndSwapObject
(
obj
,
offset
,
expect
,
update
);
}
}
public
void
set
(
T
obj
,
V
newValue
)
{
public
final
void
set
(
T
obj
,
V
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
||
accessCheck
(
obj
);
(
newValue
!=
null
&&
vclass
!=
null
&&
valueCheck
(
newValue
);
vclass
!=
newValue
.
getClass
()))
U
.
putObjectVolatile
(
obj
,
offset
,
newValue
);
updateCheck
(
obj
,
newValue
);
unsafe
.
putObjectVolatile
(
obj
,
offset
,
newValue
);
}
}
public
void
lazySet
(
T
obj
,
V
newValue
)
{
public
final
void
lazySet
(
T
obj
,
V
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
||
accessCheck
(
obj
);
(
newValue
!=
null
&&
vclass
!=
null
&&
valueCheck
(
newValue
);
vclass
!=
newValue
.
getClass
()))
U
.
putOrderedObject
(
obj
,
offset
,
newValue
);
updateCheck
(
obj
,
newValue
);
unsafe
.
putOrderedObject
(
obj
,
offset
,
newValue
);
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
public
V
get
(
T
obj
)
{
public
final
V
get
(
T
obj
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
)
accessCheck
(
obj
);
targetCheck
(
obj
);
return
(
V
)
U
.
getObjectVolatile
(
obj
,
offset
);
return
(
V
)
unsafe
.
getObjectVolatile
(
obj
,
offset
);
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
public
V
getAndSet
(
T
obj
,
V
newValue
)
{
public
final
V
getAndSet
(
T
obj
,
V
newValue
)
{
if
(
obj
==
null
||
obj
.
getClass
()
!=
tclass
||
cclass
!=
null
||
accessCheck
(
obj
);
(
newValue
!=
null
&&
vclass
!=
null
&&
valueCheck
(
newValue
);
vclass
!=
newValue
.
getClass
()))
return
(
V
)
U
.
getAndSetObject
(
obj
,
offset
,
newValue
);
updateCheck
(
obj
,
newValue
);
return
(
V
)
unsafe
.
getAndSetObject
(
obj
,
offset
,
newValue
);
}
private
void
ensureProtectedAccess
(
T
obj
)
{
if
(
cclass
.
isInstance
(
obj
))
{
return
;
}
throw
new
RuntimeException
(
new
IllegalAccessException
(
"Class "
+
cclass
.
getName
()
+
" can not access a protected member of class "
+
tclass
.
getName
()
+
" using an instance of "
+
obj
.
getClass
().
getName
()
)
);
}
}
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录