| 1 |
// Copyright 2025 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"os" |
| 9 |
"runtime" |
| 10 |
"runtime/pprof" |
| 11 |
"sync" |
| 12 |
) |
| 13 |
|
| 14 |
func init() { |
| 15 |
register("Syncthing5795", Syncthing5795) |
| 16 |
} |
| 17 |
|
| 18 |
type message_syncthing5795 interface{} |
| 19 |
|
| 20 |
type ClusterConfig_syncthing5795 struct{} |
| 21 |
|
| 22 |
type Model_syncthing5795 interface { |
| 23 |
ClusterConfig(message_syncthing5795) |
| 24 |
} |
| 25 |
|
| 26 |
type TestModel_syncthing5795 struct { |
| 27 |
ccFn func() |
| 28 |
} |
| 29 |
|
| 30 |
func (t *TestModel_syncthing5795) ClusterConfig(msg message_syncthing5795) { |
| 31 |
if t.ccFn != nil { |
| 32 |
t.ccFn() |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
type Connection_syncthing5795 interface { |
| 37 |
Start() |
| 38 |
Close() |
| 39 |
} |
| 40 |
|
| 41 |
type rawConnection_syncthing5795 struct { |
| 42 |
receiver Model_syncthing5795 |
| 43 |
|
| 44 |
inbox chan message_syncthing5795 |
| 45 |
dispatcherLoopStopped chan struct{} |
| 46 |
closed chan struct{} |
| 47 |
closeOnce sync.Once |
| 48 |
} |
| 49 |
|
| 50 |
func (c *rawConnection_syncthing5795) Start() { |
| 51 |
go c.dispatcherLoop() // G2 |
| 52 |
} |
| 53 |
|
| 54 |
func (c *rawConnection_syncthing5795) dispatcherLoop() { |
| 55 |
defer close(c.dispatcherLoopStopped) |
| 56 |
var msg message_syncthing5795 |
| 57 |
for { |
| 58 |
select { |
| 59 |
case msg = <-c.inbox: |
| 60 |
case <-c.closed: |
| 61 |
return |
| 62 |
} |
| 63 |
switch msg := msg.(type) { |
| 64 |
case *ClusterConfig_syncthing5795: |
| 65 |
c.receiver.ClusterConfig(msg) |
| 66 |
default: |
| 67 |
return |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func (c *rawConnection_syncthing5795) Close() { |
| 73 |
c.closeOnce.Do(func() { |
| 74 |
close(c.closed) |
| 75 |
<-c.dispatcherLoopStopped |
| 76 |
}) |
| 77 |
} |
| 78 |
|
| 79 |
func Syncthing5795() { |
| 80 |
prof := pprof.Lookup("goroutineleak") |
| 81 |
defer func() { |
| 82 |
// Yield several times to allow the child goroutine to run. |
| 83 |
for i := 0; i < yieldCount; i++ { |
| 84 |
runtime.Gosched() |
| 85 |
} |
| 86 |
prof.WriteTo(os.Stdout, 2) |
| 87 |
}() |
| 88 |
go func() { // G1 |
| 89 |
m := &TestModel_syncthing5795{} |
| 90 |
c := &rawConnection_syncthing5795{ |
| 91 |
dispatcherLoopStopped: make(chan struct{}), |
| 92 |
closed: make(chan struct{}), |
| 93 |
inbox: make(chan message_syncthing5795), |
| 94 |
receiver: m, |
| 95 |
} |
| 96 |
m.ccFn = c.Close |
| 97 |
|
| 98 |
c.Start() |
| 99 |
c.inbox <- &ClusterConfig_syncthing5795{} |
| 100 |
|
| 101 |
<-c.dispatcherLoopStopped |
| 102 |
}() |
| 103 |
} |