Bug 497723 - forgot to restore callgrind output cleanup
[valgrind.git] / none / tests / darwin / bug228343.c
blobf25f99e2243354391ef6117399720321412b3fd0
1 // https://bugs.kde.org/show_bug.cgi?id=228343
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/time.h>
6 #include <signal.h>
7 #include <libkern/OSAtomic.h>
8 #include <pthread.h>
10 OSSpinLock sl = OS_SPINLOCK_INIT;
11 typedef void *(*worker_t)(void*);
12 typedef void (*Sigaction)(int, siginfo_t *, void *);
14 int GLOB=0;
16 static void EnableSigprof(Sigaction SignalHandler) {
17 struct sigaction sa;
18 sa.sa_sigaction = SignalHandler;
19 sa.sa_flags = SA_RESTART | SA_SIGINFO;
20 sigemptyset(&sa.sa_mask);
21 if (sigaction(SIGPROF, &sa, NULL) != 0) {
22 perror("sigaction");
23 abort();
25 struct itimerval timer;
26 timer.it_interval.tv_sec = 0;
27 timer.it_interval.tv_usec = 1000000 / 10000;
28 timer.it_value = timer.it_interval;
29 if (setitimer(ITIMER_PROF, &timer, 0) != 0) {
30 perror("setitimer");
31 abort();
35 void *Worker() {
36 long int i;
37 for (i = 0; i < 100000000; i++) {
38 void *x = malloc((i % 64) + 1);
39 free (x);
41 return NULL;
44 void SignalHandlerWithSpinlock(int sig, siginfo_t *siginfo, void *context) {
45 OSSpinLockLock(&sl);
46 GLOB++;
47 OSSpinLockUnlock(&sl);
50 int main() {
51 EnableSigprof(SignalHandlerWithSpinlock);
52 pthread_t w_1;
53 pthread_t w_2;
54 pthread_create(&w_1, NULL, Worker, NULL);
55 pthread_create(&w_2, NULL, Worker, NULL);
56 pthread_join(w_1, NULL);
57 pthread_join(w_2, NULL);
58 printf("\tGLOB=%d\n", GLOB);
59 return 0;