-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmpxchg function is missing the loaded value #461
Comments
I need to remind myself of the use of cmpxchg, but it looks like the case where you actually need to use T is the failure case. So maybe just return an anonymous struct:
|
Let's make sure these compile errors are in place too:
|
Alternatively, you could return the value that was there (this is what similar calls do in Windows and Linux).
I suggest modifying this to have the return type be %T. It is either an error (for instance bad alignment) or you get a valid T value. If the T value is the old value (cmp), then the swap succeeded. If the T value is something else, then the swap failed.
(Have I mentioned, lately, how much I like the error idea in Zig?) |
Oops, forgot to comment about the size. All processors that support some form of CAS ("compare and swap" is the generic way this is referenced in the literature) usually only support a very small number of sizes so this is inherently a platform specific operation. Virtually all CPUs that support it at all support at least a single machine word. Some support DCAS (double compare and swap) and a few support QCAS (quad compare and swap). I am not aware of any that support some form of CAS that is not single machine word at least. Where things get a little frustrating is that "machine word" is not a very solid thing. Is that the size of an address? Of an integer register? I do not have any good answers here, but I usually assume that you can swap an address IFF the size of an address is something relatively sane. I.e. Intel's weird addressing modes with explicit segments do not work. |
cmpxchg is supposed to be a read-modify-write atomic operation. It should provide a way to return the read value.
It should return
?T
instead:The text was updated successfully, but these errors were encountered: