examples: annotate examples
[nit.git] / lib / actors / examples / thread-ring / thread_ring.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Example implemented from "The computer Language Benchmarks Game" - Thread-Ring
16 # http://benchmarksgame.alioth.debian.org/
17 #
18 # Complete description of the thread-ring :
19 # http://benchmarksgame.alioth.debian.org/u64q/threadring-description.html#threadring
20 module thread_ring is example
21
22 import actors
23
24 # One Actor, who will receive the token
25 class ThreadRing
26 actor
27
28 # Identification of `self`
29 var id: Int
30
31 # The next Actor to which `self` send the token
32 var next: ThreadRing is noinit
33
34 # Receive a token, then send it to `next` unless its value is `0`
35 fun send_token(message: Int) do
36 if message == 0 then print id
37 if message >= 1 then next.async.send_token(message - 1)
38 end
39 end
40
41 redef class Sys
42 # numbers of actors to create the ring
43 var nb_actors = 503
44 end
45
46 var first = new ThreadRing(1)
47 var current = new ThreadRing(2)
48 first.next = current
49 for i in [3..nb_actors] do
50 var t = new ThreadRing(i)
51 current.next = t
52 current = t
53 end
54 current.next = first
55 var n = if args.is_empty then 1000 else args[0].to_i
56 first.send_token(n)