forked from sstsimulator/sst-macro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pthread.cc
111 lines (87 loc) · 2.53 KB
/
test_pthread.cc
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 <sprockit/test/test.h>
#include <sstmac/software/process/app.h>
#include <sstmac/software/process/operating_system.h>
#include <sstmac/software/process/thread.h>
#include <sstmac/software/libraries/compute/sstmac_compute.h>
#include <sstmac/libraries/pthread/sstmac_pthread.h>
using namespace sstmac;
using namespace sstmac::sw;
using namespace sstmac::hw;
sstmac_register_app(test_pthread);
extern "C" int ubuntu_cant_name_mangle() { return 0; }
void* ptest(void* args)
{
SSTMAC_compute(1);
printf("Yes, I reach here!\n");
return 0;
}
void* ptest3(void* args)
{
SSTMAC_compute(1);
printf("No, I do not reach here!\n");
return 0;
}
struct pthread_args
{
pthread_cond_t cond;
pthread_mutex_t mutex;
};
void* ptest2(void* args)
{
int status;
pthread_args* pargs = (pthread_args*) args;
SSTMAC_compute(0.001);
status = pthread_mutex_lock(&pargs->mutex);
if (status != 0){
spkt_throw(sprockit::illformed_error,
"mutex failed lock");
}
std::cout << "Mutex locked" << std::endl;
SSTMAC_compute(0.001);
std::cout << "Mutex unlocked" << std::endl;
pthread_mutex_unlock(&pargs->mutex);
status = pthread_mutex_lock(&pargs->mutex);
if (status != 0){
spkt_throw(sprockit::illformed_error,
"mutex failed lock");
}
std::cout << "Condition locked" << std::endl;
SSTMAC_compute(0.001);
status = pthread_cond_wait(&pargs->cond, &pargs->mutex);
if (status != 0){
spkt_throw(sprockit::illformed_error,
"thread failed wait");
}
std::cout << "Done waiting" << std::endl;
SSTMAC_compute(0.001);
return 0;
}
int
test_pthread_main(int argc, char** argv)
{
void* no_args = 0;
pthread_t thr1, thr2;
pthread_attr_t attr1, attr2;
int status;
status = pthread_create(&thr1, &attr1, &ptest, no_args);
status = pthread_create(&thr2, &attr2, &ptest, no_args);
void* ret;
pthread_join(thr1, &ret);
pthread_join(thr2, &ret);
pthread_args pargs;
pthread_mutex_init(&pargs.mutex,0);
pthread_cond_init(&pargs.cond,0);
status = pthread_create(&thr1, &attr1, &ptest2, &pargs);
status = pthread_create(&thr2, &attr2, &ptest2, &pargs);
std::cout << "Spawned threads" << std::endl;
SSTMAC_compute(1);
std::cout << "First signal" << std::endl;
pthread_cond_signal(&pargs.cond);
std::cout << "Second signal" << std::endl;
pthread_cond_signal(&pargs.cond);
pthread_join(thr1, &ret);
pthread_join(thr2, &ret);
//spin off another pthread
status = pthread_create(&thr1, &attr1, &ptest, &pargs);
return 0;
}