Example implemented from "The computer Language Benchmarks Game" - Chameneos-Redux

http://benchmarksgame.alioth.debian.org/

Complete description of the chameneos-redux : https://benchmarksgame.alioth.debian.org/u64q/chameneosredux-description.html#chameneosredux

Introduced classes

Redefined classes

redef class Sys

actors :: chameneosredux $ Sys

The main class of the program.

All class definitions

class Pair

actors $ Pair

redef class Sys

actors :: chameneosredux $ Sys

The main class of the program.
package_diagram actors::chameneosredux chameneosredux actors actors actors::chameneosredux->actors pthreads pthreads actors->pthreads ...pthreads ... ...pthreads->pthreads a_star-m a_star-m a_star-m->actors::chameneosredux

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module concurrent_collections

pthreads :: concurrent_collections

Introduces thread-safe concurrent collections
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module extra

pthreads :: extra

Offers some POSIX threads services that are not available on all platforms
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module pthreads

pthreads :: pthreads

Main POSIX threads support and intro the classes Thread, Mutex and Barrier
module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module actors

actors :: actors

Abstraction of the actors concepts

Children

module a_star-m

a_star-m

# Example implemented from "The computer Language Benchmarks Game" - Chameneos-Redux
# http://benchmarksgame.alioth.debian.org/
#
# Complete description of the chameneos-redux :
# https://benchmarksgame.alioth.debian.org/u64q/chameneosredux-description.html#chameneosredux
module chameneosredux is example, no_warning("missing-doc")

import actors

class Creature
	actor
	var place: MeetingPlace
	var color: Int
	var id: Int
	var count = 0
	var samecount = 0

	fun run do
		loop
			var p = place.meet(id, color)
			if p == null then break
			color = p.color
			if p.sameid then samecount += 1
			count += 1
		end
	end

	fun to_string: String do return count.to_s + " " + numbers[samecount]
end

class Pair
	var sameid: Bool
	var color: Int
end

class MeetingPlace
	var meetings_left: Int
	var firstcolor: nullable Int
	var firstid: Int = 0
	var current: Future[Pair] is noinit

	private var mutex = new Mutex

	fun meet(id, c: Int): nullable Pair do
		var new_pair = new Future[Pair]
		mutex.lock
		if meetings_left == 0 then
			mutex.unlock
			return null
		else
			if firstcolor == null then
				firstcolor = c
				firstid = id
				current = new Future[Pair]
			else
				var color = complements[c][firstcolor.as(not null)]
				current.set_value(new Pair(id == firstid, color))
				firstcolor = null
				meetings_left -= 1
			end
			new_pair = current
		end
		mutex.unlock
		return new_pair.join
	end
end

redef class Sys
	fun blue: Int do return 0
	fun red: Int do return 1
	fun yellow: Int do return 2
	var numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
	var colors = ["blue", "red", "yellow"]
	# Matrix for complementing colors
	var complements: Array[Array[Int]] = [[0, 2, 1],
	                                      [2, 1, 0],
	                                      [1, 0, 2]]

end

fun print_all_colors do
	print_colors(blue, blue)
	print_colors(blue, red)
	print_colors(blue, yellow)
	print_colors(red, blue)
	print_colors(red, red)
	print_colors(red, yellow)
	print_colors(yellow, blue)
	print_colors(yellow, red)
	print_colors(yellow, yellow)
end

fun print_colors(c1, c2: Int) do
	print colors[c1] + " + " + colors[c2] + " -> " + colors[complements[c1][c2]]
end

fun get_number(n: Int): String do
	var str = ""
	var nstr = n.to_s
	for c in nstr do
		str += " " + numbers[c.to_i]
	end
	return str
end

fun work(n, nb_colors : Int ) do
	var place = new MeetingPlace(n)
	var creatures = new Array[Creature]
	for i in [0..nb_colors[ do
		printn " " + colors[i % 3]
		creatures[i] = new Creature(place, i % 3, i)
	end
	print ""

	for c in creatures do c.async.run

	active_actors.wait

	var total = 0
	for c in creatures do
		print c.to_string
		total += c.count
	end

	print get_number(total)
	print ""

	for c in creatures do
		c.async.terminate
		c.async.wait_termination
	end
end

var n = if args.is_empty then 600 else args[0].to_i

print_all_colors
print ""

work(n, 3)
work(n, 10)
lib/actors/examples/chameneos-redux/chameneosredux.nit:15,1--154,11