refactor: make instance_id a incrementing integer instead of a point value
This commit is contained in:
parent
e75eb2d6cd
commit
e1eefa2ca9
2 changed files with 14 additions and 8 deletions
|
|
@ -46,13 +46,9 @@ private:
|
||||||
|
|
||||||
auto operator==(const handle &, const handle &) -> bool;
|
auto operator==(const handle &, const handle &) -> bool;
|
||||||
|
|
||||||
/// Stable opaque actor identity: the raw instance pointer. Two handles for
|
/// Stable numeric actor identity assigned at spawn (atomic incrementing
|
||||||
/// the same actor return the same value. Returns 0 for expired handles.
|
/// counter, starting at 1). Two handles for the same actor return the same
|
||||||
[[nodiscard]] inline auto instance_id(const handle &h) -> uintptr_t {
|
/// value. Returns 0 for a null or expired handle.
|
||||||
// NOLINTNEXTLINE(*-reinterpret-cast)
|
[[nodiscard]] auto instance_id(const handle &h) -> uintptr_t;
|
||||||
if (auto sp = handle_ref(h).lock())
|
|
||||||
return reinterpret_cast<uintptr_t>(sp.get());
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace thespian
|
} // namespace thespian
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,8 @@ using namespace std::chrono_literals;
|
||||||
|
|
||||||
namespace thespian {
|
namespace thespian {
|
||||||
|
|
||||||
|
static atomic<uintptr_t> next_instance_id{1}; // NOLINT(*-avoid-non-const-global-variables)
|
||||||
|
|
||||||
struct context_impl : context {
|
struct context_impl : context {
|
||||||
explicit context_impl(executor::context executor)
|
explicit context_impl(executor::context executor)
|
||||||
: executor{move(executor)} {}
|
: executor{move(executor)} {}
|
||||||
|
|
@ -226,6 +228,7 @@ struct instance : std::enable_shared_from_this<instance> {
|
||||||
instance(context_impl &ctx, behaviour b, exit_handler eh, string_view name,
|
instance(context_impl &ctx, behaviour b, exit_handler eh, string_view name,
|
||||||
env_t env)
|
env_t env)
|
||||||
: ctx(ctx), strand_(ctx.executor.create_strand()), name_{name},
|
: ctx(ctx), strand_(ctx.executor.create_strand()), name_{name},
|
||||||
|
instance_id_{next_instance_id.fetch_add(1, memory_order_relaxed)},
|
||||||
env_(move(env)) {
|
env_(move(env)) {
|
||||||
if (eh)
|
if (eh)
|
||||||
exit_handlers_.emplace_front(eh);
|
exit_handlers_.emplace_front(eh);
|
||||||
|
|
@ -491,6 +494,7 @@ struct instance : std::enable_shared_from_this<instance> {
|
||||||
context_impl &ctx;
|
context_impl &ctx;
|
||||||
executor::strand strand_;
|
executor::strand strand_;
|
||||||
string name_;
|
string name_;
|
||||||
|
uintptr_t instance_id_;
|
||||||
receiver receiver_;
|
receiver receiver_;
|
||||||
receiver exited_receiver_;
|
receiver exited_receiver_;
|
||||||
sync_receiver sync_receiver_;
|
sync_receiver sync_receiver_;
|
||||||
|
|
@ -596,6 +600,12 @@ auto context::spawn_link(behaviour b, exit_handler eh, string_view name)
|
||||||
return spawn_link(move(b), move(eh), name, env_t{});
|
return spawn_link(move(b), move(eh), name, env_t{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto instance_id(const handle &h) -> uintptr_t {
|
||||||
|
if (auto sp = handle_ref(h).lock())
|
||||||
|
return sp->instance_id_;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
auto env() -> env_t & { return private_call().env_; }
|
auto env() -> env_t & { return private_call().env_; }
|
||||||
auto self() -> handle { return make_handle(private_call().lifetime_); }
|
auto self() -> handle { return make_handle(private_call().lifetime_); }
|
||||||
auto self_ref() -> handle & { return private_call().self_ref_; }
|
auto self_ref() -> handle & { return private_call().self_ref_; }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue