repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / tests / system / libroot / posix / signal_in_allocator_test2.cpp
blobe2699552058f19670e4b74c3e2019e0c4aa45f0e
1 /*
2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <signal.h>
11 #include <string.h>
13 #include <OS.h>
16 static int64 sHandledSignals = 0;
19 static status_t
20 signal_pusher(void* data)
22 team_info teamInfo;
23 get_team_info(B_CURRENT_TEAM, &teamInfo);
24 thread_id mainThread = teamInfo.team;
26 while (true) {
27 send_signal(mainThread, SIGUSR1);
28 snooze(1000);
31 return B_OK;
35 static void
36 signal_handler(int signal)
38 sHandledSignals++;
42 static void
43 allocator_thread(int level)
45 if (level > 100000)
46 return;
48 free(malloc(rand() % 10000));
50 allocator_thread(level + 1);
54 int
55 main()
57 // Test program to reproduce bug #2562. Is finished quickly and must be run
58 // in a loop to reproduce the bug.
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);
67 // start signal thread
68 thread_id signalThread = spawn_thread(&signal_pusher, "signal pusher",
69 B_NORMAL_PRIORITY, NULL);
70 resume_thread(signalThread);
72 allocator_thread(0);
74 kill_thread(signalThread);
75 snooze(1000);
77 printf("test successful, handled %lld signals\n", sHandledSignals);
79 return 0;