Pages

Thursday, July 27, 2017

Pel Ending

Welcome » NERWous C » Pel
  1. Ways To End
  2. End Statement
  3. Terminate Statement
  4. Pel TERMED Exception
  5. Pel Ended Status
  6. Pel ENDED Exception
  7. Pel Ending Detection
  8. Kill Statement
  9. Pel Abortion
  10. Exit Statement


Ways To End

Once a task has been created, it has to end so that the program can exit. There are several ways for a task to end.
  • A task ends if it stops running. This is the normal behavior when the task runs off the last code statement. It implicitly invokes the end command.
  • A task ends when it invokes the end command. The task can specify if it ends normally or abnormally.
  • A task ends when it invokes the return statement. The return operation is the combination of the release operation and the end operation.
  • A task is terminated if it has ended orderly due to a terminate command from another task. The task receiving the terminate request can catch it via the TERMED exception to do an orderly ending.
  • A task is killed if it has ended abruptly due to a kill command from another task. The killed task will stop running on the spot.
  • A task is aborted if it has ended abnormally due to an uncaught exception. The aborted task will stop running on the spot.
When a task ends normally, it raises a ENDED exception. When a task ends abnormally, it raises the a FAILED exception. Both exceptions can be caught by tasks that interface with the ending task.

When all the tasks in a NERWous C program have ended, the NERWouc C program can exit.


End Statement

The end statement allows a task to end by itself. The end operation also supports the mode attribute to mark the ending status:

ModeSynopsis
ENDEDThis default mode is usually omitted. The task ends normally. Any tasks that are waiting on this task will get the ENDED exception.
FAILEDThis mode must be specifically specified. The task ends abnormally. Any tasks that are waiting on this task will get the FAILED exception.

Let's see the end statement in action with the following Producer/Consumer example, where the Producer code block is run in parallel with the Consumer code block.
#define MAX 100
main () {
   int counter = MAX;
   <mel buffer> int store;

   /* Producer task */
   <!> while ( --counter ) {
      int c = Produce();
      if ( c == 0 ) <end>;    /* explicit end */
      <?>store = Produce();
   }  /* implicit end */

   /* Consumer task */
   <pel> task_consumer = <!> {
      while ( --counter ) {
         try { Consume(<?>store); }
         catch ( store<NOWRITERS> ) { <end FAILED>; }
      }
      <end>;
   }

   /* Wait for Consumer to be done */
   try {
      <?> task_consumer;
   }
   catch ( task_consumer<ENDED> ) {
      printf ("All [%d] items processed fine", MAX);
   }
   catch ( task_consumer<FAILED> ) {
      printf ("Process ended early due to 0 value");
   }
}
The main task pels the Producer code block. This code block explicitly ends when it detects a 0-valued product and invokes the <end> statement. If no 0-valued product is ever generated, the Producer code block will run for MAX iterations; it then ends with an implicit <end> statement when it falls off the while loop and has no more code to run.

After pelling the Producer code block, the main pels the Consumer code block. The difference now is that main tracks the running of the Consumer code block with the pel variable task_consumer. There are two endings for the Consumer. It either ends with the status FAILED when it detects that there is no more Producer to feed the store mel variable, or it ends with the status ENDED when it has gone through MAX iterations, and explicitly invokes the <end> statement (unlike the Producer code block that invokes it implicitly).

The following statements are equivalent:
<end mode=ENDED>
<end ENDED>
<end>
After pelling the Consumer code block, the main task waits for it to end by waiting on the pel variable task_consumer. This wait is interrupted by either the task_consumer<ENDED> exception or the task_consumer<FAILED> exception.

In the above example, the end statements are used inside pelled code blocks. In subsequent examples, we will see the use of the end statement inside pelled functions.


Terminate Statement

In the Producer/Consumer example above, the Producer code block task keeps Produce'ing until a value 0 is generated; the Producer then ends. The Consumer code block task needs to detect that the Producer has ended, so that it can also end. However, not all Consumer tasks want to do this detection.

In the example below, we introduce another way to end the Consumer task. It depends on the main task that creates both the Producer and Consumer tasks to detect that the former has ended, and then to force the latter to end via the terminate command. Instead of inline code blocks, this time we will use the functions Producer and Consumer.
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store);

    <?>task_producer;
    <terminate>task_consumer;
}
void Producer (<mel> int store) {
    while ( 1 ) {
        int c = produce();
        if ( c == 0 ) <end>;
        <?>store = c;
    }
}
void Consumer (<mel> int store) {
    while ( 1 ) {
        consume (<?>store);
    }
}
The main task remembers the tasks it creates via the pel variables -- task_producer and task_consumer. It then waits for the task_producer to have ended by checking for its return value via the wait statement:
<?>task_producer;
This wait is over when the Producer runs the end statement to end itself. The main task then explicitly terminates the Consumer task via the statement:
<terminate>task_consumer;
The terminate operation on task_consumer does not mean that the main task ends the Consumer task abruptly. It is a request for Consumer to end graciously at the next NERWous C operation (either a mel operation or a pel operation). In the above example, it happens when Consumer does a mel wait <?>store.

If the Consumer task is already doing a mel wait on store, the terminate operation will result in the task being removed from the mel store wait queue so it can process the termination request. If the Consumer task is in the middle of running the consume function, the terminate command flags the task as having been requested for termination. When the Consumer task finishes with consume, it iterates the while loop and hits the mel wait statement again. This time, instead of accessing the mel store, it will process the termination request.

If the task targeted by the terminate operation never hits a mel or pel operation, it will not process a pending termination request. The task can end later via another ending method (such as invoking an end statement), or it can run indefinitely using only non-NERWous statements.

To announce its presence, the terminate request generates a pel TERMED exception against the targeted task. This task can catch this exception to end in an orderly manner (or not to end at all). The next section will show an example using the TERMED exception.

From the standpoint of the invoking task, issuing a terminate operation against another task that is already ended, will result in an immediate success. Otherwise, the terminate operation does not wait for the targeted task to be ended. As long as the termination request has been successfully registered at the targeted task, the invoking task can continue on with its execution thread. If the invoking task later wants to check if the targeted task has indeed ended, it can check for the latter's <ENDED> status, as will be discussed in the later pel status section.


Pel TERMED Exception

In the example above, the Consumer task terminates without the chance for any self-ending work. For an orderly ending, the Consumer task can trap the TERMED exception raised by the terminate operation:
void Consumer (<mel> int store) {
 while ( 1 ) {
     try {
         consume (<?>store);
     }
     catch (pel<TERMED>) {
         printf ("I'm being terminated");
         <end>;
     }
 }
}
The ending work is done inside the catch (pel<TERMED>) code block. The keyword pel represents the current task (i.e. the Consumer task).

The Consumer task checks for the pel<TERMED> exception whenever it makes use of a mel operation (such as a mel wait), or a pel operation, inside the try block.

Once the Consumer task enters the catch code block, the pel TERMED exception is deemed to have been serviced. Thus, invocations of any NERWous C mel and pel operations are now available, without re-triggering the pel TERMED exception. A targeted task could take advantage of this clearance to continue running. However, the purpose of the TERMED catch code block is to tidy up the task before terminating -- it is not intended to have the task ignoring the terminate request. The catch (pel<TERMED>) code block should end with an end statement, which allows the task to end itself, as in the Consumer example above.

If Consumer were to omit the end statement inside the pel<TERMED> code block, it would acknowledge the termination request but would not end. For example:
void Consumer (<mel> int store) {
 while ( 1 ) {
     try {
         consume (<?>store);
     }
     catch (pel<TERMED>) {
         printf ("I'm being terminated");

         /* <end>; */
         printf ("No return - I'm ignoring the terminate request");
     }
 }
}
In the above code, the while ( 1 ) loop would iterate, the mel wait <?> would be reached again, but this time, since the TERMED exception has been serviced, the mel wait operation would not re-trigger the execution of the pel<TERMED> code block, and the Consumer would continue to run.


Pel Ended Status

Let's get back to the main task. After issuing
<terminate>task_consumer;
the main task ends. However the program can only exit if all the tasks that are pelled within the program have also ended. With the Consumer task being able to delay or even ignore the termination request, main can check for the ENDED status of Consumer to see if that it has terminated:
#include "nerw.h"
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store);

    <?>task_producer;
    printf ("Task Producer ending detected by waiting on pel variable");
    <terminate>task_consumer;
    sleep (60);      /* give Consumer 1 min to end graciously */
    <update>task_consumer;     /* update the local properties */
    if ( task_consumer<status> != "NERW_STATUS_ENDED" )
        printf ("Task Consumer still has not ended after 1 min");
}
Assuming that 60 seconds is ample enough for Consumer to wrap up and end, if main detects that the former is still running, it displays the "not ended" statement. In this example, main then ends. However if Consumer is still running, the whole program cannot exit. Later we will introduce the kill statement where main can kill the runaway task so that the whole program can exit.


Pel ENDED Exception

There are several ways for Consumer to know that it should end. From the mel variable store that it shares with Producer, it can check for the CLOSED exception or the NOWRITERS exception.

From the pel side of NERWous C, Consumer receives a termination request from the pelling task triggering a TERMED exception. We now add another way which is for the interfacing task to have ended, triggering an ENDED exception.

In the example below, let's combine all the ways:
main () {
    <mel> int store;
    <pel> task_producer = <! name="TheProd"> Producer (store);
    <pel> task_consumer = <!> Consumer (store);

    <?>task_producer;
    <terminate>task_consumer;
}
void Producer (<mel> int store) {
    while ( 1 ) {
        int c = produce();
        if ( c == 0 ) {
            <close>store;
            <end>;
        }
        <?>store = c;
    }
}
void Consumer (<mel> int store) {
    <pel> task_producer;
    <update name="TheProd">task_producer;
    while ( 1 ) {
        try {
            consume (<?>store);
            <update>task_producer;
        }
        catch (store<CLOSED>) {
            printf ("I am ending due to a MEL CLOSURE");
            <end>;
        }
        catch (store<NOWRITERS>) {
            printf ("I am ending due to no MEL SUBSCRIPTION");
            <end>;
        }
        catch (task_producer<ENDED>) {
            printf ("I am closing due to a PEL CLOSURE");
            <end>;
        }
        catch (pel<TERMED>) {
            printf ("I am ending due to a PEL TERMINATION");
            <end>;
        }
    }
}
The Consumer task needs to discover the task Producer in order to catch its ENDED exception. It does that by asking the CHAOS environment to scan for a task named TheProd, via the update statement:
<update name="TheProd">task_producer;
Once the local variable task_producer is established, Consumer can download the task Producer's properties, including its status, via the simpler update statement without the name attribute. This download is done in every while iteration. If the status shows an ENDED condition, the task_producer<ENDED> exception is triggered.

The Consumer task is also catching three other exceptions. On a 0-valued product, the Producer task invokes the <close>store operation, generating the mel closure exception (store<CLOSED>) at the Consumer task. Producer then ends itself with the end statement, thus removing itself from the mel store writers subscription. Since there is no other writer task, the store<NOWRITERS> exception can also be raised at the Consumer.

By watching the Producer task via the pel wait statement <?>task_producer the main task can detect that the Producer has ended. It then issues a terminate request to Consumer, triggering the pel termination exception (pel<TERMED>).

Chronologically, the mel closure exception is generated first. followed by the mel no-writers exception, then the pel ended exception, finally followed by the termination exception. However what exception the Consumer takes depends not only on the particular CHAOS runtime environment implementation, but also on the timing of the tasks involved.

For example, if Producer ends while Consumer is doing the consumption of the last store value, when Consumer is done with this consumption and checks on Producer via the update operation, the pel task_producer<ENDED> exception will be caught. However if Producer has ended after Consumer has checked on it, when Consumer iterates the while loop to get the next value of store, the mel closure store<CLOSED> exception will be caught instead.

A NERWous C exception can only be detected when a task is doing a corresponding NERWous C operation. When a task does a mel operation, it first checks for any termination exception, and if none, it checks for any mel exception, then any pel exception. When a task does a pel operation, it first checks for any termination exception, and if none, it checks for any pel exception then any mel exception.

In the above example, all the exception handlers end with an end statement. If end is missing (unintentionally or on purpose), then the subsequent behavior of the task will be different. The pel<TERMED> exception is an one-off exception: once it is serviced by a catch block, the exception is removed. On the other hand, the mel CLOSED and NOWRITERS exceptions and the pel ENDED exception are persistent. If the end statement is missed, on the next iteration of the while loop, the mel closure exception will be caught again when the task tries to access the mel variable at the <?>store wait statement. The task will spin ad nauseam in this manner until it is terminated or killed. To allow a termination to stop this potential live lock, the termination request is checked on entry of each mel or pel operation before any other exception.

In practice, not all the ending detection methods need to be used by a task. The task just selects one method that best fits its logic.


Pel Ending Detection

We now explore the behind-the-scene flow of the many ways using a pel variable to check if a task has ended:
  • Wait for a pel feedback
  • Check the pel status
  • Inquire a pel exception
The following function will do all three of them:
function monitorTaskEnd (<pel> atask) {
    printf ("Wait for pel feedback");
    <?> atask;
    printf ("[atask] has ended based on the pel feedback");

    printf ("Check pel status");
    if ( atask<status> == "NERW_STATUS_ENDED" )
        printf ("[atask] has ended based on the status check");

    printf ("Inquire pel exception");
    try {
        atask<ENDED>;
    }
    catch ( atask<ENDED> ) {
        printf ("[atask] has ended from the pel ENDED exception");
    }
}
Wait for a pel feedback

The statement
<?> atask;
suspends the invoking task until atask either releases a value or has ended.

For tasks that do not release value, such as the ones in this chapter, the pel feedback wait is only broken if the tasks end. For tasks that keep releasing new values, the pel feedback wait is also broken when the tasks "stream" out new values. Thus the breakoff of the pel feedback wait by itself does not allow us to know if the tasks have ended or not. However the pel feedback operation allows the update operation to piggy-back, and the <status> property can be checked to see if the tasks have ended. This is what monitorTaskEnd does above.

Check the pel status

The statement
if ( atask<status> == "NERW_STATUS_ENDED" )
checks the <status> property that is locally cached. This property is updated from the last interaction with the task presented by the pel variable atask. This property may be stale since it does not represent the actual status of the task.

To get the current status, the monitoring task can invoke the update operation explicitly. In addition, all pel operations also piggy-back the update operation implicitly. The latter is what monitorTaskEnd makes use. It depends on the pel feedback operation to re-stock all the pel variable properties, including the <status> one.

Inquire a pel exception

The statement
atask<ENDED>;
sends a status request to the targeted pel, waits for the reply back, updates the local pel properties, and raises the ENDED exception if the returned status indicates that the task has ended. Thus this statement is normally run inside a try / catch construct, with a corresponding <ENDED> exception handler. If run outside a try / catch, or if there is no corresponding handler, an unhandled <ENDED> exception will cause the invoking task to abort.


Kill Statement

Let's go back to an earlier example where the main task issues a request for Consumer to terminate:
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store);

    <?>task_producer;
    <terminate>task_consumer;
}
Upon registering the request to the Consumer task, the terminate command is done, and main flows to the next statement. Since there is no more statement, the task main ends. However for the program to exit, the Producer and Consumer tasks must also have ended.

As discussed previously, the Consumer task can take its time to end, or even may not go through with the ending. If main wants to enforce the ending, it has to use the <kill> statement. For example, after issuing the terminate request, main can wait for 60 seconds. If it does not detect the ENDED status after that time frame, it issues the kill command:
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store);

    <?>task_producer;
    <terminate>task_consumer;
    sleep (60);      /* give Consumer 1 min to close graciously */
    <update>task_consumer;
    if ( task_consumer<status> != NERW_STATUS_ENDED ) {
        printf ("Task Consumer still has not closed after 1 min");
        <kill> task_consumer;
    }
    
}
The kill command is different from the terminate command in many ways:
  1. The kill command does not allow the targeted task to clean up. The kill signal is sent to the CHAOS runtime directly. CHAOS immediately ends the targeted task.
     
  2. While terminate does not wait for the task to be terminated, the kill command will suspend the calling task until CHAOS has reported back that the targeted task has ended.

Kill All Statement

The simple kill command only kills the specified task, not its children tasks. If the killing task wants to kill a task, its children tasks, and other subsequent descendant tasks, it should use the all attribute. For example:
<kill all> task_consumer;


Pel Abortion

There is another way for the task to end. The task can end via an abortion. This happens when there is a mel, pel or task exception, but the task code does not have a corresponding catch handler. For example:
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store);

    <?>task_producer;
    <terminate>task_consumer;
}
void Producer (<mel> int store) {
    while ( 1 ) {
        int c = produce();
        if ( c == 0 ) {
            <close>store;
            <end>;
        }
        <?>store = c;
    }
}
void Consumer (<mel> int store) {
    while ( 1 ) {
        consume (<?>store);
    }
}
We have removed the try / catch handler from the Consumer task. Therefore, when the Producer task closes the mel store, there is no handler at Consumer to process that exception. Likewise, there is no handler at Consumer to process the termination request from the main task.

Whenever the Consumer task accesses the CHAOS runtime environment for a mel or pel operation, it will check for any pending exception. If one is found and there is no corresponding handler. the task will end on the spot. This is called ending via task abortion.


Exit Statement

The NERWous C examples introduced so far have exited without any exit value. An exit value is required if the NERWous C program is used in a batch or a pipeline of interrelated programs, so that the result of its run can be monitored and dictates the rest of the processing.

C programs invoke the exit command to return an exit value. The corresponding NERWous C is the <exit> command. Let's modify the main task to use this command.
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store);

    <?>task_producer;
    <terminate>task_consumer;
    <exit> 0;
}
By popular convention in many operating systems, a zero exit value means that the program has run successfully. Any non-zero value, either positive or negative, means that the program execution has an issue.

The NERWous C <exit> statement first ends the task that calls it, then has the CHAOS runtime ends any task that is still running. Once all tasks have ended, the NERWous C <exit> calls the C language exit command to exit the program with the specified exit value.

How does the CHAOS runtime ends all the tasks? It uses either the kill command or the terminate command.

Exit Now Statement

The <exit> statement supports the now attribute to force the program to exit right away. It does that by killing any task still running, so that all tasks will end right away but abruptly:
<exit now> 0;

Exit ASAP Statement

The <exit> statement supports the asap attribute to allow the program to exit as soon as possible. It does that by terminating any task still running. Termination allows the tasks to close more gracefully, especially if they have a TERMED exception handler.
<exit asap> 0;
The exit with asap may not exit the program right away or ever, if a task, by design or not, chooses to ignore the termination request and continues to run.

The default behavior when no attribute is added, is the asap behavior. Thus, the following statements are equivalent:
<exit asp>;
<exit>;

Multiple Exit Statements

What happens if more than one task invokes the <exit> statement? Consider this example:
main () {
    <!> runthis();
    domain ();
    <exit> 0;
}

function runthis () {
    dothis();
    <exit> 1;
}
What is the exit value of the program? Is it 1 from runthis, or is it 0 from main? The answer is that it can be either way, depending on which <exit> statement is invoked last. The exit value of the program is the exit value of the last <exit> statement.

If main runs domain() fast enough, it will invoke its <exit> statement with the 0 exit value first, which causes it to end. Since the task runthis is still running, it will receive a terminate request. However since this task is not doing any mel or pel operation, it does not honor the terminate request. It finishes doing dothis and invokes its own <exit> statement with 1 as the exit value. This causes runthis to end. With all the tasks having ended, the program exits with the value of the last <exit> statement, which is 1 from runthis.

Using the same logic as above, if dothis run faster, and runthis issues the <exit> statement first, the program will exit with the 0 value from the main task.

ALthough supported, it is not recommended to have multiple <exit> statements in a NERWous C program. The children tasks should use <return> statements to send back the status of their run to the main task. The main task then invokes the <exit> statement:
main () {
    <pel> p = <!> runthis();
    domain ();
    <exit> <?>p;
}

function runthis () {
    <return> dothis();
}
The next chapter will discuss the <return> statement.


Previous Next Top

Pel Returns

Welcome » NERWous C » Pel
  1. Return Statement
  2. Release Statement
  3. Multi-Value Release
  4. Multi-Value Return


Return Statement

The return statement is used by a task to end itself with a return value. It is a combination of the <release> statement which does the value return, and the <end> statement which does the task ending. The <return> statement is the NERWous C embodiment of the C language return statement.

The return statement causes the ending task to generate a special return value, This return value is a readonly mel. By nature of a readonly mel, other tasks can all read this return value at the same time whenever it is made available, or wait for it if not yet available. By waiting on the return value of a task, the inquiring tasks can detect that the targeted task has ended, as the main task is doing below:
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store);
    
    <?>task_producer;
    <terminate>task_consumer;
}
void Producer (<mel> int store) {
    while ( 1 ) {
        int c = produce();
        if ( c == 0 ) {
            <end>store;
            <return>;
        }
        <?>store = c;
    }
}
void Consumer ( int store) {
   while ( 1 ) {
     try {
         consume (store);
     }
     catch (pel<TERMED>) {
         printf ("I'm being terminated");
         return;
     }
   }
}
The wait that the main task is doing on <?>task_producer is the same pel feedback wait we have encountered in the previous Pel Endings chapter. This wait will be broken if the task_producer has ended via the <return> statement as seen in the Producer function code above.

Since the return signature of the function Producer is a void, the return value of task_producer is indefinite. Inquiring tasks can wait on this indefinite value to detect that a task has ended, but should not make use of it for any assignment or consumption. When there is no real value to be returned, the <return> statement acts simlarly like the <end> statement. This allows the same pel feedback wait to serve both statements.

Let's change the Producer function so that it returns an actual value to make the use of the <return> statement worthwhile:
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store);
    
    printf ("Task Producer has ended with value [%d]", <?>task_producer);
    <terminate>task_consumer;
}
int Producer (<mel> int store) {
    while ( 1 ) {
    int c = produce();
        if ( c == 0 ) {
            <end>store;
            <return> c;
        }
        <?>store = c;
    }
}
The main task now waits for the Producer task to end, and also makes use of the return value (as seen in the printf function call).

Since the return value of a task is a readonly mel, multiple tasks can access it. For example, let's the Consumer task access the Producer return value too:
main () {
    <mel> int store;
    <pel> task_producer = <!> Producer (store);
    <pel> task_consumer = <!> Consumer (store, task_producer);
    
    printf ("Task Producer has ended with value [%d]", <?>task_producer);
    <?>task_consumer;
    printf ("Task Consumer has also ended");
}
int Producer (<mel> int store) {
    while ( 1 ) {
        int c = produce();
        if ( c == 0 ) {
            <close>store;
            <return> c;
        }
        <?>store = c;
    }
}
void Consumer (<mel> int store, <pel>tprod) {
    int c;
    while ( 1 ) {
      try {
        c = <?>(store || tprod);
        if ( c == 0 ) <end>;
        consume (c);
      }
      catch (store<CLOSED>) {
        printf ("I am ending due to a MEL CLOSURE");
        return;
      }
      catch (tprod<ENDED>) {
        printf ("I am ending due to the Producer having ended");
        return;
      }
    }
}
In this new version, the main task does not terminate the Consumer task any more. Instead, the latter will detect that the Producer has ended by accessing its return value, and then ends itself with its own <end> statement. With the statement <?>(store || tprod), the Consumer waits for either a value in the mel store, or the return value of the Producer task.

While both the main task and Consumer task wait on the return value of Producer, there is a difference between their wait. The wait from main is a single mel read wait while the wait from Consumer is a mel OR read wait. Normally Consumer waits on store being valued. However when Producer has ended, the store mel stops being valued; instead Consumer will pick up the return value of the ending Producer. When it sees the 0 value of the return value, the Consumer will explicitly ends with its own <end> statement.

Since the main does not invoke terminate on the Consumer task any more, the catch (pel<TERMED>) has been removed from the catch exception repertoire of the Consumer task. (It can be kept for reference but there is no event to trigger it.) It has been replaced by a new exception handling:
catch (tprod<ENDED>) {
    printf ("I am ending due to the Producer having ended");
    return;
}
There are now two methods for Consumer to detect that the Producer has ended: checking its return value via the pel feedback wait or getting the pel ENDED exception. With both methods present, the return value method takes precedence. The Consumer first determines if the return value is empty before it checks for any pel exception. In the Producer/Consumer example above, when the Producer ends, it generates a return value, causing the ENDED exception handler at Consumer not to be exercised.

Although playing a subservient role to the pel feedback wait, the ENDED exception handling is more general. It can handle the case where Producer has ended graciously with a proper return value, as well as the case where Producer has crashed without releasing any return value. As a matter of fact, we can remove the check for the 0-value (if ( c == 0) return) and make Consumer more succinct:
void Consumer (<mel> int store, <pel>tprod) {
    while ( 1 ) {
      try {
        consume ( <?>(store || tprod) );
      }
      catch (store<CLOSED>) {
        printf ("I am ending due to a MEL CLOSURE");
        return;
      }
      catch (tprod<ENDED>) {
        printf ("I am ending due to the Producer having ended");
        return;
      }
    }
}

Serial Return In Parallel

Sharp-eyed readers would notice that the exception handlers in the Consumer task use the C language return statement instead of the NERWous C <return> statement. This is done on purpose to illustrate that once a serial function is pelled to run in parallel, its return statement is transformed by the CHAOS runtime into the <return> statement. This allows simple functions to be invoked serially or in parallel, without having two versions:
int SimpleConsume (int c) {
    if ( !consume (c) ) return 1;    /* error from consume */
    return 0;    /* consume is successful */
}
int rescon;
rescon = SimpleConsume (10);    /* invoke serially */
rescon = <!> SimpleConsume (0);    /* pel to run in parallel */
In the second invocation, SimpleConsume runs as a pelled task. Its return statement then becomes <return> behind-the-scene. Without this automatic conversion, we would have to create a NERWous C version of SimpleConsume that used <return> in order to run it in parallel.

Parallel Return In Serial

Once a function makes use of the <return> statement, its invocation has unintended consequence when run serially:
int SimpleConsumeTask (int c) {
   if ( !consume (c) ) <return> 1;
   <return> 0;
}
main () {
    int rescon;
    rescon = <!> SimpleConsumeTask (0);    /* OK when pelled */
    rescon = SimpleConsumeTask (10);    /* Watch out when invoked */
}
The first invocation of <return> inside the task SimpleConsumeTask is what is intended - it ends this task and returns a value to the task main. The second invocation from a serial call to the function SimpleConsumeTask is done in the context of the main task, so when <return> is run in SimpleConsumeTask, it is not a return from that function, but a <return> from the main task, causing it to end.

Pel'N'Back Shortcut

This statement seems to be buggy:
rescon = <!> SimpleConsumeTask (0);
The left-hand-side of a pel <!> operator should be a pel variable, which rescon is not. However the above statement is all right since it is a shortcut of:

<pel> _p0 = <!> SimpleConsumeTask (0);
rescon = <?> _p0;
This shortcut is called pell'n'back because after pelling the task, the parent task immediately goes to the pel feedback wait. The required pel variable is not visible, it is created behind-the-scene, and is transient as a one-time use.

The pel'n'back shortcut is used when the code to be pelled has been written for task pelling, such as containing the <return> statement.

Return For Inline Code

Earlier, we see that return and <return> can be used interchangeably inside a pelled function. However if the pelled code is an inline block code, then only the NERWous C format of <return> is supported.

Let's put the Producer and Consumer functios as code blocks inside the main function:
main () {
    <mel> int store;
    
    /* Producer coded inline */
    <pel> task_producer = <!> {
        while ( 1 ) {
            int c = produce();
            if ( c == 0 ) {
               <end>store;
               <return inline> c;
            }
            <?>store = c;
        }
     }

    /* Consumer coded inline */
    <pel> task_consumer = <!> {
        while ( 1 ) {
          try {
             consume ( <?>(store || task_producer) );
          }
          catch (store<CLOSED>) {
             printf ("I am ending due to a MEL CLOSURE");
             <return>;
          }
          catch (task_producer<ENDED>) {
             printf ("I am ending due to the Producer having ended");
             <return>;
          }
        }
    }
        
    printf ("Task Producer has ended with value [%d]", <?>task_producer);
    <?>task_consumer;
    printf ("Task Consumer has also ended");
}
The serial format return are not permitted for the pelled inline codes because return works with functions and the inline code blocks are not functions.

The inline attribute used in the producer inline code is optional. It is a cosmetic reminder that the <return> statement is to be applied to the inline code block and not to the parent task (i.e. main). Since inline is cosmetic, the consumer inline code opts not to use it in its exception handlers. As a matter of style, it is recommended that the inline attribute should be used here as well.

The local variables store and task_producer are defined in the main task and passed over to the consumer inline task, as described in Local and Global Variables.


Release Statement

In the previous examples, we use the store mel variable to pass a product from the Producer task to the Consumer task. Another way is for the Producer to "stream" its products directly to the Consumer without using the store intermediary. This is supported by the NERWous C <release> statement:
main () {
    <pel> task_producer = <!> Producer ();
    <pel> task_consumer = <!> Consumer (task_producer);
    
    <?>task_producer<ENDED>;
    printf ("Task Producer has ended with value [%d]", <?>task_producer);
    <?>task_consumer<ENDED>;
    printf ("Task Consumer has ended");
}
int Producer () {
    while ( 1 ) {
        int c = produce();
        if ( c == 0 ) {
           <return> c;
        }
        <release> c;
    }
}
void Consumer (<pel>tprod) {
    try {
        consume (<?>tprod);
        <resume>;
    }
    catch (tprod<ENDED>) {
        printf ("I am ending due to the Producer having ended");
        return;
    }
}
The <release> statement writes to the same readonly mel return value used by the <return> statement. The difference is that <return> follows the mel write with a task ending, while the <release> lets the task resume processing. In the Producer code above, the task keeps invoking the release of newly produced items, until a zero-valued item is produced; at that time, the Producer task does a <return> on that last item.

The Consumer task gets newly produced items directly from the readonly mel return value of the Producer task. Instead of checking every time for a zero-valued item, it depends on the ENDED exception to know that the Producer task has ended.

The main task has a major change in how it detects that Producer has ended. Previously, it waits on the readonly mel return value of Producer since the latter only writes to this location once, at its end time. Now with this return value used as a streaming channel, main has to wait for Producer to have the ENDED status:
<?>task_producer<ENDED>;
The main task could still use the checking for the mel return value to detect the ending of Consumer since it only does a <return> and no <release>. However we also change the code for main to wait for the ENDED status of the Consumer task, to be consistent with the Producer detection.

The main task does the wait on the tasks ENDED status because it needs to do the printf statements. If it does not have to do printf, then there is no need for the explicit wait statements, and the main code would be simplified to:
main () {
    <pel> task_producer = <!> Producer ();
    <pel> task_consumer = <!> Consumer (task_producer);
}
After the two pel statements, the main task ends. When the Producer and Consumer tasks also end, the whole program exits.


Multi-Value Release

NERWous C supports a feature that is not part of the NERW Reference Model but can make writing some concurrent and serial programs easier: the capability to return multiple values. As we all know, the classic C language allows only one value to be returned.

Let's modify our example so that the Producer task produces one premium product for every 5 standard products, until over 1000 items are produced. We will have two consumer tasks, one to consume the premium products, and the other, the standard products:
main () {
    <pel> task_producer = <!> Producer ();
    <pel> task_consumer_std = <!> Consumer_Std (task_producer);
    <pel> task_consumer_prem = <!> Consumer_Prem (task_producer);
    
    <?>task_producer<ENDED>;
    printf ("Task Producer has ended with standard value [%d] and premium value [%d]", 
             <?>task_producer.std, <?>task_producer.prem);
    <?>task_consumer_std;
    <?>task_consumer_prem;
    printf ("Both Consumer tasks have ended");
}

(int prem, int std) Producer () {
    int count = 0;
    while ( count < 1000 ) {
        for (int i=0; i<5; ++i)
            <release name=std> produce_standard();
        <release prem> produce_premium();
        count += 6;
    }
    <release> (0, 0);
}
void Consumer_Std (<pel>tprod) {
    try {
        consume_std (<? name="std">tprod);
        <resume>;
    }
    catch (tprod<ENDED>) {
        printf ("I am ending due to the Producer having ended");
        return;
    }
}
void Consumer_Prem (<pel>tprod) {
    try {
        consume_prem (<? prem>tprod);
        <resume>;
    }
    catch (tprod<ENDED>) {
        printf ("I am ending due to the Producer having ended");
        return;
    }
}
The Producer now returns two readonly mel values, prem and std. These names are declared in the Producer function signature. The Producer task can release individual values by specifying their name, such as in:
<release name=std> produce_standard();
The name attribute can be omitted, as in:
<release prem> produce_premium();
When all the return values are specified, the <release> statement does not require the name attribute, as in:
<release> (0, 0);
Each Consumer task can wait for its own preferred product by specifying the name of the readonly mel value, as in consume_std (<? name=std>tprod) or consume_prem (<? prem>tprod).


Multi-Value Return

In the above example, the Producer task does a <release> with all the readonly mel values as the last statement in the function code block. Since it runs out of code to run, it will end. The equivalent is to use the <return> statement to signal the intention to terminate:
(int prem, int std) Producer () {
    int count = 0;
    while ( count < 1000 ) {
        for (int i=0; i<5; ++i)
            <release name=std> produce_standard();
        <release prem> produce_premium();
        count += 6;
    }
    <return> (0, 0);
}
Unlike the <release> statement, all the returned values of the function must be specified in a <return> statement. Any missing value will result in a syntax error during NERWous C translation time.

The multi-value feature is only supported for the NERWous C <return> statement. NERWous C leave it open for an implementation to extend that feature to standard C language <return> statement.


Previous Next Top

Wednesday, July 5, 2017

Pel Loops

Welcome » NERWous C » Pel
  1. Pel A Loop
  2. Loop Of Pels
  3. Loop Of Synchronous Pels
  4. Synchronous Loop Of Pels
  5. Pel A Loop Of Pels


Pel A Loop

In the following example, we will pel two tasks to run two serial while loops:
main () {
    <mel buffer=10> int store;
    <cel> fastcell;

    <! at=fastcell> {   // Producer inline task
        while ( 1 ) {   // to run this while loop in serial
            int c = produce();
            <?>store = c;
            if ( c == 0 ) <end>;
        }
    }

    <!> while ( 1 ) {    // Consumer inline task
        int c = <?>store;
        if ( c == 0 ) <end>;
        consume (c);
    }
}
There are 3 tasks in the above program. The main task pels a task to do production, and another task to do consumption. The while loops inside the pelled tasks are run serially, with one item produced at a time, and one item consumed at a time. The mel store is the communication channel between the two tasks. It has a buffer to allow the producer, which runs at a faster cel node (via the fastcel at attribute), to minimize waiting for the intermittently slower consumer task to catch up.

The consumer task uses the abbreviated form of the pel statement. It omits the enclosing { and } brackets of the pel statement <!> since there is only one statement (the while loop) inside the pel code block.

The producer task uses the standard form of the pel statement with both encapsulating { and } brackets. Although more verbose, it can support other statements before and after the while loop. For example, for monitoring purpose, we can include some sandwiching printf statements:
<! at=fastcel> {
    printf ("Before while loop\n");
    while ( 1 ) {    // to run this while loop in serial
        int c = produce();
        <?>store = c;
        if ( c == 0 ) <end>;
    }
    printf ("After while loop\n");
 }

We can replace the while loop with a do-while loop:
main () {
    <mel buffer=10> int store;
    <cel> fastcell;

    <! at=fastcel> {   // Producer inline task
        printf ("Before do-while loop\n");
        do {
            int c = produce();
            <?>store = c;
            if ( c == 0 ) <end>;
        }  while ( 1 );
        printf ("After do-while loop\n");
    }

    <!> do {    // Consumer inline task
        int c = <?>store;
        if ( c == 0 ) <end>;
        consume (c);
    }  while ( 1 );
}
The above example uses the <end> statement to break out of the do-while loops. This statement also ends the inline task. For the Consumer task, both activities are the same, since there is nothing else Consumer does after breaking out of the loop. For the Producer task, the <end> statement prevents the execution of the closing printf. The remedy is to use the C language's break statement:
    <! at=fastcel> {   // Producer inline task
        printf ("Before do-while loop\n");
        do {
            int c = produce();
            <?>store = c;
            if ( c == 0 ) break;
        }  while ( 1 );
        printf ("After do-while loop\n");
    }
After break'ing, the Producer task does the printf, and without any other activity programmed, the task just ends.


Loop Of Pels

Unlike the pel-a-loop method which pels a single task, the loop-of- pels method is a serial loop that pels a new task with each iteration. The number of tasks created is equal to the number of loop iterations. While the tasks are created one at a time, the loop iterations usually run fairly fast on modern machines, which gives the impression that the tasks are being created at the same time. Once the tasks are created, they run in parallel with one another.

Do-While Loops

Let's modify our do-while example to have the producing tasks producing exactly 3 items, to be stored separately, using 3 different producers. We also have 3 different consumers:
#define NUMITEMS 3
main () {
    <mel> int stores[NUMITEMS];
    <cel> producer[NUMITEMS] = { {"Producer 1"}, {"Producer 2"}, {"Producer 3"} };
    <cel> consumer[NUMITEMS] = { {"Consumer 1"}, {"Consumer 2"}, {"Consumer 3"} };

    int n = 0;
    do {
        <! at=producer[n]> {
            <?>stores[n] = produce();
        }
    } while ( ++n < NUMITEMS );    

    n = 0;
    do <! at=consumer[n]> {
       consume (<?>stores[n]);
    } while ( ++n < NUMITEMS );
}
To separate the products, we use the mel array stores. Each producer is represented by its own cel element.

The main task finishes the do-while loop for production before starting the do-while loop for consumption. Unless the runtime primitive to create a task is very demanding, both loops can be ripped through quickly. In a blink of time, we can have 7 tasks running: the main task, 3 producing tasks and 3 consuming tasks.

Like the producing tasks, the consuming tasks are assigned to specific cels, but for consumption. A consuming task may be already running while the product it is supposed to consume, is still being produced by the corresponding producing task. In this case, the consuming task will suspend itself at the mel wait, <?>stores[n], for the product to show up.

The local variable n is an interesting fellow. As discussed in the chapter on local variables, there are in total 6 instances of n in the above example. The instances belonging to the main task are shown in red below:
   int n = 0;
    do {
        <! at=producer[n]> {
            <?>stores[n] = produce();
        }
    } while ( ++n < NUMITEMS );    

    n = 0;
    do <! at=consumer[n]> {
       consume (<?>stores[n]);
    } while ( ++n < NUMITEMS );
On the other hand, the instance of n in:
<?>stores[n] = produce();
belongs to the producer inline tasks. Since there are 3 producing tasks, there are 3 different instances of n, each localized to a particular task. All these localized instances get their initial value from the instance in the main task at the time of the pelling.

Likewise, we have 3 instances of n for the consuming tasks. If we were to change n within a task, like:
   do <! at=consumer[n]> {
       consume (<?>stores[n]);
       n = 0;
    } while ( ++n < NUMITEMS );
that change would only be applicable to the instance n of that consuming task. It would not affect the instance of n in the while statement which belongs to the main task.

In the above examples, the producing tasks use the standard form of a loop of pels, while the the consuming tasks use the abbreviated form. The standard form allows codes to sandwich the task pelling, and run in the context of the pelling task. For example:
   do {
        printf ("Begin creating task for [%d]\n", n);
        <! at=producer[n]> {
            <?>stores[n] = produce();
        }
        printf ("Request for task [%d] submitted\n", n);
    } while ( ++n < NUMITEMS );    

While Loops

Let's now replace the do-while loops with while loops.
#define NUMITEMS 3
main () {
    <mel> int stores[NUMITEMS];
    <cel> producer[NUMITEMS] = { {"Producer 1"}, {"Producer 2"}, {"Producer 3"} };
    <cel> consumer[NUMITEMS] = { {"Consumer 1"}, {"Consumer 2"}, {"Consumer 3"} };

    int n = 0;
    while ( ++n <= NUMITEMS ) {
        printf ("Begin creating task for [%d]\n", n);
        <! at=producer[n]> {
            <?>stores[n] = produce();
        }
        printf ("Request for task [%d] submitted\n", n);
    }    

    n = 0;
    while ( ++n <= NUMITEMS )
    <! at=consumer[n]> {
       consume (<?>stores[n]);
    }
}
The post-pel printf statement says "Request for task [%d] submitted" -- "submitted" and not "created", because the pel statement is asynchronous. As long as the request to create a task has been accepted by the CHAOS runtime, the main task can return from the pel statement to iterate the while loop. The submitted task will be created in due time by CHAOS.

For Loops

Let's now replace the while loops with for loops.
#define NUMITEMS 3
main () {
    <mel> int stores[NUMITEMS];
    <cel> producer[NUMITEMS] = { {"Producer 1"}, {"Producer 2"}, {"Producer 3"} };
    <cel> consumer[NUMITEMS] = { {"Consumer 1"}, {"Consumer 2"}, {"Consumer 3"} };
 
    for ( int n = 0; n < NUMITEMS; ++n ) {
        printf ("Begin creating task for [%d]\n", n);
        <! at=producer[n]> {
            <?>stores[n] = produce();
        }
        printf ("Request for task [%d] submitted\n", n);
    }   

    for ( int n = 0; n < NUMITEMS; ++n )
    <! at=consumer[n]> {
       consume (<?>stores[n]);
    }
}
Again, the producer task uses the standard form and the consumer task, the abbreviated form for loops of pels.


Loop Of Synchronous Pels

The previous loop examples pel the task asynchronously. Once the pel requests have been accepted by the CHAOS runtime, the looping task can resume its looping endeavor without waiting for the pelled tasks to have been actually created. We now change the for loop example to use synchronous task creation method to allow the looping task to make sure that the pelled tasks have been actually created.
#define NUMITEMS 3
main () {
    <mel> int stores[NUMITEMS];
    <pel> prods[NUMITEMS];
    <pel> conss[NUMITEMS];
    <cel> producer[NUMITEMS] = { {"Producer 1"}, {"Producer 2"}, {"Producer 3"} };
    <cel> consumer[NUMITEMS] = { {"Consumer 1"}, {"Consumer 2"}, {"Consumer 3"} };

    /* Producer inline block */

    int tries;
    for ( int n = 0; n < NUMITEMS; ++n ) {
        printf ("Begin creating Producer task [%d]\n", n);
 
        tries = 0;
        prods[n] = <! at=producer[n] timeout> {
            <?>stores[n] = produce();
        }
        if ( prods[n]<status> == NERW_STATUS_FAILED ) {
            printf ("EXIT LOOP - Task [%d] failed to be created due to [%s]\n", 
                n, prods[n]<why>);
            <close> stores;
            break;
        }
        else if ( prods[n]<status> == NERW_STATUS_TIMEOUT ) {
            if ( ++tries < 2 ) {
                printf ("RETRY after 1st TIMEOUT\n");
                <resume>;
            }
            else {
                printf ("STOP TRYING after 2nd TIMEOUT\n");
                continue;
            }
        }
            
        printf ("End creating Producer task for [%d]\n", n);
    }
    
    /* Consumer inline block */
    
    for ( int n = 0; n < NUMITEMS; ++n ) {
        conss[n] = <! at=consumer[n]> {
            try {
                int tries = 0;
                consume (<?>stores[n]);
            }
            catch ( stores[n]<CLOSED> ) {
                printf ("STOP TRYING -- The producer for [%d] item may have FAILED", n);
                <end>;
            }
            catch ( stores[n]<TIMEOUT> ) {
                if ( ++tries < 2 ) {
                    printf ("RETRY after 1st TIMEOUT\n");
                    <resume>;
                }
                else {
                    printf ("STOP TRYING after 2nd TIMEOUT\n");
                    <end>;
                }
            }
        }
        if ( conss[n]<status> == NERW_STATUS_FAILED ) {
            printf ("Consumer task [%d] failed to be created due to [%s]\n", 
                n, conss[n]<why>);
            continue;
        }
    }
}
For the producer tasks, an array of pel elements, <pel> prods[NUMITEMS], are declared. They are used to receive the result of the pel statement executions:
prods[n] = <! at=producer[n] timeout> { ... }
By assigning a pel variable to a pel creation statement, the main task indicates that it is willing to wait to make sure that its request for the new task has been fulfilled (successfully or not). Via this synchronizing handshake, the main task can check for the two errors that could possibly happen during a task pelling: a failure error and a timeout error. These errors are detected by checking the <status> property in which CHAOS reports the outcome of the pelled task creation.

NERW_STATUS_FAILED

This status means that CHAOS has not been able to create the task. The main task decides to abort any further attempt to pel the rest of the tasks. It closes the mel stores, then invokes the C statement break to immediately get out of the serial for loop.

What happens to the producing tasks that the main task has been able to create before issuing the break statement? If they have produce'd a value to their stores[n] mel item, these values will be lost when main issues the <close> store command. At this time, main still runs the for loop for the producing tasks, and have not started the for loop for the consuming tasks; therefore there are no tasks to consume these produced values.

For producing tasks that have been created before the break statements but have not valued their stores[n], they continue to produce. However, when they try to deposit the produced item to stores[n], they will catch a mel CLOSED exception. For simplicity, we have not programmed in a try / catch handling for the producing tasks. These tasks then end with an abortion on the unhandled CLOSED exceptions.

NERW_STATUS_TIMEOUT

For the producing tasks, the main task handles the TIMEOUT status differently than the FAILED status. First it tries to wait some more. The <resume> statement jumps the processing flow back to the latest mel or pel statement, which in this case, is the pel creation statement, allowing the main task to repeat the task pelling:
<! at=producer[n] timeout>
The timeout attribute without any value means that the main task is willing to wait the default amount of milliseconds before abandoning the wait for the pel creation. At that time, the CHAOS runtime also aborts any pel creation work even if it is already partially successful.

On timeout of the 2nd retry, the main task aborts the attempt to pel the task, and continues its iteration to pel the next task. While the NERWous C <resume> statement jumps the processing back to the pel statement <! statement, the C language continue jumps the processing all the way to the for loop statement.

Consumer Tasks

How does a consuming task know if its corresponding producing task has been created or not? It does not care about this knowledge. What the consuming task cares is to access the mel stores[n] for a reading. If the reading times out, then the consuming task runs its own TIMEOUT exception handler. The TIMEOUT exception may be due to the slowness of the CHAOS runtime at that moment, or to the slowness of the corresponding producing task to generate a value, or to the producing task not being able to be created on the first place, such as it has "hung".

The "tries" Local Variables

Note that there are two tries local variables mentioned in the above example. They are separate and unrelated. The first one belongs to the main task, and is used for the pelling of the producing tasks. The second one is in the context of each consuming task, and is used in the access of the mel variable stores[n]. Thus, there are NUMITEMS + 1 instances of the tries variable when this program runs: one for main, and one for each of the NUMITEMS consuming tasks.


Synchronous Loop Of Pels

In the previous example, we have the main task waits for the pel tasks to be created before it continues. Now we will extend this wait so that main will wait for all the tasks to be actually finished before it continues:
#define NUMITEMS 3
main () {
    <mel> int stores[NUMITEMS];
    <pel> prods[NUMITEMS];
    <pel> conss[NUMITEMS];
    <cel> producer[NUMITEMS] = { {"Producer 1"}, {"Producer 2"}, {"Producer 3"} };
    <cel> consumer[NUMITEMS] = { {"Consumer 1"}, {"Consumer 2"}, {"Consumer 3"} };

    /* Producer inline block */

    int tries;
    <collect> for ( int n = 0; n < NUMITEMS; ++n ) {
        printf ("Begin creating Producing task [%d]\n", n);
 
        tries = 0;
        prods[n] = <! at=producer[n] timeout> {
            <?>stores[n] = produce();
        }
        if ( prods[n]<status> == NERW_STATUS_FAILED ) {
            printf ("EXIT LOOP - Task [%d] failed to be created due to [%s]\n", 
                n, prods[n]<why>);
            <close> stores;
            break;
        }
        else if ( prods[n]<status> == NERW_STATUS_TIMEOUT ) {
            if ( ++tries < 2 ) {
                printf ("RETRY after 1st TIMEOUT\n");
                <resume>;
            }
            else {
                printf ("STOP TRYING after 2nd TIMEOUT\n");
                continue;
            }
        }
            
        printf ("End creating Producing task for [%d]\n", n);
    } <? ENDED>;
    printf ("All the Producing tasks have ended\n");
    
    /* Consumer inline block */
    
    <collect> for ( int n = 0; n < NUMITEMS; ++n ) {
        conss[n] = <! at=consumer[n]> {
            try {
                int tries = 0;
                consume (<?>stores[n]);
            }
            catch ( stores[n]<CLOSED> ) {
                printf ("STOP TRYING -- The producer for [%d] item may have FAILED", n);
                <end>;
            }
            catch ( stores[n]<TIMEOUT> ) {
                if ( ++tries < 2 ) {
                    printf ("RETRY after 1st TIMEOUT\n");
                    <resume>;
                }
                else {
                    printf ("STOP TRYING after 2nd TIMEOUT\n");
                    <end>;
            }
        }
        if ( conss[n]<status> == NERW_STATUS_FAILED ) {
            printf ("Consuming task [%d] failed to be created due to [%s]\n", 
                n, conss[n]<why>);
            continue;
        }
    } <? ENDED>;
}
By using the wrapper construct <collect> { ... } <? ENDED>, the main task asks the CHAOS runtime to monitor all the tasks pelled between the wrapper. The main task will wait at the <? ENDED> until all the collected tasks have ended.

In the above example, the parallelism is more muted than in previous examples. There are no producing tasks running in parallel with any consuming tasks. All the inline producing tasks are pelled then run to completion before the main task pels the inline consuming tasks. The above example still has more parallelism than a pure serial program -- the produce and consume funtions are executed in parallel via their corresponding for loop of pels.


Pel A Loop Of Pels

In the previous section, we have the producing tasks not running in parallel with the consuming tasks because we want to collect the status of all the producing runs before letting the consuming tasks run. Let us modify that example to have these tasks run in parallel again, while keeping the status collection. We'll do this by combining the pel-a-loop method with the loop-of-pels method:
#define NUMITEMS 3
main () {
    <mel> int stores[NUMITEMS];
    <pel> prods[NUMITEMS];
    <pel> conss[NUMITEMS];
    <cel> producer[NUMITEMS] = { {"Producer 1"}, {"Producer 2"}, {"Producer 3"} };
    <cel> consumer[NUMITEMS] = { {"Consumer 1"}, {"Consumer 2"}, {"Consumer 3"} };

    /* Producer inline block */

    int tries;
    <?> {
        <collect> for ( int n = 0; n < NUMITEMS; ++n ) {
            printf ("Begin creating Producing task [%d]\n", n);
 
            tries = 0;
            prods[n] = <! at=producer[n] timeout> {
                <?>stores[n] = produce();
            }
            if ( prods[n]<status> == NERW_STATUS_FAILED ) {
                printf ("EXIT LOOP - Task [%d] failed to be created due to [%s]\n", 
                    n, prods[n]<why>);
                <close> stores;
                break;
            }
            else if ( prods[n]<status> == NERW_STATUS_TIMEOUT ) {
                if ( ++tries < 2 ) {
                    printf ("RETRY after 1st TIMEOUT\n");
                    <resume>;
                }
                else {
                    printf ("STOP TRYING after 2nd TIMEOUT\n");
                    continue;
                }
            }

            printf ("End creating Producing task for [%d]\n", n);
        } <? ENDED>;
        printf ("All the Producing tasks have ended\n");
     }
     
    /* Consumer inline block */
    
    <!> {
        <collect> for ( int n = 0; n < NUMITEMS; ++n ) {
            conss[n] = <! at=consumer[n]> {
                try {
                    int tries = 0;
                    consume (<?>stores[n]);
                }
                catch ( stores[n]<CLOSED> ) {
                    printf ("STOP TRYING -- The producer for [%d] item may have FAILED", n);
                    <end>;
                }
                catch ( stores[n]<TIMEOUT> ) {
                    if ( ++tries < 2 ) {
                        printf ("RETRY after 1st TIMEOUT\n");
                        <resume>;
                    }
                    else {
                        printf ("STOP TRYING after 2nd TIMEOUT\n");
                        <end>;
                }
             }
             if ( conss[n]<status> == NERW_STATUS_FAILED ) {
                 printf ("Consuming task [%d] failed to be created due to [%s]\n", 
                    n, conss[n]<why>);
                 continue;
             }
        } <? ENDED>;
    }
}
The number of tasks that are created is as follows. First the main task is created. It then pels two pel-a-loop tasks, one for production, the other for consumption. Once the pellings are done, the main task ends.

The pel-a-loop task for production then pels NUMITEMS tasks, one for each iteration of its for loop. The pel-a-loop task for consumption does the same, and its NUMITEMS loop-of-pels tasks run in parallel with those from the production side. Once they have pelled all of their tasks, the pel-a-loop tasks (one for production, one for onsumption) wait for their own pelled tasks to finish via the wait statement <? ENDED>. These waits are done in parallel because they are done by concurrent tasks.

Once all the tasks pelled from the for loops have ended, the pel-a-loop parent tasks are woken up from their <? ENDED> waits, do their post-for-loop processing if any, then end. Their endings allow the NERWous C program to exit.


Previous Next Top