github.com/smallnest/gid@v1.2.0/gid_g121.go (about)

     1  // Copyright ©2023 Dan Kortschak. 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.
     4  
     5  //go:build go1.21
     6  // +build go1.21
     7  
     8  package gid
     9  
    10  import "unsafe"
    11  
    12  // ParentID returns the runtime ID of goroutine that created the calling
    13  // goroutine.
    14  func ParentID() int64 {
    15  	return idOf(getg(), parentGoidoff)
    16  }
    17  
    18  var parentGoidoff = offset("*runtime.g", "parentGoid")
    19  
    20  // Link is a goroutine parent-child relationship.
    21  type Link struct {
    22  	Parent, Child int64
    23  }
    24  
    25  // All returns all the known goroutine parent-child relationships.
    26  func All() []Link {
    27  	var s []Link
    28  	forEachG(func(g unsafe.Pointer) {
    29  		s = append(s, Link{Parent: idOf(g, parentGoidoff), Child: idOf(g, goidoff)})
    30  	})
    31  	return s
    32  }
    33  
    34  //go:linkname forEachG runtime.forEachG
    35  //go:nosplit
    36  func forEachG(fn func(gp unsafe.Pointer))