@@ -19,10 +19,11 @@ import (
1919)
2020
2121var (
22- pid = os .Getpid ()
23- maxThread = int32 (runtime .NumCPU ()) + 2 // 2 meaning runtime sysmon thread + template thread
24- interval = time .Second
25- debug = false
22+ pid = os .Getpid ()
23+ minThreads = int32 (runtime .NumCPU ()) + 2 // minimum number of threads required by the runtime
24+ maxThreads = int32 (runtime .NumCPU ()) + 2 // 2 meaning runtime sysmon thread + template thread
25+ interval = time .Second
26+ debug = false
2627)
2728
2829// NumM returns the number of running threads.
@@ -41,14 +42,15 @@ func NumM() int {
4142}
4243
4344// GOMAXTHREADS sets the maximum number of system threads that allowed in a Go program
44- // and returns the previous setting. If n < 1, it does not change the current setting.
45- // The default allowed number of threads of a program is runtime.NumCPU() + 2.
45+ // and returns the previous setting. If n is lower than minimum required number of threads,
46+ // it does not change the current setting.
47+ // The minimum allowed number of threads of a program is runtime.NumCPU() + 2.
4648func GOMAXTHREADS (n int ) int {
47- if n < 1 {
48- return int (atomic .LoadInt32 (& maxThread ))
49+ if n < int ( minThreads ) {
50+ return int (atomic .LoadInt32 (& maxThreads ))
4951 }
5052
51- return int (atomic .SwapInt32 (& maxThread , int32 (n )))
53+ return int (atomic .SwapInt32 (& maxThreads , int32 (n )))
5254}
5355
5456// Wait waits until the number of threads meet the GOMAXTHREADS settings.
@@ -82,7 +84,7 @@ func checkwork() {
8284func init () {
8385 checkwork ()
8486 if debug {
85- fmt .Printf ("mkill: pid %v, maxThread %v, interval %v\n " , pid , maxThread , interval )
87+ fmt .Printf ("mkill: pid %v, maxThread %v, interval %v\n " , pid , maxThreads , interval )
8688 }
8789
8890 wg := sync.WaitGroup {}
@@ -92,10 +94,10 @@ func init() {
9294 select {
9395 case <- t .C :
9496 n := NumM ()
95- nkill := int32 (n ) - atomic .LoadInt32 (& maxThread )
97+ nkill := int32 (n ) - atomic .LoadInt32 (& maxThreads )
9698 if nkill <= 0 {
9799 if debug {
98- fmt .Printf ("mkill: checked #threads total %v / max %v\n " , n , maxThread )
100+ fmt .Printf ("mkill: checked #threads total %v / max %v\n " , n , maxThreads )
99101 }
100102 continue
101103 }
0 commit comments