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,76 @@
define atomic => thread {
data
private buckets = staticarray_join(10, void),
private lock = 0
public onCreate => {
loop(.buckets->size) => {
.`buckets`->get(loop_count) = math_random(0, 1000)
}
}
public buckets => .`buckets`
public bucket(index::integer) => .`buckets`->get(#index)
public transfer(source::integer, dest::integer, amount::integer) => {
#source == #dest
? return
#amount = math_min(#amount, .`buckets`->get(#source))
.`buckets`->get(#source) -= #amount
.`buckets`->get(#dest) += #amount
}
public numBuckets => .`buckets`->size
public lock => {
.`lock` == 1
? return false
.`lock` = 1
return true
}
public unlock => {
.`lock` = 0
}
}
local(initial_total) = (with b in atomic->buckets sum #b)
local(total) = #initial_total
// Make 2 buckets close to equal
local(_) = split_thread => {
local(bucket1) = math_random(1, atomic->numBuckets)
local(bucket2) = math_random(1, atomic->numBuckets)
local(value1) = atomic->bucket(#bucket1)
local(value2) = atomic->bucket(#bucket2)
if(#value1 >= #value2) => {
atomic->transfer(#bucket1, #bucket2, (#value1 - #value2) / 2)
else
atomic->transfer(#bucket2, #bucket1, (#value2 - #value1) / 2)
}
currentCapture->restart
}
// Randomly distribute 2 buckets
local(_) = split_thread => {
local(bucket1) = math_random(1, atomic->numBuckets)
local(bucket2) = math_random(1, atomic->numBuckets)
local(value1) = atomic->bucket(#bucket1)
atomic->transfer(#bucket1, #bucket2, math_random(1, #value1))
currentCapture->restart
}
local(buckets)
while(#initial_total == #total) => {
sleep(2000)
#buckets = atomic->buckets
#total = with b in #buckets sum #b
stdoutnl(#buckets->asString + " -- total: " + #total)
}
stdoutnl(`ERROR: totals no longer match: ` + #initial_total + ', ' + #total)

View file

@ -0,0 +1,48 @@
constant nBuckets = 20
sequence buckets = tagset(nBuckets) -- {1,2,3,..,20}
constant bucket_cs = init_cs() -- critical section
atom equals = 0, rands = 0 -- operation counts
integer terminate = 0 -- control flag
procedure mythreads(integer eq)
-- if eq then equalise else randomise
integer b1,b2,amt
while not terminate do
b1 = rand(nBuckets)
b2 = rand(nBuckets)
if b1!=b2 then -- (test not actually needed)
enter_cs(bucket_cs)
if eq then
amt = floor((buckets[b1]-buckets[b2])/2)
equals += 1
else
amt = rand(buckets[b1]+1)-1
rands += 1
end if
buckets[b1] -= amt
buckets[b2] += amt
leave_cs(bucket_cs)
end if
end while
exit_thread(0)
end procedure
procedure display()
enter_cs(bucket_cs)
?{sum(buckets),equals,rands,buckets}
leave_cs(bucket_cs)
end procedure
display()
constant threads = {create_thread(routine_id("mythreads"),{1}), -- equalise
create_thread(routine_id("mythreads"),{0})} -- randomise
constant ESC = #1B
while not find(get_key(),{ESC,'q','Q'}) do
sleep(1)
display()
end while
terminate = 1
wait_thread(threads)
delete_cs(bucket_cs)