Developing concurrent programs with Pony
Subtyping
Listing 5 shows yet a further incompatibility in capabilities: The history
declaration stipulates box
(line 3), whereas in line 10, res
attempts to insert an iso
reference using push()
.
Of course consume
deletes res
; however, without the capability references, you could not write a meaningful program in Pony. Figure 5 shows the possible replacements that work in the direction of the arrow. For example, an iso
can replace any other capability, but trn
can only be used for val
or ref
, because trn
permits additional read references of the box
capability, which would contradict the exclusive character of iso
. However, val
can be replaced by trn
after a consume
removes the last writing reference of trn
type.
If you replace ref
, you can allow other writing references without problems. The box
and tag
capabilities are not critical for val
and ref
because the latter cannot write. A blog post by developer John Mumm provides a more detailed explanation [9].
Blue Horses
The main.pony
file shown in Listing 6 is the entry point in the example program; in the tradition of C, Pony uses main
as the identifier for the program start point.
Like the familiar main()
function in C or the main()
method in Java, Pony defines an actor. In line 4, the actor's constructor accepts a reference to the executing environment of the program as an object of type Env
. The next lines save the reference in the field with the name env
in the Main
instance.
Line 6 creates the actor of type Painter
, as defined by Listing 5. The constructor uses this
to receive simultaneously a reflexive reference to the Main
actor. The for
loop in lines 7 to 10 iterates over the field with the primitives [Amber, Blue, Crimson, Other]
or, preferably, its iterator object.
On the basis of field values, line 8 generates an object of type Resource
, as defined in Listing 3, and stores the reference in the run-time variable x
. The expression recover Resource(x) end
introduces a mechanism for arbitrarily setting a capability. In the present form, recover
generates a reference of type iso
.
Line 9 transfers the resource
to the Painter
actor. A further deletion process, courtesy of consume res
, occurs before sending. As mentioned earlier, Painter
finally calls the notify
behavior in line 12. The message is a String
type and ends up in the shell thanks to the env
object.
Conclusions
Thanks to Pony, programmers can write what looks to be secure, high-performance code for concurrent programs. It is free of data races and deadlocks and follows the style of object-oriented languages. The actor model should not cause any trouble, but the difficulty of understanding capabilities could deter many users. The matter is even more complex when developers need to consider the resulting effects of a collection of several capabilities.
If the complexity does not deter you, you can improve the quality of your multithreaded programs; moreover, Pony can be docked on existing projects by integrating C code via the Foreign Function Interface (FFI).
Infos
- Pony: http://ponylang.org
- Actor model: http://www.brianstorti.com/the-actor-model/
- Capabilities: http://man7.org/linux/man-pages/man7/capabilities.7.html
- Rust: http://rust-lang.org
- Java 9 plans: http://openjdk.java.net/projects/jdk9/spec/
- Dynamic classes (citation 7): https://en.wikipedia.org/wiki/Comparison_of_programming_languages_by_type_system#cite_note-7
- Pony on GitHub: https://github.com/ponylang/ponyc
- The concept of ownership in Rust: https://doc.rust-lang.org/book/ownership.html
- Sharing reference capabilities: http://jtfmumm.com/blog/2016/03/06/safely-sharing-data-pony-reference-capabilities/
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.