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 578b637 was
cb0e6de,
checked in by Thierry Delisle <tdelisle@…>, 7 years ago
|
Threads now use monitor semantics to wait until completion
|
-
Property mode set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[2781e65] | 1 | #include <fstream> |
---|
| 2 | #include <kernel> |
---|
| 3 | #include <monitor> |
---|
[4a3334cf] | 4 | #include <thread> |
---|
[2781e65] | 5 | |
---|
| 6 | static int global12, global23, global13; |
---|
| 7 | |
---|
[ef42b143] | 8 | struct monitor_t { |
---|
| 9 | monitor_desc m; |
---|
| 10 | }; |
---|
| 11 | |
---|
| 12 | monitor_desc * get_monitor( monitor_t * this ) { |
---|
| 13 | return &this->m; |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | static monitor_t m1, m2, m3; |
---|
[2781e65] | 17 | |
---|
[ef42b143] | 18 | void increment( monitor_t * mutex p1, monitor_t * mutex p2, int * value ) { |
---|
[2781e65] | 19 | *value += 1; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | struct MyThread { |
---|
[17af7d1] | 23 | thread_desc __thrd; |
---|
[2781e65] | 24 | int target; |
---|
| 25 | }; |
---|
| 26 | |
---|
| 27 | DECL_THREAD(MyThread); |
---|
| 28 | |
---|
| 29 | void ?{}( MyThread * this, int target ) { |
---|
| 30 | this->target = target; |
---|
| 31 | } |
---|
| 32 | |
---|
[cb0e6de] | 33 | void ^?{}( MyThread * mutex this ) {} |
---|
| 34 | |
---|
[2781e65] | 35 | void main( MyThread* this ) { |
---|
| 36 | for(int i = 0; i < 1000000; i++) { |
---|
| 37 | choose(this->target) { |
---|
| 38 | case 0: increment( &m1, &m2, &global12 ); |
---|
| 39 | case 1: increment( &m2, &m3, &global23 ); |
---|
| 40 | case 2: increment( &m1, &m3, &global13 ); |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | int main(int argc, char* argv[]) { |
---|
| 46 | processor p; |
---|
| 47 | { |
---|
| 48 | scoped(MyThread) * f[6]; |
---|
| 49 | for(int i = 0; i < 6; i++) { |
---|
| 50 | f[i] = ((scoped(MyThread) *)malloc()){ i % 3 }; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | for(int i = 0; i < 6; i++) { |
---|
| 54 | delete( f[i] ); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | sout | global12 | global23 | global13 | endl; |
---|
| 58 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.