Skip to content

Commit 36e1dac

Browse files
committed
fix: uniqMcs use all cpu
Signed-off-by: ningmingxiao <[email protected]>
1 parent a8faa24 commit 36e1dac

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

go-selinux/selinux_linux.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ const (
3939

4040
type selinuxState struct {
4141
mcsList map[string]bool
42+
cond *sync.Cond
4243
selinuxfs string
44+
maxMCSCount int
4345
selinuxfsOnce sync.Once
44-
enabledSet bool
45-
enabled bool
46+
mu sync.Mutex
4647
sync.Mutex
48+
enabledSet bool
49+
enabled bool
4750
}
48-
4951
type level struct {
5052
cats *big.Int
5153
sens int
@@ -936,6 +938,18 @@ func mcsAdd(mcs string) error {
936938
if mcs == "" {
937939
return nil
938940
}
941+
state.mu.Lock()
942+
if state.maxMCSCount == 0 {
943+
state.maxMCSCount = int(CategoryRange * (CategoryRange - 1) / 2)
944+
}
945+
if state.cond == nil {
946+
state.cond = sync.NewCond(&state.mu)
947+
}
948+
if len(state.mcsList) >= state.maxMCSCount {
949+
state.cond.Wait()
950+
}
951+
state.mu.Unlock()
952+
939953
state.Lock()
940954
defer state.Unlock()
941955
if state.mcsList[mcs] {
@@ -952,6 +966,9 @@ func mcsDelete(mcs string) {
952966
state.Lock()
953967
defer state.Unlock()
954968
state.mcsList[mcs] = false
969+
state.mu.Lock()
970+
state.cond.Signal()
971+
state.mu.Unlock()
955972
}
956973

957974
func intToMcs(id int, catRange uint32) string {

0 commit comments

Comments
 (0)