Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use atomic instead of mutex for Stack ID creation #27

Merged
merged 1 commit into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/meta.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"fmt"
"strconv"
"sync"
)

type Meta struct {
sync.Mutex
ID int
ID uint64
Items []*Stack
ObjectStack *ObjectStack
SelfFlag bool
Expand All @@ -25,7 +26,7 @@ func (m Meta) Value() interface{} {
}

func (m Meta) Refl() string {
str := "$" + strconv.Itoa(m.ID) + "#" + strconv.Itoa(m.Depth()) + "< "
str := "$" + fmt.Sprint(m.ID) + "#" + strconv.Itoa(m.Depth()) + "< "

for _, i := range m.Items {
str += i.Refl() + " "
Expand Down
5 changes: 3 additions & 2 deletions src/object_stack.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package main

import (
"fmt"
"strconv"
"sync"
)

type ObjectStack struct {
sync.Mutex
Items []*Object
ID int
ID uint64
}

func NewObjectStack() *ObjectStack {
Expand All @@ -22,7 +23,7 @@ func (s *ObjectStack) Value() interface{} {
}

func (s *ObjectStack) Refl() string {
str := "O" + strconv.Itoa(s.ID) + "#" + strconv.Itoa(s.Depth()) + "< "
str := "O" + fmt.Sprint(s.ID) + "#" + strconv.Itoa(s.Depth()) + "< "

for _, i := range s.Items {
str += i.Refl() + " "
Expand Down
20 changes: 8 additions & 12 deletions src/stack.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package main

import (
//"fmt"
"fmt"
"strconv"
"sync"
"sync/atomic"
)

var Stacks int
var StacksSync sync.Mutex
var stacks uint64

func getStackID() int {
StacksSync.Lock()
defer StacksSync.Unlock()
id := Stacks
Stacks++
return id
func getStackID() uint64 {
return atomic.AddUint64(&stacks, 1)
}

type Stack struct {
sync.Mutex
Items []datatypes
Type string
ID int
ID uint64
}

func NewStack(t string) *Stack {
Expand Down Expand Up @@ -55,7 +51,7 @@ func (s Stack) Refl() string {
str += i.Refl() + " "
}
case Stack:
panic("direct Stack reference: " + strconv.Itoa(i.(Stack).ID))
panic("direct Stack reference: " + fmt.Sprint(i.(Stack).ID))
case nil:
str += "??? "
default:
Expand All @@ -82,7 +78,7 @@ func (s *Stack) ReflHeader() string {
str += s.Type
}

str += strconv.Itoa(s.ID) + "#" + strconv.Itoa(s.Depth()) + "< "
str += fmt.Sprint(s.ID) + "#" + strconv.Itoa(s.Depth()) + "< "

return str
}
Expand Down