[Asterisk-Dev] Re: [Asterisk-cvs] asterisk/include/asterisk module.h, 1.18, 1.19

Kevin P. Fleming kpfleming at digium.com
Fri Jun 24 07:06:20 MST 2005

Slav Klenov wrote:

> Is it guaranteed that the res = localusecnt assignement, without holding 
> a lock,
> can be done safely, without any possibility of damaging res? I'm not 
> sure if this
> can be considered as an atomic operation.

What does 'damaging res mean' in this context?

On all modern platforms (i.e. anything with a word size of 32 bits or 
larger), as long as the int variables involved are aligned on their 
natural boundaries (not in a 'packed' structure), then reading or 
writing (but _NOT_ read/modify/write) of them is always an atomic 
operation. There is no possibility for part of the int to be written and 
then the remainder written later.

In this code, 'res' does not need to be protected, only the read from 
'usecnt', since 'res' is not a shared variable.

More information about the asterisk-dev mailing list