File: pool.go

package info (click to toggle)
golang-github-hanwen-go-fuse 0.0~git20190214.58dcd77-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,076 kB
  • sloc: cpp: 78; sh: 77; makefile: 16
file content (106 lines) | stat: -rw-r--r-- 1,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package splice

import (
	"sync"
)

var splicePool *pairPool

type pairPool struct {
	sync.Mutex
	unused    []*Pair
	usedCount int
}

func ClearSplicePool() {
	splicePool.clear()
}

func Get() (*Pair, error) {
	return splicePool.get()
}

func Total() int {
	return splicePool.total()
}

func Used() int {
	return splicePool.used()
}

// Done returns the pipe pair to pool.
func Done(p *Pair) {
	splicePool.done(p)
}

// Closes and discards pipe pair.
func Drop(p *Pair) {
	splicePool.drop(p)
}

func newSplicePairPool() *pairPool {
	return &pairPool{}
}

func (pp *pairPool) clear() {
	pp.Lock()
	for _, p := range pp.unused {
		p.Close()
	}
	pp.unused = pp.unused[:0]
	pp.Unlock()
}

func (pp *pairPool) used() (n int) {
	pp.Lock()
	n = pp.usedCount
	pp.Unlock()

	return n
}

func (pp *pairPool) total() int {
	pp.Lock()
	n := pp.usedCount + len(pp.unused)
	pp.Unlock()
	return n
}

func (pp *pairPool) drop(p *Pair) {
	p.Close()
	pp.Lock()
	pp.usedCount--
	pp.Unlock()
}

func (pp *pairPool) get() (p *Pair, err error) {
	pp.Lock()
	defer pp.Unlock()

	pp.usedCount++
	l := len(pp.unused)
	if l > 0 {
		p := pp.unused[l-1]
		pp.unused = pp.unused[:l-1]
		return p, nil
	}

	return newSplicePair()
}

func (pp *pairPool) done(p *Pair) {
	p.discard()

	pp.Lock()
	pp.usedCount--
	pp.unused = append(pp.unused, p)
	pp.Unlock()
}

func init() {
	splicePool = newSplicePairPool()
}