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

thread First  { semaphore* lock; };
thread Second { semaphore* lock; };

void ?{}( First  & this, semaphore & lock ) { ((thread&)this){"Thread 1"}; this.lock = &lock; }
void ?{}( Second & this, semaphore & lock ) { ((thread&)this){"Thread 2"}; this.lock = &lock; }

void main(First& this) {
	for(int i = 0; i < 10; i++) {
		sout | "First : Suspend No." | i + 1 | endl;
		yield();
	}
	V(*this.lock);
}

void main(Second& this) {
	P(*this.lock);
	for(int i = 0; i < 10; i++) {
		sout | "Second : Suspend No." | i + 1 | endl;
		yield();
	}
}


int main(int argc, char* argv[]) {
	semaphore lock = { 0 };
	sout | "User main begin" | endl;
	{
		processor p;
		{
			First  f = { lock };
			Second s = { lock };
		}
	}
	sout | "User main end" | endl;
}
