1// https://bugs.kde.org/show_bug.cgi?id=228343 2 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> 9 10OSSpinLock sl = OS_SPINLOCK_INIT; 11typedef void *(*worker_t)(void*); 12typedef void (*Sigaction)(int, siginfo_t *, void *); 13 14int GLOB=0; 15 16static 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(); 24 } 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(); 32 } 33} 34 35void *Worker() { 36 long int i; 37 for (i = 0; i < 100000000; i++) { 38 void *x = malloc((i % 64) + 1); 39 free (x); 40 } 41} 42 43void SignalHandlerWithSpinlock(int sig, siginfo_t *siginfo, void *context) { 44 OSSpinLockLock(&sl); 45 GLOB++; 46 OSSpinLockUnlock(&sl); 47} 48 49int main() { 50 EnableSigprof(SignalHandlerWithSpinlock); 51 pthread_t w_1; 52 pthread_t w_2; 53 pthread_create(&w_1, NULL, Worker, NULL); 54 pthread_create(&w_2, NULL, Worker, NULL); 55 pthread_join(w_1, NULL); 56 pthread_join(w_2, NULL); 57 printf("\tGLOB=%d\n", GLOB); 58 return 0; 59} 60