github.com/madokast/nopreempt@v1.0.3-0.20240125081507-3fa05aef38ed/nopreempt.go (about)

     1  package nopreempt
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  type MP struct {
     8  	mp uintptr
     9  }
    10  
    11  //go:linkname acquirem runtime.acquirem
    12  func acquirem() unsafe.Pointer
    13  
    14  //go:linkname releasem runtime.releasem
    15  func releasem(unsafe.Pointer)
    16  
    17  func GetGID() int64 {
    18  	return getg().goid
    19  }
    20  
    21  func GetMID() int64 {
    22  	return getg().m.id
    23  }
    24  
    25  func AcquireM() MP {
    26  	return MP{
    27  		mp: uintptr(acquirem()),
    28  	}
    29  }
    30  
    31  func (mp MP) MID() int64 {
    32  	return (*m)(unsafe.Pointer(mp.mp)).id
    33  }
    34  
    35  func (mp MP) Release() {
    36  	releasem(unsafe.Pointer(mp.mp))
    37  }