Help with global variable

Technical questions regarding the XTC tools and programming with XMOS.
bearcat
Respected Member
Posts: 283
Joined: Fri Mar 19, 2010 4:49 am

Help with global variable

Post by bearcat »

I have struggled with global variables in XC. Can y'all set me straight.

If I declare a variable before any functions in a module. Let's call it g_variable. This variable gets placed in the DP memory space (which can be accessed via inline assembly using the dp[] syntax). But let's say I want to access it directly in XC. The function I include it in can use it as pleased (but another case is if this function does not use it). Then I include it as an "extern" in another function.

If this access is read only, why does this give a parallel usage error? Why have the extern if this isn't allowed? There may some very limited use of declaring in a function that does not use it. Seems like I have seen a difference if used in an "if" statement or not.

I need some help to get this straight in my mind set as I have not been able to nail this down.


User avatar
Paolomio
Experienced Member
Posts: 64
Joined: Tue Oct 05, 2010 7:33 pm

Post by Paolomio »

Are the two routines on the same core and in the same thread? It's easy to see the memory cannot be accessed across different cores, but there are restrictions on memory access across threads. See "Programming XC..." pp. 30-32 and pp 103-104.
bearcat
Respected Member
Posts: 283
Joined: Fri Mar 19, 2010 4:49 am

Post by bearcat »

Ok. Thanks for the page references. As outlined:

1 - If thread Tx contains any modification to variable Vy then none of the other
threads (Tt, t 6= x) are allowed to use Vy .
2 - If thread Tx contains a reference to variable Vy then none of the other threads
(Tt, t 6= x) are allowed to modify Vy .

I am trying to get a handle around this.

1 - So if any thread modifies a global, no other thread is allowed to use it. So what is the point to an extern in this case? The original thread is usually always going to set this variable, then another thread simply needs to read the value????

2 - The original thread to reference this variable cannot allow other threads to write it, ok. That's what inline assembly can get around.

So again, what is the point to an extern?
I am getting the point that they are limiting the usage to only a single thread. But any real example has all kinds of inline assembly getting around this limitation, as I am having to do and is a pain.
User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am

Post by paul »

XC is designed as a safe language... so it stops you getting tangled up in race conditions with shared memory. The point of extern? So that I can have multiple files which are used on a single thread and can therefore access the global (but you might as well pass it around I guess).

If you really want shared memory and not use assembler, use C.

Cheers,
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
User avatar
Paolomio
Experienced Member
Posts: 64
Joined: Tue Oct 05, 2010 7:33 pm

Post by Paolomio »

paul wrote: If you really want shared memory and not use assembler, use C.
Or use channels to communicate between threads...

its not the same thing as extern global memory, but it always threadsafe.