chenBright commented on code in PR #3541:
URL: https://github.com/apache/brpc/pull/3541#discussion_r4015208068
##########
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:
fixed.
--
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]