by mcherm · 12y ago · 0 comments
So this is all based on two core features at the C(/assembly ?) level. One is the ability to apply a per-thread offset to all memory access (using "%gs:", whatever that is); the other is a (Linux-only) way to move where a memory-mapped page of memory points (remap_file_pages()).
The trick is for all data structures to be allocated in shared-memory pages unique to each thread, then re-map them so they're overlapping. Now we have all threads sharing all heap memory structures (just as you would in a traditional threading model), and zero overhead to read or write to a data value.
In a traditional threading model, you would then put in code that checks for a lock before accessing (read OR write) any data field that might be shared (and ever modified). Threads would block until a field was free before acquiring a lock and reading from or writing to it. That's a price paid for each and every access -- and if conflicts between threads are rare it's a high price to pay.
In STM we instead allow threads to modify data in (short) "transactions", then "commit" these transactions at the end; rolling back one or the other when two threads conflict. In Armin's new approach here's how this gets done:
Reading shared data is fine... go ahead and read to your heart's content. When you need to WRITE to a field inside a transaction, you first clone the entire memory page (that's an expensive operation). Then you adjust the per-thread offset so it's pointing to your own memory page and you continue reading and writing freely -- the only overhead is keeping a list of fields modified and read during the transaction.
At the end of the transaction, you pay again. First, you check whether someone changed data you had read: if so then all the work of the transaction needs to be abandoned and you can start over. Then you copy the modified values back to the "shared" location (if another thread was using them then IT may later discover that it needs to start over).
The theory of STM is that when conflicts are rare there will be few rollbacks so you'll mostly just pay the cost of cloning memory, tracking reads and modifications, and copying the values back. This will (hopefully) be less overhead than having to check for a lock before reading or writing any value, so an STM approach will be better than a traditional threads-and-locks approach. The innovation here is using the memory-mapped page and per-thread offset to allow normal access to memory for reads/writes instead of the traditional approach to STM which requires adding extra indirection logic to each and every memory read or write.
So, smart people of Hacker News... how accurate is my summary?
Two details. First, cloning a complete page and calling remap_file_pages() only occurs once on a given page, so if a number of transactions repeatedly modify the same objects, they will already be in un-shared pages (the page are tentatively shared again during major collection only).
And second, about the exact time at which "read-write" conflicts are found: when we commit, we know which objects we modified, so we can check if someone else has read them and abort that other thread instead. We never need to walk the list of all read objects: it is enough to check if some (smaller number of) objects were read or not by another thread. So the "list" of all read objects is implemented as a byte mask over all objects.
Instead, the advantage lies in STM providing a much nicer userland experience. It's downright trivial to write some hard concurrency programs using STM.
You do have to worry about contention causing too many rollbacks, but by-and-large STM is a drop-in "make concurrency make sense" module.
For many examples see Simon Marlow's Concurrency in Haskell book.
At a minimum one can hope this STM implementation will allow a higher-performance, more concurrent python in which all operations that are atomic in CPython remain atomic. Better would be a high-performance implementation of the async interfaces introduced in 3.4.
http://chimera.labs.oreilly.com/books/1230000000929/index.ht...