Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Are my functions reentrant?

Altera_Forum
Honored Contributor II
1,217 Views

I am using NIOS-II IDE to compile my programs with built-in GCC compiler (correct me if this is wrong). 

I am using uC/OS-II as RTOS. 

 

My question is: if I write some function which is later called from different tasks and event interrupt independently, is this function created as reentrant? Is there dynamically created some space on stack to save data for each calling of this function? - or should I handle this protection myself by using some synchronization techniques (semaphores etc...). 

 

Thanks.
0 Kudos
1 Reply
Altera_Forum
Honored Contributor II
530 Views

This is more of a general C/C++ question (I'm assuming that's what you're using) and it's hard to answer without more specifics. Here are some general points: 

 

- Local variables and arguments are either on the stack or in registers so they are safe for reentrancy. Each call of your function has its own stack area (by definition) so it will have it's own storage for these values and they don't overwrite each other.  

 

- Global variables and static variables are in the data segment so they are not automatically safe for reentrancy. Two calls to a function that accesses a global variable will (generally) access the same global variable. A common way of creating non-local storage for each thread is to create "thread-local-storage" (TLS) which is maintained either somewhere near the top of the stack or somewhere in the heap and pointed to by a pointer in the stack of the thread. 

 

- Read and write operations to shared variables or other shared resources may be interrupted somewhere in the middle of the operation so you need to protect yourself against that (e.g. temporarily turn off interrupts or add semaphores) to make your accesses "atomic". 

 

Andrew
0 Kudos
Reply