@@ -21,6 +21,7 @@ import (
21
21
"net/url"
22
22
"os"
23
23
"strings"
24
+ "sync"
24
25
25
26
"github.com/arduino/arduino-cli/arduino"
26
27
"github.com/arduino/arduino-cli/arduino/cores"
@@ -49,6 +50,7 @@ var tr = i18n.Tr
49
50
// referenced by an int32 handle
50
51
var instances = map [int32 ]* CoreInstance {}
51
52
var instancesCount int32 = 1
53
+ var instancesMux sync.Mutex
52
54
53
55
// CoreInstance is an instance of the Arduino Core Services. The user can
54
56
// instantiate as many as needed by providing a different configuration
@@ -66,23 +68,25 @@ type InstanceContainer interface {
66
68
// GetInstance returns a CoreInstance for the given ID, or nil if ID
67
69
// doesn't exist
68
70
func GetInstance (id int32 ) * CoreInstance {
71
+ instancesMux .Lock ()
72
+ defer instancesMux .Unlock ()
69
73
return instances [id ]
70
74
}
71
75
72
76
// GetPackageManager returns a PackageManager for the given ID, or nil if
73
77
// ID doesn't exist
74
78
func GetPackageManager (id int32 ) * packagemanager.PackageManager {
75
- i , ok := instances [ id ]
76
- if ! ok {
79
+ i := GetInstance ( id )
80
+ if i == nil {
77
81
return nil
78
82
}
79
83
return i .PackageManager
80
84
}
81
85
82
86
// GetLibraryManager returns the library manager for the given instance ID
83
- func GetLibraryManager (instanceID int32 ) * librariesmanager.LibrariesManager {
84
- i , ok := instances [ instanceID ]
85
- if ! ok {
87
+ func GetLibraryManager (id int32 ) * librariesmanager.LibrariesManager {
88
+ i := GetInstance ( id )
89
+ if i == nil {
86
90
return nil
87
91
}
88
92
return i .lm
@@ -144,9 +148,11 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
144
148
)
145
149
146
150
// Save instance
151
+ instancesMux .Lock ()
147
152
instanceID := instancesCount
148
153
instances [instanceID ] = instance
149
154
instancesCount ++
155
+ instancesMux .Unlock ()
150
156
151
157
return & rpc.CreateResponse {
152
158
Instance : & rpc.Instance {Id : instanceID },
@@ -166,7 +172,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
166
172
if reqInst == nil {
167
173
return & arduino.InvalidInstanceError {}
168
174
}
169
- instance := instances [ reqInst .GetId ()]
175
+ instance := GetInstance ( reqInst .GetId ())
170
176
if instance == nil {
171
177
return & arduino.InvalidInstanceError {}
172
178
}
@@ -413,10 +419,12 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
413
419
// Destroy FIXMEDOC
414
420
func Destroy (ctx context.Context , req * rpc.DestroyRequest ) (* rpc.DestroyResponse , error ) {
415
421
id := req .GetInstance ().GetId ()
422
+
423
+ instancesMux .Lock ()
424
+ defer instancesMux .Unlock ()
416
425
if _ , ok := instances [id ]; ! ok {
417
426
return nil , & arduino.InvalidInstanceError {}
418
427
}
419
-
420
428
delete (instances , id )
421
429
return & rpc.DestroyResponse {}, nil
422
430
}
@@ -453,9 +461,7 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
453
461
454
462
// UpdateIndex FIXMEDOC
455
463
func UpdateIndex (ctx context.Context , req * rpc.UpdateIndexRequest , downloadCB rpc.DownloadProgressCB ) (* rpc.UpdateIndexResponse , error ) {
456
- id := req .GetInstance ().GetId ()
457
- _ , ok := instances [id ]
458
- if ! ok {
464
+ if GetInstance (req .GetInstance ().GetId ()) == nil {
459
465
return nil , & arduino.InvalidInstanceError {}
460
466
}
461
467
0 commit comments