Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b04fff2
commit 6e6010d
Showing
5 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import sync | ||
import time | ||
|
||
const ( | ||
iterations_per_thread = 100000 | ||
) | ||
|
||
fn add_elements(shared foo []int, n int) { | ||
for _ in 0 .. iterations_per_thread { | ||
foo << n | ||
} | ||
foo[0]++ | ||
} | ||
|
||
fn test_autolocked_array() { | ||
shared abc := &[0] | ||
go add_elements(shared abc, 1) | ||
go add_elements(shared abc, 3) | ||
for _ in 0 .. iterations_per_thread { | ||
abc << 0 | ||
} | ||
// wait for coroutines to finish - that should really be | ||
// done by channels, yield, semaphore... | ||
for { | ||
mut finished_threads := 0 | ||
rlock abc { | ||
finished_threads = abc[0] | ||
} | ||
if finished_threads == 2 { | ||
break | ||
} | ||
time.sleep_ms(100) | ||
} | ||
// create histogram of results | ||
mut result := [0, 0, 0, 0] | ||
rlock abc { | ||
// automatic rlock for iteration is also not implemented, yet | ||
for v in abc { | ||
if v > 3 { | ||
panic('unexpected element on array') | ||
} | ||
result[v]++ | ||
} | ||
} | ||
assert result[0] == iterations_per_thread | ||
assert result[1] == iterations_per_thread | ||
assert result[2] == 1 // number of non-main threads | ||
assert result[3] == iterations_per_thread | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import sync | ||
import time | ||
|
||
const ( | ||
iterations_per_thread2 = 100000 | ||
) | ||
|
||
fn inc_elements(shared foo []int, n int) { | ||
for _ in 0 .. iterations_per_thread2 { | ||
foo[n]++ | ||
} | ||
foo[0]++ // indicat that thread is finished | ||
} | ||
|
||
fn test_autolocked_array_2() { | ||
shared abc := &[0, 0, 0] | ||
go inc_elements(shared abc, 1) | ||
go inc_elements(shared abc, 2) | ||
for _ in 0 .. iterations_per_thread2 { | ||
abc[2]++ | ||
} | ||
// wait for coroutines to finish - that should really be | ||
// done by channels, yield, semaphore... | ||
for { | ||
mut finished_threads := 0 | ||
rlock abc { | ||
finished_threads = abc[0] | ||
} | ||
if finished_threads == 2 { | ||
break | ||
} | ||
time.sleep_ms(100) | ||
} | ||
rlock abc { | ||
assert abc[1] == iterations_per_thread2 | ||
assert abc[2] == 2 * iterations_per_thread2 | ||
} | ||
} |