Skip to content

C Language ‘static’ Keyword In Function Scope

January 8, 2017

In C language, when we want to have a global variable that exist inside a function(function scope), we can use ‘static’ keyword to achieve this. Example as below:

void function1(void){    static uint8_t global_variable=0;    uint8_t variable=0;

    global_variable++;
    variable++;
    printf("global_variable1:%d, variable:%d\n", global_variable, variable\n");}
void main(void)
{
    function1(); //output: global_variable:1, variable:1
    function1(); //output: global_variable:2, variable:1
}

Example Code 1

In superloop environment(all task is being called from a always-true while loop), and function1() is not called from ISR, above would works every well.

But the above code will not work(in some cases) when it is use into RTOS(real time operating system) environment. Depending on the system design, few scenario may happen and it require a different changes.

Case 1: Only Require One ‘global_variable’ In System

In this case, despite system having multiple task, we only require one global_variable for all task.

With RTOS enviornment, ‘global_variable’ become a share resource, thus it has to be ensure its atomic operation. Without the atomic operation gurantee, while task1 is accessing and changing ‘global_variable’, task1 may be preempt by task2, which may also make change ‘global_variable’. Mutex_lock/unlock can be use for avoid such scenario as below:

void function1(void){    
    static uint8_t global_variable=0;  
    uint8_ p_global_variable;  
    uint8_t variable=0;

    mutex_lock(&gv);
    global_variable++;
    p_global_variable = global_variable;
    mutex_unlock(&gv);
    variable++;
    printf("global_variable1:%d, variable:%d\n", p_global_variable, variable\n");
}

Example Code 2

One example of such is when we want to know how many times function1 is being called by all the tasks in the system.

In above code, we have modified such that it can be use by multiple task, this is what generally call thread-safe function

Case 2: Each Task Require One ‘global_variable’

This case assume each task have their own copy of ‘global_variable’. E.g: 2 task is in the system and each one have a copy of ‘global_variable’.

In this scenario, we need to ‘duplicate’ the variable for eacth task. Since ‘static’ keyword only allow us to have a copy of ‘global_variable’, we can no longer use this keyword. Instead we have to create task dependant global variable and passing in such global variable

uint8_t Task1_global_variable=0;
uint8_t Task2_global_variable=0;

void function1(uint8_t *global_variable){    
    uint8_t variable=0;

    //mutex_lock(&gv);
    *global_variable++;
    //mutex_unlock(&gv);
    variable++;
    printf("global_variable1:%d, variable:%d\n", *global_variable, variable\n");
}
void Task1Loop(void)
{
    while(1){
        function1(&Task1_global_variable);
    }
}
void Task2Loop(void)
{
    while(1){
        function1(&Task2_global_variable);
    }
}

Example Code 3

When Task1_global_variable is use elsewhere of the code, we would need to ensure atomic operation by using mutex.

Conclusion

From above example, we can see how ‘static’ code would change when applying in RTOS environment. In general, RTOS code would be write in different form as compare to superloop environment. One have to employ the linux multi-thread programming methology in RTOS, particularly on resource sharing, concurrency issues.

Advertisements

From → Embedded System

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: