pulumi/sdk/go/internal/workgroup.go

72 lines
1.6 KiB
Go
Raw Permalink Normal View History

sdk/go: Move Output implementation to internal **Note to reviewers**: GitHub's PR view will show a lot more for this change than the actual code added/removed. Consider opening a `git diff` of this with, git diff -C --diff-algorithm=minimal This will correctly detect the bulk of the diff as "copy a file and delete a bunch of stuff from it." --- In preparation for implementing generic variants of Input and Output in a pulumix subpackage, move some of the requisite types to internal/ so that we don't have a cyclic dependency between pulumi and pulumix. This change is largely mechanical with adjustmenets made for compilation. For places in pulumi/ where internal state of an output was accessed, new top-level functions were added to internal/ that expose this information. Changes were noting explicitly: - `OutputState` was changed to record dependencies as `[]internal.Resource` instead of `[]Resource` because moving `Resource` into internal will end up moving most of pulumi/ into internal. We cast to/from internal.Resource as needed. - `internal.AnyOutputType` is a `reflect.Type` filled by sdk/go/pulumi at `init()` time. This is an annoying hack, but we can't move AnyOutput into internal. - `internal.FullyResolvedTypes` is similarly filled by sdk/go/pulumi at `init()` time. To add confidence to this change being safe, I used [apidiff] to compare the API of the go/pulumi package befor and after this change. The command reported no changes. [apidiff]: https://pkg.go.dev/golang.org/x/exp@v0.0.0-20230713183714-613f0c0eb8a1/cmd/apidiff On top of that, I also ran `go doc -all .` with the before/after and ran a text diff on the result. The following changes were reported: - `Input` is now an alias to `internal.Input` - `Output` is now an alias to `internal.Output` - `OutputState` is now an alias to `internal.OutputState` - `OutputState.ApplyT`, `OutputState.ApplyTWithContext` were deleted (except they're present on the alias so they're still available) - `Resource` now embeds `internal.Resource` Resolves #13585
2023-06-14 21:29:43 +00:00
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
sdk/go: Move Output implementation to internal **Note to reviewers**: GitHub's PR view will show a lot more for this change than the actual code added/removed. Consider opening a `git diff` of this with, git diff -C --diff-algorithm=minimal This will correctly detect the bulk of the diff as "copy a file and delete a bunch of stuff from it." --- In preparation for implementing generic variants of Input and Output in a pulumix subpackage, move some of the requisite types to internal/ so that we don't have a cyclic dependency between pulumi and pulumix. This change is largely mechanical with adjustmenets made for compilation. For places in pulumi/ where internal state of an output was accessed, new top-level functions were added to internal/ that expose this information. Changes were noting explicitly: - `OutputState` was changed to record dependencies as `[]internal.Resource` instead of `[]Resource` because moving `Resource` into internal will end up moving most of pulumi/ into internal. We cast to/from internal.Resource as needed. - `internal.AnyOutputType` is a `reflect.Type` filled by sdk/go/pulumi at `init()` time. This is an annoying hack, but we can't move AnyOutput into internal. - `internal.FullyResolvedTypes` is similarly filled by sdk/go/pulumi at `init()` time. To add confidence to this change being safe, I used [apidiff] to compare the API of the go/pulumi package befor and after this change. The command reported no changes. [apidiff]: https://pkg.go.dev/golang.org/x/exp@v0.0.0-20230713183714-613f0c0eb8a1/cmd/apidiff On top of that, I also ran `go doc -all .` with the before/after and ran a text diff on the result. The following changes were reported: - `Input` is now an alias to `internal.Input` - `Output` is now an alias to `internal.Output` - `OutputState` is now an alias to `internal.OutputState` - `OutputState.ApplyT`, `OutputState.ApplyTWithContext` were deleted (except they're present on the alias so they're still available) - `Resource` now embeds `internal.Resource` Resolves #13585
2023-06-14 21:29:43 +00:00
package internal
import (
"fmt"
"sync"
)
sdk/go: Move Output implementation to internal **Note to reviewers**: GitHub's PR view will show a lot more for this change than the actual code added/removed. Consider opening a `git diff` of this with, git diff -C --diff-algorithm=minimal This will correctly detect the bulk of the diff as "copy a file and delete a bunch of stuff from it." --- In preparation for implementing generic variants of Input and Output in a pulumix subpackage, move some of the requisite types to internal/ so that we don't have a cyclic dependency between pulumi and pulumix. This change is largely mechanical with adjustmenets made for compilation. For places in pulumi/ where internal state of an output was accessed, new top-level functions were added to internal/ that expose this information. Changes were noting explicitly: - `OutputState` was changed to record dependencies as `[]internal.Resource` instead of `[]Resource` because moving `Resource` into internal will end up moving most of pulumi/ into internal. We cast to/from internal.Resource as needed. - `internal.AnyOutputType` is a `reflect.Type` filled by sdk/go/pulumi at `init()` time. This is an annoying hack, but we can't move AnyOutput into internal. - `internal.FullyResolvedTypes` is similarly filled by sdk/go/pulumi at `init()` time. To add confidence to this change being safe, I used [apidiff] to compare the API of the go/pulumi package befor and after this change. The command reported no changes. [apidiff]: https://pkg.go.dev/golang.org/x/exp@v0.0.0-20230713183714-613f0c0eb8a1/cmd/apidiff On top of that, I also ran `go doc -all .` with the before/after and ran a text diff on the result. The following changes were reported: - `Input` is now an alias to `internal.Input` - `Output` is now an alias to `internal.Output` - `OutputState` is now an alias to `internal.OutputState` - `OutputState.ApplyT`, `OutputState.ApplyTWithContext` were deleted (except they're present on the alias so they're still available) - `Resource` now embeds `internal.Resource` Resolves #13585
2023-06-14 21:29:43 +00:00
// WorkGroup mimicks the interface of `sync.WaitGroup` but does not panic in
// case of races between `Wait` and `Add` with a positive delta in the
// state with a zero counter. The reason `sync.WaitGroup` panics is to
sdk/go: Move Output implementation to internal **Note to reviewers**: GitHub's PR view will show a lot more for this change than the actual code added/removed. Consider opening a `git diff` of this with, git diff -C --diff-algorithm=minimal This will correctly detect the bulk of the diff as "copy a file and delete a bunch of stuff from it." --- In preparation for implementing generic variants of Input and Output in a pulumix subpackage, move some of the requisite types to internal/ so that we don't have a cyclic dependency between pulumi and pulumix. This change is largely mechanical with adjustmenets made for compilation. For places in pulumi/ where internal state of an output was accessed, new top-level functions were added to internal/ that expose this information. Changes were noting explicitly: - `OutputState` was changed to record dependencies as `[]internal.Resource` instead of `[]Resource` because moving `Resource` into internal will end up moving most of pulumi/ into internal. We cast to/from internal.Resource as needed. - `internal.AnyOutputType` is a `reflect.Type` filled by sdk/go/pulumi at `init()` time. This is an annoying hack, but we can't move AnyOutput into internal. - `internal.FullyResolvedTypes` is similarly filled by sdk/go/pulumi at `init()` time. To add confidence to this change being safe, I used [apidiff] to compare the API of the go/pulumi package befor and after this change. The command reported no changes. [apidiff]: https://pkg.go.dev/golang.org/x/exp@v0.0.0-20230713183714-613f0c0eb8a1/cmd/apidiff On top of that, I also ran `go doc -all .` with the before/after and ran a text diff on the result. The following changes were reported: - `Input` is now an alias to `internal.Input` - `Output` is now an alias to `internal.Output` - `OutputState` is now an alias to `internal.OutputState` - `OutputState.ApplyT`, `OutputState.ApplyTWithContext` were deleted (except they're present on the alias so they're still available) - `Resource` now embeds `internal.Resource` Resolves #13585
2023-06-14 21:29:43 +00:00
// warn about a race condition. Using `WorkGroup` implicitly accept
// these race conditions instead. Use sparingly and document why it is
// used.
sdk/go: Move Output implementation to internal **Note to reviewers**: GitHub's PR view will show a lot more for this change than the actual code added/removed. Consider opening a `git diff` of this with, git diff -C --diff-algorithm=minimal This will correctly detect the bulk of the diff as "copy a file and delete a bunch of stuff from it." --- In preparation for implementing generic variants of Input and Output in a pulumix subpackage, move some of the requisite types to internal/ so that we don't have a cyclic dependency between pulumi and pulumix. This change is largely mechanical with adjustmenets made for compilation. For places in pulumi/ where internal state of an output was accessed, new top-level functions were added to internal/ that expose this information. Changes were noting explicitly: - `OutputState` was changed to record dependencies as `[]internal.Resource` instead of `[]Resource` because moving `Resource` into internal will end up moving most of pulumi/ into internal. We cast to/from internal.Resource as needed. - `internal.AnyOutputType` is a `reflect.Type` filled by sdk/go/pulumi at `init()` time. This is an annoying hack, but we can't move AnyOutput into internal. - `internal.FullyResolvedTypes` is similarly filled by sdk/go/pulumi at `init()` time. To add confidence to this change being safe, I used [apidiff] to compare the API of the go/pulumi package befor and after this change. The command reported no changes. [apidiff]: https://pkg.go.dev/golang.org/x/exp@v0.0.0-20230713183714-613f0c0eb8a1/cmd/apidiff On top of that, I also ran `go doc -all .` with the before/after and ran a text diff on the result. The following changes were reported: - `Input` is now an alias to `internal.Input` - `Output` is now an alias to `internal.Output` - `OutputState` is now an alias to `internal.OutputState` - `OutputState.ApplyT`, `OutputState.ApplyTWithContext` were deleted (except they're present on the alias so they're still available) - `Resource` now embeds `internal.Resource` Resolves #13585
2023-06-14 21:29:43 +00:00
type WorkGroup struct {
mutex sync.Mutex
cond *sync.Cond
counter int
}
sdk/go: Move Output implementation to internal **Note to reviewers**: GitHub's PR view will show a lot more for this change than the actual code added/removed. Consider opening a `git diff` of this with, git diff -C --diff-algorithm=minimal This will correctly detect the bulk of the diff as "copy a file and delete a bunch of stuff from it." --- In preparation for implementing generic variants of Input and Output in a pulumix subpackage, move some of the requisite types to internal/ so that we don't have a cyclic dependency between pulumi and pulumix. This change is largely mechanical with adjustmenets made for compilation. For places in pulumi/ where internal state of an output was accessed, new top-level functions were added to internal/ that expose this information. Changes were noting explicitly: - `OutputState` was changed to record dependencies as `[]internal.Resource` instead of `[]Resource` because moving `Resource` into internal will end up moving most of pulumi/ into internal. We cast to/from internal.Resource as needed. - `internal.AnyOutputType` is a `reflect.Type` filled by sdk/go/pulumi at `init()` time. This is an annoying hack, but we can't move AnyOutput into internal. - `internal.FullyResolvedTypes` is similarly filled by sdk/go/pulumi at `init()` time. To add confidence to this change being safe, I used [apidiff] to compare the API of the go/pulumi package befor and after this change. The command reported no changes. [apidiff]: https://pkg.go.dev/golang.org/x/exp@v0.0.0-20230713183714-613f0c0eb8a1/cmd/apidiff On top of that, I also ran `go doc -all .` with the before/after and ran a text diff on the result. The following changes were reported: - `Input` is now an alias to `internal.Input` - `Output` is now an alias to `internal.Output` - `OutputState` is now an alias to `internal.OutputState` - `OutputState.ApplyT`, `OutputState.ApplyTWithContext` were deleted (except they're present on the alias so they're still available) - `Resource` now embeds `internal.Resource` Resolves #13585
2023-06-14 21:29:43 +00:00
func (wg *WorkGroup) Wait() {
wg.mutex.Lock()
defer wg.mutex.Unlock()
if wg.cond == nil {
wg.cond = sync.NewCond(&wg.mutex)
}
for wg.counter > 0 {
wg.cond.Wait()
}
}
sdk/go: Move Output implementation to internal **Note to reviewers**: GitHub's PR view will show a lot more for this change than the actual code added/removed. Consider opening a `git diff` of this with, git diff -C --diff-algorithm=minimal This will correctly detect the bulk of the diff as "copy a file and delete a bunch of stuff from it." --- In preparation for implementing generic variants of Input and Output in a pulumix subpackage, move some of the requisite types to internal/ so that we don't have a cyclic dependency between pulumi and pulumix. This change is largely mechanical with adjustmenets made for compilation. For places in pulumi/ where internal state of an output was accessed, new top-level functions were added to internal/ that expose this information. Changes were noting explicitly: - `OutputState` was changed to record dependencies as `[]internal.Resource` instead of `[]Resource` because moving `Resource` into internal will end up moving most of pulumi/ into internal. We cast to/from internal.Resource as needed. - `internal.AnyOutputType` is a `reflect.Type` filled by sdk/go/pulumi at `init()` time. This is an annoying hack, but we can't move AnyOutput into internal. - `internal.FullyResolvedTypes` is similarly filled by sdk/go/pulumi at `init()` time. To add confidence to this change being safe, I used [apidiff] to compare the API of the go/pulumi package befor and after this change. The command reported no changes. [apidiff]: https://pkg.go.dev/golang.org/x/exp@v0.0.0-20230713183714-613f0c0eb8a1/cmd/apidiff On top of that, I also ran `go doc -all .` with the before/after and ran a text diff on the result. The following changes were reported: - `Input` is now an alias to `internal.Input` - `Output` is now an alias to `internal.Output` - `OutputState` is now an alias to `internal.OutputState` - `OutputState.ApplyT`, `OutputState.ApplyTWithContext` were deleted (except they're present on the alias so they're still available) - `Resource` now embeds `internal.Resource` Resolves #13585
2023-06-14 21:29:43 +00:00
func (wg *WorkGroup) Add(delta int) {
wg.mutex.Lock()
defer wg.mutex.Unlock()
if wg.cond == nil {
wg.cond = sync.NewCond(&wg.mutex)
}
c := wg.counter + delta
if c < 0 {
panic(fmt.Sprintf("Adding %d would make workGroup counter negative: %d + %d = %d",
delta, wg.counter, delta, c))
}
wg.counter = c
if c == 0 {
wg.cond.Broadcast()
}
}
sdk/go: Move Output implementation to internal **Note to reviewers**: GitHub's PR view will show a lot more for this change than the actual code added/removed. Consider opening a `git diff` of this with, git diff -C --diff-algorithm=minimal This will correctly detect the bulk of the diff as "copy a file and delete a bunch of stuff from it." --- In preparation for implementing generic variants of Input and Output in a pulumix subpackage, move some of the requisite types to internal/ so that we don't have a cyclic dependency between pulumi and pulumix. This change is largely mechanical with adjustmenets made for compilation. For places in pulumi/ where internal state of an output was accessed, new top-level functions were added to internal/ that expose this information. Changes were noting explicitly: - `OutputState` was changed to record dependencies as `[]internal.Resource` instead of `[]Resource` because moving `Resource` into internal will end up moving most of pulumi/ into internal. We cast to/from internal.Resource as needed. - `internal.AnyOutputType` is a `reflect.Type` filled by sdk/go/pulumi at `init()` time. This is an annoying hack, but we can't move AnyOutput into internal. - `internal.FullyResolvedTypes` is similarly filled by sdk/go/pulumi at `init()` time. To add confidence to this change being safe, I used [apidiff] to compare the API of the go/pulumi package befor and after this change. The command reported no changes. [apidiff]: https://pkg.go.dev/golang.org/x/exp@v0.0.0-20230713183714-613f0c0eb8a1/cmd/apidiff On top of that, I also ran `go doc -all .` with the before/after and ran a text diff on the result. The following changes were reported: - `Input` is now an alias to `internal.Input` - `Output` is now an alias to `internal.Output` - `OutputState` is now an alias to `internal.OutputState` - `OutputState.ApplyT`, `OutputState.ApplyTWithContext` were deleted (except they're present on the alias so they're still available) - `Resource` now embeds `internal.Resource` Resolves #13585
2023-06-14 21:29:43 +00:00
func (wg *WorkGroup) Done() {
wg.Add(-1)
}