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
|
|
|
|
mapMutex sync.RWMutex // protects read/write oprations 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()
|
|
|
|
}
|
|
|
|
func (clnt *client) deleteContainer(friendlyName string) {
|
|
|
|
clnt.mapMutex.Lock()
|
|
|
|
delete(clnt.containers, friendlyName)
|
|
|
|
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
|
|
|
|
}
|