-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
197 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package tls | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"unsafe" | ||
) | ||
|
||
func __xgo_link_on_gonewproc(f func(g uintptr)) { | ||
fmt.Fprintln(os.Stderr, "WARNING: failed to link __xgo_link_on_gonewproc(requires xgo).") | ||
} | ||
|
||
func __xgo_link_getcurg() unsafe.Pointer { | ||
fmt.Fprintln(os.Stderr, "WARNING: failed to link __xgo_link_getcurg(requires xgo).") | ||
return nil | ||
} | ||
|
||
func __xgo_link_on_goexit(fn func()) { | ||
fmt.Fprintln(os.Stderr, "WARNING: failed to link __xgo_link_on_goexit(requires xgo).") | ||
} | ||
|
||
func init() { | ||
__xgo_link_on_goexit(func() { | ||
// clear when exit | ||
g := uintptr(__xgo_link_getcurg()) | ||
mut.Lock() | ||
localKeys := keys | ||
mut.Unlock() | ||
for _, loc := range localKeys { | ||
loc.store.Delete(g) | ||
} | ||
}) | ||
__xgo_link_on_gonewproc(func(newg uintptr) { | ||
// inherit when new goroutine | ||
g := uintptr(__xgo_link_getcurg()) | ||
mut.Lock() | ||
localKeys := keys | ||
mut.Unlock() | ||
for _, loc := range localKeys { | ||
if !loc.inherit { | ||
continue | ||
} | ||
val, ok := loc.store.Load(g) | ||
if !ok { | ||
continue | ||
} | ||
loc.store.Store(newg, val) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package tls | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
var mut sync.Mutex | ||
var keys []*tlsKey | ||
|
||
type TLSKey interface { | ||
Get() interface{} | ||
GetOK() (interface{}, bool) | ||
Set(v interface{}) | ||
} | ||
|
||
type tlsKey struct { | ||
name string // for debugging purepose | ||
inherit bool | ||
store sync.Map // <goroutine ptr> -> interface{} | ||
} | ||
|
||
var _ TLSKey = (*tlsKey)(nil) | ||
|
||
func Declare(name string) TLSKey { | ||
b := &TLSBuilder{ | ||
name: name, | ||
} | ||
return b.Declare() | ||
} | ||
func DeclareInherit(name string) TLSKey { | ||
b := &TLSBuilder{ | ||
name: name, | ||
inherit: true, | ||
} | ||
return b.Declare() | ||
} | ||
|
||
type TLSBuilder struct { | ||
name string | ||
inherit bool | ||
} | ||
|
||
func New() *TLSBuilder { | ||
return &TLSBuilder{} | ||
} | ||
|
||
func (c *TLSBuilder) Name(name string) *TLSBuilder { | ||
c.name = name | ||
return c | ||
} | ||
|
||
func (c *TLSBuilder) Inherit() *TLSBuilder { | ||
c.inherit = true | ||
return c | ||
} | ||
|
||
func (c *TLSBuilder) Declare() TLSKey { | ||
key := &tlsKey{ | ||
name: c.name, | ||
inherit: c.inherit, | ||
} | ||
mut.Lock() | ||
keys = append(keys, key) | ||
mut.Unlock() | ||
return key | ||
} | ||
|
||
func (c *tlsKey) Get() interface{} { | ||
key := uintptr(__xgo_link_getcurg()) | ||
val, ok := c.store.Load(key) | ||
if !ok { | ||
return nil | ||
} | ||
return val | ||
} | ||
|
||
func (c *tlsKey) GetOK() (interface{}, bool) { | ||
key := uintptr(__xgo_link_getcurg()) | ||
return c.store.Load(key) | ||
} | ||
|
||
func (c *tlsKey) Set(v interface{}) { | ||
key := uintptr(__xgo_link_getcurg()) | ||
c.store.Store(key, v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package tls_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/xhd2015/xgo/runtime/tls" | ||
) | ||
|
||
var a = tls.Declare("a") | ||
var b = tls.DeclareInherit("b") | ||
|
||
func TestDeclareLocal(t *testing.T) { | ||
a.Set(1) | ||
|
||
var v1 interface{} | ||
done := make(chan struct{}) | ||
go func() { | ||
v1 = a.Get() | ||
close(done) | ||
}() | ||
<-done | ||
|
||
v2 := a.Get() | ||
|
||
if v1 != nil { | ||
t.Fatalf("expect sub goroutine get nil, actual: %v", v1) | ||
} | ||
|
||
if i := v2.(int); i != 1 { | ||
t.Fatalf("expect current goroutine get %d, actual: %d", 1, i) | ||
} | ||
} | ||
|
||
func TestInerhitLocal(t *testing.T) { | ||
b.Set(1) | ||
|
||
var v1 interface{} | ||
done := make(chan struct{}) | ||
go func() { | ||
v1 = b.Get() | ||
close(done) | ||
}() | ||
<-done | ||
|
||
v2 := b.Get() | ||
|
||
if v1 == nil { | ||
t.Fatalf("expect sub goroutine inherit b, actual: %v", v1) | ||
} | ||
if i := v1.(int); i != 1 { | ||
t.Fatalf("expect sub goroutine get %d, actual: %d", 1, i) | ||
} | ||
if i := v2.(int); i != 1 { | ||
t.Fatalf("expect current goroutine get %d, actual: %d", 1, i) | ||
} | ||
} |