File: bug228343.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (60 lines) | stat: -rw-r--r-- 1,339 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// https://bugs.kde.org/show_bug.cgi?id=228343

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>
#include <libkern/OSAtomic.h>
#include <pthread.h>

OSSpinLock sl = OS_SPINLOCK_INIT;
typedef void *(*worker_t)(void*);
typedef void (*Sigaction)(int, siginfo_t *, void *);

int GLOB=0;

static void EnableSigprof(Sigaction SignalHandler) {
 struct sigaction sa;
 sa.sa_sigaction = SignalHandler;
 sa.sa_flags = SA_RESTART | SA_SIGINFO;
 sigemptyset(&sa.sa_mask);
 if (sigaction(SIGPROF, &sa, NULL) != 0) {
   perror("sigaction");
   abort();
 }
 struct itimerval timer;
 timer.it_interval.tv_sec = 0;
 timer.it_interval.tv_usec = 1000000 / 10000;
 timer.it_value = timer.it_interval;
 if (setitimer(ITIMER_PROF, &timer, 0) != 0) {
   perror("setitimer");
   abort();
 }
}

void *Worker() {
 long int i;
 for (i = 0; i < 100000000; i++) {
   void *x = malloc((i % 64) + 1);
   free (x);
 }
 return NULL;
}

void SignalHandlerWithSpinlock(int sig, siginfo_t *siginfo, void *context) {
 OSSpinLockLock(&sl);
 GLOB++;
 OSSpinLockUnlock(&sl);
}

int main() {
 EnableSigprof(SignalHandlerWithSpinlock);
 pthread_t w_1;
 pthread_t w_2;
 pthread_create(&w_1, NULL, Worker, NULL);
 pthread_create(&w_2, NULL, Worker, NULL);
 pthread_join(w_1, NULL);
 pthread_join(w_2, NULL);
 printf("\tGLOB=%d\n", GLOB);
 return 0;
}