Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
W
wendelin.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Kirill Smelkov
wendelin.core
Commits
5074da5b
Commit
5074da5b
authored
Jul 15, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
245d1005
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
4 deletions
+56
-4
wcfs/weak.go
wcfs/weak.go
+3
-1
wcfs/weak_test.go
wcfs/weak_test.go
+53
-3
No files found.
wcfs/weak.go
View file @
5074da5b
...
@@ -63,6 +63,8 @@ type WeakRef struct {
...
@@ -63,6 +63,8 @@ type WeakRef struct {
}
}
// NewWeakRef creates new weak reference pointing to obj.
// NewWeakRef creates new weak reference pointing to obj.
//
// XXX + onrelease callback?
func
NewWeakRef
(
obj
interface
{})
*
WeakRef
{
func
NewWeakRef
(
obj
interface
{})
*
WeakRef
{
// since starting from ~ Go1.4 the GC is precise, we can save interface
// since starting from ~ Go1.4 the GC is precise, we can save interface
// pointers to uintptr and that won't prevent GC from garbage
// pointers to uintptr and that won't prevent GC from garbage
...
@@ -73,7 +75,7 @@ func NewWeakRef(obj interface{}) *WeakRef {
...
@@ -73,7 +75,7 @@ func NewWeakRef(obj interface{}) *WeakRef {
}
}
var
release
func
(
interface
{})
var
release
func
(
interface
{})
release
=
func
(
_
interface
{})
{
release
=
func
(
obj
interface
{})
{
// GC decided that the object is no longer reachable and
// GC decided that the object is no longer reachable and
// scheduled us to run as finalizer. During the time till we
// scheduled us to run as finalizer. During the time till we
// actually run, WeakRef.Get might have been come to run and
// actually run, WeakRef.Get might have been come to run and
...
...
wcfs/weak_test.go
View file @
5074da5b
...
@@ -20,13 +20,12 @@
...
@@ -20,13 +20,12 @@
package
main
package
main
import
(
import
(
"runtime"
"testing"
"testing"
"time"
"unsafe"
"unsafe"
)
)
// v -> weakref -> get -> == v
// GC -> get -> alive -> GC get alive -> GC get alive -> GC GC get ø
// verify that interface <-> iface works ok.
// verify that interface <-> iface works ok.
func
TestIface
(
t
*
testing
.
T
)
{
func
TestIface
(
t
*
testing
.
T
)
{
var
i
interface
{}
var
i
interface
{}
...
@@ -54,5 +53,56 @@ func TestIface(t *testing.T) {
...
@@ -54,5 +53,56 @@ func TestIface(t *testing.T) {
}
}
}
}
func
TestWeakRef
(
t
*
testing
.
T
)
{
func
TestWeakRef
(
t
*
testing
.
T
)
{
type
T
struct
{
_
[
8
]
int64
}
// large enough not to go into tinyalloc
p
:=
new
(
T
)
w
:=
NewWeakRef
(
p
)
pptr
:=
uintptr
(
unsafe
.
Pointer
(
p
))
assertEq
:=
func
(
a
,
b
interface
{})
{
t
.
Helper
()
if
a
!=
b
{
t
.
Fatalf
(
"not equal: %#v != %#v"
,
a
,
b
)
}
}
// perform GC + give finalizers a chancet to run.
GC
:=
func
()
{
runtime
.
GC
()
// GC only queues finalizers, not runs them directly. Give it
// some time so that finalizers could have been run.
time
.
Sleep
(
10
*
time
.
Millisecond
)
// XXX hack
}
assertEq
(
w
.
state
,
objLive
)
assertEq
(
w
.
Get
(),
p
)
assertEq
(
w
.
state
,
objGot
)
GC
()
assertEq
(
w
.
state
,
objGot
)
// fin has not been run at all (p is live)
assertEq
(
w
.
Get
(),
p
)
assertEq
(
w
.
state
,
objGot
)
p
=
nil
GC
()
assertEq
(
w
.
state
,
objLive
)
// fin ran and downgraded got -> live
switch
p_
:=
w
.
Get
()
.
(
type
)
{
default
:
t
.
Fatalf
(
"Get after objGot -> objLive: %#v"
,
p_
)
case
*
T
:
if
uintptr
(
unsafe
.
Pointer
(
p_
))
!=
pptr
{
t
.
Fatal
(
"Get after objGot -> objLive: T, but ptr is not the same"
)
}
}
assertEq
(
w
.
state
,
objGot
)
GC
()
assertEq
(
w
.
state
,
objLive
)
// fin ran again and again downgraded got -> live
GC
()
assertEq
(
w
.
state
,
objReleased
)
// fin ran again and released the object
assertEq
(
w
.
Get
(),
nil
)
}
}
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