LEFT | RIGHT |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package sync | 5 package sync |
6 | 6 |
7 // A Pool is a set of temporary objects that may be individually saved | 7 // A Pool is a set of temporary objects that may be individually saved |
8 // and retrieved. | 8 // and retrieved. |
9 // | 9 // |
10 // Any item stored in the Pool may be removed automatically by the | 10 // Any item stored in the Pool may be removed automatically by the |
11 // implementation at any time without notification. | 11 // implementation at any time without notification. |
12 // If the Pool holds the only reference when this happens, the item | 12 // If the Pool holds the only reference when this happens, the item |
13 // might be deallocated. | 13 // might be deallocated. |
14 // | 14 // |
15 // A Pool is safe for use by multiple goroutines simultaneously. | 15 // A Pool is safe for use by multiple goroutines simultaneously. |
16 // | 16 // |
17 // Pool's intended use is for free lists maintained in global variables, | 17 // Pool's intended use is for free lists maintained in global variables, |
18 // typically accessed by multiple goroutines simultaneously. Using a | 18 // typically accessed by multiple goroutines simultaneously. Using a |
19 // Pool instead of a custom free list allows the runtime to reclaim | 19 // Pool instead of a custom free list allows the runtime to reclaim |
20 // entries from the free list when it makes sense to do so. An | 20 // entries from the pool when it makes sense to do so. An |
21 // appropriate use of sync.Pool is to create a pool of temporary buffers | 21 // appropriate use of sync.Pool is to create a pool of temporary buffers |
22 // shared between independent clients of a global resource. On the | 22 // shared between independent clients of a global resource. On the |
23 // other hand, if a free list is maintained as part of an object used | 23 // other hand, if a free list is maintained as part of an object used |
24 // only by a single client and freed when the client completes, | 24 // only by a single client and freed when the client completes, |
25 // implementing that free list as a Pool is not appropriate. | 25 // implementing that free list as a Pool is not appropriate. |
26 // | 26 // |
27 // This is an experimental package and might not be released. | 27 // This is an experimental type and might not be released. |
28 type Pool struct { | 28 type Pool struct { |
29 next *Pool // for use by runtime. must be first. | 29 next *Pool // for use by runtime. must be first. |
30 list []interface{} // offset known to runtime | 30 list []interface{} // offset known to runtime |
31 mu Mutex // guards list | 31 mu Mutex // guards list |
32 | 32 |
33 // New optionally specifies a function to generate | 33 // New optionally specifies a function to generate |
34 // a value when Get would otherwise return nil. | 34 // a value when Get would otherwise return nil. |
35 // It may not be changed concurrently with calls to Get. | 35 // It may not be changed concurrently with calls to Get. |
36 New func() interface{} | 36 New func() interface{} |
37 } | 37 } |
(...skipping 28 matching lines...) Expand all Loading... |
66 x = p.list[n-1] | 66 x = p.list[n-1] |
67 p.list[n-1] = nil // Just to be safe | 67 p.list[n-1] = nil // Just to be safe |
68 p.list = p.list[:n-1] | 68 p.list = p.list[:n-1] |
69 } | 69 } |
70 p.mu.Unlock() | 70 p.mu.Unlock() |
71 if x == nil && p.New != nil { | 71 if x == nil && p.New != nil { |
72 x = p.New() | 72 x = p.New() |
73 } | 73 } |
74 return x | 74 return x |
75 } | 75 } |
LEFT | RIGHT |