In order to mitigate race conditions in multi-threaded/multi-process applications using shared memory, a common technique is to use an atomic TSL (Test and Set Lock) instruction. This instruction simultaneously reads the value of the lock variable and sets it to capture the lock if it is free, it then exams the returned (original) value of the variable to see if it was, indeed, free.
On x86 processors, this is done using the XCHG instruction. When this instruction is executed between a register and a memory location, the hardware employs a locking mechanism to make the operation atomic. However, on multicore systems, it is possible for threads running on different cores to execute the XCHG instruction on the same memory location. No specific action is taken to lock around the operation; instead, the cache-coherency protocols are employed to prevent this from happening. These protocols are pretty complicated, and I imagine involve some degree of proprietary magic.
The question I'm trying get an answer to is how a 'winner' is decided in a truly parallel situation.
Imagine two threads running the same code on two difference cores and imagine that they are running in perfect lock step. Doesn't matter how unlikely this is to happen, only that it be possible.
So, the first question is, is this possible, or is there some mechanism that renders it impossible? I would think that this would not be the case.
Assuming that it is possible, what happens when they both attempt to access the same memory location on the same clock cycle? We are assuming that the two cores are in identical states, which may require that the requested memory address is not currently in the L1 cache for either core. So let's assume a cache miss, at least up to whatever level of cache/memory both cores are fed from.
If this happens, how is the tie broken?
I can imagine a couple of possibilities. One would be for each core to have a relative priority on resources so that when ties happen, the priority of the core dictates the winner. I could also imagine a very simple random process by which the winner is chosen. I think, in practice, either approach would be viable since if two processes competing for the same resource are tied to that degree, any kind of arbitrary tie breaker is valid as long as it is fast -- pick a winner and move on.
Whatever mechanism is used also has to be generic in allowing for the possibility that every core on the processor somehow managed to want to access the same resource on the same clock cycle.
Does anyone know how this situation is handled? Preferably on a x86, but it would be more than useful to have an explanation of how it is handled on ANY multi-core processor.
On x86 processors, this is done using the XCHG instruction. When this instruction is executed between a register and a memory location, the hardware employs a locking mechanism to make the operation atomic. However, on multicore systems, it is possible for threads running on different cores to execute the XCHG instruction on the same memory location. No specific action is taken to lock around the operation; instead, the cache-coherency protocols are employed to prevent this from happening. These protocols are pretty complicated, and I imagine involve some degree of proprietary magic.
The question I'm trying get an answer to is how a 'winner' is decided in a truly parallel situation.
Imagine two threads running the same code on two difference cores and imagine that they are running in perfect lock step. Doesn't matter how unlikely this is to happen, only that it be possible.
So, the first question is, is this possible, or is there some mechanism that renders it impossible? I would think that this would not be the case.
Assuming that it is possible, what happens when they both attempt to access the same memory location on the same clock cycle? We are assuming that the two cores are in identical states, which may require that the requested memory address is not currently in the L1 cache for either core. So let's assume a cache miss, at least up to whatever level of cache/memory both cores are fed from.
If this happens, how is the tie broken?
I can imagine a couple of possibilities. One would be for each core to have a relative priority on resources so that when ties happen, the priority of the core dictates the winner. I could also imagine a very simple random process by which the winner is chosen. I think, in practice, either approach would be viable since if two processes competing for the same resource are tied to that degree, any kind of arbitrary tie breaker is valid as long as it is fast -- pick a winner and move on.
Whatever mechanism is used also has to be generic in allowing for the possibility that every core on the processor somehow managed to want to access the same resource on the same clock cycle.
Does anyone know how this situation is handled? Preferably on a x86, but it would be more than useful to have an explanation of how it is handled on ANY multi-core processor.
