@@ -38,8 +38,11 @@ type VMController struct {
38
38
39
39
var vmLogger = flogging .MustGetLogger ("container" )
40
40
41
- var vmcontroller = & VMController {
42
- containerLocks : make (map [string ]* refCountedLock ),
41
+ // NewVMController creates a new instance of VMController
42
+ func NewVMController () * VMController {
43
+ return & VMController {
44
+ containerLocks : make (map [string ]* refCountedLock ),
45
+ }
43
46
}
44
47
45
48
//constants for supported containers
@@ -64,37 +67,37 @@ func (vmc *VMController) newVM(typ string) api.VM {
64
67
65
68
func (vmc * VMController ) lockContainer (id string ) {
66
69
//get the container lock under global lock
67
- vmcontroller .Lock ()
70
+ vmc .Lock ()
68
71
var refLck * refCountedLock
69
72
var ok bool
70
- if refLck , ok = vmcontroller .containerLocks [id ]; ! ok {
73
+ if refLck , ok = vmc .containerLocks [id ]; ! ok {
71
74
refLck = & refCountedLock {refCount : 1 , lock : & sync.RWMutex {}}
72
- vmcontroller .containerLocks [id ] = refLck
75
+ vmc .containerLocks [id ] = refLck
73
76
} else {
74
77
refLck .refCount ++
75
78
vmLogger .Debugf ("refcount %d (%s)" , refLck .refCount , id )
76
79
}
77
- vmcontroller .Unlock ()
80
+ vmc .Unlock ()
78
81
vmLogger .Debugf ("waiting for container(%s) lock" , id )
79
82
refLck .lock .Lock ()
80
83
vmLogger .Debugf ("got container (%s) lock" , id )
81
84
}
82
85
83
86
func (vmc * VMController ) unlockContainer (id string ) {
84
- vmcontroller .Lock ()
85
- if refLck , ok := vmcontroller .containerLocks [id ]; ok {
87
+ vmc .Lock ()
88
+ if refLck , ok := vmc .containerLocks [id ]; ok {
86
89
if refLck .refCount <= 0 {
87
90
panic ("refcnt <= 0" )
88
91
}
89
92
refLck .lock .Unlock ()
90
93
if refLck .refCount -- ; refLck .refCount == 0 {
91
94
vmLogger .Debugf ("container lock deleted(%s)" , id )
92
- delete (vmcontroller .containerLocks , id )
95
+ delete (vmc .containerLocks , id )
93
96
}
94
97
} else {
95
98
vmLogger .Debugf ("no lock to unlock(%s)!!" , id )
96
99
}
97
- vmcontroller .Unlock ()
100
+ vmc .Unlock ()
98
101
}
99
102
100
103
//VMCReqIntf - all requests should implement this interface.
@@ -165,16 +168,16 @@ func (si StopContainerReq) getCCID() ccintf.CCID {
165
168
return si .CCID
166
169
}
167
170
168
- //VMCProcess should be used as follows
171
+ //Process should be used as follows
169
172
// . construct a context
170
173
// . construct req of the right type (e.g., CreateImageReq)
171
174
// . call it in a go routine
172
175
// . process response in the go routing
173
176
//context can be cancelled. VMCProcess will try to cancel calling functions if it can
174
177
//For instance docker clients api's such as BuildImage are not cancelable.
175
178
//In all cases VMCProcess will wait for the called go routine to return
176
- func VMCProcess (ctxt context.Context , vmtype string , req VMCReqIntf ) (VMCResp , error ) {
177
- v := vmcontroller .newVM (vmtype )
179
+ func ( vmc * VMController ) Process (ctxt context.Context , vmtype string , req VMCReqIntf ) (VMCResp , error ) {
180
+ v := vmc .newVM (vmtype )
178
181
if v == nil {
179
182
return VMCResp {}, fmt .Errorf ("Unknown VM type %s" , vmtype )
180
183
}
@@ -189,9 +192,9 @@ func VMCProcess(ctxt context.Context, vmtype string, req VMCReqIntf) (VMCResp, e
189
192
resp = VMCResp {Err : err }
190
193
return
191
194
}
192
- vmcontroller .lockContainer (id )
195
+ vmc .lockContainer (id )
193
196
resp = req .do (ctxt , v )
194
- vmcontroller .unlockContainer (id )
197
+ vmc .unlockContainer (id )
195
198
}()
196
199
197
200
select {
0 commit comments