This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a1b36c9
Showing
21 changed files
with
1,152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
Copyright for package goroutine. | ||
===================================================================== | ||
Copyright (c) 2015 Huan Du | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
===================================================================== | ||
|
||
Copyright notice for golang source code. | ||
===================================================================== | ||
Copyright (c) 2012 The Go Authors. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
===================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Hacking goroutine # | ||
|
||
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()`. | ||
|
||
```go | ||
// Get id of current goroutine. | ||
var id int64 = goroutine.GoroutineId() | ||
println(id) | ||
``` | ||
|
||
See [godoc](https://godoc.org/github.com/huandu/goroutine) for more details. | ||
|
||
## Caveats ## | ||
|
||
Package goroutine is not well tested due to lack of test machines. | ||
|
||
Tested platforms. | ||
* go1.5.1 + Darwin (Mac OSX 10.11.1) + amd64 CPU | ||
|
||
## License ## | ||
|
||
This package is licensed under MIT license. See LICENSE for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
type uintreg uint32 | ||
type intptr int32 // TODO(rsc): remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
type uintreg uint64 | ||
type intptr int64 // TODO(rsc): remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
type uintreg uint64 | ||
type intptr int32 // TODO(rsc): remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
type uintreg uint32 | ||
type intptr int32 // TODO(rsc): remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
type uintreg uint64 | ||
type intptr int64 // TODO(rsc): remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
type uintreg uint64 | ||
type intptr int64 // TODO(rsc): remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
type uintreg uint64 | ||
type intptr int64 // TODO(rsc): remove |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2015 Huan Du. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package goroutine is merely a hack. Don't use it if you can. | ||
// | ||
// Package goroutine exports goroutine id. Do whatever you want with care. | ||
package goroutine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
#ifdef GOARCH_arm | ||
#define LR R14 | ||
#endif | ||
|
||
#ifdef GOARCH_amd64 | ||
#define get_tls(r) MOVQ TLS, r | ||
#define g(r) 0(r)(TLS*1) | ||
#endif | ||
|
||
#ifdef GOARCH_amd64p32 | ||
#define get_tls(r) MOVL TLS, r | ||
#define g(r) 0(r)(TLS*1) | ||
#endif | ||
|
||
#ifdef GOARCH_386 | ||
#define get_tls(r) MOVL TLS, r | ||
#define g(r) 0(r)(TLS*1) | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2015 Huan Du. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
func getg() *g | ||
|
||
// GoroutineId return id of current goroutine. | ||
// It's guaranteed to be unique globally during app's life time. | ||
func GoroutineId() int64 { | ||
gp := getg() | ||
return gp.goid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2015 Huan Du. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
#include "go_asm.h" | ||
#include "go_tls.h" | ||
#include "textflag.h" | ||
|
||
TEXT ·getg(SB), NOSPLIT, $0-4 | ||
get_tls(CX) | ||
MOVL g(CX), AX | ||
MOVL AX, ret+0(FP) | ||
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2015 Huan Du. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
#include "go_asm.h" | ||
#include "go_tls.h" | ||
#include "textflag.h" | ||
|
||
TEXT ·getg(SB), NOSPLIT, $0-8 | ||
get_tls(CX) | ||
MOVQ g(CX), AX | ||
MOVQ AX, ret+0(FP) | ||
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2015 Huan Du. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
#include "info_amd64.s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2015 Huan Du. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
#include "go_asm.h" | ||
#include "textflag.h" | ||
|
||
TEXT ·getg(SB), NOSPLIT, $0-4 | ||
MOVW g, ret+0(FP) | ||
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2015 Huan Du. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
#include "go_asm.h" | ||
#include "go_tls.h" | ||
#include "textflag.h" | ||
|
||
TEXT ·getg(SB), NOSPLIT, $0-8 | ||
get_tls(R0) | ||
MOVQ R0, ret+0(FP) | ||
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2015 Huan Du. All rights reserved. | ||
// Use of this source code is governed by a MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
"fmt" | ||
"math/rand" | ||
) | ||
|
||
func TestGoroutineIdConsistency(t *testing.T) { | ||
cnt := 10 | ||
exit := make(chan error) | ||
|
||
for i := 0; i < cnt; i++ { | ||
go func(n int) { | ||
id1 := GoroutineId() | ||
time.Sleep(time.Duration(rand.Int63n(100)) * time.Millisecond) | ||
id2 := GoroutineId() | ||
|
||
if id1 != id2 { | ||
exit <- fmt.Errorf("Inconsistent goroutine id. [old:%v] [new:%v]", id1, id2) | ||
return | ||
} | ||
|
||
exit <- nil | ||
}(i) | ||
} | ||
|
||
failed := false | ||
|
||
for i := 0; i < cnt; i++ { | ||
err := <-exit | ||
|
||
if err != nil { | ||
t.Logf("Found error. [err:%v]", err) | ||
failed = true | ||
} | ||
} | ||
|
||
if failed { | ||
t.Fatalf("Test failed.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2009 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package goroutine | ||
|
||
import "unsafe" | ||
|
||
const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const | ||
const regSize = 4 << (^uintreg(0) >> 63) // unsafe.Sizeof(uintreg(0)) but an ideal const | ||
|
||
type mcache struct {} | ||
type typeAlg struct {} | ||
type traceBuf struct {} | ||
|
||
type gcMarkWorkerMode int | ||
|
||
type persistentAlloc struct { | ||
base unsafe.Pointer | ||
off uintptr | ||
} | ||
|
||
// A wbufptr holds a workbuf*, but protects it from write barriers. | ||
// workbufs never live on the heap, so write barriers are unnecessary. | ||
// Write barriers on workbuf pointers may also be dangerous in the GC. | ||
type wbufptr uintptr | ||
|
||
// A gcWork provides the interface to produce and consume work for the | ||
// garbage collector. | ||
// | ||
// A gcWork can be used on the stack as follows: | ||
// | ||
// var gcw gcWork | ||
// disable preemption | ||
// .. call gcw.put() to produce and gcw.get() to consume .. | ||
// gcw.dispose() | ||
// enable preemption | ||
// | ||
// Or from the per-P gcWork cache: | ||
// | ||
// (preemption must be disabled) | ||
// gcw := &getg().m.p.ptr().gcw | ||
// .. call gcw.put() to produce and gcw.get() to consume .. | ||
// if gcphase == _GCmarktermination { | ||
// gcw.dispose() | ||
// } | ||
// | ||
// It's important that any use of gcWork during the mark phase prevent | ||
// the garbage collector from transitioning to mark termination since | ||
// gcWork may locally hold GC work buffers. This can be done by | ||
// disabling preemption (systemstack or acquirem). | ||
type gcWork struct { | ||
// Invariant: wbuf is never full or empty | ||
wbuf wbufptr | ||
|
||
// Bytes marked (blackened) on this gcWork. This is aggregated | ||
// into work.bytesMarked by dispose. | ||
bytesMarked uint64 | ||
|
||
// Scan work performed on this gcWork. This is aggregated into | ||
// gcController by dispose. | ||
scanWork int64 | ||
} | ||
|
||
// Information from the compiler about the layout of stack frames. | ||
type bitvector struct { | ||
n int32 // # of bits | ||
bytedata *uint8 | ||
} | ||
|
||
type slice struct { | ||
array unsafe.Pointer | ||
len int | ||
cap int | ||
} |
Oops, something went wrong.