goroutine

package
v1.2.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2022 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DebugGoroutines = os.Getenv("DEBUG_GOROUTINES") == "1"

Functions

func ID

func ID() uint64

ID returns goroutine id of the goroutine that calls it. It calls runtime.Stack with a large enough buffer to capture the entire trace.

Example
package main

import (
	"fmt"

	"github.com/searKing/golang/go/runtime/goroutine"
)

func main() {
	fmt.Printf("%d\n", goroutine.ID())
}
Output:

1

Types

type Lock

type Lock uint64

Lock represents a goroutine ID, with goroutine ID checked, that is whether GoRoutines of lock newer and check caller differ. disable when DebugGoroutines equals false

func NewLock

func NewLock() Lock

NewLock returns a goroutine Lock, that checks whether goroutine of lock newer and check caller differ. Code borrowed from https://github.com/golang/go/blob/master/src/net/http/h2_bundle.go

Example
package main

import (
	"fmt"
	"sync"

	"github.com/searKing/golang/go/runtime/goroutine"
)

func main() {
	oldDebug := goroutine.DebugGoroutines
	goroutine.DebugGoroutines = true
	defer func() { goroutine.DebugGoroutines = oldDebug }()

	g := goroutine.NewLock()
	g.MustCheck()

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		defer func() {
			if r := recover(); r != nil {
				fmt.Printf("panic recovered: %v\n", r)
			}
		}()
		g.MustCheck() // should panic
	}()
	wg.Wait()
}
Output:

panic recovered: running on the wrong goroutine

func (Lock) Check

func (g Lock) Check() error

Check if caller's goroutine is locked

func (Lock) CheckNotOn

func (g Lock) CheckNotOn() error

Check whether caller's goroutine escape lock

func (Lock) MustCheck

func (g Lock) MustCheck()

func (Lock) MustCheckNotOn

func (g Lock) MustCheckNotOn()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL