aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/context
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2018-09-24 21:46:21 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2018-09-24 21:46:21 +0000
commitdd931d9b48647e898dc80927c532ae93cc09e192 (patch)
tree71be2295cd79b8a182f6130611658db8628772d5 /libgo/go/context
parent779d8a5ad09b01428726ea5a0e6c87bd9ac3c0e4 (diff)
downloadgcc-dd931d9b48647e898dc80927c532ae93cc09e192.zip
gcc-dd931d9b48647e898dc80927c532ae93cc09e192.tar.gz
gcc-dd931d9b48647e898dc80927c532ae93cc09e192.tar.bz2
libgo: update to Go 1.11
Reviewed-on: https://go-review.googlesource.com/136435 gotools/: * Makefile.am (mostlyclean-local): Run chmod on check-go-dir to make sure it is writable. (check-go-tools): Likewise. (check-vet): Copy internal/objabi to check-vet-dir. * Makefile.in: Rebuild. From-SVN: r264546
Diffstat (limited to 'libgo/go/context')
-rw-r--r--libgo/go/context/benchmark_test.go42
-rw-r--r--libgo/go/context/context.go5
-rw-r--r--libgo/go/context/example_test.go2
3 files changed, 47 insertions, 2 deletions
diff --git a/libgo/go/context/benchmark_test.go b/libgo/go/context/benchmark_test.go
index 3c526dd..5d56863 100644
--- a/libgo/go/context/benchmark_test.go
+++ b/libgo/go/context/benchmark_test.go
@@ -13,6 +13,30 @@ import (
"time"
)
+func BenchmarkCommonParentCancel(b *testing.B) {
+ root := WithValue(Background(), "key", "value")
+ shared, sharedcancel := WithCancel(root)
+ defer sharedcancel()
+
+ b.ResetTimer()
+ b.RunParallel(func(pb *testing.PB) {
+ x := 0
+ for pb.Next() {
+ ctx, cancel := WithCancel(shared)
+ if ctx.Value("key").(string) != "value" {
+ b.Fatal("should not be reached")
+ }
+ for i := 0; i < 100; i++ {
+ x /= x + 1
+ }
+ cancel()
+ for i := 0; i < 100; i++ {
+ x /= x + 1
+ }
+ }
+ })
+}
+
func BenchmarkWithTimeout(b *testing.B) {
for concurrency := 40; concurrency <= 4e5; concurrency *= 100 {
name := fmt.Sprintf("concurrency=%d", concurrency)
@@ -96,3 +120,21 @@ func buildContextTree(root Context, depth int) {
root, _ = WithCancel(root)
}
}
+
+func BenchmarkCheckCanceled(b *testing.B) {
+ ctx, cancel := WithCancel(Background())
+ cancel()
+ b.Run("Err", func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ctx.Err()
+ }
+ })
+ b.Run("Done", func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ select {
+ case <-ctx.Done():
+ default:
+ }
+ }
+ })
+}
diff --git a/libgo/go/context/context.go b/libgo/go/context/context.go
index 06580e0..1b4fa41 100644
--- a/libgo/go/context/context.go
+++ b/libgo/go/context/context.go
@@ -334,8 +334,9 @@ func (c *cancelCtx) Done() <-chan struct{} {
func (c *cancelCtx) Err() error {
c.mu.Lock()
- defer c.mu.Unlock()
- return c.err
+ err := c.err
+ c.mu.Unlock()
+ return err
}
func (c *cancelCtx) String() string {
diff --git a/libgo/go/context/example_test.go b/libgo/go/context/example_test.go
index b2c2aa9..2b28b57 100644
--- a/libgo/go/context/example_test.go
+++ b/libgo/go/context/example_test.go
@@ -93,6 +93,8 @@ func ExampleWithTimeout() {
// context deadline exceeded
}
+// This example demonstrates how a value can be passed to the context
+// and also how to retrieve it if it exists.
func ExampleWithValue() {
type favContextKey string