#include <fstream>
#include <kernel>
#include <monitor>
#include <stdlib>
#include <thread>

enum state_t { WAIT, SIGNAL, BARGE };

monitor global_t {};

monitor global_data_t {
	bool done;
	int counter;
	state_t state;

	unsigned short do_signal;
	unsigned short do_wait2;
	unsigned short do_wait1;
};

void ?{} ( global_data_t * this ) {
	this->done = false;
	this->counter = 0;
	this->state = BARGE;

	this->do_signal = 6;
	this->do_wait1  = 1;
	this->do_wait2  = 3;
}

void ^?{} ( global_data_t * this ) {}

global_t globalA;
global_t globalB;
global_data_t globalC;

condition cond;

thread Threads {};

bool logicC( global_t * mutex a, global_t * mutex b, global_data_t * mutex c ) {
	c->counter++;

	if( (c->counter % 1000) == 0 ) sout | c->counter | endl;

	int action = c->counter % 10;

	if( action == 0 ) {
		c->do_signal = max( ((unsigned)rand48()) % 10, 1);
		c->do_wait1 = ((unsigned)rand48()) % (c->do_signal);
		c->do_wait2 = ((unsigned)rand48()) % (c->do_signal);

		// if(c->do_wait1 == c->do_wait2) sout | "Same" | endl;
	}

	if( action == c->do_wait1 || action == c->do_wait2 ) {
		c->state = WAIT;
		wait( &cond );

		if(c->state != SIGNAL) {
			sout | "ERROR Barging detected" | c->counter | endl;
			abort();
		}
	}
	else if( action == c->do_signal ) {
		c->state = SIGNAL;

		signal( &cond );
		signal( &cond );
	}
	else {
		c->state = BARGE;
	}

	if( c->counter >= 100_000 ) c->done = true;
	return !c->done;
}

bool logicB( global_t * mutex a, global_t * mutex b ) {
	return logicC(a, b, &globalC);
}

bool logicA( global_t * mutex a ) {
	return logicB(a, &globalB);
}

void main( Threads* this ) {
	while( logicA(&globalA) ) { yield(); };
}

int main(int argc, char* argv[]) {
	rand48seed(0);
	processor p;
	{
		Threads t[17];
	}
}