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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Joshua
wendelin.core
Commits
d38d1525
Commit
d38d1525
authored
Jul 13, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
48b1d8da
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
116 additions
and
0 deletions
+116
-0
wcfs/weak.go
wcfs/weak.go
+92
-0
wcfs/weak_test.go
wcfs/weak_test.go
+24
-0
No files found.
wcfs/weak.go
0 → 100644
View file @
d38d1525
// XXX copytight = ?
// based on:
// https://groups.google.com/d/msg/golang-nuts/PYWxjT2v6ps/dL71oJk1mXEJ
// https://play.golang.org/p/f9HY6-z8Pp
package
main
// weak reference
import
(
"runtime"
"sync"
"unsafe"
)
// iface is how Go runtime represents an interface.
//
// NOTE layout must be synchronized to Go runtime representation.
type
iface
struct
{
typ
uintptr
// type
data
uintptr
// data
}
type
weakRefState
int32
const
(
objGot
weakRefState
=
+
1
// WeakRef.Get returned !nil
objLive
weakRefState
=
0
// object is alive, Get did not run yet in this GC cycle
objReleased
weakRefState
=
-
1
// the finalizer marked object as released
)
// WeakRef is a weak reference.
//
// Create one with NewWeakRef and retrieve referenced object with Get.
type
WeakRef
struct
{
iface
// XXX try to do without mutex and only with atomics
mu
sync
.
Mutex
state
weakRefState
}
// XXX
func
NewWeakRef
(
obj
interface
{})
*
WeakRef
{
// original code uses ^ for pointers not to cause GC thinking that original
// object is still referenced from WeakRef.data. However starting with ~ Go1.4
// the GC is precise, so we don't need that trick this days.
i
:=
(
*
[
2
]
uintptr
)(
unsafe
.
Pointer
(
&
obj
))
w
:=
&
WeakRef
{
iface
:
*
(
*
iface
)(
unsafe
.
Pointer
(
&
obj
)),
state
:
objLive
,
}
var
release
func
(
interface
{})
release
=
func
(
_
interface
{})
{
// GC decided that the object is no longer reachable and
// scheduled us to run as finalizer. During the time till we
// actually run, WeakRef.Get might have been come to run and
// "rematerializing" the object for use. Check if we do not
// race with any Get in progress, and reschedule us to retry at
// next GC if we do.
w
.
mu
.
Lock
()
if
w
.
state
==
objGot
{
w
.
state
=
objLive
runtime
.
SetFinalizer
(
obj
,
release
)
}
else
{
w
.
state
=
objReleased
}
w
.
mu
.
Unlock
()
}
runtime
.
SetFinalizer
(
obj
,
release
)
return
w
}
// Get returns object pointed to by this weak reference.
//
// If original object is still alive - it is returned.
// If not - nil is returned.
func
(
w
*
WeakRef
)
Get
()
(
obj
interface
{})
{
w
.
mu
.
Lock
()
if
w
.
state
!=
objReleased
{
w
.
state
=
objGot
// recreate interface{} from saved words.
// XXX do writes as pointers so that compiler emits write barriers to notify GC?
i
:=
(
*
iface
)(
unsafe
.
Pointer
(
&
obj
))
*
i
=
w
.
iface
}
w
.
mu
.
Unlock
()
return
obj
}
wcfs/weak_test.go
0 → 100644
View file @
d38d1525
// Copyright (C) 2018 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
package
main
// sizeof(interface{}) == 2*sizeof(uintptr)
// v -> weakref -> get -> == v
// GC -> get -> alive -> GC get alive -> GC get alive -> GC GC get ø
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