From 82482167ce33a77ce8d63ee4cfd0e88774b5101d Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Wed, 3 Feb 2021 14:19:42 +0000 Subject: [PATCH] vlib: replace all `goto` statements with labelled break (#8531) --- vlib/encoding/utf8/utf8.v | 2 -- vlib/sync/channels.v | 12 ++++++------ vlib/sync/sync_macos.c.v | 10 +++++----- vlib/sync/sync_windows.c.v | 10 +++++----- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/vlib/encoding/utf8/utf8.v b/vlib/encoding/utf8/utf8.v index 83f233116a..2a2fcc642d 100644 --- a/vlib/encoding/utf8/utf8.v +++ b/vlib/encoding/utf8/utf8.v @@ -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 diff --git a/vlib/sync/channels.v b/vlib/sync/channels.v index a038fff1ec..e260989235 100644 --- a/vlib/sync/channels.v +++ b/vlib/sync/channels.v @@ -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 { diff --git a/vlib/sync/sync_macos.c.v b/vlib/sync/sync_macos.c.v index 1c93a7dfb4..8fda9dce3f 100644 --- a/vlib/sync/sync_macos.c.v +++ b/vlib/sync/sync_macos.c.v @@ -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 } diff --git a/vlib/sync/sync_windows.c.v b/vlib/sync/sync_windows.c.v index aa3a0ba5a7..e5f53ca9fb 100644 --- a/vlib/sync/sync_windows.c.v +++ b/vlib/sync/sync_windows.c.v @@ -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 }