Pages

Tuesday, March 1, 2016

Mel Queues

Welcome » NERWous C » Mel
  1. Producer / Consumer
  2. Mel In Use
  3. Mel Termination
  4. Mel Operations
  5. Mel Properties


A Multi-Slotted Mel Example

To increase the concurrency between the Producer and Consumer from the previous example, we can increase the size of the mel variable:
main () {
     <mel size=100> int store;
     <pel> Producer (store);
     <pel> Consumer (store);
}
void Producer (<mel> int store) {
     int c;
     while ( c = Produce() )
         <?>store = c;

     <?>store = 0;
}
void Consumer (<mel> int store) {
     int c;
     while ( c = <?>store )
         Consume(c);
}
A faster Producer has to fill up the 100-slot buffer before it waits for the Consumer at the <?> construct.

The multi-slotted mel is a First-In-First-Out (FIFO) queue. When the Consumer looks up the store, it will pull the earliest produced item from this mel queue and assigns it to the local c variable. A faster Consumer has to consume all the items in the mel queue before it waits for the Producer at the <?> construct. (This statement is not 100% correct since the Producer will lock the mel queue when it deposits a new item, so there can be a small mutual exclusion wait for the Consumer if there is a contention.)

The FIFO nature of the mel queue is strictly enforced by the NERW Model. There is no construct to get the second or any subsequent item from the mel queue before the first one.


Run-Time Mel Queue Size

The NERW <mel> construct is a run-time operation that creates the mel during the execution of the program. It is not a static compile-time entity. Thus it is possible to specify the size of a mel queue using variables:
     int mysize = 100;
     <mel size=mysize> int store1;

     const DEFAULTSIZE = 100;
     <mel size=DEFAULTSIZE> int store2;
The size keyword can be omitted if it is the only attribute specified:
     <mel 100> int store;

     int mysize = 100;
     <mel mysize> int store1;

     const DEFAULTSIZE = 100;
     <mel DEFAULTSIZE> int store2;
There are other attributes for a mel entity besides size. Whenever these are used, the size keyword is required to differentiate between various attribute values.


Mel Queue Size Value

The size of a mel queue can be read:
     <mel 100> int store;
     int mysize = store<size>;  // mysize is equal to 100
Getting the size of a mel queue is a non-blocking operation. Thus there is no <?> wait construct, unlike getting a value from a mel queue which can block.


Mel Queue Resize

This operation may be very expensive or not supported at all on a particular platform; however the NERW Model allows a mel queue to change size during execution via the <resize> operation:
     <mel 100> int store;  // create a mel queue with 100 slots
     <resize 200> store;  // change to 200 slots, values in the first 100 slots are copied over
     <resize 50> store;  // change to 50 slots, values in the first 50 slots are retained
     <resize size+50> store;  // change to 100 slots, values in the last 50 slots are undefined
     <resize size-100> store;  // the mel queue becomes a mel variable with a single slot
     <resize size-50> store;  // the mel variable remains a mel variable with a single slot
     int mysize = 100;      // variable value
     <resize size+mysize> store;  // the mel variable becomes a mel queue with 100 slots (not 101!)
     int size = 75;
     <resize size+size> store;  // double the size of the mel queue to 200 - size is a keyword
The size of a mel entity cannot be 0 or negative. The minimum size is 1, which makes a mel queue a simple mel variable.


Previous Next Top

No comments:

Post a Comment