Files
tasks/common/future/future_impl.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

98 lines
1.7 KiB
Go

package future
import (
"context"
"sync/atomic"
)
const (
// pending status indicates future is not ready
// setting status indicates future is in transition to be ready, used to prevent data race
// ready status indicates future is ready
pending int32 = iota
setting
ready
)
type (
FutureImpl[T any] struct {
status int32
readyCh chan struct{}
value T
err error
}
)
func NewFuture[T any]() *FutureImpl[T] {
var value T
return &FutureImpl[T]{
status: pending,
readyCh: make(chan struct{}),
value: value,
err: nil,
}
}
func (f *FutureImpl[T]) Get(
ctx context.Context,
) (T, error) {
if f.Ready() {
return f.value, f.err
}
select {
case <-f.readyCh:
return f.value, f.err
case <-ctx.Done():
var value T
return value, ctx.Err()
}
}
func (f *FutureImpl[T]) Set(
value T,
err error,
) {
// cannot directly set status to `ready`, to prevent data race in case multiple `Get` occurs
// instead set status to `setting` to prevent concurrent completion of this future
if !atomic.CompareAndSwapInt32(
&f.status,
pending,
setting,
) {
panic("future has already been completed")
}
f.value = value
f.err = err
atomic.CompareAndSwapInt32(&f.status, setting, ready)
close(f.readyCh)
}
// Sets the value of the future, if it has not been set already. Returns true if this call set the value.
func (f *FutureImpl[T]) SetIfNotReady(
value T,
err error,
) bool {
if !atomic.CompareAndSwapInt32(
&f.status,
pending,
setting,
) {
return false
}
f.value = value
f.err = err
atomic.CompareAndSwapInt32(&f.status, setting, ready)
close(f.readyCh)
return true
}
func (f *FutureImpl[T]) Ready() bool {
return atomic.LoadInt32(&f.status) == ready
}