2017-12-15 08:06:54 +00:00
|
|
|
package solver
|
2017-08-10 01:20:33 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-08-10 01:20:33 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
)
|
|
|
|
|
2017-12-13 07:39:37 +00:00
|
|
|
// SharedRef is a wrapper around releasable that allows you to make new
|
2017-08-10 01:20:33 +00:00
|
|
|
// releasable child objects
|
2017-12-13 07:39:37 +00:00
|
|
|
type SharedRef struct {
|
2017-08-10 01:20:33 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
refs map[*sharedRefInstance]struct{}
|
2017-12-15 08:06:54 +00:00
|
|
|
main Ref
|
|
|
|
Ref
|
2017-08-10 01:20:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
func NewSharedRef(main Ref) *SharedRef {
|
2017-12-13 07:39:37 +00:00
|
|
|
mr := &SharedRef{
|
|
|
|
refs: make(map[*sharedRefInstance]struct{}),
|
|
|
|
Ref: main,
|
2017-08-10 01:20:33 +00:00
|
|
|
}
|
|
|
|
mr.main = mr.Clone()
|
|
|
|
return mr
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
func (mr *SharedRef) Clone() Ref {
|
2017-08-10 01:20:33 +00:00
|
|
|
mr.mu.Lock()
|
2017-12-13 07:39:37 +00:00
|
|
|
r := &sharedRefInstance{SharedRef: mr}
|
2017-08-10 01:20:33 +00:00
|
|
|
mr.refs[r] = struct{}{}
|
|
|
|
mr.mu.Unlock()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:39:37 +00:00
|
|
|
func (mr *SharedRef) Release(ctx context.Context) error {
|
2017-08-10 01:20:33 +00:00
|
|
|
return mr.main.Release(ctx)
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
func (mr *SharedRef) Sys() Ref {
|
2017-12-13 07:39:37 +00:00
|
|
|
sys := mr.Ref
|
2017-08-10 01:20:33 +00:00
|
|
|
if s, ok := sys.(interface {
|
2017-12-15 08:06:54 +00:00
|
|
|
Sys() Ref
|
2017-08-10 01:20:33 +00:00
|
|
|
}); ok {
|
|
|
|
return s.Sys()
|
|
|
|
}
|
|
|
|
return sys
|
|
|
|
}
|
|
|
|
|
|
|
|
type sharedRefInstance struct {
|
2017-12-13 07:39:37 +00:00
|
|
|
*SharedRef
|
2017-08-10 01:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *sharedRefInstance) Release(ctx context.Context) error {
|
2017-12-13 07:39:37 +00:00
|
|
|
r.SharedRef.mu.Lock()
|
|
|
|
defer r.SharedRef.mu.Unlock()
|
|
|
|
delete(r.SharedRef.refs, r)
|
|
|
|
if len(r.SharedRef.refs) == 0 {
|
|
|
|
return r.SharedRef.Ref.Release(ctx)
|
2017-08-10 01:20:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
func OriginRef(ref Ref) Ref {
|
2017-08-10 01:20:33 +00:00
|
|
|
sysRef := ref
|
|
|
|
if sys, ok := ref.(interface {
|
2017-12-15 08:06:54 +00:00
|
|
|
Sys() Ref
|
2017-08-10 01:20:33 +00:00
|
|
|
}); ok {
|
|
|
|
sysRef = sys.Sys()
|
|
|
|
}
|
|
|
|
return sysRef
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:06:54 +00:00
|
|
|
func ToImmutableRef(ref Ref) (cache.ImmutableRef, bool) {
|
2017-12-13 07:39:37 +00:00
|
|
|
immutable, ok := OriginRef(ref).(cache.ImmutableRef)
|
2017-08-10 01:20:33 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return &immutableRef{immutable, ref.Release}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
type immutableRef struct {
|
|
|
|
cache.ImmutableRef
|
|
|
|
release func(context.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ir *immutableRef) Release(ctx context.Context) error {
|
|
|
|
return ir.release(ctx)
|
|
|
|
}
|