ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change
on this file since e958ff8 was
2781e65,
checked in by Thierry Delisle <tdelisle@…>, 8 years ago
|
added support for acquiring multiple monitors at once
|
-
Property mode set to
100644
|
File size:
973 bytes
|
Line | |
---|
1 | #include <fstream> |
---|
2 | #include <kernel> |
---|
3 | #include <monitor> |
---|
4 | #include <threads> |
---|
5 | |
---|
6 | static int global12, global23, global13; |
---|
7 | |
---|
8 | static __monitor_t m1, m2, m3; |
---|
9 | |
---|
10 | void increment( /*mutex*/ __monitor_t * p1, /*mutex*/ __monitor_t * p2, int * value ) { |
---|
11 | __monitor_t * mons[] = { p1, p2 }; |
---|
12 | monitor_guard_t g = { mons, 2 }; |
---|
13 | *value += 1; |
---|
14 | } |
---|
15 | |
---|
16 | struct MyThread { |
---|
17 | thread t; |
---|
18 | int target; |
---|
19 | }; |
---|
20 | |
---|
21 | DECL_THREAD(MyThread); |
---|
22 | |
---|
23 | void ?{}( MyThread * this, int target ) { |
---|
24 | this->target = target; |
---|
25 | } |
---|
26 | |
---|
27 | void main( MyThread* this ) { |
---|
28 | for(int i = 0; i < 1000000; i++) { |
---|
29 | choose(this->target) { |
---|
30 | case 0: increment( &m1, &m2, &global12 ); |
---|
31 | case 1: increment( &m2, &m3, &global23 ); |
---|
32 | case 2: increment( &m1, &m3, &global13 ); |
---|
33 | } |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | int main(int argc, char* argv[]) { |
---|
38 | processor p; |
---|
39 | { |
---|
40 | scoped(MyThread) * f[6]; |
---|
41 | for(int i = 0; i < 6; i++) { |
---|
42 | f[i] = ((scoped(MyThread) *)malloc()){ i % 3 }; |
---|
43 | } |
---|
44 | |
---|
45 | for(int i = 0; i < 6; i++) { |
---|
46 | delete( f[i] ); |
---|
47 | } |
---|
48 | } |
---|
49 | sout | global12 | global23 | global13 | endl; |
---|
50 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.