Skip to content

Commit 9045267

Browse files
authored
ENHANCEMENT: remove global mutex (#43) (#45)
1 parent b58fd82 commit 9045267

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ func GetStatistics(path string) (statistics Statistics, err error) {
4848
}
4949

5050
var waitGroup sync.WaitGroup
51-
var mutex sync.Mutex
5251

5352

5453
waitGroup.Add(1)
5554
tree.Chdir(path) //nolint:errcheck
56-
goAroundCalculating(&waitGroup, &mutex, tree, list, &statistics, componentsSet)
55+
goAroundCalculating(&waitGroup, tree, list, &statistics, componentsSet)
5756
tree.Chdir("..") //nolint:errcheck
5857
waitGroup.Wait()
5958

@@ -65,7 +64,6 @@ func GetStatistics(path string) (statistics Statistics, err error) {
6564

6665
func goAroundCalculating(
6766
waitGroup *sync.WaitGroup,
68-
mutex *sync.Mutex,
6967
tree t.Tree,
7068
list t.Nodes,
7169
statistics *Statistics,
@@ -85,7 +83,7 @@ func goAroundCalculating(
8583

8684
waitGroup.Add(1)
8785
tree.Chdir(node.Name) //nolint:errcheck
88-
go goAroundCalculating(waitGroup, mutex, tree.Copy(), newList, statistics, componentsSet.Copy())
86+
go goAroundCalculating(waitGroup, tree.Copy(), newList, statistics, componentsSet.Copy())
8987
tree.Chdir("..") //nolint:errcheck
9088

9189
if exists {
@@ -98,7 +96,6 @@ func goAroundCalculating(
9896
LOC := uint64(1)
9997
tree.ReadNodeLineByLine(node.Name, proceedLine, &LOC)
10098

101-
mutex.Lock()
10299
statistics.Total.Append(LOC, 1)
103100
statistics.Languages[language].Append(LOC, 1)
104101

@@ -111,7 +108,6 @@ func goAroundCalculating(
111108
for componentTitle := range componentsSet.Elements {
112109
statistics.Components[componentTitle].Append(LOC, 1)
113110
}
114-
mutex.Unlock()
115111
}
116112
}
117113
}
@@ -120,7 +116,7 @@ func goAroundCalculating(
120116
func initItems(mapping map[string]string) (items Items) {
121117
items = make(Items)
122118
for _, value := range mapping {
123-
items[value] = &TableItem{Files: 0, LOC: 0}
119+
items[value] = &TableItem{Files: 0, LOC: 0, mutex: sync.Mutex{}}
124120
}
125121
return
126122
}

types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package statloc
22

3+
import "sync"
4+
35
type (
46
component struct {
57
Title string
@@ -14,6 +16,7 @@ type (
1416
TableItem struct {
1517
LOC uint64
1618
Files uint64
19+
mutex sync.Mutex
1720
}
1821

1922
Items map[string]*TableItem
@@ -62,6 +65,8 @@ func (c *component) Copy(elements map[string]struct{}) *component {
6265
}
6366

6467
func (t *TableItem) Append(LOC uint64, files uint64) {
68+
t.mutex.Lock()
6569
t.LOC += LOC
6670
t.Files += files
71+
t.mutex.Unlock()
6772
}

0 commit comments

Comments
 (0)