Cind language documentation

Selector class

A selector class is a kind of class (see Classes) whose objects receives messages themselves.
It does not have methods, which are executed when some thread sends message to the object (like in case of normal class), but has own threads, which are accepting messages, in the time and in the order they want to, by using a special accept instructions.

Selector class is declared by putting word selector after class in the class definition.
Example:

class selector packkeeper { start() { var box; accept put(package) { box = package; } accept get() { return box; } } }

In above example, creating object of this class will start a thread, named selector thread (this section starting with a word start), which firstly wait to accept incomming message put, to save package in local variable box, and then will wait to for a message get to return stored object. The messages put and get and also any other messages sended to this object will be suspended until selector thread call an appropriate accept instruction.
This is the way, how an object of selector class defines an order of executed methods.

Selector thread

A selector thread is a thread associated with an object of selector class, which accepts incomming messages sended to this objects. First selector thread is started during object creation (by instruction new, see Creating objects), as an independent separate thread, so instruction new will not wait for its done.

The code of the first selector thread is defined in section start inside a class, in the place of missed constructor of the object.
Parameters given to the instruction new will be passed to the start(...) section.

class selector c { start() { // ... selector thread code ... } }

It is possible to have more than one selector thread to accept messages for one specific object, and also to accept them concurrently (see Concurrent expressions).
For example:

class selector office { start() { {# accept mail() { ... }; # accept phonecall() { ... }; } } }

this executes a number of threads, which waits for the messages concurrently.

Accepting messages

Every messages sent to the object of selector class are waiting to be selected for execution by accept instructions. Accept instructions can be called not only by the selector threads but from the other threads as well, but only from the inside of the code of the class.

The accept instructions are accept and any.

The syntax of the accept instruction is the word accept, next there is a list of names of messages, separated by vertical line character ( | ), then a vector of parameters, and finally a code to execute (for syntax of parameters see Parameters of function). A value returned to the message sender is the value from the last instruction of the code, or value returned by instruction return during code execution. The value returned to the accepting thread from calling instruction accept is the same as the value returned to the message sender.
For example:

accept add|insert|put (x,y) { ... return z; }

will accept one of the messages: add, insert or put, receiving parameters in (x,y), and then executes the accepter's code, which returns value z. This value is returned to the message sender and also as a result of the accept instruction.

When more messages are waiting to be accepted, it is not deterministically specified which one of them will be selected by the instruction accept, and it will be randomly choosen. When more accept threads wants to accept the same message, it is also not specified which one of them will accept the incomming message. When there are no waiting messages to be accepted, then calling accept instruction will suspend the thread until expected message will arrive to be accepted.

The second instruction any, is a kind of instruction accept, but it accept any incoming message and also gets the name of the message in the variable.
Example:

any [name] (x,y) { host.println("received message: "+name); return this; }

Mutex implementation

Example of basic mutex implementation using selector class:

class selector mutex { start() { while(true) { accept PB() {}; accept VB() {}; } } }

Cind programming language 1.0.4

 

Advertisements