Code to wait for all CPUs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Code to wait for all CPUs

Post 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
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Code to wait for all CPUs

Post 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)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: Code to wait for all CPUs

Post 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.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Code to wait for all CPUs

Post 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).
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: Code to wait for all CPUs

Post 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.
Post Reply