onceper.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. package android
  15. import (
  16. "fmt"
  17. "sync"
  18. )
  19. type OncePer struct {
  20. values sync.Map
  21. }
  22. type onceValueWaiter chan bool
  23. func (once *OncePer) maybeWaitFor(key OnceKey, value interface{}) interface{} {
  24. if wait, isWaiter := value.(onceValueWaiter); isWaiter {
  25. // The entry in the map is a placeholder waiter because something else is constructing the value
  26. // wait until the waiter is signalled, then load the real value.
  27. <-wait
  28. value, _ = once.values.Load(key)
  29. if _, isWaiter := value.(onceValueWaiter); isWaiter {
  30. panic(fmt.Errorf("Once() waiter completed but key is still not valid"))
  31. }
  32. }
  33. return value
  34. }
  35. // Once computes a value the first time it is called with a given key per OncePer, and returns the
  36. // value without recomputing when called with the same key. key must be hashable. If value panics
  37. // the panic will be propagated but the next call to Once with the same key will return nil.
  38. func (once *OncePer) Once(key OnceKey, value func() interface{}) interface{} {
  39. // Fast path: check if the key is already in the map
  40. if v, ok := once.values.Load(key); ok {
  41. return once.maybeWaitFor(key, v)
  42. }
  43. // Slow path: create a OnceValueWrapper and attempt to insert it
  44. waiter := make(onceValueWaiter)
  45. if v, loaded := once.values.LoadOrStore(key, waiter); loaded {
  46. // Got a value, something else inserted its own waiter or a constructed value
  47. return once.maybeWaitFor(key, v)
  48. }
  49. // The waiter is inserted, call the value constructor, store it, and signal the waiter. Use defer in case
  50. // the function panics.
  51. var v interface{}
  52. defer func() {
  53. once.values.Store(key, v)
  54. close(waiter)
  55. }()
  56. v = value()
  57. return v
  58. }
  59. // Get returns the value previously computed with Once for a given key. If Once has not been called for the given
  60. // key Get will panic.
  61. func (once *OncePer) Get(key OnceKey) interface{} {
  62. v, ok := once.values.Load(key)
  63. if !ok {
  64. panic(fmt.Errorf("Get() called before Once()"))
  65. }
  66. return once.maybeWaitFor(key, v)
  67. }
  68. // Peek returns the value previously computed with Once for a given key. If Once has not
  69. // been called for the given key Peek will return ok == false.
  70. func (once *OncePer) Peek(key OnceKey) (interface{}, bool) {
  71. v, ok := once.values.Load(key)
  72. if !ok {
  73. return nil, false
  74. }
  75. return once.maybeWaitFor(key, v), true
  76. }
  77. // OnceStringSlice is the same as Once, but returns the value cast to a []string
  78. func (once *OncePer) OnceStringSlice(key OnceKey, value func() []string) []string {
  79. return once.Once(key, func() interface{} { return value() }).([]string)
  80. }
  81. // OnceStringSlice is the same as Once, but returns two values cast to []string
  82. func (once *OncePer) Once2StringSlice(key OnceKey, value func() ([]string, []string)) ([]string, []string) {
  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. }
  91. // OncePath is the same as Once, but returns the value cast to a Path
  92. func (once *OncePer) OncePath(key OnceKey, value func() Path) Path {
  93. return once.Once(key, func() interface{} { return value() }).(Path)
  94. }
  95. // OncePath is the same as Once, but returns the value cast to a SourcePath
  96. func (once *OncePer) OnceSourcePath(key OnceKey, value func() SourcePath) SourcePath {
  97. return once.Once(key, func() interface{} { return value() }).(SourcePath)
  98. }
  99. // OnceKey is an opaque type to be used as the key in calls to Once.
  100. type OnceKey struct {
  101. key interface{}
  102. }
  103. // NewOnceKey returns an opaque OnceKey object for the provided key. Two calls to NewOnceKey with the same key string
  104. // DO NOT produce the same OnceKey object.
  105. func NewOnceKey(key string) OnceKey {
  106. return OnceKey{&key}
  107. }
  108. // NewCustomOnceKey returns an opaque OnceKey object for the provided key. The key can be any type that is valid as the
  109. // key in a map, i.e. comparable. Two calls to NewCustomOnceKey with key values that compare equal will return OnceKey
  110. // objects that access the same value stored with Once.
  111. func NewCustomOnceKey(key interface{}) OnceKey {
  112. return OnceKey{key}
  113. }