Pages

Thursday, March 3, 2016

Mel Timeouts

Welcome » NERWous C » Mel
  1. Mel Waits with Timeouts
  2. Run-Time Timeouts
  3. Mel Default Timeout
  4. Mel Timeout Settings
  5. Mel Timeout Special Values
  6. Mel Timeout Property


Mel Waits with Timeouts

In the Producer / Consumer previous example, the Consumer task will wait at the <?>store statement until a product shows up or a CLOSED exception happens. If the Producer task is slow, then this wait time is wasted by the Consumer, especially if it can do something else meaningful. We can mitigate this waste by having a timeout in the mel wait:
void Consumer (<mel> int store) {
     while ( 1 ) {
         try {
             Consume(<? timeout=10>store);
         }
         catch ( store<TIMEDOUT> ) {
             doSomethingElse();
             continue;
         }
         catch ( store<CLOSED> ) {
             break;
         }
     }
}
Here, the Consumer is willing to wait only 10 milliseconds. If no product shows up, the store<TIMEDOUT> exception is raised, and the Consumer can doSomethingElse. Once this is done, it will go back and check on the mel store again. This cycle repeats until the mel store is closed (by the Producer in our example).


Run-Time Timeouts

The timeout value can be a variable that changes at each mel wait iteration. For example, the following Consumer would like to adjust its timeout value to try to match the production rate of the Producer.
void Consumer (<mel> int store) {
     int waitfor = 10;
     while ( 1 ) {
         try {
             Consume(<? timeout=waitfor>store);
         }
         catch ( store<TIMEDOUT> ) {
             waitfor += 5;
             continue;
         }
         catch ( store<CLOSED> ) {
             break;
         }
     }
}
The Consumer starts with a 10 msec timeout. If this is not sufficient, and the store<TIMEDOUT> exception occurs, it raises the timeout another 5 msec for the next <?>store mel wait.


Mel Default Timeout

A default timeout can be specified for any task who wants to do a mel wait with a timeout but does not need to specify the value of that timeout. NERWous C does not define where the default timeout value is defined. It is left as an implementation issue. The default timeout is invoked in the mel wait with the keyword timeout without a value:
     <? timeout>store;
The default timeout is convenient when a program needs only two types of mel element access: one that "waits forever", and one that "waits up to certain amount of time".


Mel Timeout Settings

Timeout settings can be done using calculations. When calculating a subsequent timeout, the keyword timeout stands for the current timeout, not the default timeout as in the original setting. Any calculation resulting in a negative timeout will negate the timeout (i.e. timeout is 0).

In the example below, let's assume that the default timeout is 5.
    int n = 10;
    <? timeout>store;    // access with default timeout 5 msec
    <? timeout=n>store;    // access with timeout 10 msec
    <? timeout+n>store;    // access with current timeout plus 10, thus 20 msec
    <? timeout+timeout>store;    // access with double the current timeout, thus 40 msec
    <? timeout-100>store;    // access with no timeout since 40-100 is negative
    <? timeout=-100>store;    // still no timeout due to negative value assignment
    <? timeout+8>store;    // access with timeout 8 msec since current timeout is 0
    int timeout = 20;
    <? timeout+timeout>store;    // access with double the current timeout, thus 16 msec, not 40 msec
The keyword timeout represents the current timeout within the < and > NERW markers. Thus the C variable timeout is ignored, while the variable n is accepted.


Mel Timeout Special Values

Besides the default timeout and explicit numerical settings, there are special pre-defined values for timeouts:

ValueSynopsis
NERW_TIME_NONEThis is the same as the value of 0. It indicates the mel operation is requested without a timeout, and the requesting task is willing to wait until the operation either succeeds or fails.
NERW_TIME_REMAINThis value is used if a task wants to take back an asynchronous mel request from the CHAOS runtime, and makes use of the remaining timeout, instead of starting a new timeout or no timeout.

The above values are defined in the file nerw.h that is included in all NERWous C programs. (For brevity, this include file is omitted in most examples.)


Mel Timeout Property

After a successful mel access, a program can check the timeout used via the <timeout> mel property.
     <? timeout>store;
     int deftimeout = store<timeout>;
     printf ("The default timeout setting is %d", deftimeout );
     <? timeout+20>store;
     printf ("The last access has default timeout + 20 or %d", store<timeout>);
     <?>store;
     printf ("Last access is without timeout, so value %d must be 0", store<timeout>);
Unlike the <value> property or the <buffer> property, the <timeout> property is not associated with the remote mel element. It is part of the IN Properties which belong solely to the task.

The <timeout> property is never stale since it always reflect the timeout setting to the last access (read or write) from the task to the remote mel element. (If the access is without timeout, the <timeout> property is reset to 0.) It is also not shared between tasks -- for each mel access, each task has its own <timeout> property inside its own mel variable.


Previous Next Top

No comments:

Post a Comment