Cind language documentation

Monitor class

A monitor class is a kind of class (see Classes) that guarantees exclusive access to methods of its object.
This provides another mechanism for synchronization of concurrent tasks (for more see Concurrent expressions).

Monitor class is declared by putting word monitor after class in the class definition.
Example:

class monitor c { int n = 0; .inc() { n++; } .dec() { n--; } .read() { return n; } }

For a fixed object of monitor class, when many threads invoke a method of this object at the same time, then only one of them will be selected for code execution, and rest will be stopped until it is completed, and then another one will be waked up to execute called method, and so on. It is not deterministically specified which one of sleeping threads will be waked next, and it will be randomly selected.

That mechanism is called an object lock or monitor lock, and the threads are waiting on that monitor lock to exclusively execute object's methods. Each object of monitor class has own lock (which is assigned to the object, not to class).

Enter and leave sections

Instruction enter grabs monitor lock (enters to monitor's exclusive section) to execute given code.
Instruction leave releases monitor lock in given code execution.
Example:

class monitor c { .f() { .... // inside monitor lock leave { .... // outside monitor lock enter { .... // inside monitor lock } } .... // inside monitor lock } }

The threads may be suspended until monitor lock is free to acquire, on the beginning of instruction enter and at the end of instruction leave.

Every method of monitor class, by default, acquire monitor lock to execute its code, except when a methods declaration is preceded by the keyword leaved. In that case monitor lock will not be grabbed to call a method and not be released after its done, while at the same time, another thread may own a lock.
Example:

class monitor c { .f() { .... // inside monitor lock } leaved .g() { .... // outside monitor lock } }

Only methods of the class acquire monitor lock. But it is possible to grab monitor lock inside any other code of the class, by using enter instruction.
For example:

class monitor c { .give() { fun f = fun() { enter { .... // inside monitor lock } } return f; } }

whereever function f will be fired (perhaps outside of the monitor's code), it will grab monitor lock of the object of class c.

Conditions

A condition is a synchronization construct that allows threads to wait until a certain condtion is met.
When a thread waits on some condition and it own a monitor lock, then it temporarily releases the lock for the sleeping time, allowing other threads to own the lock. And when condition is met, it will not be waked until it grabs the lock again.

Declaration of conditions inside monitor class starts with word condition and then there are list of names of declared conditions.

There are three instructions to work with condition: wait, signal and broadcast. Instruction wait stops calling thread until condition will be signaled, instruction signal wakes one thread stopped on the condition and instruction broadcast wakes all stopped threads on the condition.

Example:

class monitor packkeeper { condition have; var box = null; .put(package) { box = package; signal have; } .get() { if (box == null) { wait have; } return box; } }

Semaphore implementation

Example of basic semaphore implementation using monitor class:

class monitor semaphore { int N = 0; condition c; .P() { while (N == 0) { wait c; } N--; } .V() { N++; signal c; } }

Cind programming language 1.0.4

 

Advertisements