Page 1 of 1

REENTRANT functions

PostPosted: Sat Dec 16, 2000 8:58 am
by luben
Hello,

After I began to do some small project with your demo I found the problem with reentrance of one functions. As I know all HiTech arithmetic libraries are not reentrant functions, I mean HiTech C is not written for multitasking system (will be good news if it's not true). In addition there is no one word for reentrance of the functions. So if I don't make the calculations of one type inside of one task I could expect problems. For example if I do floating point multiplication I should do it only into one task, or I should care for some Semaphores that indicates "wait to free the library".
I know that with some additional semaphores such problems with reentance of the functions could be solved, just asking is there no other "short" way to do this.

Regards
Luben


Re: REENTRANT functions

PostPosted: Sun Dec 17, 2000 8:33 am
by luben
Hello,

Thank you for the reply. I think I just missed the "static" key word. After I add it - everything is OK.

Yes, with cooperative multitasking reentrancy problems could appear only if I call the same function from tasks and interrupt.

Thank you again
Luben


Re: REENTRANT functions

PostPosted: Sun Dec 17, 2000 9:22 am
by aek
Hi Luben.

Keep in mind that the PIC C library functions are not reentrant because PIC C does not use a parameter stack -- that's why it's so space efficient. I believe the IAR compiler also does not use a parameter stack.

In general, the code size and RAM usage for a stack-based compiler (as is possible on the PIC18) is considerably greater than a non-stack-based one.

However, I don't understand your question -- the lack of reentrancy in a PIC C library function is in no way related to tasks. That's because each library function runs to completion inside your task -- it cannot be reentered from a task. You can call the same library function (or any function, for that matter) from as many tasks as you want without any problems.

Therefore, you can call floating-point library functions from as many tasks as you want, without problems. What you cannot do is call the library functions from mainline code (e.g. tasks) and from an interrupt. In order to call a function from both "places", you must prepend the function's definition with

code:
#pragma interrupt_level 0

This instructs the PIC C compiler to create a special "interrupt parameter area" for the function when called from an interrupt, separate from the parameter area for the function when called from mainline code. Unless you recompile the PIC C library functions, you cannot call them from both mainline code and interrupt code. Also, without this #pragma, the compiler will issue an error if you call a function from both mainline code and from an interrupt.

Reentrancy of functions called by tasks is an issue with preemptive RTOSes, as it's quite possible to get a context switch in the middle of the function. But that's not possible in Salvo (with the exception of interrupts, as explained above), so you don't need to worry about it.

Perhaps you're using some local variables in each task to store the results of the library function calls? That will not work, since the local variables are "shared" across all the tasks. If you make those variables static, then it will work. See "Why must tasks use static variables?" and "I'm using a message event to pass a character variable ..." in the User Manual's FAQ for more info.

In summary, with Salvo you only need to worry about reentrancy when you are calling a function both from mainline code and from an interrupt.

[This message has been edited by aek (edited December 17, 2000).]


Re: REENTRANT functions

PostPosted: Sun Dec 17, 2000 9:29 am
by Salvo Tech Support
Perhaps you could post the code for the two tasks in question?

Note: to post code in a readable manner, follow the instructions here on using the UBBCODE Code Tag.

[This message has been edited by Salvo Tech Support (edited December 17, 2000).]