func getLink() string { l, err := os.Readlink(fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid())) if err != nil { return fmt.Sprintf("(nil: %v)", err) } return l }
func init() { // Record pid and tid of init thread for use during test. // The call to LockOSThread is just to exercise it; // we can't test that it does anything. // Instead we're testing that the conditions are good // for how it is used in init (must be on main thread). pid, tid = syscall.Getpid(), syscall.Gettid() LockOSThread() }
func (f *FakeSystem) Update(timestep time.Duration) (stop bool, err error) { f.updateTids = AppendTid(f.updateTids, syscall.Gettid()) f.updateCount++ if f.updateCount >= f.maxUpdates { stop = true } f.totalTimeMs += float64(timestep.Nanoseconds()) / 1000000 runtime.Gosched() stop = stop || f.updateReturnStop err = f.updateReturnErr return }
func suspend(g *godit) { // finalize termbox termbox.Close() // suspend the process pid := syscall.Getpid() tid := syscall.Gettid() err := syscall.Tgkill(pid, tid, syscall.SIGSTOP) if err != nil { panic(err) } // reset the state so we can get back to work again err = termbox.Init() if err != nil { panic(err) } termbox.SetInputMode(termbox.InputAlt) g.resize() }
func main() { fmt.Println("--------", runtime.GOMAXPROCS(runtime.NumCPU()), runtime.NumCPU()) for i := 0; i < 10; i++ { go func(id int) { ch := time.Tick(time.Second * 4) for now := range ch { // bounce bettween many threads => a goroutine can be run by any real thread fmt.Printf("%v pid: %d:%d\n", now, syscall.Gettid(), id) } // for j := 0; j < 10; j++ { // fmt.Println("pid is:", ) // time.Sleep(time.Second * 3) // } }(i) } time.Sleep(time.Second * 300) }
func testThreadLock(t *testing.T) { stop := make(chan int) go func() { // We need the G continue running, // so the M has a chance to run this G. for { select { case <-stop: return case <-time.After(time.Millisecond * 100): } } }() defer close(stop) for i := 0; i < 1000; i++ { if C.int(syscall.Gettid()) != C.Ctid() { t.Fatalf("cgo has not locked OS thread") } } }
func find_unused_thread(tries int, wg *sync.WaitGroup, reg *pidRegistry, worker func()) { for i := 0; i <= tries; i++ { reg.lock.Lock() runtime.LockOSThread() pid := syscall.Gettid() if _, taken := reg.pids[pid]; !taken { reg.pids[pid] = true reg.lock.Unlock() runtime.Gosched() //block to aid context switching worker() reg.lock.Lock() delete(reg.pids, pid) reg.lock.Unlock() wg.Done() return } else { reg.lock.Unlock() runtime.UnlockOSThread() } } log.Println("goroutine given up finding a new thread") wg.Done() }
func createNetworkNamespace(path string, osCreate bool) (*Info, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() origns, err := netns.Get() if err != nil { return nil, err } defer origns.Close() if err := createNamespaceFile(path); err != nil { return nil, err } if osCreate { defer netns.Set(origns) newns, err := netns.New() if err != nil { return nil, err } defer newns.Close() if err := loopbackUp(); err != nil { return nil, err } } procNet := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()) if err := syscall.Mount(procNet, path, "bind", syscall.MS_BIND, ""); err != nil { return nil, err } interfaces := []*Interface{} info := &Info{Interfaces: interfaces} return info, nil }
func Setfscreatecon(scon string) error { return writeCon(fmt.Sprintf("/proc/self/task/%d/attr/fscreate", syscall.Gettid()), scon) }
It("locks and unlocks the os thread", func() { err := ns.Execute(func(*os.File) error { defer GinkgoRecover() Expect(threadLocker.LockOSThreadCallCount()).To(Equal(1)) Expect(threadLocker.UnlockOSThreadCallCount()).To(Equal(0)) return nil }) Expect(err).NotTo(HaveOccurred()) Expect(threadLocker.LockOSThreadCallCount()).To(Equal(1)) Expect(threadLocker.UnlockOSThreadCallCount()).To(Equal(1)) }) It("runs the callback on a separate os task", func() { var ttid int err := ns.Execute(func(*os.File) error { ttid = syscall.Gettid() return nil }) Expect(err).NotTo(HaveOccurred()) Expect(syscall.Gettid()).NotTo(Equal(ttid)) }) Context("when the callback fails", func() { It("logs the error", func() { ns.Execute(func(*os.File) error { return errors.New("potato") }) Expect(logger).To(gbytes.Say("execute.callback-failed.*potato")) }) }) })
func getLink() (string, error) { return os.Readlink(fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid())) }
func (f *FakeSystem) Exit() { f.exitTids = AppendTid(f.exitTids, syscall.Gettid()) f.exitCount++ }
// getThreadID returns an opaque value that is unique per OS thread func getThreadID() uintptr { return uintptr(syscall.Gettid()) }
func Setexeccon(scon string) error { return writeCon(fmt.Sprintf("/proc/self/task/%d/attr/exec", syscall.Gettid()), scon) }
func Getfscreatecon() (string, error) { return readCon(fmt.Sprintf("/proc/self/task/%d/attr/fscreate", syscall.Gettid())) }
// Get gets a handle to the current threads network namespace. func Get() (NsHandle, error) { return GetFromThread(os.Getpid(), syscall.Gettid()) }
func gettid() int { tid := syscall.Gettid() return tid }
func GetThreadId() int { return syscall.Gettid() }
func Getexeccon() (string, error) { return readCon(fmt.Sprintf("/proc/self/task/%d/attr/exec", syscall.Gettid())) }
func (f *FakeSystem) Init() error { f.initTids = AppendTid(f.initTids, syscall.Gettid()) return f.initReturn }
func Gettid() int { return syscall.Gettid() }
func taskNamespacePath() string { return fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()) }