xref: /haiku/src/tests/system/libroot/posix/signal_in_allocator_test2.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 /*
2  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <signal.h>
11 #include <string.h>
12 
13 #include <OS.h>
14 
15 
16 static int64 sHandledSignals = 0;
17 
18 
19 static status_t
20 signal_pusher(void* data)
21 {
22 	team_info teamInfo;
23 	get_team_info(B_CURRENT_TEAM, &teamInfo);
24 	thread_id mainThread = teamInfo.team;
25 
26 	while (true) {
27 		send_signal(mainThread, SIGUSR1);
28 		snooze(1000);
29 	}
30 
31 	return B_OK;
32 }
33 
34 
35 static void
36 signal_handler(int signal)
37 {
38 	sHandledSignals++;
39 }
40 
41 
42 static void
43 allocator_thread(int level)
44 {
45 	if (level > 100000)
46 		return;
47 
48 	free(malloc(rand() % 10000));
49 
50 	allocator_thread(level + 1);
51 }
52 
53 
54 int
55 main()
56 {
57 	// Test program to reproduce bug #2562. Is finished quickly and must be run
58 	// in a loop to reproduce the bug.
59 
60 	// install signal handler
61 	if (signal(SIGUSR1, signal_handler) == SIG_ERR) {
62 		fprintf(stderr, "Error: Failed to install signal handler: %s\n",
63 			strerror(errno));
64 		exit(1);
65 	}
66 
67 	// start signal thread
68 	thread_id signalThread = spawn_thread(&signal_pusher, "signal pusher",
69 		B_NORMAL_PRIORITY, NULL);
70 	resume_thread(signalThread);
71 
72 	allocator_thread(0);
73 
74 	kill_thread(signalThread);
75 	snooze(1000);
76 
77 	printf("test successful, handled %lld signals\n", sHandledSignals);
78 
79 	return 0;
80 }
81