next up previous contents
Next: Join Continuations Up: Advanced Concurrency Coordination Previous: Advanced Concurrency Coordination   Contents


Named Tokens

Chapter 3 has introduced token continuations with the reserved keyword token. In this section, we will focus on the other type of token continuations, the named tokens.

In SALSA, the return value of the asynchronous message can be declared as a variable with type token. The variable is called the named token. Named tokens are designed to tell the SALSA run-time environment that how the continuations work.

Named tokens may be assigned to non-primitive type values, message sending expressions, or other named tokens. Examples are shown as follows:
 
1. token y  = a<-m1();
2.
3. token z = y;
4.
5. y = b<-m2(y);
6. self<-m()@c<-m3(token, z, y);
 
   
The following example shows how to use named tokens. Lines 1-2 are equivalent to lines 3-5, and lines 1-2 uses a few token declarations, as follows:
 
//lines 1-2 are equivalent to lines 3-5
1. token x  = a<-m1();
2. x = b<-m2(x);

3. token x = a<-m1(); 4. token y = b<-m2(x); 5. x = y;

The following example demonstrates how named tokens are used in loops, as follows:
 
1. token x  = a<-m1();
2. for (int i = 0; i < 10; i++) x = b<-m2(x, i);
 
   
The above example is equivalent to the following example:
 
a<-m1() @ 
b<-m2(token, 0) @ 
b<-m2(token, 1) @ 
b<-m2(token, 2) @ 
b<-m2(token, 3) @ 
b<-m2(token, 4) @ 
b<-m2(token, 5) @ 
b<-m2(token, 6) @ 
b<-m2(token, 7) @ 
b<-m2(token, 8) @ 
x = b<-m2(token, 9);
 
   
To learn more about the named tokens, we use the following example to illustrate how the named token declaration works and how some confusion could arise:
 
1.    token x  = a<-m1();
2.
3.    for (int j = 0; j < 10; j++) {
4.        b<-m2(x);
5.        x = c<-m3(x);
6.        d<-m4(x);
7.    }
 
   
As the token is updated as soon as the code is processed, this leads to some interesting occurrences. In the for loop on lines 3-7, for each iteration of the loop, the value of token x in b<-m2 and c<-m3 is the same. However, the value of token x in d<-m4 is the token returned by c<-m3, and thus equal to the value of token x in the message sends on lines 4 and 5 in the next iteration of the loop.


next up previous contents
Next: Join Continuations Up: Advanced Concurrency Coordination Previous: Advanced Concurrency Coordination   Contents
Wei-Jen Wang 2005-10-24