mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
builtin: add methods to builtin channels (#6303)
This commit is contained in:
@@ -76,12 +76,6 @@ enum Direction {
|
||||
push
|
||||
}
|
||||
|
||||
enum TransactionState {
|
||||
success
|
||||
not_ready // push()/pop() would have to wait, but no_block was requested
|
||||
closed
|
||||
}
|
||||
|
||||
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
|
||||
@@ -170,11 +164,11 @@ pub fn (mut ch Channel) push(src voidptr) {
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (mut ch Channel) try_push(src voidptr) TransactionState {
|
||||
pub fn (mut ch Channel) try_push(src voidptr) ChanState {
|
||||
return ch.try_push_priv(src, false)
|
||||
}
|
||||
|
||||
fn (mut ch Channel) try_push_priv(src voidptr, no_block bool) TransactionState {
|
||||
fn (mut ch Channel) try_push_priv(src voidptr, no_block bool) ChanState {
|
||||
if C.atomic_load_u16(&ch.closed) != 0 {
|
||||
return .closed
|
||||
}
|
||||
@@ -329,11 +323,11 @@ pub fn (mut ch Channel) pop(dest voidptr) bool {
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (mut ch Channel) try_pop(dest voidptr) TransactionState {
|
||||
pub fn (mut ch Channel) try_pop(dest voidptr) ChanState {
|
||||
return ch.try_pop_priv(dest, false)
|
||||
}
|
||||
|
||||
fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) TransactionState {
|
||||
fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
|
||||
spinloops_sem_, spinloops_ := if no_block { spinloops, spinloops_sem } else { 1, 1 }
|
||||
mut have_swapped := false
|
||||
mut write_in_progress := false
|
||||
@@ -355,7 +349,11 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) TransactionState {
|
||||
}
|
||||
}
|
||||
if no_block {
|
||||
return if C.atomic_load_u16(&ch.closed) == 0 { TransactionState.not_ready } else { TransactionState.closed }
|
||||
if C.atomic_load_u16(&ch.closed) == 0 {
|
||||
return .not_ready
|
||||
} else {
|
||||
return .closed
|
||||
}
|
||||
}
|
||||
}
|
||||
// get token to read
|
||||
@@ -367,7 +365,11 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) TransactionState {
|
||||
}
|
||||
if !got_sem {
|
||||
if no_block {
|
||||
return if C.atomic_load_u16(&ch.closed) == 0 { TransactionState.not_ready } else { TransactionState.closed }
|
||||
if C.atomic_load_u16(&ch.closed) == 0 {
|
||||
return .not_ready
|
||||
} else {
|
||||
return .closed
|
||||
}
|
||||
}
|
||||
ch.readsem.wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user