vlib: replace all `goto` statements with labelled break (#8531)

pull/8536/head
Nick Treleaven 2021-02-03 14:19:42 +00:00 committed by GitHub
parent 7ec116d588
commit 82482167ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 18 deletions

View File

@ -32,7 +32,6 @@ fn (mut s Utf8State) seq(r0 bool, r1 bool, is_tail bool) bool {
s.subindex++
return true
}
goto next
} else {
s.failed = true
if is_tail {
@ -42,7 +41,6 @@ fn (mut s Utf8State) seq(r0 bool, r1 bool, is_tail bool) bool {
}
return true
}
next:
s.index++
s.subindex = 0
return false

View File

@ -563,6 +563,7 @@ pub fn channel_select(mut channels []&Channel, dir []Direction, mut objrefs []vo
}
stopwatch := if timeout <= 0 { time.StopWatch{} } else { time.new_stopwatch({}) }
mut event_idx := -1 // negative index means `timed out`
outer:
for {
rnd := rand.u32_in_range(0, u32(channels.len))
mut num_closed := 0
@ -575,7 +576,7 @@ pub fn channel_select(mut channels []&Channel, dir []Direction, mut objrefs []vo
stat := channels[i].try_push_priv(objrefs[i], true)
if stat == .success {
event_idx = i
goto restore
break outer
} else if stat == .closed {
num_closed++
}
@ -583,7 +584,7 @@ pub fn channel_select(mut channels []&Channel, dir []Direction, mut objrefs []vo
stat := channels[i].try_pop_priv(objrefs[i], true)
if stat == .success {
event_idx = i
goto restore
break outer
} else if stat == .closed {
num_closed++
}
@ -591,21 +592,20 @@ pub fn channel_select(mut channels []&Channel, dir []Direction, mut objrefs []vo
}
if num_closed == channels.len {
event_idx = -2
goto restore
break outer
}
if timeout == 0 {
goto restore
break outer
}
if timeout > 0 {
remaining := timeout - stopwatch.elapsed()
if !sem.timed_wait(remaining) {
goto restore
break outer
}
} else {
sem.wait()
}
}
restore:
// reset subscribers
for i, ch in channels {
if dir[i] == .push {

View File

@ -149,6 +149,7 @@ pub fn (mut sem Semaphore) wait() {
}
C.pthread_mutex_lock(&sem.mtx)
c = C.atomic_load_u32(&sem.count)
outer:
for {
if c == 0 {
C.pthread_cond_wait(&sem.cond, &sem.mtx)
@ -159,11 +160,10 @@ pub fn (mut sem Semaphore) wait() {
if c > 1 {
C.pthread_cond_signal(&sem.cond)
}
goto unlock
break outer
}
}
}
unlock:
C.pthread_mutex_unlock(&sem.mtx)
}
@ -184,11 +184,12 @@ pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool {
t_spec := timeout.timespec()
mut res := 0
c = C.atomic_load_u32(&sem.count)
outer:
for {
if c == 0 {
res = C.pthread_cond_timedwait(&sem.cond, &sem.mtx, &t_spec)
if res == C.ETIMEDOUT {
goto unlock
break outer
}
c = C.atomic_load_u32(&sem.count)
}
@ -197,11 +198,10 @@ pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool {
if c > 1 {
C.pthread_cond_signal(&sem.cond)
}
goto unlock
break outer
}
}
}
unlock:
C.pthread_mutex_unlock(&sem.mtx)
return res == 0
}

View File

@ -125,6 +125,7 @@ pub fn (mut sem Semaphore) wait() {
}
C.AcquireSRWLockExclusive(&sem.mtx)
c = C.atomic_load_u32(&sem.count)
outer:
for {
if c == 0 {
C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, C.INFINITE, 0)
@ -135,11 +136,10 @@ pub fn (mut sem Semaphore) wait() {
if c > 1 {
C.WakeConditionVariable(&sem.cond)
}
goto unlock
break outer
}
}
}
unlock:
C.ReleaseSRWLockExclusive(&sem.mtx)
}
@ -160,11 +160,12 @@ pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool {
t_ms := u32(timeout / time.millisecond)
mut res := 0
c = C.atomic_load_u32(&sem.count)
outer:
for {
if c == 0 {
res = C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, t_ms, 0)
if res == 0 {
goto unlock
break outer
}
c = C.atomic_load_u32(&sem.count)
}
@ -173,11 +174,10 @@ pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool {
if c > 1 {
C.WakeConditionVariable(&sem.cond)
}
goto unlock
break outer
}
}
}
unlock:
C.ReleaseSRWLockExclusive(&sem.mtx)
return res != 0
}