2020-08-06 15:28:19 +02:00
|
|
|
module sync
|
|
|
|
|
|
|
|
import time
|
|
|
|
import rand
|
|
|
|
|
2021-04-18 15:31:05 +02:00
|
|
|
$if windows {
|
2021-04-19 18:01:47 +02:00
|
|
|
#flag -I @VEXEROOT/thirdparty/stdatomic/win
|
2021-04-18 15:31:05 +02:00
|
|
|
} $else {
|
2021-04-19 18:01:47 +02:00
|
|
|
#flag -I @VEXEROOT/thirdparty/stdatomic/nix
|
2021-04-18 15:31:05 +02:00
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
|
|
|
|
$if linux {
|
|
|
|
$if tinyc {
|
2021-06-12 19:34:45 +02:00
|
|
|
$if amd64 {
|
|
|
|
// most Linux distributions have /usr/lib/libatomic.so, but Ubuntu uses gcc version specific dir
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/6
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/7
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/8
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/9
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/10
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/11
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/12
|
2021-06-12 23:45:17 +02:00
|
|
|
} $else $if arm64 {
|
2021-06-12 19:34:45 +02:00
|
|
|
#flag -L/usr/lib/gcc/aarch64-linux-gnu/6
|
|
|
|
#flag -L/usr/lib/gcc/aarch64-linux-gnu/7
|
|
|
|
#flag -L/usr/lib/gcc/aarch64-linux-gnu/8
|
|
|
|
#flag -L/usr/lib/gcc/aarch64-linux-gnu/9
|
|
|
|
#flag -L/usr/lib/gcc/aarch64-linux-gnu/10
|
|
|
|
#flag -L/usr/lib/gcc/aarch64-linux-gnu/11
|
|
|
|
#flag -L/usr/lib/gcc/aarch64-linux-gnu/12
|
|
|
|
}
|
2021-02-25 11:35:00 +01:00
|
|
|
#flag -latomic
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <atomic.h>
|
2021-04-18 15:31:05 +02:00
|
|
|
// The following functions are actually generic in C
|
2020-08-06 15:28:19 +02:00
|
|
|
fn C.atomic_load_ptr(voidptr) voidptr
|
|
|
|
fn C.atomic_store_ptr(voidptr, voidptr)
|
|
|
|
fn C.atomic_compare_exchange_weak_ptr(voidptr, voidptr, voidptr) bool
|
|
|
|
fn C.atomic_compare_exchange_strong_ptr(voidptr, voidptr, voidptr) bool
|
|
|
|
fn C.atomic_exchange_ptr(voidptr, voidptr) voidptr
|
|
|
|
fn C.atomic_fetch_add_ptr(voidptr, voidptr) voidptr
|
|
|
|
fn C.atomic_fetch_sub_ptr(voidptr, voidptr) voidptr
|
|
|
|
|
|
|
|
fn C.atomic_load_u16(voidptr) u16
|
|
|
|
fn C.atomic_store_u16(voidptr, u16)
|
|
|
|
fn C.atomic_compare_exchange_weak_u16(voidptr, voidptr, u16) bool
|
|
|
|
fn C.atomic_compare_exchange_strong_u16(voidptr, voidptr, u16) bool
|
|
|
|
fn C.atomic_exchange_u16(voidptr, u16) u16
|
|
|
|
fn C.atomic_fetch_add_u16(voidptr, u16) u16
|
|
|
|
fn C.atomic_fetch_sub_u16(voidptr, u16) u16
|
|
|
|
|
|
|
|
fn C.atomic_load_u32(voidptr) u32
|
|
|
|
fn C.atomic_store_u32(voidptr, u32)
|
|
|
|
fn C.atomic_compare_exchange_weak_u32(voidptr, voidptr, u32) bool
|
|
|
|
fn C.atomic_compare_exchange_strong_u32(voidptr, voidptr, u32) bool
|
|
|
|
fn C.atomic_exchange_u32(voidptr, u32) u32
|
|
|
|
fn C.atomic_fetch_add_u32(voidptr, u32) u32
|
|
|
|
fn C.atomic_fetch_sub_u32(voidptr, u32) u32
|
|
|
|
|
|
|
|
fn C.atomic_load_u64(voidptr) u64
|
|
|
|
fn C.atomic_store_u64(voidptr, u64)
|
|
|
|
fn C.atomic_compare_exchange_weak_u64(voidptr, voidptr, u64) bool
|
|
|
|
fn C.atomic_compare_exchange_strong_u64(voidptr, voidptr, u64) bool
|
|
|
|
fn C.atomic_exchange_u64(voidptr, u64) u64
|
|
|
|
fn C.atomic_fetch_add_u64(voidptr, u64) u64
|
|
|
|
fn C.atomic_fetch_sub_u64(voidptr, u64) u64
|
|
|
|
|
|
|
|
const (
|
|
|
|
// how often to try to get data without blocking before to wait for semaphore
|
2021-04-15 01:44:39 +02:00
|
|
|
spinloops = 750
|
2020-08-06 15:28:19 +02:00
|
|
|
spinloops_sem = 4000
|
|
|
|
)
|
|
|
|
|
|
|
|
enum BufferElemStat {
|
|
|
|
unused = 0
|
|
|
|
writing
|
|
|
|
written
|
|
|
|
reading
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Subscription {
|
|
|
|
mut:
|
2021-04-15 01:44:39 +02:00
|
|
|
sem &Semaphore
|
2020-08-06 15:28:19 +02:00
|
|
|
prev &&Subscription
|
2021-04-15 01:44:39 +02:00
|
|
|
nxt &Subscription
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 17:31:05 +02:00
|
|
|
enum Direction {
|
|
|
|
pop
|
|
|
|
push
|
|
|
|
}
|
|
|
|
|
2020-08-06 15:28:19 +02:00
|
|
|
struct Channel {
|
2021-04-15 01:44:39 +02:00
|
|
|
ringbuf &byte // queue for buffered channels
|
|
|
|
statusbuf &byte // flags to synchronize write/read in ringbuf
|
|
|
|
objsize u32
|
2020-08-06 15:28:19 +02:00
|
|
|
mut: // atomic
|
2021-01-29 19:52:14 +01:00
|
|
|
writesem Semaphore // to wake thread that wanted to write, but buffer was full
|
|
|
|
readsem Semaphore // to wake thread that wanted to read, but buffer was empty
|
|
|
|
writesem_im Semaphore
|
|
|
|
readsem_im Semaphore
|
2020-08-06 15:28:19 +02:00
|
|
|
write_adr C.atomic_uintptr_t // if != NULL the next obj can be written here without wait
|
|
|
|
read_adr C.atomic_uintptr_t // if != NULL an obj can be read from here without wait
|
|
|
|
adr_read C.atomic_uintptr_t // used to identify origin of writesem
|
|
|
|
adr_written C.atomic_uintptr_t // used to identify origin of readsem
|
|
|
|
write_free u32 // for queue state
|
|
|
|
read_avail u32
|
|
|
|
buf_elem_write_idx u32
|
|
|
|
buf_elem_read_idx u32
|
|
|
|
// for select
|
2021-04-15 01:44:39 +02:00
|
|
|
write_subscriber &Subscription
|
|
|
|
read_subscriber &Subscription
|
|
|
|
write_sub_mtx u16
|
|
|
|
read_sub_mtx u16
|
|
|
|
closed u16
|
2020-08-11 17:52:13 +02:00
|
|
|
pub:
|
2021-04-15 01:44:39 +02:00
|
|
|
cap u32 // queue length in #objects
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_channel<T>(n u32) &Channel {
|
2020-08-06 17:31:05 +02:00
|
|
|
st := sizeof(T)
|
2021-06-14 17:12:47 +02:00
|
|
|
if isreftype(T) {
|
|
|
|
return new_channel_st(n, st)
|
|
|
|
} else {
|
|
|
|
return new_channel_st_noscan(n, st)
|
|
|
|
}
|
2020-08-14 21:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_channel_st(n u32, st u32) &Channel {
|
2020-10-29 11:17:08 +01:00
|
|
|
wsem := if n > 0 { n } else { 1 }
|
|
|
|
rsem := if n > 0 { u32(0) } else { 1 }
|
2021-04-15 01:44:39 +02:00
|
|
|
rbuf := if n > 0 { unsafe { malloc(int(n * st)) } } else { &byte(0) }
|
2021-06-14 17:12:47 +02:00
|
|
|
sbuf := if n > 0 { vcalloc_noscan(int(n * 2)) } else { &byte(0) }
|
|
|
|
mut ch := Channel{
|
2020-08-06 17:31:05 +02:00
|
|
|
objsize: st
|
2020-08-11 17:52:13 +02:00
|
|
|
cap: n
|
2020-08-06 15:28:19 +02:00
|
|
|
write_free: n
|
|
|
|
read_avail: 0
|
2020-10-29 11:17:08 +01:00
|
|
|
ringbuf: rbuf
|
|
|
|
statusbuf: sbuf
|
2020-08-06 15:28:19 +02:00
|
|
|
write_subscriber: 0
|
|
|
|
read_subscriber: 0
|
|
|
|
}
|
2021-01-29 19:52:14 +01:00
|
|
|
ch.writesem.init(wsem)
|
|
|
|
ch.readsem.init(rsem)
|
|
|
|
ch.writesem_im.init(0)
|
|
|
|
ch.readsem_im.init(0)
|
2021-06-14 17:12:47 +02:00
|
|
|
return &ch
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_channel_st_noscan(n u32, st u32) &Channel {
|
|
|
|
$if gcboehm_opt ? {
|
|
|
|
wsem := if n > 0 { n } else { 1 }
|
|
|
|
rsem := if n > 0 { u32(0) } else { 1 }
|
|
|
|
rbuf := if n > 0 { unsafe { malloc_noscan(int(n * st)) } } else { &byte(0) }
|
|
|
|
sbuf := if n > 0 { vcalloc_noscan(int(n * 2)) } else { &byte(0) }
|
|
|
|
mut ch := Channel{
|
|
|
|
objsize: st
|
|
|
|
cap: n
|
|
|
|
write_free: n
|
|
|
|
read_avail: 0
|
|
|
|
ringbuf: rbuf
|
|
|
|
statusbuf: sbuf
|
|
|
|
write_subscriber: 0
|
|
|
|
read_subscriber: 0
|
|
|
|
}
|
|
|
|
ch.writesem.init(wsem)
|
|
|
|
ch.readsem.init(rsem)
|
|
|
|
ch.writesem_im.init(0)
|
|
|
|
ch.readsem_im.init(0)
|
|
|
|
return &ch
|
|
|
|
} $else {
|
|
|
|
return new_channel_st(n, st)
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:37:37 +02:00
|
|
|
pub fn (ch &Channel) auto_str(typename string) string {
|
|
|
|
return 'chan $typename{cap: $ch.cap, closed: $ch.closed}'
|
|
|
|
}
|
|
|
|
|
2020-08-10 06:22:20 +02:00
|
|
|
pub fn (mut ch Channel) close() {
|
2021-04-09 09:57:41 +02:00
|
|
|
open_val := u16(0)
|
|
|
|
if !C.atomic_compare_exchange_strong_u16(&ch.closed, &open_val, 1) {
|
|
|
|
return
|
|
|
|
}
|
2020-08-10 06:22:20 +02:00
|
|
|
mut nulladr := voidptr(0)
|
2021-04-15 01:44:39 +02:00
|
|
|
for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_written) }, &nulladr,
|
|
|
|
voidptr(-1)) {
|
2020-08-10 06:22:20 +02:00
|
|
|
nulladr = voidptr(0)
|
|
|
|
}
|
|
|
|
ch.readsem_im.post()
|
|
|
|
ch.readsem.post()
|
|
|
|
mut null16 := u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.read_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
|
|
|
if ch.read_subscriber != voidptr(0) {
|
|
|
|
ch.read_subscriber.sem.post()
|
|
|
|
}
|
|
|
|
C.atomic_store_u16(&ch.read_sub_mtx, u16(0))
|
|
|
|
null16 = u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.write_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
|
|
|
if ch.write_subscriber != voidptr(0) {
|
|
|
|
ch.write_subscriber.sem.post()
|
|
|
|
}
|
|
|
|
C.atomic_store_u16(&ch.write_sub_mtx, u16(0))
|
2021-01-06 21:19:40 +01:00
|
|
|
ch.writesem.post()
|
2021-04-09 22:40:35 +02:00
|
|
|
if ch.cap == 0 {
|
2021-04-15 01:44:39 +02:00
|
|
|
C.atomic_store_ptr(unsafe { &voidptr(&ch.read_adr) }, voidptr(0))
|
2021-04-09 22:40:35 +02:00
|
|
|
}
|
2021-01-06 21:19:40 +01:00
|
|
|
ch.writesem_im.post()
|
2020-08-10 06:22:20 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 17:52:13 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (mut ch Channel) len() int {
|
|
|
|
return int(C.atomic_load_u32(&ch.read_avail))
|
|
|
|
}
|
|
|
|
|
2021-01-22 08:37:29 +01:00
|
|
|
[inline]
|
|
|
|
pub fn (mut ch Channel) closed() bool {
|
|
|
|
return C.atomic_load_u16(&ch.closed) != 0
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:06:42 +02:00
|
|
|
[inline]
|
2020-08-06 15:28:19 +02:00
|
|
|
pub fn (mut ch Channel) push(src voidptr) {
|
2020-08-10 18:06:42 +02:00
|
|
|
if ch.try_push_priv(src, false) == .closed {
|
2020-08-10 06:22:20 +02:00
|
|
|
panic('push on closed channel')
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
2020-08-10 18:06:42 +02:00
|
|
|
[inline]
|
2020-09-05 01:36:20 +02:00
|
|
|
pub fn (mut ch Channel) try_push(src voidptr) ChanState {
|
2020-09-12 02:29:11 +02:00
|
|
|
return ch.try_push_priv(src, true)
|
2020-08-10 18:06:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 01:36:20 +02:00
|
|
|
fn (mut ch Channel) try_push_priv(src voidptr, no_block bool) ChanState {
|
2020-08-10 06:22:20 +02:00
|
|
|
if C.atomic_load_u16(&ch.closed) != 0 {
|
|
|
|
return .closed
|
|
|
|
}
|
2021-04-19 18:01:47 +02:00
|
|
|
spinloops_sem_, spinloops_ := if no_block { 1, 1 } else { sync.spinloops, sync.spinloops_sem }
|
2020-08-06 15:28:19 +02:00
|
|
|
mut have_swapped := false
|
|
|
|
for {
|
|
|
|
mut got_sem := false
|
2021-04-15 01:44:39 +02:00
|
|
|
mut wradr := C.atomic_load_ptr(unsafe { &voidptr(&ch.write_adr) })
|
2020-08-06 15:28:19 +02:00
|
|
|
for wradr != C.NULL {
|
2021-04-15 01:44:39 +02:00
|
|
|
if C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.write_adr) },
|
|
|
|
&wradr, voidptr(0))
|
|
|
|
{
|
2020-08-06 15:28:19 +02:00
|
|
|
// there is a reader waiting for us
|
|
|
|
unsafe { C.memcpy(wradr, src, ch.objsize) }
|
|
|
|
mut nulladr := voidptr(0)
|
2021-04-15 01:44:39 +02:00
|
|
|
for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_written) },
|
|
|
|
&nulladr, wradr) {
|
2020-08-06 15:28:19 +02:00
|
|
|
nulladr = voidptr(0)
|
|
|
|
}
|
|
|
|
ch.readsem_im.post()
|
2020-08-10 06:22:20 +02:00
|
|
|
return .success
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 17:52:13 +02:00
|
|
|
if no_block && ch.cap == 0 {
|
2020-08-10 06:22:20 +02:00
|
|
|
return .not_ready
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
// get token to read
|
|
|
|
for _ in 0 .. spinloops_sem_ {
|
|
|
|
if got_sem {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
got_sem = ch.writesem.try_wait()
|
|
|
|
}
|
|
|
|
if !got_sem {
|
2020-09-12 02:29:11 +02:00
|
|
|
if no_block {
|
|
|
|
return .not_ready
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
ch.writesem.wait()
|
|
|
|
}
|
2021-01-06 21:19:40 +01:00
|
|
|
if C.atomic_load_u16(&ch.closed) != 0 {
|
|
|
|
ch.writesem.post()
|
|
|
|
return .closed
|
|
|
|
}
|
2020-08-11 17:52:13 +02:00
|
|
|
if ch.cap == 0 {
|
2020-08-06 15:28:19 +02:00
|
|
|
// try to advertise current object as readable
|
|
|
|
mut read_in_progress := false
|
2021-04-15 01:44:39 +02:00
|
|
|
C.atomic_store_ptr(unsafe { &voidptr(&ch.read_adr) }, src)
|
|
|
|
wradr = C.atomic_load_ptr(unsafe { &voidptr(&ch.write_adr) })
|
2020-08-06 15:28:19 +02:00
|
|
|
if wradr != C.NULL {
|
|
|
|
mut src2 := src
|
2021-04-15 01:44:39 +02:00
|
|
|
if C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.read_adr) },
|
|
|
|
&src2, voidptr(0))
|
|
|
|
{
|
2020-08-06 15:28:19 +02:00
|
|
|
ch.writesem.post()
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
read_in_progress = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !read_in_progress {
|
|
|
|
mut null16 := u16(0)
|
2021-04-15 01:44:39 +02:00
|
|
|
for !C.atomic_compare_exchange_weak_u16(voidptr(&ch.read_sub_mtx), &null16,
|
|
|
|
u16(1)) {
|
2020-08-06 15:28:19 +02:00
|
|
|
null16 = u16(0)
|
|
|
|
}
|
|
|
|
if ch.read_subscriber != voidptr(0) {
|
|
|
|
ch.read_subscriber.sem.post()
|
|
|
|
}
|
|
|
|
C.atomic_store_u16(&ch.read_sub_mtx, u16(0))
|
|
|
|
}
|
|
|
|
mut src2 := src
|
|
|
|
for sp := u32(0); sp < spinloops_ || read_in_progress; sp++ {
|
2021-04-15 01:44:39 +02:00
|
|
|
if C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.adr_read) },
|
|
|
|
&src2, voidptr(0))
|
|
|
|
{
|
2020-08-06 15:28:19 +02:00
|
|
|
have_swapped = true
|
|
|
|
read_in_progress = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
src2 = src
|
|
|
|
}
|
|
|
|
mut got_im_sem := false
|
|
|
|
for sp := u32(0); sp < spinloops_sem_ || read_in_progress; sp++ {
|
|
|
|
got_im_sem = ch.writesem_im.try_wait()
|
|
|
|
if got_im_sem {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
if got_im_sem {
|
|
|
|
got_im_sem = false
|
|
|
|
} else {
|
|
|
|
ch.writesem_im.wait()
|
|
|
|
}
|
2021-01-06 21:19:40 +01:00
|
|
|
if C.atomic_load_u16(&ch.closed) != 0 {
|
2021-04-15 01:44:39 +02:00
|
|
|
if have_swapped
|
|
|
|
|| C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.adr_read) }, &src2, voidptr(0)) {
|
2021-01-06 21:19:40 +01:00
|
|
|
ch.writesem.post()
|
|
|
|
return .success
|
|
|
|
} else {
|
|
|
|
return .closed
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 01:44:39 +02:00
|
|
|
if have_swapped
|
|
|
|
|| C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.adr_read) }, &src2, voidptr(0)) {
|
2020-08-06 15:28:19 +02:00
|
|
|
ch.writesem.post()
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
// this semaphore was not for us - repost in
|
|
|
|
ch.writesem_im.post()
|
2020-08-10 06:22:20 +02:00
|
|
|
if src2 == voidptr(-1) {
|
|
|
|
ch.readsem.post()
|
|
|
|
return .closed
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
src2 = src
|
|
|
|
}
|
|
|
|
}
|
2020-08-10 06:22:20 +02:00
|
|
|
return .success
|
2020-08-06 15:28:19 +02:00
|
|
|
} else {
|
|
|
|
// buffered channel
|
|
|
|
mut space_in_queue := false
|
|
|
|
mut wr_free := C.atomic_load_u32(&ch.write_free)
|
|
|
|
for wr_free > 0 {
|
2021-04-15 01:44:39 +02:00
|
|
|
space_in_queue = C.atomic_compare_exchange_weak_u32(&ch.write_free, &wr_free,
|
|
|
|
wr_free - 1)
|
2020-08-06 15:28:19 +02:00
|
|
|
if space_in_queue {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if space_in_queue {
|
|
|
|
mut wr_idx := C.atomic_load_u32(&ch.buf_elem_write_idx)
|
|
|
|
for {
|
|
|
|
mut new_wr_idx := wr_idx + 1
|
2020-08-11 17:52:13 +02:00
|
|
|
for new_wr_idx >= ch.cap {
|
|
|
|
new_wr_idx -= ch.cap
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
2021-04-15 01:44:39 +02:00
|
|
|
if C.atomic_compare_exchange_strong_u32(&ch.buf_elem_write_idx, &wr_idx,
|
|
|
|
new_wr_idx)
|
|
|
|
{
|
2020-08-06 15:28:19 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut wr_ptr := ch.ringbuf
|
|
|
|
mut status_adr := ch.statusbuf
|
|
|
|
unsafe {
|
|
|
|
wr_ptr += wr_idx * ch.objsize
|
|
|
|
status_adr += wr_idx * sizeof(u16)
|
|
|
|
}
|
|
|
|
mut expected_status := u16(BufferElemStat.unused)
|
2021-04-19 14:38:48 +02:00
|
|
|
for !C.atomic_compare_exchange_weak_u16(status_adr, &expected_status,
|
|
|
|
u16(BufferElemStat.writing)) {
|
2020-08-06 15:28:19 +02:00
|
|
|
expected_status = u16(BufferElemStat.unused)
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(wr_ptr, src, ch.objsize)
|
|
|
|
}
|
2021-04-15 01:44:39 +02:00
|
|
|
C.atomic_store_u16(unsafe { &u16(status_adr) }, u16(BufferElemStat.written))
|
2020-12-18 23:02:29 +01:00
|
|
|
C.atomic_fetch_add_u32(&ch.read_avail, 1)
|
2020-08-06 15:28:19 +02:00
|
|
|
ch.readsem.post()
|
2020-12-18 23:02:29 +01:00
|
|
|
mut null16 := u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.read_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
|
|
|
if ch.read_subscriber != voidptr(0) {
|
|
|
|
ch.read_subscriber.sem.post()
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
2020-12-18 23:02:29 +01:00
|
|
|
C.atomic_store_u16(&ch.read_sub_mtx, u16(0))
|
2020-08-10 06:22:20 +02:00
|
|
|
return .success
|
2020-08-06 15:28:19 +02:00
|
|
|
} else {
|
2020-09-12 02:29:11 +02:00
|
|
|
if no_block {
|
|
|
|
return .not_ready
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
ch.writesem.post()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-06 21:19:40 +01:00
|
|
|
// we should not get here but the V compiler want's to see a return statement
|
2021-12-01 10:24:51 +01:00
|
|
|
panic('unknown `try_push_priv` state')
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
2020-08-10 18:06:42 +02:00
|
|
|
[inline]
|
2020-08-10 06:22:20 +02:00
|
|
|
pub fn (mut ch Channel) pop(dest voidptr) bool {
|
2020-08-10 18:06:42 +02:00
|
|
|
return ch.try_pop_priv(dest, false) == .success
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
2020-08-10 18:06:42 +02:00
|
|
|
[inline]
|
2020-09-05 01:36:20 +02:00
|
|
|
pub fn (mut ch Channel) try_pop(dest voidptr) ChanState {
|
2020-09-12 02:29:11 +02:00
|
|
|
return ch.try_pop_priv(dest, true)
|
2020-08-10 18:06:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 01:36:20 +02:00
|
|
|
fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
|
2021-04-19 18:01:47 +02:00
|
|
|
spinloops_sem_, spinloops_ := if no_block { 1, 1 } else { sync.spinloops, sync.spinloops_sem }
|
2020-08-06 15:28:19 +02:00
|
|
|
mut have_swapped := false
|
|
|
|
mut write_in_progress := false
|
|
|
|
for {
|
|
|
|
mut got_sem := false
|
2020-08-11 17:52:13 +02:00
|
|
|
if ch.cap == 0 {
|
2020-08-06 15:28:19 +02:00
|
|
|
// unbuffered channel - first see if a `push()` has adversized
|
2021-04-15 01:44:39 +02:00
|
|
|
mut rdadr := C.atomic_load_ptr(unsafe { &voidptr(&ch.read_adr) })
|
2020-08-06 15:28:19 +02:00
|
|
|
for rdadr != C.NULL {
|
2021-04-15 01:44:39 +02:00
|
|
|
if C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.read_adr) },
|
|
|
|
&rdadr, voidptr(0))
|
|
|
|
{
|
2020-08-06 15:28:19 +02:00
|
|
|
// there is a writer waiting for us
|
|
|
|
unsafe { C.memcpy(dest, rdadr, ch.objsize) }
|
|
|
|
mut nulladr := voidptr(0)
|
2021-04-15 01:44:39 +02:00
|
|
|
for !C.atomic_compare_exchange_weak_ptr(unsafe { &voidptr(&ch.adr_read) },
|
|
|
|
&nulladr, rdadr) {
|
2020-08-06 15:28:19 +02:00
|
|
|
nulladr = voidptr(0)
|
|
|
|
}
|
|
|
|
ch.writesem_im.post()
|
2020-08-10 06:22:20 +02:00
|
|
|
return .success
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if no_block {
|
2020-09-05 01:36:20 +02:00
|
|
|
if C.atomic_load_u16(&ch.closed) == 0 {
|
|
|
|
return .not_ready
|
|
|
|
} else {
|
|
|
|
return .closed
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// get token to read
|
|
|
|
for _ in 0 .. spinloops_sem_ {
|
|
|
|
if got_sem {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
got_sem = ch.readsem.try_wait()
|
|
|
|
}
|
|
|
|
if !got_sem {
|
|
|
|
if no_block {
|
2020-09-05 01:36:20 +02:00
|
|
|
if C.atomic_load_u16(&ch.closed) == 0 {
|
|
|
|
return .not_ready
|
|
|
|
} else {
|
|
|
|
return .closed
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
ch.readsem.wait()
|
|
|
|
}
|
2020-08-11 17:52:13 +02:00
|
|
|
if ch.cap > 0 {
|
2020-08-06 15:28:19 +02:00
|
|
|
// try to get buffer token
|
|
|
|
mut obj_in_queue := false
|
|
|
|
mut rd_avail := C.atomic_load_u32(&ch.read_avail)
|
|
|
|
for rd_avail > 0 {
|
2021-04-15 01:44:39 +02:00
|
|
|
obj_in_queue = C.atomic_compare_exchange_weak_u32(&ch.read_avail, &rd_avail,
|
|
|
|
rd_avail - 1)
|
2020-08-06 15:28:19 +02:00
|
|
|
if obj_in_queue {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if obj_in_queue {
|
|
|
|
mut rd_idx := C.atomic_load_u32(&ch.buf_elem_read_idx)
|
|
|
|
for {
|
|
|
|
mut new_rd_idx := rd_idx + 1
|
2020-08-11 17:52:13 +02:00
|
|
|
for new_rd_idx >= ch.cap {
|
|
|
|
new_rd_idx -= ch.cap
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
2021-04-15 01:44:39 +02:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&ch.buf_elem_read_idx, &rd_idx,
|
|
|
|
new_rd_idx)
|
|
|
|
{
|
2020-08-06 15:28:19 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut rd_ptr := ch.ringbuf
|
|
|
|
mut status_adr := ch.statusbuf
|
|
|
|
unsafe {
|
|
|
|
rd_ptr += rd_idx * ch.objsize
|
|
|
|
status_adr += rd_idx * sizeof(u16)
|
|
|
|
}
|
|
|
|
mut expected_status := u16(BufferElemStat.written)
|
2021-04-19 14:38:48 +02:00
|
|
|
for !C.atomic_compare_exchange_weak_u16(status_adr, &expected_status,
|
|
|
|
u16(BufferElemStat.reading)) {
|
2020-08-06 15:28:19 +02:00
|
|
|
expected_status = u16(BufferElemStat.written)
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(dest, rd_ptr, ch.objsize)
|
|
|
|
}
|
2021-04-15 01:44:39 +02:00
|
|
|
C.atomic_store_u16(unsafe { &u16(status_adr) }, u16(BufferElemStat.unused))
|
2020-12-18 23:02:29 +01:00
|
|
|
C.atomic_fetch_add_u32(&ch.write_free, 1)
|
2020-08-06 15:28:19 +02:00
|
|
|
ch.writesem.post()
|
2020-12-18 23:02:29 +01:00
|
|
|
mut null16 := u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.write_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
|
|
|
if ch.write_subscriber != voidptr(0) {
|
|
|
|
ch.write_subscriber.sem.post()
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
2020-12-18 23:02:29 +01:00
|
|
|
C.atomic_store_u16(&ch.write_sub_mtx, u16(0))
|
2020-08-10 06:22:20 +02:00
|
|
|
return .success
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// try to advertise `dest` as writable
|
2021-04-15 01:44:39 +02:00
|
|
|
C.atomic_store_ptr(unsafe { &voidptr(&ch.write_adr) }, dest)
|
2020-08-11 17:52:13 +02:00
|
|
|
if ch.cap == 0 {
|
2021-04-15 01:44:39 +02:00
|
|
|
mut rdadr := C.atomic_load_ptr(unsafe { &voidptr(&ch.read_adr) })
|
2020-08-06 15:28:19 +02:00
|
|
|
if rdadr != C.NULL {
|
|
|
|
mut dest2 := dest
|
2021-04-15 01:44:39 +02:00
|
|
|
if C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.write_adr) },
|
|
|
|
&dest2, voidptr(0))
|
|
|
|
{
|
2020-08-06 15:28:19 +02:00
|
|
|
ch.readsem.post()
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
write_in_progress = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 17:52:13 +02:00
|
|
|
if ch.cap == 0 && !write_in_progress {
|
2020-08-06 15:28:19 +02:00
|
|
|
mut null16 := u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.write_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
|
|
|
if ch.write_subscriber != voidptr(0) {
|
|
|
|
ch.write_subscriber.sem.post()
|
|
|
|
}
|
|
|
|
C.atomic_store_u16(&ch.write_sub_mtx, u16(0))
|
|
|
|
}
|
|
|
|
mut dest2 := dest
|
|
|
|
for sp := u32(0); sp < spinloops_ || write_in_progress; sp++ {
|
2021-04-15 01:44:39 +02:00
|
|
|
if C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.adr_written) },
|
|
|
|
&dest2, voidptr(0))
|
|
|
|
{
|
2020-08-06 15:28:19 +02:00
|
|
|
have_swapped = true
|
|
|
|
break
|
2020-08-10 06:22:20 +02:00
|
|
|
} else if dest2 == voidptr(-1) {
|
|
|
|
ch.readsem.post()
|
|
|
|
return .closed
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
dest2 = dest
|
|
|
|
}
|
|
|
|
mut got_im_sem := false
|
|
|
|
for sp := u32(0); sp < spinloops_sem_ || write_in_progress; sp++ {
|
|
|
|
got_im_sem = ch.readsem_im.try_wait()
|
|
|
|
if got_im_sem {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
if got_im_sem {
|
|
|
|
got_im_sem = false
|
|
|
|
} else {
|
|
|
|
ch.readsem_im.wait()
|
|
|
|
}
|
2021-04-15 01:44:39 +02:00
|
|
|
if have_swapped
|
|
|
|
|| C.atomic_compare_exchange_strong_ptr(unsafe { &voidptr(&ch.adr_written) }, &dest2, voidptr(0)) {
|
2020-08-06 15:28:19 +02:00
|
|
|
ch.readsem.post()
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
// this semaphore was not for us - repost in
|
|
|
|
ch.readsem_im.post()
|
2020-08-10 06:22:20 +02:00
|
|
|
if dest2 == voidptr(-1) {
|
|
|
|
ch.readsem.post()
|
|
|
|
return .closed
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
dest2 = dest
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 01:44:39 +02:00
|
|
|
break
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
2020-11-11 16:23:57 +01:00
|
|
|
return .success
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait `timeout` on any of `channels[i]` until one of them can push (`is_push[i] = true`) or pop (`is_push[i] = false`)
|
2021-06-22 11:17:44 +02:00
|
|
|
// object referenced by `objrefs[i]`. `timeout = time.infinite` means wait unlimited time. `timeout <= 0` means return
|
|
|
|
// immediately if no transaction can be performed without waiting.
|
2020-08-10 18:06:42 +02:00
|
|
|
// return value: the index of the channel on which a transaction has taken place
|
|
|
|
// -1 if waiting for a transaction has exceeded timeout
|
|
|
|
// -2 if all channels are closed
|
2020-08-06 15:28:19 +02:00
|
|
|
|
2020-08-06 17:31:05 +02:00
|
|
|
pub fn channel_select(mut channels []&Channel, dir []Direction, mut objrefs []voidptr, timeout time.Duration) int {
|
|
|
|
assert channels.len == dir.len
|
|
|
|
assert dir.len == objrefs.len
|
2020-08-06 15:28:19 +02:00
|
|
|
mut subscr := []Subscription{len: channels.len}
|
2021-02-13 00:47:37 +01:00
|
|
|
mut sem := unsafe { Semaphore{} }
|
2021-01-29 19:52:14 +01:00
|
|
|
sem.init(0)
|
2020-08-06 15:28:19 +02:00
|
|
|
for i, ch in channels {
|
2021-04-25 20:40:38 +02:00
|
|
|
subscr[i].sem = unsafe { &sem }
|
2020-08-06 17:31:05 +02:00
|
|
|
if dir[i] == .push {
|
2020-08-06 15:28:19 +02:00
|
|
|
mut null16 := u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.write_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
2021-04-25 20:40:38 +02:00
|
|
|
subscr[i].prev = unsafe { &ch.write_subscriber }
|
2020-10-05 13:08:30 +02:00
|
|
|
unsafe {
|
2021-09-28 09:28:04 +02:00
|
|
|
subscr[i].nxt = &Subscription(C.atomic_exchange_ptr(&voidptr(&ch.write_subscriber),
|
|
|
|
&subscr[i]))
|
2020-10-05 13:08:30 +02:00
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
if voidptr(subscr[i].nxt) != voidptr(0) {
|
2021-04-25 20:40:38 +02:00
|
|
|
subscr[i].nxt.prev = unsafe { &subscr[i].nxt }
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
C.atomic_store_u16(&ch.write_sub_mtx, u16(0))
|
|
|
|
} else {
|
|
|
|
mut null16 := u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.read_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
2021-04-25 20:40:38 +02:00
|
|
|
subscr[i].prev = unsafe { &ch.read_subscriber }
|
2020-10-05 13:08:30 +02:00
|
|
|
unsafe {
|
2021-09-28 09:28:04 +02:00
|
|
|
subscr[i].nxt = &Subscription(C.atomic_exchange_ptr(&voidptr(&ch.read_subscriber),
|
|
|
|
&subscr[i]))
|
2020-10-05 13:08:30 +02:00
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
if voidptr(subscr[i].nxt) != voidptr(0) {
|
2021-04-25 20:40:38 +02:00
|
|
|
subscr[i].nxt.prev = unsafe { &subscr[i].nxt }
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
C.atomic_store_u16(&ch.read_sub_mtx, u16(0))
|
|
|
|
}
|
|
|
|
}
|
2021-06-22 11:17:44 +02:00
|
|
|
stopwatch := if timeout == time.infinite || timeout <= 0 {
|
|
|
|
time.StopWatch{}
|
|
|
|
} else {
|
2021-07-20 10:17:08 +02:00
|
|
|
time.new_stopwatch()
|
2021-06-22 11:17:44 +02:00
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
mut event_idx := -1 // negative index means `timed out`
|
2021-04-15 01:44:39 +02:00
|
|
|
|
|
|
|
outer: for {
|
2020-08-06 15:28:19 +02:00
|
|
|
rnd := rand.u32_in_range(0, u32(channels.len))
|
2020-08-10 06:22:20 +02:00
|
|
|
mut num_closed := 0
|
2020-08-06 15:28:19 +02:00
|
|
|
for j, _ in channels {
|
|
|
|
mut i := j + int(rnd)
|
|
|
|
if i >= channels.len {
|
|
|
|
i -= channels.len
|
|
|
|
}
|
2020-08-06 17:31:05 +02:00
|
|
|
if dir[i] == .push {
|
2020-08-10 18:06:42 +02:00
|
|
|
stat := channels[i].try_push_priv(objrefs[i], true)
|
2020-08-10 06:22:20 +02:00
|
|
|
if stat == .success {
|
2020-08-06 15:28:19 +02:00
|
|
|
event_idx = i
|
2021-02-03 15:19:42 +01:00
|
|
|
break outer
|
2020-08-10 06:22:20 +02:00
|
|
|
} else if stat == .closed {
|
|
|
|
num_closed++
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-10 18:06:42 +02:00
|
|
|
stat := channels[i].try_pop_priv(objrefs[i], true)
|
2020-08-10 06:22:20 +02:00
|
|
|
if stat == .success {
|
2020-08-06 15:28:19 +02:00
|
|
|
event_idx = i
|
2021-02-03 15:19:42 +01:00
|
|
|
break outer
|
2020-08-10 06:22:20 +02:00
|
|
|
} else if stat == .closed {
|
|
|
|
num_closed++
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-10 06:22:20 +02:00
|
|
|
if num_closed == channels.len {
|
|
|
|
event_idx = -2
|
2021-02-03 15:19:42 +01:00
|
|
|
break outer
|
2020-08-10 06:22:20 +02:00
|
|
|
}
|
2021-06-22 11:17:44 +02:00
|
|
|
if timeout <= 0 {
|
2021-02-03 15:19:42 +01:00
|
|
|
break outer
|
2020-12-20 04:52:40 +01:00
|
|
|
}
|
2021-06-22 11:17:44 +02:00
|
|
|
if timeout != time.infinite {
|
2020-08-06 15:28:19 +02:00
|
|
|
remaining := timeout - stopwatch.elapsed()
|
|
|
|
if !sem.timed_wait(remaining) {
|
2021-02-03 15:19:42 +01:00
|
|
|
break outer
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sem.wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// reset subscribers
|
|
|
|
for i, ch in channels {
|
2020-08-06 17:31:05 +02:00
|
|
|
if dir[i] == .push {
|
2020-08-06 15:28:19 +02:00
|
|
|
mut null16 := u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.write_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
2020-12-20 04:52:40 +01:00
|
|
|
unsafe {
|
|
|
|
*subscr[i].prev = subscr[i].nxt
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
if subscr[i].nxt != 0 {
|
2020-12-20 04:52:40 +01:00
|
|
|
subscr[i].nxt.prev = subscr[i].prev
|
2020-08-06 15:28:19 +02:00
|
|
|
// just in case we have missed a semaphore during restore
|
|
|
|
subscr[i].nxt.sem.post()
|
|
|
|
}
|
|
|
|
C.atomic_store_u16(&ch.write_sub_mtx, u16(0))
|
|
|
|
} else {
|
|
|
|
mut null16 := u16(0)
|
|
|
|
for !C.atomic_compare_exchange_weak_u16(&ch.read_sub_mtx, &null16, u16(1)) {
|
|
|
|
null16 = u16(0)
|
|
|
|
}
|
2020-12-20 04:52:40 +01:00
|
|
|
unsafe {
|
|
|
|
*subscr[i].prev = subscr[i].nxt
|
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
if subscr[i].nxt != 0 {
|
2020-12-20 04:52:40 +01:00
|
|
|
subscr[i].nxt.prev = subscr[i].prev
|
2020-08-06 15:28:19 +02:00
|
|
|
subscr[i].nxt.sem.post()
|
|
|
|
}
|
|
|
|
C.atomic_store_u16(&ch.read_sub_mtx, u16(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sem.destroy()
|
|
|
|
return event_idx
|
|
|
|
}
|