Skip to content

Commit 792dd6f

Browse files
committed
refactor: 统一无限循环写法,while (true) 改为 for (;;)
1 parent 7091d50 commit 792dd6f

6 files changed

Lines changed: 15 additions & 15 deletions

File tree

taskflowlite/core/async_task.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ inline auto Executor::submit(T&& task, I first, S last)
10601060
}
10611061
} else if constexpr (repeat_async_task<T>) {
10621062
// repeat: 允许 Finished → Running 转换,但禁止 Running 并发提交
1063-
while (true) {
1063+
for (;;) {
10641064
auto cur = topo->m_state.load(std::memory_order_acquire);
10651065
if (cur == Topology::State::Running) [[unlikely]] {
10661066
throw Exception("AsyncTask Error: Task is already running.");
@@ -1135,7 +1135,7 @@ inline auto Runtime::submit(T&& task, I first, S last)
11351135
throw Exception("AsyncTask Error: Task is not in Idle state.");
11361136
}
11371137
} else if constexpr (repeat_async_task<T>) {
1138-
while (true) {
1138+
for (;;) {
11391139
auto cur = topo->m_state.load(std::memory_order_acquire);
11401140
if (cur == Topology::State::Running) [[unlikely]] {
11411141
throw Exception("AsyncTask Error: Task is already running.");

taskflowlite/core/executor.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// @file executor.hpp
1+
/// @file executor.hpp
22
/// @brief 任务调度器 —— Work-Stealing 并行执行引擎,框架唯一的 OS 线程持有者。
33
/// @author wicyn
44
/// @contact https://github.com/wicyn
@@ -612,7 +612,7 @@ inline void Executor::_spawn(std::size_t num_workers) {
612612
m_handler->on_start(wr);
613613

614614
Work* w = nullptr;
615-
while (true) {
615+
for (;;) {
616616
// 本地队列优先:LIFO 顺序,缓存命中率最高
617617
while (w) {
618618
try {
@@ -661,7 +661,7 @@ inline Work* Executor::_wait_for_work(Worker& wr) noexcept {
661661
std::size_t const yield_limit = nw * wr.m_adaptive_factor + wr.m_max_steals;
662662

663663
// 阶段一:窃取
664-
while (true) {
664+
for (;;) {
665665
Work* w = (vtm < nw)
666666
? m_workers[vtm].m_wslq.steal()
667667
: m_shared_buffers[vtm - nw].queue.steal();
@@ -977,7 +977,7 @@ inline void Executor::_process_dependent(Work* w, I first, S last, std::size_t&
977977

978978
auto& state = work->m_topology->m_state;
979979

980-
while (true) {
980+
for (;;) {
981981
// 1. 每次循环开头,直接获取内存中的最新状态
982982
auto target = state.load(std::memory_order_acquire);
983983

taskflowlite/core/free_stack.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class alignas(cache_line_size * 2) FreeStack : public Immovable<FreeStack> {
4848
/// @param p 待推入 chunk 起始地址; 必须满足 ChunkLink 的安全前提。
4949
TFL_FORCE_INLINE void push(void* p) noexcept {
5050
Tagged curr = m_head.load(std::memory_order_relaxed);
51-
while (true) {
51+
for (;;) {
5252
ChunkLink::store(p, curr.ptr);
5353
const Tagged next{p, curr.tag + 1};
5454
if (m_head.compare_exchange_weak(curr, next, std::memory_order_release, std::memory_order_relaxed)) {

taskflowlite/core/macros.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ using std::unreachable;
170170
__builtin_unreachable();
171171
# endif
172172
// Why: 即使编译器不支持上述扩展,无限空循环也构成合法 C++ UB,起到类似提示作用。
173-
while (true) {
173+
for (;;) {
174174
}
175175
}
176176
#endif

taskflowlite/core/notifier.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Notifier : Immovable<Notifier> {
127127

128128
std::uint64_t state = m_state.load(std::memory_order_seq_cst);
129129

130-
while (true) {
130+
for (;;) {
131131
// 目标轮次尚未到来:存在更早进入 prepare 的线程尚未完成
132132
if (std::int64_t((state & k_epoch_mask) - epoch) < 0) {
133133
std::this_thread::yield();
@@ -176,7 +176,7 @@ class Notifier : Immovable<Notifier> {
176176

177177
std::uint64_t state = m_state.load(std::memory_order_relaxed);
178178

179-
while (true) {
179+
for (;;) {
180180
if (std::int64_t((state & k_epoch_mask) - epoch) < 0) {
181181
std::this_thread::yield();
182182
state = m_state.load(std::memory_order_relaxed);
@@ -211,7 +211,7 @@ class Notifier : Immovable<Notifier> {
211211
std::atomic_thread_fence(std::memory_order_seq_cst);
212212
std::uint64_t state = m_state.load(std::memory_order_acquire);
213213

214-
while (true) {
214+
for (;;) {
215215
// 无等待者
216216
if ((state & k_stack_mask) == k_stack_mask && (state & k_prewaiter_mask) == 0) {
217217
return;
@@ -254,7 +254,7 @@ class Notifier : Immovable<Notifier> {
254254
std::atomic_thread_fence(std::memory_order_seq_cst);
255255
std::uint64_t state = m_state.load(std::memory_order_acquire);
256256

257-
while (true) {
257+
for (;;) {
258258
if ((state & k_stack_mask) == k_stack_mask && (state & k_prewaiter_mask) == 0) {
259259
return;
260260
}

taskflowlite/core/unordered_dense.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ class table : public std::conditional_t<is_map_v<T>, base_table_type_map<T>, bas
12721272
auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
12731273
auto bucket_idx = bucket_idx_from_hash(hash);
12741274

1275-
while (true) {
1275+
for (;;) {
12761276
auto* bucket = &at(m_buckets, bucket_idx);
12771277
if (dist_and_fingerprint == bucket->m_dist_and_fingerprint) {
12781278
if (m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
@@ -1317,7 +1317,7 @@ class table : public std::conditional_t<is_map_v<T>, base_table_type_map<T>, bas
13171317
bucket_idx = next(bucket_idx);
13181318
bucket = &at(m_buckets, bucket_idx);
13191319

1320-
while (true) {
1320+
for (;;) {
13211321
if (dist_and_fingerprint == bucket->m_dist_and_fingerprint) {
13221322
if (m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
13231323
return begin() + static_cast<difference_type>(bucket->m_value_idx);
@@ -1617,7 +1617,7 @@ class table : public std::conditional_t<is_map_v<T>, base_table_type_map<T>, bas
16171617
auto bucket_idx = bucket_idx_from_hash(hash);
16181618

16191619
bool key_found = false;
1620-
while (true) {
1620+
for (;;) {
16211621
auto const& bucket = at(m_buckets, bucket_idx);
16221622
if (dist_and_fingerprint > bucket.m_dist_and_fingerprint) {
16231623
break;

0 commit comments

Comments
 (0)