Move p9.pool to a separate package
PiperOrigin-RevId: 293617493
This commit is contained in:
parent
1b6a12a768
commit
5ff780891e
|
@ -16,7 +16,6 @@ go_library(
|
|||
"messages.go",
|
||||
"p9.go",
|
||||
"path_tree.go",
|
||||
"pool.go",
|
||||
"server.go",
|
||||
"transport.go",
|
||||
"transport_flipcall.go",
|
||||
|
@ -27,6 +26,7 @@ go_library(
|
|||
"//pkg/fdchannel",
|
||||
"//pkg/flipcall",
|
||||
"//pkg/log",
|
||||
"//pkg/pool",
|
||||
"//pkg/sync",
|
||||
"//pkg/unet",
|
||||
"@org_golang_x_sys//unix:go_default_library",
|
||||
|
@ -41,7 +41,6 @@ go_test(
|
|||
"client_test.go",
|
||||
"messages_test.go",
|
||||
"p9_test.go",
|
||||
"pool_test.go",
|
||||
"transport_test.go",
|
||||
"version_test.go",
|
||||
],
|
||||
|
|
|
@ -22,6 +22,7 @@ import (
|
|||
"golang.org/x/sys/unix"
|
||||
"gvisor.dev/gvisor/pkg/flipcall"
|
||||
"gvisor.dev/gvisor/pkg/log"
|
||||
"gvisor.dev/gvisor/pkg/pool"
|
||||
"gvisor.dev/gvisor/pkg/sync"
|
||||
"gvisor.dev/gvisor/pkg/unet"
|
||||
)
|
||||
|
@ -74,10 +75,10 @@ type Client struct {
|
|||
socket *unet.Socket
|
||||
|
||||
// tagPool is the collection of available tags.
|
||||
tagPool pool
|
||||
tagPool pool.Pool
|
||||
|
||||
// fidPool is the collection of available fids.
|
||||
fidPool pool
|
||||
fidPool pool.Pool
|
||||
|
||||
// messageSize is the maximum total size of a message.
|
||||
messageSize uint32
|
||||
|
@ -155,8 +156,8 @@ func NewClient(socket *unet.Socket, messageSize uint32, version string) (*Client
|
|||
}
|
||||
c := &Client{
|
||||
socket: socket,
|
||||
tagPool: pool{start: 1, limit: uint64(NoTag)},
|
||||
fidPool: pool{start: 1, limit: uint64(NoFID)},
|
||||
tagPool: pool.Pool{Start: 1, Limit: uint64(NoTag)},
|
||||
fidPool: pool.Pool{Start: 1, Limit: uint64(NoFID)},
|
||||
pending: make(map[Tag]*response),
|
||||
recvr: make(chan bool, 1),
|
||||
messageSize: messageSize,
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
load("//tools:defs.bzl", "go_library", "go_test")
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
licenses = ["notice"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "pool",
|
||||
srcs = [
|
||||
"pool.go",
|
||||
],
|
||||
deps = [
|
||||
"//pkg/sync",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "pool_test",
|
||||
size = "small",
|
||||
srcs = [
|
||||
"pool_test.go",
|
||||
],
|
||||
library = ":pool",
|
||||
)
|
|
@ -12,33 +12,31 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package p9
|
||||
package pool
|
||||
|
||||
import (
|
||||
"gvisor.dev/gvisor/pkg/sync"
|
||||
)
|
||||
|
||||
// pool is a simple allocator.
|
||||
//
|
||||
// It is used for both tags and FIDs.
|
||||
type pool struct {
|
||||
// Pool is a simple allocator.
|
||||
type Pool struct {
|
||||
mu sync.Mutex
|
||||
|
||||
// cache is the set of returned values.
|
||||
cache []uint64
|
||||
|
||||
// start is the starting value (if needed).
|
||||
start uint64
|
||||
// Start is the starting value (if needed).
|
||||
Start uint64
|
||||
|
||||
// max is the current maximum issued.
|
||||
max uint64
|
||||
|
||||
// limit is the upper limit.
|
||||
limit uint64
|
||||
// Limit is the upper limit.
|
||||
Limit uint64
|
||||
}
|
||||
|
||||
// Get gets a value from the pool.
|
||||
func (p *pool) Get() (uint64, bool) {
|
||||
func (p *Pool) Get() (uint64, bool) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
|
@ -50,18 +48,18 @@ func (p *pool) Get() (uint64, bool) {
|
|||
}
|
||||
|
||||
// Over the limit?
|
||||
if p.start == p.limit {
|
||||
if p.Start == p.Limit {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Generate a new value.
|
||||
v := p.start
|
||||
p.start++
|
||||
v := p.Start
|
||||
p.Start++
|
||||
return v, true
|
||||
}
|
||||
|
||||
// Put returns a value to the pool.
|
||||
func (p *pool) Put(v uint64) {
|
||||
func (p *Pool) Put(v uint64) {
|
||||
p.mu.Lock()
|
||||
p.cache = append(p.cache, v)
|
||||
p.mu.Unlock()
|
|
@ -12,14 +12,14 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package p9
|
||||
package pool
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPoolUnique(t *testing.T) {
|
||||
p := pool{start: 1, limit: 3}
|
||||
p := Pool{Start: 1, Limit: 3}
|
||||
got := make(map[uint64]bool)
|
||||
|
||||
for {
|
||||
|
@ -39,7 +39,7 @@ func TestPoolUnique(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExausted(t *testing.T) {
|
||||
p := pool{start: 1, limit: 500}
|
||||
p := Pool{Start: 1, Limit: 500}
|
||||
for i := 0; i < 499; i++ {
|
||||
_, ok := p.Get()
|
||||
if !ok {
|
||||
|
@ -54,7 +54,7 @@ func TestExausted(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPoolRecycle(t *testing.T) {
|
||||
p := pool{start: 1, limit: 500}
|
||||
p := Pool{Start: 1, Limit: 500}
|
||||
n1, _ := p.Get()
|
||||
p.Put(n1)
|
||||
n2, _ := p.Get()
|
Loading…
Reference in New Issue