2020-09-19 03:14:35 +03:00
|
|
|
import time
|
|
|
|
import sync
|
|
|
|
|
|
|
|
struct St {
|
|
|
|
a int
|
|
|
|
}
|
|
|
|
|
2020-09-20 04:50:09 +03:00
|
|
|
fn getint() int {
|
|
|
|
return 8
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f1(ch1 chan int, ch2 chan St, ch3 chan int, ch4 chan int, ch5 chan int, sem sync.Semaphore) {
|
2020-09-19 03:14:35 +03:00
|
|
|
mut a := 5
|
|
|
|
select {
|
|
|
|
a = <-ch3 {
|
|
|
|
a = 0
|
|
|
|
}
|
|
|
|
b := <-ch2 {
|
|
|
|
a = b.a
|
|
|
|
}
|
2020-09-20 04:50:09 +03:00
|
|
|
ch3 <- 5 {
|
|
|
|
a = 1
|
|
|
|
}
|
2021-01-25 21:04:29 +03:00
|
|
|
ch2 <- St{
|
|
|
|
a: 37
|
|
|
|
} {
|
2020-09-19 03:14:35 +03:00
|
|
|
a = 2
|
|
|
|
}
|
2020-09-20 04:50:09 +03:00
|
|
|
ch4 <- (6 + 7 * 9) {
|
|
|
|
a = 8
|
|
|
|
}
|
|
|
|
ch5 <- getint() {
|
|
|
|
a = 9
|
|
|
|
}
|
2020-09-19 03:14:35 +03:00
|
|
|
> 300 * time.millisecond {
|
|
|
|
a = 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert a == 3
|
|
|
|
sem.post()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f2(ch1 chan St, ch2 chan int, sem sync.Semaphore) {
|
|
|
|
mut r := 23
|
|
|
|
for i in 0 .. 2 {
|
|
|
|
select {
|
2020-09-20 04:50:09 +03:00
|
|
|
b := <-ch1 {
|
2020-09-19 03:14:35 +03:00
|
|
|
r = b.a
|
|
|
|
}
|
|
|
|
ch2 <- r {
|
|
|
|
r = 17
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
assert r == 17
|
|
|
|
} else {
|
|
|
|
assert r == 13
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sem.post()
|
2021-01-25 21:04:29 +03:00
|
|
|
}
|
2020-09-19 03:14:35 +03:00
|
|
|
|
|
|
|
fn test_select_blocks() {
|
|
|
|
ch1 := chan int{cap: 1}
|
|
|
|
ch2 := chan St{}
|
|
|
|
ch3 := chan int{}
|
2020-09-20 04:50:09 +03:00
|
|
|
ch4 := chan int{}
|
|
|
|
ch5 := chan int{}
|
2020-09-19 03:14:35 +03:00
|
|
|
sem := sync.new_semaphore()
|
|
|
|
mut r := false
|
|
|
|
t := select {
|
2021-01-25 21:04:29 +03:00
|
|
|
b := <-ch1 {
|
2020-09-19 03:14:35 +03:00
|
|
|
println(b)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no channel ready
|
|
|
|
r = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert r == true
|
|
|
|
assert t == true
|
|
|
|
go f2(ch2, ch3, sem)
|
|
|
|
n := <-ch3
|
|
|
|
assert n == 23
|
2021-01-25 21:04:29 +03:00
|
|
|
ch2 <- St{
|
|
|
|
a: 13
|
|
|
|
}
|
2020-09-19 03:14:35 +03:00
|
|
|
sem.wait()
|
|
|
|
stopwatch := time.new_stopwatch({})
|
2020-09-20 04:50:09 +03:00
|
|
|
go f1(ch1, ch2, ch3, ch4, ch5, sem)
|
2020-09-19 03:14:35 +03:00
|
|
|
sem.wait()
|
|
|
|
elapsed_ms := f64(stopwatch.elapsed()) / time.millisecond
|
2021-01-25 21:04:29 +03:00
|
|
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/high-resolution-timers
|
|
|
|
// > For example, for Windows running on an x86 processor, the default interval between
|
|
|
|
// > system clock ticks is typically about 15 milliseconds, and the minimum interval
|
|
|
|
// > between system clock ticks is about 1 millisecond.
|
|
|
|
assert elapsed_ms >= 280.0 // 300 - (15ms + 5ms just in case)
|
2020-09-19 03:14:35 +03:00
|
|
|
|
|
|
|
ch1.close()
|
|
|
|
ch2.close()
|
|
|
|
mut h := 7
|
2020-09-21 08:34:04 +03:00
|
|
|
mut is_open := true
|
|
|
|
if select {
|
2021-01-25 21:04:29 +03:00
|
|
|
_ := <-ch2 {
|
2020-09-19 03:14:35 +03:00
|
|
|
h = 0
|
|
|
|
}
|
|
|
|
ch1 <- h {
|
|
|
|
h = 1
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
h = 2
|
|
|
|
}
|
2020-09-21 08:34:04 +03:00
|
|
|
} {
|
|
|
|
panic('channel is still open')
|
|
|
|
} else {
|
|
|
|
is_open = false
|
2020-09-19 03:14:35 +03:00
|
|
|
}
|
|
|
|
// no branch should have run
|
|
|
|
assert h == 7
|
|
|
|
// since all channels are closed `select` should return `false`
|
2020-09-21 08:34:04 +03:00
|
|
|
assert is_open == false
|
2020-09-19 03:14:35 +03:00
|
|
|
}
|