Skip to content

Commit f2609ca

Browse files
committed
Inlined coreInstancesContainer singleton methods
1 parent 69397f3 commit f2609ca

File tree

1 file changed

+32
-58
lines changed

1 file changed

+32
-58
lines changed

Diff for: commands/internal/instances/instances.go

+32-58
Original file line numberDiff line numberDiff line change
@@ -22,56 +22,18 @@ type coreInstance struct {
2222
lm *librariesmanager.LibrariesManager
2323
}
2424

25-
// coreInstancesContainer has methods to add an remove instances atomically.
26-
type coreInstancesContainer struct {
27-
instances map[int32]*coreInstance
28-
instancesCount int32
29-
instancesMux sync.Mutex
30-
}
31-
3225
// instances contains all the running Arduino Core Services instances
33-
var instances = &coreInstancesContainer{
34-
instances: map[int32]*coreInstance{},
35-
instancesCount: 1,
36-
}
37-
38-
// GetInstance returns a CoreInstance for the given ID, or nil if ID
39-
// doesn't exist
40-
func (c *coreInstancesContainer) GetInstance(id int32) *coreInstance {
41-
c.instancesMux.Lock()
42-
defer c.instancesMux.Unlock()
43-
return c.instances[id]
44-
}
45-
46-
// AddAndAssignID saves the CoreInstance and assigns a unique ID to
47-
// retrieve it later
48-
func (c *coreInstancesContainer) AddAndAssignID(i *coreInstance) int32 {
49-
c.instancesMux.Lock()
50-
defer c.instancesMux.Unlock()
51-
id := c.instancesCount
52-
c.instances[id] = i
53-
c.instancesCount++
54-
return id
55-
}
56-
57-
// RemoveID removes the CoreInstance referenced by id. Returns true
58-
// if the operation is successful, or false if the CoreInstance does
59-
// not exist
60-
func (c *coreInstancesContainer) RemoveID(id int32) bool {
61-
c.instancesMux.Lock()
62-
defer c.instancesMux.Unlock()
63-
if _, ok := c.instances[id]; !ok {
64-
return false
65-
}
66-
delete(c.instances, id)
67-
return true
68-
}
26+
var instances map[int32]*coreInstance = map[int32]*coreInstance{}
27+
var instancesCount int32 = 1
28+
var instancesMux sync.Mutex
6929

7030
// GetPackageManager returns a PackageManager. If the package manager is not found
7131
// (because the instance is invalid or has been destroyed), nil is returned.
7232
// Deprecated: use GetPackageManagerExplorer instead.
73-
func GetPackageManager(instance *rpc.Instance) *packagemanager.PackageManager {
74-
i := instances.GetInstance(instance.GetId())
33+
func GetPackageManager(inst *rpc.Instance) *packagemanager.PackageManager {
34+
instancesMux.Lock()
35+
i := instances[inst.GetId()]
36+
instancesMux.Unlock()
7537
if i == nil {
7638
return nil
7739
}
@@ -90,8 +52,10 @@ func GetPackageManagerExplorer(req *rpc.Instance) (explorer *packagemanager.Expl
9052
}
9153

9254
// GetLibraryManager returns the library manager for the given instance.
93-
func GetLibraryManager(req *rpc.Instance) *librariesmanager.LibrariesManager {
94-
i := instances.GetInstance(req.GetId())
55+
func GetLibraryManager(inst *rpc.Instance) *librariesmanager.LibrariesManager {
56+
instancesMux.Lock()
57+
i := instances[inst.GetId()]
58+
instancesMux.Unlock()
9559
if i == nil {
9660
return nil
9761
}
@@ -100,11 +64,13 @@ func GetLibraryManager(req *rpc.Instance) *librariesmanager.LibrariesManager {
10064

10165
// SetLibraryManager sets the library manager for the given instance.
10266
func SetLibraryManager(inst *rpc.Instance, lm *librariesmanager.LibrariesManager) bool {
103-
coreInstance := instances.GetInstance(inst.GetId())
104-
if coreInstance == nil {
67+
instancesMux.Lock()
68+
i := instances[inst.GetId()]
69+
instancesMux.Unlock()
70+
if i == nil {
10571
return false
10672
}
107-
coreInstance.lm = lm
73+
i.lm = lm
10874
return true
10975
}
11076

@@ -149,22 +115,30 @@ func Create(extraUserAgent ...string) (*rpc.Instance, error) {
149115
)
150116

151117
// Save instance
152-
instanceID := instances.AddAndAssignID(instance)
153-
return &rpc.Instance{Id: instanceID}, nil
118+
instancesMux.Lock()
119+
id := instancesCount
120+
instances[id] = instance
121+
instancesCount++
122+
instancesMux.Unlock()
123+
124+
return &rpc.Instance{Id: id}, nil
154125
}
155126

156127
// IsValid returns true if the given instance is valid.
157128
func IsValid(inst *rpc.Instance) bool {
158-
if inst == nil {
159-
return false
160-
}
161-
return instances.GetInstance(inst.GetId()) != nil
129+
instancesMux.Lock()
130+
i := instances[inst.GetId()]
131+
instancesMux.Unlock()
132+
return i != nil
162133
}
163134

164135
// Delete removes an instance.
165136
func Delete(inst *rpc.Instance) bool {
166-
if inst == nil {
137+
instancesMux.Lock()
138+
defer instancesMux.Unlock()
139+
if _, ok := instances[inst.GetId()]; !ok {
167140
return false
168141
}
169-
return instances.RemoveID(inst.GetId())
142+
delete(instances, inst.GetId())
143+
return true
170144
}

0 commit comments

Comments
 (0)