How does multicore CPU thread synchronisation work at the hardware level?

-2

I'm trying to understand how multicore CPU's implement thread synchronisation at the hardware level.

From my research so far I've found that the process involves using a location in memory to act as a lock and for a thread to run its sychronized section of code it needs to aqcuire the lock by using a special instruction to simultaniously read the value of 0 and write a value of 1 to this memory location.

This should work perfectly, however modern CPU's contain multiple levels of per core and shared cache. if a CPU implements cache write-back then the memory location holding the lock value will be out of sync with in L1 cache across cores. If the CPU implements cache write-through protocol then there will be a significant performance hit to every write to memory.

How does the CPU maintain synchronisation of memory locations used as locks in cache?

MSD

Posted 2019-05-09T22:09:54.953

Reputation: 1

Answers

-2

Found the answer: Cache coherence (MESI protocol)

https://docs.roguewave.com/threadspotter/2011.2/manual_html_linux/manual_html/ch_intro_coherence.html

Below are highlights from the article in the link above(incase of broken link):

"Cache line states in the MESI protocol

M - Modified The data in the cache line is modified and is guaranteed to only reside in this cache. The copy in main memory is not up to date, so when the cache line leaves the modified state the data must be written back to main memory.

E - Exclusive The data in the cache line is unmodified, but is guaranteed to only reside in this cache.

S - Shared The data in the cache line is unmodified, and there may also be copies of it in other caches.

I - Invalid The cache line does not contain valid data."

"If a thread reads data not present in any cache, it will fetch the line into its cache in exclusive state (E)"

"If a thread reads from a cache line that is in shared state (S) in another thread's cache, it fetches the cache line into its cache in shared state (S)"

"If a thread reads from a cache line that is in exclusive state (E) in another thread's cache, it fetches the cache line to its cache in shared state (S) and downgrades the cache line to shared state (S) in the other cache"

"If a thread reads from a cache line that is in modified state (M) in another thread's cache, the other cache must first write-back its modified version of the cache line and downgrade it to shared state (S). The thread doing the read can then add the cache line to its cache in shared state (S)"

MSD

Posted 2019-05-09T22:09:54.953

Reputation: 1