@@ -22,56 +22,18 @@ type coreInstance struct {
22
22
lm * librariesmanager.LibrariesManager
23
23
}
24
24
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
-
32
25
// 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
69
29
70
30
// GetPackageManager returns a PackageManager. If the package manager is not found
71
31
// (because the instance is invalid or has been destroyed), nil is returned.
72
32
// 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 ()
75
37
if i == nil {
76
38
return nil
77
39
}
@@ -90,8 +52,10 @@ func GetPackageManagerExplorer(req *rpc.Instance) (explorer *packagemanager.Expl
90
52
}
91
53
92
54
// 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 ()
95
59
if i == nil {
96
60
return nil
97
61
}
@@ -100,11 +64,13 @@ func GetLibraryManager(req *rpc.Instance) *librariesmanager.LibrariesManager {
100
64
101
65
// SetLibraryManager sets the library manager for the given instance.
102
66
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 {
105
71
return false
106
72
}
107
- coreInstance .lm = lm
73
+ i .lm = lm
108
74
return true
109
75
}
110
76
@@ -149,22 +115,30 @@ func Create(extraUserAgent ...string) (*rpc.Instance, error) {
149
115
)
150
116
151
117
// 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
154
125
}
155
126
156
127
// IsValid returns true if the given instance is valid.
157
128
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
162
133
}
163
134
164
135
// Delete removes an instance.
165
136
func Delete (inst * rpc.Instance ) bool {
166
- if inst == nil {
137
+ instancesMux .Lock ()
138
+ defer instancesMux .Unlock ()
139
+ if _ , ok := instances [inst .GetId ()]; ! ok {
167
140
return false
168
141
}
169
- return instances .RemoveID (inst .GetId ())
142
+ delete (instances , inst .GetId ())
143
+ return true
170
144
}
0 commit comments