diff --git a/addMutex.go b/addMutex.go index efa8501..8a4b0f3 100644 --- a/addMutex.go +++ b/addMutex.go @@ -34,7 +34,7 @@ func addMutex(names map[string]string) error { found = true log.Info("FOUND line:", line) fmt.Fprintln(w, line) - fmt.Fprintln(w, "\tsync.RWMutex // auto-added by go.wit.com/apps/autogenpb") + fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") fmt.Fprintln(w, "") } else { fmt.Fprintln(w, line) diff --git a/argv.go b/argv.go index 472425d..94d047c 100644 --- a/argv.go +++ b/argv.go @@ -17,6 +17,7 @@ type args struct { Marshal []string `arg:"--marshal" help:"what to marshal on"` NoMarshal bool `arg:"--no-marshal" help:"do not make a marshal.pb.go file"` NoSort bool `arg:"--no-sort" help:"do not make a sort.pb.go file"` + Mutex bool `arg:"--mutex" help:"try mutex hack (breaks proto.Marshal()"` DryRun bool `arg:"--dry-run" help:"show what would be run"` } diff --git a/main.go b/main.go index d9c32e2..00dd720 100644 --- a/main.go +++ b/main.go @@ -109,15 +109,16 @@ func main() { // experiment to add a mutex to the structs. // this might fix my other not so great lock implementation on sort (?) - // not sure though because I haven't tried it. leave it here until - // I can test it - if err := addMutex(sortmap); err == nil { - log.Info("adding mutex to existing protoc-gen-go file worked") - sortmap["mutex"] = "true" - sortmap["lock"] = "all" - } else { - log.Info("adding mutex to existing protoc-gen-go file did not work") - sortmap["mutex"] = "false" + // seems to work, but proto.Marshal() breaks with nil reference + if argv.Mutex { + if err := addMutex(sortmap); err == nil { + log.Info("adding mutex to existing protoc-gen-go file worked") + sortmap["mutex"] = "true" + sortmap["lock"] = "all" + } else { + log.Info("adding mutex to existing protoc-gen-go file did not work") + sortmap["mutex"] = "false" + } } } diff --git a/sort.go b/sort.go index 0b884d9..f7f9ff9 100644 --- a/sort.go +++ b/sort.go @@ -153,8 +153,13 @@ func iterSortAll(w io.Writer, names map[string]string) { fmt.Fprintln(w, "}") fmt.Fprintln(w, "") fmt.Fprintln(w, "func (all *"+names["Bases"]+") Len() int {") - fmt.Fprintln(w, " "+names["lock"]+".RLock()") - fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + if sortmap["lock"] == "all" { + fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") + } else { + fmt.Fprintln(w, " "+names["lock"]+".RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + } fmt.Fprintln(w, "") fmt.Fprintln(w, " return len(all."+names["Bases"]+")") fmt.Fprintln(w, "}") @@ -183,8 +188,13 @@ func iterSortBy(w io.Writer, names map[string]string) { func iterEnd(w io.Writer, names map[string]string) { fmt.Fprintln(w, "// safely returns a slice of pointers to the "+names["Base"]+" protobufs") fmt.Fprintln(w, "func (all *"+names["Bases"]+") selectAll"+names["Base"]+"() []*"+names["Base"]+" {") - fmt.Fprintln(w, " "+names["lock"]+".RLock()") - fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + if sortmap["lock"] == "all" { + fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") + } else { + fmt.Fprintln(w, " "+names["lock"]+".RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + } fmt.Fprintln(w, "") fmt.Fprintln(w, " // Create a new slice to hold pointers to each "+names["Base"]+"") fmt.Fprintln(w, " var aStuff []*"+names["Base"]+"") @@ -209,8 +219,13 @@ func iterAppend(w io.Writer, names map[string]string) { // fmt.Fprintln(w, "func (all *"+names["Bases"]+") Append(newP *"+names["Base"]+") bool { // todo: make unique name here") fmt.Fprintln(w, "func (all *"+names["Bases"]+") AppendUnique"+names["append"]+"(newP *"+names["Base"]+") bool {") } - fmt.Fprintln(w, " "+names["lock"]+".Lock()") - fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()") + if sortmap["lock"] == "all" { + fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") + } else { + fmt.Fprintln(w, " "+names["lock"]+".RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + } fmt.Fprintln(w, "") if names["append"] != "" { fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {") @@ -233,8 +248,13 @@ func iterReplace(w io.Writer, names map[string]string) { fmt.Fprintln(w, "// enforces "+names["append"]+" is unique") fmt.Fprintln(w, "func (all *"+names["Bases"]+") Replace"+names["append"]+"(newP *"+names["Base"]+") bool { // todo: make unique name here") - fmt.Fprintln(w, " "+names["lock"]+".Lock()") - fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()") + if sortmap["lock"] == "all" { + fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") + } else { + fmt.Fprintln(w, " "+names["lock"]+".RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + } fmt.Fprintln(w, "") fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {") fmt.Fprintln(w, " if p."+names["append"]+" == newP."+names["append"]+" {") @@ -250,8 +270,13 @@ func iterReplace(w io.Writer, names map[string]string) { func iterDelete(w io.Writer, names map[string]string) { fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) bool {") - fmt.Fprintln(w, " "+names["lock"]+".Lock()") - fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()") + if sortmap["lock"] == "all" { + fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") + } else { + fmt.Fprintln(w, " "+names["lock"]+".RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + } fmt.Fprintln(w, "") fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {") fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {") @@ -268,8 +293,13 @@ func iterDelete(w io.Writer, names map[string]string) { // this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct func iterDeleteWithCopy(w io.Writer, names map[string]string) { fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) *"+names["Base"]+" {") - fmt.Fprintln(w, " "+names["lock"]+".Lock()") - fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()") + if sortmap["lock"] == "all" { + fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") + } else { + fmt.Fprintln(w, " "+names["lock"]+".RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + } fmt.Fprintln(w, "") fmt.Fprintln(w, " var newr "+names["Base"]) fmt.Fprintln(w, "") @@ -289,8 +319,13 @@ func iterDeleteWithCopy(w io.Writer, names map[string]string) { func iterFind(w io.Writer, names map[string]string) { fmt.Fprintln(w, "// find a dependancy by the go path") fmt.Fprintln(w, "func (all *"+names["Bases"]+") FindBy"+names["append"]+"(s string) *"+names["Base"]+" {") - fmt.Fprintln(w, " "+names["lock"]+".Lock()") - fmt.Fprintln(w, " defer "+names["lock"]+".Unlock()") + if sortmap["lock"] == "all" { + fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()") + } else { + fmt.Fprintln(w, " "+names["lock"]+".RLock()") + fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()") + } fmt.Fprintln(w, "") fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {") fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")