On 6/13/06, Adam <[EMAIL PROTECTED]> wrote:
In the following common seeing code segment, I notice someone add 'atomic'
before 'm_data = data;' (such as OscilloscopeRF), someone does not (such as
Delta from Moteiv). Since another 'dataReady' interupt could come before
task sendData is excuted, it seems to me the first 'atomic' is not necessary
-- even if you have, the 'm_data' will be changed before sendData excuted.

Further more, if 'msg' will be modified here only, I even think the second
'atomic' is not necessary. Since one task will not preempt another task. In
other words, another post sendData will not excute until the current
sendData finish.

async event result_t ADC.dataReady(uint16_t data) {
    atomic m_data = data;
    post sendData();
    return SUCCESS;
  }

 task void sendData() {
    .....
    atomic msg->data = m_data; // assume msg will be modified here only
 }

It seems to me that 'atomic' is useful only if there are a set of variables
- you want to keep the integrity of the variable set.

There is no guarantee that the write of a single variable is atomic.
In fact, on many mote platforms, reads or writes of anything larger
than a single variable are not atomic. So in the code above, a
dataReady interrupt at the wrong time could give a rather weird value
in m_data (assuming it is 16-bits it could get one byte from one
reading, and one byte from the next).

Also, if you write

 x = x+1

there's only one variable. But you still get a data-race even if it is
a single byte. In fact, this is the classic example of a data-race...

Some people skip the atomic because either:
- they are more worried at the code size/execution time overhead of
atomic than the potential data race
- they know that the data-race won't occur for some other reason.
E.g., if I'm sampling once every 5 minutes, then there isn't going to
be a race on m_data (or if there is, there's clearly a very large
other problem ;-))

David Gay
_______________________________________________
Tinyos-help mailing list
Tinyos-help@Millennium.Berkeley.EDU
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to