goroutine

package module
v0.0.0-...-53e1d22 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2018 License: BSD-3-Clause, MIT Imports: 25 Imported by: 5

README

Hacking goroutine

Build Status

[DEPRECATED]

Package goroutine is merely a hack. It exports goroutine id to outside so that you can use it for whatever purpose. However, it's highly recommended to not use this package in your daily life. It may be broken at any go release as it's a hack.

Usage

Get the latest version through go get -u github.com/huandu/goroutine.

Get current goroutine id with goroutine.GoroutineId().

// Get id of current goroutine.
var id int64 = goroutine.GoroutineId()
println(id)

See godoc for more details.

Supported builds

Package goroutine is not well tested due to lack of test machines. Ideally, it should work on all go >= go1.5.

Tested platforms.

  • Darwin (Mac OSX 10.11.6) + amd64 CPU
    • go1.5.1
    • go1.6.3
    • go1.7
    • go1.7.1
    • go1.7.3
    • go1.7.4
    • go1.7.5
    • go1.8
    • go1.8.1
    • go1.9
    • go1.9.2
  • Travis CI (See https://travis-ci.org/huandu/goroutine)
    • go1.5
    • go1.5.1
    • go1.5.2
    • go1.5.3
    • go1.5.4
    • go1.6
    • go1.6.1
    • go1.6.2
    • go1.6.3
    • go1.7
    • go1.7.1
    • go1.7.2
    • go1.7.3
    • go1.7.4
    • go1.7.5
    • go1.8
    • go1.8.1
    • go1.8.2
    • go1.8.3
    • go1.9
    • go1.9.1
    • go1.9.2

How it works

Go runtime inside a Go program binary is statically linked. It means, if I know Go version and runtime source code for this version, I can copy struct declaration from runtime package source to my package and cast runtime internal pointers to its underlying struct safely. As Go source code is open for everyone, I can always find the right struct for an interesting runtime pointer and then manipulate it.

In this package, I just get current goroutine pointer (copy the getg() implementation from compiler), cast it to a right struct and then return the id. It sounds simple but of course not. The struct g refers to many other internal types defined in runtime package. I cannot simply copy some necessary types to make it work. I have to scan all types and constants in runtime and its internal packages to make the struct g well defined. Another challenge is that Go authors update runtime structs in nearly every major version (or even in a minor version). I have to maintain hacked code for every Go release respectively. I develop a semi-automatical tool to make things easier. The tool is not smart enough. I may need to think of other better way to avoid to generate hacked source code for every Go release.

NOTE: Starting from go1.7.2, Go compiler generates some constant definitions for runtime package according to build flags and environment when building. It makes current hack impossible to handle all posibile flags and environment combinations. I make a hack to detour it and no impact to the major task of this package - get goroutine id. However, it's not a perfect solution.

I'm thinking of a perfect solution. If you have any suggestion, please open issue and let me know. Many thanks.

License

This package is licensed under MIT license. See LICENSE for details.

Documentation

Overview

Package goroutine is merely a hack. Don't use it if you can.

Package goroutine exports goroutine id. Do whatever you want with care.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoroutineId

func GoroutineId() int64

GoroutineId return id of current goroutine. It's guaranteed to be unique globally during app's life time.

Types

This section is empty.

Directories

Path Synopsis
go1_5/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_5_1/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_5_2/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_5_3/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_5_4/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_6/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_6_1/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_6_2/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_6_3/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_7/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_7_1/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_7_2/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_7_3/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_7_4/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_7_5/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_8/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_8_1/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_8_2/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_8_3/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_9/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_9_1/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
go1_9_2/runtime
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.
Package runtime contains operations that interact with Go's runtime system, such as functions to control goroutines.

Jump to

Keyboard shortcuts

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