Copilot commented on code in PR #3541:
URL: https://github.com/apache/brpc/pull/3541#discussion_r4013972756


##########
src/butil/processor.h:
##########
@@ -0,0 +1,48 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#ifndef BUTIL_PROCESSOR_H
+#define BUTIL_PROCESSOR_H
+
+#include "butil/build_config.h"
+
+// Pause instruction to prevent excess processor bus usage, only works in GCC
+#ifndef cpu_relax
+#if defined(ARCH_CPU_ARM_FAMILY)
+#define cpu_relax() asm volatile("yield\n": : :"memory")
+#elif defined(ARCH_CPU_RISCV_FAMILY)
+// Use the pause hint (Zihintpause extension). Encoding 0x0100000F
+// (fence 0, 1) is a HINT on all RISC-V implementations: it never traps
+// and is ignored on CPUs without Zihintpause. On CPUs with Zihintpause
+// it provides a multi-cycle stall hint that reduces power and improves
+// resource fairness during spin-wait loops. Matches the Linux kernel's
+// RISC-V cpu_relax() behavior. .word is used instead of .insn or the
+// pause mnemonic for maximum assembler compatibility.
+# define cpu_relax() asm volatile(".word 0x0100000f\n": : :"memory")
+#elif defined(ARCH_CPU_LOONGARCH64_FAMILY)
+# define cpu_relax() asm volatile("nop\n": : :"memory");

Review Comment:
   The `cpu_relax()` macro for `ARCH_CPU_LOONGARCH64_FAMILY` includes a 
trailing semicolon inside the macro body. This can break valid call sites like 
`if (cond) cpu_relax(); else ...` (the extra `;` terminates the `if` body 
before the `else`). Remove the semicolon from the macro definition so the macro 
expands to a single statement consistently across architectures.



##########
src/bthread/task_group.h:
##########
@@ -237,66 +207,83 @@ friend class TaskControl;
 
     // Last scheduling time, task type and cumulated CPU time.
     class CPUTimeStat {
-        static constexpr int64_t LAST_SCHEDULING_TIME_MASK = 
0x7FFFFFFFFFFFFFFFLL;
-        static constexpr int64_t TASK_TYPE_MASK = 0x8000000000000000LL;
     public:
-        CPUTimeStat() : _last_run_ns_and_type(0), _cumulated_cputime_ns(0) {}
-        CPUTimeStat(AtomicInteger128::Value value)
-            : _last_run_ns_and_type(value.v1), _cumulated_cputime_ns(value.v2) 
{}
-
-        // Convert to AtomicInteger128::Value for atomic operations.
-        explicit operator AtomicInteger128::Value() const {
-            return {_last_run_ns_and_type, _cumulated_cputime_ns};
+        CPUTimeStat() : CPUTimeStat(0, 0, false) {}
+
+        CPUTimeStat(int64_t last_run_ns, int64_t cumulated_cputime_ns, bool 
main_task)
+            : _cumulated_cputime_ns(cumulated_cputime_ns)
+            , _last_run_ns(last_run_ns)
+            , _main_task(main_task) {}
+
+        CPUTimeStat(const CPUTimeStat& other)
+            : CPUTimeStat(other.last_run_ns(),
+                          other.cumulated_cputime_ns(),
+                          other.is_main_task()) {}
+
+        CPUTimeStat& operator=(const CPUTimeStat& other) {
+            if (this != &other) {
+                _last_run_ns.store(other.last_run_ns(),
+                                   butil::memory_order_relaxed);
+                _cumulated_cputime_ns.store(other.cumulated_cputime_ns(),
+                                            butil::memory_order_relaxed);
+                _main_task.store(other.is_main_task(), 
butil::memory_order_relaxed);
+            }
+            return *this;
         }
 
         void set_last_run_ns(int64_t last_run_ns, bool main_task) {
-            _last_run_ns_and_type = (last_run_ns & LAST_SCHEDULING_TIME_MASK) |
-                                    (static_cast<int64_t>(main_task) << 63);
+            _last_run_ns.store(last_run_ns, butil::memory_order_relaxed);
+            _main_task.store(main_task, butil::memory_order_relaxed);
         }
         int64_t last_run_ns() const {
-            return _last_run_ns_and_type & LAST_SCHEDULING_TIME_MASK;
-        }
-        int64_t last_run_ns_and_type() const {
-            return _last_run_ns_and_type;
+            return _last_run_ns.load(butil::memory_order_relaxed);
         }
 
         bool is_main_task() const {
-            return _last_run_ns_and_type & TASK_TYPE_MASK;
+            return _main_task.load(butil::memory_order_relaxed);
         }
 
         void add_cumulated_cputime_ns(int64_t cputime_ns, bool main_task) {
             if (main_task) {
                 return;
             }
-            _cumulated_cputime_ns += cputime_ns;
+
+            _cumulated_cputime_ns.store(cumulated_cputime_ns() + cputime_ns,
+                                       butil::memory_order_relaxed);

Review Comment:
   This performs an add via `load()+store()`, which is not an atomic 
read-modify-write. Even if the current design intends a single writer, using 
`fetch_add(..., memory_order_relaxed)` better matches the method semantics, 
avoids accidental lost updates if a second writer is ever introduced, and is 
clearer/less error-prone.



##########
test/Makefile:
##########
@@ -150,9 +150,10 @@ TEST_BUTIL_SOURCES = \
     scoped_locale.cc \
     popen_unittest.cpp \
     bounded_queue_unittest.cc \
-    butil_unittest_main.cpp \
-    scope_guard_unittest.cpp \
-    optional_unittest.cpp
+       scope_guard_unittest.cpp \
+       optional_unittest.cpp \
+       seqlock_unittest.cpp\

Review Comment:
   The new entries introduce inconsistent indentation (tabs vs spaces) and the 
continuation on `seqlock_unittest.cpp` is missing the conventional space before 
the trailing `\\`. While this likely still works, it makes the list easier to 
mis-edit (e.g., if the next line’s indentation changes, tokens can accidentally 
concatenate). Consider normalizing indentation to match surrounding lines and 
formatting as `seqlock_unittest.cpp \\`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to