Non-full loads across awaits
terrarier2111 opened this issue · comments
Is it legal to perform a non-full load and use the value across await boundaries? If so could you give a short explanation how this is acomplished as i saw something that looked like thread-local storage in the fastpath. Also if this pattern is allowed is there a penalty associated with using these values across awaits?
Hello
Yes, that's allowed, at least as safety goes. The thread local storage is mostly just a cache for fast-lookup of data. The Guard can indeed move from one thread to another and be dropped at a different one than where it originated (there's even a test specifically for that I think).
However, there's a downside. Each Guard holds a slot that „belongs“ to the thread where it originated. The slot is eventually returned to the originating slot when no longer in use (simply by marking a flag „not in use“), but until then it is blocked. If a thread runs out of these slots, more loads get slower.
That is, it's not outright wrong or broken in any way to have it across an await, but you might slow things down if you have too many of them alive at the same time.