mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
There are several steps to stopping and starting the world and currently they're open-coded in several places. The garbage collector is the only thing that needs to stop and start the world in a non-trivial pattern. Replace all other uses with calls to higher-level functions that implement the entire pattern necessary to stop and start the world. This is a pure refectoring and should not change any code semantics. In the following commits, we'll make changes that are easier to do with this abstraction in place. This commit renames the old starttheworld to startTheWorldWithSema. This is a slight misnomer right now because the callers release worldsema just before calling this. However, a later commit will swap these and I don't want to think of another name in the mean time. Change-Id: I5dc97f87b44fb98963c49c777d7053653974c911 Reviewed-on: https://go-review.googlesource.com/10154 Reviewed-by: Russ Cox <rsc@golang.org>
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package runtime
|
|
|
|
import "unsafe"
|
|
|
|
// GOMAXPROCS sets the maximum number of CPUs that can be executing
|
|
// simultaneously and returns the previous setting. If n < 1, it does not
|
|
// change the current setting.
|
|
// The number of logical CPUs on the local machine can be queried with NumCPU.
|
|
// This call will go away when the scheduler improves.
|
|
func GOMAXPROCS(n int) int {
|
|
if n > _MaxGomaxprocs {
|
|
n = _MaxGomaxprocs
|
|
}
|
|
lock(&sched.lock)
|
|
ret := int(gomaxprocs)
|
|
unlock(&sched.lock)
|
|
if n <= 0 || n == ret {
|
|
return ret
|
|
}
|
|
|
|
stopTheWorld("GOMAXPROCS")
|
|
|
|
// newprocs will be processed by startTheWorld
|
|
newprocs = int32(n)
|
|
|
|
startTheWorld()
|
|
return ret
|
|
}
|
|
|
|
// NumCPU returns the number of logical CPUs on the local machine.
|
|
func NumCPU() int {
|
|
return int(ncpu)
|
|
}
|
|
|
|
// NumCgoCall returns the number of cgo calls made by the current process.
|
|
func NumCgoCall() int64 {
|
|
var n int64
|
|
for mp := (*m)(atomicloadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
|
|
n += int64(mp.ncgocall)
|
|
}
|
|
return n
|
|
}
|
|
|
|
// NumGoroutine returns the number of goroutines that currently exist.
|
|
func NumGoroutine() int {
|
|
return int(gcount())
|
|
}
|