By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,977 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,977 IT Pros & Developers. It's quick & easy.

Atomic function

P: n/a
What does it mean to make a function atomic?

Something I read on it wasn't very clear, but made me think that I needed to
disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?

int Test;

int GetTestValue( void )
{
return Test;
}

Thanks in advance.

------------------------------
Brian
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Pegboy writes:
What does it mean to make a function atomic?

Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?

int Test;

int GetTestValue( void )
{
return Test;
}


It means that the entire function is completed without any intervening
actions. That is a = b + c fetches the two operands, does the add and stores
the result in a without any outside observer being able to insert anything
else into the "cracks". As far as I know this is impossible with the normal
instructions available to a C programmer, in my experience it requires a
special hardware instruction called, perhaps, "test & set" in which the
memory read and write are tightly bound to each other. So if you have
multiple processors, multiple threads, caches, and so on the code still
works. I saw some hand waving implying wonderful breakthroughs in
semaphores WRT mutexes a few days ago in one of these groups. but I wasn't
sufficiently enthused or believing to chase that rabbit,

I would expect modern microprocessors to have the needed special instruction
.. But it might be reserved for the OS to handle as part of the API or pass
through at it's discretion. Keywords mutex, semaphore, flag, "test +and
set" .....

The test & set instruction protects a body of code. Test & set a flag, do
your thing, clear the flag. Only the one instruction was special.
Nov 14 '05 #2

P: n/a
"Pegboy" <pe****@neb.rr.com> wrote in message news:<_S*******************@twister.rdc-kc.rr.com>...
What does it mean to make a function atomic?


If you can say with certainty that a function will execute within one
clock cycle, that function is atomic.

This is useful because something that executes within a single clock
cycle cannot be interrupted; the interrupt will either happen before
or after the function. It will never be partially executed. This is
important because in a multithreaded environment the code is
inherrently mutually exclusive (thread-safe)

---
Jared Dykstra
http://www.bork.org/~jared
Nov 14 '05 #3

P: n/a
"Pegboy" <pe****@neb.rr.com> wrote in message news:<_S*******************@twister.rdc-kc.rr.com>...
What does it mean to make a function atomic?

Something I read on it wasn't very clear, but made me think that I needed to
disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?

int Test;

int GetTestValue( void )
{
return Test;
}

Thanks in advance.

------------------------------
Brian


I would suggest that you post to comp.realtime or comp.programming as
this is somewhat OT here.

p.s. The answers you were given, demonstrate why you need to post to
the right group. Neither was really correct, but then again this is a
group which talks about standard C. Hope this helps :=)
Nov 14 '05 #4

P: n/a
In <ba**************************@posting.google.com > dy******@hotmail.com (Jared Dykstra) writes:
"Pegboy" <pe****@neb.rr.com> wrote in message news:<_S*******************@twister.rdc-kc.rr.com>...
What does it mean to make a function atomic?


If you can say with certainty that a function will execute within one
clock cycle, that function is atomic.

This is useful because something that executes within a single clock
cycle cannot be interrupted; the interrupt will either happen before
or after the function. It will never be partially executed. This is
important because in a multithreaded environment the code is
inherrently mutually exclusive (thread-safe)


It then follows that the only atomic function is

void atomic(void) { return; }

as the machine code equivalent of the C return statement already takes
at least one clock cycle to execute ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5

P: n/a

"Pegboy" <pe****@neb.rr.com> wrote in message
news:_S*******************@twister.rdc-kc.rr.com...
What does it mean to make a function atomic?

Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?

int Test;

int GetTestValue( void )
{
return Test;
}

Thanks in advance.

------------------------------
Brian


Thanks all for the help.

-------------------------
Brian
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.