I notice the sys/atomic.h atomic_xxx interfaces are limited to things that do read/modify/write (inc/dec/swap/etc). There is no atomic_set to do a simple assignment. In a couple headers defining wrappers around the atomic_xxx interfaces, some define an atomic_set that does a simple assignment, with no added protection. Samples: http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/io/drm/drm_atomic.h#57 http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/io/chxge/sge.h#53 Googling "atomic_set site:opensolaris.org" I found several discussions. I see there is an issue with atomically setting a 64 bit value. There are both 32 and 64 bit load/set instructions. If the compiler uses 2 32 bit operations on on 64 bit values (even though cache coherency is at a 64 bit level) an interrupt could occur between 2 32 bit operations, causing half old/new. Reference: http://mail.opensolaris.org/pipermail/opensolaris-code/2007-May/005046.html So on 64 bit values, some form of protection is required to be safe. On 32 bit values, doing a simple a=b, it does not appear locking is required. Assuming my assessment above is correct, question is: is it considered clean and proper to rely on this? Just wondering if putback code reviews would consider this relying on assumptions we shouldn''t, or having unnecessary locking... (I originally posed this question on the opensolaris discuss, but never got an answer) Thanks, Joe
Casper.Dik at sun.com
2008-Nov-10 16:20 UTC
[zfs-discuss] atomic_set and what assumptions are proper
>I notice the sys/atomic.h atomic_xxx interfaces are limited to things >that do read/modify/write (inc/dec/swap/etc). There is no atomic_set to >do a simple assignment.My question is: what protocol is used to update a specific variable to a specific value WHILE AT THE SAME TIME another part can also update the variable to a different specific value. (After the update, the variable can have any of N values) Casper
Casper.Dik@Sun.COM wrote: I notice the sys/atomic.h atomic_xxx interfaces are limited to things that do read/modify/write (inc/dec/swap/etc). There is no atomic_set to do a simple assignment. My question is: what protocol is used to update a specific variable to a specific value WHILE AT THE SAME TIME another part can also update the variable to a different specific value. The specific situation that raised the question is a signal handler. The same signal handler function was used for several signals that are all taken to mean "clean up and shut down". The function saved the signal to an unprotected global variable. If different signals are received, we don''t care which one the global ends up being set to. The signal handler could be written differently to eliminate the atomicity question. But other scenarios come to mind also. Such as setting an error in a shared piece of memory (where it might be sufficient to attempt to keep the first error, but in race conditions perhaps get the second error). (After the update, the variable can have any of N values Right - this only applies to situations where any of the N values are acceptable. -Joe _______________________________________________ zfs-discuss mailing list zfs-discuss@opensolaris.org http://mail.opensolaris.org/mailman/listinfo/zfs-discuss