Skip to content
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

Fix SetThreadName on Linux #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 8 additions & 5 deletions ajalibraries/ajabase/system/linux/threadimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ AJAThreadImpl::ThreadProcStatic(void* pThreadImplContext)
if (errno == 0) // theoretically gettid() cannot fail, so by extension syscall(SYS_gettid) can't either...?
pThreadImpl->mTid = myTid;

if (pThreadImpl->mThreadName.size() > 0)
pthread_setname_np(pThreadImpl->mThread, pThreadImpl->mThreadName.c_str());

// signal parent we've started
int rc = pthread_mutex_lock(&pThreadImpl->mStartMutex);
Expand Down Expand Up @@ -609,11 +611,12 @@ AJAThreadImpl::ThreadProcStatic(void* pThreadImplContext)
}

AJAStatus AJAThreadImpl::SetThreadName(const char *name) {
int ret = prctl(PR_SET_NAME, (unsigned long)name, 0, 0);
if(ret == -1) {
AJA_REPORT(0, AJA_DebugSeverity_Error, "Failed to set thread name to %s", name);
return AJA_STATUS_FAIL;
}
AJAAutoLock lock(&mLock);
mThreadName = name;
if (mThreadName.size() > 15)
mThreadName.resize(15);
if (mThread > 0)
pthread_setname_np(mThread, mThreadName.c_str());
return AJA_STATUS_SUCCESS;
}

Expand Down
3 changes: 3 additions & 0 deletions ajalibraries/ajabase/system/linux/threadimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ajabase/common/common.h"
#include "ajabase/system/thread.h"
#include "ajabase/system/lock.h"
#include <string>


class AJAThreadImpl
Expand Down Expand Up @@ -59,6 +60,8 @@ class AJAThreadImpl
bool mExiting;
pthread_mutex_t mExitMutex;
pthread_cond_t mExitCond;

std::string mThreadName;
};

#endif // AJA_THREAD_IMPL_H
Expand Down