Skip to content

Commit

Permalink
[tsl] Make sure that EigenEnvironment::Task is move-only
Browse files Browse the repository at this point in the history
Accidental copies of tasks might lead to undefined behavior, e.g. we can accidentally execute `delete` multiple times. Make sure that this can't happen by making task move-only.

PiperOrigin-RevId: 708519872
  • Loading branch information
ezhulenev authored and Google-ML-Automation committed Dec 21, 2024
1 parent d29d8ea commit dc7aaf8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions xla/tsl/platform/threadpool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,20 @@ struct EigenEnvironment {
using EnvThread = Thread;

struct TaskImpl {
std::function<void()> f;
std::function<void()> fn;
Context context;
uint64 trace_id;
};

struct Task {
Task() = default;

Task(std::function<void()> fn, Context context, uint64 trace_id)
: f(TaskImpl{std::move(fn), std::move(context), trace_id}) {}

Task(Task&&) = default;
Task& operator=(Task&&) = default;

std::optional<TaskImpl> f;
};

Expand Down Expand Up @@ -94,14 +102,14 @@ struct EigenEnvironment {
id = tracing::GetUniqueArg();
tracing::RecordEvent(tracing::EventCategory::kScheduleClosure, id);
}
return Task{TaskImpl{std::move(f), Context(ContextKind::kThread), id}};
return Task(std::move(f), Context(ContextKind::kThread), id);
}

void ExecuteTask(const Task& t) {
WithContext wc(t.f->context);
tracing::ScopedRegion region(tracing::EventCategory::kRunClosure,
t.f->trace_id);
t.f->f();
t.f->fn();
}
};

Expand Down

0 comments on commit dc7aaf8

Please sign in to comment.