#include #include #include #include #include #include static const unsigned long N = 500ul; #ifndef PREEMPTION_RATE #define PREEMPTION_RATE 10_000ul #endif unsigned int default_preemption() { return PREEMPTION_RATE; } monitor global_t {}; global_t globalA; thread Acceptor {}; thread Acceptee {}; volatile bool done; unsigned rand10() { return (unsigned)rand48() % 10; } //---------------------------------------------------------------------------------------------------- // Acceptor void do_notify( global_t * mutex a ); void do_wait( global_t * mutex a ) { sout | "Waiting to accept" | endl; yield( rand10() ); sout | "Accepting" | endl; __acceptable_t acceptable; acceptable.func = (fptr_t)do_notify; acceptable.count = 1; acceptable.monitors = &a; __waitfor_internal( 1, &acceptable ); sout | "Accepted" | endl; yield( rand10() ); } void main( Acceptor* this ) { for( int i = 0; i < N; i++ ) { do_wait( &globalA ); sout | i | endl; } done = true; } //---------------------------------------------------------------------------------------------------- // Acceptee void do_notify( global_t * mutex a ) { } void main( Acceptee* this ) { while( !done ) { yield( rand10() ); do_notify( &globalA ); yield( rand10() ); } } //---------------------------------------------------------------------------------------------------- // Main int main(int argc, char* argv[]) { done = false; rand48seed( time( NULL ) ); printf("%p\n", &globalA); sout | "Starting" | endl; { Acceptor r; Acceptee e[13]; } sout | "Done" | endl; }