FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
clojure/src/jvm/clojure/lang/Atom.java at master · clojure/clojure · GitHub
clojure
/
clojure
Public
Notifications
You must be signed in to change notification settings
Fork
1.5k
Star
11k
Code
Actions
Projects
Security and quality
1
Insights
Additional navigation options
Code
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
clojure
/
src
/
jvm
/
clojure
/
lang
/
Atom.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
174 lines (155 loc) · 3.54 KB
Breadcrumbs
clojure
/
src
/
jvm
/
clojure
/
lang
/
Atom.java
Copy path
File metadata and controls
174 lines (155 loc) · 3.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/* rich Jan 1, 2009 */
package
clojure
.
lang
;
import
java
.
util
.
concurrent
.
atomic
.
AtomicReference
;
final
public
class
Atom
extends
ARef
implements
IAtom2
{
final
AtomicReference
state
;
public
Atom
(
Object
state
){
this
.
state
=
new
AtomicReference
(
state
);
}
public
Atom
(
Object
state
,
IPersistentMap
meta
){
super
(
meta
);
this
.
state
=
new
AtomicReference
(
state
);
}
public
Object
deref
(){
return
state
.
get
();
}
public
Object
swap
(
IFn
f
) {
for
(; ;)
{
Object
v
=
deref
();
Object
newv
=
f
.
invoke
(
v
);
validate
(
newv
);
if
(
state
.
compareAndSet
(
v
,
newv
))
{
notifyWatches
(
v
,
newv
);
return
newv
;
}
}
}
public
Object
swap
(
IFn
f
,
Object
arg
) {
for
(; ;)
{
Object
v
=
deref
();
Object
newv
=
f
.
invoke
(
v
,
arg
);
validate
(
newv
);
if
(
state
.
compareAndSet
(
v
,
newv
))
{
notifyWatches
(
v
,
newv
);
return
newv
;
}
}
}
public
Object
swap
(
IFn
f
,
Object
arg1
,
Object
arg2
) {
for
(; ;)
{
Object
v
=
deref
();
Object
newv
=
f
.
invoke
(
v
,
arg1
,
arg2
);
validate
(
newv
);
if
(
state
.
compareAndSet
(
v
,
newv
))
{
notifyWatches
(
v
,
newv
);
return
newv
;
}
}
}
public
Object
swap
(
IFn
f
,
Object
x
,
Object
y
,
ISeq
args
) {
for
(; ;)
{
Object
v
=
deref
();
Object
newv
=
f
.
applyTo
(
RT
.
listStar
(
v
,
x
,
y
,
args
));
validate
(
newv
);
if
(
state
.
compareAndSet
(
v
,
newv
))
{
notifyWatches
(
v
,
newv
);
return
newv
;
}
}
}
public
IPersistentVector
swapVals
(
IFn
f
) {
for
(; ;)
{
Object
oldv
=
deref
();
Object
newv
=
f
.
invoke
(
oldv
);
validate
(
newv
);
if
(
state
.
compareAndSet
(
oldv
,
newv
))
{
notifyWatches
(
oldv
,
newv
);
return
LazilyPersistentVector
.
createOwning
(
oldv
,
newv
);
}
}
}
public
IPersistentVector
swapVals
(
IFn
f
,
Object
arg
) {
for
(; ;)
{
Object
oldv
=
deref
();
Object
newv
=
f
.
invoke
(
oldv
,
arg
);
validate
(
newv
);
if
(
state
.
compareAndSet
(
oldv
,
newv
))
{
notifyWatches
(
oldv
,
newv
);
return
LazilyPersistentVector
.
createOwning
(
oldv
,
newv
);
}
}
}
public
IPersistentVector
swapVals
(
IFn
f
,
Object
arg1
,
Object
arg2
) {
for
(; ;)
{
Object
oldv
=
deref
();
Object
newv
=
f
.
invoke
(
oldv
,
arg1
,
arg2
);
validate
(
newv
);
if
(
state
.
compareAndSet
(
oldv
,
newv
))
{
notifyWatches
(
oldv
,
newv
);
return
LazilyPersistentVector
.
createOwning
(
oldv
,
newv
);
}
}
}
public
IPersistentVector
swapVals
(
IFn
f
,
Object
x
,
Object
y
,
ISeq
args
) {
for
(; ;)
{
Object
oldv
=
deref
();
Object
newv
=
f
.
applyTo
(
RT
.
listStar
(
oldv
,
x
,
y
,
args
));
validate
(
newv
);
if
(
state
.
compareAndSet
(
oldv
,
newv
))
{
notifyWatches
(
oldv
,
newv
);
return
LazilyPersistentVector
.
createOwning
(
oldv
,
newv
);
}
}
}
public
boolean
compareAndSet
(
Object
oldv
,
Object
newv
){
validate
(
newv
);
boolean
ret
=
state
.
compareAndSet
(
oldv
,
newv
);
if
(
ret
)
notifyWatches
(
oldv
,
newv
);
return
ret
;
}
public
Object
reset
(
Object
newval
){
Object
oldval
=
state
.
get
();
validate
(
newval
);
state
.
set
(
newval
);
notifyWatches
(
oldval
,
newval
);
return
newval
;
}
public
IPersistentVector
resetVals
(
Object
newv
){
validate
(
newv
);
for
(; ;)
{
Object
oldv
=
deref
();
if
(
state
.
compareAndSet
(
oldv
,
newv
))
{
notifyWatches
(
oldv
,
newv
);
return
LazilyPersistentVector
.
createOwning
(
oldv
,
newv
);
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL