github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/sandboxlist.go (about) 1 // Copyright (c) 2018 HyperHQ Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package virtcontainers 7 8 import ( 9 "fmt" 10 "sync" 11 ) 12 13 type sandboxList struct { 14 lock sync.RWMutex 15 sandboxes map[string]*Sandbox 16 } 17 18 // globalSandboxList tracks sandboxes globally 19 var globalSandboxList = &sandboxList{sandboxes: make(map[string]*Sandbox)} 20 21 func (p *sandboxList) addSandbox(sandbox *Sandbox) (err error) { 22 if sandbox == nil { 23 return nil 24 } 25 26 p.lock.Lock() 27 defer p.lock.Unlock() 28 if p.sandboxes[sandbox.id] == nil { 29 p.sandboxes[sandbox.id] = sandbox 30 } else { 31 err = fmt.Errorf("sandbox %s exists", sandbox.id) 32 } 33 return err 34 } 35 36 func (p *sandboxList) removeSandbox(id string) { 37 p.lock.Lock() 38 defer p.lock.Unlock() 39 delete(p.sandboxes, id) 40 } 41 42 func (p *sandboxList) lookupSandbox(id string) (*Sandbox, error) { 43 p.lock.RLock() 44 defer p.lock.RUnlock() 45 if p.sandboxes[id] != nil { 46 return p.sandboxes[id], nil 47 } 48 return nil, fmt.Errorf("sandbox %s does not exist", id) 49 }