Page 1 of 1

Code to wait for all CPUs

Posted: Mon Sep 29, 2014 3:39 am
by tsdnz
Hi all, I need code to sync all my cores.
This was great fun, I had a couple of attempts before I found a solution.
I was wondering if anyone else has another solution?

Code: Select all

FIL void tCPUs::WaitForAllCPUs()
	{
		bool AllHere;
		
		ThisCPU()->WaitFlag1 = 1;
		AllHere = false;
		while (!AllHere)
		{
			AllHere = true;
			for (DWORD i = 0; i < NumberOfCPUs; i++) if (CPU[i]->WaitFlag1 != 1) AllHere = false;
		}

		ThisCPU()->WaitFlag2 = 1;
		AllHere = false;
		while (!AllHere)
		{
			AllHere = true;
			for (DWORD i = 0; i < NumberOfCPUs; i++) if (CPU[i]->WaitFlag2 != 1) AllHere = false;
		}

		ThisCPU()->WaitFlag1 = 0;
		AllHere = false;
		while (!AllHere)
		{
			AllHere = true;
			for (DWORD i = 0; i < NumberOfCPUs; i++) if (CPU[i]->WaitFlag1 != 0) AllHere = false;
		}

		ThisCPU()->WaitFlag2 = 0;
		AllHere = false;
		while (!AllHere)
		{
			AllHere = true;
			for (DWORD i = 0; i < NumberOfCPUs; i++) if (CPU[i]->WaitFlag2 != 0) AllHere = false;
		}
	}
These are in the CPU class

Code: Select all

		volatile BYTE		WaitFlag1;	// Used to sync all CPUs
		volatile BYTE		WaitFlag2;	// Used to sync all CPUs

Re: Code to wait for all CPUs

Posted: Mon Sep 29, 2014 8:51 pm
by thepowersgang
Umm... why? Why would you need all the CPUs to be running the same code at the same time? (Usually you want to avoid that situation)

Re: Code to wait for all CPUs

Posted: Mon Sep 29, 2014 8:58 pm
by tsdnz
Hi, my OS can be changed into a different type/state.
When the [type] is changed all CPUs must wait until the new state is created and ready.

Re: Code to wait for all CPUs

Posted: Mon Sep 29, 2014 10:25 pm
by thepowersgang
Ok, the best way of doing that is to have a flag which the CPUs wait to become true, and when they start waiting, they increment a "waiting cpu count" variable. The initiator (i.e. the CPU that is doing the switch) would wait for the waiting count to equal the total number of processors, do some work, then set the continue flag (and reset the waiting count).

Re: Code to wait for all CPUs

Posted: Wed Oct 01, 2014 1:15 am
by tsdnz
Hi, not sure that will work.
What if two wait calls are called one after the other.

Edit: Just re-read. If you have multiple flags it will.