source: src/tests/concurrent/waitfor/simple.c @ 41770ed1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 41770ed1 was 8ad6533, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

remove cfatime.h, move itimerval constructor to "time", update concurrent examples to use Duration

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[97e3296]1#include <fstream>
2#include <kernel>
3#include <monitor>
4#include <stdlib>
5#include <thread>
6
7#include <time.h>
8
[90c4df0]9static const unsigned long N = 500ul;
[97e3296]10
11#ifndef PREEMPTION_RATE
[8ad6533]12#define PREEMPTION_RATE 10`ms
[97e3296]13#endif
14
[8ad6533]15Duration default_preemption() {
[97e3296]16        return PREEMPTION_RATE;
17}
18
19monitor global_t {};
20
21global_t globalA;
22
23thread Acceptor {};
24thread Acceptee {};
25
[90c4df0]26volatile bool done;
27
[97e3296]28//----------------------------------------------------------------------------------------------------
29// Acceptor
30void do_notify( global_t * mutex a );
31
32void do_wait( global_t * mutex a ) {
[90c4df0]33        sout | "Waiting to accept" | endl;
[6c7b1e7]34        yield( random( 10 ) );
[90c4df0]35
36        sout | "Accepting" | endl;
[97e3296]37
38        __acceptable_t acceptable;
[90c4df0]39        acceptable.func          = (fptr_t)do_notify;
[97e3296]40        acceptable.count         = 1;
41        acceptable.monitors      = &a;
42
[310e5b7]43        __waitfor_internal( 1, &acceptable );
[97e3296]44
[90c4df0]45        sout | "Accepted" | endl;
[6c7b1e7]46        yield( random( 10 ) );
[97e3296]47}
48
49void main( Acceptor* this ) {
[90c4df0]50        for( int i = 0; i < N; i++ ) {
51                do_wait( &globalA );
52                sout | i | endl;
53        }
54
55        done = true;
[97e3296]56}
57
58//----------------------------------------------------------------------------------------------------
59// Acceptee
60void do_notify( global_t * mutex a ) {
[90c4df0]61
[97e3296]62}
63
64void main( Acceptee* this ) {
[90c4df0]65        while( !done ) {
[6c7b1e7]66                yield( random( 10 ) );
[90c4df0]67                do_notify( &globalA );
[6c7b1e7]68                yield( random( 10 ) );
[90c4df0]69        }
[97e3296]70}
71
72//----------------------------------------------------------------------------------------------------
73// Main
74int main(int argc, char* argv[]) {
[90c4df0]75        done = false;
[54aba8d]76        srandom( time( NULL ) );
[90c4df0]77        printf("%p\n", &globalA);
[97e3296]78        sout | "Starting" | endl;
79        {
80                Acceptor r;
[90c4df0]81                Acceptee e[13];
82
[97e3296]83        }
84        sout | "Done" | endl;
[6c7b1e7]85}
Note: See TracBrowser for help on using the repository browser.