1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // From OpenBSD's sys/time.h
22 var sigset_none = uint32(0)
23 var sigset_all = ^sigset_none
25 // From OpenBSD's <sys/sysctl.h>
31 func getncpu() int32 {
32 mib := [2]uint32{CTL_HW, HW_NCPU}
34 nout := unsafe.Sizeof(out)
36 // Fetch hw.ncpu via sysctl.
37 ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
45 func semacreate() uintptr {
50 func semasleep(ns int64) int32 {
53 // Compute sleep deadline.
59 ts.set_sec(int64(timediv(ns, 1000000000, &nsec)))
67 if xchg(&_g_.m.waitsemalock, 1) == 0 {
73 if _g_.m.waitsemacount != 0 {
74 // semaphore is available.
77 atomicstore(&_g_.m.waitsemalock, 0)
78 return 0 // semaphore acquired
81 // sleep until semaphore != 0 or timeout.
82 // thrsleep unlocks m.waitsemalock.
83 ret := thrsleep((uintptr)(unsafe.Pointer(&_g_.m.waitsemacount)), CLOCK_MONOTONIC, tsp, (uintptr)(unsafe.Pointer(&_g_.m.waitsemalock)), (*int32)(unsafe.Pointer(&_g_.m.waitsemacount)))
84 if ret == EWOULDBLOCK {
91 func semawakeup(mp *m) {
94 if xchg(&mp.waitsemalock, 1) == 0 {
100 ret := thrwakeup(uintptr(unsafe.Pointer(&mp.waitsemacount)), 1)
101 if ret != 0 && ret != ESRCH {
102 // semawakeup can be called on signal stack.
104 print("thrwakeup addr=", &mp.waitsemacount, " sem=", mp.waitsemacount, " ret=", ret, "\n")
108 atomicstore(&mp.waitsemalock, 0)
111 func newosproc(mp *m, stk unsafe.Pointer) {
113 print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " id=", mp.id, "/", int32(mp.tls[0]), " ostk=", &mp, "\n")
116 mp.tls[0] = uintptr(mp.id) // so 386 asm can find it
119 tf_tcb: unsafe.Pointer(&mp.tls[0]),
120 tf_tid: (*int32)(unsafe.Pointer(&mp.procid)),
121 tf_stack: uintptr(stk),
124 oset := sigprocmask(_SIG_SETMASK, sigset_all)
125 ret := tfork(¶m, unsafe.Sizeof(param), mp, mp.g0, funcPC(mstart))
126 sigprocmask(_SIG_SETMASK, oset)
129 print("runtime: failed to create new OS thread (have ", mcount()-1, " already; errno=", -ret, ")\n")
131 print("runtime: is kern.rthreads disabled?\n")
133 gothrow("runtime.newosproc")
141 var urandom_data [_HashRandomBytes]byte
142 var urandom_dev = []byte("/dev/urandom\x00")
145 func get_random_data(rnd *unsafe.Pointer, rnd_len *int32) {
146 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
147 if read(fd, unsafe.Pointer(&urandom_data), _HashRandomBytes) == _HashRandomBytes {
148 *rnd = unsafe.Pointer(&urandom_data[0])
149 *rnd_len = _HashRandomBytes
161 // Called to initialize a new m (including the bootstrap m).
162 // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
163 func mpreinit(mp *m) {
164 mp.gsignal = malg(32 * 1024)
168 // Called to initialize a new m (including the bootstrap m).
169 // Called on the new thread, can not allocate memory.
173 // m.procid is a uint64, but tfork writes an int32. Fix it up.
174 _g_.m.procid = uint64(*(*int32)(unsafe.Pointer(&_g_.m.procid)))
176 // Initialize signal handling
177 signalstack((*byte)(unsafe.Pointer(_g_.m.gsignal.stack.lo)), 32*1024)
178 sigprocmask(_SIG_SETMASK, sigset_none)
181 // Called from dropm to undo the effect of an minit.
186 func memlimit() uintptr {
192 type sigactiont struct {
198 func setsig(i int32, fn uintptr, restart bool) {
200 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK
202 sa.sa_flags |= _SA_RESTART
204 sa.sa_mask = sigset_all
205 if fn == funcPC(sighandler) {
206 fn = funcPC(sigtramp)
209 sigaction(i, &sa, nil)
212 func getsig(i int32) uintptr {
214 sigaction(i, nil, &sa)
215 if sa.sa_sigaction == funcPC(sigtramp) {
216 return funcPC(sighandler)
218 return sa.sa_sigaction
221 func signalstack(p *byte, n int32) {
224 st.ss_sp = uintptr(unsafe.Pointer(p))
225 st.ss_size = uintptr(n)
228 st.ss_flags = _SS_DISABLE
230 sigaltstack(&st, nil)
233 func unblocksignals() {
234 sigprocmask(_SIG_SETMASK, sigset_none)