trying to get Mutex back to a working state

This commit is contained in:
Jeff Carr 2025-01-11 01:30:08 -06:00
parent 51a326c5d7
commit 3f2909aa0d
5 changed files with 39 additions and 21 deletions

View File

@ -1,8 +1,6 @@
VERSION = $(shell git describe --tags) VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d_%H%M) BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
hmm: test
simple: build simple: build
# make -C example clean simpleMutexGlobal goimports vet # make -C example clean simpleMutexGlobal goimports vet
make -C example clean simpleMutexProtoc goimports vet make -C example clean simpleMutexProtoc goimports vet
@ -42,8 +40,10 @@ auto:
# rm -f auto.sort.pb.go auto.newsort.pb.go # auto.marshal.pb.go # rm -f auto.sort.pb.go auto.newsort.pb.go # auto.marshal.pb.go
test: test:
make -C example modproto
make -C example rawproto make -C example rawproto
# The Go Protocol Buffers library embeds a sync.Mutex within the MessageState struct to prevent unintended shallow copies of message structs
# It only fails in Marshal() functions though. That is dumb.
make -C example modproto # THIS DOES NOT WORK. It could work however. This autogenerated code could be used to debug it.
junk: junk:
cd example; rm -f go.* *.pb.go cd example; rm -f go.* *.pb.go

View File

@ -6,12 +6,29 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
"go.wit.com/log" "go.wit.com/log"
) )
func (pf *File) syncLock(w io.Writer) {
var LOCK string = pf.Bases.Lockname
fmt.Fprintln(w, "// a simple global lock")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// this is needed because it seems Marshal() fails if locks are in the structs (?)")
fmt.Fprintln(w, "// this might just be a syntactical runtime error. notsure.")
fmt.Fprintln(w, "// maybe this autogen tool will help someone that actually knows what is going on inside")
fmt.Fprintln(w, "// go/src/google.golang.org/protobuf/proto/proto_methods.go")
fmt.Fprintln(w, "// go/src/google.golang.org/protobuf/proto/encode.go")
fmt.Fprintln(w, "// my guess is that Marshal() needs to be told to ignore sync.RWMutex as it ephemeral and can't be stored")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex")
fmt.Fprintln(w, "")
}
func (pb *Files) addMutex(f *File) error { func (pb *Files) addMutex(f *File) error {
fullname := f.Pbfilename fullname := f.Pbfilename
log.Info("pb filename:", fullname) log.Info("pb filename:", fullname)
@ -47,7 +64,9 @@ func (pb *Files) addMutex(f *File) error {
if argv.Mutex { if argv.Mutex {
log.Info("Adding Mutex to:", line) log.Info("Adding Mutex to:", line)
fmt.Fprintln(w, line) fmt.Fprintln(w, line)
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") // this must be 'Lock' or Marshal() panics?
// fmt.Fprintln(w, "\t// auto-added by go.wit.com/apps/autogenpb")
// fmt.Fprintln(w, "\tsync.RWMutex")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} else { } else {
log.Info("Skipping. Mutex = false for:", line) log.Info("Skipping. Mutex = false for:", line)
@ -79,6 +98,12 @@ func (pf *File) structMatch(line string) bool {
return true return true
} }
// ONLY PASS THIS IF YOU DO NOT WANT TO USE MARSHAL()
if argv.Marshal {
return false
}
msg = pf.Base msg = pf.Base
start = "type " + msg.Name + " struct {" start = "type " + msg.Name + " struct {"
if strings.HasPrefix(line, start) { if strings.HasPrefix(line, start) {

View File

@ -12,6 +12,7 @@ type args struct {
Package string `arg:"--package" help:"the package name"` Package string `arg:"--package" help:"the package name"`
Proto string `arg:"--proto" help:"the .proto filename"` Proto string `arg:"--proto" help:"the .proto filename"`
Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"` Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"`
Marshal bool `arg:"--marshal" default:"true" help:"if you need Marshal(), per-message Mutex's fail for some reason"`
Delete bool `arg:"--delete" help:"use delete with copy experiment"` Delete bool `arg:"--delete" help:"use delete with copy experiment"`
DryRun bool `arg:"--dry-run" help:"show what would be run"` DryRun bool `arg:"--dry-run" help:"show what would be run"`
GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"`

View File

@ -5,15 +5,6 @@ import (
"io" "io"
) )
func (pf *File) syncLock(w io.Writer) {
var LOCK string = pf.Bases.Lockname
fmt.Fprintln(w, "// bad global lock until modifying the .pb.go file is tested")
fmt.Fprintln(w, "// sync.RWMutex or sync.Mutex?")
fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex")
fmt.Fprintln(w, "")
}
func (msg *MsgName) iterTop(w io.Writer) { func (msg *MsgName) iterTop(w io.Writer) {
var BASE string = msg.Name var BASE string = msg.Name

View File

@ -11,6 +11,7 @@ func (msg *MsgName) getLockname(s string) string {
if argv.Mutex { if argv.Mutex {
// use the mutex lock from the modified protoc.pb.go file // use the mutex lock from the modified protoc.pb.go file
return s + ".Lock" return s + ".Lock"
// return s // causes Marshal() to panic? always use the variable name 'Lock'?
} }
// a single global lock by struct name // a single global lock by struct name
return msg.Lockname return msg.Lockname