[NO REPLY] Special function types (rev #3, 2018-1-15)

A place of insane ideas, nothing to see here.
User avatar
Giovanni
Site Admin
Posts: 11385
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 430 times
Been thanked: 363 times
Contact:

[NO REPLY] Special function types (rev #3, 2018-1-15)

Postby Giovanni » Mon Dec 11, 2017 2:22 pm

Several types of functions are explicitly declared: normal, root, [async|pure|const].

Normal functions

Normal C functions without any specific attribute nor restrictions.

Root functions

  • Root functions cannot be called by any other function.
  • No register preserved through the call in order to save stack space.
  • Can be called by assembler code only.
  • The main() function is implicitly a root function.
  • RTOS tasks can be declared as root functions saving RAM.
  • IRQ handlers can be declared as root functions saving RAM if called by an assembler veneer.

Code: Select all

  root void foo(void) {
    ...
  }


async, pure and const functions

The three attributes are mutually exclusive and of increasing restriction.

Const functions

Const functions are the most restrictive function type.

  • Functions only accessing its own parameters, not even const global data.
  • Can take pointers as parameters but cannot dereference.
  • Const functions can only call const functions.

Code: Select all

  const double sqrt(double x) {
    ...
  }


Pure functions

  • Functions only accessing its own parameters and const global data.
  • Can take pointers as parameters but cannot dereference for write.
  • Pure functions can only call pure or const functions.

Code: Select all

  pure uint32 lookup(const uint32 *table, int index) {
    return table[index];
  }


Asynchronous functions

Asynchronous functions are functions that can be called asynchronously during the program execution. The nature of asynchronous functions is not pre-defined (ISRs, tasks, multi-core or others).

Asynchronous functions can be assigned to arbitrary classes that can be mapped on the one of the above reasons (ISRs, tasks etc).

  • Asynchronous function do not do any special processing required by ISRs, for example saving/restoring registers, it is a pure attribute to a normal function. Interrupt-related attributes are eventually added using __attribute__ because can be architecture-dependent.
  • Asynchronous functions can preempt other asynchronous functions.
  • Asynchronous functions can only call async (of the same class), pure or const functions.
  • Asynchronous functions can only access const global variables or automatic variables.
  • Asynchronous functions can be called by other async functions of the same class.
  • Access to global variables can be enabled by the use of exclusive compound statements in order to enforce arbitration.

The declaration form is:

async <<class>> <return type> <function name> (<parameters list>|void )


Code: Select all

/* Defines and asynchronous function of class "ISR".*/
async <ISR> void my_isr2(void) {
}


Giovanni

Return to “Safer C”

Who is online

Users browsing this forum: No registered users and 1 guest