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

sync: don't force Mutex and Semaphore to be reference (#8331)

This commit is contained in:
Uwe Krüger
2021-01-29 19:52:14 +01:00
committed by GitHub
parent d370e4de9f
commit 4a955d9c54
18 changed files with 541 additions and 296 deletions

View File

@@ -66,7 +66,7 @@ enum BufferElemStat {
struct Subscription {
mut:
sem Semaphore
sem &Semaphore
prev &&Subscription
nxt &Subscription
}
@@ -77,14 +77,14 @@ enum Direction {
}
struct Channel {
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
ringbuf byteptr // queue for buffered channels
statusbuf byteptr // flags to synchronize write/read in ringbuf
objsize u32
mut: // atomic
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
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
@@ -113,11 +113,7 @@ fn new_channel_st(n u32, st u32) &Channel {
rsem := if n > 0 { u32(0) } else { 1 }
rbuf := if n > 0 { malloc(int(n * st)) } else { byteptr(0) }
sbuf := if n > 0 { vcalloc(int(n * 2)) } else { byteptr(0) }
return &Channel{
writesem: new_semaphore_init(wsem)
readsem: new_semaphore_init(rsem)
writesem_im: new_semaphore()
readsem_im: new_semaphore()
mut ch := &Channel{
objsize: st
cap: n
write_free: n
@@ -127,6 +123,11 @@ fn new_channel_st(n u32, st u32) &Channel {
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
}
pub fn (mut ch Channel) close() {
@@ -528,9 +529,10 @@ pub fn channel_select(mut channels []&Channel, dir []Direction, mut objrefs []vo
assert channels.len == dir.len
assert dir.len == objrefs.len
mut subscr := []Subscription{len: channels.len}
sem := new_semaphore()
mut sem := Semaphore{}
sem.init(0)
for i, ch in channels {
subscr[i].sem = sem
subscr[i].sem = &sem
if dir[i] == .push {
mut null16 := u16(0)
for !C.atomic_compare_exchange_weak_u16(&ch.write_sub_mtx, &null16, u16(1)) {