@@ -19,6 +19,7 @@ import (
19
19
"encoding/json"
20
20
"fmt"
21
21
"os"
22
+ "sync"
22
23
23
24
"github.com/arduino/arduino-cli/internal/i18n"
24
25
"github.com/arduino/go-paths-helper"
@@ -28,8 +29,9 @@ var tr = i18n.Tr
28
29
29
30
// Database keeps track of all the compile commands run by the builder
30
31
type Database struct {
31
- Contents []Command
32
- File * paths.Path
32
+ lock sync.Mutex
33
+ contents []Command
34
+ file * paths.Path
33
35
}
34
36
35
37
// Command keeps track of a single run of a compile command
@@ -43,8 +45,8 @@ type Command struct {
43
45
// NewDatabase creates an empty CompilationDatabase
44
46
func NewDatabase (filename * paths.Path ) * Database {
45
47
return & Database {
46
- File : filename ,
47
- Contents : []Command {},
48
+ file : filename ,
49
+ contents : []Command {},
48
50
}
49
51
}
50
52
@@ -55,16 +57,18 @@ func LoadDatabase(file *paths.Path) (*Database, error) {
55
57
return nil , err
56
58
}
57
59
res := NewDatabase (file )
58
- return res , json .Unmarshal (f , & res .Contents )
60
+ return res , json .Unmarshal (f , & res .contents )
59
61
}
60
62
61
63
// SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json,
62
64
// see https://clang.llvm.org/docs/JSONCompilationDatabase.html
63
65
func (db * Database ) SaveToFile () {
64
- if jsonContents , err := json .MarshalIndent (db .Contents , "" , " " ); err != nil {
66
+ db .lock .Lock ()
67
+ defer db .lock .Unlock ()
68
+ if jsonContents , err := json .MarshalIndent (db .contents , "" , " " ); err != nil {
65
69
fmt .Println (tr ("Error serializing compilation database: %s" , err ))
66
70
return
67
- } else if err := db .File .WriteFile (jsonContents ); err != nil {
71
+ } else if err := db .file .WriteFile (jsonContents ); err != nil {
68
72
fmt .Println (tr ("Error writing compilation database: %s" , err ))
69
73
}
70
74
}
@@ -88,5 +92,7 @@ func (db *Database) Add(target *paths.Path, command *paths.Process) {
88
92
File : target .String (),
89
93
}
90
94
91
- db .Contents = append (db .Contents , entry )
95
+ db .lock .Lock ()
96
+ db .contents = append (db .contents , entry )
97
+ db .lock .Unlock ()
92
98
}
0 commit comments