ADT
aaron-thesis
arm-eh
ast-experimental
cleanup-dtors
deferred_resn
demangler
enum
forall-pointer-decay
jacob/cs343-translation
jenkins-sandbox
new-ast
new-ast-unique-expr
new-env
no_list
persistent-indexer
pthread-emulation
qualifiedEnum
resolv-new
with_gc
Last change
on this file since ef9cc56 was 51f3798, checked in by Thierry Delisle <tdelisle@…>, 9 years ago |
Added raii guard for monitors
|
-
Property mode
set to
100644
|
File size:
637 bytes
|
Rev | Line | |
---|
[3bb51e1] | 1 | #include <fstream>
|
---|
| 2 | #include <kernel>
|
---|
| 3 | #include <monitor>
|
---|
| 4 | #include <threads>
|
---|
| 5 |
|
---|
| 6 | struct global_t {
|
---|
| 7 | int value;
|
---|
| 8 | monitor m;
|
---|
| 9 | };
|
---|
| 10 |
|
---|
| 11 | void ?{}(global_t * this) {
|
---|
| 12 | this->value = 0;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | static global_t global;
|
---|
| 16 |
|
---|
| 17 | void increment( /*mutex*/ global_t * this ) {
|
---|
[51f3798] | 18 | monitor_guard g = { &this->m };
|
---|
[3bb51e1] | 19 | this->value += 1;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | struct MyThread { thread t; };
|
---|
| 23 |
|
---|
| 24 | DECL_THREAD(MyThread);
|
---|
| 25 |
|
---|
| 26 | void ?{}( MyThread * this ) {}
|
---|
| 27 |
|
---|
| 28 | void main( MyThread* this ) {
|
---|
| 29 | for(int i = 0; i < 1000000; i++) {
|
---|
| 30 | increment( &global );
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | int main(int argc, char* argv[]) {
|
---|
| 35 | assert( global.m.entry_queue.tail != NULL );
|
---|
| 36 | processor p;
|
---|
| 37 | {
|
---|
| 38 | scoped(MyThread) f[4];
|
---|
| 39 | }
|
---|
| 40 | sout | global.value | endl;
|
---|
| 41 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.