Skip to content

Commit

Permalink
libcore: Checking the current thread more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent a577531 commit 782e2f4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions doomsday/libs/core/include/de/concurrency/thread.h
Expand Up @@ -43,6 +43,7 @@ class DE_PUBLIC Thread : public Waitable
void terminate();
bool isRunning() const;
bool isFinished() const;
bool isCurrentThread() const;

virtual void run() = 0;

Expand Down
9 changes: 9 additions & 0 deletions doomsday/libs/core/src/concurrency/thread.cpp
Expand Up @@ -115,6 +115,15 @@ bool Thread::isFinished() const
return isFinished_Thread(d->thread);
}

bool Thread::isCurrentThread() const
{
if (d->thread)
{
return thrd_current() == id_Thread(d->thread);
}
return false;
}

void Thread::sleep(const TimeSpan &span) // static
{
sleep_Thread(span);
Expand Down
6 changes: 3 additions & 3 deletions doomsday/libs/core/src/core/app.cpp
Expand Up @@ -103,7 +103,7 @@ static Value *Function_App_Locate(Context &, Function::ArgumentValues const &arg
DE_PIMPL(App)
, DE_OBSERVES(PackageLoader, Activity)
{
iThread *mainThread = nullptr;
thrd_t mainThread = nullptr;

/// Metadata about the application.
Record metadata;
Expand Down Expand Up @@ -175,7 +175,7 @@ DE_PIMPL(App)
packagesToLoadAtInit << "net.dengine.stdlib";

singletonApp = a;
mainThread = current_Thread();
mainThread = thrd_current();

logBuffer.setEntryFilter(&logFilter);

Expand Down Expand Up @@ -556,7 +556,7 @@ bool App::inMainThread()
// No app even created yet, must be main thread.
return true;
}
return DE_APP->d->mainThread == current_Thread();
return DE_APP->d->mainThread == thrd_current();
}

#if !defined (DE_STATIC_LINK)
Expand Down
1 change: 1 addition & 0 deletions doomsday/libs/core/src/core/garbage.cpp
Expand Up @@ -176,6 +176,7 @@ void Garbage_TrashInstance(void *ptr, GarbageDestructor destructor)
if (ptr)
{
Garbage *g = garbageForThread(current_Thread());
DE_ASSERT(!Garbage_IsTrashed(ptr));
g->allocs[ptr] = destructor;
}
}
Expand Down

0 comments on commit 782e2f4

Please sign in to comment.