2016-03-18 18:50:19 +00:00
|
|
|
package libcontainerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2016-03-29 16:43:12 +00:00
|
|
|
"github.com/docker/docker/pkg/locker"
|
2016-03-18 18:50:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// clientCommon contains the platform agnostic fields used in the client structure
|
|
|
|
type clientCommon struct {
|
2016-03-29 16:43:12 +00:00
|
|
|
backend Backend
|
|
|
|
containers map[string]*container
|
|
|
|
locker *locker.Locker
|
|
|
|
mapMutex sync.RWMutex // protects read/write oprations from containers map
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) lock(containerID string) {
|
2016-03-29 16:43:12 +00:00
|
|
|
clnt.locker.Lock(containerID)
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (clnt *client) unlock(containerID string) {
|
2016-03-29 16:43:12 +00:00
|
|
|
clnt.locker.Unlock(containerID)
|
2016-03-18 18:50:19 +00: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 23:51:45 +00:00
|
|
|
func (clnt *client) deleteContainer(containerID string) {
|
2016-03-18 18:50:19 +00:00
|
|
|
clnt.mapMutex.Lock()
|
2016-08-15 23:51:45 +00:00
|
|
|
delete(clnt.containers, containerID)
|
2016-03-18 18:50:19 +00: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
|
|
|
|
}
|