Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
ac802560
Commit
ac802560
authored
Oct 26, 2020
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
e39e6682
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
36 deletions
+47
-36
go/zodb/internal/weak/weak.go
go/zodb/internal/weak/weak.go
+2
-3
go/zodb/internal/weak/weak_test.go
go/zodb/internal/weak/weak_test.go
+37
-25
go/zodb/internal/weak/ztrace.go
go/zodb/internal/weak/ztrace.go
+8
-8
No files found.
go/zodb/internal/weak/weak.go
View file @
ac802560
...
@@ -68,7 +68,7 @@ type Ref struct {
...
@@ -68,7 +68,7 @@ type Ref struct {
state
weakRefState
state
weakRefState
}
}
//trace:event traceRelease(w *Ref
, released bool
)
//trace:event traceRelease(w *Ref)
// NewRef creates new weak reference pointing to obj.
// NewRef creates new weak reference pointing to obj.
//
//
...
@@ -100,11 +100,10 @@ func NewRef(obj interface{}) *Ref {
...
@@ -100,11 +100,10 @@ func NewRef(obj interface{}) *Ref {
if
w
.
state
==
objGot
{
if
w
.
state
==
objGot
{
w
.
state
=
objLive
w
.
state
=
objLive
runtime
.
SetFinalizer
(
obj
,
release
)
runtime
.
SetFinalizer
(
obj
,
release
)
traceRelease
(
w
,
false
)
}
else
{
}
else
{
w
.
state
=
objReleased
w
.
state
=
objReleased
traceRelease
(
w
,
true
)
}
}
traceRelease
(
w
)
w
.
mu
.
Unlock
()
w
.
mu
.
Unlock
()
}
}
...
...
go/zodb/internal/weak/weak_test.go
View file @
ac802560
...
@@ -56,20 +56,27 @@ func TestIface(t *testing.T) {
...
@@ -56,20 +56,27 @@ func TestIface(t *testing.T) {
}
}
func
TestWeakRef
(
t
*
testing
.
T
)
{
func
TestWeakRef
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
100
;
i
++
{
println
(
i
)
testWeakRef
(
t
)
}
}
func
testWeakRef
(
t
*
testing
.
T
)
{
type
T
struct
{
_
[
8
]
int64
}
// large enough not to go into tinyalloc
type
T
struct
{
_
[
8
]
int64
}
// large enough not to go into tinyalloc
p
:=
new
(
T
)
p
:=
new
(
T
)
w
:=
NewRef
(
p
)
w
:=
NewRef
(
p
)
pptr
:=
uintptr
(
unsafe
.
Pointer
(
p
))
pptr
:=
uintptr
(
unsafe
.
Pointer
(
p
))
wrelease
:=
make
(
chan
bool
)
// events from traceRelease(w)
wrelease
:=
make
(
chan
weakRefState
)
// w.state from traceRelease(w) event
tpg
:=
&
tracing
.
ProbeGroup
{}
tpg
:=
&
tracing
.
ProbeGroup
{}
tracing
.
Lock
()
tracing
.
Lock
()
traceRelease_Attach
(
tpg
,
func
(
w_
*
Ref
,
released
bool
)
{
traceRelease_Attach
(
tpg
,
func
(
w_
*
Ref
)
{
if
w_
!=
w
{
if
w_
!=
w
{
panic
(
"release: w != w_"
)
panic
(
"release: w != w_"
)
}
}
wrelease
<-
released
wrelease
<-
w
.
state
})
})
traceGotPre_Attach
(
tpg
,
func
(
w
*
Ref
)
{
traceGotPre_Attach
(
tpg
,
func
(
w
*
Ref
)
{
// nop for now
// nop for now
...
@@ -86,20 +93,10 @@ func TestWeakRef(t *testing.T) {
...
@@ -86,20 +93,10 @@ func TestWeakRef(t *testing.T) {
}
}
// perform GC + give finalizers a chance to run.
// perform GC + give finalizers a chance to run.
GC
:=
func
(
expectRelease
bool
)
{
GC
nofin
:=
func
(
)
{
t
.
Helper
()
t
.
Helper
()
runtime
.
GC
()
runtime
.
GC
()
// GC only queues finalizers, not runs them directly. Give it
// some time so that finalizers could have been run.
if
expectRelease
{
select
{
case
<-
wrelease
:
// ok
case
<-
time
.
After
(
100
*
time
.
Millisecond
)
:
t
.
Fatal
(
"no release event"
)
}
}
else
{
select
{
select
{
case
<-
time
.
After
(
10
*
time
.
Millisecond
)
:
case
<-
time
.
After
(
10
*
time
.
Millisecond
)
:
// ok
// ok
...
@@ -107,19 +104,36 @@ func TestWeakRef(t *testing.T) {
...
@@ -107,19 +104,36 @@ func TestWeakRef(t *testing.T) {
t
.
Fatal
(
"unexpected release event"
)
t
.
Fatal
(
"unexpected release event"
)
}
}
}
}
GCfin
:=
func
(
stateOK
weakRefState
)
{
t
.
Helper
()
runtime
.
GC
()
// GC only queues finalizers, not runs them directly. Give it
// some time so that finalizers could have been run.
var
state
weakRefState
select
{
case
state
=
<-
wrelease
:
// ok
case
<-
time
.
After
(
1
*
time
.
Second
)
:
t
.
Fatal
(
"no release event"
)
}
if
state
!=
stateOK
{
t
.
Fatalf
(
"release: state != stateOK; state=%v stateOK=%v"
,
state
,
stateOK
)
}
}
}
assertEq
(
w
.
state
,
objLive
)
assertEq
(
w
.
state
,
objLive
)
assertEq
(
w
.
Get
(),
p
)
assertEq
(
w
.
Get
(),
p
)
assertEq
(
w
.
state
,
objGot
)
assertEq
(
w
.
state
,
objGot
)
GC
(
false
)
GC
nofin
(
)
assertEq
(
w
.
state
,
objGot
)
// fin has not been run at all (p is live)
assertEq
(
w
.
state
,
objGot
)
// fin has not been run at all (p is live)
assertEq
(
w
.
Get
(),
p
)
assertEq
(
w
.
Get
(),
p
)
assertEq
(
w
.
state
,
objGot
)
assertEq
(
w
.
state
,
objGot
)
p
=
nil
p
=
nil
GC
(
true
)
GC
fin
(
objLive
)
// fin ran and downgraded got -> live
assertEq
(
w
.
state
,
objLive
)
// fin ran and downgraded got -> live
assertEq
(
w
.
state
,
objLive
)
switch
p_
:=
w
.
Get
()
.
(
type
)
{
switch
p_
:=
w
.
Get
()
.
(
type
)
{
default
:
default
:
t
.
Fatalf
(
"Get after objGot -> objLive: %#v"
,
p_
)
t
.
Fatalf
(
"Get after objGot -> objLive: %#v"
,
p_
)
...
@@ -130,10 +144,8 @@ func TestWeakRef(t *testing.T) {
...
@@ -130,10 +144,8 @@ func TestWeakRef(t *testing.T) {
}
}
assertEq
(
w
.
state
,
objGot
)
assertEq
(
w
.
state
,
objGot
)
GC
(
true
)
GCfin
(
objLive
)
// fin ran again and again downgraded got -> live
assertEq
(
w
.
state
,
objLive
)
// fin ran again and again downgraded got -> live
GC
(
true
)
GCfin
(
objReleased
)
// fin ran again and released the object
assertEq
(
w
.
state
,
objReleased
)
// fin ran again and released the object
assertEq
(
w
.
Get
(),
nil
)
assertEq
(
w
.
Get
(),
nil
)
}
}
go/zodb/internal/weak/ztrace.go
View file @
ac802560
...
@@ -35,32 +35,32 @@ func traceGotPre_Attach(pg *tracing.ProbeGroup, probe func(w *Ref)) *tracing.Pro
...
@@ -35,32 +35,32 @@ func traceGotPre_Attach(pg *tracing.ProbeGroup, probe func(w *Ref)) *tracing.Pro
return
&
p
.
Probe
return
&
p
.
Probe
}
}
// traceevent: traceRelease(w *Ref
, released bool
)
// traceevent: traceRelease(w *Ref)
type
_t_traceRelease
struct
{
type
_t_traceRelease
struct
{
tracing
.
Probe
tracing
.
Probe
probefunc
func
(
w
*
Ref
,
released
bool
)
probefunc
func
(
w
*
Ref
)
}
}
var
_traceRelease
*
_t_traceRelease
var
_traceRelease
*
_t_traceRelease
func
traceRelease
(
w
*
Ref
,
released
bool
)
{
func
traceRelease
(
w
*
Ref
)
{
if
_traceRelease
!=
nil
{
if
_traceRelease
!=
nil
{
_traceRelease_run
(
w
,
released
)
_traceRelease_run
(
w
)
}
}
}
}
func
_traceRelease_run
(
w
*
Ref
,
released
bool
)
{
func
_traceRelease_run
(
w
*
Ref
)
{
for
p
:=
_traceRelease
;
p
!=
nil
;
p
=
(
*
_t_traceRelease
)(
unsafe
.
Pointer
(
p
.
Next
()))
{
for
p
:=
_traceRelease
;
p
!=
nil
;
p
=
(
*
_t_traceRelease
)(
unsafe
.
Pointer
(
p
.
Next
()))
{
p
.
probefunc
(
w
,
released
)
p
.
probefunc
(
w
)
}
}
}
}
func
traceRelease_Attach
(
pg
*
tracing
.
ProbeGroup
,
probe
func
(
w
*
Ref
,
released
bool
))
*
tracing
.
Probe
{
func
traceRelease_Attach
(
pg
*
tracing
.
ProbeGroup
,
probe
func
(
w
*
Ref
))
*
tracing
.
Probe
{
p
:=
_t_traceRelease
{
probefunc
:
probe
}
p
:=
_t_traceRelease
{
probefunc
:
probe
}
tracing
.
AttachProbe
(
pg
,
(
**
tracing
.
Probe
)(
unsafe
.
Pointer
(
&
_traceRelease
)),
&
p
.
Probe
)
tracing
.
AttachProbe
(
pg
,
(
**
tracing
.
Probe
)(
unsafe
.
Pointer
(
&
_traceRelease
)),
&
p
.
Probe
)
return
&
p
.
Probe
return
&
p
.
Probe
}
}
// trace export signature
// trace export signature
func
_trace_exporthash_
c65df3d81975319429a288a09095370dace6a87b
()
{}
func
_trace_exporthash_
55416cf1cbcad92c4b4497269fa14b46e82469bd
()
{}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment