mirror of
https://github.com/openharmony/resourceschedule_ffrt.git
synced 2026-08-24 18:26:04 -04:00
fix: replace C++17 inline static data member with Meyers singleton in fiber
`static inline std::atomic_uint64_t idx{1}` declared inside the templated
`fiber` struct triggers "non-const static data member must be initialized
out of line" when job_utils.h is included by OHOS downstream consumers
(e.g. ability_runtime/test/fuzztest/extensionrecordmanagerc_fuzzer).
The OHOS prebuilt clang treats that construct under the C++14 rule
even with `-std=c++17` set, so the C++17 inline-variable escape hatch
is unavailable here. Because job_utils.h is a public header re-included
across the tree, the fix must live entirely in this file — flags
cannot be assumed uniform downstream.
Replace the data member with a private static accessor:
static std::atomic_uint64_t& next_idx()
{
static std::atomic_uint64_t counter{1};
return counter;
}
The function-local `static` initializer relies on C++11 magic statics
([stmt.dcl]/p4) for thread-safe first-call initialization, so the fix
no longer depends on C++17 inline variables at all. Each template
instantiation of `fiber<UsageId, FiberLocal, ThreadLocal>` still owns
its own counter (same as before), header-only is preserved, and the
single call site `c->id_ = idx.fetch_add(...)` becomes
`c->id_ = next_idx().fetch_add(...)`.
Also normalize the inline trailing comment to a full Doxygen block
matching the rest of the private section (e.g. `fiber_entry`), with a
`@brief` in third-person singular and an `@return` clause that documents
the function-local static.
Signed-off-by: chuchihtung <zhuzhidong2@huawei.com>
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -862,7 +862,7 @@ struct fiber : detail::non_copyable {
|
||||
}
|
||||
new(&c->fn) std::function<void()>(std::forward<std::function<void()>>(f));
|
||||
new(&c->local_) FiberLocal;
|
||||
c->id_ = idx.fetch_add(1, std::memory_order_relaxed);
|
||||
c->id_ = next_idx().fetch_add(1, std::memory_order_relaxed);
|
||||
FFRT_API_LOGD("fiber %llu create", c->id_);
|
||||
return c;
|
||||
}
|
||||
@@ -981,7 +981,16 @@ private:
|
||||
uint64_t id_; ///< Fiber identifier.
|
||||
FiberLocal local_; ///< Fiber-local storage.
|
||||
|
||||
static inline std::atomic_uint64_t idx{1}; ///< Atomic counter for generating unique fiber IDs.
|
||||
/**
|
||||
* @brief Gets the atomic counter used to assign unique fiber IDs.
|
||||
*
|
||||
* @return Reference to the process-wide counter, initialized to 1.
|
||||
*/
|
||||
static std::atomic_uint64_t& next_idx()
|
||||
{
|
||||
static std::atomic_uint64_t counter{1};
|
||||
return counter;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user