1 // Copyright 2020 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.
5 // +build !plan9,!windows
9 // This is for issue #42207.
10 // During a call to needm we could get a SIGCHLD signal
11 // which would itself call needm, causing a deadlock.
19 extern void GoNeedM();
23 static void* needmSignalThread(void* p) {
24 pthread_t* pt = (pthread_t*)(p);
27 for (i = 0; i < 100; i++) {
28 if (pthread_kill(*pt, SIGCHLD) < 0) {
36 // We don't need many calls, as the deadlock is only likely
37 // to occur the first couple of times that needm is called.
38 // After that there will likely be an extra M available.
41 static void* needmCallbackThread(void* p) {
44 for (i = 0; i < SIGNALERS; i++) {
45 sched_yield(); // Help the signal threads get started.
47 for (i = 0; i < CALLS; i++) {
53 static void runNeedmSignalThread() {
56 pthread_t s[SIGNALERS];
58 pthread_create(&caller, NULL, needmCallbackThread, NULL);
59 for (i = 0; i < SIGNALERS; i++) {
60 pthread_create(&s[i], NULL, needmSignalThread, &caller);
62 for (i = 0; i < SIGNALERS; i++) {
63 pthread_join(s[i], NULL);
65 pthread_join(caller, NULL);
77 register("NeedmDeadlock", NeedmDeadlock
)
84 func NeedmDeadlock() {
85 // The failure symptom is that the program hangs because of a
86 // deadlock in needm, so set an alarm.
88 time
.Sleep(5 * time
.Second
)
89 fmt
.Println("Hung for 5 seconds")
93 C
.runNeedmSignalThread()