Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,34 @@
(require 'sequences)
(require 'tasks)
;; inter-tasks message : (op-code . data)
(define (is-message? op message)
(and message (equal? op (first message))))
;; reader task
(define (reader infile )
(wait S)
(define message (semaphore-pop S))
(when (is-message? 'count message ) (writeln 'reader-> message) (task-stop-all))
(if (first infile) ;; not EOF
(set! message (cons 'write (next infile)))
(set! message (list 'reader-count-please)))
(semaphore-push S message)
(signal S)
infile)
(define (writer count)
(wait S)
(define message (semaphore-pop S))
(when (is-message? 'write message )
(writeln (rest message))
(set! count (1+ count))
(set! message (cons 'ack count)))
(when (is-message? 'reader-count-please message )
(set! message (cons 'count count)))
(semaphore-push S message)
(signal S)
count)

View file

@ -0,0 +1,16 @@
import: parallel
: printing(chPrint, chCount)
0 while( chPrint receive dup notNull ) [ println 1+ ] drop
chCount send drop ;
: concurrentPrint(aFileName)
| chPrint chCount line |
Channel new ->chPrint
Channel new ->chCount
#[ printing(chPrint, chCount) ] &
aFileName File new forEach: line [ chPrint send(line) drop ]
chPrint close
chCount receive "Number of lines printed : " print println ;

View file

@ -0,0 +1,51 @@
atom frThread, -- file reader thread
lcThread -- line counter thread
sequence queue = {}
integer qlock = init_cs()
integer linecount = 1
procedure readfile()
object line
integer fn = open("input.txt","r")
while 1 do
line = gets(fn)
enter_cs(qlock)
queue = append(queue,line)
line = atom(line) -- kill refcount!
leave_cs(qlock)
if line then exit end if
end while
close(fn)
wait_thread(lcThread)
printf(1,"Lines read: %d\n",linecount)
exit_thread(0)
end procedure
procedure countlines()
object line
linecount = 0
while 1 do
enter_cs(qlock)
if length(queue)=0 then
leave_cs(qlock)
-- sleep(0.1)
else
line = queue[1]
queue = queue[2..$]
leave_cs(qlock)
if atom(line) then exit end if
-- ?line
linecount += 1
end if
end while
exit_thread(0)
end procedure
frThread = create_thread(routine_id("readfile"),{})
lcThread = create_thread(routine_id("countlines"),{})
wait_thread(frThread)
puts(1,"done")
{} = wait_key()

View file

@ -0,0 +1,44 @@
//
// Reader.swift
//
import Foundation
class Reader: NSObject {
let inputPath = "~/Desktop/input.txt".stringByExpandingTildeInPath
var gotNumberOfLines = false
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "linesPrinted:",
name: "LinesPrinted", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
// Selector for the number of lines printed
func linesPrinted(not:NSNotification) {
println(not.object!)
self.gotNumberOfLines = true
exit(0)
}
func readFile() {
var err:NSError?
let fileString = NSString(contentsOfFile: self.inputPath,
encoding: NSUTF8StringEncoding, error: &err)
if let lines = fileString?.componentsSeparatedByString("\n") {
for line in lines {
NSNotificationCenter.defaultCenter().postNotificationName("Line", object: line)
}
NSNotificationCenter.defaultCenter().postNotificationName("LineNumberRequest", object: nil)
while !self.gotNumberOfLines {
sleep(1 as UInt32)
}
}
}
}

View file

@ -0,0 +1,38 @@
//
// Printer.swift
//
import Foundation
class Printer: NSObject {
var numberOfLines = 0
var gotRequestLineNumber = false
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "gotLine:",
name: "Line", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "lineNumberRequest:",
name: "LineNumberRequest", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func gotLine(not:NSNotification) {
println(not.object!)
self.numberOfLines++
}
func lineNumberRequest(not:NSNotification) {
self.gotRequestLineNumber = true
NSNotificationCenter.defaultCenter().postNotificationName("LinesPrinted", object: self.numberOfLines)
}
func waitForLines() {
while !self.gotRequestLineNumber {
sleep(1 as UInt32)
}
}
}

View file

@ -0,0 +1,17 @@
//
// main.swift
//
import Foundation
dispatch_async(dispatch_get_global_queue(0, 0)) {
let printer = Printer()
printer.waitForLines()
}
dispatch_async(dispatch_get_global_queue(0, 0)) {
let reader = Reader()
reader.readFile()
}
CFRunLoopRun()