github.com/aloncn/graphics-go@v0.0.1/src/runtime/testdata/testprog/deadlock.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "runtime" 10 "runtime/debug" 11 "time" 12 ) 13 14 func init() { 15 registerInit("InitDeadlock", InitDeadlock) 16 registerInit("NoHelperGoroutines", NoHelperGoroutines) 17 18 register("SimpleDeadlock", SimpleDeadlock) 19 register("LockedDeadlock", LockedDeadlock) 20 register("LockedDeadlock2", LockedDeadlock2) 21 register("GoexitDeadlock", GoexitDeadlock) 22 register("StackOverflow", StackOverflow) 23 register("ThreadExhaustion", ThreadExhaustion) 24 register("RecursivePanic", RecursivePanic) 25 register("GoexitExit", GoexitExit) 26 register("GoNil", GoNil) 27 register("MainGoroutineID", MainGoroutineID) 28 register("Breakpoint", Breakpoint) 29 register("GoexitInPanic", GoexitInPanic) 30 register("PanicAfterGoexit", PanicAfterGoexit) 31 register("RecoveredPanicAfterGoexit", RecoveredPanicAfterGoexit) 32 register("PanicTraceback", PanicTraceback) 33 } 34 35 func SimpleDeadlock() { 36 select {} 37 panic("not reached") 38 } 39 40 func InitDeadlock() { 41 select {} 42 panic("not reached") 43 } 44 45 func LockedDeadlock() { 46 runtime.LockOSThread() 47 select {} 48 } 49 50 func LockedDeadlock2() { 51 go func() { 52 runtime.LockOSThread() 53 select {} 54 }() 55 time.Sleep(time.Millisecond) 56 select {} 57 } 58 59 func GoexitDeadlock() { 60 F := func() { 61 for i := 0; i < 10; i++ { 62 } 63 } 64 65 go F() 66 go F() 67 runtime.Goexit() 68 } 69 70 func StackOverflow() { 71 var f func() byte 72 f = func() byte { 73 var buf [64 << 10]byte 74 return buf[0] + f() 75 } 76 debug.SetMaxStack(1474560) 77 f() 78 } 79 80 func ThreadExhaustion() { 81 debug.SetMaxThreads(10) 82 c := make(chan int) 83 for i := 0; i < 100; i++ { 84 go func() { 85 runtime.LockOSThread() 86 c <- 0 87 select {} 88 }() 89 <-c 90 } 91 } 92 93 func RecursivePanic() { 94 func() { 95 defer func() { 96 fmt.Println(recover()) 97 }() 98 var x [8192]byte 99 func(x [8192]byte) { 100 defer func() { 101 if err := recover(); err != nil { 102 panic("wrap: " + err.(string)) 103 } 104 }() 105 panic("bad") 106 }(x) 107 }() 108 panic("again") 109 } 110 111 func GoexitExit() { 112 go func() { 113 time.Sleep(time.Millisecond) 114 }() 115 i := 0 116 runtime.SetFinalizer(&i, func(p *int) {}) 117 runtime.GC() 118 runtime.Goexit() 119 } 120 121 func GoNil() { 122 defer func() { 123 recover() 124 }() 125 var f func() 126 go f() 127 select {} 128 } 129 130 func MainGoroutineID() { 131 panic("test") 132 } 133 134 func NoHelperGoroutines() { 135 i := 0 136 runtime.SetFinalizer(&i, func(p *int) {}) 137 time.AfterFunc(time.Hour, func() {}) 138 panic("oops") 139 } 140 141 func Breakpoint() { 142 runtime.Breakpoint() 143 } 144 145 func GoexitInPanic() { 146 go func() { 147 defer func() { 148 runtime.Goexit() 149 }() 150 panic("hello") 151 }() 152 runtime.Goexit() 153 } 154 155 func PanicAfterGoexit() { 156 defer func() { 157 panic("hello") 158 }() 159 runtime.Goexit() 160 } 161 162 func RecoveredPanicAfterGoexit() { 163 defer func() { 164 defer func() { 165 r := recover() 166 if r == nil { 167 panic("bad recover") 168 } 169 }() 170 panic("hello") 171 }() 172 runtime.Goexit() 173 } 174 175 func PanicTraceback() { 176 pt1() 177 } 178 179 func pt1() { 180 defer func() { 181 panic("panic pt1") 182 }() 183 pt2() 184 } 185 186 func pt2() { 187 defer func() { 188 panic("panic pt2") 189 }() 190 panic("hello") 191 }