Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Colin Cross | 99c6dfa | 2017-11-07 13:34:26 -0800 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 19 | "sync" |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | type OncePer struct { |
Colin Cross | e5cdaf9 | 2019-02-11 15:06:16 -0800 | [diff] [blame] | 23 | values sync.Map |
| 24 | } |
| 25 | |
| 26 | type onceValueWaiter chan bool |
| 27 | |
| 28 | func (once *OncePer) maybeWaitFor(key OnceKey, value interface{}) interface{} { |
| 29 | if wait, isWaiter := value.(onceValueWaiter); isWaiter { |
| 30 | // The entry in the map is a placeholder waiter because something else is constructing the value |
| 31 | // wait until the waiter is signalled, then load the real value. |
| 32 | <-wait |
| 33 | value, _ = once.values.Load(key) |
| 34 | if _, isWaiter := value.(onceValueWaiter); isWaiter { |
| 35 | panic(fmt.Errorf("Once() waiter completed but key is still not valid")) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return value |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 42 | // Once computes a value the first time it is called with a given key per OncePer, and returns the |
| 43 | // value without recomputing when called with the same key. key must be hashable. |
Colin Cross | e48ff5b | 2019-02-04 13:03:13 -0800 | [diff] [blame] | 44 | func (once *OncePer) Once(key OnceKey, value func() interface{}) interface{} { |
Colin Cross | 99c6dfa | 2017-11-07 13:34:26 -0800 | [diff] [blame] | 45 | // Fast path: check if the key is already in the map |
| 46 | if v, ok := once.values.Load(key); ok { |
Colin Cross | e5cdaf9 | 2019-02-11 15:06:16 -0800 | [diff] [blame] | 47 | return once.maybeWaitFor(key, v) |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Colin Cross | e5cdaf9 | 2019-02-11 15:06:16 -0800 | [diff] [blame] | 50 | // Slow path: create a OnceValueWrapper and attempt to insert it |
| 51 | waiter := make(onceValueWaiter) |
| 52 | if v, loaded := once.values.LoadOrStore(key, waiter); loaded { |
| 53 | // Got a value, something else inserted its own waiter or a constructed value |
| 54 | return once.maybeWaitFor(key, v) |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Colin Cross | e5cdaf9 | 2019-02-11 15:06:16 -0800 | [diff] [blame] | 57 | // The waiter is inserted, call the value constructor, store it, and signal the waiter |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 58 | v := value() |
Colin Cross | 99c6dfa | 2017-11-07 13:34:26 -0800 | [diff] [blame] | 59 | once.values.Store(key, v) |
Colin Cross | e5cdaf9 | 2019-02-11 15:06:16 -0800 | [diff] [blame] | 60 | close(waiter) |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 61 | |
Colin Cross | 99c6dfa | 2017-11-07 13:34:26 -0800 | [diff] [blame] | 62 | return v |
| 63 | } |
| 64 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 65 | // Get returns the value previously computed with Once for a given key. If Once has not been called for the given |
| 66 | // key Get will panic. |
Colin Cross | e48ff5b | 2019-02-04 13:03:13 -0800 | [diff] [blame] | 67 | func (once *OncePer) Get(key OnceKey) interface{} { |
Colin Cross | 99c6dfa | 2017-11-07 13:34:26 -0800 | [diff] [blame] | 68 | v, ok := once.values.Load(key) |
| 69 | if !ok { |
| 70 | panic(fmt.Errorf("Get() called before Once()")) |
| 71 | } |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 72 | |
Colin Cross | d7cfaee | 2019-02-15 23:00:48 -0800 | [diff] [blame] | 73 | return once.maybeWaitFor(key, v) |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 76 | // OnceStringSlice is the same as Once, but returns the value cast to a []string |
Colin Cross | e48ff5b | 2019-02-04 13:03:13 -0800 | [diff] [blame] | 77 | func (once *OncePer) OnceStringSlice(key OnceKey, value func() []string) []string { |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 78 | return once.Once(key, func() interface{} { return value() }).([]string) |
| 79 | } |
| 80 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 81 | // OnceStringSlice is the same as Once, but returns two values cast to []string |
Colin Cross | e48ff5b | 2019-02-04 13:03:13 -0800 | [diff] [blame] | 82 | func (once *OncePer) Once2StringSlice(key OnceKey, value func() ([]string, []string)) ([]string, []string) { |
Colin Cross | 9272ade | 2016-08-17 15:24:12 -0700 | [diff] [blame] | 83 | type twoStringSlice [2][]string |
| 84 | s := once.Once(key, func() interface{} { |
| 85 | var s twoStringSlice |
| 86 | s[0], s[1] = value() |
| 87 | return s |
| 88 | }).(twoStringSlice) |
| 89 | return s[0], s[1] |
| 90 | } |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 91 | |
| 92 | // OnceKey is an opaque type to be used as the key in calls to Once. |
| 93 | type OnceKey struct { |
| 94 | key interface{} |
| 95 | } |
| 96 | |
| 97 | // NewOnceKey returns an opaque OnceKey object for the provided key. Two calls to NewOnceKey with the same key string |
| 98 | // DO NOT produce the same OnceKey object. |
| 99 | func NewOnceKey(key string) OnceKey { |
| 100 | return OnceKey{&key} |
| 101 | } |
| 102 | |
| 103 | // NewCustomOnceKey returns an opaque OnceKey object for the provided key. The key can be any type that is valid as the |
| 104 | // key in a map, i.e. comparable. Two calls to NewCustomOnceKey with key values that compare equal will return OnceKey |
| 105 | // objects that access the same value stored with Once. |
| 106 | func NewCustomOnceKey(key interface{}) OnceKey { |
| 107 | return OnceKey{key} |
| 108 | } |