Skip to content

Commit

Permalink
Updating id gen to directly embed mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Ray committed Jul 30, 2021
1 parent 2a3ce18 commit cb91db0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
12 changes: 3 additions & 9 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@ import "sync"

// IDGen is a simple, thread safe, integer id generator
type IDGen struct {
sync.Mutex
nextID int
idLock sync.Mutex
}

func NewIDGen() *IDGen {
return &IDGen{
nextID: 0,
}
}

// Gen generates the next id and returns it
func (gen *IDGen) Gen() (id int) {
gen.idLock.Lock()
defer gen.idLock.Unlock()
gen.Lock()
defer gen.Unlock()

id = gen.nextID
gen.nextID++
Expand Down
2 changes: 1 addition & 1 deletion packer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewPacker(width, height int, flags uint8) *Packer {
packer := &Packer{
bounds: bounds,
flags: flags,
id: pixelutils.NewIDGen(),
id: &pixelutils.IDGen{},
pic: pixel.MakePictureData(bounds),
emptySpaces: make(spaceList, 1),
images: make(map[int]pixel.Rect),
Expand Down

0 comments on commit cb91db0

Please sign in to comment.