From 43a123c1ae6613b3efeed291fa552ecd909d3acf Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 16 Apr 2024 21:23:18 +0200 Subject: Adding upstream version 1.20.14. Signed-off-by: Daniel Baumann --- test/deferfin.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 test/deferfin.go (limited to 'test/deferfin.go') diff --git a/test/deferfin.go b/test/deferfin.go new file mode 100644 index 0000000..1312bbb --- /dev/null +++ b/test/deferfin.go @@ -0,0 +1,58 @@ +// run + +// Copyright 2013 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. + +// Test that defers do not prevent garbage collection. + +package main + +import ( + "runtime" + "sync" + "sync/atomic" + "time" +) + +var sink func() + +func main() { + // Does not work with gccgo, due to partially conservative GC. + // Try to enable when we have fully precise GC. + if runtime.Compiler == "gccgo" { + return + } + N := 10 + count := int32(N) + var wg sync.WaitGroup + wg.Add(N) + for i := 0; i < N; i++ { + go func() { + defer wg.Done() + v := new(string) + f := func() { + if *v != "" { + panic("oops") + } + } + if *v != "" { + // let the compiler think f escapes + sink = f + } + runtime.SetFinalizer(v, func(p *string) { + atomic.AddInt32(&count, -1) + }) + defer f() + }() + } + wg.Wait() + for i := 0; i < 3; i++ { + time.Sleep(10 * time.Millisecond) + runtime.GC() + } + if count != 0 { + println(count, "out of", N, "finalizer are not called") + panic("not all finalizers are called") + } +} -- cgit v1.2.3