[NO REPLY] Compound statements (rev #5, 2018-1-18)

Obsolete material.
User avatar
Giovanni
Site Admin
Posts: 12313
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 594 times
Been thanked: 505 times
Contact:

[NO REPLY] Compound statements (rev #5, 2018-1-18)

Postby Giovanni » Mon Dec 11, 2017 12:17 pm

In order to implement other mechanisms I have in mind (see critical zones, monitors and others) a redefinition of compound statements is necessary:

Lets call the standard compound statement a Default Compound Statement.

Code: Select all

{
  statement1;
  ...
  statementN;
}


A new mechanism is introduced, a way to define different kind of compound statements:

Code: Select all

worddef [abstract] [exclusive] [sync] [async <class1>[<class2>...<classN>]] [(expr1; ...; exprN)] {
  int hidden_variable1;
  char hidden_variable2;
  static uint32_t hidden_variable3;

  /* All the following sections are optional.*/
static_init:
  /* Only has access to hidden static variables.*/
  hidden_variable = (uint32_t)5;
init:
  /* Executed on enter of block but not on continue.*/
  ...
/* Executed from any context (synchronous or **unspecified** asynchronous).*/
enter:
  /* Block enter.*/
  ...
leave:
  /* Block leave.*/
  ...
on_retry:
  /* Retry statement then implicit "continue".*/
  ...
on_continue:
  /* Retry statement then implicit "continue".*/
  ...
on_break:
  /* Break statement then implicit "break".*/
  ...
/* Executed only if the statement is used from synchronous context.*/
sync_enter:
  ...
sync_leave:
  ...
sync_on_retry:
  ...
sync_on_continue:
  ...
sync_on_break:
  ...
/* Executed only if the statement is used from the **specified** asynchronous context.*/
async_enter <class>:
  ...
async_leave <class>:
  ...
async_on_retry <class>:
  ...
async_on_continue <class>:
  ...
async_on_break <class>:
  ...
} <name>;


The abstract modifier makes the new block construct not usable directly, it can be used only as basic construct type.

The exclusive modifier restricts the code inside the construct to only access functions or variables marked with the same construct attribute or call const/pure functions.

The async <class1>...<classN> modifier restricts the use of the construct to asynchronous context of the specified classes.

The sync modifier enables the use of the construct from synchronous context.

If async and sync are not specified then the construct can be used from any context.

Inside declarations, break leaves the block jumping to the associated leave section(s), continue jumps back to the enter section(s). A new keyword retry causes the execution of the specified statement(s). The construct is designed to allow the implementation of safe, monitor-like, constructs.

There are tricks attached:

  • The special block name is both a keyword for instantiate the block AND and an attribute for variables and functions. This is related to the use of the exclusive modifier.
  • It is not possible to nest exclusive blocks of the same type, it is possible to nest differently named exclusive blocks.
  • Functions having an exclusive block attribute cannot use the construct inside, that would mean allow nesting.
  • Exclusive block have the same access privileges of the using function regarding global variables: async=pure/const-only, normal=all.

Example:

Code: Select all

worddef {
leave:
  continue;
} forever;

worddef (expr1) {
  unsigned int n;
init:
  n = (unsigned int)expr1;
enter:
  if (n == 0u)
    break;
leave:
  n = n - 1;
  if (n > 0u)
    continue;
} repeat;


It would be used as follow:

Code: Select all

repeat (10) {
  ...
}

forever {
  ...
}


Alternative use is to define a block type derived by another one.

Code: Select all

worddef <block type> <new block type>;


Nothing is added by if the block is exclusive then the two types equal but mutually exclusive. Example:

Code: Select all

/* Defines an abstract monitor construct.*/
worddef abstract exclusive async <tasks> {
  static mutex_t mutex;               /* Not instantiated, it is abstract.*/
  static condition_variable_t cond;   /* Not instantiated, it is abstract.*/
init:
  mutex_init(&mutex);
  cond_init(&cond);
sync_enter:
  mutex_lock(&mutex);
sync_leave:
  mutex_unlock(&mutex);
synch_retry:
  condition_variable_wait(&cond);
async_enter:
  mutex_lock(&mutex);
async_leave:
  mutex_unlock(&mutex);
asynch_retry:
  condition_variable_wait(&cond);
} monitor;

/* The two new monitors have different mutex and condition variables.*/
worddef monitor port_a_monitor; /* Monitor variables are instantiated for port_a_monitor.*/
worddef monitor port_b_monitor; /* Monitor variables are instantiated for port_b_monitor.*/


Giovanni

Return to “obsolete”

Who is online

Users browsing this forum: No registered users and 2 guests