Skip to content

Commit

Permalink
ssx: Update task local ptr
Browse files Browse the repository at this point in the history
Use task counter instead of the task pointer.
  • Loading branch information
Lazin committed May 13, 2024
1 parent 1acaf9d commit 835ab5b
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/v/ssx/include/ssx/task_local_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,18 @@ class task_local_ptr_invalidated : public std::runtime_error {
/// will trigger 'task_local_invalidated' exception.
template<class T>
class task_local {
static const seastar::task* get_current_task() {
auto task = seastar::engine().current_task();
if (task == nullptr) {
task = seastar::thread_impl::get()->waiting_task();
}
vassert(task != nullptr, "No current task");
return task;
static uint64_t get_current_task_cnt() {
return seastar::engine().get_sched_stats().tasks_processed;
}

public:
explicit task_local(const T& val)
: _val(val)
, _task(get_current_task()) {}
, _task_cnt(get_current_task_cnt()) {}

explicit task_local(T&& val)
: _val(val)
, _task(get_current_task()) {}
, _task_cnt(get_current_task_cnt()) {}

task_local() = delete;
~task_local() = default;
Expand Down Expand Up @@ -91,7 +86,7 @@ class task_local {
/// Check if the time slice is the same. Invalidate
/// the pointer if its not.
void check_time_slice() const {
if (get_current_task() != _task) {
if (get_current_task_cnt() != _task_cnt) {
// Opt-out from all checks if we don't have a valid pointer to
// the current task.
_val = std::nullopt;
Expand All @@ -105,8 +100,7 @@ class task_local {
}

mutable std::optional<T> _val;
// Non-owning ptr
const seastar::task* _task;
uint64_t _task_cnt;
};

template<class T, class... Args>
Expand Down

0 comments on commit 835ab5b

Please sign in to comment.