Repository navigation
Atomic integer example
This tutorial provides the most basic example of a Waldo file. It spans just one host and will hopefully show you how to compile a Waldo file and include it in your project. Don't worry about the file structure or much else right now. There are other tutorials for that. This is just to get you building and running.
The ping pong tutorial walks through the general structure of a Waldo file, so don't worry if the code below doesn't make too much sense just yet. At a high level, all that is happening is that we are declaring and defining a single endpoint, SingleSide.
AtomicInt
Endpoint SingleSide;
SingleSide
{
Number internal_number = 0;
Public Function increment_number(Number increment_by) returns Number
{
internal_number += increment_by;
return internal_number;
}
}
SingleSide holds one piece of data, a Number, named internal_number, and a method, named increment_number. If we were to translate this to a single-threaded C++ application for instance, it might looks something like this:
class SingleSide
{
public:
SingleSide()
: internal_number(0)
{}
double increment_number( double increment_by)
{
internal_number += increment_by;
return internal_number;
}
private:
double internal_number;
}
(Note: that because of Waldo's transactional memory model, for a multi-threaded application, the comparable C++ code would actually be more complicated. Check out the memory model tutorials on the tutorials page.)
To use this Waldo code in one of your projects, you'll first have to compile it. Compiling will generate a single file, which you can then import into a native Python project. This part of the tutorial will show you how to compile your Waldo source, instantiate an endpoint object, and call methods on it.
From the Waldo base directory, to compile, you should just need to call bin/wcompile.py [filename]. This will produce a file named emitted.py, which contains the necessary code for SingleSide. (Note: the Waldo compiler has a few different options, which you can read about with bin/wcompile.py -h. For instance, to emit to a file named my_file.py instead of emitted.py, just call bin/wcompile.py [filename] my_file.py.)
To instantiate SingleSide in a real Waldo project, you have to import the file generated by the Waldo compiler as well as a few other Waldo libraries. If your Python project that is using Waldo is in the same directory as your Waldo repository, this is easy. At the top of your file, just include the import statement:
# to import endpoint class
from emitted import SingleSide
# to import Waldo libraries
from lib import Waldo
If it is not, you may have to append the path to sys. For instance:
# to import endpoint class
from emitted import SingleSide
# to import Waldo libraries
import sys
sys.path.append( [absolute path to Waldo directory])
from lib import Waldo
Following these imports, you need to instantiate a SingleSide object in your project. To do this for a single endpoint (not connected to any other endpoint), just use Waldo.no_partner_create:
ss = Waldo.no_partner_create(SingleSide)
You can then call methods on this instantiated endpoint using typical Python syntax, eg.:
ss.increment_number(5)
Altogether then, a file using your AtomicInteger protocol might look as follows:
from emitted import SingleSide
from lib import Waldo
ss = Waldo.no_partner_create(SingleSide)
print (ss.increment_number(5))
print (ss.increment_number(10))
print (ss.increment_number(20))