From 26a4e01e42271d0edb4e48d85d8a0baff2f528ee Mon Sep 17 00:00:00 2001 From: dmkjfs Date: Fri, 31 Oct 2025 04:26:54 +0300 Subject: [PATCH] remove global mutex --- main.go | 10 +++------- types.go | 5 +++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index da33223..8919b89 100644 --- a/main.go +++ b/main.go @@ -48,12 +48,11 @@ func GetStatistics(path string) (statistics Statistics, err error) { } var waitGroup sync.WaitGroup - var mutex sync.Mutex waitGroup.Add(1) tree.Chdir(path) //nolint:errcheck - goAroundCalculating(&waitGroup, &mutex, tree, list, &statistics, componentsSet) + goAroundCalculating(&waitGroup, tree, list, &statistics, componentsSet) tree.Chdir("..") //nolint:errcheck waitGroup.Wait() @@ -65,7 +64,6 @@ func GetStatistics(path string) (statistics Statistics, err error) { func goAroundCalculating( waitGroup *sync.WaitGroup, - mutex *sync.Mutex, tree t.Tree, list t.Nodes, statistics *Statistics, @@ -85,7 +83,7 @@ func goAroundCalculating( waitGroup.Add(1) tree.Chdir(node.Name) //nolint:errcheck - go goAroundCalculating(waitGroup, mutex, tree.Copy(), newList, statistics, componentsSet.Copy()) + go goAroundCalculating(waitGroup, tree.Copy(), newList, statistics, componentsSet.Copy()) tree.Chdir("..") //nolint:errcheck if exists { @@ -98,7 +96,6 @@ func goAroundCalculating( LOC := uint64(1) tree.ReadNodeLineByLine(node.Name, proceedLine, &LOC) - mutex.Lock() statistics.Total.Append(LOC, 1) statistics.Languages[language].Append(LOC, 1) @@ -111,7 +108,6 @@ func goAroundCalculating( for componentTitle := range componentsSet.Elements { statistics.Components[componentTitle].Append(LOC, 1) } - mutex.Unlock() } } } @@ -120,7 +116,7 @@ func goAroundCalculating( func initItems(mapping map[string]string) (items Items) { items = make(Items) for _, value := range mapping { - items[value] = &TableItem{Files: 0, LOC: 0} + items[value] = &TableItem{Files: 0, LOC: 0, mutex: sync.Mutex{}} } return } diff --git a/types.go b/types.go index be24324..213b172 100644 --- a/types.go +++ b/types.go @@ -1,5 +1,7 @@ package statloc +import "sync" + type ( component struct { Title string @@ -14,6 +16,7 @@ type ( TableItem struct { LOC uint64 Files uint64 + mutex sync.Mutex } Items map[string]*TableItem @@ -62,6 +65,8 @@ func (c *component) Copy(elements map[string]struct{}) *component { } func (t *TableItem) Append(LOC uint64, files uint64) { + t.mutex.Lock() t.LOC += LOC t.Files += files + t.mutex.Unlock() }