@@ -46,12 +46,6 @@ import (
46
46
47
47
var tr = i18n .Tr
48
48
49
- // this map contains all the running Arduino Core Services instances
50
- // referenced by an int32 handle
51
- var instances = map [int32 ]* CoreInstance {}
52
- var instancesCount int32 = 1
53
- var instancesMux sync.Mutex
54
-
55
49
// CoreInstance is an instance of the Arduino Core Services. The user can
56
50
// instantiate as many as needed by providing a different configuration
57
51
// for each one.
@@ -60,18 +54,55 @@ type CoreInstance struct {
60
54
lm * librariesmanager.LibrariesManager
61
55
}
62
56
57
+ // coreInstancesContainer has methods to add an remove instances atomically.
58
+ type coreInstancesContainer struct {
59
+ instances map [int32 ]* CoreInstance
60
+ instancesCount int32
61
+ instancesMux sync.Mutex
62
+ }
63
+
64
+ // instances contains all the running Arduino Core Services instances
65
+ var instances = & coreInstancesContainer {
66
+ instances : map [int32 ]* CoreInstance {},
67
+ instancesCount : 1 ,
68
+ }
69
+
63
70
// GetInstance returns a CoreInstance for the given ID, or nil if ID
64
71
// doesn't exist
65
- func GetInstance (id int32 ) * CoreInstance {
66
- instancesMux .Lock ()
67
- defer instancesMux .Unlock ()
68
- return instances [id ]
72
+ func (c * coreInstancesContainer ) GetInstance (id int32 ) * CoreInstance {
73
+ c .instancesMux .Lock ()
74
+ defer c .instancesMux .Unlock ()
75
+ return c .instances [id ]
76
+ }
77
+
78
+ // AddAndAssignID saves the CoreInstance and assign an unique ID to
79
+ // retrieve it later
80
+ func (c * coreInstancesContainer ) AddAndAssignID (i * CoreInstance ) int32 {
81
+ c .instancesMux .Lock ()
82
+ defer c .instancesMux .Unlock ()
83
+ id := c .instancesCount
84
+ c .instances [id ] = i
85
+ c .instancesCount ++
86
+ return id
87
+ }
88
+
89
+ // RemoveID removed the CoreInstance referenced by id. Returns true
90
+ // if the operation is successful, or false if the CoreInstance does
91
+ // not exists
92
+ func (c * coreInstancesContainer ) RemoveID (id int32 ) bool {
93
+ c .instancesMux .Lock ()
94
+ defer c .instancesMux .Unlock ()
95
+ if _ , ok := c .instances [id ]; ! ok {
96
+ return false
97
+ }
98
+ delete (c .instances , id )
99
+ return true
69
100
}
70
101
71
102
// GetPackageManager returns a PackageManager for the given ID, or nil if
72
103
// ID doesn't exist
73
104
func GetPackageManager (id int32 ) * packagemanager.PackageManager {
74
- i := GetInstance (id )
105
+ i := instances . GetInstance (id )
75
106
if i == nil {
76
107
return nil
77
108
}
@@ -82,7 +113,7 @@ func GetPackageManager(id int32) *packagemanager.PackageManager {
82
113
// explorer holds a read lock on the underlying PackageManager and it should
83
114
// be released by calling the returned "release" function.
84
115
func GetPackageManagerExplorer (instance rpc.InstanceCommand ) (explorer * packagemanager.Explorer , release func ()) {
85
- i := GetInstance (instance .GetInstance ().GetId ())
116
+ i := instances . GetInstance (instance .GetInstance ().GetId ())
86
117
if i == nil {
87
118
return nil , nil
88
119
}
@@ -91,7 +122,7 @@ func GetPackageManagerExplorer(instance rpc.InstanceCommand) (explorer *packagem
91
122
92
123
// GetLibraryManager returns the library manager for the given instance ID
93
124
func GetLibraryManager (id int32 ) * librariesmanager.LibrariesManager {
94
- i := GetInstance (id )
125
+ i := instances . GetInstance (id )
95
126
if i == nil {
96
127
return nil
97
128
}
@@ -154,11 +185,7 @@ func Create(req *rpc.CreateRequest, extraUserAgent ...string) (*rpc.CreateRespon
154
185
)
155
186
156
187
// Save instance
157
- instancesMux .Lock ()
158
- instanceID := instancesCount
159
- instances [instanceID ] = instance
160
- instancesCount ++
161
- instancesMux .Unlock ()
188
+ instanceID := instances .AddAndAssignID (instance )
162
189
163
190
return & rpc.CreateResponse {
164
191
Instance : & rpc.Instance {Id : instanceID },
@@ -178,7 +205,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
178
205
if reqInst == nil {
179
206
return & arduino.InvalidInstanceError {}
180
207
}
181
- instance := GetInstance (reqInst .GetId ())
208
+ instance := instances . GetInstance (reqInst .GetId ())
182
209
if instance == nil {
183
210
return & arduino.InvalidInstanceError {}
184
211
}
@@ -430,14 +457,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
430
457
431
458
// Destroy FIXMEDOC
432
459
func Destroy (ctx context.Context , req * rpc.DestroyRequest ) (* rpc.DestroyResponse , error ) {
433
- id := req .GetInstance ().GetId ()
434
-
435
- instancesMux .Lock ()
436
- defer instancesMux .Unlock ()
437
- if _ , ok := instances [id ]; ! ok {
460
+ if ok := instances .RemoveID (req .GetInstance ().GetId ()); ! ok {
438
461
return nil , & arduino.InvalidInstanceError {}
439
462
}
440
- delete (instances , id )
441
463
return & rpc.DestroyResponse {}, nil
442
464
}
443
465
@@ -473,7 +495,7 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
473
495
474
496
// UpdateIndex FIXMEDOC
475
497
func UpdateIndex (ctx context.Context , req * rpc.UpdateIndexRequest , downloadCB rpc.DownloadProgressCB ) (* rpc.UpdateIndexResponse , error ) {
476
- if GetInstance (req .GetInstance ().GetId ()) == nil {
498
+ if instances . GetInstance (req .GetInstance ().GetId ()) == nil {
477
499
return nil , & arduino.InvalidInstanceError {}
478
500
}
479
501
0 commit comments