-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththreads10.c
106 lines (87 loc) · 2.55 KB
/
threads10.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ucontext.h>
#include <pthread.h>
#include <signal.h>
void advanced_signal_handler(int signo, siginfo_t *si, void *data)
{
ucontext_t* uc = (ucontext_t *) data;
ucontext_t* t_uc = uc->uc_link; // Second transactional context HTM.
if (t_uc != NULL) {
if (t_uc->uc_mcontext.regs->msr & 0x600000000) {
printf("* Caught a SIGTRAP in transaction: ");
printf("uc->uc_mcontext.regs->msr = %p\n", (void *) uc->uc_mcontext.regs->msr);
return;
}
}
printf("* Caught a SIGTRAP but NOT in transaction: ");
printf("uc->uc_mcontext.regs->msr = %p\n", (void *) uc->uc_mcontext.regs->msr);
uc->uc_mcontext.regs->nip += 4;
}
void* worker_with_just_a_trap(void *sig)
{
pthread_t my_id;
my_id = pthread_self();
while (1 == 1)
{
printf("=> I'm thread %lx. I'll perform JUST A TRAP every 2 seconds\n", my_id);
sleep(2);
asm (
" trap \n\t"
);
}
}
void* worker_with_htm_and_trap(void *sig)
{
pthread_t my_id;
my_id = pthread_self();
while (1 == 1)
{
printf("=> I'm thread %lx. I'll perform A TRAP IN TRANSACTION every 1 second\n", my_id);
sleep(1);
asm (
" tbegin. \n\t" // Begin HTM
" beq 2f \n\t" // HTM abort handler.
" trap \n\t"
" mr 15, 16 \n\t" // Do something (I'll never reach here).
" mr 14, 15 \n\t" // Do something (I'll never reach here).
" tend. \n\t" // End HTM.
"2: \n\t"
: /* no output */
: /* no input */
: "r14", "r15", "r16"
);
}
}
int main(void)
{
pthread_t t0;
pthread_t t1;
sigset_t sigset;
struct sigaction sa;
// Install thread-shared signal handler.
// sa.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = advanced_signal_handler;
sigaction(SIGTRAP, &sa, NULL);
// Create the set containing signals to be unblocked.
sigemptyset(&sigset);
sigaddset(&sigset, SIGTRAP);
// Create thread t0
pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
pthread_create(&t0, NULL, worker_with_just_a_trap, (void*) &sigset);
// Create thread t1
pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
pthread_create(&t1, NULL, worker_with_htm_and_trap, (void*) &sigset);
// SIGTRAP, main thread remains with SIGTRAP blocked.
sigprocmask(SIG_BLOCK, &sigset, NULL);
while (1 == 1)
{
sleep(1); // MT-safe, hum?
}
// I don't expect pc will ever reach here. Thread workers are looping forever.
pthread_join(t0, NULL);
pthread_join(t1, NULL);
exit(0);
}