-
Notifications
You must be signed in to change notification settings - Fork 1
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
2 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,56 @@ | ||
package gls | ||
|
||
import ( | ||
"github.com/lvyahui8/ellyn/ellyn_common/collections" | ||
"sync/atomic" | ||
) | ||
|
||
var m = collections.NewConcurrentMap(2048, func(key interface{}) int { | ||
return int(key.(uint64)) | ||
}) | ||
|
||
var localId int64 = 0 | ||
|
||
type routineLocal struct { | ||
id int64 | ||
} | ||
|
||
func NewRoutineLocal() *routineLocal { | ||
return &routineLocal{ | ||
id: atomic.AddInt64(&localId, 1), | ||
} | ||
} | ||
|
||
func (rl *routineLocal) Set(val interface{}) { | ||
goId := GetGoId() | ||
tableVal, ok := m.Load(goId) | ||
var table map[int64]interface{} | ||
if !ok { | ||
table = make(map[int64]interface{}) | ||
m.Store(goId, table) | ||
} else { | ||
table = tableVal.(map[int64]interface{}) | ||
} | ||
table[rl.id] = val | ||
} | ||
|
||
func (rl *routineLocal) Get() (interface{}, bool) { | ||
goId := GetGoId() | ||
tableVal, ok := m.Load(goId) | ||
if !ok { | ||
return nil, false | ||
} | ||
table := tableVal.(map[int64]interface{}) | ||
res, ok := table[rl.id] | ||
return res, ok | ||
} | ||
|
||
func (rl *routineLocal) Clear() { | ||
goId := GetGoId() | ||
tableVal, ok := m.Load(goId) | ||
if !ok { | ||
return | ||
} | ||
table := tableVal.(map[int64]interface{}) | ||
delete(table, rl.id) | ||
} |
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,40 @@ | ||
package gls | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestRoutineLocalBasic(t *testing.T) { | ||
local := NewRoutineLocal() | ||
local.Set(1) | ||
val, ok := local.Get() | ||
require.True(t, ok) | ||
require.Equal(t, 1, val) | ||
local.Clear() | ||
val, ok = local.Get() | ||
require.False(t, ok) | ||
require.Nil(t, val) | ||
} | ||
|
||
func TestRoutineLocalConcurrent(t *testing.T) { | ||
local := NewRoutineLocal() | ||
local.Set(1) | ||
w := sync.WaitGroup{} | ||
w.Add(1) | ||
go func() { | ||
defer w.Done() | ||
val, ok := local.Get() | ||
require.False(t, ok) | ||
require.Nil(t, val) | ||
local.Set(100) | ||
val, ok = local.Get() | ||
require.True(t, ok) | ||
require.Equal(t, 100, val) | ||
}() | ||
w.Wait() | ||
val, ok := local.Get() | ||
require.True(t, ok) | ||
require.Equal(t, 1, val) | ||
} |