ufsstat - testers / feedback wanted!
Peter Jeremy
PeterJeremy at optushome.com.au
Fri Oct 14 03:30:04 PDT 2005
On Fri, 2005-Oct-14 10:10:04 +0100, Brian Candler wrote:
>I'd be grateful if you could clarify that point for me. Are you saying that
>if I write
>
> long long foo;
> ...
> foo++;
>
>then the C compiler generates code for 'foo++' which is not thread-safe?
>(And therefore I would have to protect it with a mutex or critical section)
foo++ is not normally thread-safe even if foo is an int or pointer.
Typical iA32 code looks like:
addl $1,foo
This is only thread-safe in a UP environment.
Typical RISC code looks like:
load foo,%reg
add $1,%reg
store %reg,foo
This is not thread-safe.
Note that the compiler may keep foo in a register for an extended period
unless you convince the compiler not to. Especially on RISC processors,
the compiler will normally spread the load/add/store to try and avoid
pipeline stalls.
If you share foo between two threads, you need to use atomic operations
(see <machine/atomic.h>), mutexes or critical sections on all updates.
Note that you can do atomic 64-bit operations on iA32 (except 80386 and
80486) using a locked cmpxchg8b in a loop.
--
Peter Jeremy
More information about the freebsd-current
mailing list