There are ways to deal with that situation like checking that nobody else has the same token, disabling write access until that is confirmed for example. You will need to set up some form of arbitration protocol to control which processor wins the toss in the event.
If processor A were clocked slightly earlier than processor B (assuming both have the same clock rate) then A could set a flag which B would have to check.
Off the top of my head, if the clocking of A and B were at arbitrary times then perhaps each could check to see if the memory is being accessed, then check again after a set delay which is different for A and B. Only if both checks showed no current access would access be granted.
The first thing you need is shared memory that is coherent across all processors (not cached) then you need the equivalent atomic compare-and-swap instruction on both processors to create a token. https://en.wikipedia.org/wiki/Compare-and-swap
That's why I suggested a double check using different delays for A and B. On the second check, the processor with the longer delay would find it unavailable.
The first thing you need is shared memory that is coherent across all processors (not cached) then you need the equivalent atomic compare-and-swap instruction on both processors to create a token. https://en.wikipedia.org/wiki/Compare-and-swap
I read about the Compare-and-Swap operation,
However, it deals with different processes running on the same processor.
It's not the same as different processes on different processor.
I mean,
How do you gain the ATOMIC();(see below) between Different Processors?
C:
int compare_and_swap(int* reg, int oldval, int newval)
{
ATOMIC();
int old_reg_val = *reg;
if (old_reg_val == oldval)
*reg = newval;
END_ATOMIC();
return old_reg_val;
}
It works on multiprocessor systems if they have hardware support (Shared memory and locking primitive instructions). I have no idea about the capabilities of your system.
Here's one quick and dirt way, but I warn you, I haven't really thought this through, so treat it more as food for thought...
Let's say you have a number generator randomly spitting out values 1-10 which temporarily stores two of those values, making sure that each is different. Each value is assigned to a processor. Whenever a token request is made and both processors want access to the same memory location, each already has a priority assigned, lowest number wins the toss and the other processor has to wait until the token is relinquished. Either way, whenever the token becomes free, the number generator starts allocating numbers again