blob: fa415d183fe27586feb4b031359539da764a4497 [file] [log] [blame]
Colin Cross9272ade2016-08-17 15:24:12 -07001// 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
15package android
16
17import (
Colin Cross99c6dfa2017-11-07 13:34:26 -080018 "fmt"
Colin Cross9272ade2016-08-17 15:24:12 -070019 "sync"
Colin Cross9272ade2016-08-17 15:24:12 -070020)
21
22type OncePer struct {
Colin Crosse5cdaf92019-02-11 15:06:16 -080023 values sync.Map
24}
25
26type onceValueWaiter chan bool
27
28func (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 Cross9272ade2016-08-17 15:24:12 -070040}
41
Colin Cross9272ade2016-08-17 15:24:12 -070042// Once computes a value the first time it is called with a given key per OncePer, and returns the
Colin Cross66bdb692019-05-14 11:33:05 -070043// value without recomputing when called with the same key. key must be hashable. If value panics
44// the panic will be propagated but the next call to Once with the same key will return nil.
Colin Crosse48ff5b2019-02-04 13:03:13 -080045func (once *OncePer) Once(key OnceKey, value func() interface{}) interface{} {
Colin Cross99c6dfa2017-11-07 13:34:26 -080046 // Fast path: check if the key is already in the map
47 if v, ok := once.values.Load(key); ok {
Colin Crosse5cdaf92019-02-11 15:06:16 -080048 return once.maybeWaitFor(key, v)
Colin Cross9272ade2016-08-17 15:24:12 -070049 }
50
Colin Crosse5cdaf92019-02-11 15:06:16 -080051 // Slow path: create a OnceValueWrapper and attempt to insert it
52 waiter := make(onceValueWaiter)
53 if v, loaded := once.values.LoadOrStore(key, waiter); loaded {
54 // Got a value, something else inserted its own waiter or a constructed value
55 return once.maybeWaitFor(key, v)
Colin Cross9272ade2016-08-17 15:24:12 -070056 }
57
Colin Cross66bdb692019-05-14 11:33:05 -070058 // The waiter is inserted, call the value constructor, store it, and signal the waiter. Use defer in case
59 // the function panics.
60 var v interface{}
61 defer func() {
62 once.values.Store(key, v)
63 close(waiter)
64 }()
65
66 v = value()
Colin Cross9272ade2016-08-17 15:24:12 -070067
Colin Cross99c6dfa2017-11-07 13:34:26 -080068 return v
69}
70
Colin Cross571cccf2019-02-04 11:22:08 -080071// Get returns the value previously computed with Once for a given key. If Once has not been called for the given
72// key Get will panic.
Colin Crosse48ff5b2019-02-04 13:03:13 -080073func (once *OncePer) Get(key OnceKey) interface{} {
Colin Cross99c6dfa2017-11-07 13:34:26 -080074 v, ok := once.values.Load(key)
75 if !ok {
76 panic(fmt.Errorf("Get() called before Once()"))
77 }
Colin Cross9272ade2016-08-17 15:24:12 -070078
Colin Crossd7cfaee2019-02-15 23:00:48 -080079 return once.maybeWaitFor(key, v)
Colin Cross9272ade2016-08-17 15:24:12 -070080}
81
Joe Onorato2e5e4012022-06-07 17:16:08 -070082// Peek returns the value previously computed with Once for a given key. If Once has not
83// been called for the given key Peek will return ok == false.
84func (once *OncePer) Peek(key OnceKey) (interface{}, bool) {
85 v, ok := once.values.Load(key)
86 if !ok {
87 return nil, false
88 }
89
90 return once.maybeWaitFor(key, v), true
91}
92
Colin Cross571cccf2019-02-04 11:22:08 -080093// OnceStringSlice is the same as Once, but returns the value cast to a []string
Colin Crosse48ff5b2019-02-04 13:03:13 -080094func (once *OncePer) OnceStringSlice(key OnceKey, value func() []string) []string {
Colin Cross9272ade2016-08-17 15:24:12 -070095 return once.Once(key, func() interface{} { return value() }).([]string)
96}
97
Colin Cross571cccf2019-02-04 11:22:08 -080098// OnceStringSlice is the same as Once, but returns two values cast to []string
Colin Crosse48ff5b2019-02-04 13:03:13 -080099func (once *OncePer) Once2StringSlice(key OnceKey, value func() ([]string, []string)) ([]string, []string) {
Colin Cross9272ade2016-08-17 15:24:12 -0700100 type twoStringSlice [2][]string
101 s := once.Once(key, func() interface{} {
102 var s twoStringSlice
103 s[0], s[1] = value()
104 return s
105 }).(twoStringSlice)
106 return s[0], s[1]
107}
Colin Cross571cccf2019-02-04 11:22:08 -0800108
Colin Cross33961b52019-07-11 11:01:22 -0700109// OncePath is the same as Once, but returns the value cast to a Path
110func (once *OncePer) OncePath(key OnceKey, value func() Path) Path {
111 return once.Once(key, func() interface{} { return value() }).(Path)
112}
113
114// OncePath is the same as Once, but returns the value cast to a SourcePath
115func (once *OncePer) OnceSourcePath(key OnceKey, value func() SourcePath) SourcePath {
116 return once.Once(key, func() interface{} { return value() }).(SourcePath)
117}
118
Colin Cross571cccf2019-02-04 11:22:08 -0800119// OnceKey is an opaque type to be used as the key in calls to Once.
120type OnceKey struct {
121 key interface{}
122}
123
124// NewOnceKey returns an opaque OnceKey object for the provided key. Two calls to NewOnceKey with the same key string
125// DO NOT produce the same OnceKey object.
126func NewOnceKey(key string) OnceKey {
127 return OnceKey{&key}
128}
129
130// NewCustomOnceKey returns an opaque OnceKey object for the provided key. The key can be any type that is valid as the
131// key in a map, i.e. comparable. Two calls to NewCustomOnceKey with key values that compare equal will return OnceKey
132// objects that access the same value stored with Once.
133func NewCustomOnceKey(key interface{}) OnceKey {
134 return OnceKey{key}
135}