• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package android
16
17import (
18	"fmt"
19	"sync"
20)
21
22type OncePer struct {
23	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
40}
41
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.  If value panics
44// the panic will be propagated but the next call to Once with the same key will return nil.
45func (once *OncePer) Once(key OnceKey, value func() interface{}) interface{} {
46	// Fast path: check if the key is already in the map
47	if v, ok := once.values.Load(key); ok {
48		return once.maybeWaitFor(key, v)
49	}
50
51	// 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)
56	}
57
58	// 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()
67
68	return v
69}
70
71// 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.
73func (once *OncePer) Get(key OnceKey) interface{} {
74	v, ok := once.values.Load(key)
75	if !ok {
76		panic(fmt.Errorf("Get() called before Once()"))
77	}
78
79	return once.maybeWaitFor(key, v)
80}
81
82// OnceStringSlice is the same as Once, but returns the value cast to a []string
83func (once *OncePer) OnceStringSlice(key OnceKey, value func() []string) []string {
84	return once.Once(key, func() interface{} { return value() }).([]string)
85}
86
87// OnceStringSlice is the same as Once, but returns two values cast to []string
88func (once *OncePer) Once2StringSlice(key OnceKey, value func() ([]string, []string)) ([]string, []string) {
89	type twoStringSlice [2][]string
90	s := once.Once(key, func() interface{} {
91		var s twoStringSlice
92		s[0], s[1] = value()
93		return s
94	}).(twoStringSlice)
95	return s[0], s[1]
96}
97
98// OncePath is the same as Once, but returns the value cast to a Path
99func (once *OncePer) OncePath(key OnceKey, value func() Path) Path {
100	return once.Once(key, func() interface{} { return value() }).(Path)
101}
102
103// OncePath is the same as Once, but returns the value cast to a SourcePath
104func (once *OncePer) OnceSourcePath(key OnceKey, value func() SourcePath) SourcePath {
105	return once.Once(key, func() interface{} { return value() }).(SourcePath)
106}
107
108// OnceKey is an opaque type to be used as the key in calls to Once.
109type OnceKey struct {
110	key interface{}
111}
112
113// NewOnceKey returns an opaque OnceKey object for the provided key.  Two calls to NewOnceKey with the same key string
114// DO NOT produce the same OnceKey object.
115func NewOnceKey(key string) OnceKey {
116	return OnceKey{&key}
117}
118
119// NewCustomOnceKey returns an opaque OnceKey object for the provided key.  The key can be any type that is valid as the
120// key in a map, i.e. comparable.  Two calls to NewCustomOnceKey with key values that compare equal will return OnceKey
121// objects that access the same value stored with Once.
122func NewCustomOnceKey(key interface{}) OnceKey {
123	return OnceKey{key}
124}
125