Skip to content

Commit

Permalink
Correct race condition on updating a non-existant space
Browse files Browse the repository at this point in the history
  • Loading branch information
200sc committed Mar 19, 2019
1 parent b98baa7 commit 8556c52
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions collision/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ func (t *Tree) Remove(sps ...*Space) int {
return removed
}

// UpdateLabel will set the input space's label in this tree. Modifying a
// space's label without going through a tree's method such as this will
// have no effect.
// UpdateLabel will set the input space's label. DEPRECATED. Just set
// the Label field on the Space pointer.
func (t *Tree) UpdateLabel(classtype Label, s *Space) {
t.Remove(s)
s.Label = classtype
t.Add(s)
}

// ErrNotExist is returned by methods on spaces
// when the space to update or act on did not exist
var ErrNotExist = errors.New("Space did not exist to update")

// UpdateSpace resets a space's location to a given
// rtreego.Rect.
// This is not an operation on a space because
Expand All @@ -102,7 +103,11 @@ func (t *Tree) UpdateSpace(x, y, w, h float64, s *Space) error {
}
loc := NewRect(x, y, w, h)
t.Lock()
t.Delete(s)
deleted := t.Delete(s)
if !deleted {
t.Unlock()
return ErrNotExist
}
s.Location = loc
t.Insert(s)
t.Unlock()
Expand All @@ -116,7 +121,11 @@ func (t *Tree) UpdateSpaceRect(rect floatgeom.Rect3, s *Space) error {
return oakerr.NilInput{InputName: "s"}
}
t.Lock()
t.Delete(s)
deleted := t.Delete(s)
if !deleted {
t.Unlock()
return ErrNotExist
}
s.Location = rect
t.Insert(s)
t.Unlock()
Expand All @@ -138,8 +147,6 @@ func (t *Tree) ShiftSpace(x, y float64, s *Space) error {
// themselves, if they exist in the tree, but self-collision
// will not be reported by Hits.
func (t *Tree) Hits(sp *Space) []*Space {
// Eventually we'll expose SearchIntersect for use cases where you
// want to see if you intersect yourself
results := t.SearchIntersect(sp.Bounds())
hitSelf := -1
i := 0
Expand Down

0 comments on commit 8556c52

Please sign in to comment.