Skip to content

[SYCL] [NFC] Make instance variable in scheduler singleton #2286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,35 @@ EventImplPtr Scheduler::addCopyBack(Requirement *Req) {
// The init_priority here causes the constructor for scheduler to run relatively
// early, and therefore the destructor to run relatively late (after anything
// else that has no priority set, or has a priority higher than 2000).
Scheduler Scheduler::instance __attribute__((init_priority(2000)));
std::atomic<Scheduler *> Scheduler::instance(nullptr);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a more c++ way?

Suggested change
std::atomic<Scheduler *> Scheduler::instance(nullptr);
std::atomic<Scheduler *> Scheduler::instance{nullptr};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An even more C++ way?

std::atomic<Scheduler *> Scheduler::instance {};

#else
#pragma warning(disable : 4073)
#pragma init_seg(lib)
Scheduler Scheduler::instance;
std::atomic<Scheduler *> Scheduler::instance(nullptr);
#endif

Scheduler &Scheduler::getInstance() { return instance; }
Scheduler &Scheduler::getInstance() {
Scheduler *Ptr = instance;

if (!Ptr) {
Ptr = new Scheduler;
Scheduler *Expected = nullptr;

while (!instance.compare_exchange_weak(Expected, Ptr))
if (Expected) {
delete Ptr;

Ptr = Expected;
break;
}
}

return *Ptr;
}

void Scheduler::setInstance(Scheduler *Ptr) {
instance = Ptr;
}

std::vector<EventImplPtr> Scheduler::getWaitList(EventImplPtr Event) {
std::shared_lock<std::shared_timed_mutex> Lock(MGraphLock);
Expand Down
4 changes: 3 additions & 1 deletion sycl/source/detail/scheduler/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,9 @@ class Scheduler {

protected:
Scheduler();
static Scheduler instance;
static std::atomic<Scheduler *> instance;

static void setInstance(Scheduler *);

/// Provides exclusive access to std::shared_timed_mutex object with deadlock
/// avoidance
Expand Down