| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package mathrand | 5 package mathrand |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "math/rand" | 8 "math/rand" |
| 9 "sync" | 9 "sync" |
| 10 ) | 10 ) |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // the output using: | 87 // the output using: |
| 88 // | 88 // |
| 89 // sample = ExpFloat64(ctx) / desiredRateParameter | 89 // sample = ExpFloat64(ctx) / desiredRateParameter |
| 90 // | 90 // |
| 91 ExpFloat64() float64 | 91 ExpFloat64() float64 |
| 92 | 92 |
| 93 // WithGoRand invokes the supplied "fn" while holding an exclusive lock | 93 // WithGoRand invokes the supplied "fn" while holding an exclusive lock |
| 94 // for it. This can be used by callers to pull and use a *rand.Rand inst
ance | 94 // for it. This can be used by callers to pull and use a *rand.Rand inst
ance |
| 95 // out of the Context safely. | 95 // out of the Context safely. |
| 96 // | 96 // |
| 97 // Other "mathrand" functions and objects MUST NOT BE USED inside the |
| 98 // callback, as WithGoRand holds the lock to the current Rand instance,
so any |
| 99 // additional function call will deadlock. |
| 100 // |
| 97 // The callback's r must not be retained or used outside of hte scope of
the | 101 // The callback's r must not be retained or used outside of hte scope of
the |
| 98 // callback. | 102 // callback. |
| 99 WithGoRand(fn func(r *rand.Rand) error) error | 103 WithGoRand(fn func(r *rand.Rand) error) error |
| 100 } | 104 } |
| 101 | 105 |
| 102 // Locking wraps a Rand instance in a layer that locks around all of its | 106 // Locking wraps a Rand instance in a layer that locks around all of its |
| 103 // methods. | 107 // methods. |
| 104 // | 108 // |
| 105 // A user must hold Locking's Mutex if the want to directly access and use | 109 // A user must hold Locking's Mutex if the want to directly access and use |
| 106 // Locking's R member safely. | 110 // Locking's R member safely. |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 | 264 |
| 261 // wrapped is a simple wrapper to a *math.Rand instance. | 265 // wrapped is a simple wrapper to a *math.Rand instance. |
| 262 type wrapped struct { | 266 type wrapped struct { |
| 263 *rand.Rand | 267 *rand.Rand |
| 264 } | 268 } |
| 265 | 269 |
| 266 // Wrap wraps a Rand instance, allowing it to satisfy the Rand interface. | 270 // Wrap wraps a Rand instance, allowing it to satisfy the Rand interface. |
| 267 func wrapRand(r *rand.Rand) Rand { return wrapped{r} } | 271 func wrapRand(r *rand.Rand) Rand { return wrapped{r} } |
| 268 | 272 |
| 269 func (w wrapped) WithGoRand(fn func(r *rand.Rand) error) error { return fn(w.Ran
d) } | 273 func (w wrapped) WithGoRand(fn func(r *rand.Rand) error) error { return fn(w.Ran
d) } |
| OLD | NEW |