Pages

Tuesday, March 1, 2016

Mel Closures

Welcome » NERWous C » Mel
  1. Closing A Mel
  2. Multiple Mel Closures
  3. Closing At Task End


Closing A Mel

In the Producer / Consumer previous example, we use the value 0 to allow the Producer to indicate that it is done. The more generic way is for the Producer to close the mel element:
main () {
     <mel buffer=100> int store;
     <!> Producer (store);
     <!> Consumer (store);
}
void Producer (<mel> int store) {
     int c;
     while ( c = Produce() )
         <?>store = c;

     <close>store;
}
void Consumer (<mel> int store) {
     try {
         while ( 1 )
             Consume(<?>store);
     } catch ( store<CLOSED> ) {
         return;
     }
}
The Producer invokes the <close> operation on its resident mel variable store. The CHAOS runtime receives this request, puts the Producer job on hold, retrieves the CORE properties from the mel variable, reaches out to the remote mel element now identified, marks it as closed, and frees the Producer job to resume processing. To limit the requesting job from waiting too long, the CHAOS runtime will garbage-collect any released resources from the closed mel element at an opportune time.

Once a mel element is closed, any attempt to write to it or read from it will result in the CLOSED exception. This is what happens to the Consumer task when it attempts to read from the mel element store. The Consumer catches the exception and does a return. Since the Consumer is run under a task, a return will end the task.


Multiple Mel Closures

The Consumer can also close the mel element. For example, we would like our Consumer to consume up to 1000 items and no more. When this threshold is reached, the Consumer will close the mel element store to inform the Producer to stop producing.
main () {
     <mel> int store;
     <!> Producer (store);
     <!> Consumer (store);
}
void Producer (<mel> int store) {
     int c;
     try {
         while ( c = Produce() )      // Produce until 0
             <?>store = c;

         <close>store;
     }
     catch ( store<CLOSED> ) {   // Or until the Consumer stops consuming
         return;
     }
}
void Consumer (<mel> int store) {
     int upto = 1000;
     try {
         while ( --upto >= 0 )
             Consume(<?>store);   // Consume until upto is reached

         <close>store;
     }
     catch ( store<CLOSED> ) {    // Or until the Producer stops producing
         return;
     }
}
If a mel element is already closed, apply another <close> operation to it will be silently ignored.


Closing At Task End

There is no automatic mel closing when the task that requests its creation, ends. When a mel element is created, its belongs to the whole NERWous C program, and not to a task. The only thing that the task has control of, is the resident mel variable that represents its access to the remote mel element. This mel variable will fall out of scope when the task ends. If a task wants to close the mel element when it ends, it has to specifically invoke the <close> operation.

When the NERWous C program exits, all mel elements are automatically closed. The CHAOS runtime then collects all the released resources for another program.


Previous Next Top

No comments:

Post a Comment