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