1 /* See also https://bugs.kde.org/show_bug.cgi?id=432381. */
12 #include <sys/timerfd.h>
17 #define STACKSIZE (PTHREAD_STACK_MIN + 4096)
19 typedef struct thread_local
{
24 static void f(void *data
, int n
)
26 enum { NR_SWITCHES
= 200000 };
27 thread_local_t
*tlocal
= data
;
30 struct timespec delay
= { .tv_nsec
= 1000 };
31 nanosleep(&delay
, NULL
);
32 if (++tlocal
->nrsw
== NR_SWITCHES
)
34 swapcontext(&tlocal
->uc
[n
], &tlocal
->uc
[3 - n
]);
38 void *worker(void *data
)
40 thread_local_t
*tlocal
= data
;
42 if (getcontext(&(tlocal
->uc
[1])) < 0)
44 if (getcontext(&(tlocal
->uc
[2])) < 0)
47 tlocal
->uc
[1].uc_link
= &tlocal
->uc
[0];
48 tlocal
->uc
[1].uc_stack
.ss_sp
= malloc(STACKSIZE
);
49 tlocal
->uc
[1].uc_stack
.ss_size
= STACKSIZE
;
50 makecontext(&tlocal
->uc
[1], (void (*)(void))f
, 2, tlocal
, 1);
51 (void)VALGRIND_STACK_REGISTER(tlocal
->uc
[1].uc_stack
.ss_sp
,
52 tlocal
->uc
[1].uc_stack
.ss_sp
+
53 tlocal
->uc
[1].uc_stack
.ss_size
);
55 tlocal
->uc
[2].uc_link
= &tlocal
->uc
[0];
56 tlocal
->uc
[2].uc_stack
.ss_sp
= malloc(STACKSIZE
);
57 tlocal
->uc
[2].uc_stack
.ss_size
= STACKSIZE
;
58 makecontext(&tlocal
->uc
[2], (void (*)(void))f
, 2, tlocal
, 2);
59 (void)VALGRIND_STACK_REGISTER(tlocal
->uc
[2].uc_stack
.ss_sp
,
60 tlocal
->uc
[2].uc_stack
.ss_sp
+
61 tlocal
->uc
[2].uc_stack
.ss_size
);
63 swapcontext(&tlocal
->uc
[0], &tlocal
->uc
[1]);
67 int main(int argc
, char *argv
[])
70 thread_local_t tlocal
[NR
];
75 memset(tlocal
, 0, sizeof(tlocal
));
77 pthread_attr_init(&attr
);
78 res
= pthread_attr_setstacksize(&attr
, STACKSIZE
);
81 for (i
= 0; i
< NR
; i
++)
82 pthread_create(&thread
[i
], &attr
, worker
, &tlocal
[i
]);
84 pthread_attr_destroy(&attr
);
86 // Wait until the threads have been created.
88 for (i
= 0; i
< NR
; i
++)
89 pthread_kill(thread
[i
], SIGALRM
);
91 for (i
= 0; i
< NR
; i
++)
92 pthread_join(thread
[i
], NULL
);