-1

Recently I have been developing my own RTOS for ARM and PIC devices so I am analysing current RTOS concepts. I am having some doubts while studying FreeRTOS,

How is it handling function variables? How we can manage the variable inside a function?

Ex-1:

We declared a local variable 'i' in two threads with functions A and B in FreeRTOS, but variable 'i' of function A is affected by function B.

Ex-2:

We have two threads A , B and one function like delay. But both threads A and B call the same function delay. How does FreeRTOS allocate a single delay resource to two threads (A and B)?

artless noise
  • 21,212
  • 6
  • 68
  • 105
Anand g
  • 9
  • 4
  • Your examples are unclear. In Ex-1, what do you mean that local "variable `i` of function A is affected by function B"? Is `i` passed to B or set by B? Show us some code. In Ex-2, what do mean by a "delay resource"? Is resource a local variable to the function, `delay`? Or is there a physical resource such as a timer that you're concerned about? – kkrambo Jan 19 '16 at 14:37
  • Ex-1 is not an example of the behaviour of FreeRTOS (or any RTOS or even of the C language). If that is truly happening, you have a bug, or have described the behaviour incorrectly - post the code. Ex-2 is an entirely different question. – Clifford Jan 19 '16 at 20:39
  • Ex-2 is not a different question; it is a mis-understanding. An OS must use global structures. If the `delay` function was pure, then the OS would behave as per the title. The OS doesn't use function variables. – artless noise Jan 20 '16 at 14:03

3 Answers3

5

Variables, declared in function, are placed on stack. Threads have different stacks, so the function local variables are different for them.

Here is an answer to the 2nd questions. For delay, the OS must create data structures for each thread. For instance the delay argument will have a delta to delay. The thread with the minimum delta is woken; but a timer interrupt for the 2nd thread is set when the first runs. If the 2nd thread has higher priority, it is woken on the interrupt (if not, it is put in a ready state). Stacks and delay state are managed globally by the OS per task in a list or other data structure.

artless noise
  • 21,212
  • 6
  • 68
  • 105
V. Kravchenko
  • 1,859
  • 10
  • 12
1

Local variables are stored on the stack; each task has an independent stack. The stack is selected during a context switch by restoring the tasks stack-pointer to the SP register.

In an RTOS in general each task has a task control block, the TCB will contain a counter set to the length of the delay, on each timer interrupt, the counter is decremented; when a task delay counter becomes zero, the task becomes ready and if it is the highest priority ready task, it will run. The exact details may vary for different RTOS implementations (and FreeTROS in general is far from typical in many ways).

A better resource for learning RTOS fundamentals is probably Micro C/OS-II (or III). Its internals are exhaustively documented and written specifically to teach RTOS implementation principles.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

I presume this is a school project, and I fear you have maybe bitten off more than you can chew (as the saying goes) as an understanding of embedded programming is necessary before more advanced RTOS topics. Here is a (very old) link that will provide some fundamentals to how FreeRTOS (and other RTOSes) will manage threads and stacks: http://www.freertos.org/implementation/main.html

Richard
  • 3,081
  • 11
  • 9