@@ -619,46 +619,33 @@ type containerServerState struct {
619619
620620// AddContainer adds a container to the container state store
621621func (c * ContainerServer ) AddContainer (ctr * oci.Container ) {
622- c .stateLock .Lock ()
623- defer c .stateLock .Unlock ()
624622 sandbox := c .state .sandboxes .Get (ctr .Sandbox ())
625623 sandbox .AddContainer (ctr )
626624 c .state .containers .Add (ctr .ID (), ctr )
627625}
628626
629627// AddInfraContainer adds a container to the container state store
630628func (c * ContainerServer ) AddInfraContainer (ctr * oci.Container ) {
631- c .stateLock .Lock ()
632- defer c .stateLock .Unlock ()
633629 c .state .infraContainers .Add (ctr .ID (), ctr )
634630}
635631
636632// GetContainer returns a container by its ID
637633func (c * ContainerServer ) GetContainer (id string ) * oci.Container {
638- c .stateLock .Lock ()
639- defer c .stateLock .Unlock ()
640634 return c .state .containers .Get (id )
641635}
642636
643637// GetInfraContainer returns a container by its ID
644638func (c * ContainerServer ) GetInfraContainer (id string ) * oci.Container {
645- c .stateLock .Lock ()
646- defer c .stateLock .Unlock ()
647639 return c .state .infraContainers .Get (id )
648640}
649641
650642// HasContainer checks if a container exists in the state
651643func (c * ContainerServer ) HasContainer (id string ) bool {
652- c .stateLock .Lock ()
653- defer c .stateLock .Unlock ()
654- ctr := c .state .containers .Get (id )
655- return ctr != nil
644+ return c .state .containers .Get (id ) != nil
656645}
657646
658647// RemoveContainer removes a container from the container state store
659648func (c * ContainerServer ) RemoveContainer (ctr * oci.Container ) {
660- c .stateLock .Lock ()
661- defer c .stateLock .Unlock ()
662649 sbID := ctr .Sandbox ()
663650 sb := c .state .sandboxes .Get (sbID )
664651 sb .RemoveContainer (ctr )
@@ -667,15 +654,11 @@ func (c *ContainerServer) RemoveContainer(ctr *oci.Container) {
667654
668655// RemoveInfraContainer removes a container from the container state store
669656func (c * ContainerServer ) RemoveInfraContainer (ctr * oci.Container ) {
670- c .stateLock .Lock ()
671- defer c .stateLock .Unlock ()
672657 c .state .infraContainers .Delete (ctr .ID ())
673658}
674659
675660// listContainers returns a list of all containers stored by the server state
676661func (c * ContainerServer ) listContainers () []* oci.Container {
677- c .stateLock .Lock ()
678- defer c .stateLock .Unlock ()
679662 return c .state .containers .List ()
680663}
681664
@@ -699,43 +682,36 @@ func (c *ContainerServer) ListContainers(filters ...func(*oci.Container) bool) (
699682
700683// AddSandbox adds a sandbox to the sandbox state store
701684func (c * ContainerServer ) AddSandbox (sb * sandbox.Sandbox ) {
702- c .stateLock .Lock ()
703- defer c .stateLock .Unlock ()
704685 c .state .sandboxes .Add (sb .ID (), sb )
686+
687+ c .stateLock .Lock ()
705688 c .state .processLevels [selinux .NewContext (sb .ProcessLabel ())["level" ]]++
689+ c .stateLock .Unlock ()
706690}
707691
708692// GetSandbox returns a sandbox by its ID
709693func (c * ContainerServer ) GetSandbox (id string ) * sandbox.Sandbox {
710- c .stateLock .Lock ()
711- defer c .stateLock .Unlock ()
712694 return c .state .sandboxes .Get (id )
713695}
714696
715697// GetSandboxContainer returns a sandbox's infra container
716698func (c * ContainerServer ) GetSandboxContainer (id string ) * oci.Container {
717- c .stateLock .Lock ()
718- defer c .stateLock .Unlock ()
719699 sb := c .state .sandboxes .Get (id )
720700 return sb .InfraContainer ()
721701}
722702
723703// HasSandbox checks if a sandbox exists in the state
724704func (c * ContainerServer ) HasSandbox (id string ) bool {
725- c .stateLock .Lock ()
726- defer c .stateLock .Unlock ()
727- sb := c .state .sandboxes .Get (id )
728- return sb != nil
705+ return c .state .sandboxes .Get (id ) != nil
729706}
730707
731708// RemoveSandbox removes a sandbox from the state store
732709func (c * ContainerServer ) RemoveSandbox (id string ) {
733- c .stateLock .Lock ()
734- defer c .stateLock .Unlock ()
735710 sb := c .state .sandboxes .Get (id )
736711 processLabel := sb .ProcessLabel ()
737- c .state .sandboxes .Delete (id )
738712 level := selinux .NewContext (processLabel )["level" ]
713+
714+ c .stateLock .Lock ()
739715 pl , ok := c .state .processLevels [level ]
740716 if ok {
741717 c .state .processLevels [level ] = pl - 1
@@ -744,12 +720,13 @@ func (c *ContainerServer) RemoveSandbox(id string) {
744720 delete (c .state .processLevels , level )
745721 }
746722 }
723+ c .stateLock .Unlock ()
724+
725+ c .state .sandboxes .Delete (id )
747726}
748727
749728// ListSandboxes lists all sandboxes in the state store
750729func (c * ContainerServer ) ListSandboxes () []* sandbox.Sandbox {
751- c .stateLock .Lock ()
752- defer c .stateLock .Unlock ()
753730 return c .state .sandboxes .List ()
754731}
755732
0 commit comments