Add locks to InstanceState

Rename receivers for consistency
This commit is contained in:
James Bardin 2016-08-31 16:34:40 -04:00
parent 8d5080fe72
commit 90aab0105d
1 changed files with 37 additions and 24 deletions

View File

@ -1470,36 +1470,36 @@ type InstanceState struct {
func (s *InstanceState) Lock() { s.mu.Lock() } func (s *InstanceState) Lock() { s.mu.Lock() }
func (s *InstanceState) Unlock() { s.mu.Unlock() } func (s *InstanceState) Unlock() { s.mu.Unlock() }
func (i *InstanceState) init() { func (s *InstanceState) init() {
i.Lock() s.Lock()
defer i.Unlock() defer s.Unlock()
if i.Attributes == nil { if s.Attributes == nil {
i.Attributes = make(map[string]string) s.Attributes = make(map[string]string)
} }
if i.Meta == nil { if s.Meta == nil {
i.Meta = make(map[string]string) s.Meta = make(map[string]string)
} }
i.Ephemeral.init() s.Ephemeral.init()
} }
// Copy all the Fields from another InstanceState // Copy all the Fields from another InstanceState
func (i *InstanceState) Set(from *InstanceState) { func (s *InstanceState) Set(from *InstanceState) {
i.Lock() s.Lock()
defer i.Unlock() defer s.Unlock()
from.Lock() from.Lock()
defer from.Unlock() defer from.Unlock()
i.ID = from.ID s.ID = from.ID
i.Attributes = from.Attributes s.Attributes = from.Attributes
i.Ephemeral = from.Ephemeral s.Ephemeral = from.Ephemeral
i.Meta = from.Meta s.Meta = from.Meta
i.Tainted = from.Tainted s.Tainted = from.Tainted
} }
func (i *InstanceState) DeepCopy() *InstanceState { func (s *InstanceState) DeepCopy() *InstanceState {
copy, err := copystructure.Config{Lock: true}.Copy(i) copy, err := copystructure.Config{Lock: true}.Copy(s)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -1508,7 +1508,13 @@ func (i *InstanceState) DeepCopy() *InstanceState {
} }
func (s *InstanceState) Empty() bool { func (s *InstanceState) Empty() bool {
return s == nil || s.ID == "" if s == nil {
return true
}
s.Lock()
defer s.Unlock()
return s.ID == ""
} }
func (s *InstanceState) Equal(other *InstanceState) bool { func (s *InstanceState) Equal(other *InstanceState) bool {
@ -1516,6 +1522,8 @@ func (s *InstanceState) Equal(other *InstanceState) bool {
if s == nil || other == nil { if s == nil || other == nil {
return s == other return s == other
} }
s.Lock()
defer s.Unlock()
// IDs must be equal // IDs must be equal
if s.ID != other.ID { if s.ID != other.ID {
@ -1575,6 +1583,8 @@ func (s *InstanceState) MergeDiff(d *InstanceDiff) *InstanceState {
result.init() result.init()
if s != nil { if s != nil {
s.Lock()
defer s.Unlock()
for k, v := range s.Attributes { for k, v := range s.Attributes {
result.Attributes[k] = v result.Attributes[k] = v
} }
@ -1597,16 +1607,19 @@ func (s *InstanceState) MergeDiff(d *InstanceDiff) *InstanceState {
return result return result
} }
func (i *InstanceState) String() string { func (s *InstanceState) String() string {
s.Lock()
defer s.Unlock()
var buf bytes.Buffer var buf bytes.Buffer
if i == nil || i.ID == "" { if s == nil || s.ID == "" {
return "<not created>" return "<not created>"
} }
buf.WriteString(fmt.Sprintf("ID = %s\n", i.ID)) buf.WriteString(fmt.Sprintf("ID = %s\n", s.ID))
attributes := i.Attributes attributes := s.Attributes
attrKeys := make([]string, 0, len(attributes)) attrKeys := make([]string, 0, len(attributes))
for ak, _ := range attributes { for ak, _ := range attributes {
if ak == "id" { if ak == "id" {
@ -1622,7 +1635,7 @@ func (i *InstanceState) String() string {
buf.WriteString(fmt.Sprintf("%s = %s\n", ak, av)) buf.WriteString(fmt.Sprintf("%s = %s\n", ak, av))
} }
buf.WriteString(fmt.Sprintf("Tainted = %t\n", i.Tainted)) buf.WriteString(fmt.Sprintf("Tainted = %t\n", s.Tainted))
return buf.String() return buf.String()
} }