Atomic function

P

Pegboy

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.
 
O

osmium

Pegboy said:
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.
 
J

Jared Dykstra

Pegboy said:
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)
 
J

J Bennett

Pegboy said:
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.

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 :=)
 
D

Dan Pop

In said:
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
 
P

Pegboy

Pegboy said:
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.

Thanks all for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top