//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//
//		DEPRECATED TEST
//
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------

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

static const unsigned long N = 50_000ul;

#ifndef PREEMPTION_RATE
#define PREEMPTION_RATE 10`ms
#endif

Duration default_preemption() {
	return 0;
}
enum state_t { WAIT, SIGNAL, BARGE };

monitor global_t {};

monitor global_data_t {
	volatile 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( random( 10 ), 1);
		c.do_wait1 = random( c.do_signal );
		c.do_wait2 = random( 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 >= N ) 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(); };
}

static thread_desc * volatile the_threads;

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