thread ring example from benchmarksgame
authorBlackMinou <romain.chanoir@viacesi.fr>
Tue, 13 Sep 2016 14:52:48 +0000 (10:52 -0400)
committerBlackMinou <romain.chanoir@viacesi.fr>
Tue, 21 Feb 2017 01:30:24 +0000 (20:30 -0500)
Signed-off-by: BlackMinou <romain.chanoir@viacesi.fr>

lib/actors/examples/thread-ring/makefile [new file with mode: 0644]
lib/actors/examples/thread-ring/thread_ring.nit [new file with mode: 0644]
tests/sav/thread_ring.res [new file with mode: 0644]

diff --git a/lib/actors/examples/thread-ring/makefile b/lib/actors/examples/thread-ring/makefile
new file mode 100644 (file)
index 0000000..60895c1
--- /dev/null
@@ -0,0 +1,15 @@
+file= thread_ring
+
+default: threaded
+
+threaded:
+       nitc $(file).nit
+
+test:
+       ./$(file) 1000
+bm:
+       time ./$(file) 50000000
+
+clean:
+       rm $(file)
+       rm actors_$(file).nit
diff --git a/lib/actors/examples/thread-ring/thread_ring.nit b/lib/actors/examples/thread-ring/thread_ring.nit
new file mode 100644 (file)
index 0000000..2da490d
--- /dev/null
@@ -0,0 +1,58 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Example implemented from "The computer Language Benchmarks Game" - Thread-Ring
+# http://benchmarksgame.alioth.debian.org/
+#
+# Complete description of the thread-ring :
+# http://benchmarksgame.alioth.debian.org/u64q/threadring-description.html#threadring
+module thread_ring
+
+import actors
+
+# One Actor, who will receive the token
+class ThreadRing
+       actor
+
+       # Identification of `self`
+       var id: Int
+
+       # The next Actor to which `self` send the token
+       var next: ThreadRing is noinit
+
+       # Receive a token, then send it to `next` unless its value is `0`
+       fun send_token(message: Int) do
+               if message == 0 then
+                       print id
+               end
+               if message >= 1 then
+                       next.async.send_token(message - 1)
+               end
+       end
+end
+
+var first = new ThreadRing(1)
+var current = new ThreadRing(2)
+first.next = current
+for i in [3..503] do
+       var t = new ThreadRing(i)
+       current.next = t
+       current = t
+end
+current.next = first
+if args.is_empty then
+       first.send_token(1000)
+else
+       first.send_token(args[0].to_i)
+end
diff --git a/tests/sav/thread_ring.res b/tests/sav/thread_ring.res
new file mode 100644 (file)
index 0000000..f9aaa4d
--- /dev/null
@@ -0,0 +1 @@
+498