1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: implement channel push()/pop() using <- operator (#6154)

This commit is contained in:
Uwe Krüger
2020-08-17 23:35:36 +02:00
committed by GitHub
parent 5c98c0760b
commit 0d7cc8829a
4 changed files with 52 additions and 21 deletions

View File

@ -1,45 +1,42 @@
import sync
import time
fn do_rec_i64(ch chan i64) {
mut sum := i64(0)
for _ in 0 .. 300 {
mut a := i64(0)
(&sync.Channel(ch)).pop(&a)
sum += a
sum += <-ch
}
assert sum == 300 * (300 - 1) / 2
}
fn do_send_int(ch chan int) {
for i in 0 .. 300 {
(&sync.Channel(ch)).push(&i)
ch <- i
}
}
fn do_send_byte(ch chan byte) {
for i in 0 .. 300 {
ii := byte(i)
(&sync.Channel(ch)).push(&ii)
ch <- byte(i)
}
}
fn do_send_i64(mut ch sync.Channel) {
fn do_send_i64(ch chan i64) {
for i in 0 .. 300 {
ii := i64(i)
ch.push(&ii)
ch <- i
}
}
fn test_select() {
chi := chan int{}
mut chl := sync.new_channel<i64>(1)
chl := chan i64{cap: 1}
chb := chan byte{cap: 10}
recch := chan i64{cap: 0}
go do_rec_i64(recch)
go do_send_int(chi)
go do_send_byte(chb)
go do_send_i64(mut chl)
mut channels := [&sync.Channel(chi), &sync.Channel(recch), chl, &sync.Channel(chb)]
go do_send_i64(chl)
mut channels := [&sync.Channel(chi), &sync.Channel(recch), &sync.Channel(chl), &sync.Channel(chb)]
directions := [sync.Direction.pop, .push, .pop, .pop]
mut sum := i64(0)
mut rl := i64(0)
@ -73,4 +70,5 @@ fn test_select() {
256 * (256 - 1) / 2 +
44 * (44 - 1) / 2
assert sum == expected_sum
time.sleep_ms(20) // to give assert in coroutine enough time
}