headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / system / libroot / posix / signal_in_allocator_test.cpp
blob35aa52d37534572f68f8b8d84c334445a12263f8
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 free(malloc(10));
39 sHandledSignals++;
43 int
44 main()
46 const int testSeconds = 2;
47 printf("Trying to provoke allocator deadlock in signal handler.\n");
48 printf("If successful test will finish in %d seconds...\n", testSeconds);
50 // install signal handler
51 if (signal(SIGUSR1, signal_handler) == SIG_ERR) {
52 fprintf(stderr, "Error: Failed to install signal handler: %s\n",
53 strerror(errno));
54 exit(1);
57 // start signal thread
58 thread_id signalThread = spawn_thread(&signal_pusher, "signal pusher",
59 B_NORMAL_PRIORITY, NULL);
60 resume_thread(signalThread);
62 bigtime_t endTime = system_time() + 1000000 * (bigtime_t)testSeconds;
63 while (system_time() < endTime) {
64 const int allocationCount = 1000;
65 void* allocations[allocationCount];
66 for (int i = 0; i < allocationCount; i++)
67 allocations[i] = malloc(rand() % 50);
68 for (int i = 0; i < allocationCount; i++)
69 free(allocations[i]);
72 kill_thread(signalThread);
73 snooze(1000);
75 printf("test successful, handled %" B_PRId64 " signals\n", sHandledSignals);
77 return 0;