github.com/skf/moby@v1.13.1/libcontainerd/client.go (about)

     1  package libcontainerd
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/docker/docker/pkg/locker"
     8  )
     9  
    10  // clientCommon contains the platform agnostic fields used in the client structure
    11  type clientCommon struct {
    12  	backend    Backend
    13  	containers map[string]*container
    14  	locker     *locker.Locker
    15  	mapMutex   sync.RWMutex // protects read/write oprations from containers map
    16  }
    17  
    18  func (clnt *client) lock(containerID string) {
    19  	clnt.locker.Lock(containerID)
    20  }
    21  
    22  func (clnt *client) unlock(containerID string) {
    23  	clnt.locker.Unlock(containerID)
    24  }
    25  
    26  // must hold a lock for cont.containerID
    27  func (clnt *client) appendContainer(cont *container) {
    28  	clnt.mapMutex.Lock()
    29  	clnt.containers[cont.containerID] = cont
    30  	clnt.mapMutex.Unlock()
    31  }
    32  func (clnt *client) deleteContainer(containerID string) {
    33  	clnt.mapMutex.Lock()
    34  	delete(clnt.containers, containerID)
    35  	clnt.mapMutex.Unlock()
    36  }
    37  
    38  func (clnt *client) getContainer(containerID string) (*container, error) {
    39  	clnt.mapMutex.RLock()
    40  	container, ok := clnt.containers[containerID]
    41  	defer clnt.mapMutex.RUnlock()
    42  	if !ok {
    43  		return nil, fmt.Errorf("invalid container: %s", containerID) // fixme: typed error
    44  	}
    45  	return container, nil
    46  }