2016-03-18 14:50:19 -04:00
|
|
|
package libcontainerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2016-03-29 12:43:12 -04:00
|
|
|
"github.com/docker/docker/pkg/locker"
|
2016-03-18 14:50:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// clientCommon contains the platform agnostic fields used in the client structure
|
|
|
|
type clientCommon struct {
|
2016-03-29 12:43:12 -04:00
|
|
|
backend Backend
|
|
|
|
containers map[string]*container
|
|
|
|
locker *locker.Locker
|
2017-01-11 10:09:47 -05:00
|
|
|
mapMutex sync.RWMutex // protects read/write operations from containers map
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) lock(containerID string) {
|
2016-03-29 12:43:12 -04:00
|
|
|
clnt.locker.Lock(containerID)
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) unlock(containerID string) {
|
2016-03-29 12:43:12 -04:00
|
|
|
clnt.locker.Unlock(containerID)
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// must hold a lock for cont.containerID
|
|
|
|
func (clnt *client) appendContainer(cont *container) {
|
|
|
|
clnt.mapMutex.Lock()
|
|
|
|
clnt.containers[cont.containerID] = cont
|
|
|
|
clnt.mapMutex.Unlock()
|
|
|
|
}
|
2016-08-15 19:51:45 -04:00
|
|
|
func (clnt *client) deleteContainer(containerID string) {
|
2016-03-18 14:50:19 -04:00
|
|
|
clnt.mapMutex.Lock()
|
2016-08-15 19:51:45 -04:00
|
|
|
delete(clnt.containers, containerID)
|
2016-03-18 14:50:19 -04:00
|
|
|
clnt.mapMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) getContainer(containerID string) (*container, error) {
|
|
|
|
clnt.mapMutex.RLock()
|
|
|
|
container, ok := clnt.containers[containerID]
|
|
|
|
defer clnt.mapMutex.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid container: %s", containerID) // fixme: typed error
|
|
|
|
}
|
|
|
|
return container, nil
|
|
|
|
}
|